aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / system.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Functions for accessing fields of the system table. |
| 4 | //! |
| 5 | //! Some of these functions use a callback argument rather than returning a |
| 6 | //! reference to the field directly. This pattern is used because some fields |
| 7 | //! are allowed to change, and so a static lifetime cannot be used. |
| 8 | //! |
| 9 | //! Some functions can only be called while boot services are active, and will |
| 10 | //! panic otherwise. See each function's documentation for details. |
| 11 | |
| 12 | use crate::proto::console::text::{Input, Output}; |
| 13 | use crate::table::cfg::ConfigTableEntry; |
| 14 | use crate::table::{self, Revision}; |
| 15 | use crate::{CStr16, Char16}; |
| 16 | use core::slice; |
| 17 | |
| 18 | /// Get the firmware vendor string. |
| 19 | /// |
| 20 | /// Returns an empty string if the firmware does not provide a vendor string. |
| 21 | #[must_use] |
| 22 | pub fn firmware_vendor() -> &'static CStr16 { |
| 23 | let st = table::system_table_raw_panicking(); |
| 24 | // SAFETY: The system table pointer is live here. |
| 25 | let st = unsafe { st.as_ref() }; |
| 26 | |
| 27 | let vendor: *const Char16 = st.firmware_vendor.cast(); |
| 28 | // The spec does not allow a null pointer here, but do not let a |
| 29 | // non-conforming firmware turn a safe call into a null dereference. |
| 30 | if vendor.is_null() { |
| 31 | return crate::cstr16!(); |
| 32 | } |
| 33 | |
| 34 | // SAFETY: The pointer is not null, and this assumes that the firmware |
| 35 | // vendor string is never mutated or freed. |
| 36 | unsafe { CStr16::from_ptr(vendor) } |
| 37 | } |
| 38 | |
| 39 | /// Get the firmware revision. |
| 40 | #[must_use] |
| 41 | pub fn firmware_revision() -> u32 { |
| 42 | let st = table::system_table_raw_panicking(); |
| 43 | // SAFETY: The system table pointer is live here. |
| 44 | let st = unsafe { st.as_ref() }; |
| 45 | |
| 46 | st.firmware_revision |
| 47 | } |
| 48 | |
| 49 | /// Get the revision of the system table, which is defined to be the revision of |
| 50 | /// the UEFI specification implemented by the firmware. |
| 51 | #[must_use] |
| 52 | pub fn uefi_revision() -> Revision { |
| 53 | let st = table::system_table_raw_panicking(); |
| 54 | // SAFETY: The system table pointer is live here. |
| 55 | let st = unsafe { st.as_ref() }; |
| 56 | |
| 57 | st.header.revision |
| 58 | } |
| 59 | |
| 60 | /// Call `f` with a slice of [`ConfigTableEntry`]. Each entry provides access to |
| 61 | /// a vendor-specific table. |
| 62 | /// |
| 63 | /// # Example |
| 64 | /// |
| 65 | /// ```rust,no_run |
| 66 | /// use uefi::system::with_config_table; |
| 67 | /// use uefi::table::cfg::ConfigTableEntry; |
| 68 | /// |
| 69 | /// with_config_table(|slice| { |
| 70 | /// for i in slice { |
| 71 | /// match i.guid { |
| 72 | /// ConfigTableEntry::ACPI_GUID => println!("Found ACPI1"), |
| 73 | /// ConfigTableEntry::ACPI2_GUID => println!("Found ACPI2"), |
| 74 | /// guid => println!("Found {}", guid), |
| 75 | /// } |
| 76 | /// } |
| 77 | /// }); |
| 78 | /// ``` |
| 79 | /// |
| 80 | /// # Panics |
| 81 | /// |
| 82 | /// This function will panic if the firmware provides a misaligned |
| 83 | /// configuration table pointer. |
| 84 | pub fn with_config_table<F, R>(mut f: F) -> R |
| 85 | where |
| 86 | F: FnMut(&[ConfigTableEntry]) -> R, |
| 87 | { |
| 88 | let st = table::system_table_raw_panicking(); |
| 89 | // SAFETY: The system table pointer is live here. |
| 90 | let st = unsafe { st.as_ref() }; |
| 91 | |
| 92 | let ptr: *const ConfigTableEntry = st.configuration_table.cast(); |
| 93 | let len = st.number_of_configuration_table_entries; |
| 94 | let slice = if ptr.is_null() { |
| 95 | &[] |
| 96 | } else { |
| 97 | // A slice reference must be aligned. Fail loudly on a firmware bug |
| 98 | // rather than silently hiding all entries. |
| 99 | assert!(ptr.is_aligned(), "configuration table is misaligned"); |
| 100 | // SAFETY: The pointer is non-null, aligned, and valid for the |
| 101 | // requested slice length. |
| 102 | unsafe { slice::from_raw_parts(ptr, len) } |
| 103 | }; |
| 104 | |
| 105 | f(slice) |
| 106 | } |
| 107 | |
| 108 | /// Call `f` with the [`Input`] protocol attached to stdin. |
| 109 | /// |
| 110 | /// `f` must not call `with_stdin` again. Each call creates an exclusive |
| 111 | /// reference to the same protocol, and creating a second one while the |
| 112 | /// first is still in use is undefined behavior. |
| 113 | /// |
| 114 | /// # Panics |
| 115 | /// |
| 116 | /// This function will panic if called after exiting boot services, or if stdin |
| 117 | /// is not available. |
| 118 | pub fn with_stdin<F, R>(mut f: F) -> R |
| 119 | where |
| 120 | F: FnMut(&mut Input) -> R, |
| 121 | { |
| 122 | let st = table::system_table_raw_panicking(); |
| 123 | // SAFETY: valid per requirements of `set_system_table`. |
| 124 | let st = unsafe { st.as_ref() }; |
| 125 | // The I/O protocols cannot be used after exiting boot services. |
| 126 | assert!(!st.boot_services.is_null(), "boot services are not active"); |
| 127 | assert!(!st.stdin.is_null(), "stdin is not available"); |
| 128 | |
| 129 | let stdin: *mut Input = st.stdin.cast(); |
| 130 | |
| 131 | // SAFETY: The pointer is not null, aligned, and initialized. |
| 132 | let stdin = unsafe { &mut *stdin }; |
| 133 | |
| 134 | f(stdin) |
| 135 | } |
| 136 | |
| 137 | /// Call `f` with the [`Output`] protocol attached to stdout. |
| 138 | /// |
| 139 | /// `f` must not call `with_stdout` again, directly or through the `print!` |
| 140 | /// and `println!` macros, which use it. The same applies to the `log` |
| 141 | /// macros while the logger from [`helpers`] writes to stdout. Each call |
| 142 | /// creates an exclusive reference to the same protocol, and creating a |
| 143 | /// second one while the first is still in use is undefined behavior. |
| 144 | /// |
| 145 | /// # Panics |
| 146 | /// |
| 147 | /// This function will panic if called after exiting boot services, or if stdout |
| 148 | /// is not available. |
| 149 | /// |
| 150 | /// [`helpers`]: crate::helpers |
| 151 | pub fn with_stdout<F, R>(mut f: F) -> R |
| 152 | where |
| 153 | F: FnMut(&mut Output) -> R, |
| 154 | { |
| 155 | let st = table::system_table_raw_panicking(); |
| 156 | // SAFETY: The system table pointer is live here. |
| 157 | let st = unsafe { st.as_ref() }; |
| 158 | // The I/O protocols cannot be used after exiting boot services. |
| 159 | assert!(!st.boot_services.is_null(), "boot services are not active"); |
| 160 | assert!(!st.stdout.is_null(), "stdout is not available"); |
| 161 | |
| 162 | let stdout: *mut Output = st.stdout.cast(); |
| 163 | |
| 164 | // SAFETY: The pointer is not null, aligned, and initialized. |
| 165 | let stdout = unsafe { &mut *stdout }; |
| 166 | |
| 167 | f(stdout) |
| 168 | } |
| 169 | |
| 170 | /// Call `f` with the [`Output`] protocol attached to stderr. |
| 171 | /// |
| 172 | /// `f` must not call `with_stderr` again. Each call creates an exclusive |
| 173 | /// reference to the same protocol, and creating a second one while the |
| 174 | /// first is still in use is undefined behavior. |
| 175 | /// |
| 176 | /// # Panics |
| 177 | /// |
| 178 | /// This function will panic if called after exiting boot services, or if stderr |
| 179 | /// is not available. |
| 180 | pub fn with_stderr<F, R>(mut f: F) -> R |
| 181 | where |
| 182 | F: FnMut(&mut Output) -> R, |
| 183 | { |
| 184 | let st = table::system_table_raw_panicking(); |
| 185 | // SAFETY: The system table pointer is live here. |
| 186 | let st = unsafe { st.as_ref() }; |
| 187 | // The I/O protocols cannot be used after exiting boot services. |
| 188 | assert!(!st.boot_services.is_null(), "boot services are not active"); |
| 189 | assert!(!st.stderr.is_null(), "stderr is not available"); |
| 190 | |
| 191 | let stderr: *mut Output = st.stderr.cast(); |
| 192 | |
| 193 | // SAFETY: The pointer is not null, aligned, and initialized. |
| 194 | let stderr = unsafe { &mut *stderr }; |
| 195 | |
| 196 | f(stderr) |
| 197 | } |
| 198 | |
| 199 | #[cfg(test)] |
| 200 | mod tests { |
| 201 | use super::*; |
| 202 | |
| 203 | #[expect(dead_code)] |
| 204 | #[expect(clippy::assertions_on_constants)] |
| 205 | fn with_config_table_compile_test() { |
| 206 | assert!(false, "compile test only"); |
| 207 | |
| 208 | let mut acpi2_address = None; |
| 209 | |
| 210 | with_config_table(|slice| { |
| 211 | for i in slice { |
| 212 | if i.guid == ConfigTableEntry::ACPI2_GUID { |
| 213 | acpi2_address = Some(i.address); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | } |
| 219 | } |