From 9dfeac319b12a186535bc3dca2a378c7e6d21bb7 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 21 Dec 2022 15:46:17 +0000 Subject: [PATCH] boot: RFC: WIP: Preliminary work on flash_area open-once The code has been modified to have single place where flash_area area opened, at the beginning of execution. Currently only Zephyr compiles properly and works. close is missing. Signed-off-by: Dominik Ermel --- boot/bootutil/include/bootutil/bootutil.h | 20 ++++ boot/bootutil/src/bootutil_priv.h | 3 +- boot/bootutil/src/bootutil_public.c | 8 +- boot/bootutil/src/encrypted.c | 6 +- boot/bootutil/src/loader.c | 98 +++++-------------- boot/bootutil/src/swap_misc.c | 54 +++------- boot/bootutil/src/swap_move.c | 31 +++++- boot/bootutil/src/swap_scratch.c | 14 +-- boot/zephyr/flash_map_extended.c | 39 +++++--- .../flash_map_backend/flash_map_backend.h | 6 +- .../include/mcuboot_config/mcuboot_config.h | 4 + boot/zephyr/main.c | 34 +++++++ boot/zephyr/single_loader.c | 9 +- 13 files changed, 171 insertions(+), 155 deletions(-) diff --git a/boot/bootutil/include/bootutil/bootutil.h b/boot/bootutil/include/bootutil/bootutil.h index 9c1fbab4e..09cd21880 100644 --- a/boot/bootutil/include/bootutil/bootutil.h +++ b/boot/bootutil/include/bootutil/bootutil.h @@ -77,6 +77,26 @@ struct image_trailer { uint8_t magic[BOOT_MAGIC_SZ]; }; +#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT) +#define FLASH_AREA_IMAGES 1 +#elif MCUBOOT_IMAGE_NUMBER == 1 +#define FLASH_AREA_IMAGES 2 +#elif MCUBOOT_IMAGE_NUMBER == 2 +#define FLASH_AREA_IMAGES 4 +#endif + +#if !defined(MCUBOOT_SWAP_USING_SCRATCH) +#define FLASH_AREA_OBJECTS FLASH_AREA_IMAGES +#else +#define FLASH_AREA_OBJECTS (FLASH_AREA_IMAGES + 1) +#define SCRATCH_FA flash_area_objects[FLASH_AREA_OBJECTS - 1] +#endif + +extern const struct flash_area *flash_area_objects[FLASH_AREA_OBJECTS]; + +#define PRIMARY_IMAGE_FA(x) flash_area_objects[(x) + 0] +#define SECONDARY_IMAGE_FA(x) flash_area_objects[(x) + 1] + /* you must have pre-allocated all the entries within this structure */ fih_int boot_go(struct boot_rsp *rsp); fih_int boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id); diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h index 43a2bace4..6f6cbafe1 100644 --- a/boot/bootutil/src/bootutil_priv.h +++ b/boot/bootutil/src/bootutil_priv.h @@ -56,9 +56,10 @@ struct flash_area; #if (defined(MCUBOOT_OVERWRITE_ONLY) + \ defined(MCUBOOT_SWAP_USING_MOVE) + \ + defined(MCUBOOT_SWAP_USING_SCRATCH) + \ defined(MCUBOOT_DIRECT_XIP) + \ defined(MCUBOOT_RAM_LOAD)) > 1 -#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD" +#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_SWAP_USING_SCRATCH, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD" #endif #if !defined(MCUBOOT_OVERWRITE_ONLY) && \ diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index c567892da..8b6177283 100644 --- a/boot/bootutil/src/bootutil_public.c +++ b/boot/bootutil/src/bootutil_public.c @@ -432,15 +432,15 @@ boot_swap_type_multi(int image_index) BOOT_HOOK_REGULAR, image_index, &primary_slot); if (rc == BOOT_HOOK_REGULAR) { - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), - &primary_slot); + rc = boot_read_swap_state(PRIMARY_IMAGE_FA(image_index), + &primary_slot); } if (rc) { return BOOT_SWAP_TYPE_PANIC; } - rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), - &secondary_slot); + rc = boot_read_swap_state(SECONDARY_IMAGE_FA(image_index), + &secondary_slot); if (rc == BOOT_EFLASH) { BOOT_LOG_INF("Secondary image of image pair (%d.) " "is unreachable. Treat it as empty", image_index); diff --git a/boot/bootutil/src/encrypted.c b/boot/bootutil/src/encrypted.c index fdd98524c..030cacce5 100644 --- a/boot/bootutil/src/encrypted.c +++ b/boot/bootutil/src/encrypted.c @@ -698,7 +698,7 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index, uint8_t slot; int rc; - rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap)); + rc = flash_area_multi_image_slot(image_index, fap); if (rc < 0) { return rc; } @@ -745,7 +745,7 @@ boot_enc_valid(struct enc_key_data *enc_state, int image_index, { int rc; - rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap)); + rc = flash_area_to_multi_image_slot(image_index, fap); if (rc < 0) { /* can't get proper slot number - skip encryption, */ /* postpone the error for a upper layer */ @@ -777,7 +777,7 @@ boot_encrypt(struct enc_key_data *enc_state, int image_index, nonce[14] = (uint8_t)(off >> 8); nonce[15] = (uint8_t)off; - rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap)); + rc = flash_area_to_multi_image_slot(image_index, fap); if (rc < 0) { assert(0); return; diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c index e59fad78d..56a47ab47 100644 --- a/boot/bootutil/src/loader.c +++ b/boot/bootutil/src/loader.c @@ -35,6 +35,7 @@ #include #include #include +#include "mcuboot_config/mcuboot_config.h" #include "bootutil/bootutil.h" #include "bootutil/bootutil_public.h" #include "bootutil/image.h" @@ -56,7 +57,6 @@ #include #endif -#include "mcuboot_config/mcuboot_config.h" BOOT_LOG_MODULE_DECLARE(mcuboot); @@ -240,12 +240,7 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size) (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 = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); off = BOOT_TLV_OFF(boot_img_hdr(state, slot)); @@ -309,7 +304,7 @@ boot_write_sz(struct boot_loader_state *state) } static int -boot_initialize_area(struct boot_loader_state *state, int flash_area) +boot_initialize_area(struct boot_loader_state *state, const struct flash_area *fa) { uint32_t num_sectors = BOOT_MAX_IMG_SECTORS; boot_sector_t *out_sectors; @@ -318,14 +313,14 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area) num_sectors = BOOT_MAX_IMG_SECTORS; - if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) { + if (fa == PRIMARY_IMAGE_FA(BOOT_CURR_IMG(state))) { out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors; out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors; - } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) { + } else if (fa == SECONDARY_IMAGE_FA(BOOT_CURR_IMG(state))) { out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors; out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors; #if MCUBOOT_SWAP_USING_SCRATCH - } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) { + } else if (fa == SCRATCH_FA) { out_sectors = state->scratch.sectors; out_num_sectors = &state->scratch.num_sectors; #endif @@ -334,8 +329,12 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area) } #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS - rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors); + rc = flash_area_get_sectors_fa(fa, &num_sectors, out_sectors); #else + /* TODO: This is only used by mynewt; note that flash_area_to_sectors + * actually does flash_area_open/flash_area_close pair but this safe for now, + * as these function do no locking or reference counting. */ + int flash_area = flash_area_get_id(fa); _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed"); rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors); #endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */ @@ -360,12 +359,12 @@ boot_read_sectors(struct boot_loader_state *state) image_index = BOOT_CURR_IMG(state); - rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index)); + rc = boot_initialize_area(state, PRIMARY_IMAGE_FA(image_index)); if (rc != 0) { return BOOT_EFLASH; } - rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index)); + rc = boot_initialize_area(state, SECONDARY_IMAGE_FA(image_index)); if (rc != 0) { /* We need to differentiate from the primary image issue */ return BOOT_EFLASH_SEC; @@ -439,20 +438,15 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) #if MCUBOOT_SWAP_USING_SCRATCH if (bs->use_scratch) { /* Write to scratch. */ - area_id = FLASH_AREA_IMAGE_SCRATCH; + fap = SCRATCH_FA; } else { #endif /* Write to the primary slot. */ - area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); + fap = PRIMARY_IMAGE_FA(BOOT_CURR_IMG(state)); #if MCUBOOT_SWAP_USING_SCRATCH } #endif - rc = flash_area_open(area_id, &fap); - if (rc != 0) { - return BOOT_EFLASH; - } - off = boot_status_off(fap) + boot_status_internal_off(bs, BOOT_WRITE_SZ(state)); align = flash_area_align(fap); @@ -465,8 +459,6 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) rc = BOOT_EFLASH; } - flash_area_close(fap); - return rc; } #endif /* !MCUBOOT_RAM_LOAD */ @@ -596,14 +588,9 @@ boot_check_header_erased(struct boot_loader_state *state, int slot) 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 = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); 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))) { @@ -712,11 +699,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot, fih_int fih_rc = FIH_FAILURE; 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) { - FIH_RET(fih_rc); - } + fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); hdr = boot_img_hdr(state, slot); if (boot_check_header_erased(state, slot) == 0 || @@ -817,8 +800,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot, #endif out: - flash_area_close(fap); - FIH_RET(fih_rc); } @@ -844,12 +825,7 @@ boot_update_security_counter(uint8_t image_index, int slot, uint32_t img_security_cnt; int rc; - rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot), - &fap); - if (rc != 0) { - rc = BOOT_EFLASH; - goto done; - } + fap = flash_area_from_multi_image_slot(image_index, slot); rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt); if (rc != 0) { @@ -862,7 +838,6 @@ boot_update_security_counter(uint8_t image_index, int slot, } done: - flash_area_close(fap); return rc; } #endif /* MCUBOOT_HW_ROLLBACK_PROT */ @@ -1410,12 +1385,7 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot) 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 = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap, IMAGE_TLV_DEPENDENCY, true); @@ -1457,7 +1427,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot) } done: - flash_area_close(fap); return rc; } @@ -1966,7 +1935,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) struct boot_status bs; int rc = -1; fih_int fih_rc = FIH_FAILURE; - int fa_id; int image_index; bool has_upgrade; @@ -2019,14 +1987,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp) * of this call. */ for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) { - fa_id = flash_area_id_from_multi_image_slot(image_index, slot); - rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot)); - assert(rc == 0); + BOOT_IMG_AREA(state, slot) = flash_area_from_multi_image_slot(image_index, slot); } #if MCUBOOT_SWAP_USING_SCRATCH - rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, - &BOOT_SCRATCH_AREA(state)); - assert(rc == 0); + BOOT_SCRATCH_AREA(state) = SCRATCH_FA; #endif /* Determine swap type and complete swap if it has been aborted. */ @@ -2213,8 +2177,6 @@ split_go(int loader_slot, int split_slot, void **entry) { boot_sector_t *sectors; uintptr_t entry_val; - int loader_flash_id; - int split_flash_id; int rc; fih_int fih_rc = FIH_FAILURE; @@ -2225,14 +2187,8 @@ split_go(int loader_slot, int split_slot, void **entry) BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0; BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS; - loader_flash_id = flash_area_id_from_image_slot(loader_slot); - rc = flash_area_open(loader_flash_id, - &BOOT_IMG_AREA(&boot_data, loader_slot)); - assert(rc == 0); - split_flash_id = flash_area_id_from_image_slot(split_slot); - rc = flash_area_open(split_flash_id, - &BOOT_IMG_AREA(&boot_data, split_slot)); - assert(rc == 0); + BOOT_IMG_AREA(&boot_data, loader_slot) = flash_area_from_image_slot(loader_slot); + BOOT_IMG_AREA(&boot_data, split_slot) = flash_area_from_image_slot(split_slot); /* Determine the sector layout of the image slots and scratch area. */ rc = boot_read_sectors(&boot_data); @@ -2577,11 +2533,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state, uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst); image_index = BOOT_CURR_IMG(state); - 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 = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); tlv_off = BOOT_TLV_OFF(hdr); @@ -2633,8 +2585,6 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state, rc = 0; done: - flash_area_close(fap_src); - return rc; } diff --git a/boot/bootutil/src/swap_misc.c b/boot/bootutil/src/swap_misc.c index 733a39744..d5536e3aa 100644 --- a/boot/bootutil/src/swap_misc.c +++ b/boot/bootutil/src/swap_misc.c @@ -41,22 +41,22 @@ 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; + const struct flash_area *fa_primary; + const struct flash_area *fa_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); + fa_primary = flash_area_from_multi_image_slot(image_index, + BOOT_PRIMARY_SLOT); + fa_secondary = flash_area_from_multi_image_slot(image_index, + BOOT_SECONDARY_SLOT); - if (flash_area_get_id(fap) == fa_id_primary) { + if (fap == fa_primary) { slot = BOOT_PRIMARY_SLOT; - } else if (flash_area_get_id(fap) == fa_id_secondary) { + } else if (fap == fa_secondary) { slot = BOOT_SECONDARY_SLOT; } else { return BOOT_EFLASH; @@ -96,8 +96,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(SECONDARY_IMAGE_FA(image_index), + &swap_state); assert(rc == 0); if (bs->swap_type != BOOT_SWAP_TYPE_NONE) { @@ -133,7 +133,6 @@ swap_read_status(struct boot_loader_state *state, struct boot_status *bs) const struct flash_area *fap; uint32_t off; uint8_t swap_info; - int area_id; int rc; bs->source = swap_status_source(state); @@ -143,12 +142,12 @@ swap_read_status(struct boot_loader_state *state, struct boot_status *bs) #if MCUBOOT_SWAP_USING_SCRATCH case BOOT_STATUS_SOURCE_SCRATCH: - area_id = FLASH_AREA_IMAGE_SCRATCH; + fap = SCRATCH_FA; break; #endif case BOOT_STATUS_SOURCE_PRIMARY_SLOT: - area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)); + fap = PRIMARY_IMAGE_FA(BOOT_CURR_IMG(state)); break; default: @@ -156,11 +155,6 @@ swap_read_status(struct boot_loader_state *state, struct boot_status *bs) return BOOT_EBADARGS; } - rc = flash_area_open(area_id, &fap); - if (rc != 0) { - return BOOT_EFLASH; - } - rc = swap_read_status_bytes(fap, state, bs); if (rc == 0) { off = boot_swap_info_off(fap); @@ -180,41 +174,22 @@ swap_read_status(struct boot_loader_state *state, struct boot_status *bs) } done: - flash_area_close(fap); - return rc; } int swap_set_copy_done(uint8_t image_index) { - const struct flash_area *fap; - int rc; - - rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), - &fap); - if (rc != 0) { - return BOOT_EFLASH; - } - - rc = boot_write_copy_done(fap); - flash_area_close(fap); - return rc; + return boot_write_copy_done(PRIMARY_IMAGE_FA(image_index)); } int swap_set_image_ok(uint8_t image_index) { - const struct flash_area *fap; + const struct flash_area *const fap = PRIMARY_IMAGE_FA(image_index); struct boot_swap_state state; int rc; - rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), - &fap); - if (rc != 0) { - return BOOT_EFLASH; - } - rc = boot_read_swap_state(fap, &state); if (rc != 0) { rc = BOOT_EFLASH; @@ -226,7 +201,6 @@ swap_set_image_ok(uint8_t image_index) } out: - flash_area_close(fap); return rc; } diff --git a/boot/bootutil/src/swap_move.c b/boot/bootutil/src/swap_move.c index 6f3398270..7fc332347 100644 --- a/boot/bootutil/src/swap_move.c +++ b/boot/bootutil/src/swap_move.c @@ -56,7 +56,9 @@ boot_read_image_header(struct boot_loader_state *state, int slot, const struct flash_area *fap; uint32_t off; uint32_t sz; +#if OLD int area_id; +#endif int rc; #if (BOOT_IMAGE_NUMBER == 1) @@ -89,12 +91,15 @@ boot_read_image_header(struct boot_loader_state *state, int slot, } } +#if OLD 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; } +#endif + fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr); if (rc != 0) { @@ -111,7 +116,9 @@ boot_read_image_header(struct boot_loader_state *state, int slot, rc = 0; done: +#if OLD flash_area_close(fap); +#endif return rc; } @@ -275,15 +282,24 @@ swap_status_source(struct boot_loader_state *state) image_index = BOOT_CURR_IMG(state); +#if OLD rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), &state_primary_slot); assert(rc == 0); +#endif + rc = boot_read_swap_state(PRIMARY_IMAGE_FA(image_index), + &state_primary_slot); + assert(rc == 0); BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); +#if OLD rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), &state_secondary_slot); assert(rc == 0); +#endif + rc = boot_read_swap_state(SECONDARY_IMAGE_FA(image_index), + &state_secondary_slot); BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot); @@ -445,8 +461,10 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs, uint32_t trailer_sz; uint32_t first_trailer_idx; uint8_t image_index; +#ifdef OLD const struct flash_area *fap_pri; const struct flash_area *fap_sec; +#endif int rc; BOOT_LOG_INF("Starting swap using move algorithm."); @@ -492,6 +510,7 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs, image_index = BOOT_CURR_IMG(state); +#ifdef OLD rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri); assert (rc == 0); @@ -499,12 +518,16 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs, assert (rc == 0); fixup_revert(state, bs, fap_sec); +#endif + fixup_revert(state, bs, SECONDARY_IMAGE_FA(image_index)); if (bs->op == BOOT_STATUS_OP_MOVE) { idx = g_last_idx; while (idx > 0) { if (idx <= (g_last_idx - bs->idx + 1)) { - boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec); + boot_move_sector_up(idx, sector_sz, state, bs, + PRIMARY_IMAGE_FA(image_index), + SECONDARY_IMAGE_FA(image_index)); } idx--; } @@ -516,13 +539,17 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs, idx = 1; while (idx <= g_last_idx) { if (idx >= bs->idx) { - boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec); + boot_swap_sectors(idx, sector_sz, state, bs, + PRIMARY_IMAGE_FA(image_index), + SECONDARY_IMAGE_FA(image_index)); } idx++; } +#if OLD flash_area_close(fap_pri); flash_area_close(fap_sec); +#endif } #endif diff --git a/boot/bootutil/src/swap_scratch.c b/boot/bootutil/src/swap_scratch.c index f3275c747..68c66f299 100644 --- a/boot/bootutil/src/swap_scratch.c +++ b/boot/bootutil/src/swap_scratch.c @@ -52,7 +52,6 @@ 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; - int area_id; int rc = 0; (void)bs; @@ -61,13 +60,8 @@ boot_read_image_header(struct boot_loader_state *state, int slot, (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 = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); - flash_area_close(fap); - } + fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot); + rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr); if (rc != 0) { rc = BOOT_EFLASH; @@ -373,12 +367,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), + rc = boot_read_swap_state(PRIMARY_IMAGE_FA(image_index), &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(SCRATCH_FA, &state_scratch); assert(rc == 0); #endif diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c index 0faa41068..ef63d00f9 100644 --- a/boot/zephyr/flash_map_extended.c +++ b/boot/zephyr/flash_map_extended.c @@ -14,6 +14,7 @@ #include #include +#include "bootutil/bootutil.h" #include "bootutil/bootutil_log.h" BOOT_LOG_MODULE_DECLARE(mcuboot); @@ -43,29 +44,24 @@ int flash_device_base(uint8_t fd_id, uintptr_t *ret) return 0; } -/* - * This depends on the mappings defined in sysflash.h. - * MCUBoot uses continuous numbering for the primary slot, the secondary slot, - * and the scratch while zephyr might number it differently. - */ -int flash_area_id_from_multi_image_slot(int image_index, int slot) +const struct flash_area* flash_area_from_multi_image_slot(int image_index, int slot) { switch (slot) { - case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); + case 0: return PRIMARY_IMAGE_FA(image_index); #if !defined(CONFIG_SINGLE_APPLICATION_SLOT) - case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); + case 1: return SECONDARY_IMAGE_FA(image_index); #endif #if defined(CONFIG_BOOT_SWAP_USING_SCRATCH) - case 2: return FLASH_AREA_IMAGE_SCRATCH; + case 2: return SCRATCH_FA; #endif } - - return -EINVAL; /* flash_area_open will fail on that */ + __ASSERT(false, "Reqesting non-existent area should have never happened"); + return NULL; } -int flash_area_id_from_image_slot(int slot) +const struct flash_area *flash_area_from_image_slot(int slot) { - return flash_area_id_from_multi_image_slot(0, slot); + return flash_area_from_multi_image_slot(0, slot); } int flash_area_id_to_multi_image_slot(int image_index, int area_id) @@ -83,8 +79,23 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id) return -1; } +int flash_area_to_multi_image_slot(int image_index, const struct flash_area *fa) +{ + if (fa == PRIMARY_IMAGE_FA(image_index)) { + return 0; + } +#if !defined(CONFIG_SINGLE_APPLICATION_SLOT) + if (fa == SECONDARY_IMAGE_FA(image_index)) { + return 1; + } +#endif + + BOOT_LOG_ERR("invalid flash area"); + return -1; +} + #if defined(CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) -int flash_area_id_from_direct_image(int image_id) +const struct flash_area *flash_area_from_direct_image(int image_id) { switch (image_id) { case 0: diff --git a/boot/zephyr/include/flash_map_backend/flash_map_backend.h b/boot/zephyr/include/flash_map_backend/flash_map_backend.h index 919c3d757..ed54c43d6 100644 --- a/boot/zephyr/include/flash_map_backend/flash_map_backend.h +++ b/boot/zephyr/include/flash_map_backend/flash_map_backend.h @@ -44,8 +44,9 @@ extern "C" { */ int flash_device_base(uint8_t fd_id, uintptr_t *ret); -int flash_area_id_from_image_slot(int slot); -int flash_area_id_from_multi_image_slot(int image_index, int slot); +const struct flash_area *flash_area_from_image_slot(int slot); +const struct flash_area *flash_area_from_multi_image_slot(int image_index, + int slot); /** * Converts the specified flash area ID and image index (in multi-image setup) @@ -55,6 +56,7 @@ int flash_area_id_from_multi_image_slot(int image_index, int slot); * slot. */ int flash_area_id_to_multi_image_slot(int image_index, int area_id); +int flash_area_to_multi_image_slot(int image_index, const struct flash_area *fa); /* Retrieve the flash sector a given offset belongs to. * diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index 10926876d..9241a0266 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -73,6 +73,10 @@ #define MCUBOOT_SWAP_USING_MOVE 1 #endif +#ifdef CONFIG_BOOT_SWAP_USING_SCRATCH +#define MCUBOOT_SWAP_USING_SCRATCH 1 +#endif + #ifdef CONFIG_BOOT_DIRECT_XIP #define MCUBOOT_DIRECT_XIP #endif diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c index 148f06e14..3b2296423 100644 --- a/boot/zephyr/main.c +++ b/boot/zephyr/main.c @@ -26,6 +26,7 @@ #include #include #include +#include "sysflash/sysflash.h" #if defined(CONFIG_CPU_AARCH32_CORTEX_A) || defined(CONFIG_CPU_AARCH32_CORTEX_R) #include @@ -118,6 +119,8 @@ static inline bool boot_skip_serial_recovery() } #endif +const struct flash_area *flash_area_objects[FLASH_AREA_OBJECTS]; + BOOT_LOG_MODULE_REGISTER(mcuboot); #ifdef CONFIG_MCUBOOT_INDICATION_LED @@ -498,6 +501,33 @@ static bool detect_pin(void) } #endif +bool flash_area_open_all() +{ + int rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &PRIMARY_IMAGE_FA(0)); + + if (rc != 0) { + BOOT_LOG_ERR("%s open failed", "Primary image"); + return false; + } +#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT) + rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &SECONDARY_IMAGE_FA(0)); + if (rc != 0) { + BOOT_LOG_ERR("%s open failed", "Secondary image"); + return false; + } +#if defined(MCUBOOT_SWAP_USING_SCRATCH) + rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &SCRATCH_FA); + if (rc != 0) { + BOOT_LOG_ERR("%s open failed", "Scratch"); + return false; + } +#endif /* defined(MCUBOOT_SWAP_USING_SCRATCH */ + +#endif /* !defined(MCUBOOT_SINGLE_APPLICATION_SLOT) */ + + return true; +} + void main(void) { struct boot_rsp rsp; @@ -525,6 +555,10 @@ void main(void) mcuboot_status_change(MCUBOOT_STATUS_STARTUP); + if (!flash_area_open_all()) { + FIH_PANIC; + } + #ifdef CONFIG_MCUBOOT_SERIAL if (detect_pin() && !boot_skip_serial_recovery()) { diff --git a/boot/zephyr/single_loader.c b/boot/zephyr/single_loader.c index f6c65f6eb..5b2b24bae 100644 --- a/boot/zephyr/single_loader.c +++ b/boot/zephyr/single_loader.c @@ -405,8 +405,11 @@ boot_handle_enc_fw() int rc = -1; fih_int fih_rc = FIH_FAILURE; +#if 0 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &_fa_p); assert(rc == 0); +#endif + _fa_p = PRIMARY_IMAGE_FA(0); rc = boot_image_load_header(_fa_p, &_hdr); if (rc != 0) { @@ -427,7 +430,6 @@ boot_handle_enc_fw() } out: - flash_area_close(_fa_p); return rc; } #endif @@ -445,8 +447,7 @@ boot_go(struct boot_rsp *rsp) int rc = -1; fih_int fih_rc = FIH_FAILURE; - rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &_fa_p); - assert(rc == 0); + _fa_p = PRIMARY_IMAGE_FA(0); rc = boot_image_load_header(_fa_p, &_hdr); if (rc != 0) @@ -471,7 +472,5 @@ boot_go(struct boot_rsp *rsp) rsp->br_hdr = &_hdr; out: - flash_area_close(_fa_p); - FIH_RET(fih_rc); }