summaryrefslogtreecommitdiff
path: root/cgpt
diff options
context:
space:
mode:
authorNam T. Nguyen <namnguyen@chromium.org>2014-08-22 15:01:38 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-08-28 01:17:48 +0000
commita2d72f70c18905aba25eb0971f6f601dd1fa5a60 (patch)
tree42f9be2f6fb2d9bd5fd54a7d4b31d5c0c378e381 /cgpt
parentf510973497a430c1bb41d9b7e996d02fb7f7179e (diff)
downloadvboot-a2d72f70c18905aba25eb0971f6f601dd1fa5a60.tar.gz
vboot: cgpt: Refer to partition entries by entries_lba.
This CL accesses the partition entry array through its header's entries_lba value. Previously, we assume the primary entry array lies on third sector, and the secondary array lies (1 + 32) sectors from disk end. This assumption was fine, even Wikipedia assumed the same. But in order for us to support writing boot code to the third sector (as required by some Freescale board), the primary entry array must be moved to another location. Therefore, we must use "entries_lba" to locate the arrays from now on. BRANCH=none BUG=chromium:406432 TEST=unittest TEST=`cgpt create -p` and then `cgpt show`. Make sure the table header and entries are properly moved. Change-Id: Ia9008b0bb204f290b1f6240df562ce7d3a9bbff2 Reviewed-on: https://chromium-review.googlesource.com/213861 Reviewed-by: Bill Richardson <wfrichar@chromium.org> Tested-by: Bill Richardson <wfrichar@chromium.org> Commit-Queue: Nam Nguyen <namnguyen@chromium.org> Tested-by: Nam Nguyen <namnguyen@chromium.org>
Diffstat (limited to 'cgpt')
-rw-r--r--cgpt/cgpt_common.c15
-rw-r--r--cgpt/cgpt_create.c11
-rw-r--r--cgpt/cgpt_show.c7
-rw-r--r--cgpt/cmd_create.c7
4 files changed, 25 insertions, 15 deletions
diff --git a/cgpt/cgpt_common.c b/cgpt/cgpt_common.c
index bfc13864..44652a37 100644
--- a/cgpt/cgpt_common.c
+++ b/cgpt/cgpt_common.c
@@ -318,14 +318,15 @@ static int GptLoad(struct drive *drive, uint32_t sector_bytes) {
drive->gpt.sector_bytes, GPT_HEADER_SECTOR)) {
return -1;
}
+ GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header;
if (CGPT_OK != Load(drive, &drive->gpt.primary_entries,
- GPT_PMBR_SECTOR + GPT_HEADER_SECTOR,
+ primary_header->entries_lba,
drive->gpt.sector_bytes, GPT_ENTRIES_SECTORS)) {
return -1;
}
+ GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header;
if (CGPT_OK != Load(drive, &drive->gpt.secondary_entries,
- drive->gpt.drive_sectors - GPT_HEADER_SECTOR
- - GPT_ENTRIES_SECTORS,
+ secondary_header->entries_lba,
drive->gpt.sector_bytes, GPT_ENTRIES_SECTORS)) {
return -1;
}
@@ -351,18 +352,19 @@ static int GptSave(struct drive *drive) {
Error("Cannot write secondary header: %s\n", strerror(errno));
}
}
+ GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header;
if (drive->gpt.modified & GPT_MODIFIED_ENTRIES1) {
if (CGPT_OK != Save(drive, drive->gpt.primary_entries,
- GPT_PMBR_SECTOR + GPT_HEADER_SECTOR,
+ primary_header->entries_lba,
drive->gpt.sector_bytes, GPT_ENTRIES_SECTORS)) {
errors++;
Error("Cannot write primary entries: %s\n", strerror(errno));
}
}
+ GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header;
if (drive->gpt.modified & GPT_MODIFIED_ENTRIES2) {
if (CGPT_OK != Save(drive, drive->gpt.secondary_entries,
- drive->gpt.drive_sectors - GPT_HEADER_SECTOR
- - GPT_ENTRIES_SECTORS,
+ secondary_header->entries_lba,
drive->gpt.sector_bytes, GPT_ENTRIES_SECTORS)) {
errors++;
Error("Cannot write secondary entries: %s\n", strerror(errno));
@@ -1155,6 +1157,7 @@ uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers) {
memcpy(primary_header, secondary_header, sizeof(GptHeader));
primary_header->my_lba = GPT_PMBR_SECTOR; /* the second sector on drive */
primary_header->alternate_lba = secondary_header->my_lba;
+ /* TODO (namnguyen): Preserve (header, entries) padding space. */
primary_header->entries_lba = primary_header->my_lba + GPT_HEADER_SECTOR;
return GPT_MODIFIED_HEADER1;
}
diff --git a/cgpt/cgpt_create.c b/cgpt/cgpt_create.c
index 9f60b3a1..c9134da6 100644
--- a/cgpt/cgpt_create.c
+++ b/cgpt/cgpt_create.c
@@ -29,15 +29,16 @@ static int GptCreate(struct drive *drive, CgptCreateParams *params) {
memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
h->revision = GPT_HEADER_REVISION;
h->size = sizeof(GptHeader);
- h->my_lba = 1;
- h->alternate_lba = drive->gpt.drive_sectors - 1;
- h->first_usable_lba = 1 + 1 + GPT_ENTRIES_SECTORS;
- h->last_usable_lba = drive->gpt.drive_sectors - 1 - GPT_ENTRIES_SECTORS - 1;
+ h->my_lba = GPT_PMBR_SECTOR; /* The second sector on drive. */
+ h->alternate_lba = drive->gpt.drive_sectors - GPT_HEADER_SECTOR;
+ h->entries_lba = h->my_lba + GPT_HEADER_SECTOR + params->padding;
+ h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
+ h->last_usable_lba = (drive->gpt.drive_sectors - GPT_HEADER_SECTOR -
+ GPT_ENTRIES_SECTORS - 1);
if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
Error("Unable to generate new GUID.\n");
return -1;
}
- h->entries_lba = 2;
h->number_of_entries = 128;
h->size_of_entry = sizeof(GptEntry);
diff --git a/cgpt/cgpt_show.c b/cgpt/cgpt_show.c
index f5d6001e..3603770b 100644
--- a/cgpt/cgpt_show.c
+++ b/cgpt/cgpt_show.c
@@ -415,7 +415,8 @@ static int GptShow(struct drive *drive, CgptShowParams *params) {
HeaderDetails(header, entries, indent, params->numeric);
}
- printf(GPT_FMT, (int)(GPT_PMBR_SECTOR + GPT_HEADER_SECTOR),
+ GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header;
+ printf(GPT_FMT, (int)primary_header->entries_lba,
(int)GPT_ENTRIES_SECTORS,
drive->gpt.valid_entries & MASK_PRIMARY ? "" : "INVALID",
"Pri GPT table");
@@ -425,8 +426,8 @@ static int GptShow(struct drive *drive, CgptShowParams *params) {
EntriesDetails(drive, PRIMARY, params->numeric);
/****************************** Secondary *************************/
- printf(GPT_FMT, (int)(drive->gpt.drive_sectors - GPT_HEADER_SECTOR -
- GPT_ENTRIES_SECTORS),
+ GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header;
+ printf(GPT_FMT, (int)secondary_header->entries_lba,
(int)GPT_ENTRIES_SECTORS,
drive->gpt.valid_entries & MASK_SECONDARY ? "" : "INVALID",
"Sec GPT table");
diff --git a/cgpt/cmd_create.c b/cgpt/cmd_create.c
index c751b24d..ca1b815d 100644
--- a/cgpt/cmd_create.c
+++ b/cgpt/cmd_create.c
@@ -17,6 +17,8 @@ static void Usage(void)
"Options:\n"
" -z Zero the sectors of the GPT table and entries\n"
" -s Size (in byes) of the disk (MTD only)\n"
+ " -p Size (in blocks) of the disk to pad between the\n"
+ " primary GPT header and its entries, default 0\n"
"\n", progname);
}
@@ -29,7 +31,7 @@ int cmd_create(int argc, char *argv[]) {
char *e = 0;
opterr = 0; // quiet, you
- while ((c=getopt(argc, argv, ":hzs:")) != -1)
+ while ((c=getopt(argc, argv, ":hzsp:")) != -1)
{
switch (c)
{
@@ -39,6 +41,9 @@ int cmd_create(int argc, char *argv[]) {
case 's':
params.size = strtoull(optarg, &e, 0);
break;
+ case 'p':
+ params.padding = strtoull(optarg, &e, 0);
+ break;
case 'h':
Usage();