@@ -182,6 +182,8 @@ pub struct GPTHeader {
182
182
/// 16 bytes representing the UUID of the GPT.
183
183
pub disk_guid : [ u8 ; 16 ] ,
184
184
/// Location (in sectors) of the partition entries array.
185
+ ///
186
+ /// This is always `2` if the header is a primary header and not the backup header.
185
187
pub partition_entry_lba : u64 ,
186
188
/// Number of partition entries in the array.
187
189
pub number_of_partition_entries : u32 ,
@@ -317,9 +319,9 @@ impl GPTHeader {
317
319
self . partition_entry_array_crc32 = self . generate_partition_entry_array_crc32 ( partitions) ;
318
320
}
319
321
320
- /// Updates the header to match the specifications of the reader given in argument.
321
- /// `first_usable_lba`, `last_usable_lba`, `primary_lba`, `backup_lba` will be updated after
322
- /// this operation.
322
+ /// Updates the header to match the specifications of the seeker given in argument.
323
+ /// `first_usable_lba`, `last_usable_lba`, `primary_lba`, `backup_lba`, `partition_entry_lba`
324
+ /// will be updated after this operation.
323
325
pub fn update_from < S : ?Sized > ( & mut self , seeker : & mut S , sector_size : u64 ) -> Result < ( ) >
324
326
where
325
327
S : Seek ,
@@ -337,6 +339,13 @@ impl GPTHeader {
337
339
}
338
340
self . last_usable_lba = len - partition_array_size - 1 - 1 ;
339
341
self . first_usable_lba = 2 + partition_array_size;
342
+ // NOTE: the partition_entry_lba is either 2 either something near the end of the disk.
343
+ // If it is something near the end of the disk, it means the self object is a backup
344
+ // GPT header (which is located at the end of the disk) and its partition_entry_lba
345
+ // must be updated accordingly
346
+ if self . partition_entry_lba != 2 {
347
+ self . partition_entry_lba = self . last_usable_lba + 1 ;
348
+ }
340
349
341
350
Ok ( ( ) )
342
351
}
@@ -783,7 +792,24 @@ impl GPT {
783
792
/// Write the GPT to a writer. This function will seek automatically in the writer to write the
784
793
/// primary header and the backup header at their proper location.
785
794
///
786
- /// # Examples:
795
+ /// # Implementation notes
796
+ ///
797
+ /// Calling this function will call `update_from` in order to update the `last_usable_lba`
798
+ /// among other fields of the GPT header, thus modifying the `self` object.
799
+ ///
800
+ /// # Errors
801
+ ///
802
+ /// The partitions will be checked for consistency before being wrote to disk:
803
+ ///
804
+ /// * the partition GUIDs must be unique,
805
+ /// * the partitions must have positive size,
806
+ /// * the partitions must not overlap,
807
+ /// * the partitions must fit within the disk.
808
+ ///
809
+ /// Note that the `self` object will be updated even if one of those error occurs.
810
+ ///
811
+ /// # Examples
812
+ ///
787
813
/// Basic usage:
788
814
/// ```
789
815
/// let ss = 512;
@@ -800,11 +826,9 @@ impl GPT {
800
826
where
801
827
W : Write + Seek ,
802
828
{
803
- self . check_partition_guids ( ) ?;
804
829
self . header . update_from ( & mut writer, self . sector_size ) ?;
805
- if self . header . partition_entry_lba != 2 {
806
- self . header . partition_entry_lba = self . header . last_usable_lba + 1 ;
807
- }
830
+
831
+ self . check_partition_guids ( ) ?;
808
832
self . check_partition_boundaries ( ) ?;
809
833
810
834
let mut backup = self . header . clone ( ) ;
0 commit comments