Skip to content

Commit 9dfeac3

Browse files
committed
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 <dominik.ermel@nordicsemi.no>
1 parent 92e0e1e commit 9dfeac3

File tree

13 files changed

+171
-155
lines changed

13 files changed

+171
-155
lines changed

boot/bootutil/include/bootutil/bootutil.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ struct image_trailer {
7777
uint8_t magic[BOOT_MAGIC_SZ];
7878
};
7979

80+
#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
81+
#define FLASH_AREA_IMAGES 1
82+
#elif MCUBOOT_IMAGE_NUMBER == 1
83+
#define FLASH_AREA_IMAGES 2
84+
#elif MCUBOOT_IMAGE_NUMBER == 2
85+
#define FLASH_AREA_IMAGES 4
86+
#endif
87+
88+
#if !defined(MCUBOOT_SWAP_USING_SCRATCH)
89+
#define FLASH_AREA_OBJECTS FLASH_AREA_IMAGES
90+
#else
91+
#define FLASH_AREA_OBJECTS (FLASH_AREA_IMAGES + 1)
92+
#define SCRATCH_FA flash_area_objects[FLASH_AREA_OBJECTS - 1]
93+
#endif
94+
95+
extern const struct flash_area *flash_area_objects[FLASH_AREA_OBJECTS];
96+
97+
#define PRIMARY_IMAGE_FA(x) flash_area_objects[(x) + 0]
98+
#define SECONDARY_IMAGE_FA(x) flash_area_objects[(x) + 1]
99+
80100
/* you must have pre-allocated all the entries within this structure */
81101
fih_int boot_go(struct boot_rsp *rsp);
82102
fih_int boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id);

boot/bootutil/src/bootutil_priv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ struct flash_area;
5656

5757
#if (defined(MCUBOOT_OVERWRITE_ONLY) + \
5858
defined(MCUBOOT_SWAP_USING_MOVE) + \
59+
defined(MCUBOOT_SWAP_USING_SCRATCH) + \
5960
defined(MCUBOOT_DIRECT_XIP) + \
6061
defined(MCUBOOT_RAM_LOAD)) > 1
61-
#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD"
62+
#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_SWAP_USING_SCRATCH, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD"
6263
#endif
6364

6465
#if !defined(MCUBOOT_OVERWRITE_ONLY) && \

boot/bootutil/src/bootutil_public.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,15 @@ boot_swap_type_multi(int image_index)
432432
BOOT_HOOK_REGULAR, image_index, &primary_slot);
433433
if (rc == BOOT_HOOK_REGULAR)
434434
{
435-
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
436-
&primary_slot);
435+
rc = boot_read_swap_state(PRIMARY_IMAGE_FA(image_index),
436+
&primary_slot);
437437
}
438438
if (rc) {
439439
return BOOT_SWAP_TYPE_PANIC;
440440
}
441441

442-
rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
443-
&secondary_slot);
442+
rc = boot_read_swap_state(SECONDARY_IMAGE_FA(image_index),
443+
&secondary_slot);
444444
if (rc == BOOT_EFLASH) {
445445
BOOT_LOG_INF("Secondary image of image pair (%d.) "
446446
"is unreachable. Treat it as empty", image_index);

boot/bootutil/src/encrypted.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ boot_enc_load(struct enc_key_data *enc_state, int image_index,
698698
uint8_t slot;
699699
int rc;
700700

701-
rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap));
701+
rc = flash_area_multi_image_slot(image_index, fap);
702702
if (rc < 0) {
703703
return rc;
704704
}
@@ -745,7 +745,7 @@ boot_enc_valid(struct enc_key_data *enc_state, int image_index,
745745
{
746746
int rc;
747747

748-
rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap));
748+
rc = flash_area_to_multi_image_slot(image_index, fap);
749749
if (rc < 0) {
750750
/* can't get proper slot number - skip encryption, */
751751
/* postpone the error for a upper layer */
@@ -777,7 +777,7 @@ boot_encrypt(struct enc_key_data *enc_state, int image_index,
777777
nonce[14] = (uint8_t)(off >> 8);
778778
nonce[15] = (uint8_t)off;
779779

780-
rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fap));
780+
rc = flash_area_to_multi_image_slot(image_index, fap);
781781
if (rc < 0) {
782782
assert(0);
783783
return;

boot/bootutil/src/loader.c

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <inttypes.h>
3636
#include <stdlib.h>
3737
#include <string.h>
38+
#include "mcuboot_config/mcuboot_config.h"
3839
#include "bootutil/bootutil.h"
3940
#include "bootutil/bootutil_public.h"
4041
#include "bootutil/image.h"
@@ -56,7 +57,6 @@
5657
#include <os/os_malloc.h>
5758
#endif
5859

59-
#include "mcuboot_config/mcuboot_config.h"
6060

6161
BOOT_LOG_MODULE_DECLARE(mcuboot);
6262

@@ -240,12 +240,7 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
240240
(void)state;
241241
#endif
242242

243-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
244-
rc = flash_area_open(area_id, &fap);
245-
if (rc != 0) {
246-
rc = BOOT_EFLASH;
247-
goto done;
248-
}
243+
fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
249244

250245
off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
251246

@@ -309,7 +304,7 @@ boot_write_sz(struct boot_loader_state *state)
309304
}
310305

