Skip to content

Commit 0d97ccf

Browse files
committed
uefi: Add TryFrom u8 slice to DevicePathHeader
Allows for making a `DevicePathHeader` from a byte splice, used primarily to verify whether the byte splice can also contain `DevicePathHeader.length` bytes.
1 parent a55ef5d commit 0d97ccf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

uefi/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Added `table::{set_system_table, system_table_boot, system_table_runtime}`.
1212
This provides an initial API for global tables that do not require passing
1313
around a reference.
14+
- Implemented `DevicePathHeader::from_ffi_ptr`.
1415

1516
## Changed
1617
- `SystemTable::exit_boot_services` is now `unsafe`. See that method's

uefi/src/proto/device_path/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ pub struct DevicePathHeader {
119119
pub length: u16,
120120
}
121121

122+
impl<'a> TryFrom<&[u8]> for &'a DevicePathHeader {
123+
type Error = ByteConversionError;
124+
125+
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
126+
if mem::size_of::<DevicePathHeader>() <= bytes.len() {
127+
unsafe {
128+
return Ok(&*bytes.as_ptr().cast::<DevicePathHeader>());
129+
}
130+
}
131+
Err(ByteConversionError::InvalidLength)
132+
}
133+
}
134+
122135
/// A single node within a [`DevicePath`].
123136
///
124137
/// Each node starts with a [`DevicePathHeader`]. The rest of the data
@@ -729,6 +742,15 @@ impl DeviceSubType {
729742
pub const END_ENTIRE: DeviceSubType = DeviceSubType(0xff);
730743
}
731744

745+
/// Error returned when attempting to convert from a `&[u8]` to a
746+
/// [`DevicePath`] type using:
747+
/// - [`DevicePathHeader::try_from`](struct.DevicePathHeader.html#method.try_from)
748+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
749+
pub enum ByteConversionError {
750+
/// The length of the given slice is not valid for its [`DevicePath`] type.
751+
InvalidLength,
752+
}
753+
732754
/// Error returned when converting from a [`DevicePathNode`] to a more
733755
/// specific node type.
734756
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)