Add macro to get TLVs offset from header
TLV offset was determined by manually adding the header and image
size in many places; this makes the addition into a macro receiving
a image_header to ease future changes.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h
index 7ae6103..a2e8f75 100644
--- a/boot/bootutil/src/bootutil_priv.h
+++ b/boot/bootutil/src/bootutil_priv.h
@@ -275,6 +275,7 @@
#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
#define BOOT_WRITE_SZ(state) ((state)->write_sz)
#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
+#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
static inline struct image_header*
boot_img_hdr(struct boot_loader_state *state, size_t slot)
diff --git a/boot/bootutil/src/encrypted.c b/boot/bootutil/src/encrypted.c
index d8f7d6b..9edff51 100644
--- a/boot/bootutil/src/encrypted.c
+++ b/boot/bootutil/src/encrypted.c
@@ -242,7 +242,7 @@
return 1;
}
- off = hdr->ih_img_size + hdr->ih_hdr_size;
+ off = BOOT_TLV_OFF(hdr);
rc = flash_area_read(fap, off, &info, sizeof(info));
if (rc) {
diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c
index 01efbcb..a496bc4 100644
--- a/boot/bootutil/src/image_validate.c
+++ b/boot/bootutil/src/image_validate.c
@@ -69,6 +69,7 @@
#if (BOOT_IMAGE_NUMBER == 1) || !defined(MCUBOOT_ENC_IMAGES)
(void)enc_state;
(void)image_index;
+ (void)hdr_size;
#endif
bootutil_sha256_init(&sha256_ctx);
@@ -89,7 +90,7 @@
/* Hash is computed over image header and image itself. */
hdr_size = hdr->ih_hdr_size;
- size = hdr->ih_img_size + hdr_size;
+ size = BOOT_TLV_OFF(hdr);
#if (MCUBOOT_IMAGE_NUMBER > 1)
/* If dependency TLVs are present then the TLV info header and the
@@ -231,7 +232,7 @@
}
/* The TLVs come after the image. */
- off = hdr->ih_img_size + hdr->ih_hdr_size;
+ off = BOOT_TLV_OFF(hdr);
rc = flash_area_read(fap, off, &info, sizeof(info));
if (rc) {
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 80bebd7..fc5d09c 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -267,8 +267,7 @@
goto done;
}
- rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
- &info, sizeof(info));
+ rc = flash_area_read(fap, BOOT_TLV_OFF(hdr), &info, sizeof(info));
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
@@ -277,7 +276,7 @@
rc = BOOT_EBADIMAGE;
goto done;
}
- *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
+ *size = BOOT_TLV_OFF(hdr) + info.it_tlv_tot;
rc = 0;
done:
@@ -1084,12 +1083,12 @@
} else {
blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
}
- if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
+ if (off + bytes_copied + chunk_sz > BOOT_TLV_OFF(hdr)) {
/* do not decrypt TLVs */
- if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
+ if (off + bytes_copied >= BOOT_TLV_OFF(hdr)) {
blk_sz = 0;
} else {
- blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
+ blk_sz = BOOT_TLV_OFF(hdr) - (off + bytes_copied);
}
}
boot_encrypt(state->enc, image_index, fap_src,
@@ -1841,8 +1840,7 @@
}
hdr = boot_img_hdr(state, slot);
- /* The TLVs come after the image. */
- off = hdr->ih_hdr_size + hdr->ih_img_size;
+ off = BOOT_TLV_OFF(hdr);
/* The TLV area always starts with an image_tlv_info structure. */
rc = flash_area_read(fap, off, &info, sizeof(info));