uefi_raw/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Raw interface for working with UEFI.
4//!
5//! This crate is intended for implementing UEFI services. It is also used for
6//! implementing the [`uefi`] crate, which provides a safe wrapper around UEFI.
7//!
8//! For creating UEFI applications and drivers, consider using the [`uefi`]
9//! crate instead of `uefi-raw`.
10//!
11//! [`uefi`]: https://crates.io/crates/uefi
12
13#![no_std]
14#![cfg_attr(docsrs, feature(doc_cfg))]
15#![deny(
16    clippy::all,
17    clippy::missing_const_for_fn,
18    clippy::must_use_candidate,
19    clippy::ptr_as_ptr,
20    clippy::use_self,
21    missing_debug_implementations,
22    unused
23)]
24
25mod enums;
26
27pub mod capsule;
28pub mod firmware_storage;
29pub mod protocol;
30pub mod table;
31pub mod time;
32
33mod net;
34mod status;
35
36pub use net::*;
37pub use status::Status;
38pub use uguid::{Guid, guid};
39
40use core::ffi::c_void;
41
42/// Handle to an event structure.
43pub type Event = *mut c_void;
44
45/// Handle to a UEFI entity (protocol, image, etc).
46pub type Handle = *mut c_void;
47
48/// One-byte character.
49///
50/// Most strings in UEFI use [`Char16`], but a few places use one-byte
51/// characters. Unless otherwise noted, these are encoded as 8-bit ASCII using
52/// the ISO-Latin-1 character set.
53pub type Char8 = u8;
54
55/// Two-byte character.
56///
57/// Unless otherwise noted, the encoding is UCS-2. The UCS-2 encoding was
58/// defined by Unicode 2.1 and ISO/IEC 10646 standards, but is no longer part of
59/// the modern Unicode standards. It is essentially UTF-16 without support for
60/// surrogate pairs.
61pub type Char16 = u16;
62
63/// Physical memory address. This is always a 64-bit value, regardless
64/// of target platform.
65pub type PhysicalAddress = u64;
66
67/// Virtual memory address. This is always a 64-bit value, regardless
68/// of target platform.
69pub type VirtualAddress = u64;
70
71/// ABI-compatible UEFI boolean.
72///
73/// This is similar to a `bool`, but allows values other than 0 or 1 to be
74/// stored without it being undefined behavior.
75///
76/// Any non-zero value is treated as logically `true`.
77#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq, Hash)]
78#[repr(transparent)]
79pub struct Boolean(pub u8);
80
81impl Boolean {
82    /// [`Boolean`] representing `true`.
83    pub const TRUE: Self = Self(1);
84
85    /// [`Boolean`] representing `false`.
86    pub const FALSE: Self = Self(0);
87}
88
89impl From<bool> for Boolean {
90    fn from(value: bool) -> Self {
91        match value {
92            true => Self(1),
93            false => Self(0),
94        }
95    }
96}
97
98impl From<Boolean> for bool {
99    #[allow(clippy::match_like_matches_macro)]
100    fn from(value: Boolean) -> Self {
101        // We handle it as in C: Any bit-pattern != 0 equals true
102        match value.0 {
103            0 => false,
104            _ => true,
105        }
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    /// Test the properties promised in [0]. This also applies for the other
115    /// architectures.
116    ///
117    /// [0] https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192
118    fn test_boolean_abi() {
119        assert_eq!(size_of::<Boolean>(), 1);
120        assert_eq!(Boolean::from(true).0, 1);
121        assert_eq!(Boolean::from(false).0, 0);
122        assert_eq!(Boolean::TRUE.0, 1);
123        assert_eq!(Boolean::FALSE.0, 0);
124        assert!(!bool::from(Boolean(0b0)));
125        assert!(bool::from(Boolean(0b1)));
126        // We do it as in C: Every bit pattern not 0 is equal to true.
127        assert!(bool::from(Boolean(0b11111110)));
128        assert!(bool::from(Boolean(0b11111111)));
129    }
130}