Skip to content

boot: Add freestanding locate_device_path #1316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
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
6 changes: 6 additions & 0 deletions uefi-test-runner/src/proto/device_path.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use alloc::string::ToString;
use alloc::vec::Vec;
use uefi::boot;
use uefi::prelude::*;
use uefi::proto::device_path::text::*;
use uefi::proto::device_path::{DevicePath, LoadedImageDevicePath};
use uefi::proto::loaded_image::LoadedImage;
use uefi::proto::media::disk::DiskIo;

pub fn test(bt: &BootServices) {
info!("Running device path protocol test");
Expand Down Expand Up @@ -64,6 +66,10 @@ pub fn test(bt: &BootServices) {
{
assert_eq!(n1, n2);
}

// Test `locate_device_path`.
let mut dp = &*device_path;
boot::locate_device_path::<DiskIo>(&mut dp).unwrap();
}

// test 2/2: test high-level to-string api
Expand Down
34 changes: 34 additions & 0 deletions uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,40 @@ pub fn protocols_per_handle(handle: Handle) -> Result<ProtocolsPerHandle> {
})
}

/// Locates the handle of a device on the device path that supports the specified protocol.
///
/// The `device_path` is updated to point at the remaining part of the [`DevicePath`] after
/// the part that matched the protocol. For example, it can be used with a device path
/// that contains a file path to strip off the file system portion of the device path,
/// leaving the file path and handle to the file system driver needed to access the file.
///
/// If the first node of `device_path` matches the protocol, the `device_path`
/// is advanced to the device path terminator node. If `device_path` is a
/// multi-instance device path, the function will operate on the first instance.
///
/// # Errors
///
/// * [`Status::NOT_FOUND`]: no matching handles.
pub fn locate_device_path<P: ProtocolPointer + ?Sized>(
device_path: &mut &DevicePath,
) -> Result<Handle> {
let bt = boot_services_raw_panicking();
let bt = unsafe { bt.as_ref() };

let mut handle = ptr::null_mut();
let mut device_path_ptr: *const uefi_raw::protocol::device_path::DevicePathProtocol =
device_path.as_ffi_ptr().cast();
unsafe {
(bt.locate_device_path)(&P::GUID, &mut device_path_ptr, &mut handle).to_result_with_val(
|| {
*device_path = DevicePath::from_ffi_ptr(device_path_ptr.cast());
// OK to unwrap: handle is non-null for Status::SUCCESS.
Handle::from_ptr(handle).unwrap()
},
)
}
}

/// Returns an array of handles that support the requested protocol in a
/// pool-allocated buffer.
///
Expand Down