Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/aienux/blob/main/vendor/uefi/src/helpers/logger.rs
# aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`aienux / vendor / uefi / src / helpers / logger.rs`
269 lines ยท 8921 bytes Raw Blame History
1 // SPDX-License-Identifier: MIT OR Apache-2.0
2
3 //! This optional feature adds support for the `log` crate, providing
4 //! a custom logger implementation which writes to a UEFI text output protocol.
5 //!
6 //! The main export of this module is the `Logger` structure,
7 //! which implements the `log` crate's trait `Log`.
8 //!
9 //! # Implementation Details
10 //!
11 //! The implementation is not the most efficient, since there is no buffering done,
12 //! and the messages have to be converted from UTF-8 to UEFI's UCS-2.
13 //!
14 //! The last part also means that some Unicode characters might not be
15 //! supported by the UEFI console. Don't expect emoji output support.
16
17 use crate::proto::console::text::Output;
18 use crate::system;
19 use core::fmt::{self, Write};
20 use core::ptr;
21 use core::sync::atomic::{AtomicPtr, Ordering};
22
23 /// Global logger object
24 static LOGGER: Logger = Logger::new();
25
26 /// Set up logging
27 ///
28 /// This is unsafe because you must arrange for the logger to be reset with
29 /// disable() on exit from UEFI boot services.
30 pub unsafe fn init() {
31 // Connect the logger to stdout.
32 system::with_stdout(|stdout| {
33 // SAFETY: `with_stdout` hands out a live stdout handle for this call.
34 unsafe {
35 LOGGER.set_output(stdout);
36 }
37 });
38
39 // Set the logger.
40 log::set_logger(&LOGGER).unwrap(); // Can only fail if already initialized.
41
42 // Set logger max level to level specified by log features
43 log::set_max_level(log::STATIC_MAX_LEVEL);
44 }
45
46 pub fn disable() {
47 LOGGER.disable();
48 }
49
50 /// Writer to the QEMU debugcon device and the debug-console of
51 /// cloud-hypervisor.
52 ///
53 /// More info:
54 #[cfg(all(
55 any(target_arch = "x86", target_arch = "x86_64"),
56 feature = "log-debugcon"
57 ))]
58 #[derive(Copy, Clone, Debug)]
59 struct DebugconWriter;
60
61 #[cfg(all(
62 any(target_arch = "x86", target_arch = "x86_64"),
63 feature = "log-debugcon"
64 ))]
65 impl DebugconWriter {
66 const IO_PORT: u16 = 0xe9;
67 }
68
69 #[cfg(all(
70 any(target_arch = "x86", target_arch = "x86_64"),
71 feature = "log-debugcon"
72 ))]
73 impl Write for DebugconWriter {
74 fn write_str(&mut self, s: &str) -> fmt::Result {
75 for &byte in s.as_bytes() {
76 // SAFETY: Port 0xe9 is a fixed debug output sink, and writing a byte
77 // has no aliasing or lifetime requirements.
78 unsafe {
79 core::arch::asm!("outb %al, %dx", in("al") byte, in("dx") Self::IO_PORT, options(att_syntax))
80 };
81 }
82 Ok(())
83 }
84 }
85
86 /// Logging implementation which writes to a UEFI output stream.
87 ///
88 /// If this logger is used as a global logger, you must disable it using the
89 /// `disable` method before exiting UEFI boot services in order to prevent
90 /// undefined behaviour from inadvertent logging.
91 #[derive(Debug)]
92 pub struct Logger {
93 writer: AtomicPtr