-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1331 from phip1611/uefi-std
uefi std: add example and add book chapter
- Loading branch information
Showing
12 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ members = [ | |
"uefi", | ||
"uefi-macros", | ||
"uefi-raw", | ||
"uefi-std-example", | ||
"uefi-test-runner", | ||
"xtask", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Combining Rust `std` with `uefi` | ||
|
||
## TL;DR | ||
|
||
In Mid-2024, we recommend to stick to our normal guide. Use this document as | ||
guide and outlook for the future of UEFI and Rust. | ||
|
||
## About | ||
|
||
Programs created with the `uefi` crate are typically created with `#![no_std]` | ||
and `#![no_main]`. A `#![no_std]` crate can use the `core` and `alloc` parts of | ||
Rust's standard library, but not `std`. A `#![no_main]` executable does not use | ||
the standard main entry point, and must define its own entry point; `uefi` | ||
provides the `#[entry]` macro for this purpose. | ||
|
||
Rust has added partial support for building UEFI executables without | ||
`#![no_std]` and `#![no_main]`, thus, the standard way. Some functionality | ||
requires a nightly toolchain, they are gated by the `uefi_std` feature (Rust | ||
language feature, not `uefi` crate feature). Follow the | ||
[tracking issue](https://github.com/rust-lang/rust/issues/100499) for details. | ||
|
||
## Code Example | ||
|
||
Please refer to [`<repo>/uefi-std-example`](/uefi-std-example/README.md) to | ||
see a specific example. The relevant `main.rs` looks as follows: | ||
|
||
```rust | ||
{{#include ../../../uefi-std-example/src/main.rs}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ pkgs.mkShell { | |
rustToolchain | ||
|
||
# Other | ||
mdbook | ||
yamlfmt | ||
which # used by "cargo xtask fmt" | ||
]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "uefi-std-example" | ||
version = "0.1.0" | ||
authors = ["The Rust OSDev team"] | ||
publish = false | ||
edition = "2021" | ||
|
||
[dependencies] | ||
# Attention: Don't activate the panic_handler feature, as it will clash with | ||
# the one coming from `std`. | ||
uefi = { path = "../uefi", features = ["alloc"], default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Minimal Rust App using `std` and `uefi` | ||
|
||
Minimal example of a "standard Rust application" that showcases how `uefi` can | ||
be utilized and enhance the developers experience, when `std` is available. | ||
|
||
For simplicity, this example is minimal and the documentation is focused on | ||
`x86_64-unknown-uefi`. However, it works similar for other supported UEFI | ||
platforms. | ||
|
||
## Build | ||
|
||
Build the app using | ||
`$ cargo +nightly build --target x86_64-unknown-uefi`. To build it from the root | ||
directory (the Cargo workspace), append `-p uefi-std-example`. | ||
|
||
## Run | ||
|
||
The resulting `.efi` file can be found in `target/x86_64-unknown-uefi/<debug|release>/uefi-std-example.efi`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Note: In Rust 1.82.0-nightly and before, the `uefi_std` feature is | ||
// required for accessing `std::os::uefi::env::*`. The other default | ||
// functionality doesn't need a nightly toolchain (with Rust 1.80 and later), | ||
// but with that limited functionality you - currently - also can't integrate | ||
// the `uefi` crate. | ||
#![feature(uefi_std)] | ||
|
||
use std::os::uefi as uefi_std; | ||
use uefi::runtime::ResetType; | ||
use uefi::{Handle, Status}; | ||
|
||
/// Performs the necessary setup code for the `uefi` crate. | ||
fn setup_uefi_crate() { | ||
let st = uefi_std::env::system_table(); | ||
let ih = uefi_std::env::image_handle(); | ||
|
||
// Mandatory setup code for `uefi` crate. | ||
unsafe { | ||
uefi::table::set_system_table(st.as_ptr().cast()); | ||
|
||
let ih = Handle::from_ptr(ih.as_ptr().cast()).unwrap(); | ||
uefi::boot::set_image_handle(ih); | ||
} | ||
} | ||
|
||
fn main() { | ||
println!("Hello World from uefi_std"); | ||
setup_uefi_crate(); | ||
println!("UEFI-Version is {}", uefi::system::uefi_revision()); | ||
uefi::runtime::reset(ResetType::SHUTDOWN, Status::SUCCESS, None); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters