Skip to content

Commit e3a1de2

Browse files
authored
Merge pull request #1410 from nicholasbishop/bishop-fix-null-interface
Fix handling of a null interface pointer in `boot::open_protocol`
2 parents cce789a + d2b7eac commit e3a1de2

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn test(st: &SystemTable<Boot>) {
1717
memory::test(bt);
1818
misc::test(st);
1919
test_locate_handles(bt);
20-
test_load_image(bt);
20+
test_load_image();
2121
}
2222

2323
fn test_locate_handles(bt: &BootServices) {
@@ -65,15 +65,15 @@ fn test_locate_handles(bt: &BootServices) {
6565
///
6666
/// It transitively tests the protocol [`LoadedImageDevicePath`] which is
6767
/// required as helper.
68-
fn test_load_image(bt: &BootServices) {
68+
fn test_load_image() {
6969
/// The path of the loaded image executing this integration test.
7070
const LOADED_IMAGE_PATH: &str = r"\EFI\BOOT\TEST_RUNNER.EFI";
7171

7272
info!("Testing the `load_image` function");
7373

74-
let image_device_path_protocol = bt
75-
.open_protocol_exclusive::<LoadedImageDevicePath>(bt.image_handle())
76-
.expect("should open LoadedImage protocol");
74+
let image_device_path_protocol =
75+
boot::open_protocol_exclusive::<LoadedImageDevicePath>(boot::image_handle())
76+
.expect("should open LoadedImage protocol");
7777

7878
// Note: This is the full device path. The LoadedImage protocol would only
7979
// provide us with the file-path portion of the device path.
@@ -95,7 +95,8 @@ fn test_load_image(bt: &BootServices) {
9595

9696
// Variant A: FromBuffer
9797
{
98-
let fs = boot::get_image_file_system(bt.image_handle()).expect("should open file system");
98+
let fs =
99+
boot::get_image_file_system(boot::image_handle()).expect("should open file system");
99100
let path = CString16::try_from(image_device_path_file_path.as_str()).unwrap();
100101
let image_data = FileSystem::new(fs)
101102
.read(&*path)
@@ -104,17 +105,17 @@ fn test_load_image(bt: &BootServices) {
104105
buffer: image_data.as_slice(),
105106
file_path: None,
106107
};
107-
let loaded_image = bt
108-
.load_image(bt.image_handle(), load_source)
109-
.expect("should load image");
108+
let loaded_image =
109+
boot::load_image(boot::image_handle(), load_source).expect("should load image");
110110

111111
log::debug!("load_image with FromBuffer strategy works");
112112

113113
// Check that the `LoadedImageDevicePath` protocol can be opened and
114114
// that the interface data is `None`.
115-
let loaded_image_device_path = bt
116-
.open_protocol_exclusive::<LoadedImageDevicePath>(loaded_image)
117-
.expect("should open LoadedImageDevicePath protocol");
115+
let loaded_image_device_path =
116+
boot::open_protocol_exclusive::<LoadedImageDevicePath>(loaded_image)
117+
.expect("should open LoadedImageDevicePath protocol");
118+
log::info!("bish 1");
118119
assert!(loaded_image_device_path.get().is_none());
119120
}
120121
// Variant B: FromDevicePath
@@ -123,9 +124,7 @@ fn test_load_image(bt: &BootServices) {
123124
device_path: image_device_path,
124125
boot_policy: BootPolicy::ExactMatch,
125126
};
126-
let _ = bt
127-
.load_image(bt.image_handle(), load_source)
128-
.expect("should load image");
127+
let _ = boot::load_image(boot::image_handle(), load_source).expect("should load image");
129128

130129
log::debug!("load_image with FromFilePath strategy works");
131130
}

uefi/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ details of the deprecated items that were removed in this release.
1010
- **Breaking:** `FileSystem` no longer has a lifetime parameter, and the
1111
deprecated conversion from `uefi::table::boot::ScopedProtocol` has been
1212
removed.
13+
- Fixed `boot::open_protocol` to properly handle a null interface pointer.
1314

1415

1516
# uefi - 0.32.0 (2024-09-09)

uefi/src/boot.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,16 @@ pub unsafe fn open_protocol<P: ProtocolPointer + ?Sized>(
969969
Handle::opt_to_ptr(params.controller),
970970
attributes as u32,
971971
)
972-
.to_result_with_val(|| ScopedProtocol {
973-
interface: NonNull::new(P::mut_ptr_from_ffi(interface)),
974-
open_params: params,
972+
.to_result_with_val(|| {
973+
let interface = if interface.is_null() {
974+
None
975+
} else {
976+
NonNull::new(P::mut_ptr_from_ffi(interface))
977+
};
978+
ScopedProtocol {
979+
interface,
980+
open_params: params,
981+
}
975982
})
976983
}
977984

0 commit comments

Comments
 (0)