Skip to content

Commit 2f4f5ab

Browse files
committed
boot: Reuse pointers for flash_area objects from state
Reduce flash_area_open/flash_area_close calls by reusing pointers to flash_area objects stored in boot_loader_state, that should be populated by context_boot_go. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 7330df7 commit 2f4f5ab

File tree

8 files changed

+91
-194
lines changed

8 files changed

+91
-194
lines changed

boot/bootutil/src/bootutil_misc.c

+16-18
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,17 @@ boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
205205
* If the magic is successfully found, a flash_area * is returned and it
206206
* is the responsibility of the called to close it.
207207
*
208-
* @returns 0 on success, -1 on errors
208+
* @returns flash_area pointer on success, NULL on failure.
209209
*/
210-
int
211-
boot_find_status(int image_index, const struct flash_area **fap)
210+
const struct flash_area *
211+
boot_find_status(const struct boot_loader_state *state, int image_index)
212212
{
213-
uint8_t areas[] = {
213+
const struct flash_area *fa_p = NULL;
214+
const struct flash_area *areas[] = {
214215
#if MCUBOOT_SWAP_USING_SCRATCH
215-
FLASH_AREA_IMAGE_SCRATCH,
216+
state->scratch.area,
216217
#endif
217-
FLASH_AREA_IMAGE_PRIMARY(image_index),
218+
state->imgs[image_index][BOOT_PRIMARY_SLOT].area,
218219
};
219220
unsigned int i;
220221

@@ -225,29 +226,26 @@ boot_find_status(int image_index, const struct flash_area **fap)
225226
* is assumed that if magic is valid then other metadata is too,
226227
* because magic is always written in the last step.
227228
*/
228-
229229
for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
230230
uint8_t magic[BOOT_MAGIC_SZ];
231+
int rc = 0;
231232

232-
if (flash_area_open(areas[i], fap)) {
233-
break;
234-
}
233+
fa_p = areas[i];
234+
rc = flash_area_read(fa_p, boot_magic_off(fa_p), magic, BOOT_MAGIC_SZ);
235235

236-
if (flash_area_read(*fap, boot_magic_off(*fap), magic, BOOT_MAGIC_SZ)) {
237-
flash_area_close(*fap);
236+
if (rc != 0) {
237+
BOOT_LOG_ERR("Failed to read status from %d, err %d\n",
238+
flash_area_get_id(fa_p), rc);
239+
fa_p = NULL;
238240
break;
239241
}
240242

241243
if (BOOT_MAGIC_GOOD == boot_magic_decode(magic)) {
242-
return 0;
244+
break;
243245
}
244-
245-
flash_area_close(*fap);
246246
}
247247

248-
/* If we got here, no magic was found */
249-
fap = NULL;
250-
return -1;
248+
return fa_p;
251249
}
252250

253251
int

boot/bootutil/src/bootutil_priv.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,15 @@ fih_ret bootutil_verify_img(uint8_t *img, uint32_t size,
297297

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

300-
int boot_find_status(int image_index, const struct flash_area **fap);
300+
const struct flash_area *boot_find_status(const struct boot_loader_state *state,
301+
int image_index);
301302
int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
302303
uint32_t boot_status_sz(uint32_t min_write_sz);
303304
uint32_t boot_trailer_sz(uint32_t min_write_sz);
304305
int boot_status_entries(int image_index, const struct flash_area *fap);
305306
uint32_t boot_status_off(const struct flash_area *fap);
306307
int boot_read_swap_state(const struct flash_area *fap,
307308
struct boot_swap_state *state);
308-
int boot_read_swap_state_by_id(int flash_area_id,
309-
struct boot_swap_state *state);
310309
int boot_write_magic(const struct flash_area *fap);
311310
int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
312311
int boot_write_copy_done(const struct flash_area *fap);

boot/bootutil/src/loader.c

+23-58
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,10 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
548548
struct image_dependency dep;
549549
uint32_t off;
550550
uint16_t len;
551-
int area_id;
552551
int rc;
553552

554-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
555-
rc = flash_area_open(area_id, &fap);
556-
if (rc != 0) {
557-
rc = BOOT_EFLASH;
558-
goto done;
559-
}
553+
fap = BOOT_IMG_AREA(state, slot);
554+
assert(fap != NULL);
560555

561556
#if defined(MCUBOOT_SWAP_USING_OFFSET)
562557
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)
603598
}
604599

