Skip to content

Commit

Permalink
Add LoadedImageDevicePath protocol
Browse files Browse the repository at this point in the history
This protocol is the same as `DevicePath`, but only available on image handles.
  • Loading branch information
nicholasbishop committed May 8, 2023
1 parent 98670a3 commit 7f456f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
14 changes: 13 additions & 1 deletion uefi-test-runner/src/proto/device_path.rs
@@ -1,6 +1,6 @@
use uefi::prelude::*;
use uefi::proto::device_path::text::*;
use uefi::proto::device_path::DevicePath;
use uefi::proto::device_path::{DevicePath, LoadedImageDevicePath};
use uefi::proto::loaded_image::LoadedImage;
use uefi::table::boot::BootServices;

Expand Down Expand Up @@ -48,4 +48,16 @@ pub fn test(image: Handle, bt: &BootServices) {
.expect("Failed to convert text to device path");
assert_eq!(path, convert);
}

// Get the `LoadedImageDevicePath`. Verify it start with the same nodes as
// `device_path`.
let loaded_image_device_path = bt
.open_protocol_exclusive::<LoadedImageDevicePath>(image)
.expect("Failed to open LoadedImageDevicePath protocol");
for (n1, n2) in device_path
.node_iter()
.zip(loaded_image_device_path.node_iter())
{
assert_eq!(n1, n2);
}
}
30 changes: 30 additions & 0 deletions uefi/src/proto/device_path/mod.rs
Expand Up @@ -85,6 +85,7 @@ use crate::proto::{unsafe_protocol, ProtocolPointer};
use core::ffi::c_void;
use core::fmt::{self, Debug, Formatter};
use core::mem;
use core::ops::Deref;
use ptr_meta::Pointee;

opaque_type! {
Expand Down Expand Up @@ -610,6 +611,35 @@ pub enum NodeConversionError {
UnsupportedType,
}

/// Protocol for accessing the device path that was passed in to [`load_image`]
/// when loading a PE/COFF image.
///
/// The layout of this type is the same as a [`DevicePath`].
///
/// [`load_image`]: crate::table::boot::BootServices::load_image
#[repr(transparent)]
#[unsafe_protocol("bc62157e-3e33-4fec-9920-2d3b36d750df")]
#[derive(Pointee)]
pub struct LoadedImageDevicePath(DevicePath);

impl ProtocolPointer for LoadedImageDevicePath {
unsafe fn ptr_from_ffi(ptr: *const c_void) -> *const Self {
ptr_meta::from_raw_parts(ptr.cast(), DevicePath::size_in_bytes_from_ptr(ptr))
}

unsafe fn mut_ptr_from_ffi(ptr: *mut c_void) -> *mut Self {
ptr_meta::from_raw_parts_mut(ptr.cast(), DevicePath::size_in_bytes_from_ptr(ptr))
}
}

impl Deref for LoadedImageDevicePath {
type Target = DevicePath;

fn deref(&self) -> &DevicePath {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/table/boot.rs
Expand Up @@ -986,10 +986,11 @@ impl BootServices {
/// image.
///
/// If the image is successfully loaded, a [`Handle`] supporting the
/// [`LoadedImage`] and `LoadedImageDevicePath` protocols is
/// [`LoadedImage`] and [`LoadedImageDevicePath`] protocols is
/// returned. The image can be started with [`start_image`] or
/// unloaded with [`unload_image`].
///
/// [`LoadedImageDevicePath`]: crate::proto::device_path::LoadedImageDevicePath
/// [`start_image`]: BootServices::start_image
/// [`unload_image`]: BootServices::unload_image
///
Expand Down

0 comments on commit 7f456f4

Please sign in to comment.