Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 83a0032

Browse files
committed
test: add load_image integration test
1 parent ffe0ad3 commit 83a0032

File tree

1 file changed

+75
-5
lines changed
  • uefi-test-runner/src/boot

1 file changed

+75
-5
lines changed

‎uefi-test-runner/src/boot/mod.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
use alloc::string::ToString;
12
use uefi::proto::console::text::Output;
2-
use uefi::table::boot::{BootServices, SearchType};
3+
use uefi::proto::device_path::{DevicePath, DeviceSubType, DeviceType, LoadedImageDevicePath};
4+
use uefi::table::boot::{BootServices, LoadImageSource, SearchType};
35
use uefi::table::{Boot, SystemTable};
4-
use uefi::Identify;
6+
use uefi::{CStr16, CString16, Identify};
7+
8+
mod memory;
9+
mod misc;
510

611
pub fn test(st: &SystemTable<Boot>) {
712
let bt = st.boot_services();
813
info!("Testing boot services");
914
memory::test(bt);
1015
misc::test(st);
1116
test_locate_handle_buffer(bt);
17+
test_load_image(bt);
1218
}
1319

14-
mod memory;
15-
mod misc;
16-
1720
fn test_locate_handle_buffer(bt: &BootServices) {
1821
info!("Testing the `locate_handle_buffer` function");
1922

@@ -36,3 +39,70 @@ fn test_locate_handle_buffer(bt: &BootServices) {
3639
);
3740
}
3841
}
42+
43+
/// This test loads the "self image" again into memory using the `load_image`
44+
/// boot service function. The image is not started but just loaded into memory.
45+
///
46+
/// It transitively tests the protocol [`LoadedImageDevicePath`] which is
47+
/// required as helper.
48+
fn test_load_image(bt: &BootServices) {
49+
info!("Testing the `load_image` function");
50+
51+
let image_device_path_protocol = bt
52+
.open_protocol_exclusive::<LoadedImageDevicePath>(bt.image_handle())
53+
.expect("should open LoadedImage protocol");
54+
55+
// Note: This is the full device path. The LoadedImage protocol would only
56+
// provide us with the file-path portion of the device path.
57+
let image_device_path: &DevicePath = &image_device_path_protocol;
58+
59+
// Get the file-path portion of the device path which is typically behind
60+
// device path node (0x4, 0x4).
61+
// TODO we need an abstraction in the UEFI crate for this.
62+
let image_device_path_file_path = image_device_path
63+
.node_iter()
64+
.find(|node| {
65+
node.device_type() == DeviceType::MEDIA /* 0x4 */
66+
&& node.sub_type() == DeviceSubType::HARDWARE_VENDOR /* 0x4 */
67+
})
68+
.map(|node| unsafe { CStr16::from_ptr(node.data().as_ptr().cast()) })
69+
// to Rust string
70+
.map(|cstr16| cstr16.to_string().to_uppercase())
71+
.expect("should have file-path portion in device path");
72+
73+
// On x86_64, this will be `\EFI\BOOT\BOOTX64.EFI` for example.
74+
assert!(image_device_path_file_path
75+
.as_str()
76+
.starts_with(r"\EFI\BOOT\BOOT"));
77+
assert!(image_device_path_file_path.as_str().ends_with(".EFI"));
78+
79+
// Variant A: FromBuffer
80+
{
81+
let mut fs = bt
82+
.get_image_file_system(bt.image_handle())
83+
.expect("should open file system");
84+
let path = CString16::try_from(image_device_path_file_path.as_str()).unwrap();
85+
let image_data = fs.read(&*path).expect("should read file content");
86+
let load_source = LoadImageSource::FromBuffer {
87+
buffer: image_data.as_slice(),
88+
file_path: None,
89+
};
90+
let _ = bt
91+
.load_image(bt.image_handle(), load_source)
92+
.expect("should load image");
93+
94+
log::debug!("load_image with FromBuffer strategy works");
95+
}
96+
// Variant B: FromFilePath
97+
{
98+
let load_source = LoadImageSource::FromFilePath {
99+
file_path: image_device_path,
100+
from_boot_manager: false,
101+
};
102+
let _ = bt
103+
.load_image(bt.image_handle(), load_source)
104+
.expect("should load image");
105+
106+
log::debug!("load_image with FromFilePath strategy works");
107+
}
108+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /