cgpt/cgpt_common.c
/* Primary entries and secondary entries should be bitwise identical.
 * If two entries tables are valid, compare them. If not the same,
 * overwrites secondary with primary (primary always has higher priority),
 * and marks secondary as modified.
 * If only one is valid, overwrites invalid one.
 * If all are invalid, does nothing.
 * This function returns bit masks for GptData.modified field.
 * Note that CRC is NOT re-computed in this function.
 */
Primary entries and secondary entries should be bitwise identical.
If two entries tables are valid, compare them. If not the same,
overwrites secondary with primary (primary always has higher priority),
and marks secondary as modified.
If only one is valid, overwrites invalid one.
If all are invalid, does nothing.
This function returns bit masks for GptData.modified field.
Note that CRC is NOT re-computed in this function.
Primary entries and secondary entries should be bitwise identical . If two entries tables are valid compare them . If not the same overwrites secondary with primary ( primary always has higher priority ) and marks secondary as modified . If only one is valid overwrites invalid one . If all are invalid does nothing . This function returns bit masks for GptData . modified field . Note that CRC is NOT re - computed in this function .
uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries) {
  GptHeader* h = (GptHeader*)(gpt->primary_header);
  if (!memcmp(h->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE))
    return 0;
  if (gpt->valid_headers & MASK_PRIMARY) {
    h = (GptHeader*)gpt->primary_header;
  } else if (gpt->valid_headers & MASK_SECONDARY) {
    h = (GptHeader*)gpt->secondary_header;
  } else {
    return 0;
  }
  size_t entries_size = h->number_of_entries * h->size_of_entry;
  if (valid_entries == MASK_BOTH) {
    if (memcmp(gpt->primary_entries, gpt->secondary_entries, entries_size)) {
      memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size);
      return GPT_MODIFIED_ENTRIES2;
    }
  } else if (valid_entries == MASK_PRIMARY) {
    memcpy(gpt->secondary_entries, gpt->primary_entries, entries_size);
    return GPT_MODIFIED_ENTRIES2;
  } else if (valid_entries == MASK_SECONDARY) {
    memcpy(gpt->primary_entries, gpt->secondary_entries, entries_size);
    return GPT_MODIFIED_ENTRIES1;
  }
  return 0;
}
uint8_t RepairEntries ( GptData * gpt , const uint32_t valid_entries ) { GptHeader * h = ( GptHeader * ) ( gpt -> primary_header ) ; if ( ! memcmp ( h -> signature , GPT_HEADER_SIGNATURE2 , GPT_HEADER_SIGNATURE_SIZE ) ) return 0 ; if ( gpt -> valid_headers & MASK_PRIMARY ) { h = ( GptHeader * ) gpt -> primary_header ; } else if ( gpt -> valid_headers & MASK_SECONDARY ) { h = ( GptHeader * ) gpt -> secondary_header ; } else { return 0 ; } size_t entries_size = h -> number_of_entries * h -> size_of_entry ; if ( valid_entries == MASK_BOTH ) { if ( memcmp ( gpt -> primary_entries , gpt -> secondary_entries , entries_size ) ) { memcpy ( gpt -> secondary_entries , gpt -> primary_entries , entries_size ) ; return GPT_MODIFIED_ENTRIES2 ; } } else if ( valid_entries == MASK_PRIMARY ) { memcpy ( gpt -> secondary_entries , gpt -> primary_entries , entries_size ) ; return GPT_MODIFIED_ENTRIES2 ; } else if ( valid_entries == MASK_SECONDARY ) { memcpy ( gpt -> primary_entries , gpt -> secondary_entries , entries_size ) ; return GPT_MODIFIED_ENTRIES1 ; } return 0 ; }
Primary entries and secondary entries should be bitwise identical.
Primary entries and secondary entries should be bitwise identical .
/* If we have an alternate GPT header signature, don't overwrite
   * the secondary GPT with the primary one as that might wipe the
   * partition table. Also don't overwrite the primary one with the
   * secondary one as that will stop Windows from booting. */ /* We cannot trust any header, don't update entries. */