A framework for writing UEFI applications in Rust. It is intended to act like the Rust standard library on the UEFI platform with support for things like:
Vec
and String
via a custom allocatorprintln!
, write!
, format!
etc.Read
and Write
traits and the related typesIpAddr
and its supporting typesIn addition, it offers an ergonomic API for UEFI-specific functionality such as:
Thirdly it exposes an API for doing raw FFI with the UEFI platform. It's the same FFI API that is used to implement the above functionality.
WARNING: this crate is still a work in progress and the API surface can change without notice. Currently only x64
architecture is supported
To write a UEFI application using this framework follow the below steps:
cargo-xbuild
by running cargo install cargo-xbuild
rustup default nightly
cargo new my_efi_app
, where "myefiapp" is the name of the applicationefi = "0.2"
under [dependencies]
in Cargo.toml
my_efi_app/src/main.rs
. Comments in the code explain each part:```rust
// Externs for efi and alloc crates (alloc crate is the one that contains definitions of String and Vec etc.)
// EFI entrypoint or main function. UEFI firmware will call this function to start the application. // The signature and the name of this function must be exactly as below.
pub extern "win64" fn efimain(imagehandle: efi::ffi::EFIHANDLE, systable : *const efi::ffi::EFISYSTEMTABLE) -> isize { efi::initenv(imagehandle, systable); // Call to initenv must be the first thing in efi_main. Without it things like println!() won't work
println!("Welcome to UEFI");
// Your business logic here
0
}
// A handler to respond to panics in the code. Required by the Rust compiler
fn panic(_: &core::panic::PanicInfo) -> ! { loop {} }
// A handler to respond to allocation failures. Required by the Rust compiler
fn allocerror(: core::alloc::Layout) -> ! { loop {} } ```
cargo xbuild --target x86_64-unknown-uefi
my_efi_app.efi
will be found in target\x86_64-unknown-uefi\debug\
Load the applicationin qemu and run it via EFI shell. You will need the OVMF firmware for this. Google using ovmf in qemu
for details.
For a sample application see examples/sample_efi_app.rs
. Build it by running cargo xbuild --target x86_64-unknown-uefi --example sample_efi_app
. The resulting binary sample_efi_app.efi
will be found in target\x86_64-unknown-uefi\debug\examples\
.