1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Collections . Immutable ;
4+ using System . ComponentModel . DataAnnotations ;
5+ using System . IO ;
6+ using System . Text ;
27using TotalImage . Containers ;
38
49namespace TotalImage . Partitions
510{
611 /// <summary>
7- /// A partition table class that represents a GPT partition table
12+ /// A partition table class that represents a GUID Partition Table
813 /// </summary>
914 public class GptPartitionTable : PartitionTable
1015 {
16+ private GptHeader ? _header = null ;
1117 private readonly uint _sectorSize ;
1218
19+ /// <summary>
20+ /// The header for the GUID Partition Table
21+ /// </summary>
22+ public GptHeader ? Header => _header ;
23+
1324 /// <inheritdoc />
1425 public GptPartitionTable ( Container container , uint sectorSize = 512 ) : base ( container )
1526 {
@@ -19,7 +30,190 @@ public GptPartitionTable(Container container, uint sectorSize = 512) : base(cont
1930 /// <inheritdoc />
2031 protected override IEnumerable < PartitionEntry > LoadPartitions ( )
2132 {
22- throw new System . NotImplementedException ( ) ;
33+ using BinaryReader br = new BinaryReader ( _container . Content , Encoding . ASCII , true ) ;
34+ br . BaseStream . Seek ( _sectorSize , SeekOrigin . Begin ) ;
35+
36+ _header = new GptHeader ( br ) ;
37+
38+ if ( _header . Signature != "EFI PART" )
39+ {
40+ throw new InvalidDataException ( "Not a GPT Protective Partition" ) ;
41+ }
42+
43+ br . BaseStream . Seek ( ( long ) ( _sectorSize * _header . TableLBA ) , SeekOrigin . Begin ) ;
44+
45+ var entries = ImmutableList . CreateBuilder < PartitionEntry > ( ) ;
46+ for ( int i = 0 ; i < _header . PartitionEntries ; i ++ )
47+ {
48+ Guid typeId = new Guid ( br . ReadBytes ( 16 ) ) ;
49+ Guid entryId = new Guid ( br . ReadBytes ( 16 ) ) ;
50+ ulong firstLba = br . ReadUInt64 ( ) ;
51+ ulong lastLba = br . ReadUInt64 ( ) ;
52+ GptPartitionFlags flags = ( GptPartitionFlags ) br . ReadUInt64 ( ) ;
53+ string name = Encoding . Unicode . GetString ( br . ReadBytes ( 72 ) ) ;
54+
55+ long offset = ( long ) ( firstLba * _sectorSize ) ;
56+ long length = ( long ) ( ( lastLba - firstLba + 1 ) * _sectorSize ) ;
57+
58+ long additionalBytes = _header . SizeOfPartitionEntry - 0x80 ;
59+ if ( additionalBytes > 0 )
60+ {
61+ br . BaseStream . Seek ( additionalBytes , SeekOrigin . Current ) ;
62+ }
63+
64+ if ( typeId != Guid . Empty )
65+ {
66+ entries . Add ( new GptPartitionEntry ( typeId , entryId , flags , name , offset , length , new PartialStream ( _container . Content , offset , length ) ) ) ;
67+ }
68+ }
69+
70+ return entries . ToImmutableList ( ) ;
71+ }
72+
73+ /// <summary>
74+ /// Represents the header of a GUID Partition Table
75+ /// </summary>
76+ public class GptHeader
77+ {
78+ /// <summary>
79+ /// The signature of a GUID Partition Table header - should be "EFI PART"
80+ /// </summary>
81+ public string Signature { get ; }
82+
83+ /// <summary>
84+ /// The major version number of the GUID Partition Table
85+ /// </summary>
86+ public ushort VersionMajor { get ; }
87+
88+ /// <summary>
89+ /// The minor version of the GUID Partition Table
90+ /// </summary>
91+ public ushort VersionMinor { get ; }
92+
93+ /// <summary>
94+ /// The size of the GUID Partition Table header
95+ /// </summary>
96+ public uint HeaderSize { get ; }
97+
98+ /// <summary>
99+ /// The CRC32 of the GUID Partition Table header
100+ /// </summary>
101+ public uint HeaderHash { get ; }
102+
103+ /// <summary>
104+ /// The LBA location of the current header
105+ /// </summary>
106+ public ulong CurrentLBA { get ; }
107+
108+ /// <summary>
109+ /// The LBA location of the alternative copy of the header
110+ /// </summary>
111+ public ulong BackupLBA { get ; }
112+
113+ /// <summary>
114+ /// The first LBA available for use in GPT partitions
115+ /// </summary>
116+ public ulong FirstPartitionLBA { get ; }
117+
118+ /// <summary>
119+ /// The last LBA available for use in GPT partitions
120+ /// </summary>
121+ public ulong LastPartitionLBA { get ; }
122+
123+ /// <summary>
124+ /// The GUID of this disk
125+ /// </summary>
126+ public Guid DiskGuid { get ; }
127+ /// <summary>
128+ /// The starting LBA of the partition table
129+ /// </summary>
130+ public ulong TableLBA { get ; }
131+
132+ /// <summary>
133+ /// The number of entries in the partition table
134+ /// </summary>
135+ public uint PartitionEntries { get ; }
136+
137+ /// <summary>
138+ /// The size in bytes of each partition entry
139+ /// </summary>
140+ public uint SizeOfPartitionEntry { get ; }
141+
142+ /// <summary>
143+ /// The CRC32 of the partition entries together
144+ /// </summary>
145+ public uint TableHash { get ; }
146+
147+ /// <summary>
148+ /// Read a GUID Partition Table header from a stream
149+ /// </summary>
150+ /// <param name="reader">The binary reader for the stream</param>
151+ public GptHeader ( BinaryReader reader )
152+ {
153+ Signature = new string ( reader . ReadChars ( 8 ) ) ;
154+ uint version = reader . ReadUInt32 ( ) ;
155+ VersionMajor = ( ushort ) ( version >> 16 ) ;
156+ VersionMinor = ( ushort ) ( version & 0xFFFF ) ;
157+ HeaderSize = reader . ReadUInt32 ( ) ;
158+ HeaderHash = reader . ReadUInt32 ( ) ;
159+ reader . BaseStream . Seek ( 4 , SeekOrigin . Current ) ; // skip past reserved field
160+ CurrentLBA = reader . ReadUInt64 ( ) ;
161+ BackupLBA = reader . ReadUInt64 ( ) ;
162+ FirstPartitionLBA = reader . ReadUInt64 ( ) ;
163+ LastPartitionLBA = reader . ReadUInt64 ( ) ;
164+ DiskGuid = new Guid ( reader . ReadBytes ( 16 ) ) ;
165+ TableLBA = reader . ReadUInt64 ( ) ;
166+ PartitionEntries = reader . ReadUInt32 ( ) ;
167+ SizeOfPartitionEntry = reader . ReadUInt32 ( ) ;
168+ TableHash = reader . ReadUInt32 ( ) ;
169+ }
170+ }
171+
172+ /// <summary>
173+ /// Represents an entry in a GUID Partition Table
174+ /// </summary>
175+ public class GptPartitionEntry : PartitionEntry
176+ {
177+ public Guid TypeId { get ; }
178+
179+ public Guid EntryId { get ; }
180+
181+ public GptPartitionFlags Flags { get ; }
182+
183+ public string Name { get ; }
184+
185+ public GptPartitionEntry ( Guid typeId , Guid entryId , GptPartitionFlags flags , string name , long offset , long length , Stream stream ) : base ( offset , length , stream )
186+ {
187+ TypeId = typeId ;
188+ EntryId = entryId ;
189+ Flags = flags ;
190+ Name = name ;
191+ }
192+ }
193+
194+ /// <summary>
195+ /// Represents any flags set on the partition entry
196+ /// </summary>
197+ [ Flags ]
198+ public enum GptPartitionFlags : ulong
199+ {
200+ /// <summary>
201+ /// Indicates no flags are set
202+ /// </summary>
203+ [ Display ( Name = "No flags" ) ]
204+ None = 0x0 ,
205+
206+ /// <summary>
207+ /// Indicates EFI firmware should ignore the partition
208+ /// </summary>
209+ [ Display ( Name = "Should be ignored by EFI" ) ]
210+ EfiIgnore = 0x1 ,
211+
212+ /// <summary>
213+ /// Legacy BIOSes can boot from this partition - equivalent to "active" flag in MBR
214+ /// </summary>
215+ [ Display ( Name = "Bootable by Legacy BIOS" ) ]
216+ BiosBootable = 0x2
23217 }
24218 }
25219}
0 commit comments