diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index 0f01003eb..70b95be05 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -11,6 +11,8 @@ - Added `table::{set_system_table, system_table_boot, system_table_runtime}`. This provides an initial API for global tables that do not require passing around a reference. +- Added `TryFrom<&[u8]>` for `DevicePathHeader`. +- Added `ByteConversionError`. ## Changed - `SystemTable::exit_boot_services` is now `unsafe`. See that method's diff --git a/uefi/src/proto/device_path/mod.rs b/uefi/src/proto/device_path/mod.rs index 658d84c9d..6696bcce8 100644 --- a/uefi/src/proto/device_path/mod.rs +++ b/uefi/src/proto/device_path/mod.rs @@ -119,6 +119,19 @@ pub struct DevicePathHeader { pub length: u16, } +impl<'a> TryFrom<&[u8]> for &'a DevicePathHeader { + type Error = ByteConversionError; + + fn try_from(bytes: &[u8]) -> Result { + if mem::size_of::() <= bytes.len() { + unsafe { + return Ok(&*bytes.as_ptr().cast::()); + } + } + Err(ByteConversionError::InvalidLength) + } +} + /// A single node within a [`DevicePath`]. /// /// Each node starts with a [`DevicePathHeader`]. The rest of the data @@ -729,6 +742,14 @@ impl DeviceSubType { pub const END_ENTIRE: DeviceSubType = DeviceSubType(0xff); } +/// Error returned when attempting to convert from a `&[u8]` to a +/// [`DevicePath`] type. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum ByteConversionError { + /// The length of the given slice is not valid for its [`DevicePath`] type. + InvalidLength, +} + /// Error returned when converting from a [`DevicePathNode`] to a more /// specific node type. #[derive(Clone, Copy, Debug, Eq, PartialEq)]