35
35
#include <inttypes.h>
36
36
#include <stdlib.h>
37
37
#include <string.h>
38
+ #include "mcuboot_config/mcuboot_config.h"
38
39
#include "bootutil/bootutil.h"
39
40
#include "bootutil/bootutil_public.h"
40
41
#include "bootutil/image.h"
56
57
#include <os/os_malloc.h>
57
58
#endif
58
59
59
- #include "mcuboot_config/mcuboot_config.h"
60
60
61
61
BOOT_LOG_MODULE_DECLARE (mcuboot );
62
62
@@ -240,12 +240,7 @@ boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
240
240
(void )state ;
241
241
#endif
242
242
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 );
249
244
250
245
off = BOOT_TLV_OFF (boot_img_hdr (state , slot ));
251
246
@@ -309,7 +304,7 @@ boot_write_sz(struct boot_loader_state *state)
309
304
}
310
305
311
306
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 )
313
308
{
314
309
uint32_t num_sectors = BOOT_MAX_IMG_SECTORS ;
315
310
boot_sector_t * out_sectors ;
@@ -318,14 +313,14 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
318
313
319
314
num_sectors = BOOT_MAX_IMG_SECTORS ;
320
315
321
- if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
316
+ if (fa == PRIMARY_IMAGE_FA (BOOT_CURR_IMG (state ))) {
322
317
out_sectors = BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors ;
323
318
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 ))) {
325
320
out_sectors = BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors ;
326
321
out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
327
322
#if MCUBOOT_SWAP_USING_SCRATCH
328
- } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
323
+ } else if (fa == SCRATCH_FA ) {
329
324
out_sectors = state -> scratch .sectors ;
330
325
out_num_sectors = & state -> scratch .num_sectors ;
331
326
#endif
@@ -334,8 +329,12 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
334
329
}
335
330
336
331
#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 );
338
333
#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 );
339
338
_Static_assert (sizeof (int ) <= sizeof (uint32_t ), "Fix needed" );
340
339
rc = flash_area_to_sectors (flash_area , (int * )& num_sectors , out_sectors );
341
340
#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
@@ -360,12 +359,12 @@ boot_read_sectors(struct boot_loader_state *state)
360
359
361
360
image_index = BOOT_CURR_IMG (state );
362
361
363
- rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
362
+ rc = boot_initialize_area (state , PRIMARY_IMAGE_FA (image_index ));
364
363
if (rc != 0 ) {
365
364
return BOOT_EFLASH ;
366
365
}
367
366
368
- rc = boot_initialize_area (state , FLASH_AREA_IMAGE_SECONDARY (image_index ));
367
+ rc = boot_initialize_area (state , SECONDARY_IMAGE_FA (image_index ));
369
368
if (rc != 0 ) {
370
369
/* We need to differentiate from the primary image issue */
371
370
return BOOT_EFLASH_SEC ;
@@ -439,20 +438,15 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
439
438
#if MCUBOOT_SWAP_USING_SCRATCH
440
439
if (bs -> use_scratch ) {
441
440
/* Write to scratch. */
442
- area_id = FLASH_AREA_IMAGE_SCRATCH ;
441
+ fap = SCRATCH_FA ;
443
442
} else {
444
443
#endif
445
444
/* 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 ));
447
446
#if MCUBOOT_SWAP_USING_SCRATCH
448
447
}
449
448
#endif
450
449
451
- rc = flash_area_open (area_id , & fap );
452
- if (rc != 0 ) {
453
- return BOOT_EFLASH ;
454
- }
455
-
456
450
off = boot_status_off (fap ) +
457
451
boot_status_internal_off (bs , BOOT_WRITE_SZ (state ));
458
452
align = flash_area_align (fap );
@@ -465,8 +459,6 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
465
459
rc = BOOT_EFLASH ;
466
460
}
467
461
468
- flash_area_close (fap );
469
-
470
462
return rc ;
471
463
}
472
464
#endif /* !MCUBOOT_RAM_LOAD */
@@ -596,14 +588,9 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
596
588
int area_id ;
597
589
int rc ;
598
590
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 );
604
592
605
593
erased_val = flash_area_erased_val (fap );
606
- flash_area_close (fap );
607
594
608
595
hdr = boot_img_hdr (state , slot );
609
596
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,
712
699
fih_int fih_rc = FIH_FAILURE ;
713
700
int rc ;
714
701
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 );
720
703
721
704
hdr = boot_img_hdr (state , slot );
722
705
if (boot_check_header_erased (state , slot ) == 0 ||
@@ -817,8 +800,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
817
800
#endif
818
801
819
802
out :
820
- flash_area_close (fap );
821
-
822
803
FIH_RET (fih_rc );
823
804
}
824
805
@@ -844,12 +825,7 @@ boot_update_security_counter(uint8_t image_index, int slot,
844
825
uint32_t img_security_cnt ;
845
826
int rc ;
846
827
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 );
853
829
854
830
rc = bootutil_get_img_security_cnt (hdr , fap , & img_security_cnt );
855
831
if (rc != 0 ) {
@@ -862,7 +838,6 @@ boot_update_security_counter(uint8_t image_index, int slot,
862
838
}
863
839
864
840
done :
865
- flash_area_close (fap );
866
841
return rc ;
867
842
}
868
843
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
@@ -1410,12 +1385,7 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
1410
1385
int area_id ;
1411
1386
int rc ;
1412
1387
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 );
1419
1389
1420
1390
rc = bootutil_tlv_iter_begin (& it , boot_img_hdr (state , slot ), fap ,
1421
1391
IMAGE_TLV_DEPENDENCY , true);
@@ -1457,7 +1427,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
1457
1427
}
1458
1428
1459
1429
done :
1460
- flash_area_close (fap );
1461
1430
return rc ;
1462
1431
}
1463
1432
@@ -1966,7 +1935,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
1966
1935
struct boot_status bs ;
1967
1936
int rc = -1 ;
1968
1937
fih_int fih_rc = FIH_FAILURE ;
1969
- int fa_id ;
1970
1938
int image_index ;
1971
1939
bool has_upgrade ;
1972
1940
@@ -2019,14 +1987,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2019
1987
* of this call.
2020
1988
*/
2021
1989
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 );
2025
1991
}
2026
1992
#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 ;
2030
1994
#endif
2031
1995
2032
1996
/* 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)
2213
2177
{
2214
2178
boot_sector_t * sectors ;
2215
2179
uintptr_t entry_val ;
2216
- int loader_flash_id ;
2217
- int split_flash_id ;
2218
2180
int rc ;
2219
2181
fih_int fih_rc = FIH_FAILURE ;
2220
2182
@@ -2225,14 +2187,8 @@ split_go(int loader_slot, int split_slot, void **entry)
2225
2187
BOOT_IMG (& boot_data , loader_slot ).sectors = sectors + 0 ;
2226
2188
BOOT_IMG (& boot_data , split_slot ).sectors = sectors + BOOT_MAX_IMG_SECTORS ;
2227
2189
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 );
2236
2192
2237
2193
/* Determine the sector layout of the image slots and scratch area. */
2238
2194
rc = boot_read_sectors (& boot_data );
@@ -2577,11 +2533,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2577
2533
uint8_t * ram_dst = (void * )(IMAGE_RAM_BASE + img_dst );
2578
2534
2579
2535
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 );
2585
2537
2586
2538
tlv_off = BOOT_TLV_OFF (hdr );
2587
2539
@@ -2633,8 +2585,6 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2633
2585
rc = 0 ;
2634
2586
2635
2587
done :
2636
- flash_area_close (fap_src );
2637
-
2638
2588
return rc ;
2639
2589
}
2640
2590
0 commit comments