Skip to content

Commit bd96541

Browse files
boot: Add freestanding get_handle_for_protocol
1 parent 86727ee commit bd96541

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

uefi/src/boot.rs

+38
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,44 @@ pub fn locate_handle_buffer(search_ty: SearchType) -> Result<HandleBuffer> {
588588
})
589589
}
590590

591+
/// Find an arbitrary handle that supports a particular [`Protocol`]. Returns
592+
/// [`NOT_FOUND`] if no handles support the protocol.
593+
///
594+
/// This method is a convenient wrapper around [`locate_handle_buffer`] for
595+
/// getting just one handle. This is useful when you don't care which handle the
596+
/// protocol is opened on. For example, [`DevicePathToText`] isn't tied to a
597+
/// particular device, so only a single handle is expected to exist.
598+
///
599+
/// [`NOT_FOUND`]: Status::NOT_FOUND
600+
/// [`DevicePathToText`]: uefi::proto::device_path::text::DevicePathToText
601+
///
602+
/// # Example
603+
///
604+
/// ```
605+
/// use uefi::proto::device_path::text::DevicePathToText;
606+
/// use uefi::{boot, Handle};
607+
/// # use uefi::Result;
608+
///
609+
/// # fn get_fake_val<T>() -> T { todo!() }
610+
/// # fn test() -> Result {
611+
/// # let image_handle: Handle = get_fake_val();
612+
/// let handle = boot::get_handle_for_protocol::<DevicePathToText>()?;
613+
/// let device_path_to_text = boot::open_protocol_exclusive::<DevicePathToText>(handle)?;
614+
/// # Ok(())
615+
/// # }
616+
/// ```
617+
///
618+
/// # Errors
619+
///
620+
/// * [`Status::NOT_FOUND`]: no matching handle.
621+
/// * [`Status::OUT_OF_RESOURCES`]: out of memory.
622+
pub fn get_handle_for_protocol<P: ProtocolPointer + ?Sized>() -> Result<Handle> {
623+
locate_handle_buffer(SearchType::ByProtocol(&P::GUID))?
624+
.first()
625+
.cloned()
626+
.ok_or_else(|| Status::NOT_FOUND.into())
627+
}
628+
591629
/// Opens a protocol interface for a handle.
592630
///
593631
/// See also [`open_protocol_exclusive`], which provides a safe subset of this

0 commit comments

Comments
 (0)