Skip to content

Commit d66959a

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

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

uefi/src/boot.rs

+40
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,46 @@ 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::table::boot::{BootServices, OpenProtocolAttributes, OpenProtocolParams};
607+
/// use uefi::Handle;
608+
/// # use uefi::Result;
609+
///
610+
/// # fn get_fake_val<T>() -> T { todo!() }
611+
/// # fn test() -> Result {
612+
/// # let boot_services: &BootServices = get_fake_val();
613+
/// # let image_handle: Handle = get_fake_val();
614+
/// let handle = get_handle_for_protocol::<DevicePathToText>()?;
615+
/// let device_path_to_text = open_protocol_exclusive::<DevicePathToText>(handle)?;
616+
/// # Ok(())
617+
/// # }
618+
/// ```
619+
///
620+
/// # Errors
621+
///
622+
/// * [`Status::NOT_FOUND`]: no matching handle.
623+
/// * [`Status::OUT_OF_RESOURCES`]: out of memory.
624+
pub fn get_handle_for_protocol<P: ProtocolPointer + ?Sized>() -> Result<Handle> {
625+
locate_handle_buffer(SearchType::ByProtocol(&P::GUID))?
626+
.first()
627+
.cloned()
628+
.ok_or_else(|| Status::NOT_FOUND.into())
629+
}
630+
591631
/// Opens a protocol interface for a handle.
592632
///
593633
/// See also [`open_protocol_exclusive`], which provides a safe subset of this

0 commit comments

Comments
 (0)