Commit c962135f authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

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: default avatarFabio Utzig <utzig@apache.org>
parent f13db126
Loading
Loading
Loading
Loading
+1
Changes for boot/bootutil/src/bootutil_priv.h: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -275,6 +275,7 @@ int boot_is_version_sufficient(struct image_version *req,
#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)
+1 −1
Changes for boot/bootutil/src/encrypted.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -242,7 +242,7 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
        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) {
+3 −2
Changes for boot/bootutil/src/image_validate.c: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
#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 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,

    /* 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 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
    }

    /* 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) {
+6 −8
Changes for boot/bootutil/src/loader.c: 6 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -267,8 +267,7 @@ boot_read_image_size(struct boot_loader_state *state, int slot,
        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 @@ boot_read_image_size(struct boot_loader_state *state, int slot,
        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 @@ boot_copy_sector(struct boot_loader_state *state,
                } 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 @@ boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
    }

    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));