@@ -12,6 +12,13 @@ use serde::Deserialize;
1212
1313use bootc_utils:: CommandRunExt ;
1414
15+ /// EFI System Partition (ESP) on MBR
16+ /// Refer to https://en.wikipedia.org/wiki/Partition_type
17+ pub const ESP_ID_MBR : & [ u8 ] = & [ 0x06 , 0xEF ] ;
18+
19+ /// EFI System Partition (ESP) for UEFI boot on GPT
20+ pub const ESP : & str = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" ;
21+
1522#[ derive( Debug , Deserialize ) ]
1623struct DevicesOutput {
1724 blockdevices : Vec < Device > ,
@@ -175,6 +182,19 @@ impl PartitionTable {
175182 pub fn find_partition_of_bootable ( & self ) -> Option < & Partition > {
176183 self . partitions . iter ( ) . find ( |p| p. is_bootable ( ) )
177184 }
185+
186+ /// Find the esp partition.
187+ pub fn find_partition_of_esp ( & self ) -> Result < Option < & Partition > > {
188+ match & self . label {
189+ PartitionType :: Dos => Ok ( self . partitions . iter ( ) . find ( |b| {
190+ u8:: from_str_radix ( & b. parttype , 16 )
191+ . map ( |pt| ESP_ID_MBR . contains ( & pt) )
192+ . unwrap_or ( false )
193+ } ) ) ,
194+ PartitionType :: Gpt => Ok ( self . find_partition_of_type ( ESP ) ) ,
195+ _ => Err ( anyhow:: anyhow!( "Unsupported partition table type" ) ) ,
196+ }
197+ }
178198}
179199
180200impl Partition {
@@ -612,10 +632,14 @@ mod test {
612632 . find_partition_of_type ( "00000000-0000-0000-0000-000000000000" ) ;
613633 assert ! ( nonexistent. is_none( ) ) ;
614634
635+ // Find esp partition on GPT
636+ let esp = table. partitiontable . find_partition_of_esp ( ) ?. unwrap ( ) ;
637+ assert_eq ! ( esp. node, "/dev/loop0p1" ) ;
638+
615639 Ok ( ( ) )
616640 }
617641 #[ test]
618- fn test_find_partition_of_bootable ( ) -> Result < ( ) > {
642+ fn test_find_partition_of_type_mbr ( ) -> Result < ( ) > {
619643 let fixture = indoc:: indoc! { r#"
620644 {
621645 "partitiontable": {
@@ -640,7 +664,7 @@ mod test {
640664 "node": "/dev/mmcblk0p3",
641665 "start": 3125248,
642666 "size": 121610240,
643- "type": "83 "
667+ "type": "ef "
644668 }
645669 ]
646670 }
@@ -655,6 +679,10 @@ mod test {
655679 . find_partition_of_bootable ( )
656680 . expect ( "bootable partition not found" ) ;
657681 assert_eq ! ( esp. node, "/dev/mmcblk0p1" ) ;
682+
683+ // Find esp partition on MBR
684+ let esp1 = table. partitiontable . find_partition_of_esp ( ) ?. unwrap ( ) ;
685+ assert_eq ! ( esp1. node, "/dev/mmcblk0p1" ) ;
658686 Ok ( ( ) )
659687 }
660688}
0 commit comments