Skip to content

Commit 7ec9fb6

Browse files
committed
test-runner: add test for LoadFile and LoadFile2 protocols
This actually tests multiple things: - LoadFile and LoadFile2 wrappers work - (un)install_protocol_interface works - how our API is suited to create a custom implementation of an existing protocol definition - The workflow used in Linux loaders to enable the Linux EFI stub to load the initrd via the LOAD_FILE2 protocol. Further what I'm doing in this test, is already deployed in the wild: https://github.com/nix-community/lanzaboote/blob/b7f68a50e6902f28c07a9f8d41df76f4c0a9315b/rust/uefi/linux-bootloader/src/linux_loader.rs#L142
1 parent 737347c commit 7ec9fb6

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uefi-test-runner/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ publish = false
66
edition = "2021"
77

88
[dependencies]
9+
uefi-raw = { path = "../uefi-raw" }
910
uefi = { path = "../uefi", features = ["alloc", "global_allocator", "panic_handler", "logger", "qemu"] }
1011

1112
log.workspace = true

uefi-test-runner/src/proto/load.rs

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
use alloc::boxed::Box;
2+
use alloc::string::{String, ToString};
3+
use alloc::vec::Vec;
4+
use core::ffi::c_void;
5+
use core::pin::Pin;
6+
use core::ptr;
7+
use core::ptr::addr_of;
8+
use uefi::prelude::BootServices;
9+
use uefi::proto::device_path::build::DevicePathBuilder;
10+
use uefi::proto::media::load_file::{LoadFile, LoadFile2};
11+
use uefi::proto::BootPolicy;
12+
use uefi::{Guid, Handle};
13+
use uefi_raw::protocol::device_path::DevicePathProtocol;
14+
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
15+
use uefi_raw::Status;
16+
17+
unsafe extern "efiapi" fn raw_load_file(
18+
this: *mut LoadFile2Protocol,
19+
_file_path: *const DevicePathProtocol,
20+
_boot_policy: bool,
21+
buffer_size: *mut usize,
22+
buffer: *mut c_void,
23+
) -> Status {
24+
log::debug!("Called static extern \"efiapi\" `raw_load_file` glue function");
25+
let this = this.cast::<CustomLoadFile2Protocol>().as_ref().unwrap();
26+
this.load_file(buffer_size, buffer.cast())
27+
}
28+
29+
#[repr(C)]
30+
struct CustomLoadFile2Protocol {
31+
inner: LoadFile2Protocol,
32+
file_data: Vec<u8>,
33+
}
34+
35+
impl CustomLoadFile2Protocol {
36+
fn new(file_data: Vec<u8>) -> Pin<Box<Self>> {
37+
let inner = Self {
38+
inner: LoadFile2Protocol {
39+
load_file: raw_load_file,
40+
},
41+
file_data,
42+
};
43+
Box::pin(inner)
44+
}
45+
46+
fn load_file(&self, buf_len: *mut usize, buf: *mut c_void) -> Status {
47+
if buf.is_null() || unsafe { *buf_len } < self.file_data.len() {
48+
log::debug!("Returning buffer size");
49+
unsafe { *buf_len = self.file_data.len() };
50+
Status::BUFFER_TOO_SMALL
51+
} else {
52+
log::debug!("Writing file content to buffer");
53+
unsafe {
54+
ptr::copy_nonoverlapping(self.file_data.as_ptr(), buf.cast(), self.file_data.len());
55+
}
56+
Status::SUCCESS
57+
}
58+
}
59+
}
60+
61+
unsafe fn install_protocol(
62+
bt: &BootServices,
63+
handle: Handle,
64+
guid: Guid,
65+
protocol: &mut CustomLoadFile2Protocol,
66+
) {
67+
bt.install_protocol_interface(Some(handle), &guid, addr_of!(*protocol).cast())
68+
.unwrap();
69+
}
70+
71+
unsafe fn uninstall_protocol(
72+
bt: &BootServices,
73+
handle: Handle,
74+
guid: Guid,
75+
protocol: &mut CustomLoadFile2Protocol,
76+
) {
77+
bt.uninstall_protocol_interface(handle, &guid, addr_of!(*protocol).cast())
78+
.unwrap();
79+
}
80+
81+
/// This tests the LoadFile and LoadFile2 protocols. As this protocol is not
82+
/// implemented in OVMF for the default handle, we implement it manually using
83+
/// `install_protocol_interface`. Then, we load a file from our custom installed
84+
/// protocol leveraging our protocol abstraction.
85+
///
86+
/// the way we are implementing the LoadFile(2) protocol is roughly what certain
87+
/// Linux loaders do so that Linux can find its initrd [0, 1].
88+
///
89+
/// [0] https://github.com/u-boot/u-boot/commit/ec80b4735a593961fe701cc3a5d717d4739b0fd0#diff-1f940face4d1cf74f9d2324952759404d01ee0a81612b68afdcba6b49803bdbbR171
90+
/// [1] https://github.com/torvalds/linux/blob/ee9a43b7cfe2d8a3520335fea7d8ce71b8cabd9d/drivers/firmware/efi/libstub/efi-stub-helper.c#L550
91+
pub fn test(bt: &BootServices) {
92+
let image = bt.image_handle();
93+
94+
let load_data_msg = "Example file content.";
95+
let load_data = load_data_msg.to_string().into_bytes();
96+
let mut proto_load_file = CustomLoadFile2Protocol::new(load_data);
97+
// Get the ptr to the inner value, not the wrapping smart pointer type.
98+
let proto_load_file_ptr = proto_load_file.as_mut().get_mut();
99+
100+
// Install our custom protocol implementation as LoadFile and LoadFile2
101+
// protocol.
102+
unsafe {
103+
install_protocol(bt, image, LoadFileProtocol::GUID, proto_load_file_ptr);
104+
install_protocol(bt, image, LoadFile2Protocol::GUID, proto_load_file_ptr);
105+
}
106+
107+
let mut dvp_vec = Vec::new();
108+
let dvp = DevicePathBuilder::with_vec(&mut dvp_vec);
109+
let dvp = dvp.finalize().unwrap();
110+
111+
let mut opened_load_file_protocol = bt.open_protocol_exclusive::<LoadFile>(image).unwrap();
112+
let loadfile_file = opened_load_file_protocol
113+
.load_file(dvp, BootPolicy::BootSelection)
114+
.unwrap();
115+
let loadfile_file_string = String::from_utf8(loadfile_file.to_vec()).unwrap();
116+
117+
let mut opened_load_file2_protocol = bt.open_protocol_exclusive::<LoadFile2>(image).unwrap();
118+
let loadfile2_file = opened_load_file2_protocol.load_file(dvp).unwrap();
119+
let loadfile2_file_string = String::from_utf8(loadfile2_file.to_vec()).unwrap();
120+
121+
assert_eq!(load_data_msg, &loadfile_file_string);
122+
assert_eq!(load_data_msg, &loadfile2_file_string);
123+
124+
// Cleanup: Uninstall protocols again.
125+
drop(opened_load_file_protocol);
126+
drop(opened_load_file2_protocol);
127+
unsafe {
128+
uninstall_protocol(bt, image, LoadFileProtocol::GUID, proto_load_file_ptr);
129+
uninstall_protocol(bt, image, LoadFile2Protocol::GUID, proto_load_file_ptr);
130+
}
131+
}

uefi-test-runner/src/proto/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub fn test(st: &mut SystemTable<Boot>) {
1717
debug::test(bt);
1818
device_path::test(bt);
1919
driver::test(bt);
20+
load::test(bt);
2021
loaded_image::test(bt);
2122
media::test(bt);
2223
network::test(bt);
@@ -78,6 +79,7 @@ mod console;
7879
mod debug;
7980
mod device_path;
8081
mod driver;
82+
mod load;
8183
mod loaded_image;
8284
mod media;
8385
mod misc;

uefi/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ details of the new `system`/`boot`/`runtime` modules, and upcoming deprecations.
2121
This comes with some changes. Read below. We recommend to directly use the
2222
implementations instead of the traits.
2323
- Added `LoadFile` and `LoadFile2` which abstracts over the `LOAD_FILE` and
24-
`LOAD_FILE2` protocols.
24+
`LOAD_FILE2` protocols. The UEFI test runner includes an integration test
25+
that shows how Linux loaders can use this to implement the initrd loading
26+
mechanism used in Linux.
2527

2628
## Changed
2729
- **Breaking:** `uefi::helpers::init` no longer takes an argument.

0 commit comments

Comments
 (0)