[parted-devel] [PATCH 1/2] gpt: Preserve primary partition entry array location

Pascal Hambourg pascal at plouf.fr.eu.org
Wed Nov 19 18:44:17 GMT 2025


The primary partition entry array may start at a location other than
LBA 2 in order to not overlap with the boot loader for some ARM SoCs.
E.g.:
- The boot loader for Freescale/NXP i.MX < 8 family must start at LBA 2.
- The boot loader for older Allwinner sunxi family must start at LBA 16.
---
 libparted/labels/gpt.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 780fb705..87261dbd 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -351,6 +351,7 @@ struct __attribute__ ((packed, aligned(8))) _GPTDiskData
   efi_guid_t uuid;
   int pmbr_boot;
   PedSector AlternateLBA;
+  PedSector PrimaryPartitionEntryLBA;
 };
 
 /* uses libparted's disk_specific field in PedPartition, to store our info */
@@ -601,6 +602,7 @@ gpt_alloc (const PedDevice *dev)
   uuid_generate ((unsigned char *) &gpt_disk_data->uuid);
   swap_uuid_and_efi_guid (&gpt_disk_data->uuid);
   gpt_disk_data->pmbr_boot = 0;
+  gpt_disk_data->PrimaryPartitionEntryLBA = GPT_PRIMARY_PART_TABLE_LBA;
   return disk;
 
 error_free_disk:
@@ -1036,6 +1038,8 @@ gpt_read (PedDisk *disk)
 #endif /* !DISCOVER_ONLY */
       pth_free (backup_gpt);
       gpt = primary_gpt;
+      gpt_disk_data->PrimaryPartitionEntryLBA =
+        PED_LE64_TO_CPU (primary_gpt->PartitionEntryLBA);
     }
   else if (!primary_gpt && !backup_gpt)
     {
@@ -1058,6 +1062,8 @@ gpt_read (PedDisk *disk)
         goto error_free_gpt;
 
       gpt = primary_gpt;
+      gpt_disk_data->PrimaryPartitionEntryLBA =
+        PED_LE64_TO_CPU (primary_gpt->PartitionEntryLBA);
     }
   else /* !primary_gpt && backup_gpt */
     {
@@ -1212,7 +1218,8 @@ _generate_header (const PedDisk *disk, int alternate, uint32_t ptes_crc,
     {
       gpt->MyLBA = PED_CPU_TO_LE64 (1);
       gpt->AlternateLBA = PED_CPU_TO_LE64 (gpt_disk_data->AlternateLBA);
-      gpt->PartitionEntryLBA = PED_CPU_TO_LE64 (2);
+      gpt->PartitionEntryLBA =
+        PED_CPU_TO_LE64 (gpt_disk_data->PrimaryPartitionEntryLBA);
     }
 
   gpt->FirstUsableLBA = PED_CPU_TO_LE64 (gpt_disk_data->data_area.start);
@@ -1301,7 +1308,8 @@ gpt_write (const PedDisk *disk)
   free (pth_raw);
   if (!write_ok)
     goto error_free_ptes;
-  if (!ped_device_write (disk->dev, ptes, 2, ptes_sectors))
+  if (!ped_device_write (disk->dev, ptes,
+      gpt_disk_data->PrimaryPartitionEntryLBA, ptes_sectors))
     goto error_free_ptes;
 
   /* Write Alternate PTH & PTEs */
@@ -1515,8 +1523,10 @@ gpt_alloc_metadata (PedDisk *disk)
                                  disk->dev->sector_size);
 
   /* metadata at the start of the disk includes the MBR */
-  if (!add_metadata_part (disk, GPT_PMBR_LBA,
-                          GPT_PMBR_SECTORS + gptlength + pteslength))
+  if (!add_metadata_part (disk, GPT_PMBR_LBA, GPT_PMBR_SECTORS + gptlength))
+    return 0;
+  if (!add_metadata_part (disk, gpt_disk_data->PrimaryPartitionEntryLBA,
+                          pteslength))
     return 0;
 
   /* metadata at the end of the disk */
-- 
2.39.5




More information about the parted-devel mailing list