• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 // TODO: replace with `trusty-log` crate once it is `no_std`-compatible
25 
26 use alloc::ffi::CString;
27 use alloc::format;
28 use core::ffi::c_uint;
29 use log::{LevelFilter, Log, Metadata, Record};
30 
31 use crate::init::lk_init_level;
32 use crate::LK_INIT_HOOK;
33 
34 use crate::sys::fflush;
35 use crate::sys::fputs;
36 use crate::sys::lk_stderr;
37 
38 static TRUSTY_LOGGER: TrustyKernelLogger = TrustyKernelLogger;
39 
40 pub struct TrustyKernelLogger;
41 
42 impl Log for TrustyKernelLogger {
enabled(&self, _metadata: &Metadata) -> bool43     fn enabled(&self, _metadata: &Metadata) -> bool {
44         true
45     }
46 
log(&self, record: &Record)47     fn log(&self, record: &Record) {
48         if self.enabled(record.metadata()) {
49             let cstr = CString::new(format!("{} - {}\n", record.level(), record.args())).unwrap();
50             // Safety:
51             // The pointer returned by `cstr.as_ptr()` is valid because the lifetime of the
52             // `CString` encompasses the lifetime of the unsafe block.
53             // `lk_stderr()` returns a FILE pointer that is valid or null.
54             unsafe { fputs(cstr.as_ptr(), lk_stderr()) };
55         }
56     }
57 
flush(&self)58     fn flush(&self) {
59         // Safety:
60         // `lk_stderr()` returns a FILE pointer that is valid or null.
61         unsafe { fflush(lk_stderr()) };
62     }
63 }
64 
kernel_log_init_func(_level: c_uint)65 extern "C" fn kernel_log_init_func(_level: c_uint) {
66     log::set_logger(&TRUSTY_LOGGER).unwrap();
67     // TODO: should be set based on LK_DEBUGLEVEL
68     log::set_max_level(LevelFilter::Trace);
69 }
70 
71 LK_INIT_HOOK!(kernel_log_init, kernel_log_init_func, lk_init_level::LK_INIT_LEVEL_HEAP);
72