summaryrefslogtreecommitdiff
path: root/firmware/lib/cgptlib/cgptlib_internal.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 /firmware/lib/cgptlib/cgptlib_internal.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 'firmware/lib/cgptlib/cgptlib_internal.c')
-rw-r--r--firmware/lib/cgptlib/cgptlib_internal.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/firmware/lib/cgptlib/cgptlib_internal.c b/firmware/lib/cgptlib/cgptlib_internal.c
index 4eea03df..e9f27c96 100644
--- a/firmware/lib/cgptlib/cgptlib_internal.c
+++ b/firmware/lib/cgptlib/cgptlib_internal.c
@@ -20,6 +20,16 @@ int CheckParameters(GptData *gpt)
return GPT_ERROR_INVALID_SECTOR_SIZE;
/*
+ * gpt_drive_sectors should be reasonable. It cannot be unset, and it cannot
+ * differ from drive_sectors if the GPT structs are stored on same device.
+ */
+ if (gpt->gpt_drive_sectors == 0 ||
+ (gpt->stored_on_device == GPT_STORED_ON_DEVICE &&
+ gpt->gpt_drive_sectors != gpt->drive_sectors)) {
+ return GPT_ERROR_INVALID_SECTOR_NUMBER;
+ }
+
+ /*
* Sector count of a drive should be reasonable. If the given value is
* too small to contain basic GPT structure (PMBR + Headers + Entries),
* the value is wrong.
@@ -43,7 +53,8 @@ uint32_t HeaderCrc(GptHeader *h)
return crc32;
}
-int CheckHeader(GptHeader *h, int is_secondary, uint64_t drive_sectors)
+int CheckHeader(GptHeader *h, int is_secondary, uint64_t drive_sectors,
+ uint8_t stored_on_device)
{
if (!h)
return 1;
@@ -80,7 +91,8 @@ int CheckHeader(GptHeader *h, int is_secondary, uint64_t drive_sectors)
return 1;
if ((h->number_of_entries < MIN_NUMBER_OF_ENTRIES) ||
(h->number_of_entries > MAX_NUMBER_OF_ENTRIES) ||
- (h->number_of_entries * h->size_of_entry != TOTAL_ENTRIES_SIZE))
+ (stored_on_device == GPT_STORED_ON_DEVICE &&
+ h->number_of_entries * h->size_of_entry != TOTAL_ENTRIES_SIZE))
return 1;
/*
@@ -100,18 +112,27 @@ int CheckHeader(GptHeader *h, int is_secondary, uint64_t drive_sectors)
return 1;
}
+ /* FirstUsableLBA <= LastUsableLBA. */
+ if (h->first_usable_lba > h->last_usable_lba)
+ return 1;
+
+ if (stored_on_device != GPT_STORED_ON_DEVICE) {
+ if (h->last_usable_lba >= drive_sectors) {
+ return 1;
+ }
+ return 0;
+ }
+
/*
* FirstUsableLBA must be after the end of the primary GPT table array.
* LastUsableLBA must be before the start of the secondary GPT table
- * array. FirstUsableLBA <= LastUsableLBA.
+ * array.
*/
/* TODO(namnguyen): Also check for padding between header & entries. */
if (h->first_usable_lba < 2 + GPT_ENTRIES_SECTORS)
return 1;
if (h->last_usable_lba >= drive_sectors - 1 - GPT_ENTRIES_SECTORS)
return 1;
- if (h->first_usable_lba > h->last_usable_lba)
- return 1;
/* Success */
return 0;
@@ -224,11 +245,11 @@ int GptSanityCheck(GptData *gpt)
return retval;
/* Check both headers; we need at least one valid header. */
- if (0 == CheckHeader(header1, 0, gpt->drive_sectors)) {
+ if (0 == CheckHeader(header1, 0, gpt->drive_sectors, gpt->stored_on_device)) {
gpt->valid_headers |= MASK_PRIMARY;
goodhdr = header1;
}
- if (0 == CheckHeader(header2, 1, gpt->drive_sectors)) {
+ if (0 == CheckHeader(header2, 1, gpt->drive_sectors, gpt->stored_on_device)) {
gpt->valid_headers |= MASK_SECONDARY;
if (!goodhdr)
goodhdr = header2;