Commit 1b2fc096 authored by Dominik Ermel's avatar Dominik Ermel Committed by Jamie
Browse files

boot: Reuse pointers for flash_area objects from state



Reduce flash_area_open/flash_area_close calls by reusing
pointers to flash_area objects stored in boot_loader_state,
that should be populated by context_boot_go.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent d00b11dc
Loading
Loading
Loading
Loading
+16 −18
Original line number Diff line number Diff line
@@ -205,16 +205,17 @@ boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
 * If the magic is successfully found, a flash_area * is returned and it
 * is the responsibility of the called to close it.
 *
 * @returns 0 on success, -1 on errors
 * @returns flash_area pointer on success, NULL on failure.
 */
int
boot_find_status(int image_index, const struct flash_area **fap)
const struct flash_area *
boot_find_status(const struct boot_loader_state *state, int image_index)
{
    uint8_t areas[] = {
    const struct flash_area *fa_p = NULL;
    const struct flash_area *areas[] = {
#if MCUBOOT_SWAP_USING_SCRATCH
        FLASH_AREA_IMAGE_SCRATCH,
        state->scratch.area,
#endif
        FLASH_AREA_IMAGE_PRIMARY(image_index),
        state->imgs[image_index][BOOT_PRIMARY_SLOT].area,
    };
    unsigned int i;

@@ -225,29 +226,26 @@ boot_find_status(int image_index, const struct flash_area **fap)
     * is assumed that if magic is valid then other metadata is too,
     * because magic is always written in the last step.
     */

    for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
        uint8_t magic[BOOT_MAGIC_SZ];
        int rc = 0;

        if (flash_area_open(areas[i], fap)) {
            break;
        }
        fa_p = areas[i];
        rc = flash_area_read(fa_p, boot_magic_off(fa_p), magic, BOOT_MAGIC_SZ);

        if (flash_area_read(*fap, boot_magic_off(*fap), magic, BOOT_MAGIC_SZ)) {
            flash_area_close(*fap);
        if (rc != 0) {
            BOOT_LOG_ERR("Failed to read status from %d, err %d\n",
                         flash_area_get_id(fa_p), rc);
            fa_p = NULL;
            break;
        }

        if (BOOT_MAGIC_GOOD == boot_magic_decode(magic)) {
            return 0;
            break;
        }

        flash_area_close(*fap);
    }

    /* If we got here, no magic was found */
    fap = NULL;
    return -1;
    return fa_p;
}

int
+2 −3
Original line number Diff line number Diff line
@@ -297,7 +297,8 @@ fih_ret bootutil_verify_img(uint8_t *img, uint32_t size,

fih_ret boot_fih_memequal(const void *s1, const void *s2, size_t n);

int boot_find_status(int image_index, const struct flash_area **fap);
const struct flash_area *boot_find_status(const struct boot_loader_state *state,
                                          int image_index);
int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
uint32_t boot_status_sz(uint32_t min_write_sz);
uint32_t boot_trailer_sz(uint32_t min_write_sz);
@@ -305,8 +306,6 @@ int boot_status_entries(int image_index, const struct flash_area *fap);
uint32_t boot_status_off(const struct flash_area *fap);
int boot_read_swap_state(const struct flash_area *fap,
                         struct boot_swap_state *state);
int boot_read_swap_state_by_id(int flash_area_id,
                               struct boot_swap_state *state);
int boot_write_magic(const struct flash_area *fap);
int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
int boot_write_copy_done(const struct flash_area *fap);
+23 −58
Original line number Diff line number Diff line
@@ -548,15 +548,10 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
    struct image_dependency dep;
    uint32_t off;
    uint16_t len;
    int area_id;
    int rc;

    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
    rc = flash_area_open(area_id, &fap);
    if (rc != 0) {
        rc = BOOT_EFLASH;
        goto done;
    }
    fap = BOOT_IMG_AREA(state, slot);
    assert(fap != NULL);

#if defined(MCUBOOT_SWAP_USING_OFFSET)
    it.start_off = boot_get_state_secondary_offset(state, fap);
@@ -603,7 +598,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
    }

done:
    flash_area_close(fap);
    return rc;
}

