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_auto_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
25#[macro_use]
26mod enums;
27
28pub mod capsule;
29pub mod firmware_storage;
30pub mod protocol;
31pub mod table;
32pub mod time;
33
34mod status;
35
36use core::ffi::c_void;
37use core::fmt::{self, Debug, Formatter};
38pub use status::Status;
39pub use uguid::{guid, Guid};
40
41/// Handle to an event structure.
42pub type Event = *mut c_void;
43
44/// Handle to a UEFI entity (protocol, image, etc).
45pub type Handle = *mut c_void;
46
47/// One-byte character.
48///
49/// Most strings in UEFI use [`Char16`], but a few places use one-byte
50/// characters. Unless otherwise noted, these are encoded as 8-bit ASCII using
51/// the ISO-Latin-1 character set.
52pub type Char8 = u8;
53
54/// Two-byte character.
55///
56/// Unless otherwise noted, the encoding is UCS-2. The UCS-2 encoding was
57/// defined by Unicode 2.1 and ISO/IEC 10646 standards, but is no longer part of
58/// the modern Unicode standards. It is essentially UTF-16 without support for
59/// surrogate pairs.
60pub type Char16 = u16;
61
62/// Physical memory address. This is always a 64-bit value, regardless
63/// of target platform.
64pub type PhysicalAddress = u64;
65
66/// Virtual memory address. This is always a 64-bit value, regardless
67/// of target platform.
68pub type VirtualAddress = u64;
69
70/// An IPv4 internet protocol address.
71#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
72#[repr(transparent)]
73pub struct Ipv4Address(pub [u8; 4]);
74
75/// An IPv6 internet protocol address.
76#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
77#[repr(transparent)]
78pub struct Ipv6Address(pub [u8; 16]);
79
80/// An IPv4 or IPv6 internet protocol address.
81///
82/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
83/// type is defined in the same way as edk2 for compatibility with C code. Note
84/// that this is an untagged union, so there's no way to tell which type of
85/// address an `IpAddress` value contains without additional context.
86#[derive(Clone, Copy)]
87#[repr(C)]
88pub union IpAddress {
89    /// This member serves to align the whole type to a 4 bytes as required by
90    /// the spec. Note that this is slightly different from `repr(align(4))`,
91    /// which would prevent placing this type in a packed structure.
92    pub addr: [u32; 4],
93
94    /// An IPv4 internet protocol address.
95    pub v4: Ipv4Address,
96
97    /// An IPv6 internet protocol address.
98    pub v6: Ipv6Address,
99}
100
101impl IpAddress {
102    /// Construct a new IPv4 address.
103    #[must_use]
104    pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
105        Self {
106            v4: Ipv4Address(ip_addr),
107        }
108    }
109
110    /// Construct a new IPv6 address.
111    #[must_use]
112    pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
113        Self {
114            v6: Ipv6Address(ip_addr),
115        }
116    }
117}
118
119impl Debug for IpAddress {
120    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
121        // The type is an untagged union, so we don't know whether it contains
122        // an IPv4 or IPv6 address. It's also not safe to just print the whole
123        // 16 bytes, since they might not all be initialized.
124        f.debug_struct("IpAddress").finish()
125    }
126}
127
128impl Default for IpAddress {
129    fn default() -> Self {
130        Self { addr: [0u32; 4] }
131    }
132}
133
134/// A Media Access Control (MAC) address.
135#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
136#[repr(transparent)]
137pub struct MacAddress(pub [u8; 32]);