Skip to content

Commit d4e46c3

Browse files
authored
Merge pull request #1310 from nicholasbishop/bishop-test-proto
boot: Add freestanding test_protocol
2 parents d44c314 + 3afc0b8 commit d4e46c3

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use uefi::boot::{self, OpenProtocolParams};
12
use uefi::prelude::*;
23
use uefi::proto::loaded_image::LoadedImage;
3-
use uefi::{boot, proto, Identify};
4+
use uefi::{proto, Identify};
45

56
pub fn test(st: &mut SystemTable<Boot>) {
67
info!("Testing various protocols");
@@ -11,6 +12,7 @@ pub fn test(st: &mut SystemTable<Boot>) {
1112
find_protocol(bt);
1213
test_protocols_per_handle(bt);
1314
test_protocols_per_handle_freestanding();
15+
test_test_protocol_freestanding();
1416

1517
debug::test(bt);
1618
device_path::test(bt);
@@ -63,6 +65,15 @@ fn test_protocols_per_handle_freestanding() {
6365
assert!(pph.iter().any(|guid| **guid == LoadedImage::GUID));
6466
}
6567

68+
fn test_test_protocol_freestanding() {
69+
assert!(boot::test_protocol::<LoadedImage>(OpenProtocolParams {
70+
handle: boot::image_handle(),
71+
agent: boot::image_handle(),
72+
controller: None,
73+
})
74+
.unwrap());
75+
}
76+
6677
mod console;
6778
mod debug;
6879
mod device_path;

uefi/src/boot.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::ops::{Deref, DerefMut};
1111
use core::ptr::{self, NonNull};
1212
use core::sync::atomic::{AtomicPtr, Ordering};
1313
use core::{mem, slice};
14-
use uefi::{table, Char16, Event, Guid, Handle, Result, Status, StatusExt};
14+
use uefi::{table, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
1515
use uefi_raw::table::boot::InterfaceType;
1616

1717
#[cfg(doc)]
@@ -612,6 +612,38 @@ pub fn open_protocol_exclusive<P: ProtocolPointer + ?Sized>(
612612
}
613613
}
614614

615+
/// Tests whether a handle supports a protocol.
616+
///
617+
/// Returns `Ok(true)` if the handle supports the protocol, `Ok(false)` if not.
618+
///
619+
/// # Errors
620+
///
621+
/// * [`Status::INVALID_PARAMETER`]: one of the handles in `params` is invalid.
622+
pub fn test_protocol<P: ProtocolPointer + ?Sized>(params: OpenProtocolParams) -> Result<bool> {
623+
const TEST_PROTOCOL: u32 = 0x04;
624+
625+
let bt = boot_services_raw_panicking();
626+
let bt = unsafe { bt.as_ref() };
627+
628+
let mut interface = ptr::null_mut();
629+
let status = unsafe {
630+
(bt.open_protocol)(
631+
params.handle.as_ptr(),
632+
&P::GUID,
633+
&mut interface,
634+
params.agent.as_ptr(),
635+
Handle::opt_to_ptr(params.controller),
636+
TEST_PROTOCOL,
637+
)
638+
};
639+
640+
match status {
641+
Status::SUCCESS => Ok(true),
642+
Status::UNSUPPORTED => Ok(false),
643+
_ => Err(Error::from(status)),
644+
}
645+
}
646+
615647
/// Loads a UEFI image into memory and return a [`Handle`] to the image.
616648
///
617649
/// There are two ways to load the image: by copying raw image data

0 commit comments

Comments
 (0)