@@ -948,20 +942,14 @@ boot_data_is_set_to(uint8_t val, void *data, size_t len)
static int
boot_check_header_erased(struct boot_loader_state *state, int slot)
{
    const struct flash_area *fap;
    const struct flash_area *fap = NULL;
    struct image_header *hdr;
    uint8_t erased_val;
    int area_id;
    int rc;

    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
    rc = flash_area_open(area_id, &fap);
    if (rc != 0) {
        return -1;
    }
    fap = BOOT_IMG_AREA(state, slot);
    assert(fap != NULL);

    erased_val = flash_area_erased_val(fap);
    flash_area_close(fap);

    hdr = boot_img_hdr(state, slot);
    if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
@@ -1022,19 +1010,14 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
{
    const struct flash_area *fap;
    struct image_header *hdr;
    int area_id;
    FIH_DECLARE(fih_rc, FIH_FAILURE);
    int rc;

#if !defined(MCUBOOT_SWAP_USING_OFFSET)
    (void)expected_swap_type;
#endif

    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
    rc = flash_area_open(area_id, &fap);
    if (rc != 0) {
        FIH_RET(fih_rc);
    }
    fap = BOOT_IMG_AREA(state, slot);
    assert(fap != NULL);

    hdr = boot_img_hdr(state, slot);
    if (boot_check_header_erased(state, slot) == 0 ||
@@ -1056,22 +1039,15 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
#if defined(MCUBOOT_SWAP_USING_MOVE)
            if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
                boot_swap_type_multi(BOOT_CURR_IMG(state)) == BOOT_SWAP_TYPE_REVERT) {
                const struct flash_area *fap_pri;

                rc = flash_area_open(flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
                                                                         BOOT_PRIMARY_SLOT),
                                                                         &fap_pri);
                const struct flash_area *fap_pri = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);

                if (rc == 0) {
                    rc = swap_erase_trailer_sectors(state, fap_pri);
                    flash_area_close(fap_pri);
                assert(fap_pri != NULL);

                    if (rc == 0) {
                if (swap_erase_trailer_sectors(state, fap_pri) == 0) {
                    BOOT_LOG_INF("Cleared image %d primary slot trailer due to stuck revert",
                                 BOOT_CURR_IMG(state));
                }
            }
            }
#endif
        }
#endif
@@ -1103,6 +1079,8 @@ boot_validate_slot(struct boot_loader_state *state, int slot,

#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
    if (slot != BOOT_PRIMARY_SLOT) {
        int rc;

        /* Check if version of secondary slot is sufficient */
        rc = boot_version_cmp(
                &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
@@ -1151,14 +1129,13 @@ check_validity:
     * overwriting an application written to the incorrect slot.
     * This feature is only supported by ARM platforms.
     */
    if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
    if (fap == BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT)) {
        const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
        struct image_header *secondary_hdr = boot_img_hdr(state, slot);
        uint32_t reset_value = 0;
        uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);

        rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
        if (rc != 0) {
        if (flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)) != 0) {
            fih_rc = FIH_NO_BOOTABLE_IMAGE;
            goto out;
        }
@@ -1181,8 +1158,6 @@ check_validity:
#endif

out:
    flash_area_close(fap);

    FIH_RET(fih_rc);
}

@@ -1208,12 +1183,8 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_
    uint32_t img_security_cnt;
    int rc;

    rc = flash_area_open(flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot),
                         &fap);
    if (rc != 0) {
        rc = BOOT_EFLASH;
        goto done;
    }
    fap = BOOT_IMG_AREA(state, slot);
    assert(fap != NULL);

    rc = bootutil_get_img_security_cnt(state, hdr_slot_idx, fap, &img_security_cnt);
    if (rc != 0) {
@@ -1226,7 +1197,6 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_
    }

done:
    flash_area_close(fap);
    return rc;
}
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
@@ -1732,7 +1702,7 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
         * in the trailer...
         */

        rc = boot_find_status(image_index, &fap);
        fap = boot_find_status(state, image_index);
        assert(fap != NULL);
        rc = boot_read_swap_size(fap, &bs->swap_size);
        assert(rc == 0);
@@ -2764,17 +2734,15 @@ print_loaded_images(struct boot_loader_state *state)
static int
boot_select_or_erase(struct boot_loader_state *state)
{
    const struct flash_area *fap;
    int fa_id;
    const struct flash_area *fap = NULL;
    int rc;
    uint32_t active_slot;
    struct boot_swap_state* active_swap_state;

    active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;

    fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
    rc = flash_area_open(fa_id, &fap);
    assert(rc == 0);
    fap = BOOT_IMG_AREA(state, active_slot);
    assert(fap != NULL);

    active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);

