Add TLV iterator API
This introduces an API which allows for iteration over an image's TLVs
without resorting to low-level implementation details. All previous TLV
low-level handling was updated to comply with this new interface, and it
also makes it easier for external code to handle TLVs.
The API provides two functions:
1) To start a new iterator:
```
int bootutil_tlv_iter_begin(struct image_tlv_iter *it,
const struct image_header *hdr,
const struct flash_area *fap, uint8_t type,
bool prot);
```
2) To iterate over existing TLVs of given type:
```
int bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off,
uint16_t *len, uint8_t *type);
```
A type of IMAGE_TLV_ANY was added to allow for iteration over all TLVs.
Low-level TLV access functions were removed from API, but low-level
structs are still visible in the namespace.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/encrypted.c b/boot/bootutil/src/encrypted.c
index b4c5727..032b46b 100644
--- a/boot/bootutil/src/encrypted.c
+++ b/boot/bootutil/src/encrypted.c
@@ -223,11 +223,10 @@
size_t olen;
#endif
uint32_t off;
- uint32_t end;
- struct image_tlv tlv;
+ uint16_t len;
+ struct image_tlv_iter it;
uint8_t buf[TLV_ENC_RSA_SZ];
uint8_t slot;
- uint8_t enckey_type;
int rc;
rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id);
@@ -241,34 +240,23 @@
return 1;
}
- rc = boot_find_tlv_offs(hdr, fap, &off, &end);
+ rc = bootutil_tlv_iter_begin(&it, hdr, fap, EXPECTED_ENC_TLV, false);
if (rc) {
return -1;
}
- for (enckey_type = 0; off < end; off += sizeof(tlv) + tlv.it_len) {
- rc = flash_area_read(fap, off, &tlv, sizeof tlv);
- if (rc) {
- return rc;
- }
-
- if (tlv.it_type == EXPECTED_ENC_TLV) {
- if (tlv.it_len != EXPECTED_ENC_LEN) {
- return -1;
- }
- rc = flash_area_read(fap, off + sizeof(tlv), buf, EXPECTED_ENC_LEN);
- if (rc) {
- return -1;
- }
- enckey_type = EXPECTED_ENC_TLV;
- break;
- }
+ rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
+ if (rc != 0) {
+ return rc;
}
- if (enckey_type == 0) {
+ if (len != EXPECTED_ENC_LEN) {
return -1;
- } else if (enckey_type != EXPECTED_ENC_TLV) {
- return 0;
+ }
+
+ rc = flash_area_read(fap, off, buf, EXPECTED_ENC_LEN);
+ if (rc) {
+ return -1;
}
#if defined(MCUBOOT_ENCRYPT_RSA)