Skip to content

Commit

Permalink
boot: Reuse pointers for flash_area objects from state
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
de-nordic committed Feb 17, 2025
1 parent 602cb45 commit 726d3bc
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 192 deletions.
34 changes: 16 additions & 18 deletions boot/bootutil/src/bootutil_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions boot/bootutil/src/bootutil_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
81 changes: 24 additions & 57 deletions boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -603,7 +598,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
}

done:
flash_area_close(fap);
return rc;
}

Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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 ||
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -1181,8 +1160,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
#endif

out:
flash_area_close(fap);

FIH_RET(fih_rc);
}

Expand All @@ -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) {
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -3006,7 +2975,6 @@ boot_select_or_erase(struct boot_loader_state *state)
rc = 0;
}
}
flash_area_close(fap);
}

return rc;
Expand Down Expand Up @@ -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)];
}

Expand Down
37 changes: 8 additions & 29 deletions boot/bootutil/src/ram_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 726d3bc

Please sign in to comment.