summaryrefslogtreecommitdiff
path: root/cgpt/cgpt_create.c
diff options
context:
space:
mode:
authorNam T. Nguyen <namnguyen@chromium.org>2014-10-24 13:20:39 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-11-13 18:29:09 +0000
commit6ee52d9a929d00e871e7316240b54f381146fbc6 (patch)
treeca403414469d9364d144a67e63f8f439874802e3 /cgpt/cgpt_create.c
parent43e0a9ed6c0b332631442fcf581e7456d62e4532 (diff)
downloadvboot-6ee52d9a929d00e871e7316240b54f381146fbc6.tar.gz
vboot: cgpt: Support writing GPT structs to NOR flash
This CL allows the GPT headers and partition entry arrays to be stored in a NOR flash device. Instead of treating both the NOR and NAND devices as one (in a sandwich way), this CL writes and reads the GPT structs independently of the actual device that houses the partitions. Therefore, the first usable LBA of the partitions will be at 0, and the last usable LBA is at the end of the NAND. +------------------------+ | NOR houses GPT structs | +------------------------+ | 0 | Index into v v +------------------------+ | NAND houses partitions | +------------------------+ Note that the "my_lba", "alternate_lba", "entries_lba" in the GPT headers are no longer meaningful. Consumers of cgptlib will have to set "stored_on_device" to either GPT_STORED_ON_DEVICE or GPT_STORED_OFF_DEVICE, and "gpt_drive_sectors" to the number of 512-byte sectors available to store GPT structs. The NOR read and write operations are done by "flashrom". BUG=chromium:425677 BRANCH=none TEST=unittest TEST=build with DEBUG, cgpt create/add/show on a stumpy-moblab Change-Id: I083b3c94da3b0bb3da1a7b10c6969774080a2afd Reviewed-on: https://chromium-review.googlesource.com/226800 Reviewed-by: Nam Nguyen <namnguyen@chromium.org> Commit-Queue: Nam Nguyen <namnguyen@chromium.org> Tested-by: Nam Nguyen <namnguyen@chromium.org>
Diffstat (limited to 'cgpt/cgpt_create.c')
-rw-r--r--cgpt/cgpt_create.c35
1 files changed, 29 insertions, 6 deletions
diff --git a/cgpt/cgpt_create.c b/cgpt/cgpt_create.c
index 7d3c0595..6a21a8b0 100644
--- a/cgpt/cgpt_create.c
+++ b/cgpt/cgpt_create.c
@@ -44,17 +44,40 @@ static int GptCreate(struct drive *drive, CgptCreateParams *params) {
h->revision = GPT_HEADER_REVISION;
h->size = sizeof(GptHeader);
h->my_lba = GPT_PMBR_SECTORS; /* The second sector on drive. */
- h->alternate_lba = drive->gpt.drive_sectors - GPT_HEADER_SECTORS;
- h->entries_lba = h->my_lba + GPT_HEADER_SECTORS + params->padding;
- h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
- h->last_usable_lba = (drive->gpt.drive_sectors - GPT_HEADER_SECTORS -
- GPT_ENTRIES_SECTORS - 1);
+ h->alternate_lba = drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS;
+ h->entries_lba = h->my_lba + GPT_HEADER_SECTORS;
+ if (drive->gpt.stored_on_device == GPT_STORED_ON_DEVICE) {
+ h->entries_lba += params->padding;
+ h->first_usable_lba = h->entries_lba + GPT_ENTRIES_SECTORS;
+ h->last_usable_lba = (drive->gpt.drive_sectors - GPT_HEADER_SECTORS -
+ GPT_ENTRIES_SECTORS - 1);
+ } else {
+ h->first_usable_lba = 0;
+ h->last_usable_lba = (drive->gpt.drive_sectors - 1);
+ }
if (CGPT_OK != GenerateGuid(&h->disk_uuid)) {
Error("Unable to generate new GUID.\n");
return -1;
}
- h->number_of_entries = 128;
h->size_of_entry = sizeof(GptEntry);
+ h->number_of_entries = TOTAL_ENTRIES_SIZE / h->size_of_entry;
+ if (drive->gpt.stored_on_device != GPT_STORED_ON_DEVICE) {
+ // We might have smaller space for the GPT table. Scale accordingly.
+ size_t half_size = drive->flash_size / 2;
+ size_t header_block_size = GPT_HEADER_SECTORS * drive->gpt.sector_bytes;
+ if (half_size < header_block_size) {
+ Error("Not enough space for a GPT header.\n");
+ return -1;
+ }
+ half_size -= header_block_size;
+ if (half_size < MIN_NUMBER_OF_ENTRIES * h->size_of_entry) {
+ Error("Not enough space for minimum number of entries.\n");
+ return -1;
+ }
+ if (128 > half_size / h->size_of_entry) {
+ h->number_of_entries = half_size / h->size_of_entry;
+ }
+ }
// Copy to secondary
RepairHeader(&drive->gpt, MASK_PRIMARY);