summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
authorAlbert Chaulk <achaulk@chromium.org>2013-06-25 11:30:46 -0700
committerChromeBot <chrome-bot@google.com>2013-07-03 16:02:34 -0700
commit289b604f6154b118f3c47aa35b09e99a4e144814 (patch)
tree2db004eff5ba2fc4d3b80d056df970650b74a92b /firmware
parent7d401c5cd58ec422e239b28c334fc2e94778f565 (diff)
downloadvboot-289b604f6154b118f3c47aa35b09e99a4e144814.tar.gz
Port MTD structures to use 64-bit byte offsets instead of sectors.
As per the discussion on issue 221745 we will be using 64-bit byte offsets for the MTD partition table and converting to/from sectors internally in cgpt. Existing interfaces do not change, eg sizes are still reported in sectors, only the on-disk representation is affected. BRANCH=none BUG=chromium:221745 TEST=unit tests pass Change-Id: Id312d42783acfdabe6eb8aea11dcbd298e00a100 Reviewed-on: https://gerrit.chromium.org/gerrit/60919 Commit-Queue: Albert Chaulk <achaulk@chromium.org> Reviewed-by: Albert Chaulk <achaulk@chromium.org> Tested-by: Albert Chaulk <achaulk@chromium.org>
Diffstat (limited to 'firmware')
-rw-r--r--firmware/lib/cgptlib/include/mtdlib.h21
-rw-r--r--firmware/lib/cgptlib/mtdlib.c79
2 files changed, 68 insertions, 32 deletions
diff --git a/firmware/lib/cgptlib/include/mtdlib.h b/firmware/lib/cgptlib/include/mtdlib.h
index 59fb8b32..0c87822a 100644
--- a/firmware/lib/cgptlib/include/mtdlib.h
+++ b/firmware/lib/cgptlib/include/mtdlib.h
@@ -63,10 +63,9 @@
typedef struct {
- uint32_t starting_lba;
- uint32_t ending_lba;
+ uint64_t starting_offset;
+ uint64_t ending_offset;
uint32_t flags;
- uint32_t reserved;
} __attribute__((packed)) MtdDiskPartition;
typedef struct {
@@ -77,15 +76,15 @@ typedef struct {
*/
uint32_t crc32;
uint32_t size;
- uint32_t first_lba;
- uint32_t last_lba;
+ uint64_t first_offset;
+ uint64_t last_offset;
MtdDiskPartition partitions[MTD_MAX_PARTITIONS];
} __attribute__((packed)) MtdDiskLayout;
-#define MTD_DRIVE_V1_SIZE (24 + 16*16)
+#define MTD_DRIVE_V1_SIZE (32 + 16*20)
-#define MTDENTRY_EXPECTED_SIZE (16)
-#define MTDLAYOUT_EXPECTED_SIZE (24 + 16 * MTDENTRY_EXPECTED_SIZE)
+#define MTDENTRY_EXPECTED_SIZE (20)
+#define MTDLAYOUT_EXPECTED_SIZE (32 + 16 * MTDENTRY_EXPECTED_SIZE)
typedef struct {
@@ -135,6 +134,12 @@ void MtdSetEntryPriority(MtdDiskPartition *e, int priority);
void MtdSetEntryTries(MtdDiskPartition *e, int tries);
void MtdSetEntryType(MtdDiskPartition *e, int type);
+void MtdGetPartitionSize(const MtdDiskPartition *e,
+ uint64_t *start, uint64_t *end, uint64_t *size);
+
+void MtdGetPartitionSizeInSectors(const MtdDiskPartition *e, uint64_t *start,
+ uint64_t *end, uint64_t *size);
+
void MtdModified(MtdData *mtd);
int MtdGptInit(MtdData *mtd);
int MtdIsPartitionValid(const MtdDiskPartition *part);
diff --git a/firmware/lib/cgptlib/mtdlib.c b/firmware/lib/cgptlib/mtdlib.c
index db9f4b5d..18aa7c5a 100644
--- a/firmware/lib/cgptlib/mtdlib.c
+++ b/firmware/lib/cgptlib/mtdlib.c
@@ -11,6 +11,8 @@
#include "utility.h"
#include "vboot_api.h"
+const int kSectorShift = 9; /* 512 bytes / sector. */
+
int MtdInit(MtdData *mtd) {
int ret;
@@ -85,8 +87,8 @@ static void SetBitfield(MtdDiskPartition *e,
e->flags = (e->flags & ~mask) | ((v << offset) & mask);
}
void MtdSetEntrySuccessful(MtdDiskPartition *e, int successful) {
- SetBitfield(e, MTD_ATTRIBUTE_SUCCESSFUL_OFFSET, MTD_ATTRIBUTE_SUCCESSFUL_MASK,
- successful);
+ SetBitfield(e, MTD_ATTRIBUTE_SUCCESSFUL_OFFSET,
+ MTD_ATTRIBUTE_SUCCESSFUL_MASK, successful);
}
void MtdSetEntryPriority(MtdDiskPartition *e, int priority) {
SetBitfield(e, MTD_ATTRIBUTE_PRIORITY_OFFSET, MTD_ATTRIBUTE_PRIORITY_MASK,
@@ -116,31 +118,36 @@ int MtdCheckEntries(MtdDiskPartition *entries, MtdDiskLayout *h) {
if (i != j) {
MtdDiskPartition *entry = entries + i;
MtdDiskPartition *e2 = entries + j;
+ uint64_t start, end;
+ uint64_t other_start, other_end;
if (!MtdIsPartitionValid(entry) || !MtdIsPartitionValid(e2))
continue;
- if((entry->starting_lba == 0 && entry->ending_lba == 0) ||
- (e2->starting_lba == 0 && e2->ending_lba == 0)) {
+ MtdGetPartitionSize(entry, &start, &end, NULL);
+ MtdGetPartitionSize(e2, &other_start, &other_end, NULL);
+
+ if((start == 0 && end == 0) ||
+ (other_start == 0 && other_end == 0)) {
continue;
}
- if (entry->ending_lba > h->last_lba) {
+ if (end > h->last_offset) {
return GPT_ERROR_OUT_OF_REGION;
}
- if (entry->starting_lba < h->first_lba) {
+ if (start < h->first_offset) {
return GPT_ERROR_OUT_OF_REGION;
}
- if (entry->starting_lba > entry->ending_lba) {
+ if (start > end) {
return GPT_ERROR_OUT_OF_REGION;
}
- if ((entry->starting_lba >= e2->starting_lba) &&
- (entry->starting_lba <= e2->ending_lba)) {
+ if ((start >= other_start) &&
+ (start <= other_end)) {
return GPT_ERROR_START_LBA_OVERLAP;
}
- if ((entry->ending_lba >= e2->starting_lba) &&
- (entry->ending_lba <= e2->ending_lba)) {
+ if ((end >= other_start) &&
+ (end <= other_end)) {
return GPT_ERROR_END_LBA_OVERLAP;
}
}
@@ -162,8 +169,8 @@ int MtdSanityCheck(MtdData *disk) {
return GPT_ERROR_INVALID_HEADERS;
}
- if (disk->primary.first_lba > disk->primary.last_lba ||
- disk->primary.last_lba > disk->drive_sectors) {
+ if (disk->primary.first_offset > disk->primary.last_offset ||
+ disk->primary.last_offset > disk->drive_sectors * disk->sector_bytes) {
return GPT_ERROR_INVALID_SECTOR_NUMBER;
}
@@ -196,6 +203,31 @@ uint32_t MtdHeaderCrc(MtdDiskLayout *h) {
return crc32;
}
+void MtdGetPartitionSize(const MtdDiskPartition *e,
+ uint64_t *start, uint64_t *end, uint64_t *size) {
+ uint64_t start_tmp, end_tmp;
+ if (!start)
+ start = &start_tmp;
+ if (!end)
+ end = &end_tmp;
+
+ Memcpy(start, &e->starting_offset, sizeof(e->starting_offset));
+ Memcpy(end, &e->ending_offset, sizeof(e->ending_offset));
+ if (size) {
+ *size = *end - *start + 1;
+ }
+}
+
+void MtdGetPartitionSizeInSectors(const MtdDiskPartition *e, uint64_t *start,
+ uint64_t *end, uint64_t *size) {
+ MtdGetPartitionSize(e, start, end, size);
+ if (start)
+ *start >>= kSectorShift;
+ if (end)
+ *end >>= kSectorShift;
+ if (size)
+ *size >>= kSectorShift;
+}
int MtdNextKernelEntry(MtdData *mtd, uint64_t *start_sector, uint64_t *size)
@@ -218,18 +250,17 @@ int MtdNextKernelEntry(MtdData *mtd, uint64_t *start_sector, uint64_t *size)
e = entries + i;
if (!MtdIsKernelEntry(e))
continue;
- VBDEBUG(("GptNextKernelEntry looking at same prio "
+ VBDEBUG(("MtdNextKernelEntry looking at same prio "
"partition %d\n", i+1));
- VBDEBUG(("GptNextKernelEntry s%d t%d p%d\n",
+ VBDEBUG(("MtdNextKernelEntry s%d t%d p%d\n",
MtdGetEntrySuccessful(e), MtdGetEntryTries(e),
MtdGetEntryPriority(e)));
if (!(MtdGetEntrySuccessful(e) || MtdGetEntryTries(e)))
continue;
if (MtdGetEntryPriority(e) == mtd->current_priority) {
+ MtdGetPartitionSizeInSectors(e, start_sector, NULL, size);
mtd->current_kernel = i;
- *start_sector = e->starting_lba;
- *size = e->ending_lba - e->starting_lba + 1;
- VBDEBUG(("GptNextKernelEntry likes it\n"));
+ VBDEBUG(("MtdNextKernelEntry likes it\n"));
return GPT_SUCCESS;
}
}
@@ -243,9 +274,9 @@ int MtdNextKernelEntry(MtdData *mtd, uint64_t *start_sector, uint64_t *size)
int current_prio = MtdGetEntryPriority(e);
if (!MtdIsKernelEntry(e))
continue;
- VBDEBUG(("GptNextKernelEntry looking at new prio "
+ VBDEBUG(("MtdNextKernelEntry looking at new prio "
"partition %d\n", i+1));
- VBDEBUG(("GptNextKernelEntry s%d t%d p%d\n",
+ VBDEBUG(("MtdNextKernelEntry s%d t%d p%d\n",
MtdGetEntrySuccessful(e), MtdGetEntryTries(e),
MtdGetEntryPriority(e)));
if (!(MtdGetEntrySuccessful(e) || MtdGetEntryTries(e)))
@@ -269,14 +300,14 @@ int MtdNextKernelEntry(MtdData *mtd, uint64_t *start_sector, uint64_t *size)
mtd->current_priority = new_prio;
if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) {
- VBDEBUG(("GptNextKernelEntry no more kernels\n"));
+ VBDEBUG(("MtdNextKernelEntry no more kernels\n"));
return GPT_ERROR_NO_VALID_KERNEL;
}
- VBDEBUG(("GptNextKernelEntry likes partition %d\n", new_kernel + 1));
+ VBDEBUG(("MtdNextKernelEntry likes partition %d\n", new_kernel + 1));
e = entries + new_kernel;
- *start_sector = e->starting_lba;
- *size = e->ending_lba - e->starting_lba + 1;
+ MtdGetPartitionSizeInSectors(e, start_sector, NULL, size);
+
return GPT_SUCCESS;
}