Commit 233af7d7 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

Add utility function to find offset/len of TLVs

Add a new function, boot_find_tlv_offs, that loads the beginning and end
offset of the TLV region.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent a87cc7d9
Loading
Loading
Loading
Loading
+3 −0
Changes for boot/bootutil/src/bootutil_priv.h: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -255,6 +255,9 @@ int boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey);
int boot_is_version_sufficient(struct image_version *req,
                               struct image_version *ver);
#endif
int boot_find_tlv_offs(const struct image_header *hdr,
                       const struct flash_area *fap,
                       uint32_t *off, uint32_t *end);

/*
 * Accessors for the contents of struct boot_loader_state.
+5 −14
Changes for boot/bootutil/src/encrypted.c: 5 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -224,7 +224,6 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
#endif
    uint32_t off;
    uint32_t end;
    struct image_tlv_info info;
    struct image_tlv tlv;
    uint8_t buf[TLV_ENC_RSA_SZ];
    uint8_t slot;
@@ -242,20 +241,12 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
        return 1;
    }

    off = BOOT_TLV_OFF(hdr);

    rc = flash_area_read(fap, off, &info, sizeof(info));
    rc = boot_find_tlv_offs(hdr, fap, &off, &end);
    if (rc) {
        return rc;
    }
    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        return -1;
    }
    end = off + info.it_tlv_tot;
    off += sizeof(info);

    enckey_type = 0;
    for (; off < end; off += sizeof(tlv) + tlv.it_len) {
    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;
@@ -267,7 +258,7 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
            }
            rc = flash_area_read(fap, off + sizeof(tlv), buf, EXPECTED_ENC_LEN);
            if (rc) {
                return rc;
                return -1;
            }
            enckey_type = EXPECTED_ENC_TLV;
            break;
@@ -276,9 +267,10 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,

    if (enckey_type == 0) {
        return -1;
    } else if (enckey_type != EXPECTED_ENC_TLV) {
        return 0;
    }

    if (enckey_type == EXPECTED_ENC_TLV) {
#if defined(MCUBOOT_ENCRYPT_RSA)
    mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);

@@ -299,7 +291,6 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
    assert(*bootutil_enc_key.len == 16);
    rc = key_unwrap(buf, enckey);
#endif
    }

    return rc;
}
+1 −10
Changes for boot/bootutil/src/image_validate.c: 1 added line, 10 removed lines.
Original line number Diff line number Diff line
@@ -211,7 +211,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
    uint32_t off;
    uint32_t end;
    int sha256_valid = 0;
    struct image_tlv_info info;
#ifdef EXPECTED_SIG_TLV
    int valid_signature = 0;
    int key_id = -1;
@@ -231,18 +230,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
        memcpy(out_hash, hash, 32);
    }

    /* The TLVs come after the image. */
    off = BOOT_TLV_OFF(hdr);

    rc = flash_area_read(fap, off, &info, sizeof(info));
    rc = boot_find_tlv_offs(hdr, fap, &off, &end);
    if (rc) {
        return rc;
    }
    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        return -1;
    }
    end = off + info.it_tlv_tot;
    off += sizeof(info);

    /*
     * Traverse through all of the TLVs, performing any checks we know
+35 −23
Changes for boot/bootutil/src/loader.c: 35 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -242,6 +242,38 @@ boot_status_source(struct boot_loader_state *state)
    return BOOT_STATUS_SOURCE_NONE;
}

/*
 * Locate the TLVs in an image.
 *
 * @param hdr The image_header struct of the image being checked
 * @param fap flash_area struct of the slot storing the image being checked
 * @param off Address of the first TLV (after TLV info)
 * @param end Address where TLV area ends
 *
 * Returns 0 on success.
 */
int
boot_find_tlv_offs(const struct image_header *hdr, const struct flash_area *fap,
        uint32_t *off, uint32_t *end)
{
    struct image_tlv_info info;
    uint32_t off_;

    off_ = BOOT_TLV_OFF(hdr);

    if (flash_area_read(fap, off_, &info, sizeof(info))) {
        return BOOT_EFLASH;
    }

    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        return BOOT_EBADIMAGE;
    }

    *end = off_ + info.it_tlv_tot;
    *off = off_ + sizeof(info);
    return 0;
}

/*
 * Compute the total size of the given image.  Includes the size of
 * the TLVs.
@@ -251,8 +283,7 @@ static int
boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
{
    const struct flash_area *fap;
    struct image_tlv_info info;
    uint32_t tlv_off;
    uint32_t off;
    int area_id;
    int rc;

@@ -267,17 +298,10 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
        goto done;
    }

    tlv_off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
    rc = flash_area_read(fap, tlv_off, &info, sizeof(info));
    rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, size);
    if (rc != 0) {
        rc = BOOT_EFLASH;
        goto done;
    }
    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        rc = BOOT_EBADIMAGE;
        goto done;
    }
    *size = tlv_off + info.it_tlv_tot;
    rc = 0;

done:
@@ -1821,7 +1845,6 @@ static int
boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
{
    const struct flash_area *fap;
    struct image_tlv_info info;
    struct image_tlv tlv;
    struct image_dependency dep;
    uint32_t off;
@@ -1837,21 +1860,10 @@ boot_verify_all_dependency(struct boot_loader_state *state, uint32_t slot)
        goto done;
    }

    off = BOOT_TLV_OFF(boot_img_hdr(state, slot));

    /* The TLV area always starts with an image_tlv_info structure. */
    rc = flash_area_read(fap, off, &info, sizeof(info));
    rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, &end);
    if (rc != 0) {
        rc = BOOT_EFLASH;
        goto done;
    }

    if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
        rc = BOOT_EBADIMAGE;
        goto done;
    }
    end = off + info.it_tlv_tot;
    off += sizeof(info);

    /* Traverse through all of the TLVs to find the dependency TLVs. */
    for (; off < end; off += sizeof(tlv) + tlv.it_len) {