311306
static int
312-
boot_initialize_area(struct boot_loader_state *state, int flash_area)
307+
boot_initialize_area(struct boot_loader_state *state, const struct flash_area *fa)
313308
{
314309
uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
315310
boot_sector_t *out_sectors;
@@ -318,14 +313,14 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
318313

319314
num_sectors = BOOT_MAX_IMG_SECTORS;
320315

321-
if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
316+
if (fa == PRIMARY_IMAGE_FA(BOOT_CURR_IMG(state))) {
322317
out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
323318
out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
324-
} else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
319+
} else if (fa == SECONDARY_IMAGE_FA(BOOT_CURR_IMG(state))) {
325320
out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
326321
out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
327322
#if MCUBOOT_SWAP_USING_SCRATCH
328-
} else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
323+
} else if (fa == SCRATCH_FA) {
329324
out_sectors = state->scratch.sectors;
330325
out_num_sectors = &state->scratch.num_sectors;
331326
#endif
@@ -334,8 +329,12 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
334329
}
335330

336331
#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
337-
rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
332+
rc = flash_area_get_sectors_fa(fa, &num_sectors, out_sectors);
338333
#else
334+
/* TODO: This is only used by mynewt; note that flash_area_to_sectors
335+
* actually does flash_area_open/flash_area_close pair but this safe for now,
336+
* as these function do no locking or reference counting. */
337+
int flash_area = flash_area_get_id(fa);
339338
_Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
340339
rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
341340
#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
@@ -360,12 +359,12 @@ boot_read_sectors(struct boot_loader_state *state)
360359

361360
image_index = BOOT_CURR_IMG(state);
362361

363-
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
362+
rc = boot_initialize_area(state, PRIMARY_IMAGE_FA(image_index));
364363
if (rc != 0) {
365364
return BOOT_EFLASH;
366365
}
367366

368-
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
367+
rc = boot_initialize_area(state, SECONDARY_IMAGE_FA(image_index));
369368
if (rc != 0) {
370369
/* We need to differentiate from the primary image issue */
371370
return BOOT_EFLASH_SEC;
@@ -439,20 +438,15 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
439438
#if MCUBOOT_SWAP_USING_SCRATCH
440439
if (bs->use_scratch) {
441440
/* Write to scratch. */
442-
area_id = FLASH_AREA_IMAGE_SCRATCH;
441+
fap = SCRATCH_FA;
443442
} else {
444443
#endif
445444
/* Write to the primary slot. */
446-
area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
445+
fap = PRIMARY_IMAGE_FA(BOOT_CURR_IMG(state));
447446
#if MCUBOOT_SWAP_USING_SCRATCH
448447
}
449448
#endif
450449

451-
rc = flash_area_open(area_id, &fap);
452-
if (rc != 0) {
453-
return BOOT_EFLASH;
454-
}
455-
456450
off = boot_status_off(fap) +
457451
boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
458452
align = flash_area_align(fap);
@@ -465,8 +459,6 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
465459
rc = BOOT_EFLASH;
466460
}
467461

468-
flash_area_close(fap);
469-
470462
return rc;
471463
}
472464
#endif /* !MCUBOOT_RAM_LOAD */
@@ -596,14 +588,9 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
596588
int area_id;
597589
int rc;
598590

599-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
600-
rc = flash_area_open(area_id, &fap);
601-
if (rc != 0) {
602-
return -1;
603-
}
591+
fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
604592

605593
erased_val = flash_area_erased_val(fap);
606-
flash_area_close(fap);
607594

608595
hdr = boot_img_hdr(state, slot);
609596
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,
712699
fih_int fih_rc = FIH_FAILURE;
713700
int rc;
714701

