@@ -80,9 +80,8 @@ pub use device_path_gen::{
8080 acpi, bios_boot_spec, end, hardware, media, messaging, DevicePathNodeEnum ,
8181} ;
8282
83- use crate :: data_types:: UnalignedSlice ;
8483use crate :: proto:: { Protocol , ProtocolPointer } ;
85- use crate :: { unsafe_guid, Guid } ;
84+ use crate :: unsafe_guid;
8685use core:: ffi:: c_void;
8786use core:: marker:: { PhantomData , PhantomPinned } ;
8887use core:: { mem, ptr} ;
@@ -177,39 +176,6 @@ impl DevicePathNode {
177176 pub fn as_enum ( & self ) -> Result < DevicePathNodeEnum , NodeConversionError > {
178177 DevicePathNodeEnum :: try_from ( self )
179178 }
180-
181- /// Convert to a [`FilePathMediaDevicePath`]. Returns `None` if the
182- /// node is not of the appropriate type.
183- pub fn as_file_path_media_device_path ( & self ) -> Option < & FilePathMediaDevicePath > {
184- if self . full_type ( ) == ( DeviceType :: MEDIA , DeviceSubType :: MEDIA_FILE_PATH ) {
185- // Get the length of the `path_name` field.
186- let path_name_size_in_bytes = usize:: from ( self . header . length ) - 4 ;
187- assert ! ( path_name_size_in_bytes % mem:: size_of:: <u16 >( ) == 0 ) ;
188- let path_name_len = path_name_size_in_bytes / mem:: size_of :: < u16 > ( ) ;
189-
190- // Convert the `self` pointer to `FilePathMediaDevicePath`,
191- // which is a DST as it ends in a slice.
192- let p = self as * const Self ;
193- let p: * const FilePathMediaDevicePath = ptr:: from_raw_parts ( p. cast ( ) , path_name_len) ;
194- Some ( unsafe { & * p } )
195- } else {
196- None
197- }
198- }
199-
200- /// Convert to a [`HardDriveMediaDevicePath`]. Returns `None` if the
201- /// node is not of the appropriate type.
202- pub fn as_hard_drive_media_device_path ( & self ) -> Option < & HardDriveMediaDevicePath > {
203- if self . full_type ( ) == ( DeviceType :: MEDIA , DeviceSubType :: MEDIA_HARD_DRIVE ) {
204- assert ! ( { self . header. length } == HARD_DRIVE_MEDIA_DEVICE_PATH_LENGTH ) ;
205-
206- let p = self as * const Self ;
207- let p = p. cast :: < HardDriveMediaDevicePath > ( ) ;
208- Some ( unsafe { & * p } )
209- } else {
210- None
211- }
212- }
213179}
214180
215181/// A single device path instance that ends with either an [`END_INSTANCE`]
@@ -578,131 +544,6 @@ impl DeviceSubType {
578544 pub const END_ENTIRE : DeviceSubType = DeviceSubType ( 0xff ) ;
579545}
580546
581- /// ACPI Device Path
582- #[ repr( C , packed) ]
583- pub struct AcpiDevicePath {
584- header : DevicePathHeader ,
585-
586- /// Device's PnP hardware ID stored in a numeric 32-bit compressed EISA-type ID. This value must match the
587- /// corresponding _HID in the ACPI name space.
588- pub hid : u32 ,
589- /// Unique ID that is required by ACPI if two devices have the same _HID. This value must also match the
590- /// corresponding _UID/_HID pair in the ACPI name space. Only the 32-bit numeric value type of _UID is supported;
591- /// thus strings must not be used for the _UID in the ACPI name space.
592- pub uid : u32 ,
593- }
594-
595- /// File Path Media Device Path.
596- #[ repr( C , packed) ]
597- pub struct FilePathMediaDevicePath {
598- header : DevicePathHeader ,
599- path_name : [ u16 ] ,
600- }
601-
602- impl FilePathMediaDevicePath {
603- /// Get the path. An [`UnalignedSlice`] is returned since this is a
604- /// packed struct.
605- pub fn path_name ( & self ) -> UnalignedSlice < u16 > {
606- // Safety: creating this `UnalignedSlice` is safe because the
607- // `path_name` pointer is valid (although potentially
608- // unaligned), and the lifetime of the output is tied to `self`,
609- // so there's no possibility of use-after-free.
610- unsafe {
611- // Use `addr_of` to avoid creating an unaligned reference.
612- let ptr: * const [ u16 ] = ptr:: addr_of!( self . path_name) ;
613- let ( ptr, len) : ( * const ( ) , usize ) = ptr. to_raw_parts ( ) ;
614- UnalignedSlice :: new ( ptr. cast :: < u16 > ( ) , len)
615- }
616- }
617- }
618-
619- /// Hard Drive Media Device Path.
620- #[ repr( C , packed) ]
621- pub struct HardDriveMediaDevicePath {
622- header : DevicePathHeader ,
623- partition_number : u32 ,
624- partition_start : u64 ,
625- partition_size : u64 ,
626- partition_signature : PartitionSignatureUnion ,
627- partition_format : PartitionFormat ,
628- signature_type : SignatureType ,
629- }
630-
631- /// [`HardDriveMediaDevicePath`] is a fixed-length structure of 42 bytes.
632- const HARD_DRIVE_MEDIA_DEVICE_PATH_LENGTH : u16 = 42 ;
633-
634- impl HardDriveMediaDevicePath {
635- /// Returns the format of the partition (MBR, GPT, or unknown).
636- pub const fn partition_format ( & self ) -> PartitionFormat {
637- self . partition_format
638- }
639-
640- /// Returns the 1-based index of the partition.
641- pub const fn partition_number ( & self ) -> u32 {
642- self . partition_number
643- }
644-
645- /// Returns the partition size in logical blocks.
646- pub const fn partition_size ( & self ) -> u64 {
647- self . partition_size
648- }
649-
650- /// Returns the starting LBA of the partition.
651- pub const fn partition_start ( & self ) -> u64 {
652- self . partition_start
653- }
654-
655- /// Returns the MBR or GPT partition signature
656- pub fn partition_signature ( & self ) -> Option < PartitionSignature > {
657- match self . signature_type {
658- SignatureType :: MBR => {
659- let mbr_signature = unsafe { self . partition_signature . mbr_signature } ;
660- Some ( PartitionSignature :: MBR ( mbr_signature) )
661- }
662- SignatureType :: GUID => {
663- let guid = unsafe { self . partition_signature . guid } ;
664- Some ( PartitionSignature :: GUID ( guid) )
665- }
666- _ => None ,
667- }
668- }
669- }
670-
671- newtype_enum ! {
672- /// Partition format.
673- pub enum PartitionFormat : u8 => {
674- /// MBR (PC-AT compatible Master Boot Record) format.
675- MBR = 0x01 ,
676- /// GPT (GUID Partition Table) format.
677- GPT = 0x02 ,
678- }
679- }
680-
681- /// Partition signature.
682- #[ derive( Clone , Debug , Eq , PartialEq ) ]
683- pub enum PartitionSignature {
684- /// 32-bit MBR partition signature.
685- MBR ( u32 ) ,
686- /// 128-bit GUID partition signature.
687- GUID ( Guid ) ,
688- }
689-
690- #[ repr( C ) ]
691- union PartitionSignatureUnion {
692- mbr_signature : u32 ,
693- guid : Guid ,
694- }
695-
696- newtype_enum ! {
697- /// Signature type.
698- enum SignatureType : u8 => {
699- /// 32-bit MBR partition signature.
700- MBR = 0x01 ,
701- /// 128-bit GUID partition signature.
702- GUID = 0x02 ,
703- }
704- }
705-
706547/// Error returned when converting from a [`DevicePathNode`] to a more
707548/// specific node type.
708549#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
@@ -717,7 +558,6 @@ pub enum NodeConversionError {
717558#[ cfg( test) ]
718559mod tests {
719560 use super :: * ;
720- use crate :: { guid, CString16 } ;
721561 use alloc_api:: vec:: Vec ;
722562
723563 /// Create a node to `path` from raw data.
@@ -823,76 +663,4 @@ mod tests {
823663 // Only two instances.
824664 assert ! ( iter. next( ) . is_none( ) ) ;
825665 }
826-
827- #[ test]
828- fn test_file_path_media ( ) {
829- // Manually create data for a `FilePathMediaDevicePath` node.
830- let raw_data: [ u16 ; 7 ] = [
831- // MEDIA | MEDIA_FILE_PATH
832- 0x0404 ,
833- // Length
834- 0x00_0e ,
835- b't' . into ( ) ,
836- b'e' . into ( ) ,
837- b's' . into ( ) ,
838- b't' . into ( ) ,
839- // Trailing null.
840- 0 ,
841- ] ;
842-
843- // Convert the raw data to a `DevicePath` node.
844- let dp = unsafe { DevicePathNode :: from_ffi_ptr ( raw_data. as_ptr ( ) . cast ( ) ) } ;
845- assert_eq ! ( dp. length( ) , 14 ) ;
846-
847- // Check that the `file_name` is correct.
848- let fpm = dp. as_file_path_media_device_path ( ) . unwrap ( ) ;
849- assert_eq ! (
850- fpm. path_name( ) . to_cstring16( ) . unwrap( ) ,
851- CString16 :: try_from( "test" ) . unwrap( )
852- ) ;
853- }
854-
855- #[ test]
856- fn test_hard_drive_media_mbr ( ) {
857- let mbr_partition_bytes: [ u8 ; 42 ] = [
858- 0x04 , 0x01 , 0x2a , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x3f , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
859- 0x00 , 0x00 , 0xc1 , 0xbf , 0x0f , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xfa , 0xfd , 0x1a , 0xbe ,
860- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x01 ,
861- ] ;
862- let dp = unsafe { DevicePathNode :: from_ffi_ptr ( mbr_partition_bytes. as_ptr ( ) . cast ( ) ) } ;
863- assert_eq ! ( dp. length( ) , HARD_DRIVE_MEDIA_DEVICE_PATH_LENGTH ) ;
864-
865- let hdm = dp. as_hard_drive_media_device_path ( ) . unwrap ( ) ;
866- assert_eq ! ( hdm. partition_format( ) , PartitionFormat :: MBR ) ;
867- assert_eq ! ( hdm. partition_number( ) , 1 ) ;
868- assert_eq ! ( hdm. partition_size( ) , 1032129 ) ;
869- assert_eq ! ( hdm. partition_start( ) , 63 ) ;
870- assert_eq ! (
871- hdm. partition_signature( ) ,
872- Some ( PartitionSignature :: MBR ( 0xBE1AFDFA ) )
873- ) ;
874- }
875-
876- #[ test]
877- fn test_hard_drive_media_gpt ( ) {
878- let guid_partition_bytes: [ u8 ; 42 ] = [
879- 0x04 , 0x01 , 0x2a , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x80 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
880- 0x00 , 0x00 , 0x00 , 0x10 , 0x03 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xa0 , 0x39 , 0xaa , 0x41 ,
881- 0x35 , 0x3d , 0x84 , 0x4f , 0xb1 , 0x95 , 0xae , 0x3a , 0x95 , 0x0b , 0xfb , 0xad , 0x02 , 0x02 ,
882- ] ;
883- let dp = unsafe { DevicePathNode :: from_ffi_ptr ( guid_partition_bytes. as_ptr ( ) . cast ( ) ) } ;
884- assert_eq ! ( dp. length( ) , HARD_DRIVE_MEDIA_DEVICE_PATH_LENGTH ) ;
885-
886- let hdm = dp. as_hard_drive_media_device_path ( ) . unwrap ( ) ;
887- assert_eq ! ( hdm. partition_format( ) , PartitionFormat :: GPT ) ;
888- assert_eq ! ( hdm. partition_number( ) , 1 ) ;
889- assert_eq ! ( hdm. partition_size( ) , 200704 ) ;
890- assert_eq ! ( hdm. partition_start( ) , 128 ) ;
891- assert_eq ! (
892- hdm. partition_signature( ) ,
893- Some ( PartitionSignature :: GUID ( guid!(
894- "41aa39a0-3d35-4f84-b195-ae3a950bfbad"
895- ) ) )
896- ) ;
897- }
898666}
0 commit comments