605600
done:
606-
flash_area_close(fap);
607601
return rc;
608602
}
609603

@@ -948,20 +942,14 @@ boot_data_is_set_to(uint8_t val, void *data, size_t len)
948942
static int
949943
boot_check_header_erased(struct boot_loader_state *state, int slot)
950944
{
951-
const struct flash_area *fap;
945+
const struct flash_area *fap = NULL;
952946
struct image_header *hdr;
953947
uint8_t erased_val;
954-
int area_id;
955-
int rc;
956948

957-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
958-
rc = flash_area_open(area_id, &fap);
959-
if (rc != 0) {
960-
return -1;
961-
}
949+
fap = BOOT_IMG_AREA(state, slot);
950+
assert(fap != NULL);
962951

963952
erased_val = flash_area_erased_val(fap);
964-
flash_area_close(fap);
965953

966954
hdr = boot_img_hdr(state, slot);
967955
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,
10221010
{
10231011
const struct flash_area *fap;
10241012
struct image_header *hdr;
1025-
int area_id;
10261013
FIH_DECLARE(fih_rc, FIH_FAILURE);
1027-
int rc;
10281014

10291015
#if !defined(MCUBOOT_SWAP_USING_OFFSET)
10301016
(void)expected_swap_type;
10311017
#endif
10321018

1033-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1034-
rc = flash_area_open(area_id, &fap);
1035-
if (rc != 0) {
1036-
FIH_RET(fih_rc);
1037-
}
1019+
fap = BOOT_IMG_AREA(state, slot);
1020+
assert(fap != NULL);
10381021

10391022
hdr = boot_img_hdr(state, slot);
10401023
if (boot_check_header_erased(state, slot) == 0 ||
@@ -1056,20 +1039,13 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
10561039
#if defined(MCUBOOT_SWAP_USING_MOVE)
10571040
if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
10581041
boot_swap_type_multi(BOOT_CURR_IMG(state)) == BOOT_SWAP_TYPE_REVERT) {
1059-
const struct flash_area *fap_pri;
1060-
1061-
rc = flash_area_open(flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
1062-
BOOT_PRIMARY_SLOT),
1063-
&fap_pri);
1042+
const struct flash_area *fap_pri = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
10641043

1065-
if (rc == 0) {
1066-
rc = swap_erase_trailer_sectors(state, fap_pri);
1067-
flash_area_close(fap_pri);
1044+
assert(fap_pri != NULL);
10681045

1069-
if (rc == 0) {
1070-
BOOT_LOG_INF("Cleared image %d primary slot trailer due to stuck revert",
1071-
BOOT_CURR_IMG(state));
1072-
}
1046+
if (swap_erase_trailer_sectors(state, fap_pri) == 0) {
1047+
BOOT_LOG_INF("Cleared image %d primary slot trailer due to stuck revert",
1048+
BOOT_CURR_IMG(state));
10731049
}
10741050
}
10751051
#endif
@@ -1103,6 +1079,8 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11031079

11041080
#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
11051081
if (slot != BOOT_PRIMARY_SLOT) {
1082+
int rc;
1083+
11061084
/* Check if version of secondary slot is sufficient */
11071085
rc = boot_version_cmp(
11081086
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
@@ -1151,14 +1129,13 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11511129
* overwriting an application written to the incorrect slot.
11521130
* This feature is only supported by ARM platforms.
11531131
*/
1154-
if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
1132+
if (fap == BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT)) {
11551133
const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
11561134
struct image_header *secondary_hdr = boot_img_hdr(state, slot);
11571135
uint32_t reset_value = 0;
11581136
uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
11591137

1160-
rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
1161-
if (rc != 0) {
1138+
if (flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)) != 0) {
11621139
fih_rc = FIH_NO_BOOTABLE_IMAGE;
11631140
goto out;
11641141
}
@@ -1181,8 +1158,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11811158
#endif
11821159

11831160
out:
1184-
flash_area_close(fap);
1185-
11861161
FIH_RET(fih_rc);
11871162
}
11881163

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

1211-
rc = flash_area_open(flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot),
1212-
&fap);
1213-
if (rc != 0) {
1214-
rc = BOOT_EFLASH;
1215-
goto done;
1216-
}
1186+
fap = BOOT_IMG_AREA(state, slot);
1187+
assert(fap != NULL);
12171188

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

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

1735-
rc = boot_find_status(image_index, &fap);
1705+
fap = boot_find_status(state, image_index);
17361706
assert(fap != NULL);
17371707
rc = boot_read_swap_size(fap, &bs->swap_size);
17381708
assert(rc == 0);
@@ -2764,17 +2734,15 @@ print_loaded_images(struct boot_loader_state *state)
27642734
static int
27652735
boot_select_or_erase(struct boot_loader_state *state)
27662736
{
2767-
const struct flash_area *fap;
2768-
int fa_id;
2737+
const struct flash_area *fap = NULL;
27692738
int rc;
27702739
uint32_t active_slot;
27712740
struct boot_swap_state* active_swap_state;
27722741

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

2775-
fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
2776-
rc = flash_area_open(fa_id, &fap);
2777-
assert(rc == 0);
2744+
fap = BOOT_IMG_AREA(state, active_slot);
2745+
assert(fap != NULL);
27782746

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

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

2798-
flash_area_close(fap);
27992766
rc = -1;
28002767
} else {
28012768
if (active_swap_state->copy_done != BOOT_FLAG_SET) {
@@ -2817,7 +2784,6 @@ boot_select_or_erase(struct boot_loader_state *state)
28172784
rc = 0;
28182785
}
28192786
}
2820-
flash_area_close(fap);
28212787
}
28222788

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

