summaryrefslogtreecommitdiff
path: root/firmware/lib
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/lib')
-rw-r--r--firmware/lib/cgptlib/cgptlib.c32
-rw-r--r--firmware/lib/cgptlib/cgptlib_internal.c27
-rw-r--r--firmware/lib/cgptlib/include/cgptlib.h2
-rw-r--r--firmware/lib/cgptlib/include/cgptlib_internal.h6
-rw-r--r--firmware/lib/cgptlib/include/mtdlib.h148
-rw-r--r--firmware/lib/cgptlib/mtdlib.c337
6 files changed, 528 insertions, 24 deletions
diff --git a/firmware/lib/cgptlib/cgptlib.c b/firmware/lib/cgptlib/cgptlib.c
index 6fc29f62..53b69924 100644
--- a/firmware/lib/cgptlib/cgptlib.c
+++ b/firmware/lib/cgptlib/cgptlib.c
@@ -114,10 +114,9 @@ int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size)
int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type)
{
- GptHeader *header = (GptHeader *)gpt->primary_header;
GptEntry *entries = (GptEntry *)gpt->primary_entries;
GptEntry *e = entries + gpt->current_kernel;
- uint16_t previous_attr = e->attrs.fields.gpt_att;
+ int modified = 0;
if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
return GPT_ERROR_INVALID_UPDATE_TYPE;
@@ -138,6 +137,7 @@ int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type)
tries = GetEntryTries(e);
if (tries > 1) {
/* Still have tries left */
+ modified = 1;
SetEntryTries(e, tries - 1);
break;
}
@@ -150,9 +150,9 @@ int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type)
* Only clear tries and priority if the successful bit
* is not set.
*/
- e->attrs.fields.gpt_att = previous_attr &
- ~(CGPT_ATTRIBUTE_TRIES_MASK |
- CGPT_ATTRIBUTE_PRIORITY_MASK);
+ modified = 1;
+ SetEntryTries(e, 0);
+ SetEntryPriority(e, 0);
}
break;
}
@@ -160,25 +160,9 @@ int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type)
return GPT_ERROR_INVALID_UPDATE_TYPE;
}
- /* If no change to attributes, we're done */
- if (e->attrs.fields.gpt_att == previous_attr)
- return GPT_SUCCESS;
-
- /* Update the CRCs */
- header->entries_crc32 = Crc32((const uint8_t *)entries,
- header->size_of_entry *
- header->number_of_entries);
- header->header_crc32 = HeaderCrc(header);
- gpt->modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
-
- /*
- * Use the repair function to update the other copy of the GPT. This
- * is a tad inefficient, but is much faster than the disk I/O to update
- * the GPT on disk so it doesn't matter.
- */
- gpt->valid_headers = MASK_PRIMARY;
- gpt->valid_entries = MASK_PRIMARY;
- GptRepair(gpt);
+ if (modified) {
+ GptModified(gpt);
+ }
return GPT_SUCCESS;
}
diff --git a/firmware/lib/cgptlib/cgptlib_internal.c b/firmware/lib/cgptlib/cgptlib_internal.c
index c87f981c..2224ac14 100644
--- a/firmware/lib/cgptlib/cgptlib_internal.c
+++ b/firmware/lib/cgptlib/cgptlib_internal.c
@@ -375,6 +375,27 @@ void GetCurrentKernelUniqueGuid(GptData *gpt, void *dest)
Memcpy(dest, &e->unique, sizeof(Guid));
}
+void GptModified(GptData *gpt) {
+ GptHeader *header = (GptHeader *)gpt->primary_header;
+
+ /* Update the CRCs */
+ header->entries_crc32 = Crc32(gpt->primary_entries,
+ header->size_of_entry *
+ header->number_of_entries);
+ header->header_crc32 = HeaderCrc(header);
+ gpt->modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
+
+ /*
+ * Use the repair function to update the other copy of the GPT. This
+ * is a tad inefficient, but is much faster than the disk I/O to update
+ * the GPT on disk so it doesn't matter.
+ */
+ gpt->valid_headers = MASK_PRIMARY;
+ gpt->valid_entries = MASK_PRIMARY;
+ GptRepair(gpt);
+}
+
+
const char *GptErrorText(int error_code)
{
switch(error_code) {
@@ -414,6 +435,12 @@ const char *GptErrorText(int error_code)
case GPT_ERROR_DUP_GUID:
return "Duplicated GUID";
+ case GPT_ERROR_INVALID_FLASH_GEOMETRY:
+ return "Invalid flash geometry";
+
+ case GPT_ERROR_NO_SUCH_ENTRY:
+ return "No entry found";
+
default:
break;
};
diff --git a/firmware/lib/cgptlib/include/cgptlib.h b/firmware/lib/cgptlib/include/cgptlib.h
index ccaa2beb..ef329783 100644
--- a/firmware/lib/cgptlib/include/cgptlib.h
+++ b/firmware/lib/cgptlib/include/cgptlib.h
@@ -21,6 +21,8 @@ enum {
GPT_ERROR_START_LBA_OVERLAP,
GPT_ERROR_END_LBA_OVERLAP,
GPT_ERROR_DUP_GUID,
+ GPT_ERROR_INVALID_FLASH_GEOMETRY,
+ GPT_ERROR_NO_SUCH_ENTRY,
/* Number of errors */
GPT_ERROR_COUNT
};
diff --git a/firmware/lib/cgptlib/include/cgptlib_internal.h b/firmware/lib/cgptlib/include/cgptlib_internal.h
index c7606287..b2691419 100644
--- a/firmware/lib/cgptlib/include/cgptlib_internal.h
+++ b/firmware/lib/cgptlib/include/cgptlib_internal.h
@@ -131,6 +131,12 @@ int GptSanityCheck(GptData *gpt);
*/
void GptRepair(GptData *gpt);
+/**
+ * Called when the primary entries are modified and the CRCs need to be
+ * recalculated and propagated to the secondary entries
+ */
+void GptModified(GptData *gpt);
+
/* Getters and setters for partition attribute fields. */
int GetEntrySuccessful(const GptEntry *e);
diff --git a/firmware/lib/cgptlib/include/mtdlib.h b/firmware/lib/cgptlib/include/mtdlib.h
new file mode 100644
index 00000000..135a530b
--- /dev/null
+++ b/firmware/lib/cgptlib/include/mtdlib.h
@@ -0,0 +1,148 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef VBOOT_REFERENCE_MTDLIB_H_
+#define VBOOT_REFERENCE_MTDLIB_H_
+
+#include "cgptlib.h"
+#include "sysincludes.h"
+
+
+#define MTD_DRIVE_SIGNATURE "CrOSPart" /* This must be exactly 8 chars */
+
+/*
+ * Bit definitions and masks for MTD attributes.
+ *
+ * 13-16 -- partition number
+ * 9-12 -- partition type
+ * 8 -- success
+ * 7-4 -- tries
+ * 3-0 -- priority
+ */
+#define MTD_ATTRIBUTE_PRIORITY_OFFSET (0)
+#define MTD_ATTRIBUTE_MAX_PRIORITY (15UL)
+#define MTD_ATTRIBUTE_PRIORITY_MASK (MTD_ATTRIBUTE_MAX_PRIORITY << \
+ MTD_ATTRIBUTE_PRIORITY_OFFSET)
+
+#define MTD_ATTRIBUTE_TRIES_OFFSET (4)
+#define MTD_ATTRIBUTE_MAX_TRIES (15UL)
+#define MTD_ATTRIBUTE_TRIES_MASK (MTD_ATTRIBUTE_MAX_TRIES << \
+ MTD_ATTRIBUTE_TRIES_OFFSET)
+
+#define MTD_ATTRIBUTE_SUCCESSFUL_OFFSET (8)
+#define MTD_ATTRIBUTE_MAX_SUCCESSFUL (1UL)
+#define MTD_ATTRIBUTE_SUCCESSFUL_MASK (MTD_ATTRIBUTE_MAX_SUCCESSFUL << \
+ MTD_ATTRIBUTE_SUCCESSFUL_OFFSET)
+
+#define MTD_ATTRIBUTE_TYPE_OFFSET (9)
+#define MTD_ATTRIBUTE_MAX_TYPE (15UL)
+#define MTD_ATTRIBUTE_TYPE_MASK (MTD_ATTRIBUTE_MAX_TYPE << \
+ MTD_ATTRIBUTE_TYPE_OFFSET)
+
+#define MTD_ATTRIBUTE_NUMBER_OFFSET (13)
+#define MTD_ATTRIBUTE_MAX_NUMBER (15UL)
+#define MTD_ATTRIBUTE_NUMBER_MASK (MTD_ATTRIBUTE_MAX_NUMBER << \
+ MTD_ATTRIBUTE_NUMBER_OFFSET)
+
+
+#define MTD_PARTITION_TYPE_UNUSED 0
+#define MTD_PARTITION_TYPE_CHROMEOS_KERNEL 1
+#define MTD_PARTITION_TYPE_CHROMEOS_FIRMWARE 2
+#define MTD_PARTITION_TYPE_CHROMEOS_ROOTFS 3
+#define MTD_PARTITION_TYPE_CHROMEOS_RESERVED 4
+#define MTD_PARTITION_TYPE_CHROMEOS_FLAGSTORE 5
+#define MTD_PARTITION_TYPE_LINUX_DATA 6
+#define MTD_PARTITION_TYPE_EFI 7
+
+/* This is mostly arbitrary at the moment, but gives a little room to expand. */
+#define MTD_MAX_PARTITIONS 16
+
+
+
+typedef struct {
+ uint32_t starting_lba;
+ uint32_t ending_lba;
+ uint32_t flags;
+ uint32_t reserved;
+} __attribute__((packed)) MtdDiskPartition;
+
+typedef struct {
+ unsigned char signature[8];
+ /* For compatibility, this is only ever the CRC of the first
+ * MTD_DRIVE_V1_SIZE bytes. Further extensions must include their own CRCs,
+ * so older FW can boot newer layouts if we expand in the future.
+ */
+ uint32_t crc32;
+ uint32_t size;
+ uint32_t first_lba;
+ uint32_t last_lba;
+ MtdDiskPartition partitions[MTD_MAX_PARTITIONS];
+} __attribute__((packed)) MtdDiskLayout;
+
+#define MTD_DRIVE_V1_SIZE (24 + 16*16)
+
+#define MTDENTRY_EXPECTED_SIZE (16)
+#define MTDLAYOUT_EXPECTED_SIZE (24 + 16 * MTDENTRY_EXPECTED_SIZE)
+
+
+typedef struct {
+ /* Specifies the flash geometry, in erase blocks & write pages */
+ uint32_t flash_block_bytes;
+ uint32_t flash_page_bytes;
+
+ /* Location, in blocks, of FTS partition */
+ uint32_t fts_block_offset;
+ /* Size, in blocks, of FTS partition */
+ uint32_t fts_block_size;
+
+ /* Size of a LBA sector, in bytes */
+ uint32_t sector_bytes;
+ /* Size of drive in LBA sectors, in sectors */
+ uint64_t drive_sectors;
+
+ /*
+ * The current chromeos kernel index in partition table. -1 means not
+ * found on drive.
+ */
+ int current_kernel;
+ int current_priority;
+
+ /* If set, the flags partition has been modified and needs to be flushed */
+ int modified;
+
+ /* Internal variables */
+ MtdDiskLayout primary;
+} MtdData;
+
+
+/* APIs are documented in cgptlib.h & cgptlib_internal.h */
+int MtdInit(MtdData *mtd);
+int MtdCheckParameters(MtdData *mtd);
+
+int MtdNextKernelEntry(MtdData *gpt, uint64_t *start_sector, uint64_t *size);
+int MtdUpdateKernelEntry(MtdData *gpt, uint32_t update_type);
+
+int MtdGetEntryPriority(const MtdDiskPartition *e);
+int MtdGetEntryTries(const MtdDiskPartition *e);
+int MtdGetEntrySuccessful(const MtdDiskPartition *e);
+int MtdGetEntryType(const MtdDiskPartition *e);
+int MtdIsKernelEntry(const MtdDiskPartition *e);
+void MtdSetEntrySuccessful(MtdDiskPartition *e, int successful) ;
+void MtdSetEntryPriority(MtdDiskPartition *e, int priority);
+void MtdSetEntryTries(MtdDiskPartition *e, int tries);
+void MtdSetEntryType(MtdDiskPartition *e, int type);
+
+void MtdModified(MtdData *mtd);
+int MtdGptInit(MtdData *mtd);
+int MtdIsPartitionValid(const MtdDiskPartition *part);
+int MtdCheckEntries(MtdDiskPartition *entries, MtdDiskLayout *h);
+int MtdSanityCheck(MtdData *disk);
+void MtdRepair(MtdData *gpt);
+void MtdGetCurrentKernelUniqueGuid(MtdData *gpt, void *dest);
+uint32_t MtdHeaderCrc(MtdDiskLayout *h);
+
+
+#endif
+
diff --git a/firmware/lib/cgptlib/mtdlib.c b/firmware/lib/cgptlib/mtdlib.c
new file mode 100644
index 00000000..db9f4b5d
--- /dev/null
+++ b/firmware/lib/cgptlib/mtdlib.c
@@ -0,0 +1,337 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "mtdlib.h"
+
+#include "cgptlib.h"
+#include "cgptlib_internal.h"
+#include "crc32.h"
+#include "utility.h"
+#include "vboot_api.h"
+
+int MtdInit(MtdData *mtd) {
+ int ret;
+
+ mtd->modified = 0;
+ mtd->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
+ mtd->current_priority = 999;
+
+ ret = MtdSanityCheck(mtd);
+ if (GPT_SUCCESS != ret) {
+ VBDEBUG(("MtdInit() failed sanity check\n"));
+ return ret;
+ }
+
+ return GPT_SUCCESS;
+}
+
+int MtdCheckParameters(MtdData *disk) {
+ if (disk->sector_bytes != 512) {
+ return GPT_ERROR_INVALID_SECTOR_SIZE;
+ }
+
+ /* At minimum, the disk must consist of at least one erase block */
+ if (disk->drive_sectors < disk->flash_block_bytes / disk->sector_bytes) {
+ return GPT_ERROR_INVALID_SECTOR_NUMBER;
+ }
+
+ /* Write pages must be an integer multiple of sector size */
+ if (disk->flash_page_bytes == 0 ||
+ disk->flash_page_bytes % disk->sector_bytes != 0) {
+ return GPT_ERROR_INVALID_FLASH_GEOMETRY;
+ }
+
+ /* Erase blocks must be an integer multiple of write pages */
+ if (disk->flash_block_bytes == 0 ||
+ disk->flash_block_bytes % disk->flash_page_bytes != 0) {
+ return GPT_ERROR_INVALID_FLASH_GEOMETRY;
+ }
+
+ /* Without a FTS region, why are you using MTD? */
+ if (disk->fts_block_size == 0) {
+ return GPT_ERROR_INVALID_FLASH_GEOMETRY;
+ }
+ return GPT_SUCCESS;
+}
+
+int MtdGetEntryPriority(const MtdDiskPartition *e) {
+ return ((e->flags & MTD_ATTRIBUTE_PRIORITY_MASK) >>
+ MTD_ATTRIBUTE_PRIORITY_OFFSET);
+}
+
+int MtdGetEntryTries(const MtdDiskPartition *e) {
+ return ((e->flags & MTD_ATTRIBUTE_TRIES_MASK) >>
+ MTD_ATTRIBUTE_TRIES_OFFSET);
+}
+
+int MtdGetEntrySuccessful(const MtdDiskPartition *e) {
+ return ((e->flags & MTD_ATTRIBUTE_SUCCESSFUL_MASK) >>
+ MTD_ATTRIBUTE_SUCCESSFUL_OFFSET);
+}
+
+int MtdGetEntryType(const MtdDiskPartition *e) {
+ return ((e->flags & MTD_ATTRIBUTE_TYPE_MASK) >> MTD_ATTRIBUTE_TYPE_OFFSET);
+}
+
+int MtdIsKernelEntry(const MtdDiskPartition *e) {
+ return MtdGetEntryType(e) == MTD_PARTITION_TYPE_CHROMEOS_KERNEL;
+}
+
+
+static void SetBitfield(MtdDiskPartition *e,
+ uint32_t offset, uint32_t mask, uint32_t v) {
+ 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);
+}
+void MtdSetEntryPriority(MtdDiskPartition *e, int priority) {
+ SetBitfield(e, MTD_ATTRIBUTE_PRIORITY_OFFSET, MTD_ATTRIBUTE_PRIORITY_MASK,
+ priority);
+}
+void MtdSetEntryTries(MtdDiskPartition *e, int tries) {
+ SetBitfield(e, MTD_ATTRIBUTE_TRIES_OFFSET, MTD_ATTRIBUTE_TRIES_MASK, tries);
+}
+void MtdSetEntryType(MtdDiskPartition *e, int type) {
+ SetBitfield(e, MTD_ATTRIBUTE_TYPE_OFFSET, MTD_ATTRIBUTE_TYPE_MASK, type);
+}
+
+void MtdModified(MtdData *mtd) {
+ mtd->modified = 1;
+ mtd->primary.crc32 = MtdHeaderCrc(&mtd->primary);
+}
+
+int MtdIsPartitionValid(const MtdDiskPartition *part) {
+ return MtdGetEntryType(part) != 0;
+}
+
+int MtdCheckEntries(MtdDiskPartition *entries, MtdDiskLayout *h) {
+ uint32_t i, j;
+
+ for (i = 0; i < MTD_MAX_PARTITIONS; i++) {
+ for (j = 0; j < MTD_MAX_PARTITIONS; j++) {
+ if (i != j) {
+ MtdDiskPartition *entry = entries + i;
+ MtdDiskPartition *e2 = entries + j;
+
+ if (!MtdIsPartitionValid(entry) || !MtdIsPartitionValid(e2))
+ continue;
+
+ if((entry->starting_lba == 0 && entry->ending_lba == 0) ||
+ (e2->starting_lba == 0 && e2->ending_lba == 0)) {
+ continue;
+ }
+
+ if (entry->ending_lba > h->last_lba) {
+ return GPT_ERROR_OUT_OF_REGION;
+ }
+ if (entry->starting_lba < h->first_lba) {
+ return GPT_ERROR_OUT_OF_REGION;
+ }
+ if (entry->starting_lba > entry->ending_lba) {
+ return GPT_ERROR_OUT_OF_REGION;
+ }
+
+ if ((entry->starting_lba >= e2->starting_lba) &&
+ (entry->starting_lba <= e2->ending_lba)) {
+ return GPT_ERROR_START_LBA_OVERLAP;
+ }
+ if ((entry->ending_lba >= e2->starting_lba) &&
+ (entry->ending_lba <= e2->ending_lba)) {
+ return GPT_ERROR_END_LBA_OVERLAP;
+ }
+ }
+ }
+ }
+ return GPT_SUCCESS;
+}
+
+int MtdSanityCheck(MtdData *disk) {
+ MtdDiskLayout *h = &disk->primary;
+ int ret;
+
+ ret = MtdCheckParameters(disk);
+ if(GPT_SUCCESS != ret)
+ return ret;
+
+ if (Memcmp(disk->primary.signature, MTD_DRIVE_SIGNATURE,
+ sizeof(disk->primary.signature))) {
+ return GPT_ERROR_INVALID_HEADERS;
+ }
+
+ if (disk->primary.first_lba > disk->primary.last_lba ||
+ disk->primary.last_lba > disk->drive_sectors) {
+ return GPT_ERROR_INVALID_SECTOR_NUMBER;
+ }
+
+ if (h->crc32 != MtdHeaderCrc(h)) {
+ return GPT_ERROR_CRC_CORRUPTED;
+ }
+ if (h->size < MTD_DRIVE_V1_SIZE) {
+ return GPT_ERROR_INVALID_HEADERS;
+ }
+ return MtdCheckEntries(h->partitions, h);
+}
+
+void MtdRepair(MtdData *gpt) {
+
+}
+
+void MtdGetCurrentKernelUniqueGuid(MtdData *gpt, void *dest) {
+ Memset(dest, 0, 16);
+}
+
+uint32_t MtdHeaderCrc(MtdDiskLayout *h) {
+ uint32_t crc32, original_crc32;
+
+ /* Original CRC is calculated with the CRC field 0. */
+ original_crc32 = h->crc32;
+ h->crc32 = 0;
+ crc32 = Crc32((const uint8_t *)h, h->size);
+ h->crc32 = original_crc32;
+
+ return crc32;
+}
+
+
+
+int MtdNextKernelEntry(MtdData *mtd, uint64_t *start_sector, uint64_t *size)
+{
+ MtdDiskLayout *header = &mtd->primary;
+ MtdDiskPartition *entries = header->partitions;
+ MtdDiskPartition *e;
+ int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
+ int new_prio = 0;
+ uint32_t i;
+
+ /*
+ * If we already found a kernel, continue the scan at the current
+ * kernel's priority, in case there is another kernel with the same
+ * priority.
+ */
+ if (mtd->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) {
+ for (i = mtd->current_kernel + 1;
+ i < MTD_MAX_PARTITIONS; i++) {
+ e = entries + i;
+ if (!MtdIsKernelEntry(e))
+ continue;
+ VBDEBUG(("GptNextKernelEntry looking at same prio "
+ "partition %d\n", i+1));
+ VBDEBUG(("GptNextKernelEntry 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) {
+ mtd->current_kernel = i;
+ *start_sector = e->starting_lba;
+ *size = e->ending_lba - e->starting_lba + 1;
+ VBDEBUG(("GptNextKernelEntry likes it\n"));
+ return GPT_SUCCESS;
+ }
+ }
+ }
+
+ /*
+ * We're still here, so scan for the remaining kernel with the highest
+ * priority less than the previous attempt.
+ */
+ for (i = 0, e = entries; i < MTD_MAX_PARTITIONS; i++, e++) {
+ int current_prio = MtdGetEntryPriority(e);
+ if (!MtdIsKernelEntry(e))
+ continue;
+ VBDEBUG(("GptNextKernelEntry looking at new prio "
+ "partition %d\n", i+1));
+ VBDEBUG(("GptNextKernelEntry s%d t%d p%d\n",
+ MtdGetEntrySuccessful(e), MtdGetEntryTries(e),
+ MtdGetEntryPriority(e)));
+ if (!(MtdGetEntrySuccessful(e) || MtdGetEntryTries(e)))
+ continue;
+ if (current_prio >= mtd->current_priority) {
+ /* Already returned this kernel in a previous call */
+ continue;
+ }
+ if (current_prio > new_prio) {
+ new_kernel = i;
+ new_prio = current_prio;
+ }
+ }
+
+ /*
+ * Save what we found. Note that if we didn't find a new kernel,
+ * new_prio will still be -1, so future calls to this function will
+ * also fail.
+ */
+ mtd->current_kernel = new_kernel;
+ mtd->current_priority = new_prio;
+
+ if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) {
+ VBDEBUG(("GptNextKernelEntry no more kernels\n"));
+ return GPT_ERROR_NO_VALID_KERNEL;
+ }
+
+ VBDEBUG(("GptNextKernelEntry likes partition %d\n", new_kernel + 1));
+ e = entries + new_kernel;
+ *start_sector = e->starting_lba;
+ *size = e->ending_lba - e->starting_lba + 1;
+ return GPT_SUCCESS;
+}
+
+int MtdUpdateKernelEntry(MtdData *mtd, uint32_t update_type)
+{
+ MtdDiskLayout *header = &mtd->primary;
+ MtdDiskPartition *entries = header->partitions;
+ MtdDiskPartition *e = entries + mtd->current_kernel;
+ int modified = 0;
+
+ if (mtd->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
+ return GPT_ERROR_INVALID_UPDATE_TYPE;
+ if (!MtdIsKernelEntry(e))
+ return GPT_ERROR_INVALID_UPDATE_TYPE;
+
+ switch (update_type) {
+ case GPT_UPDATE_ENTRY_TRY: {
+ /* Used up a try */
+ int tries;
+ if (MtdGetEntrySuccessful(e)) {
+ /*
+ * Successfully booted this partition, so tries field
+ * is ignored.
+ */
+ return GPT_SUCCESS;
+ }
+ tries = MtdGetEntryTries(e);
+ if (tries > 1) {
+ /* Still have tries left */
+ modified = 1;
+ MtdSetEntryTries(e, tries - 1);
+ break;
+ }
+ /* Out of tries, so drop through and mark partition bad. */
+ }
+ case GPT_UPDATE_ENTRY_BAD: {
+ /* Giving up on this partition entirely. */
+ if (!MtdGetEntrySuccessful(e)) {
+ /*
+ * Only clear tries and priority if the successful bit
+ * is not set.
+ */
+ modified = 1;
+ MtdSetEntryTries(e, 0);
+ MtdSetEntryPriority(e, 0);
+ }
+ break;
+ }
+ default:
+ return GPT_ERROR_INVALID_UPDATE_TYPE;
+ }
+
+ if (modified) {
+ MtdModified(mtd);
+ }
+
+ return GPT_SUCCESS;
+}