diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 72991ccff..3d0d44b0d 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -14,6 +14,11 @@ - Added `DevicePathUtilitiesProtocol`. - Added `UsbIoProtocol`. - Added `Usb2HostControllerProtocol`. +- Added `DevicePathProtocol::length()` properly constructing the `u16` value + +## Changed +- `DevicePathProtocol` now derives + `Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash` # uefi-raw - 0.10.0 (2025-02-07) diff --git a/uefi-raw/src/protocol/device_path.rs b/uefi-raw/src/protocol/device_path.rs index ad0635d6f..f72ccb7b7 100644 --- a/uefi-raw/src/protocol/device_path.rs +++ b/uefi-raw/src/protocol/device_path.rs @@ -8,22 +8,29 @@ pub use device_path_gen::{acpi, bios_boot_spec, end, hardware, media, messaging} /// Device path protocol. /// -/// A device path contains one or more device path instances made of up +/// A device path contains one or more device path instances made up of /// variable-length nodes. /// /// Note that the fields in this struct define the header at the start of each /// node; a device path is typically larger than these four bytes. -#[derive(Debug)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(C)] pub struct DevicePathProtocol { pub major_type: DeviceType, pub sub_type: DeviceSubType, + /// Total length of the type including the fixed header as u16 in LE order. pub length: [u8; 2], // followed by payload (dynamically sized) } impl DevicePathProtocol { pub const GUID: Guid = guid!("09576e91-6d3f-11d2-8e39-00a0c969723b"); + + /// Returns the total length of the device path node. + #[must_use] + pub const fn length(&self) -> u16 { + u16::from_le_bytes(self.length) + } } newtype_enum! { @@ -252,3 +259,17 @@ pub struct DevicePathUtilitiesProtocol { impl DevicePathUtilitiesProtocol { pub const GUID: Guid = guid!("0379be4e-d706-437d-b037-edb82fb772a4"); } + +#[cfg(test)] +mod tests { + use super::*; + use core::mem; + + /// Test that ensures the struct is packed. Thus, we don't need to + /// explicitly specify `packed`. + #[test] + fn abi() { + assert_eq!(mem::size_of::(), 4); + assert_eq!(mem::align_of::(), 1); + } +} diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index 10b533c48..fa9cb6218 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -27,6 +27,8 @@ `proto::device_path::text` to `proto::device_path`. - **Breaking:** `exit_boot_services` now consumes a `Option` which defaults to the recommended value of `MemoryType::LOADER_DATA`. +- **Breaking:** Removed duplication in `DevicePathHeader`. Instead of public fields, + there is now a public constructor combined with public getters. - `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on, as this is considered a hard internal error where users can't do anything about it anyway. It will panic instead. diff --git a/uefi/src/proto/device_path/build.rs b/uefi/src/proto/device_path/build.rs index f0d19e4f1..4707e3ada 100644 --- a/uefi/src/proto/device_path/build.rs +++ b/uefi/src/proto/device_path/build.rs @@ -225,7 +225,7 @@ pub unsafe trait BuildNode { unsafe impl BuildNode for &DevicePathNode { fn size_in_bytes(&self) -> Result { - Ok(self.header.length) + Ok(self.header.length()) } fn write_data(&self, out: &mut [MaybeUninit]) { diff --git a/uefi/src/proto/device_path/device_path_gen.rs b/uefi/src/proto/device_path/device_path_gen.rs index 8154da885..d30b93ee8 100644 --- a/uefi/src/proto/device_path/device_path_gen.rs +++ b/uefi/src/proto/device_path/device_path_gen.rs @@ -3669,14 +3669,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::END, DeviceSubType::END_INSTANCE, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::END, - sub_type: DeviceSubType::END_INSTANCE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); } } } @@ -3696,14 +3693,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::END, DeviceSubType::END_ENTIRE, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::END, - sub_type: DeviceSubType::END_ENTIRE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); } } } @@ -3731,14 +3725,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_PCI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_PCI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3768,14 +3762,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_PCCARD, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_PCCARD, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3805,14 +3799,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_MEMORY_MAPPED, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_MEMORY_MAPPED, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3848,14 +3842,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_VENDOR, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_VENDOR, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3888,14 +3882,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_CONTROLLER, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_CONTROLLER, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3926,14 +3920,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::HARDWARE, + DeviceSubType::HARDWARE_BMC, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::HARDWARE, - sub_type: DeviceSubType::HARDWARE_BMC, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -3971,14 +3965,10 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new(DeviceType::ACPI, DeviceSubType::ACPI, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::ACPI, - sub_type: DeviceSubType::ACPI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).cast::().write_unaligned(self.hid); out_ptr.add(8usize).cast::().write_unaligned(self.uid); } @@ -4027,14 +4017,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::ACPI, DeviceSubType::ACPI_EXPANDED, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::ACPI, - sub_type: DeviceSubType::ACPI_EXPANDED, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); let mut dst_group_offset = 0; out_ptr.add(4usize).cast::().write_unaligned(self.hid); out_ptr.add(8usize).cast::().write_unaligned(self.uid); @@ -4076,14 +4063,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::ACPI, DeviceSubType::ACPI_ADR, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::ACPI, - sub_type: DeviceSubType::ACPI_ADR, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); self.adr .as_ptr() .cast::() @@ -4109,14 +4093,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::ACPI, DeviceSubType::ACPI_NVDIMM, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::ACPI, - sub_type: DeviceSubType::ACPI_NVDIMM, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4173,14 +4154,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_ATAPI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_ATAPI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4216,14 +4197,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_SCSI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_SCSI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4255,14 +4236,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_FIBRE_CHANNEL, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_FIBRE_CHANNEL, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).write_bytes(0, size_of::()); out_ptr .add(8usize) @@ -4295,14 +4276,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_FIBRE_CHANNEL_EX, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_FIBRE_CHANNEL_EX, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).write_bytes(0, size_of::()); out_ptr .add(8usize) @@ -4334,14 +4315,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_1394, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_1394, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).write_bytes(0, size_of::()); out_ptr .add(8usize) @@ -4370,14 +4351,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_USB, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_USB, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4414,14 +4395,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_SATA, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_SATA, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4461,14 +4442,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_USB_WWID, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_USB_WWID, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4509,14 +4490,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_DEVICE_LOGICAL_UNIT, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_DEVICE_LOGICAL_UNIT, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4550,14 +4531,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_USB_CLASS, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_USB_CLASS, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4599,14 +4580,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_I2O, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_I2O, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4635,14 +4616,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_MAC_ADDRESS, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_MAC_ADDRESS, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 32usize]>() @@ -4687,14 +4668,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_IPV4, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_IPV4, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 4usize]>() @@ -4763,14 +4744,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_IPV6, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_IPV6, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 16usize]>() @@ -4824,14 +4805,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_VLAN, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_VLAN, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4867,14 +4848,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_INFINIBAND, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_INFINIBAND, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -4922,14 +4903,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_UART, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_UART, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).write_bytes(0, size_of::()); out_ptr .add(8usize) @@ -4970,14 +4951,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_VENDOR, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_VENDOR, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5016,14 +4997,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_SCSI_SAS_EX, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_SCSI_SAS_EX, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 8usize]>() @@ -5074,14 +5055,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_ISCSI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_ISCSI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5130,14 +5111,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_NVME_NAMESPACE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_NVME_NAMESPACE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5167,14 +5148,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_URI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_URI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); self.value .as_ptr() .cast::() @@ -5202,14 +5183,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_UFS, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_UFS, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5239,14 +5220,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_SD, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_SD, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5272,14 +5253,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_BLUETOOTH, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_BLUETOOTH, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 6usize]>() @@ -5305,14 +5286,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_WIFI, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_WIFI, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 32usize]>() @@ -5338,14 +5319,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_EMMC, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_EMMC, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5373,14 +5354,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_BLUETOOTH_LE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_BLUETOOTH_LE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 6usize]>() @@ -5412,14 +5393,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_DNS, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_DNS, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5449,14 +5430,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_NVDIMM_NAMESPACE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_NVDIMM_NAMESPACE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::<[u8; 16usize]>() @@ -5488,14 +5469,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_REST_SERVICE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_REST_SERVICE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5531,14 +5512,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MESSAGING, + DeviceSubType::MESSAGING_NVME_OF_NAMESPACE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MESSAGING, - sub_type: DeviceSubType::MESSAGING_NVME_OF_NAMESPACE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).cast::().write_unaligned(self.nidt); out_ptr .add(5usize) @@ -5624,14 +5605,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MEDIA, + DeviceSubType::MEDIA_HARD_DRIVE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_HARD_DRIVE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5682,14 +5663,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::MEDIA, DeviceSubType::MEDIA_CD_ROM, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_CD_ROM, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5725,14 +5703,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::MEDIA, DeviceSubType::MEDIA_VENDOR, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_VENDOR, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5765,14 +5740,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MEDIA, + DeviceSubType::MEDIA_FILE_PATH, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_FILE_PATH, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); self.path_name .as_ptr() .cast::() @@ -5798,14 +5773,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::MEDIA, DeviceSubType::MEDIA_PROTOCOL, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_PROTOCOL, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -5831,14 +5803,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MEDIA, + DeviceSubType::MEDIA_PIWG_FIRMWARE_FILE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_PIWG_FIRMWARE_FILE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); self.data .as_ptr() .cast::() @@ -5864,14 +5836,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MEDIA, + DeviceSubType::MEDIA_PIWG_FIRMWARE_VOLUME, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_PIWG_FIRMWARE_VOLUME, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); self.data .as_ptr() .cast::() @@ -5899,14 +5871,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::MEDIA, + DeviceSubType::MEDIA_RELATIVE_OFFSET_RANGE, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_RELATIVE_OFFSET_RANGE, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr.add(4usize).write_bytes(0, size_of::()); out_ptr .add(8usize) @@ -5943,14 +5915,11 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = + DevicePathHeader::new(DeviceType::MEDIA, DeviceSubType::MEDIA_RAM_DISK, length); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::MEDIA, - sub_type: DeviceSubType::MEDIA_RAM_DISK, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() @@ -6024,14 +5993,14 @@ pub mod build { let size = usize::from(self.size_in_bytes().unwrap()); assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::BIOS_BOOT_SPEC, + DeviceSubType::BIOS_BOOT_SPECIFICATION, + length, + ); unsafe { - out_ptr - .cast::() - .write_unaligned(DevicePathHeader { - device_type: DeviceType::BIOS_BOOT_SPEC, - sub_type: DeviceSubType::BIOS_BOOT_SPECIFICATION, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); out_ptr .add(4usize) .cast::() diff --git a/uefi/src/proto/device_path/mod.rs b/uefi/src/proto/device_path/mod.rs index 697f8ca9c..d3e6bf049 100644 --- a/uefi/src/proto/device_path/mod.rs +++ b/uefi/src/proto/device_path/mod.rs @@ -80,6 +80,7 @@ pub mod text; pub mod util; mod device_path_gen; + pub use device_path_gen::{ acpi, bios_boot_spec, end, hardware, media, messaging, DevicePathNodeEnum, }; @@ -92,6 +93,7 @@ use core::fmt::{self, Debug, Display, Formatter}; use core::ops::Deref; use ptr_meta::Pointee; +use uefi_raw::protocol::device_path::DevicePathProtocol; #[cfg(feature = "alloc")] use { crate::boot::{self, OpenProtocolAttributes, OpenProtocolParams, ScopedProtocol, SearchType}, @@ -135,16 +137,41 @@ impl Deref for PoolDevicePathNode { } } -/// Header that appears at the start of every [`DevicePathNode`]. +/// Fixed header that appears at the start of every [`DevicePathNode`]. +/// +/// This type is ABI-compatible with `EFI_DEVICE_PATH_PROTOCOL`. #[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[repr(C, packed)] -pub struct DevicePathHeader { - /// Type of device - pub device_type: DeviceType, - /// Sub type of device - pub sub_type: DeviceSubType, - /// Size (in bytes) of the [`DevicePathNode`], including this header. - pub length: u16, +#[repr(transparent)] +pub struct DevicePathHeader(DevicePathProtocol); + +impl DevicePathHeader { + /// Constructs a new [`DevicePathHeader`]. + #[must_use] + pub const fn new(major_type: DeviceType, sub_type: DeviceSubType, length: u16) -> Self { + Self(DevicePathProtocol { + major_type, + sub_type, + length: length.to_le_bytes(), + }) + } + + /// Returns the [`DeviceType`]. + #[must_use] + pub const fn device_type(&self) -> DeviceType { + self.0.major_type + } + + /// Returns the [`DeviceSubType`]. + #[must_use] + pub const fn sub_type(&self) -> DeviceSubType { + self.0.sub_type + } + + /// Returns the total length of the device path node. + #[must_use] + pub const fn length(&self) -> u16 { + self.0.length() + } } impl<'a> TryFrom<&'a [u8]> for &'a DevicePathHeader { @@ -202,7 +229,7 @@ impl DevicePathNode { pub unsafe fn from_ffi_ptr<'a>(ptr: *const FfiDevicePath) -> &'a Self { let header = unsafe { *ptr.cast::() }; - let data_len = usize::from(header.length) - size_of::(); + let data_len = usize::from(header.length()) - size_of::(); unsafe { &*ptr_meta::from_raw_parts(ptr.cast(), data_len) } } @@ -216,25 +243,25 @@ impl DevicePathNode { /// Type of device #[must_use] pub const fn device_type(&self) -> DeviceType { - self.header.device_type + self.header.device_type() } /// Sub type of device #[must_use] pub const fn sub_type(&self) -> DeviceSubType { - self.header.sub_type + self.header.sub_type() } /// Tuple of the node's type and subtype. #[must_use] pub const fn full_type(&self) -> (DeviceType, DeviceSubType) { - (self.header.device_type, self.header.sub_type) + (self.device_type(), self.sub_type()) } /// Size (in bytes) of the full [`DevicePathNode`], including the header. #[must_use] pub const fn length(&self) -> u16 { - self.header.length + self.header.length() } /// True if this node ends an entire [`DevicePath`]. @@ -297,7 +324,7 @@ impl<'a> TryFrom<&'a [u8]> for &'a DevicePathNode { fn try_from(bytes: &[u8]) -> Result { let dp = <&DevicePathHeader>::try_from(bytes)?; - if usize::from(dp.length) <= bytes.len() { + if usize::from(dp.length()) <= bytes.len() { unsafe { Ok(DevicePathNode::from_ffi_ptr(bytes.as_ptr().cast())) } } else { Err(ByteConversionError::InvalidLength) diff --git a/xtask/src/device_path/node.rs b/xtask/src/device_path/node.rs index 263b08689..0a6746c37 100644 --- a/xtask/src/device_path/node.rs +++ b/xtask/src/device_path/node.rs @@ -473,12 +473,14 @@ impl Node { assert_eq!(size, out.len()); let out_ptr: *mut u8 = maybe_uninit_slice_as_mut_ptr(out); + let length = u16::try_from(size).unwrap(); + let header = DevicePathHeader::new( + DeviceType::#device_type, + DeviceSubType::#sub_type, + length, + ); unsafe { - out_ptr.cast::().write_unaligned(DevicePathHeader { - device_type: DeviceType::#device_type, - sub_type: DeviceSubType::#sub_type, - length: u16::try_from(size).unwrap(), - }); + out_ptr.cast::().write_unaligned(header); #(#copy_stmts)* } }