boot/bootutil/src/ram_load.c

+7-28
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,11 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
135135
uint32_t max_sz = 1024;
136136
uint16_t idx;
137137
uint8_t * cur_dst;
138-
int area_id;
139138
int rc;
140139
uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
141140

142-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
143-
rc = flash_area_open(area_id, &fap_src);
144-
if (rc != 0){
145-
return BOOT_EFLASH;
146-
}
141+
fap_src = BOOT_IMG_AREA(state, slot);
142+
assert(fap_src != NULL);
147143

148144
tlv_off = BOOT_TLV_OFF(hdr);
149145

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

190186
done:
191-
flash_area_close(fap_src);
192-
193187
return rc;
194188
}
195189

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

216209
#if (BOOT_IMAGE_NUMBER == 1)
217210
(void)state;
218211
#endif
219212

220-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
221-
222-
rc = flash_area_open(area_id, &fap_src);
223-
if (rc != 0) {
224-
return BOOT_EFLASH;
225-
}
213+
fap_src = BOOT_IMG_AREA(state, slot);
214+
assert(fap_src != NULL);
226215

227216
/* Direct copy from flash to its new location in SRAM. */
228217
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,
231220
BOOT_CURR_IMG(state), rc);
232221
}
233222

234-
flash_area_close(fap_src);
235-
236223
return rc;
237224
}
238225

@@ -421,22 +408,14 @@ boot_remove_image_from_sram(struct boot_loader_state *state)
421408
int
422409
boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
423410
{
424-
int area_id;
425-
int rc;
426411
const struct flash_area *fap;
427412

428-
(void)state;
429-
430413
BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
431414
slot);
432-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
433-
rc = flash_area_open(area_id, &fap);
434-
if (rc == 0) {
435-
flash_area_erase(fap, 0, flash_area_get_size(fap));
436-
flash_area_close(fap);
437-
}
415+
fap = BOOT_IMG_AREA(state, slot);
416+
assert(fap != NULL);
438417

439-
return rc;
418+
return flash_area_erase(fap, 0, flash_area_get_size(fap));
440419
}
441420

442421
int boot_load_image_from_flash_to_sram(struct boot_loader_state *state,

0 commit comments

Comments
 (0)