From 427fdc58e13d308677e431aac4d4c1c6f64e6446 Mon Sep 17 00:00:00 2001 From: Michael Marineau Date: Wed, 21 Sep 2016 14:33:48 -0700 Subject: [PATCH] gpt: selectively update fields during repair Just a little cleanup/refactor to skip touching data we don't need to. --- grub-core/lib/gpt.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/grub-core/lib/gpt.c b/grub-core/lib/gpt.c index 4d646aacc..1b7e219f6 100644 --- a/grub-core/lib/gpt.c +++ b/grub-core/lib/gpt.c @@ -577,8 +577,6 @@ grub_gpt_get_partentry (grub_gpt_t gpt, grub_uint32_t n) grub_err_t grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt) { - grub_uint64_t backup_header, backup_entries; - /* Skip if there is nothing to do. */ if (grub_gpt_both_valid (gpt)) return GRUB_ERR_NONE; @@ -591,6 +589,8 @@ grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt) if (grub_gpt_primary_valid (gpt)) { + grub_uint64_t backup_header; + grub_dprintf ("gpt", "primary GPT is valid\n"); /* Relocate backup to end if disk if the disk has grown. */ @@ -601,32 +601,28 @@ grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt) backup_header = disk->total_sectors - 1; grub_dprintf ("gpt", "backup GPT header relocated to 0x%llx\n", (unsigned long long) backup_header); + + gpt->primary.alternate_lba = grub_cpu_to_le64 (backup_header); } grub_memcpy (&gpt->backup, &gpt->primary, sizeof (gpt->backup)); + gpt->backup.header_lba = gpt->primary.alternate_lba; + gpt->backup.alternate_lba = gpt->primary.header_lba; + gpt->backup.partitions = grub_cpu_to_le64 (backup_header - + grub_gpt_size_to_sectors (gpt, gpt->entries_size)); } else if (grub_gpt_backup_valid (gpt)) { grub_dprintf ("gpt", "backup GPT is valid\n"); - backup_header = grub_le_to_cpu64 (gpt->backup.header_lba); + grub_memcpy (&gpt->primary, &gpt->backup, sizeof (gpt->primary)); + gpt->primary.header_lba = gpt->backup.alternate_lba; + gpt->primary.alternate_lba = gpt->backup.header_lba; + gpt->primary.partitions = grub_cpu_to_le64_compile_time (2); } else return grub_error (GRUB_ERR_BUG, "No valid GPT"); - backup_entries = backup_header - - grub_gpt_size_to_sectors (gpt, gpt->entries_size); - grub_dprintf ("gpt", "backup GPT entries will be located at 0x%llx\n", - (unsigned long long) backup_entries); - - /* Update/fixup header and partition table locations. */ - gpt->primary.header_lba = grub_cpu_to_le64_compile_time (1); - gpt->primary.alternate_lba = grub_cpu_to_le64 (backup_header); - gpt->primary.partitions = grub_cpu_to_le64_compile_time (2); - gpt->backup.header_lba = gpt->primary.alternate_lba; - gpt->backup.alternate_lba = gpt->primary.header_lba; - gpt->backup.partitions = grub_cpu_to_le64 (backup_entries); - /* Recompute checksums. */ if (grub_gpt_update_checksums (gpt)) return grub_errno;