Skip to content

Commit 18b8121

Browse files
committed
uefi: Add convenient methods for &DevicePath -> PoolDevicePath
1 parent fbed494 commit 18b8121

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

uefi/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Added `proto::shell::Shell::{var(), set_var(), vars()}`
66
- Added `proto::pci::root_bridge::PciRootBridgeIo::configuration()`.
77
- Added `proto::pci::root_bridge::PciRootBridgeIo::enumerate()`.
8+
- Added `proto::device_path::DevicePath::to_pool()`.
9+
- Added `proto::device_path::DevicePathUtilities::duplicate_path()`.
810

911
## Changed
1012
- Changed ordering of `proto::pci::PciIoAddress` to (bus -> dev -> fun -> reg -> ext_reg).

uefi/src/proto/device_path/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ impl DevicePath {
590590
unsafe { mem::transmute(data) }
591591
}
592592

593+
/// Returns an owned pool copy of this path.
594+
#[cfg(feature = "alloc")]
595+
pub fn to_pool(&self) -> Result<PoolDevicePath, DevicePathUtilitiesError> {
596+
open_utility_protocol()?
597+
.duplicate_path(self)
598+
.map_err(|_| DevicePathUtilitiesError::OutOfMemory)
599+
}
600+
593601
/// Transforms the device path to its string representation using the
594602
/// [`DevicePathToText`] protocol.
595603
#[cfg(feature = "alloc")]

uefi/src/proto/device_path/util.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ impl DevicePathUtilities {
2929
unsafe { (self.0.get_device_path_size)(device_path.as_ffi_ptr().cast()) }
3030
}
3131

32+
/// Create a new device path by cloning the given `path` into newly allocated memory.
33+
///
34+
/// # Arguments
35+
/// - `path`: A reference to the device path to clone.
36+
///
37+
/// # Returns
38+
/// A [`PoolDevicePath`] instance created by cloning the given `path`.
39+
pub fn duplicate_path(&self, path: &DevicePath) -> crate::Result<PoolDevicePath> {
40+
unsafe {
41+
let ptr = (self.0.duplicate_device_path)(path.as_ffi_ptr().cast());
42+
NonNull::new(ptr.cast_mut())
43+
.map(|p| PoolDevicePath(PoolAllocation::new(p.cast())))
44+
.ok_or_else(|| Status::OUT_OF_RESOURCES.into())
45+
}
46+
}
47+
3248
/// Creates a new device path by appending the second device path to the first.
3349
///
3450
/// # Arguments

0 commit comments

Comments
 (0)