Commit e575fe9e authored by Andrzej Puzdrowski's avatar Andrzej Puzdrowski
Browse files

image encryption: fix enc_state array indexing for zephyr

enc_state table was indexed with assumption that
image flash area are subsequent and increasing numbers.
It might not be true while building zephyr.

Patch introduce flash_area_id_to_image_slot() implementation for
the zephyr port and uses it to assign proper slot number.
This API is already available in MyNewt.

Signed-off-by: default avatarAndrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
parent ad0e9b80
Loading
Loading
Loading
Loading
+23 −3
Changes for boot/bootutil/src/encrypted.c: 23 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -233,7 +233,11 @@ boot_enc_load(const struct image_header *hdr, const struct flash_area *fap,
    uint8_t enckey_type;
    int rc;

    slot = fap->fa_id - FLASH_AREA_IMAGE_PRIMARY;
    rc = flash_area_id_to_image_slot(fap->fa_id);
    if (rc < 0) {
        return rc;
    }
    slot = rc;

    /* Already loaded... */
    if (enc_state[slot].valid) {
@@ -305,7 +309,16 @@ boot_enc_load(const struct image_header *hdr, const struct flash_area *fap,
int
boot_enc_valid(const struct flash_area *fap)
{
    return enc_state[fap->fa_id - FLASH_AREA_IMAGE_PRIMARY].valid;
    int rc;

    rc = flash_area_id_to_image_slot(fap->fa_id);
    if (rc < 0) {
        /* can't get proper slot number - skip encryption, */
        /* postpone the erro for a upper layer */
        return 0;
    }

    return enc_state[rc].valid;
}

void
@@ -317,6 +330,7 @@ boot_encrypt(const struct flash_area *fap, uint32_t off, uint32_t sz,
    uint8_t u8;
    uint8_t nonce[16];
    uint8_t blk[16];
    int rc;

    memset(nonce, 0, 12);
    off >>= 4;
@@ -325,7 +339,13 @@ boot_encrypt(const struct flash_area *fap, uint32_t off, uint32_t sz,
    nonce[14] = (uint8_t)(off >> 8);
    nonce[15] = (uint8_t)off;

    enc = &enc_state[fap->fa_id - FLASH_AREA_IMAGE_PRIMARY];
    rc = flash_area_id_to_image_slot(fap->fa_id);
    if (rc < 0) {
        assert(0);
        return;
    }

    enc = &enc_state[rc];
    assert(enc->valid == 1);
    for (i = 0; i < sz; i++) {
        if (i == 0 || blk_off == 0) {
+13
Changes for boot/zephyr/flash_map_extended.c: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -66,6 +66,19 @@ int flash_area_id_from_image_slot(int slot)
    return -EINVAL; /* flash_area_open will fail on that */
}

int flash_area_id_to_image_slot(int area_id)
{
    switch (area_id) {
    case FLASH_AREA_IMAGE_PRIMARY:
        return 0;
    case FLASH_AREA_IMAGE_SECONDARY:
        return 1;
    default:
        BOOT_LOG_ERR("invalid flash area ID");
        return -1;
    }
}

int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
{
    int rc;
+8
Changes for boot/zephyr/include/flash_map_backend/flash_map_backend.h: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -52,6 +52,14 @@ int flash_device_base(uint8_t fd_id, uintptr_t *ret);

int flash_area_id_from_image_slot(int slot);

/**
 * Converts the specified flash area ID to an image slot index.
 *
 * Returns image slot index (0 or 1), or -1 if ID doesn't correspond to an image
 * slot.
 */
int flash_area_id_to_image_slot(int area_id);

/* Retrieve the flash sector a given offset belongs to.
 *
 * Returns 0 on success, or an error code on failure.
+13
Changes for sim/mcuboot-sys/csupport/run.c: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -384,6 +384,19 @@ int flash_area_get_sectors(int fa_id, uint32_t *count,
    return 0;
}

int flash_area_id_to_image_slot(int area_id)
{
    switch (area_id) {
    case FLASH_AREA_IMAGE_PRIMARY:
        return 0;
    case FLASH_AREA_IMAGE_SECONDARY:
        return 1;
    default:
        printf("Unsupported image area ID\n");
        abort();
    }
}

void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function)
{
    if (!(x)) {