Boot: Add TLV iterator API
Add TLV iterator API to MCUBoot as part of a partial synchronization
with the mainstream MCUBoot repository. The hash of the source commit:
61fd888a7f4d741714553f36839dd49fb0065731.
Change-Id: I817b199f4923433010253c4a201ada250f743aa8
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c
index d5514cd..9b5b935 100644
--- a/bl2/ext/mcuboot/bootutil/src/loader.c
+++ b/bl2/ext/mcuboot/bootutil/src/loader.c
@@ -20,7 +20,7 @@
/*
* Original code taken from mcuboot project at:
* https://github.com/JuulLabs-OSS/mcuboot
- * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
+ * Git SHA of the original version: 61fd888a7f4d741714553f36839dd49fb0065731
* Modifications are Copyright (c) 2018-2019 Arm Limited.
*/
@@ -580,6 +580,7 @@
boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
{
const struct flash_area *fap = NULL;
+ struct image_tlv_info info;
uint32_t off;
int area_id;
int rc;
@@ -595,10 +596,19 @@
goto done;
}
- rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, size);
- if (rc != 0) {
+ off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
+
+ if (flash_area_read(fap, off, &info, sizeof(info))) {
+ rc = BOOT_EFLASH;
goto done;
}
+
+ if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
+ rc = BOOT_EBADIMAGE;
+ goto done;
+ }
+
+ *size = off + info.it_tlv_tot;
rc = 0;
done:
@@ -1779,11 +1789,10 @@
boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
{
const struct flash_area *fap;
- struct image_tlv tlv;
+ struct image_tlv_iter it;
struct image_dependency dep;
uint32_t off;
- uint32_t end;
- bool dep_tlvs_found = false;
+ uint16_t len;
int area_id;
int rc;
@@ -1794,62 +1803,42 @@
goto done;
}
- rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, &end);
+ rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
+ IMAGE_TLV_DEPENDENCY, true);
if (rc != 0) {
goto done;
}
- /* Traverse through all of the TLVs to find the dependency TLVs. */
- while(off < end) {
- rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
- if (rc != 0) {
- rc = BOOT_EFLASH;
- goto done;
- }
-
- if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
- dep_tlvs_found = true;
-
- if (tlv.it_len != sizeof(dep)) {
- rc = BOOT_EBADIMAGE;
- goto done;
- }
-
- rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
- if (rc != 0) {
- rc = BOOT_EFLASH;
- goto done;
- }
-
- if (dep.image_id >= BOOT_IMAGE_NUMBER) {
- rc = BOOT_EBADARGS;
- goto done;
- }
-
- /* Verify dependency and modify the swap type if not satisfied. */
- rc = boot_verify_slot_dependency(state, &dep);
- if (rc != 0) {
- /* Dependency not satisfied. */
- goto done;
- }
-
- /* Dependency satisfied, no action needed.
- * Continue with the next TLV entry.
- */
- } else if (dep_tlvs_found) {
- /* The dependency TLVs are contiguous in the TLV area. If a
- * dependency had already been found and the last read TLV
- * has a different type then there are no more dependency TLVs.
- * The search can be finished.
- */
+ while (true) {
+ rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
+ if (rc < 0) {
+ return -1;
+ } else if (rc > 0) {
+ rc = 0;
break;
}
- /* Avoid integer overflow. */
- if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
- /* Potential overflow. */
- return BOOT_EBADIMAGE;
- } else {
- off += sizeof(tlv) + tlv.it_len;
+
+ if (len != sizeof(dep)) {
+ rc = BOOT_EBADIMAGE;
+ goto done;
+ }
+
+ rc = flash_area_read(fap, off, &dep, len);
+ if (rc != 0) {
+ rc = BOOT_EFLASH;
+ goto done;
+ }
+
+ if (dep.image_id >= BOOT_IMAGE_NUMBER) {
+ rc = BOOT_EBADARGS;
+ goto done;
+ }
+
+ /* Verify dependency and modify the swap type if not satisfied. */
+ rc = boot_verify_slot_dependency(state, &dep);
+ if (rc != 0) {
+ /* Dependency not satisfied. */
+ goto done;
}
}