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-raw / src / lib.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Raw UEFI types and bindings for protocols, boot, and runtime services. This |
| 4 | //! can serve as base for an UEFI firmware implementation or a high-level |
| 5 | //! wrapper to access UEFI functionality from an UEFI image. |
| 6 | //! |
| 7 | //! For creating UEFI applications and drivers, consider using the [`uefi`] |
| 8 | //! crate instead of `uefi-raw`. |
| 9 | //! |
| 10 | //! The types follow the UEFI specification as closely as possible and use raw |
| 11 | //! pointers, whose mutability reflects who writes through them and who owns |
| 12 | //! the pointee. See the [API guidelines] for the detailed rules. |
| 13 | //! |
| 14 | //! [API guidelines]: https://github.com/rust-osdev/uefi-rs/blob/main/uefi-raw/api_guidelines.md |
| 15 | //! [`uefi`]: https://crates.io/crates/uefi |
| 16 | |
| 17 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 18 | #![no_std] |
| 19 | #![deny( |
| 20 | clippy::all, |
| 21 | clippy::absolute_paths, |
| 22 | clippy::missing_const_for_fn, |
| 23 | clippy::must_use_candidate, |
| 24 | clippy::missing_safety_doc, |
| 25 | clippy::ptr_as_ptr, |
| 26 | clippy::ref_as_ptr, |
| 27 | clippy::undocumented_unsafe_blocks, |
| 28 | clippy::use_self, |
| 29 | missing_debug_implementations, |
| 30 | missing_docs, |
| 31 | unsafe_op_in_unsafe_fn, |
| 32 | unused |
| 33 | )] |
| 34 | |
| 35 | mod enums; |
| 36 | |
| 37 | pub mod capsule; |
| 38 | pub mod firmware_storage; |
| 39 | pub mod protocol; |
| 40 | pub mod table; |
| 41 | pub mod time; |
| 42 | |
| 43 | mod net; |
| 44 | mod status; |
| 45 | |
| 46 | use core::cmp; |
| 47 | pub use net::*; |
| 48 | pub use status::Status; |
| 49 | pub use uguid::{Guid, guid}; |
| 50 | |
| 51 | use core::ffi::c_void; |
| 52 | use core::hash::{Hash, Hasher}; |
| 53 | |
| 54 | /// Handle to an event structure. |
| 55 | pub type Event = *mut c_void; |
| 56 | |
| 57 | /// Handle to a UEFI entity (protocol, image, etc). |
| 58 | pub type Handle = *mut c_void; |
| 59 | |
| 60 | /// One-byte character. |
| 61 | /// |
| 62 | /// Most strings in UEFI use [`Char16`], but a few places use one-byte |
| 63 | /// characters. Unless otherwise noted, these are encoded as 8-bit ASCII using |
| 64 | /// the ISO-Latin-1 character set. |
| 65 | pub type Char8 = u8; |
| 66 | |
| 67 | /// Two-byte character. |
| 68 | /// |
| 69 | /// Unless otherwise noted, the encoding is UCS-2. The UCS-2 encoding was |
| 70 | /// defined by Unicode 2.1 and ISO/IEC 10646 standards, but is no longer part of |
| 71 | /// the modern Unicode standards. It is essentially UTF-16 without support for |
| 72 | /// surrogate pairs. |
| 73 | pub type Char16 = u16; |
| 74 | |
| 75 | /// Physical memory address. This is always a 64-bit value, regardless |
| 76 | /// of target platform. |
| 77 | pub type PhysicalAddress = u64; |
| 78 | |
| 79 | /// Virtual memory address. This is always a 64-bit value, regardless |
| 80 | /// of target platform. |
| 81 | pub type VirtualAddress = u64; |
| 82 | |
| 83 | /// ABI-compatible UEFI boolean. |
| 84 | /// |
| 85 | /// This is similar to a `bool`, but allows values other than 0 or 1 to be |
| 86 | /// stored without it being undefined behavior. |
| 87 | /// |
| 88 | /// Any non-zero value is treated as logical `true`. The comparison, ordering, |
| 89 | /// and hashing implementations follow that logical interpretation, so all |
| 90 | /// non-zero values compare equal and hash the same way. The original byte is |
| 91 | /// still preserved in the public field; compare the `.0` values directly when |
| 92 | /// the exact raw bit pattern matters. |
| 93 | #[derive(Copy, Clone, Debug, Default)] |
| 94 | #[repr(transparent)] |
| 95 | pub struct Boolean(pub u8); |
| 96 | |
| 97 | // Ensure ABI guarantees for Boolean, as promised in [0]. |
| 98 | // |
| 99 | // [0]: https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192 |
| 100 | const _: () = { |
| 101 | assert!(align_of::<Boolean>() == 1); |
| 102 | assert!(size_of::<Boolean>() == 1); |
| 103 | }; |
| 104 | |
| 105 | impl Boolean { |
| 106 | /// [`Boolean`] representing `true`. |
| 107 | /// |
| 108 | /// # Caution |
| 109 | /// |
| 110 | /// This is only one possible true bit pattern. In UEFI, every non-zero |
| 111 | /// bit pattern is treated as logical `true`. |
| 112 | pub const TRUE: Self = Self(1); |
| 113 | |
| 114 | /// [`Boolean`] representing `false`. |
| 115 | pub const FALSE: Self = Self(0); |
| 116 | |
| 117 | /// Const-compatible check for **logical equality**. |
| 118 | const fn eq_const(self, other: Self) -> bool { |
| 119 | match (self.0, other.0) { |
| 120 | (0, 0) => true, |
| 121 | (0, _) => false, |
| 122 | (_, 0) => false, |
| 123 | // We handle it as in C: Any bit-pattern != 0 equals true |
| 124 | (_, _) => true, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Returns whether the underlying value equals a Rust `true`. |
| 129 | #[must_use] |
| 130 | pub const fn is_true(self) -> bool { |
| 131 | Self::eq_const(self, Self::TRUE) |
| 132 | } |
| 133 | |
| 134 | /// Returns whether the underlying value equals a Rust `true`. |
| 135 | #[must_use] |
| 136 | pub const fn is_false(self) -> bool { |
| 137 | !self.is_true() |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl From<u8> for Boolean { |
| 142 | fn from(value: u8) -> Self { |
| 143 | Self(value) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl From<bool> for Boolean { |
| 148 | fn from(value: bool) -> Self { |
| 149 | match value { |
| 150 | true => Self(1), |
| 151 | false => Self(0), |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | impl From<Boolean> for bool { |
| 157 | #[expect(clippy::match_like_matches_macro)] |
| 158 | fn from(value: Boolean) -> Self { |
| 159 | // We handle it as in C: Any bit-pattern != 0 equals true |
| 160 | match value.0 { |
| 161 | 0 => false, |
| 162 | _ => true, |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | impl PartialEq for Boolean { |
| 168 | fn eq(&self, other: &Self) -> bool { |
| 169 | Self::eq_const(*self, *other) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | impl Eq for Boolean {} |
| 174 | |
| 175 | impl PartialOrd for Boolean { |
| 176 | fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { |
| 177 | Some(self.cmp(other)) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl Ord for Boolean { |
| 182 | fn cmp(&self, other: &Self) -> cmp::Ordering { |
| 183 | match (self.0, other.0) { |
| 184 | (0, 0) => cmp::Ordering::Equal, |
| 185 | (0, _) => cmp::Ordering::Less, |
| 186 | (_, 0) => cmp::Ordering::Greater, |
| 187 | // We handle it as in C: Any bit-pattern != 0 equals true |
| 188 | (_, _) => cmp::Ordering::Equal, |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl Hash for Boolean { |
| 194 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 195 | let seed = if self.0 == 0 { 0 } else { 1 }; |
| 196 | state.write_u8(seed); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #[cfg(test)] |
| 201 | mod tests { |
| 202 | use super::*; |
| 203 | |
| 204 | #[test] |
| 205 | /// Test the properties promised in [0] and convenient rusty conversions. |
| 206 | /// |
| 207 | /// [0] https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192 |
| 208 | fn test_boolean_conversions() { |
| 209 | assert_eq!(Boolean::from(true).0, 1); |
| 210 | assert_eq!(Boolean::from(false).0, 0); |
| 211 | assert_eq!(Boolean::TRUE.0, 1); |
| 212 | assert_eq!(Boolean::FALSE.0, 0); |
| 213 | assert!(!bool::from(Boolean(0b0))); |
| 214 | assert!(bool::from(Boolean(0b1))); |
| 215 | // We do it as in C: Every bit pattern not 0 is equal to true. |
| 216 | assert!(bool::from(Boolean(0b11111110))); |
| 217 | assert!(bool::from(Boolean(0b11111111))); |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn test_order() { |
| 222 | assert!(Boolean::FALSE < Boolean::TRUE); |
| 223 | assert!(Boolean::TRUE > Boolean::FALSE); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn test_equality() { |
| 228 | assert_eq!(Boolean(0), Boolean(0)); |
| 229 | assert_ne!(Boolean(0), Boolean(3)); |
| 230 | assert_ne!(Boolean(7), Boolean(0)); |
| 231 | assert_eq!(Boolean(1), Boolean(1)); |
| 232 | assert_eq!(Boolean(13), Boolean(7)); |
| 233 | } |
| 234 | |
| 235 | // Tests that hash impl matches equal impl |
| 236 | #[test] |
| 237 | fn test_hash() { |
| 238 | #[derive(Default)] |
| 239 | struct TestHasher(u64); |
| 240 | |
| 241 | impl Hasher for TestHasher { |
| 242 | fn finish(&self) -> u64 { |
| 243 | self.0 |
| 244 | } |
| 245 | |
| 246 | fn write(&mut self, bytes: &[u8]) { |
| 247 | for byte in bytes { |
| 248 | self.0 = self.0.wrapping_mul(257).wrapping_add(u64::from(*byte)); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | fn calculate_hash(value: Boolean) -> u64 { |
| 254 | let mut hasher = TestHasher::default(); |
| 255 | value.hash(&mut hasher); |
| 256 | hasher.finish() |
| 257 | } |
| 258 | |
| 259 | assert_eq!(calculate_hash(Boolean(1)), calculate_hash(Boolean(13))); |
| 260 | assert_eq!(calculate_hash(Boolean(13)), calculate_hash(Boolean(255))); |
| 261 | assert_ne!(calculate_hash(Boolean(0)), calculate_hash(Boolean(13))); |
| 262 | } |
| 263 | } |