aboutsummaryrefslogtreecommitdiff
path: root/firmware
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
parent43e0a9ed6c0b332631442fcf581e7456d62e4532 (diff)
downloadvboot_reference-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')
-rw-r--r--firmware/include/gpt_misc.h26
-rw-r--r--firmware/lib/cgptlib/cgptlib_internal.c35
-rw-r--r--firmware/lib/cgptlib/include/cgptlib_internal.h3
-rw-r--r--firmware/lib/gpt_misc.c14
-rw-r--r--firmware/lib/vboot_kernel.c3
5 files changed, 66 insertions, 15 deletions
diff --git a/firmware/include/gpt_misc.h b/firmware/include/gpt_misc.h
index 4d91bd8c..53b30347 100644
--- a/firmware/include/gpt_misc.h
+++ b/firmware/include/gpt_misc.h
@@ -57,6 +57,24 @@ enum {
GPT_UPDATE_ENTRY_BAD = 2,
};
+enum {
+ GPT_STORED_ON_DEVICE = 0, /* The GPT is stored on the same device. */
+ GPT_STORED_OFF_DEVICE = 1, /* The GPT is stored on another place. */
+};
+
+/*
+ * A note about stored_on_device and gpt_drive_sectors:
+ *
+ * This code is used by both the "cgpt" utility and depthcharge/vboot. ATM,
+ * depthcharge does not have logic to properly setup stored_on_device and
+ * gpt_drive_sectors, but it does do a memset(gpt, 0, sizeof(GptData)). And so,
+ * GPT_STORED_ON_DEVICE should be 0 to make stored_on_device compatible with
+ * present behavior. At the same time, in vboot_kernel:LoadKernel(), and
+ * cgpt_common:GptLoad(), we need to have simple shims to set gpt_drive_sectors
+ * to drive_sectors.
+ *
+ * TODO(namnguyen): Remove those shims when the firmware can set these fields.
+ */
typedef struct {
/* Fill in the following fields before calling GptInit() */
/* GPT primary header, from sector 1 of disk (size: 512 bytes) */
@@ -69,8 +87,12 @@ typedef struct {
uint8_t *secondary_entries;
/* Size of a LBA sector, in bytes */
uint32_t sector_bytes;
- /* Size of drive in LBA sectors, in sectors */
+ /* Size of drive (that the partitions are on) in LBA sectors */
uint64_t drive_sectors;
+ /* Are the GPT structures stored on the same device */
+ uint8_t stored_on_device;
+ /* Size of the device that holds the GPT structures, 512-byte sectors */
+ uint64_t gpt_drive_sectors;
/* Outputs */
/* Which inputs have been modified? GPT_MODIFIED_* */
@@ -98,6 +120,8 @@ typedef struct {
* secondary_entries
* sector_bytes
* drive_sectors
+ * stored_on_device
+ * gpt_device_sectors
*
* On return the modified field may be set, if the GPT data has been modified
* and should be written to disk.
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;
diff --git a/firmware/lib/cgptlib/include/cgptlib_internal.h b/firmware/lib/cgptlib/include/cgptlib_internal.h
index e9e63cad..4a52420c 100644
--- a/firmware/lib/cgptlib/include/cgptlib_internal.h
+++ b/firmware/lib/cgptlib/include/cgptlib_internal.h
@@ -90,7 +90,8 @@ int CheckParameters(GptData* gpt);
*
* Returns 0 if header is valid, 1 if invalid.
*/
-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);
/**
* Calculate and return the header CRC.
diff --git a/firmware/lib/gpt_misc.c b/firmware/lib/gpt_misc.c
index d00d6b2e..499cc9e7 100644
--- a/firmware/lib/gpt_misc.c
+++ b/firmware/lib/gpt_misc.c
@@ -16,7 +16,7 @@
/**
* Allocate and read GPT data from the drive.
*
- * The sector_bytes and drive_sectors fields should be filled on input. The
+ * The sector_bytes and gpt_drive_sectors fields should be filled on input. The
* primary and secondary header and entries are filled on output.
*
* Returns 0 if successful, 1 if error.
@@ -48,7 +48,8 @@ int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata)
/* Only read primary GPT if the primary header is valid */
GptHeader* primary_header = (GptHeader*)gptdata->primary_header;
- if (0 == CheckHeader(primary_header, 0, gptdata->drive_sectors)) {
+ if (0 == CheckHeader(primary_header, 0, gptdata->gpt_drive_sectors,
+ gptdata->stored_on_device)) {
primary_valid = 1;
if (0 != VbExDiskRead(disk_handle,
primary_header->entries_lba,
@@ -60,13 +61,14 @@ int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata)
}
/* Read secondary header from the end of the drive */
- if (0 != VbExDiskRead(disk_handle, gptdata->drive_sectors - 1, 1,
+ if (0 != VbExDiskRead(disk_handle, gptdata->gpt_drive_sectors - 1, 1,
gptdata->secondary_header))
return 1;
/* Only read secondary GPT if the secondary header is valid */
GptHeader* secondary_header = (GptHeader*)gptdata->secondary_header;
- if (0 == CheckHeader(secondary_header, 1, gptdata->drive_sectors)) {
+ if (0 == CheckHeader(secondary_header, 1, gptdata->gpt_drive_sectors,
+ gptdata->stored_on_device)) {
secondary_valid = 1;
if (0 != VbExDiskRead(disk_handle,
secondary_header->entries_lba,
@@ -138,7 +140,7 @@ int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata)
}
}
- entries_lba = (gptdata->drive_sectors - entries_sectors -
+ entries_lba = (gptdata->gpt_drive_sectors - entries_sectors -
GPT_HEADER_SECTORS);
if (gptdata->secondary_header) {
GptHeader *h = (GptHeader *)(gptdata->secondary_header);
@@ -146,7 +148,7 @@ int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata)
if (gptdata->modified & GPT_MODIFIED_HEADER2) {
VBDEBUG(("Updating GPT entries 2\n"));
if (0 != VbExDiskWrite(disk_handle,
- gptdata->drive_sectors - 1, 1,
+ gptdata->gpt_drive_sectors - 1, 1,
gptdata->secondary_header))
goto fail;
}
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index b2cb8172..62e62967 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -116,6 +116,9 @@ VbError_t LoadKernel(LoadKernelParams *params, VbCommonParams *cparams)
/* Read GPT data */
gpt.sector_bytes = (uint32_t)blba;
gpt.drive_sectors = params->ending_lba + 1;
+ /* TODO: Set stored_on_device and gpt_drive_sectors appropriately */
+ gpt.stored_on_device = GPT_STORED_ON_DEVICE;
+ gpt.gpt_drive_sectors = gpt.drive_sectors;
if (0 != AllocAndReadGptData(params->disk_handle, &gpt)) {
VBDEBUG(("Unable to read GPT data\n"));
shcall->check_result = VBSD_LKC_CHECK_GPT_READ_ERROR;