Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion uefi-test-runner/src/proto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use uefi::boot::{self, OpenProtocolParams};
use uefi::prelude::*;
use uefi::proto::loaded_image::LoadedImage;
use uefi::{boot, proto, Identify};
use uefi::{proto, Identify};

pub fn test(st: &mut SystemTable<Boot>) {
info!("Testing various protocols");
Expand All @@ -11,6 +12,7 @@ pub fn test(st: &mut SystemTable<Boot>) {
find_protocol(bt);
test_protocols_per_handle(bt);
test_protocols_per_handle_freestanding();
test_test_protocol_freestanding();

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

fn test_test_protocol_freestanding() {
assert!(boot::test_protocol::<LoadedImage>(OpenProtocolParams {
handle: boot::image_handle(),
agent: boot::image_handle(),
controller: None,
})
.unwrap());
}

mod console;
mod debug;
mod device_path;
Expand Down
34 changes: 33 additions & 1 deletion uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::ops::{Deref, DerefMut};
use core::ptr::{self, NonNull};
use core::sync::atomic::{AtomicPtr, Ordering};
use core::{mem, slice};
use uefi::{table, Char16, Event, Guid, Handle, Result, Status, StatusExt};
use uefi::{table, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
use uefi_raw::table::boot::InterfaceType;

#[cfg(doc)]
Expand Down Expand Up @@ -612,6 +612,38 @@ pub fn open_protocol_exclusive<P: ProtocolPointer + ?Sized>(
}
}

/// Tests whether a handle supports a protocol.
///
/// Returns `Ok(true)` if the handle supports the protocol, `Ok(false)` if not.
///
/// # Errors
///
/// * [`Status::INVALID_PARAMETER`]: one of the handles in `params` is invalid.
pub fn test_protocol<P: ProtocolPointer + ?Sized>(params: OpenProtocolParams) -> Result<bool> {
const TEST_PROTOCOL: u32 = 0x04;

let bt = boot_services_raw_panicking();
let bt = unsafe { bt.as_ref() };

let mut interface = ptr::null_mut();
let status = unsafe {
(bt.open_protocol)(
params.handle.as_ptr(),
&P::GUID,
&mut interface,
params.agent.as_ptr(),
Handle::opt_to_ptr(params.controller),
TEST_PROTOCOL,
)
};

match status {
Status::SUCCESS => Ok(true),
Status::UNSUPPORTED => Ok(false),
_ => Err(Error::from(status)),
}
}

/// Loads a UEFI image into memory and return a [`Handle`] to the image.
///
/// There are two ways to load the image: by copying raw image data
Expand Down