From 3b05e1afdf94977e34e451a673adadb99e8d7074 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 30 Dec 2022 17:55:17 -0500 Subject: [PATCH] uefi: Add `ptr_meta` dependency The `ptr_meta` crate (https://docs.rs/ptr_meta) is a polyfill for the unstable [`ptr_metadata`](https://github.com/rust-lang/rust/issues/81513) feature on stable Rust. This allows us to drop use of an unstable feature with fairly minimal code changes; mostly just adding a derive for the `Pointee` type on some DST structs. --- CHANGELOG.md | 2 + uefi/Cargo.toml | 1 + uefi/src/lib.rs | 1 - uefi/src/proto/device_path/build.rs | 3 +- uefi/src/proto/device_path/device_path_gen.rs | 85 +++++++++++-------- uefi/src/proto/device_path/mod.rs | 19 +++-- uefi/src/proto/media/file/info.rs | 13 +-- uefi/src/proto/tcg/v1.rs | 4 +- xtask/src/device_path/field.rs | 2 +- xtask/src/device_path/mod.rs | 3 +- xtask/src/device_path/node.rs | 12 ++- 11 files changed, 88 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eed8492ed..fcfd6b011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ `impl Iterator` which simplifies usage. - `GraphicsOutput::modes()` now returns `ModesIter` instead of `impl Iterator` which simplifies usage. +- Use of the unstable `ptr_metadata` feature has been replaced with a dependency + on the [`ptr_meta`](https://docs.rs/ptr_meta) crate. ### Removed diff --git a/uefi/Cargo.toml b/uefi/Cargo.toml index a86544601..2ed1e5969 100644 --- a/uefi/Cargo.toml +++ b/uefi/Cargo.toml @@ -25,6 +25,7 @@ unstable = [] [dependencies] bitflags = "1.3.1" log = { version = "0.4.5", default-features = false } +ptr_meta = { version = "0.2.0", default-features = false } ucs2 = "0.3.2" uefi-macros = "0.9.0" diff --git a/uefi/src/lib.rs b/uefi/src/lib.rs index d411fb8d9..a8d30f4cf 100644 --- a/uefi/src/lib.rs +++ b/uefi/src/lib.rs @@ -60,7 +60,6 @@ #![feature(abi_efiapi)] #![feature(maybe_uninit_slice)] -#![feature(ptr_metadata)] #![cfg_attr(feature = "alloc", feature(vec_into_raw_parts))] #![cfg_attr(feature = "unstable", feature(error_in_core))] #![cfg_attr(all(feature = "unstable", feature = "alloc"), feature(allocator_api))] diff --git a/uefi/src/proto/device_path/build.rs b/uefi/src/proto/device_path/build.rs index d838f3a2e..4233380eb 100644 --- a/uefi/src/proto/device_path/build.rs +++ b/uefi/src/proto/device_path/build.rs @@ -9,7 +9,6 @@ pub use uefi::proto::device_path::device_path_gen::build::*; use crate::proto::device_path::{DevicePath, DevicePathNode}; use core::mem::MaybeUninit; -use core::ptr; #[cfg(feature = "alloc")] use alloc::vec::Vec; @@ -139,7 +138,7 @@ impl<'a> DevicePathBuilder<'a> { }; let ptr: *const () = data.as_ptr().cast(); - Ok(unsafe { &*ptr::from_raw_parts(ptr, data.len()) }) + Ok(unsafe { &*ptr_meta::from_raw_parts(ptr, data.len()) }) } } diff --git a/uefi/src/proto/device_path/device_path_gen.rs b/uefi/src/proto/device_path/device_path_gen.rs index 9b2524832..0c74d5585 100644 --- a/uefi/src/proto/device_path/device_path_gen.rs +++ b/uefi/src/proto/device_path/device_path_gen.rs @@ -14,8 +14,9 @@ use crate::table::boot::MemoryType; use crate::{guid, Guid}; use bitflags::bitflags; use core::mem::{size_of, size_of_val}; -use core::ptr::{self, addr_of}; +use core::ptr::addr_of; use core::{fmt, slice}; +use ptr_meta::{Pointee, PtrExt}; /// Device path nodes for [`DeviceType::END`]. pub mod end { use super::*; @@ -215,6 +216,7 @@ pub mod hardware { /// Vendor-defined hardware device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Vendor { pub(super) header: DevicePathHeader, pub(super) vendor_guid: Guid, @@ -241,7 +243,7 @@ pub mod hardware { .field("vendor_guid", &{ self.vendor_guid }) .field("vendor_defined_data", { let ptr = addr_of!(self.vendor_defined_data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -262,7 +264,7 @@ pub mod hardware { } let node: *const DevicePathNode = node; - let node: *const Vendor = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Vendor = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -406,6 +408,7 @@ pub mod acpi { /// Expanded ACPI device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Expanded { pub(super) header: DevicePathHeader, pub(super) hid: u32, @@ -488,13 +491,14 @@ pub mod acpi { } let node: *const DevicePathNode = node; - let node: *const Expanded = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Expanded = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } /// ADR ACPI device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Adr { pub(super) header: DevicePathHeader, pub(super) adr: [u32], @@ -507,7 +511,7 @@ pub mod acpi { #[must_use] pub fn adr(&self) -> UnalignedSlice { let ptr: *const [u32] = addr_of!(self.adr); - let (ptr, len): (*const (), usize) = ptr.to_raw_parts(); + let (ptr, len): (*const (), usize) = PtrExt::to_raw_parts(ptr); unsafe { UnalignedSlice::new(ptr.cast::(), len) } } } @@ -517,7 +521,7 @@ pub mod acpi { f.debug_struct("Adr") .field("adr", { let ptr = addr_of!(self.adr); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -538,7 +542,7 @@ pub mod acpi { } let node: *const DevicePathNode = node; - let node: *const Adr = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Adr = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -1026,6 +1030,7 @@ pub mod messaging { /// USB World Wide ID (WWID) messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct UsbWwid { pub(super) header: DevicePathHeader, pub(super) interface_number: u16, @@ -1057,7 +1062,7 @@ pub mod messaging { #[must_use] pub fn serial_number(&self) -> UnalignedSlice { let ptr: *const [u16] = addr_of!(self.serial_number); - let (ptr, len): (*const (), usize) = ptr.to_raw_parts(); + let (ptr, len): (*const (), usize) = PtrExt::to_raw_parts(ptr); unsafe { UnalignedSlice::new(ptr.cast::(), len) } } } @@ -1070,7 +1075,7 @@ pub mod messaging { .field("device_product_id", &{ self.device_product_id }) .field("serial_number", { let ptr = addr_of!(self.serial_number); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -1091,7 +1096,7 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const UsbWwid = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const UsbWwid = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -1639,6 +1644,7 @@ pub mod messaging { /// Vendor-defined messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Vendor { pub(super) header: DevicePathHeader, pub(super) vendor_guid: Guid, @@ -1665,7 +1671,7 @@ pub mod messaging { .field("vendor_guid", &{ self.vendor_guid }) .field("vendor_defined_data", { let ptr = addr_of!(self.vendor_defined_data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -1686,7 +1692,7 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const Vendor = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Vendor = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -1753,6 +1759,7 @@ pub mod messaging { /// iSCSI messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Iscsi { pub(super) header: DevicePathHeader, pub(super) protocol: crate::proto::device_path::messaging::IscsiProtocol, @@ -1808,7 +1815,7 @@ pub mod messaging { .field("target_portal_group_tag", &{ self.target_portal_group_tag }) .field("iscsi_target_name", { let ptr = addr_of!(self.iscsi_target_name); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -1829,7 +1836,7 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const Iscsi = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Iscsi = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -1884,6 +1891,7 @@ pub mod messaging { /// Uniform Resource Identifier (URI) messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Uri { pub(super) header: DevicePathHeader, pub(super) value: [u8], @@ -1902,7 +1910,7 @@ pub mod messaging { f.debug_struct("Uri") .field("value", { let ptr = addr_of!(self.value); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -1923,7 +1931,7 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const Uri = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Uri = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -2162,6 +2170,7 @@ pub mod messaging { /// DNS messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Dns { pub(super) header: DevicePathHeader, pub(super) address_type: crate::proto::device_path::messaging::DnsAddressType, @@ -2179,7 +2188,7 @@ pub mod messaging { #[must_use] pub fn addresses(&self) -> UnalignedSlice { let ptr: *const [IpAddress] = addr_of!(self.addresses); - let (ptr, len): (*const (), usize) = ptr.to_raw_parts(); + let (ptr, len): (*const (), usize) = PtrExt::to_raw_parts(ptr); unsafe { UnalignedSlice::new(ptr.cast::(), len) } } } @@ -2190,7 +2199,7 @@ pub mod messaging { .field("address_type", &{ self.address_type }) .field("addresses", { let ptr = addr_of!(self.addresses); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2211,7 +2220,7 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const Dns = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Dns = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -2254,6 +2263,7 @@ pub mod messaging { /// REST service messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct RestService { pub(super) header: DevicePathHeader, pub(super) service_type: crate::proto::device_path::messaging::RestServiceType, @@ -2282,7 +2292,7 @@ pub mod messaging { .field("access_mode", &{ self.access_mode }) .field("vendor_guid_and_data", { let ptr = addr_of!(self.vendor_guid_and_data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2303,13 +2313,15 @@ pub mod messaging { } let node: *const DevicePathNode = node; - let node: *const RestService = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const RestService = + ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } /// NVME over Fabric (NVMe-oF) namespace messaging device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct NvmeOfNamespace { pub(super) header: DevicePathHeader, pub(super) nidt: u8, @@ -2345,7 +2357,7 @@ pub mod messaging { .field("nid", &{ self.nid }) .field("subsystem_nqn", { let ptr = addr_of!(self.subsystem_nqn); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2367,7 +2379,7 @@ pub mod messaging { let node: *const DevicePathNode = node; let node: *const NvmeOfNamespace = - ptr::from_raw_parts(node.cast(), dst_size / elem_size); + ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -2566,6 +2578,7 @@ pub mod media { /// Vendor-defined media device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct Vendor { pub(super) header: DevicePathHeader, pub(super) vendor_guid: Guid, @@ -2592,7 +2605,7 @@ pub mod media { .field("vendor_guid", &{ self.vendor_guid }) .field("vendor_defined_data", { let ptr = addr_of!(self.vendor_defined_data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2613,13 +2626,14 @@ pub mod media { } let node: *const DevicePathNode = node; - let node: *const Vendor = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const Vendor = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } /// File path media device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct FilePath { pub(super) header: DevicePathHeader, pub(super) path_name: [u16], @@ -2630,7 +2644,7 @@ pub mod media { #[must_use] pub fn path_name(&self) -> UnalignedSlice { let ptr: *const [u16] = addr_of!(self.path_name); - let (ptr, len): (*const (), usize) = ptr.to_raw_parts(); + let (ptr, len): (*const (), usize) = PtrExt::to_raw_parts(ptr); unsafe { UnalignedSlice::new(ptr.cast::(), len) } } } @@ -2640,7 +2654,7 @@ pub mod media { f.debug_struct("FilePath") .field("path_name", { let ptr = addr_of!(self.path_name); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2661,7 +2675,7 @@ pub mod media { } let node: *const DevicePathNode = node; - let node: *const FilePath = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const FilePath = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -2704,6 +2718,7 @@ pub mod media { /// PIWG firmware file media device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct PiwgFirmwareFile { pub(super) header: DevicePathHeader, pub(super) data: [u8], @@ -2722,7 +2737,7 @@ pub mod media { f.debug_struct("PiwgFirmwareFile") .field("data", { let ptr = addr_of!(self.data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2744,13 +2759,14 @@ pub mod media { let node: *const DevicePathNode = node; let node: *const PiwgFirmwareFile = - ptr::from_raw_parts(node.cast(), dst_size / elem_size); + ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } /// PIWG firmware volume media device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct PiwgFirmwareVolume { pub(super) header: DevicePathHeader, pub(super) data: [u8], @@ -2769,7 +2785,7 @@ pub mod media { f.debug_struct("PiwgFirmwareVolume") .field("data", { let ptr = addr_of!(self.data); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -2791,7 +2807,7 @@ pub mod media { let node: *const DevicePathNode = node; let node: *const PiwgFirmwareVolume = - ptr::from_raw_parts(node.cast(), dst_size / elem_size); + ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } @@ -2955,6 +2971,7 @@ pub mod bios_boot_spec { use super::*; /// BIOS Boot Specification device path node. #[repr(C, packed)] + #[derive(Pointee)] pub struct BootSpecification { pub(super) header: DevicePathHeader, pub(super) device_type: u16, @@ -2990,7 +3007,7 @@ pub mod bios_boot_spec { .field("status_flag", &{ self.status_flag }) .field("description_string", { let ptr = addr_of!(self.description_string); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -3012,7 +3029,7 @@ pub mod bios_boot_spec { let node: *const DevicePathNode = node; let node: *const BootSpecification = - ptr::from_raw_parts(node.cast(), dst_size / elem_size); + ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); Ok(unsafe { &*node }) } } diff --git a/uefi/src/proto/device_path/mod.rs b/uefi/src/proto/device_path/mod.rs index 4fc863356..cc263f1da 100644 --- a/uefi/src/proto/device_path/mod.rs +++ b/uefi/src/proto/device_path/mod.rs @@ -84,7 +84,8 @@ pub use device_path_gen::{ use crate::proto::{unsafe_protocol, ProtocolPointer}; use core::ffi::c_void; use core::marker::{PhantomData, PhantomPinned}; -use core::{mem, ptr}; +use core::mem; +use ptr_meta::Pointee; /// Opaque type that should be used to represent a pointer to a /// [`DevicePath`] or [`DevicePathNode`] in foreign function interfaces. This @@ -118,7 +119,7 @@ pub struct DevicePathHeader { /// See the [module-level documentation] for more details. /// /// [module-level documentation]: crate::proto::device_path -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] #[repr(C, packed)] pub struct DevicePathNode { header: DevicePathHeader, @@ -138,7 +139,7 @@ impl DevicePathNode { let header = *ptr.cast::(); let data_len = usize::from(header.length) - mem::size_of::(); - &*ptr::from_raw_parts(ptr.cast(), data_len) + &*ptr_meta::from_raw_parts(ptr.cast(), data_len) } /// Cast to a [`FfiDevicePath`] pointer. @@ -195,7 +196,7 @@ impl DevicePathNode { /// [`END_INSTANCE`]: DeviceSubType::END_INSTANCE /// [module-level documentation]: crate::proto::device_path #[repr(C, packed)] -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] pub struct DevicePathInstance { data: [u8], } @@ -226,18 +227,18 @@ impl DevicePathInstance { /// [`END_ENTIRE`]: DeviceSubType::END_ENTIRE #[repr(C, packed)] #[unsafe_protocol("09576e91-6d3f-11d2-8e39-00a0c969723b")] -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] pub struct DevicePath { data: [u8], } impl ProtocolPointer for DevicePath { unsafe fn ptr_from_ffi(ptr: *const c_void) -> *const Self { - ptr::from_raw_parts(ptr.cast(), Self::size_in_bytes_from_ptr(ptr)) + ptr_meta::from_raw_parts(ptr.cast(), Self::size_in_bytes_from_ptr(ptr)) } unsafe fn mut_ptr_from_ffi(ptr: *mut c_void) -> *mut Self { - ptr::from_raw_parts_mut(ptr.cast(), Self::size_in_bytes_from_ptr(ptr)) + ptr_meta::from_raw_parts_mut(ptr.cast(), Self::size_in_bytes_from_ptr(ptr)) } } @@ -336,7 +337,7 @@ impl<'a> Iterator for DevicePathInstanceIterator<'a> { self.remaining_path = None; } else { self.remaining_path = unsafe { - Some(&*ptr::from_raw_parts( + Some(&*ptr_meta::from_raw_parts( rest.as_ptr().cast::<()>(), rest.len(), )) @@ -344,7 +345,7 @@ impl<'a> Iterator for DevicePathInstanceIterator<'a> { } unsafe { - Some(&*ptr::from_raw_parts( + Some(&*ptr_meta::from_raw_parts( head.as_ptr().cast::<()>(), head.len(), )) diff --git a/uefi/src/proto/media/file/info.rs b/uefi/src/proto/media/file/info.rs index b49bc2942..acde2831b 100644 --- a/uefi/src/proto/media/file/info.rs +++ b/uefi/src/proto/media/file/info.rs @@ -4,6 +4,7 @@ use crate::table::runtime::Time; use crate::{guid, CStr16, Char16, Guid, Identify}; use core::ffi::c_void; use core::{mem, ptr}; +use ptr_meta::Pointee; /// Common trait for data structures that can be used with /// `File::set_info()` or `File::get_info()`. @@ -32,7 +33,7 @@ pub trait FromUefi { /// `FileSystemVolumeLabel`, all of which are dynamically-sized structs /// that have zero or more header fields followed by a variable-length /// [Char16] name. -trait InfoInternal: Align + ptr::Pointee { +trait InfoInternal: Align + ptr_meta::Pointee { /// Offset in bytes of the start of the name slice at the end of /// the struct. fn name_offset() -> usize; @@ -83,7 +84,7 @@ trait InfoInternal: Align + ptr::Pointee { // Create a raw fat pointer using the `storage` as a base. let info_ptr: *mut Self = - ptr::from_raw_parts_mut(storage.as_mut_ptr().cast::<()>(), name_length_ucs2); + ptr_meta::from_raw_parts_mut(storage.as_mut_ptr().cast::<()>(), name_length_ucs2); // Initialize the struct header. init(info_ptr, info_size as u64); @@ -110,7 +111,7 @@ where let name_ptr = Self::name_ptr(ptr.cast::()); let name = CStr16::from_ptr(name_ptr); let name_len = name.as_slice_with_nul().len(); - &mut *ptr::from_raw_parts_mut(ptr.cast::<()>(), name_len) + &mut *ptr_meta::from_raw_parts_mut(ptr.cast::<()>(), name_len) } } @@ -140,7 +141,7 @@ pub enum FileInfoCreationError { /// existing file in the same directory. /// - If a file is read-only, the only allowed change is to remove the read-only /// attribute. Other changes must be carried out in a separate transaction. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] #[repr(C)] pub struct FileInfo { size: u64, @@ -254,7 +255,7 @@ impl FileProtocolInfo for FileInfo {} /// /// Please note that only the system volume's volume label may be set using /// this information structure. Consider using `FileSystemVolumeLabel` instead. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] #[repr(C)] pub struct FileSystemInfo { size: u64, @@ -346,7 +347,7 @@ impl FileProtocolInfo for FileSystemInfo {} /// System volume label /// /// May only be obtained on the root directory's file handle. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Pointee)] #[repr(C)] pub struct FileSystemVolumeLabel { volume_label: [Char16], diff --git a/uefi/src/proto/tcg/v1.rs b/uefi/src/proto/tcg/v1.rs index 0ab59ac14..5152cd9c2 100644 --- a/uefi/src/proto/tcg/v1.rs +++ b/uefi/src/proto/tcg/v1.rs @@ -15,6 +15,7 @@ use crate::{Result, Status}; use core::fmt::{self, Debug, Formatter}; use core::marker::PhantomData; use core::{mem, ptr}; +use ptr_meta::Pointee; /// 20-byte SHA-1 digest. pub type Sha1Digest = [u8; 20]; @@ -96,6 +97,7 @@ pub struct Version { /// field. These two are independent; although the event data _can_ be /// what is hashed in the digest field, it doesn't have to be. #[repr(C, packed)] +#[derive(Pointee)] pub struct PcrEvent { pcr_index: PcrIndex, event_type: EventType, @@ -110,7 +112,7 @@ impl PcrEvent { let ptr_u32: *const u32 = ptr.cast(); let event_size = ptr_u32.add(7).read_unaligned(); let event_size = usize_from_u32(event_size); - unsafe { &*ptr::from_raw_parts(ptr.cast(), event_size) } + unsafe { &*ptr_meta::from_raw_parts(ptr.cast(), event_size) } } /// PCR index for the event. diff --git a/xtask/src/device_path/field.rs b/xtask/src/device_path/field.rs index b43acfab2..a209c12c4 100644 --- a/xtask/src/device_path/field.rs +++ b/xtask/src/device_path/field.rs @@ -253,7 +253,7 @@ impl NodeField { ret_type = quote!(UnalignedSlice<#slice_elem>); ret_val = quote!( let ptr: *const [#slice_elem] = addr_of!(self.#field_name); - let (ptr, len): (*const (), usize) = ptr.to_raw_parts(); + let (ptr, len): (*const (), usize) = PtrExt::to_raw_parts(ptr); unsafe { UnalignedSlice::new(ptr.cast::<#slice_elem>(), len) } diff --git a/xtask/src/device_path/mod.rs b/xtask/src/device_path/mod.rs index d9369be72..f3626c61c 100644 --- a/xtask/src/device_path/mod.rs +++ b/xtask/src/device_path/mod.rs @@ -30,8 +30,9 @@ fn gen_code_as_string(groups: &[NodeGroup]) -> Result { use crate::proto::network::IpAddress; use crate::table::boot::MemoryType; use core::mem::{size_of, size_of_val}; - use core::ptr::{self, addr_of}; + use core::ptr::addr_of; use core::{fmt, slice}; + use ptr_meta::{Pointee, PtrExt}; #(#packed_modules)* diff --git a/xtask/src/device_path/node.rs b/xtask/src/device_path/node.rs index cc9ec9d86..bc9cb1b93 100644 --- a/xtask/src/device_path/node.rs +++ b/xtask/src/device_path/node.rs @@ -121,9 +121,17 @@ impl Node { fields.push(quote!(data: [u8])); } + // If the struct is a DST, derive the `ptr_meta::Pointee` trait. + let derive_pointee = if self.is_dst() { + quote!(#[derive(Pointee)]) + } else { + quote!() + }; + quote!( #(#struct_docs)* #[repr(C, packed)] + #derive_pointee pub struct #struct_ident { #(pub(super) #fields),* } @@ -169,7 +177,7 @@ impl Node { // slice instead. quote!({ let ptr = addr_of!(#field_val); - let (ptr, len) = ptr.to_raw_parts(); + let (ptr, len) = PtrExt::to_raw_parts(ptr); let byte_len = size_of::<#slice_elem_ty>() * len; unsafe { &slice::from_raw_parts(ptr.cast::(), byte_len) } }) @@ -216,7 +224,7 @@ impl Node { return Err(NodeConversionError::InvalidLength); } let node: *const DevicePathNode = node; - let node: *const #struct_ident = ptr::from_raw_parts(node.cast(), dst_size / elem_size); + let node: *const #struct_ident = ptr_meta::from_raw_parts(node.cast(), dst_size / elem_size); ) } else { quote!(