Hello,
I'm working with some hardware that boots off an sdcard and required
U-Boot be at LBA 2.
I'd like to use GPT partitioning on the sdcard, but by default GPT
stores partition entries at LBA 2.
I've attached my first draft patch that adds an optional extra parameter
to mklabel to allow creating a GPT with a different offset. Any
feedback would be great.
Thanks,
Derek
>From f520cd01ea9a499a4da943ee4530be61adec2cdd Mon Sep 17 00:00:00 2001
From: Derek Foreman <[email protected]>
Date: Wed, 3 Apr 2013 13:27:05 -0500
Subject: [PATCH] Add an optional offset for GPT partition entries
---
include/parted/disk.in.h | 3 +++
libparted/labels/gpt.c | 22 +++++++++++++++++-----
parted/parted.c | 18 ++++++++++++++++++
3 files changed, 38 insertions(+), 5 deletions(-)
diff --git a/include/parted/disk.in.h b/include/parted/disk.in.h
index aa905c5..f645ef5 100644
--- a/include/parted/disk.in.h
+++ b/include/parted/disk.in.h
@@ -210,6 +210,9 @@ struct _PedDiskOps {
int (*disk_is_flag_available) (
const PedDisk *disk,
PedDiskFlag flag);
+ int (*disk_set_partitions_offset) (
+ const PedDisk *disk,
+ PedSector offset);
/** \todo add label guessing op here */
/* partition operations */
diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 490de70..205b47d 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -269,6 +269,7 @@ struct __attribute__ ((packed)) _GPTDiskData
int entry_count;
efi_guid_t uuid;
int pmbr_boot;
+ int partitions_offset;
};
/* uses libparted's disk_specific field in PedPartition, to store our info */
@@ -529,6 +530,7 @@ gpt_alloc (const PedDevice *dev)
uuid_generate ((unsigned char *) &gpt_disk_data->uuid);
swap_uuid_and_efi_guid ((unsigned char *) (&gpt_disk_data->uuid));
gpt_disk_data->pmbr_boot = 0;
+ gpt_disk_data->partitions_offset = 2;
return disk;
error_free_disk:
@@ -713,12 +715,12 @@ _parse_header (PedDisk *disk, const GuidPartitionTableHeader_t *gpt,
parted invocation. */
last_usable_if_grown
- = (disk->dev->length - 2 -
+ = (disk->dev->length - gpt->PartitionEntryLBA -
((PedSector) (PED_LE32_TO_CPU (gpt->NumberOfPartitionEntries)) *
(PedSector) (PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry)) /
disk->dev->sector_size));
- last_usable_min_default = disk->dev->length - 2 -
+ last_usable_min_default = disk->dev->length - gpt->PartitionEntryLBA -
GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE / disk->dev->sector_size;
if (last_usable_if_grown > last_usable_min_default)
@@ -765,7 +767,7 @@ _parse_header (PedDisk *disk, const GuidPartitionTableHeader_t *gpt,
PED_ASSERT (gpt_disk_data->entry_count <= 8192);
gpt_disk_data->uuid = gpt->DiskGUID;
-
+ gpt_disk_data->partitions_offset = gpt->PartitionEntryLBA;
return 1;
}
@@ -1158,7 +1160,7 @@ _generate_header (const PedDisk *disk, int alternate, uint32_t ptes_crc,
{
gpt->MyLBA = PED_CPU_TO_LE64 (1);
gpt->AlternateLBA = PED_CPU_TO_LE64 (disk->dev->length - 1);
- gpt->PartitionEntryLBA = PED_CPU_TO_LE64 (2);
+ gpt->PartitionEntryLBA = PED_CPU_TO_LE64 (gpt_disk_data->partitions_offset);
}
gpt->FirstUsableLBA = PED_CPU_TO_LE64 (gpt_disk_data->data_area.start);
@@ -1251,7 +1253,7 @@ 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->partitions_offset, ptes_sectors))
goto error_free_ptes;
/* Write Alternate PTH & PTEs */
@@ -1872,6 +1874,15 @@ gpt_partition_align (PedPartition *part, const PedConstraint *constraint)
return 0;
}
+static int
+gpt_disk_set_partitions_offset(const PedDisk *disk, PedSector offset)
+{
+ GPTDiskData *gpt_disk_data = disk->disk_specific;
+
+ gpt_disk_data->partitions_offset = offset;
+ return 1;
+}
+
#include "pt-common.h"
PT_define_limit_functions (gpt)
@@ -1885,6 +1896,7 @@ static PedDiskOps gpt_disk_ops =
disk_set_flag: gpt_disk_set_flag,
disk_get_flag: gpt_disk_get_flag,
disk_is_flag_available: gpt_disk_is_flag_available,
+ disk_set_partitions_offset: gpt_disk_set_partitions_offset,
PT_op_function_initializers (gpt)
};
diff --git a/parted/parted.c b/parted/parted.c
index b20d432..617ea75 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -488,6 +488,10 @@ do_mklabel (PedDevice** dev)
{
PedDisk* disk;
const PedDiskType* type = NULL;
+ PedGeometry *range = NULL;
+ PedSector offset;
+ char *peek_word;
+ int set_offset = 0;
ped_exception_fetch_all ();
disk = ped_disk_new (*dev);
@@ -497,6 +501,17 @@ do_mklabel (PedDevice** dev)
if (!command_line_get_disk_type (_("New disk label type?"), &type))
goto error;
+ peek_word = command_line_peek_word ();
+ if (peek_word && isdigit (peek_word[0])) {
+ if (!command_line_get_sector (_("Offset?"), *dev, &offset, &range, NULL))
+ goto error;
+ else
+ set_offset = 1;
+ }
+
+ if (set_offset && !type->ops->disk_set_partitions_offset)
+ goto error;
+
if (disk) {
if (!_disk_warn_busy (disk))
goto error_destroy_disk;
@@ -510,6 +525,9 @@ do_mklabel (PedDevice** dev)
if (!disk)
goto error;
+ if (set_offset && !type->ops->disk_set_partitions_offset(disk, offset))
+ goto error_destroy_disk;
+
if (!ped_disk_commit (disk))
goto error_destroy_disk;
ped_disk_destroy (disk);
--
1.7.10.4