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"
5657#include <os/os_malloc.h>
5758#endif
5859
59- #include "mcuboot_config/mcuboot_config.h"
6060
6161BOOT_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
311306static 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 ;
@@ -596,14 +595,9 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
596595 int area_id ;
597596 int rc ;
598597
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- }
598+ fap = flash_area_from_multi_image_slot (BOOT_CURR_IMG (state ), slot );
604599
605600 erased_val = flash_area_erased_val (fap );
606- flash_area_close (fap );
607601
608602 hdr = boot_img_hdr (state , slot );
609603 if (!boot_data_is_set_to (erased_val , & hdr -> ih_magic , sizeof (hdr -> ih_magic ))) {
@@ -712,11 +706,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
712706 fih_int fih_rc = FIH_FAILURE ;
713707 int rc ;
714708
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- }
709+ fap = flash_area_from_multi_image_slot (BOOT_CURR_IMG (state ), slot );
720710
721711 hdr = boot_img_hdr (state , slot );
722712 if (boot_check_header_erased (state , slot ) == 0 ||
@@ -817,8 +807,6 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
817807#endif
818808
819809out :
820- flash_area_close (fap );
821-
822810 FIH_RET (fih_rc );
823811}
824812
@@ -844,12 +832,7 @@ boot_update_security_counter(uint8_t image_index, int slot,
844832 uint32_t img_security_cnt ;
845833 int rc ;
846834
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- }
835+ fap = flash_area_from_multi_image_slot (image_index , slot );
853836
854837 rc = bootutil_get_img_security_cnt (hdr , fap , & img_security_cnt );
855838 if (rc != 0 ) {
@@ -862,7 +845,6 @@ boot_update_security_counter(uint8_t image_index, int slot,
862845 }
863846
864847done :
865- flash_area_close (fap );
866848 return rc ;
867849}
868850#endif /* MCUBOOT_HW_ROLLBACK_PROT */
@@ -1410,12 +1392,7 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
14101392 int area_id ;
14111393 int rc ;
14121394
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- }
1395+ fap = flash_area_from_multi_image_slot (BOOT_CURR_IMG (state ), slot );
14191396
14201397 rc = bootutil_tlv_iter_begin (& it , boot_img_hdr (state , slot ), fap ,
14211398 IMAGE_TLV_DEPENDENCY , true);
@@ -1457,7 +1434,6 @@ boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
14571434 }
14581435
14591436done :
1460- flash_area_close (fap );
14611437 return rc ;
14621438}
14631439
@@ -1966,7 +1942,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
19661942 struct boot_status bs ;
19671943 int rc = -1 ;
19681944 fih_int fih_rc = FIH_FAILURE ;
1969- int fa_id ;
19701945 int image_index ;
19711946 bool has_upgrade ;
19721947
@@ -2019,14 +1994,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
20191994 * of this call.
20201995 */
20211996 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 );
1997+ BOOT_IMG_AREA (state , slot ) = flash_area_from_multi_image_slot (image_index , slot );
20251998 }
20261999#if MCUBOOT_SWAP_USING_SCRATCH
2027- rc = flash_area_open (FLASH_AREA_IMAGE_SCRATCH ,
2028- & BOOT_SCRATCH_AREA (state ));
2029- assert (rc == 0 );
2000+ BOOT_SCRATCH_AREA (state ) = SCRATCH_FA ;
20302001#endif
20312002
20322003 /* Determine swap type and complete swap if it has been aborted. */
@@ -2213,8 +2184,6 @@ split_go(int loader_slot, int split_slot, void **entry)
22132184{
22142185 boot_sector_t * sectors ;
22152186 uintptr_t entry_val ;
2216- int loader_flash_id ;
2217- int split_flash_id ;
22182187 int rc ;
22192188 fih_int fih_rc = FIH_FAILURE ;
22202189
@@ -2225,14 +2194,8 @@ split_go(int loader_slot, int split_slot, void **entry)
22252194 BOOT_IMG (& boot_data , loader_slot ).sectors = sectors + 0 ;
22262195 BOOT_IMG (& boot_data , split_slot ).sectors = sectors + BOOT_MAX_IMG_SECTORS ;
22272196
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 );
2197+ BOOT_IMG_AREA (& boot_data , loader_slot ) = flash_area_from_image_slot (loader_slot );
2198+ BOOT_IMG_AREA (& boot_data , split_slot ) = flash_area_from_image_slot (split_slot );
22362199
22372200 /* Determine the sector layout of the image slots and scratch area. */
22382201 rc = boot_read_sectors (& boot_data );
@@ -2577,11 +2540,7 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
25772540 uint8_t * ram_dst = (void * )(IMAGE_RAM_BASE + img_dst );
25782541
25792542 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- }
2543+ fap_src = flash_area_from_multi_image_slot (BOOT_CURR_IMG (state ), slot );
25852544
25862545 tlv_off = BOOT_TLV_OFF (hdr );
25872546
@@ -2633,8 +2592,6 @@ boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
26332592 rc = 0 ;
26342593
26352594done :
2636- flash_area_close (fap_src );
2637-
26382595 return rc ;
26392596}
26402597
0 commit comments