From aef9e80a29e8ab4cde4e8de5cc8cc51767388609 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Tue, 3 Jan 2023 15:18:50 +0000 Subject: [PATCH 1/8] flash_map_backend: Remove flash_area_id_to_multi_image_slot The function has not been in use and some ports have not been implementing it anyway. Signed-off-by: Dominik Ermel --- boot/cypress/cy_flash_pal/cy_flash_map.c | 5 ----- .../include/flash_map_backend/flash_map_backend.h | 2 -- boot/mbed/include/flash_map_backend/flash_map_backend.h | 1 - boot/mbed/src/flash_map_backend.cpp | 4 ---- boot/zephyr/flash_map_extended.c | 5 ----- boot/zephyr/include/flash_map_backend/flash_map_backend.h | 8 -------- sim/mcuboot-sys/csupport/storage/flash_map.h | 1 - 7 files changed, 26 deletions(-) diff --git a/boot/cypress/cy_flash_pal/cy_flash_map.c b/boot/cypress/cy_flash_pal/cy_flash_map.c index 0878568d3..af52bab9c 100644 --- a/boot/cypress/cy_flash_pal/cy_flash_map.c +++ b/boot/cypress/cy_flash_pal/cy_flash_map.c @@ -438,11 +438,6 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id) return -1; } -int flash_area_id_to_image_slot(int area_id) -{ - return flash_area_id_to_multi_image_slot(0, area_id); -} - uint8_t flash_area_erased_val(const struct flash_area *fap) { int ret = 0; diff --git a/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h b/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h index 6fd506d2f..6330934eb 100644 --- a/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h +++ b/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h @@ -172,8 +172,6 @@ uint32_t flash_area_align(const struct flash_area *); int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret); /*< Returns the `fa_id` for slot, where slot is 0 (primary) or 1 (secondary) */ int flash_area_id_from_image_slot(int slot); -/*< Returns the slot, for the `fa_id` supplied */ -int flash_area_id_to_image_slot(int area_id); int flash_area_id_from_multi_image_slot(int image_index, int slot); int flash_area_id_to_multi_image_slot(int image_index, int area_id); diff --git a/boot/mbed/include/flash_map_backend/flash_map_backend.h b/boot/mbed/include/flash_map_backend/flash_map_backend.h index 34bac73e5..d526c5cfa 100644 --- a/boot/mbed/include/flash_map_backend/flash_map_backend.h +++ b/boot/mbed/include/flash_map_backend/flash_map_backend.h @@ -161,7 +161,6 @@ int flash_area_id_from_image_slot(int slot); int flash_area_id_from_multi_image_slot(int image_index, int slot); -int flash_area_id_to_image_slot(int area_id); /** * Converts the specified flash area ID and image index (in multi-image setup) * to an image slot index. diff --git a/boot/mbed/src/flash_map_backend.cpp b/boot/mbed/src/flash_map_backend.cpp index 7a1908afd..955ac42b6 100644 --- a/boot/mbed/src/flash_map_backend.cpp +++ b/boot/mbed/src/flash_map_backend.cpp @@ -231,10 +231,6 @@ int flash_area_id_from_image_slot(int slot) { return slot; } -int flash_area_id_to_image_slot(int area_id) { - return area_id; -} - /** * Multi images support not implemented yet */ diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c index eaf34ae35..b4d0a3eb1 100644 --- a/boot/zephyr/flash_map_extended.c +++ b/boot/zephyr/flash_map_extended.c @@ -83,11 +83,6 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id) return -1; } -int flash_area_id_to_image_slot(int area_id) -{ - return flash_area_id_to_multi_image_slot(0, area_id); -} - #if defined(CONFIG_MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD) int flash_area_id_from_direct_image(int image_id) { 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 dff2bb761..fa08ac405 100644 --- a/boot/zephyr/include/flash_map_backend/flash_map_backend.h +++ b/boot/zephyr/include/flash_map_backend/flash_map_backend.h @@ -47,14 +47,6 @@ 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); -/** - * 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); - /** * Converts the specified flash area ID and image index (in multi-image setup) * to an image slot index. diff --git a/sim/mcuboot-sys/csupport/storage/flash_map.h b/sim/mcuboot-sys/csupport/storage/flash_map.h index 92649d9d3..bf53b2a8e 100644 --- a/sim/mcuboot-sys/csupport/storage/flash_map.h +++ b/sim/mcuboot-sys/csupport/storage/flash_map.h @@ -152,7 +152,6 @@ int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret); int flash_area_id_from_image_slot(int slot); int flash_area_id_from_multi_image_slot(int image_index, int slot); -int flash_area_id_to_image_slot(int area_id); int flash_area_id_to_multi_image_slot(int image_index, int area_id); #ifdef __cplusplus From fa01b2fde9c45fa397dc1ec8a5066a6d32030d14 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 2/8] zephyr: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- boot/zephyr/flash_map_extended.c | 30 +++++++++++++++++++ .../flash_map_backend/flash_map_backend.h | 15 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c index b4d0a3eb1..0faa41068 100644 --- a/boot/zephyr/flash_map_extended.c +++ b/boot/zephyr/flash_map_extended.c @@ -135,3 +135,33 @@ __weak uint8_t flash_area_erased_val(const struct flash_area *fap) (void)fap; return ERASED_VAL; } + +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *ret) +{ + off_t fa_off = fa->fa_off; + off_t offset = 0; + size_t fa_size = fa->fa_size; + int max_sectors = *count; + int sector_idx = 0; + int rc = 0; + + while (rc == 0 && sector_idx < max_sectors && offset < fa_size) { + struct flash_pages_info fpi; + + rc = flash_get_page_info_by_offs(fa->fa_dev, fa_off + offset, &fpi); + + if (rc == 0) { + ret[sector_idx].fs_off = fpi.start_offset - fa_off; + ret[sector_idx].fs_size = fpi.size; + ++sector_idx; + offset += fpi.size; + } + } + + if (sector_idx >= max_sectors && offset >= fa_size) { + return -ENOENT; + } + + return rc; +} 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 fa08ac405..919c3d757 100644 --- a/boot/zephyr/include/flash_map_backend/flash_map_backend.h +++ b/boot/zephyr/include/flash_map_backend/flash_map_backend.h @@ -62,6 +62,21 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id); */ int flash_area_sector_from_off(off_t off, struct flash_sector *sector); +/* Get sectors for given flash_area object. This function is similar to + * flash_area_get_sectors but takes flash_area object pointer instead + * of flash area identifier. + * + * @param fa pointer to flash_area object. + * @param count in: size of array for returned sectors, out: number + * of sectors filled in. + * @param ret array of sectors. + * + * Returns 0 on success, -ERANGE when there is not enough space in + * @p ret for sector information; other negative errno code. + */ +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *ret); + static inline uint32_t flash_area_get_off(const struct flash_area *fa) { return (uint32_t)fa->fa_off; From 45865bfe278ab3babc9067468704c3fd974b3955 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 3/8] nuttx: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- .../flash_map_backend/flash_map_backend.h | 22 +++++++++ .../src/flash_map_backend/flash_map_backend.c | 46 +++++++++++++++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/boot/nuttx/include/flash_map_backend/flash_map_backend.h b/boot/nuttx/include/flash_map_backend/flash_map_backend.h index 521ff5d55..04d953cf4 100644 --- a/boot/nuttx/include/flash_map_backend/flash_map_backend.h +++ b/boot/nuttx/include/flash_map_backend/flash_map_backend.h @@ -351,6 +351,28 @@ uint8_t flash_area_erased_val(const struct flash_area *fa); int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); +/**************************************************************************** + * Name: flash_area_get_sectors_fa + * + * Description: + * Retrieve info about sectors within the area. + * + * Input Parameters: + * fa - flash area object pointer. + * count - On input, represents the capacity of the sectors buffer. + * + * Output Parameters: + * count - On output, it shall contain the number of retrieved sectors. + * sectors - Buffer for sectors data. + * + * Returned Value: + * Zero on success, or negative value in case of error. + * + ****************************************************************************/ + +int flash_area_get_sectors(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors); + /**************************************************************************** * Name: flash_area_id_from_multi_image_slot * diff --git a/boot/nuttx/src/flash_map_backend/flash_map_backend.c b/boot/nuttx/src/flash_map_backend/flash_map_backend.c index 5a1c1db39..c71f4fb30 100644 --- a/boot/nuttx/src/flash_map_backend/flash_map_backend.c +++ b/boot/nuttx/src/flash_map_backend/flash_map_backend.c @@ -641,13 +641,13 @@ uint8_t flash_area_erased_val(const struct flash_area *fa) } /**************************************************************************** - * Name: flash_area_get_sectors + * Name: flash_area_get_sectors_fa * * Description: * Retrieve info about sectors within the area. * * Input Parameters: - * fa_id - ID of the flash area whose info will be retrieved. + * fa - pointer to flash area object. * count - On input, represents the capacity of the sectors buffer. * * Output Parameters: @@ -659,15 +659,9 @@ uint8_t flash_area_erased_val(const struct flash_area *fa) * ****************************************************************************/ -int flash_area_get_sectors(int fa_id, uint32_t *count, - struct flash_sector *sectors) +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors) { - size_t off; - uint32_t total_count = 0; - struct flash_device_s *dev = lookup_flash_device_by_id(fa_id); - const size_t sector_size = dev->mtdgeo.erasesize; - const struct flash_area *fa = fa = dev->fa_cfg; - for (off = 0; off < fa->fa_size; off += sector_size) { /* Note: Offset here is relative to flash area, not device */ @@ -686,6 +680,38 @@ int flash_area_get_sectors(int fa_id, uint32_t *count, return OK; } + +/**************************************************************************** + * Name: flash_area_get_sectors + * + * Description: + * Retrieve info about sectors within the area. + * + * Input Parameters: + * fa_id - ID of the flash area whose info will be retrieved. + * count - On input, represents the capacity of the sectors buffer. + * + * Output Parameters: + * count - On output, it shall contain the number of retrieved sectors. + * sectors - Buffer for sectors data. + * + * Returned Value: + * Zero on success, or negative value in case of error. + * + ****************************************************************************/ + +int flash_area_get_sectors(int fa_id, uint32_t *count, + struct flash_sector *sectors) +{ + size_t off; + uint32_t total_count = 0; + struct flash_device_s *dev = lookup_flash_device_by_id(fa_id); + const size_t sector_size = dev->mtdgeo.erasesize; + const struct flash_area *fa = fa = dev->fa_cfg; + + return flash_area_get_sectors_fa(fa, count, sectors); +} + /**************************************************************************** * Name: flash_area_id_from_multi_image_slot * From e69fc07d4d67091fa1ad42e0a713828742eeb42d Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 4/8] mbed: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- boot/mbed/include/flash_map_backend/flash_map_backend.h | 2 ++ boot/mbed/src/flash_map_backend.cpp | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/boot/mbed/include/flash_map_backend/flash_map_backend.h b/boot/mbed/include/flash_map_backend/flash_map_backend.h index d526c5cfa..4787c0caf 100644 --- a/boot/mbed/include/flash_map_backend/flash_map_backend.h +++ b/boot/mbed/include/flash_map_backend/flash_map_backend.h @@ -155,6 +155,8 @@ uint8_t flash_area_erased_val(const struct flash_area * fap); */ int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors); int flash_area_id_from_image_slot(int slot); diff --git a/boot/mbed/src/flash_map_backend.cpp b/boot/mbed/src/flash_map_backend.cpp index 955ac42b6..0375cbef6 100644 --- a/boot/mbed/src/flash_map_backend.cpp +++ b/boot/mbed/src/flash_map_backend.cpp @@ -227,6 +227,11 @@ int flash_area_get_sectors(int fa_id, uint32_t* count, struct flash_sector* sect return 0; } +int flash_area_get_sectors_fa(const struct flash_area *fap, uint32_t* count, + struct flash_sector* sectors) { + return flash_area_get_sectors(fap->fa_id, count, sectors); +} + int flash_area_id_from_image_slot(int slot) { return slot; } From 185652928a223145b0e6981f83d764be59e0ecc1 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 5/8] espressif: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- .../flash_map_backend/flash_map_backend.h | 2 ++ boot/espressif/port/esp_mcuboot.c | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/boot/espressif/include/flash_map_backend/flash_map_backend.h b/boot/espressif/include/flash_map_backend/flash_map_backend.h index 804b76839..f5caba290 100644 --- a/boot/espressif/include/flash_map_backend/flash_map_backend.h +++ b/boot/espressif/include/flash_map_backend/flash_map_backend.h @@ -76,6 +76,8 @@ uint8_t flash_area_erased_val(const struct flash_area *area); //! Given flash area ID, return info about sectors within the area int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors); //! Retrieve the flash sector a given offset belongs to. int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector); diff --git a/boot/espressif/port/esp_mcuboot.c b/boot/espressif/port/esp_mcuboot.c index 088749c3e..46b2d7c25 100644 --- a/boot/espressif/port/esp_mcuboot.c +++ b/boot/espressif/port/esp_mcuboot.c @@ -342,15 +342,9 @@ uint8_t flash_area_erased_val(const struct flash_area *area) { return 0xff; } - -int flash_area_get_sectors(int fa_id, uint32_t *count, - struct flash_sector *sectors) +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors) { - const struct flash_area *fa = prv_lookup_flash_area(fa_id); - if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) { - return -1; - } - const size_t sector_size = FLASH_SECTOR_SIZE; uint32_t total_count = 0; for (size_t off = 0; off < fa->fa_size; off += sector_size) { @@ -364,6 +358,17 @@ int flash_area_get_sectors(int fa_id, uint32_t *count, return 0; } +int flash_area_get_sectors(int fa_id, uint32_t *count, + struct flash_sector *sectors) +{ + const struct flash_area *fa = prv_lookup_flash_area(fa_id); + if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) { + return -1; + } + + return flash_area_get_sectors_fa(fa, count, sectors); +} + int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector) { sector->fs_off = (off / FLASH_SECTOR_SIZE) * FLASH_SECTOR_SIZE; From eb70599bf95b66a8a7049391cdf708f063391280 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 6/8] cypress: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- boot/cypress/cy_flash_pal/cy_flash_map.c | 90 ++++++++++--------- .../flash_map_backend/flash_map_backend.h | 2 + 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/boot/cypress/cy_flash_pal/cy_flash_map.c b/boot/cypress/cy_flash_pal/cy_flash_map.c index af52bab9c..3be955ded 100644 --- a/boot/cypress/cy_flash_pal/cy_flash_map.c +++ b/boot/cypress/cy_flash_pal/cy_flash_map.c @@ -481,67 +481,73 @@ int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off, } #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS -int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret) +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *cnt, struct flash_sector *ret) { int rc = 0; uint32_t i = 0; struct flash_area *fa = NULL; - while(NULL != boot_area_descs[i]) + size_t sector_size = 0; + + if(fa->fa_device_id == FLASH_DEVICE_INTERNAL_FLASH) { - if(idx == boot_area_descs[i]->fa_id) - { - fa = boot_area_descs[i]; - break; - } - i++; + sector_size = CY_FLASH_SIZEOF_ROW; + } +#ifdef CY_BOOT_USE_EXTERNAL_FLASH + else if((fa->fa_device_id & FLASH_DEVICE_EXTERNAL_FLAG) == FLASH_DEVICE_EXTERNAL_FLAG) + { + /* implement for SMIF */ + /* lets assume they are equal */ + sector_size = CY_FLASH_SIZEOF_ROW; + } +#endif + else + { + rc = -1; } - if(NULL != boot_area_descs[i]) + if(0 == rc) { - size_t sector_size = 0; + uint32_t addr = 0; + size_t sectors_n = 0; - if(fa->fa_device_id == FLASH_DEVICE_INTERNAL_FLASH) - { - sector_size = CY_FLASH_SIZEOF_ROW; - } -#ifdef CY_BOOT_USE_EXTERNAL_FLASH - else if((fa->fa_device_id & FLASH_DEVICE_EXTERNAL_FLAG) == FLASH_DEVICE_EXTERNAL_FLAG) - { - /* implement for SMIF */ - /* lets assume they are equal */ - sector_size = CY_FLASH_SIZEOF_ROW; - } -#endif - else - { - rc = -1; - } + sectors_n = (fa->fa_size + (sector_size - 1)) / sector_size; + assert(sectors_n <= *cnt); - if(0 == rc) + addr = fa->fa_off; + for(i = 0; i < sectors_n; i++) { - uint32_t addr = 0; - size_t sectors_n = 0; + ret[i].fs_size = sector_size ; + ret[i].fs_off = addr ; + addr += sector_size ; + } - sectors_n = (fa->fa_size + (sector_size - 1)) / sector_size; - assert(sectors_n <= *cnt); + *cnt = sectors_n; + } + return rc; +} - addr = fa->fa_off; - for(i = 0; i < sectors_n; i++) - { - ret[i].fs_size = sector_size ; - ret[i].fs_off = addr ; - addr += sector_size ; - } +int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret) +{ + int rc = -1; + uint32_t i = 0; + struct flash_area *fa = NULL; - *cnt = sectors_n; + while(NULL != boot_area_descs[i]) + { + if(idx == boot_area_descs[i]->fa_id) + { + fa = boot_area_descs[i]; + break; } + i++; } - else + + if(NULL != fa) { - rc = -1; + flash_area_get_sectors_fa(fa, cnt, ret); } - return rc; } + #endif diff --git a/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h b/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h index 6330934eb..bd17c9686 100644 --- a/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h +++ b/boot/cypress/cy_flash_pal/include/flash_map_backend/flash_map_backend.h @@ -177,6 +177,8 @@ int flash_area_id_from_multi_image_slot(int image_index, int slot); int flash_area_id_to_multi_image_slot(int image_index, int area_id); #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret); +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *cnt, + struct flash_sector *ret); #endif /* * Returns the value expected to be read when accesing any erased From 92e0e1ecb657db58caeacc89c9cbd9b426c950f2 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Thu, 5 Jan 2023 17:25:20 +0000 Subject: [PATCH 7/8] sim: Add implementation of flash_area_get_sectors_fa Variant of flash_area_get_sectors that takes flash_area object pointer instead of flash area identifier. Signed-off-by: Dominik Ermel --- sim/mcuboot-sys/csupport/run.c | 34 ++++++++++++++++++++ sim/mcuboot-sys/csupport/storage/flash_map.h | 5 +++ 2 files changed, 39 insertions(+) diff --git a/sim/mcuboot-sys/csupport/run.c b/sim/mcuboot-sys/csupport/run.c index df62cb4d8..0601d8948 100644 --- a/sim/mcuboot-sys/csupport/run.c +++ b/sim/mcuboot-sys/csupport/run.c @@ -396,6 +396,40 @@ int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) return 0; } +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors) +{ + uint32_t i; + struct area *slot; + struct area_desc *flash_areas; + + flash_areas = sim_get_flash_areas(); + for (i = 0; i < flash_areas->num_slots; i++) { + if (flash_areas->slots[i].id == fa->fa_id) + break; + } + if (i == flash_areas->num_slots) { + printf("Unsupported area\n"); + abort(); + } + + slot = &flash_areas->slots[i]; + + if (slot->num_areas > *count) { + printf("Too many areas in slot\n"); + abort(); + } + + for (i = 0; i < slot->num_areas; i++) { + sectors[i].fs_off = slot->areas[i].fa_off - + slot->whole.fa_off; + sectors[i].fs_size = slot->areas[i].fa_size; + } + *count = slot->num_areas; + + return 0; +} + int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors) { diff --git a/sim/mcuboot-sys/csupport/storage/flash_map.h b/sim/mcuboot-sys/csupport/storage/flash_map.h index bf53b2a8e..5493029e3 100644 --- a/sim/mcuboot-sys/csupport/storage/flash_map.h +++ b/sim/mcuboot-sys/csupport/storage/flash_map.h @@ -135,6 +135,11 @@ uint8_t flash_area_erased_val(const struct flash_area *); */ int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); +/* + * Given flash area object poiner, return info about sectors within the area. + */ +int flash_area_get_sectors_fa(const struct flash_area *fa, uint32_t *count, + struct flash_sector *sectors); /* Retrieve the flash sector a given offset belongs to. From 9dfeac319b12a186535bc3dca2a378c7e6d21bb7 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 21 Dec 2022 15:46:17 +0000 Subject: [PATCH 8/8] 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); }