Skip to content

Commit c748974

Browse files
committed
test: add load_image integration test
1 parent a3e551c commit c748974

File tree

1 file changed

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

1 file changed

+59
-5
lines changed

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
use alloc::string::ToString;
12
use uefi::proto::console::text::Output;
2-
use uefi::table::boot::{BootServices, SearchType};
3-
use uefi::Identify;
3+
use uefi::proto::device_path::text::{AllowShortcuts, DevicePathToText, DisplayOnly};
4+
use uefi::proto::loaded_image::LoadedImage;
5+
use uefi::table::boot::{BootServices, LoadImageSource, SearchType};
6+
use uefi::{CString16, Identify};
7+
8+
mod memory;
9+
mod misc;
410

511
pub fn test(bt: &BootServices) {
612
info!("Testing boot services");
713
memory::test(bt);
814
misc::test(bt);
915
test_locate_handle_buffer(bt);
16+
test_load_image(bt);
1017
}
1118

12-
mod memory;
13-
mod misc;
14-
1519
fn test_locate_handle_buffer(bt: &BootServices) {
1620
info!("Testing the `locate_handle_buffer` function");
1721

@@ -34,3 +38,53 @@ fn test_locate_handle_buffer(bt: &BootServices) {
3438
);
3539
}
3640
}
41+
42+
/// This test loads the "self image" again into memory using the `load_image`
43+
/// boot service function. The image is not started but just loaded into memory.
44+
///
45+
/// It transitively tests the protocols
46+
/// * `DevicePathToText`
47+
/// * `LoadedImage`
48+
/// which are required as helpers.
49+
fn test_load_image(bt: &BootServices) {
50+
info!("Testing the `load_image` function");
51+
52+
let loaded_image = bt
53+
.open_protocol_exclusive::<LoadedImage>(bt.image_handle())
54+
.expect("should open LoadedImage protocol");
55+
56+
let image_device_path = loaded_image.file_path().expect("should have file path");
57+
58+
let &device_path_to_text_handle = bt
59+
.locate_handle_buffer(SearchType::ByProtocol(&DevicePathToText::GUID))
60+
.expect("Should be able to loace handle for DevicePathToText")
61+
.first()
62+
.expect("should locate DevicePathToText protocol handle");
63+
64+
let device_path_to_text = bt
65+
.open_protocol_exclusive::<DevicePathToText>(device_path_to_text_handle)
66+
.expect("should open DevicePathToText protocol");
67+
68+
let device_path_text = device_path_to_text
69+
.convert_device_path_to_text(
70+
bt,
71+
image_device_path,
72+
DisplayOnly(true),
73+
AllowShortcuts(false),
74+
)
75+
.expect("should convert to human-readable path");
76+
let device_path_text = CString16::from(&*device_path_text);
77+
let device_path_text = device_path_text.to_string().to_uppercase();
78+
79+
// On x86_64, this will be `\EFI\BOOT\BOOTX64.EFI` for example.
80+
assert!(device_path_text.as_str().starts_with(r"\EFI\BOOT\BOOT"));
81+
assert!(device_path_text.as_str().ends_with(".EFI"));
82+
83+
let load_source = LoadImageSource::FromFilePath {
84+
file_path: image_device_path,
85+
from_boot_manager: false,
86+
};
87+
let _ = bt
88+
.load_image(bt.image_handle(), load_source)
89+
.expect("should load image");
90+
}

0 commit comments

Comments
 (0)