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 / lib.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! # `uefi-rs` |
| 4 | //! |
| 5 | //! Rusty wrapper for the [Unified Extensible Firmware Interface][UEFI]. |
| 6 | //! |
| 7 | //! This crate makes it easy to develop Rust software that leverages **safe**, |
| 8 | //! **convenient**, and **performant** abstractions for [UEFI] functionality. |
| 9 | //! |
| 10 | //! See the [Rust UEFI Book] for a tutorial, how-tos, and overviews of some |
| 11 | //! important UEFI concepts. For more details of UEFI, see the latest [UEFI |
| 12 | //! Specification][spec]. |
| 13 | //! |
| 14 | //! # Minimal Example |
| 15 | //! |
| 16 | //! Minimal example for a UEFI application using functionality of the |
| 17 | //! `uefi` crate: |
| 18 | //! |
| 19 | //! ```no_run |
| 20 | //! #![no_main] |
| 21 | //! #![no_std] |
| 22 | //! |
| 23 | //! use uefi::prelude::*; |
| 24 | //! |
| 25 | //! #[entry] |
| 26 | //! fn main() -> Status { |
| 27 | //! uefi::helpers::init().unwrap(); |
| 28 | //! |
| 29 | //! Status::SUCCESS |
| 30 | //! } |
| 31 | //! # extern crate std; |
| 32 | //! ``` |
| 33 | //! |
| 34 | //! Please find more info in our [Rust UEFI Book]. |
| 35 | //! |
| 36 | //! # Value-add and Use Cases |
| 37 | //! |
| 38 | //! `uefi` supports writing code for both pre- and post-exit boot services |
| 39 | //! epochs, but its true strength shines when you create UEFI images that heavily |
| 40 | //! interact with UEFI boot services. Still, you have the flexibility to just |
| 41 | //! integrate selected types and abstractions into your project, for example to |
| 42 | //! parse the UEFI memory map. |
| 43 | //! |
| 44 | //! _Note that for producing UEFI images, you also need to use a corresponding |
| 45 | //! `uefi` compiler target of Rust, such as `x86_64-unknown-uefi`._ |
| 46 | //! |
| 47 | //! ## Example Use Cases |
| 48 | //! |
| 49 | //! This library significantly simplifies the process of creating **UEFI images** |
| 50 | //! by abstracting away much of the UEFI API complexity and by providing |
| 51 | //! convenient wrappers. When we mention UEFI images, we are talking about UEFI |
| 52 | //! applications, UEFI boot service drivers, and EFI runtime service drivers, |
| 53 | //! which typically have the `.efi` file extension. For instance, a UEFI |
| 54 | //! application could be an OS-specific loader, similar to _GRUB_ or _Limine_. |
| 55 | //! |
| 56 | //! Additionally, you can use this crate in non-UEFI images (such as a kernel |
| 57 | //! in ELF format) to perform tasks like parsing the UEFI memory map embedded in |
| 58 | //! the boot information provided by a bootloader. It also enables access to |
| 59 | //! UEFI runtime services from a non-UEFI image kernel. |
| 60 | //! |
| 61 | //! # Supported Compiler Versions and Architectures |
| 62 | //! |
| 63 | //! `uefi` works with stable Rust, but additional nightly-only features are |
| 64 | //! gated behind the `unstable` Cargo feature. Please find more information |
| 65 | //! about additional crate features below. |
| 66 | //! |
| 67 | //! `uefi` is compatible with all platforms that both the Rust compiler and |
| 68 | //! UEFI support, such as `i686`, `x86_64`, and `aarch64`. Please note that we |
| 69 | //! can't test all possible hardware/firmware/platform combinations in CI. |
| 70 | //! |
| 71 | //! ## MSRV |
| 72 | //! |
| 73 | //! The minimum supported Rust version is currently 1.91. |
| 74 | //! Our policy is to support at least the past two stable releases. |
| 75 | //! |
| 76 | //! # API/User Documentation, Documentation Structure, and other Resources |
| 77 | //! |
| 78 | //! Down below, you find typical technical documentation of all types, modules, |
| 79 | //! and functions exported by `uefi`. |
| 80 | //! |
| 81 | //! For a TL;DR quick start with an example on how to create your own EFI |
| 82 | //! application, please check out [the UEFI application template][template]. The |
| 83 | //! [Rust UEFI Book] is a more beginner-friendly tutorial with How-Tos, and |
| 84 | //! overviews of some important UEFI concepts and the abstractions provided by |
| 85 | //! this library. |
| 86 | //! |
| 87 | //! For more details of UEFI itself, see the latest [UEFI Specification][spec]. |
| 88 | //! |
| 89 | //! # Library Structure & Tips |
| 90 | //! |
| 91 | //! The top-level module contains some of the most used types and macros, |
| 92 | //! including the [`Handle`] and [`Result`] types, the [`CStr16`] and |
| 93 | //! [`CString16`] types for working with UCS-2 strings, and the [`entry`] and |
| 94 | //! [`guid`] macros. |
| 95 | //! |
| 96 | //! ## UEFI Strings |
| 97 | //! |
| 98 | //! Rust string literals are UTF-8 encoded and thus, not compatible with most |
| 99 | //! UEFI interfaces. We provide [`CStr16`] and [`CString16`] for proper working |
| 100 | //! with UCS-2 strings, including various transformation functions from standard |
| 101 | //! Rust strings. You can use [`cstr16!`] to create UCS-2 string literals at |
| 102 | //! compile time. |
| 103 | //! |
| 104 | //! ## Tables |
| 105 | //! |
| 106 | //! Most UEFI functionality comes from the system, boot, and runtime |
| 107 | //! tables. These can be accessed via the [`system`], [`boot`], and [`runtime`] |
| 108 | //! modules. |
| 109 | //! |
| 110 | //! ## Protocols |
| 111 | //! |
| 112 | //! When boot services are active, most functionality is provided via UEFI |
| 113 | //! protocols. Protocols provide operations such as reading and writing files, |
| 114 | //! drawing to the screen, sending and receiving network requests, and much |
| 115 | //! more. The list of protocols that are actually available when running an |
| 116 | //! application depends on the device. For example, a PC with no network card |
| 117 | //! may not provide network protocols. |
| 118 | //! |
| 119 | //! See the [`boot`] documentation for details of how to open a |
| 120 | //! protocol, and see the [`proto`] module for protocol implementations. New |
| 121 | //! protocols can be defined with the [`unsafe_protocol`] macro. |
| 122 | //! |
| 123 | //! ## Drivers |
| 124 | //! |
| 125 | //! UEFI defines a driver model which provides a modular framework for extending |
| 126 | //! the firmware's capabilities. The [`driver`] module provides a high-level |
| 127 | //! abstraction for implementing UEFI drivers in Rust. |
| 128 | //! |
| 129 | //! ## Optional Cargo crate features |
| 130 | //! |
| 131 | //! Recommended features follow below. |
| 132 | //! |
| 133 | //! ### Feature List |
| 134 | //! |
| 135 | //! - `alloc`: Enable functionality requiring the [`alloc`] crate from |
| 136 | //! the Rust standard library. For example, methods that return a |
| 137 | //! `Vec` rather than filling a statically-sized array. |
| 138 | //! We **highly recommend** activating this feature as more and more |
| 139 | //! functionality in `uefi-rs` depends and will depend on `alloc` for |
| 140 | //! convenience and good developer experience. |
| 141 | //! The `alloc` feature requires a global allocator: you can use the |
| 142 | //! `global_allocator` feature or provide your own. The global allocator and |
| 143 | //! the `alloc` feature are independent of internal direct usages of the |
| 144 | //! UEFI boot service allocator which may happen anyway, where necessary. |
| 145 | //! - `global_allocator`: Set [`allocator::Allocator`] as the global Rust |
| 146 | //! allocator. This is a simple allocator that relies on the UEFI pool |
| 147 | //! allocator. You can choose to provide your own allocator instead of |
| 148 | //! using this feature, or no allocator at all if you don't need to |
| 149 | //! dynamically allocate any memory. Note that even without that feature, |
| 150 | //! some code might use the internal UEFI allocator. |
| 151 | //! - `jiff02`: Integration of [`runtime::Time`] with the `jiff` crate |
| 152 | //! (version 0.2). Specifically, it integrates the time struct with |
| 153 | //! `DateTime` and `Zoned` via `TryFrom`. |
| 154 | //! - `logger`: Logging implementation for the standard [`log`] crate |
| 155 | //! that prints output to the UEFI console. No buffering is done; this |
| 156 | //! is not a high-performance logger. |
| 157 | //! - `log-debugcon`: Whether the logger set up by `logger` should also log |
| 158 | //! to the debugcon device (available in QEMU or Cloud Hypervisor on x86). |
| 159 | //! - `panic_handler`: Add a default panic handler that logs to `stdout`. |
| 160 | //! - `time03`: Integration of [`Time`][time-struct] with the `time` crate |
| 161 | //! (version 0.3). Specifically, it provides [`TryFrom`] integration with |
| 162 | //! `PrimitiveDateTime` and `OffsetDateTime` of the `time` crate. |
| 163 | //! - `unstable`: Enable functionality that depends on [unstable features] in |
| 164 | //! the Rust compiler (nightly version). |
| 165 | //! - `qemu`: Enable some code paths to adapt their execution when executed |
| 166 | //! in QEMU, such as using the special `qemu-exit` device when the panic |
| 167 | //! handler is called. |
| 168 | //! |
| 169 | //! Some of these features, such as the `logger` or `panic_handler` features, |
| 170 | //! only unfold their potential when you invoke `uefi::helpers::init` as soon |
| 171 | //! as possible in your application. |
| 172 | //! |
| 173 | //! ### Recommended Features |
| 174 | //! |
| 175 | //! In typical use-cases, the following features are useful for you: |
| 176 | //! - Building a UEFI image: |
| 177 | //! - Recommended: `alloc`, `global_allocator`, `logger`, `panic_handler` |
| 178 | //! - Optional: `log-debugcon`, `qemu`, `unstable` |
| 179 | //! - Building another application/library: |
| 180 | //! - Recommended: `alloc` |
| 181 | //! - Optional: `unstable` |
| 182 | //! |
| 183 | //! # Discuss and Contribute |
| 184 | //! |
| 185 | //! For general discussions, feel free to join us in our [Zulip] and ask |
| 186 | //! your questions there. |
| 187 | //! |
| 188 | //! Further, you can submit bugs and also ask questions in our [issue tracker]. |
| 189 | //! Contributions in the form of a PR are also highly welcome. Check our |
| 190 | //! [contributing guide][contributing] for details. |
| 191 | //! |
| 192 | //! # Comparison to other Projects in the Ecosystem |
| 193 | //! |
| 194 | //! ## Rust `std` implementation |
| 195 | //! |
| 196 | //! There is an ongoing effort for a [`std` implementation][rustc-uefi-std] of |
| 197 | //! the Rust standard library, which allows you to write UEFI programs that look |
| 198 | //! very similar to normal Rust programs running on top of an OS. |
| 199 | //! |
| 200 | //! It is still under development. You can track the progress in the |
| 201 | //! corresponding [tracking issue][uefi-std-tr-issue]. |
| 202 | //! |
| 203 | //! Using the `std` implementation simplifies the overall process of producing |
| 204 | //! the binary. For example, our [`#[entry]`][entry-macro] macro won't be |
| 205 | //! required any longer. As the `std` implementation evolves over time, you'll |
| 206 | //! need fewer and fewer abstractions of this crate. For everything not covered |
| 207 | //! by the `std` implementation, you can obtain relevant structures to work with |
| 208 | //! our crate via: |
| 209 | //! - `std::os::uefi::env::boot_services()` |
| 210 | //! - `std::os::uefi::env::get_system_handle()` |
| 211 | //! - `std::os::uefi::env::get_system_table()` |
| 212 | //! |
| 213 | //! ## `r-efi` |
| 214 | //! |
| 215 | //! [`r-efi`] provides Raw UEFI bindings without high-level convenience similar |
| 216 | //! to our `uefi-raw` crate, which is part of this project, but more |
| 217 | //! feature-complete. It targets a lower-level than our `uefi` crate does. |
| 218 | //! |
| 219 | //! # License |
| 220 | //! <!-- Keep in Sync with README! --> |
| 221 | //! |
| 222 | //! Licensed under either of [Apache License, Version 2.0][apache] or |
| 223 | //! [MIT license][mit] at your option. |
| 224 | //! |
| 225 | //! # UEFI and `uefi-rs` Terminology |
| 226 | //! |
| 227 | //! Please look into the [specification][spec] for a comprehensive overview. In |
| 228 | //! UEFI, both "EFI" and "UEFI" are often used interchangeably, such as |
| 229 | //! "UEFI image" or "EFI image". We prefer "UEFI" in our crate and its |
| 230 | //! documentation. |
| 231 | //! |
| 232 | //! ## Important UEFI Terms and Abstractions |
| 233 | //! |
| 234 | //! - **agent**: The entity (usually a driver or application) performing |
| 235 | //! operations on a handle or protocol. |
| 236 | //! - **controller**: A handle that represents a hardware device managed by |
| 237 | //! drivers. |
| 238 | //! - **device path**: A structured description of a device's location or |
| 239 | //! access method, used for booting and device identification. |
| 240 | //! - **driver binding**: A service that matches drivers to controllers and |
| 241 | //! manages their lifecycle. |
| 242 | //! - **image**: A loaded executable (e.g., UEFI application or driver) with its |
| 243 | //! own execution context - an `.efi` file (PE/COFF executable). |
| 244 | //! - **handle**: An opaque identifier representing a UEFI object to which |
| 245 | //! protocols can be attached. |
| 246 | //! - **protocol**: A standardized set of functions and data structures defining |
| 247 | //! an interface for services. |
| 248 | //! - **(protocol) interface**: A concrete implementation of a protocol attached |
| 249 | //! to a handle. |
| 250 | //! |
| 251 | //! [Rust UEFI Book]: https://rust-osdev.github.io/uefi-rs/HEAD/ |
| 252 | //! [UEFI]: https://uefi.org/ |
| 253 | //! [Zulip]: https://rust-osdev.zulipchat.com |
| 254 | //! [`GlobalAlloc`]: alloc::alloc::GlobalAlloc |
| 255 | //! [`cstr16!`]: crate::cstr16 |
| 256 | //! [`r-efi`]: https://crates.io/crates/r-efi |
| 257 | //! [`unsafe_protocol`]: proto::unsafe_protocol |
| 258 | //! [apache]: https://github.com/rust-osdev/uefi-rs/blob/main/uefi/LICENSE-APACHE |
| 259 | //! [contributing]: https://github.com/rust-osdev/uefi-rs/blob/main/CONTRIBUTING.md |
| 260 | //! [entry-macro]: uefi_macros::entry |
| 261 | //! [issue tracker]: https://github.com/rust-osdev/uefi-rs/issues |
| 262 | //! [mit]: https://github.com/rust-osdev/uefi-rs/blob/main/uefi/LICENSE-MIT |
| 263 | //! [rustc-uefi-std]: https://doc.rust-lang.org/nightly/rustc/platform-support/unknown-uefi.html |
| 264 | //! [spec]: https://uefi.org/specifications |
| 265 | //! [template]: https://github.com/rust-osdev/uefi-rs/tree/main/template |
| 266 | //! [time-struct]: crate::runtime::Time |
| 267 | //! [uefi-std-tr-issue]: https://github.com/rust-lang/rust/issues/100499 |
| 268 | //! [unstable features]: https://doc.rust-lang.org/unstable-book/ |
| 269 | |
| 270 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 271 | #![no_std] |
| 272 | #![deny( |
| 273 | clippy::all, |
| 274 | clippy::absolute_paths, |
| 275 | clippy::missing_const_for_fn, |
| 276 | clippy::must_use_candidate, |
| 277 | clippy::missing_safety_doc, |
| 278 | clippy::ptr_as_ptr, |
| 279 | clippy::ref_as_ptr, |
| 280 | clippy::undocumented_unsafe_blocks, |
| 281 | clippy::use_self, |
| 282 | missing_debug_implementations, |
| 283 | missing_docs, |
| 284 | unsafe_op_in_unsafe_fn, |
| 285 | unused |
| 286 | )] |
| 287 | |
| 288 | #[cfg(feature = "alloc")] |
| 289 | extern crate alloc; |
| 290 | // allow referring to self as ::uefi for macros to work universally (from this crate and from others) |
| 291 | // see https://github.com/rust-lang/rust/issues/54647 |
| 292 | extern crate self as uefi; |
| 293 | #[macro_use] |
| 294 | extern crate uefi_raw; |
| 295 | |
| 296 | #[macro_use] |
| 297 | pub mod data_types; |
| 298 | pub mod allocator; |
| 299 | pub mod boot; |
| 300 | #[cfg(feature = "alloc")] |
| 301 | pub mod driver; |
| 302 | #[cfg(feature = "alloc")] |
| 303 | pub mod fs; |
| 304 | pub mod helpers; |
| 305 | pub mod mem; |
| 306 | pub mod prelude; |
| 307 | pub mod proto; |
| 308 | pub mod runtime; |
| 309 | pub mod system; |
| 310 | pub mod table; |
| 311 | |
| 312 | pub(crate) mod polyfill; |
| 313 | |
| 314 | mod macros; |
| 315 | mod result; |
| 316 | mod util; |
| 317 | |
| 318 | #[cfg(feature = "alloc")] |
| 319 | pub use data_types::CString16; |
| 320 | pub use data_types::{CStr8, CStr16, Char8, Char16, Event, Guid, Handle, Identify}; |
| 321 | pub use result::{Error, Result, ResultExt, Status, StatusExt}; |
| 322 | /// Re-export ucs2_cstr so that it can be used in the implementation of the |
| 323 | /// cstr16 macro. It is hidden since it's not intended to be used directly. |
| 324 | #[doc(hidden)] |
| 325 | pub use ucs2::ucs2_cstr; |
| 326 | pub use uefi_macros::entry; |
| 327 | pub use uguid::guid; |