Skip to content

Commit b04209e

Browse files
authored
Merge pull request #1199 from andre-braga/header
uefi: Add TryFrom u8 slice to DevicePathHeader
2 parents a55ef5d + 2ee302a commit b04209e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

uefi/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
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+
- Added `TryFrom<&[u8]>` for `DevicePathHeader`.
15+
- Added `ByteConversionError`.
1416

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

uefi/src/proto/device_path/mod.rs

Lines changed: 21 additions & 0 deletions
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,14 @@ 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.
747+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
748+
pub enum ByteConversionError {
749+
/// The length of the given slice is not valid for its [`DevicePath`] type.
750+
InvalidLength,
751+
}
752+
732753
/// Error returned when converting from a [`DevicePathNode`] to a more
733754
/// specific node type.
734755
#[derive(Clone, Copy, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)