Skip to content

uefi: Add TryFrom u8 slice to DevicePathHeader #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions uefi/src/proto/device_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
if mem::size_of::<DevicePathHeader>() <= bytes.len() {
unsafe {
return Ok(&*bytes.as_ptr().cast::<DevicePathHeader>());
}
}
Err(ByteConversionError::InvalidLength)
}
}

/// A single node within a [`DevicePath`].
///
/// Each node starts with a [`DevicePathHeader`]. The rest of the data
Expand Down Expand Up @@ -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)]
Expand Down