From 726d3bcb0bdc604dafcce2534f907a933e1074fe Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Mon, 17 Feb 2025 20:08:31 +0100 Subject: [PATCH] boot: Reuse pointers for flash_area objects from state Reduce flash_area_open/flash_area_close calls by re-using pointers to flash_area objects stored in boot_loader_state, that should be populated by context_boot_go. Signed-off-by: Dominik Ermel --- boot/bootutil/src/bootutil_misc.c | 34 ++++++------- boot/bootutil/src/bootutil_priv.h | 5 +- boot/bootutil/src/loader.c | 81 +++++++++---------------------- boot/bootutil/src/ram_load.c | 37 +++----------- boot/bootutil/src/swap_misc.c | 17 ++----- boot/bootutil/src/swap_move.c | 21 +++----- boot/bootutil/src/swap_offset.c | 52 +++++++------------- boot/bootutil/src/swap_scratch.c | 38 ++++++--------- 8 files changed, 93 insertions(+), 192 deletions(-) diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c index 08cf5971d..75604626f 100644 --- a/boot/bootutil/src/bootutil_misc.c +++ b/boot/bootutil/src/bootutil_misc.c @@ -273,16 +273,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; @@ -293,29 +294,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 diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h index 2ae2ae1a0..0dd03e0a2 100644 --- a/boot/bootutil/src/bootutil_priv.h +++ b/boot/bootutil/src/bootutil_priv.h @@ -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); @@ -315,8 +316,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); diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c index 61b71fc60..0e3369d1a 100644 --- a/boot/bootutil/src/loader.c +++ b/boot/bootutil/src/loader.c @@ -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,20 +1039,13 @@ 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_scramble_trailer_sectors(state, fap_pri); - flash_area_close(fap_pri); + assert(fap != NULL); - if (rc == 0) { - BOOT_LOG_INF("Cleared image %d primary slot trailer due to stuck revert", - BOOT_CURR_IMG(state)); - } + if (swap_scramble_trailer_sectors(state, fap_pri) == 0) { + BOOT_LOG_INF("Cleared image %d primary slot trailer due to stuck revert", + BOOT_CURR_IMG(state)); } } #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,11 +1129,12 @@ boot_validate_slot(struct boot_loader_state *state, int slot, * 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); + int rc; rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)); if (rc != 0) { @@ -1181,8 +1160,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot, #endif out: - flash_area_close(fap); - FIH_RET(fih_rc); } @@ -1206,14 +1183,10 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_ { const struct flash_area *fap = NULL; uint32_t img_security_cnt; - int rc; + int rc = 0; - 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 +1199,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 */ @@ -1923,7 +1895,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); @@ -2953,17 +2925,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); @@ -2984,7 +2954,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) { @@ -3006,7 +2975,6 @@ boot_select_or_erase(struct boot_loader_state *state) rc = 0; } } - flash_area_close(fap); } return rc; @@ -3389,8 +3357,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)]; } diff --git a/boot/bootutil/src/ram_load.c b/boot/bootutil/src/ram_load.c index 7942c188b..9c206698e 100644 --- a/boot/bootutil/src/ram_load.c +++ b/boot/bootutil/src/ram_load.c @@ -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; + int rc = 0; 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) { - boot_scramble_slot(fap, slot); - flash_area_close(fap); - } + fap = BOOT_IMG_AREA(state, slot); + assert(fap != NULL); - return rc; + return boot_scramble_slot(fap, slot); } int boot_load_image_from_flash_to_sram(struct boot_loader_state *state, diff --git a/boot/bootutil/src/swap_misc.c b/boot/bootutil/src/swap_misc.c index 4efc0aff2..7fcfcf94a 100644 --- a/boot/bootutil/src/swap_misc.c +++ b/boot/bootutil/src/swap_misc.c @@ -47,21 +47,12 @@ 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; 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_PRIMARY_SLOT)) { slot = BOOT_SECONDARY_SLOT; } else { return BOOT_EFLASH; @@ -132,8 +123,8 @@ 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), - &swap_state); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_SECONDARY_SLOT].area, + &swap_state); assert(rc == 0); if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { diff --git a/boot/bootutil/src/swap_move.c b/boot/bootutil/src/swap_move.c index 902932aaf..f77dce947 100644 --- a/boot/bootutil/src/swap_move.c +++ b/boot/bootutil/src/swap_move.c @@ -77,7 +77,6 @@ boot_read_image_header(struct boot_loader_state *state, int slot, uint32_t sz; uint32_t last_idx; uint32_t swap_size; - int area_id; int rc; #if (BOOT_IMAGE_NUMBER == 1) @@ -86,12 +85,11 @@ boot_read_image_header(struct boot_loader_state *state, int slot, off = 0; if (bs && !boot_status_is_reset(bs)) { - boot_find_status(BOOT_CURR_IMG(state), &fap); + fap = boot_find_status(state, BOOT_CURR_IMG(state)); if (fap == NULL || boot_read_swap_size(fap, &swap_size)) { rc = BOOT_EFLASH; goto done; } - flash_area_close(fap); last_idx = find_last_idx(state, swap_size); sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); @@ -115,12 +113,8 @@ boot_read_image_header(struct boot_loader_state *state, int 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) { - rc = BOOT_EFLASH; - goto done; - } + fap = BOOT_IMG_AREA(state, slot); + assert(fap != NULL); rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr); if (rc != 0) { @@ -137,7 +131,6 @@ boot_read_image_header(struct boot_loader_state *state, int slot, rc = 0; done: - flash_area_close(fap); return rc; } @@ -354,14 +347,14 @@ swap_status_source(struct boot_loader_state *state) image_index = BOOT_CURR_IMG(state); - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), - &state_primary_slot); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_PRIMARY_SLOT].area, + &state_primary_slot); assert(rc == 0); BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), - &state_secondary_slot); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_SECONDARY_SLOT].area, + &state_secondary_slot); assert(rc == 0); BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot); diff --git a/boot/bootutil/src/swap_offset.c b/boot/bootutil/src/swap_offset.c index 883b125ce..bade352ad 100644 --- a/boot/bootutil/src/swap_offset.c +++ b/boot/bootutil/src/swap_offset.c @@ -80,12 +80,11 @@ uint32_t find_last_idx(struct boot_loader_state *state, uint32_t swap_size) int boot_read_image_header(struct boot_loader_state *state, int slot, struct image_header *out_hdr, struct boot_status *bs) { - const struct flash_area *fap; + const struct flash_area *fap = NULL; uint32_t off = 0; uint32_t sz; uint32_t last_idx; uint32_t swap_size; - int area_id; int rc; bool check_other_sector = true; @@ -94,7 +93,7 @@ int boot_read_image_header(struct boot_loader_state *state, int slot, #endif if (bs == NULL) { - area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); + fap = BOOT_IMG_AREA(state, slot); if (slot == BOOT_SECONDARY_SLOT && boot_swap_type_multi(BOOT_CURR_IMG(state)) != BOOT_SWAP_TYPE_REVERT) { @@ -103,14 +102,13 @@ int boot_read_image_header(struct boot_loader_state *state, int slot, } else { if (!boot_status_is_reset(bs)) { check_other_sector = false; - boot_find_status(BOOT_CURR_IMG(state), &fap); + fap = boot_find_status(state, BOOT_CURR_IMG(state)); if (fap == NULL || boot_read_swap_size(fap, &swap_size)) { rc = BOOT_EFLASH; goto done; } - flash_area_close(fap); last_idx = find_last_idx(state, swap_size); sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); @@ -163,9 +161,9 @@ int boot_read_image_header(struct boot_loader_state *state, int slot, } } - area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); + fap = BOOT_IMG_AREA(state, slot); } else { - area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); + fap = BOOT_IMG_AREA(state, slot); if (bs->swap_type == BOOT_SWAP_TYPE_REVERT || boot_swap_type_multi(BOOT_CURR_IMG(state)) == BOOT_SWAP_TYPE_REVERT) { @@ -177,11 +175,7 @@ int boot_read_image_header(struct boot_loader_state *state, int slot, } } - rc = flash_area_open(area_id, &fap); - if (rc != 0) { - rc = BOOT_EFLASH; - goto done; - } + assert(fap != NULL); rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr); if (rc != 0) { @@ -222,9 +216,7 @@ int boot_read_image_header(struct boot_loader_state *state, int slot, } rc = 0; - done: - flash_area_close(fap); return rc; } @@ -429,12 +421,13 @@ int swap_status_source(struct boot_loader_state *state) #endif image_index = BOOT_CURR_IMG(state); - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), &state_primary_slot); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_PRIMARY_SLOT].area, + &state_primary_slot); assert(rc == 0); BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), - &state_secondary_slot); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_SECONDARY_SLOT].area, + &state_secondary_slot); assert(rc == 0); BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot); @@ -625,9 +618,8 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs, uint32_t last_idx; uint32_t used_sectors_pri; uint32_t used_sectors_sec; - uint8_t image_index; - const struct flash_area *fap_pri; - const struct flash_area *fap_sec; + const struct flash_area *fap_pri = NULL; + const struct flash_area *fap_sec = NULL; int rc; BOOT_LOG_INF("Starting swap using offset algorithm."); @@ -661,11 +653,11 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs, image_index = BOOT_CURR_IMG(state); - rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri); - assert (rc == 0); + fap_pri = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT); + assert(fap_pri != NULL); - rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec); - assert (rc == 0); + fap_sec = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT); + assert(fap_sec != NULL); fixup_revert(state, bs, fap_sec); @@ -731,9 +723,6 @@ void swap_run(struct boot_loader_state *state, struct boot_status *bs, idx++; } } - - flash_area_close(fap_pri); - flash_area_close(fap_sec); } int app_max_size(struct boot_loader_state *state) @@ -762,19 +751,14 @@ int boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *si uint32_t off; uint32_t secondary_slot_off = 0; uint32_t protect_tlv_size; - int area_id; int rc; #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); - if (rc != 0) { - rc = BOOT_EFLASH; - goto done; - } + fap = BOOT_IMG_AREA(state, slot); + assert(fap != NULL); off = BOOT_TLV_OFF(boot_img_hdr(state, slot)); diff --git a/boot/bootutil/src/swap_scratch.c b/boot/bootutil/src/swap_scratch.c index c9f1dde6d..a424870c4 100644 --- a/boot/bootutil/src/swap_scratch.c +++ b/boot/bootutil/src/swap_scratch.c @@ -356,12 +356,12 @@ swap_status_source(struct boot_loader_state *state) #endif image_index = BOOT_CURR_IMG(state); - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), - &state_primary_slot); + rc = boot_read_swap_state(state->imgs[image_index][BOOT_PRIMARY_SLOT].area, + &state_primary_slot); assert(rc == 0); #if MCUBOOT_SWAP_USING_SCRATCH - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch); + rc = boot_read_swap_state(state->scratch.area, &state_scratch); assert(rc == 0); #endif @@ -854,19 +854,15 @@ int app_max_size(struct boot_loader_state *state) #else int app_max_size(struct boot_loader_state *state) { - const struct flash_area *fap; - int fa_id; - int rc; + const struct flash_area *fap = NULL; uint32_t active_slot; int primary_sz, secondary_sz; 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); primary_sz = flash_area_get_size(fap); - flash_area_close(fap); if (active_slot == BOOT_PRIMARY_SLOT) { active_slot = BOOT_SECONDARY_SLOT; @@ -874,11 +870,9 @@ int app_max_size(struct boot_loader_state *state) active_slot = BOOT_PRIMARY_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); secondary_sz = flash_area_get_size(fap); - flash_area_close(fap); return (secondary_sz < primary_sz ? secondary_sz : primary_sz); } @@ -894,7 +888,6 @@ boot_read_image_header(struct boot_loader_state *state, int slot, uint32_t swap_count; uint32_t swap_size; #endif - int area_id; int hdr_slot; int rc = 0; @@ -913,7 +906,7 @@ boot_read_image_header(struct boot_loader_state *state, int slot, * other slot depending on the progress of the swap process. */ if (bs && !boot_status_is_reset(bs)) { - rc = boot_find_status(BOOT_CURR_IMG(state), &fap); + fap = boot_find_status(state, BOOT_CURR_IMG(state)); if (rc != 0) { rc = BOOT_EFLASH; @@ -953,19 +946,16 @@ boot_read_image_header(struct boot_loader_state *state, int slot, } if (hdr_slot == BOOT_NUM_SLOTS) { - area_id = FLASH_AREA_IMAGE_SCRATCH; + fap = state->scratch.area; } else { - area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), hdr_slot); + fap = BOOT_IMG_AREA(state, hdr_slot); } #else - area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), hdr_slot); + fap = BOOT_IMG_AREA(state, hdr_slot); #endif + assert(fap != NULL); - rc = flash_area_open(area_id, &fap); - if (rc == 0) { - rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); - flash_area_close(fap); - } + rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); if (rc != 0) { rc = BOOT_EFLASH;