715-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
716-
rc = flash_area_open(area_id, &fap);
717-
if (rc != 0) {
718-
FIH_RET(fih_rc);
719-
}
702+
fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
720703

721704
hdr = boot_img_hdr(state, slot);
722705
if (boot_check_header_erased(state, slot) == 0 ||
@@ -817,8 +800,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
817800
#endif
818801

819802
out:
820-
flash_area_close(fap);
821-
822803
FIH_RET(fih_rc);
823804
}
824805

@@ -844,12 +825,7 @@ boot_update_security_counter(uint8_t image_index, int slot,
844825
uint32_t img_security_cnt;
845826
int rc;
846827

847-
rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
848-
&fap);
849-
if (rc != 0) {
850-
rc = BOOT_EFLASH;
851-
goto done;
852-
}
828+
fap = flash_area_from_multi_image_slot(image_index, slot);
853829

854830
rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
855831
if (rc != 0) {
@@ -862,7 +838,6 @@ boot_update_security_counter(uint8_t image_index, int slot,
862838
}
863839

864840
done:
865-
flash_area_close(fap);
866841
return rc;
867842
}
868843
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
@@ -1410,12 +1385,7 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
14101385
int area_id;
14111386
int rc;
14121387

1413-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1414-
rc = flash_area_open(area_id, &fap);
1415-
if (rc != 0) {
1416-
rc = BOOT_EFLASH;
1417-
goto done;
1418-
}
1388+
fap = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
14191389

14201390
rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
14211391
IMAGE_TLV_DEPENDENCY, true);
@@ -1457,7 +1427,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
14571427
}
14581428

14591429
done:
1460-
flash_area_close(fap);
14611430
return rc;
14621431
}
14631432

@@ -1966,7 +1935,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
19661935
struct boot_status bs;
19671936
int rc = -1;
19681937
fih_int fih_rc = FIH_FAILURE;
1969-
int fa_id;
19701938
int image_index;
19711939
bool has_upgrade;
19721940

@@ -2019,14 +1987,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
20191987
* of this call.
20201988
*/
20211989
for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2022-
fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2023-
rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2024-
assert(rc == 0);
1990+
BOOT_IMG_AREA(state, slot) = flash_area_from_multi_image_slot(image_index, slot);
20251991
}
20261992
#if MCUBOOT_SWAP_USING_SCRATCH
2027-
rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
2028-
&BOOT_SCRATCH_AREA(state));
2029-
assert(rc == 0);
1993+
BOOT_SCRATCH_AREA(state) = SCRATCH_FA;
20301994
#endif
20311995

20321996
/* 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)
22132177
{
22142178
boot_sector_t *sectors;
22152179
uintptr_t entry_val;
2216-
int loader_flash_id;
2217-
int split_flash_id;
22182180
int rc;
22192181
fih_int fih_rc = FIH_FAILURE;
22202182

@@ -2225,14 +2187,8 @@ split_go(int loader_slot, int split_slot, void **entry)
22252187
BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
22262188
BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
22272189

2228-
loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2229-
rc = flash_area_open(loader_flash_id,
2230-
&BOOT_IMG_AREA(&boot_data, loader_slot));
2231-
assert(rc == 0);
2232-
split_flash_id = flash_area_id_from_image_slot(split_slot);
2233-
rc = flash_area_open(split_flash_id,
2234-
&BOOT_IMG_AREA(&boot_data, split_slot));
2235-
assert(rc == 0);
2190+
BOOT_IMG_AREA(&boot_data, loader_slot) = flash_area_from_image_slot(loader_slot);
2191+
BOOT_IMG_AREA(&boot_data, split_slot) = flash_area_from_image_slot(split_slot);
22362192

22372193
/* Determine the sector layout of the image slots and scratch area. */
22382194
rc = boot_read_sectors(&boot_data);
@@ -2577,11 +2533,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
25772533
uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
25782534

25792535
image_index = BOOT_CURR_IMG(state);
2580-
area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2581-
rc = flash_area_open(area_id, &fap_src);
2582-
if (rc != 0){
2583-
return BOOT_EFLASH;
2584-
}
2536+
fap_src = flash_area_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
25852537

25862538
tlv_off = BOOT_TLV_OFF(hdr);
25872539

@@ -2633,8 +2585,6 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
26332585
rc = 0;
26342586

26352587
done:
2636-
flash_area_close(fap_src);
2637-
26382588
return rc;
26392589
}
26402590

0 commit comments

Comments
 (0)