cgpt/cgpt_common.c
/* This function repairs primary and secondary headers if possible.
 * If both headers are valid (CRC32 is correct) but
 *   a) indicate inconsistent usable LBA ranges,
 *   b) inconsistent partition entry size and number,
 *   c) inconsistent disk_uuid,
 * we will use the primary header to overwrite secondary header.
 * If primary is invalid (CRC32 is wrong), then we repair it from secondary.
 * If secondary is invalid (CRC32 is wrong), then we repair it from primary.
 * This function returns the bitmasks for modified header.
 * Note that CRC value is NOT re-computed in this function. UpdateCrc() will
 * do it later.
 */
This function repairs primary and secondary headers if possible.
If both headers are valid (CRC32 is correct) but
a) indicate inconsistent usable LBA ranges,
b) inconsistent partition entry size and number,
c) inconsistent disk_uuid,
we will use the primary header to overwrite secondary header.
If primary is invalid (CRC32 is wrong), then we repair it from secondary.
If secondary is invalid (CRC32 is wrong), then we repair it from primary.
This function returns the bitmasks for modified header.
Note that CRC value is NOT re-computed in this function. UpdateCrc() will
do it later.
This function repairs primary and secondary headers if possible . If both headers are valid ( CRC32 is correct ) but a ) indicate inconsistent usable LBA ranges b ) inconsistent partition entry size and number c ) inconsistent disk_uuid we will use the primary header to overwrite secondary header . If primary is invalid ( CRC32 is wrong ) then we repair it from secondary . If secondary is invalid ( CRC32 is wrong ) then we repair it from primary . This function returns the bitmasks for modified header . Note that CRC value is NOT re - computed in this function . UpdateCrc () will do it later .
uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers) {
  GptHeader *primary_header, *secondary_header;
  primary_header = (GptHeader*)gpt->primary_header;
  secondary_header = (GptHeader*)gpt->secondary_header;
  if (valid_headers == MASK_BOTH) {
    if (!IsSynonymous(primary_header, secondary_header)) {
      CopySynonymousParts(secondary_header, primary_header);
      return GPT_MODIFIED_HEADER2;
    }
  } else if (valid_headers == MASK_PRIMARY) {
    memcpy(secondary_header, primary_header, sizeof(GptHeader));
    secondary_header->my_lba = gpt->gpt_drive_sectors - 1;  
    secondary_header->alternate_lba = primary_header->my_lba;
    secondary_header->entries_lba = secondary_header->my_lba -
        CalculateEntriesSectors(primary_header, gpt->sector_bytes);
    return GPT_MODIFIED_HEADER2;
  } else if (valid_headers == MASK_SECONDARY) {
    memcpy(primary_header, secondary_header, sizeof(GptHeader));
    primary_header->my_lba = GPT_PMBR_SECTORS;  
    primary_header->alternate_lba = secondary_header->my_lba;
    primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTORS;
    return GPT_MODIFIED_HEADER1;
  }
  return 0;
}
uint8_t RepairHeader ( GptData * gpt , const uint32_t valid_headers ) { GptHeader * primary_header , * secondary_header ; primary_header = ( GptHeader * ) gpt -> primary_header ; secondary_header = ( GptHeader * ) gpt -> secondary_header ; if ( valid_headers == MASK_BOTH ) { if ( ! IsSynonymous ( primary_header , secondary_header ) ) { CopySynonymousParts ( secondary_header , primary_header ) ; return GPT_MODIFIED_HEADER2 ; } } else if ( valid_headers == MASK_PRIMARY ) { memcpy ( secondary_header , primary_header , sizeof ( GptHeader ) ) ; secondary_header -> my_lba = gpt -> gpt_drive_sectors - 1 ; secondary_header -> alternate_lba = primary_header -> my_lba ; secondary_header -> entries_lba = secondary_header -> my_lba - CalculateEntriesSectors ( primary_header , gpt -> sector_bytes ) ; return GPT_MODIFIED_HEADER2 ; } else if ( valid_headers == MASK_SECONDARY ) { memcpy ( primary_header , secondary_header , sizeof ( GptHeader ) ) ; primary_header -> my_lba = GPT_PMBR_SECTORS ; primary_header -> alternate_lba = secondary_header -> my_lba ; primary_header -> entries_lba = primary_header -> my_lba + GPT_HEADER_SECTORS ; return GPT_MODIFIED_HEADER1 ; } return 0 ; }
This function repairs primary and secondary headers if possible.
This function repairs primary and secondary headers if possible .
/* the last sector */ /* the second sector on drive */ /* TODO (namnguyen): Preserve (header, entries) padding space. */