@@ -2795,7 +2763,6 @@ boot_select_or_erase(struct boot_loader_state *state)
        rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
        assert(rc == 0);

        flash_area_close(fap);
        rc = -1;
    } else {
        if (active_swap_state->copy_done != BOOT_FLAG_SET) {
@@ -2817,7 +2784,6 @@ boot_select_or_erase(struct boot_loader_state *state)
                rc = 0;
            }
        }
        flash_area_close(fap);
    }

    return rc;
@@ -3200,8 +3166,7 @@ const struct image_max_size *boot_get_max_app_size(void)
uint32_t boot_get_state_secondary_offset(struct boot_loader_state *state,
                                         const struct flash_area *fap)
{
    if (state != NULL && flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
                                                             BOOT_SECONDARY_SLOT) == fap->fa_id) {
    if (state != NULL && BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT) == fap) {
        return state->secondary_offset[BOOT_CURR_IMG(state)];
    }

+7 −28
Original line number Diff line number Diff line
@@ -135,15 +135,11 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
    uint32_t max_sz = 1024;
    uint16_t idx;
    uint8_t * cur_dst;
    int area_id;
    int rc;
    uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);

    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
    rc = flash_area_open(area_id, &fap_src);
    if (rc != 0){
        return BOOT_EFLASH;
    }
    fap_src = BOOT_IMG_AREA(state, slot);
    assert(fap_src != NULL);

    tlv_off = BOOT_TLV_OFF(hdr);

@@ -188,8 +184,6 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
    rc = 0;

done:
    flash_area_close(fap_src);

    return rc;
}

@@ -211,18 +205,13 @@ boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
{
    int rc;
    const struct flash_area *fap_src = NULL;
    int area_id;

#if (BOOT_IMAGE_NUMBER == 1)
    (void)state;
#endif

    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);

    rc = flash_area_open(area_id, &fap_src);
    if (rc != 0) {
        return BOOT_EFLASH;
    }
    fap_src = BOOT_IMG_AREA(state, slot);
    assert(fap_src != NULL);

    /* Direct copy from flash to its new location in SRAM. */
    rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
@@ -231,8 +220,6 @@ boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
                     BOOT_CURR_IMG(state), rc);
    }

    flash_area_close(fap_src);

    return rc;
}

@@ -421,22 +408,14 @@ boot_remove_image_from_sram(struct boot_loader_state *state)
int
boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
{
    int area_id;
    int rc;
    const struct flash_area *fap;

    (void)state;

    BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
                                                         slot);
    area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
    rc = flash_area_open(area_id, &fap);
    if (rc == 0) {
        flash_area_erase(fap, 0, flash_area_get_size(fap));
        flash_area_close(fap);
    }
    fap = BOOT_IMG_AREA(state, slot);
    assert(fap != NULL);

    return rc;
    return flash_area_erase(fap, 0, flash_area_get_size(fap));
}

int boot_load_image_from_flash_to_sram(struct boot_loader_state *state,
+4 −13
Original line number Diff line number Diff line
@@ -41,22 +41,13 @@ swap_erase_trailer_sectors(const struct boot_loader_state *state,
    uint32_t total_sz;
    uint32_t off;
    uint32_t sz;
    int fa_id_primary;
    int fa_id_secondary;
    uint8_t image_index;
    int rc;

    BOOT_LOG_DBG("erasing trailer; fa_id=%d", flash_area_get_id(fap));

    image_index = BOOT_CURR_IMG(state);
    fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
            BOOT_PRIMARY_SLOT);
    fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
            BOOT_SECONDARY_SLOT);

    if (flash_area_get_id(fap) == fa_id_primary) {
    if (fap == BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)) {
        slot = BOOT_PRIMARY_SLOT;
    } else if (flash_area_get_id(fap) == fa_id_secondary) {
    } else if (fap == BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT)) {
        slot = BOOT_SECONDARY_SLOT;
    } else {
        return BOOT_EFLASH;
@@ -96,7 +87,7 @@ swap_status_init(const struct boot_loader_state *state,

    BOOT_LOG_DBG("initializing status; fa_id=%d", flash_area_get_id(fap));

    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
    rc = boot_read_swap_state(state->imgs[image_index][BOOT_SECONDARY_SLOT].area,
                              &swap_state);
    assert(rc == 0);

Loading