@@ -237,31 +237,6 @@ fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
237237 rsp -> br_hdr = boot_img_hdr (state , active_slot );
238238}
239239
240- /**
241- * Closes all flash areas.
242- *
243- * @param state Boot loader status information.
244- */
245- static void
246- close_all_flash_areas (struct boot_loader_state * state )
247- {
248- uint32_t slot ;
249-
250- IMAGES_ITER (BOOT_CURR_IMG (state )) {
251- #if BOOT_IMAGE_NUMBER > 1
252- if (state -> img_mask [BOOT_CURR_IMG (state )]) {
253- continue ;
254- }
255- #endif
256- #if MCUBOOT_SWAP_USING_SCRATCH
257- flash_area_close (BOOT_SCRATCH_AREA (state ));
258- #endif
259- for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
260- flash_area_close (BOOT_IMG_AREA (state , BOOT_NUM_SLOTS - 1 - slot ));
261- }
262- }
263- }
264-
265240#if (BOOT_IMAGE_NUMBER > 1 ) || \
266241 defined(MCUBOOT_DIRECT_XIP ) || \
267242 defined(MCUBOOT_RAM_LOAD ) || \
@@ -2365,11 +2340,9 @@ check_downgrade_prevention(struct boot_loader_state *state)
23652340fih_ret
23662341context_boot_go (struct boot_loader_state * state , struct boot_rsp * rsp )
23672342{
2368- size_t slot ;
23692343 struct boot_status bs ;
23702344 int rc = -1 ;
23712345 FIH_DECLARE (fih_rc , FIH_FAILURE );
2372- int fa_id ;
23732346 int image_index ;
23742347 bool has_upgrade ;
23752348 volatile int fih_cnt ;
@@ -2395,6 +2368,15 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
23952368 (void )has_upgrade ;
23962369#endif
23972370
2371+ /* Open primary and secondary image areas for the duration
2372+ * of this call.
2373+ */
2374+ rc = boot_open_all_flash_areas (state );
2375+ if (rc != 0 ) {
2376+ BOOT_LOG_ERR ("Failed to open flash areas, cannot continue" );
2377+ FIH_PANIC ;
2378+ }
2379+
23982380 /* Iterate over all the images. By the end of the loop the swap type has
23992381 * to be determined for each image and all aborted swaps have to be
24002382 * completed.
@@ -2432,32 +2414,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
24322414 state -> scratch .sectors = scratch_sectors ;
24332415#endif
24342416#endif
2435-
2436- /* Open primary and secondary image areas for the duration
2437- * of this call.
2438- */
2439- for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
2440- fa_id = flash_area_id_from_multi_image_slot (image_index , slot );
2441- rc = flash_area_open (fa_id , & BOOT_IMG_AREA (state , slot ));
2442- assert (rc == 0 );
2443-
2444- if (rc != 0 ) {
2445- BOOT_LOG_ERR ("Failed to open flash area ID %d (image %d slot %d): %d, "
2446- "cannot continue" , fa_id , image_index , (int8_t )slot , rc );
2447- FIH_PANIC ;
2448- }
2449- }
2450- #if MCUBOOT_SWAP_USING_SCRATCH
2451- rc = flash_area_open (FLASH_AREA_IMAGE_SCRATCH ,
2452- & BOOT_SCRATCH_AREA (state ));
2453- assert (rc == 0 );
2454-
2455- if (rc != 0 ) {
2456- BOOT_LOG_ERR ("Failed to open scratch flash area: %d, cannot continue" , rc );
2457- FIH_PANIC ;
2458- }
2459- #endif
2460-
24612417 /* Determine swap type and complete swap if it has been aborted. */
24622418 boot_prepare_image_for_update (state , & bs );
24632419
@@ -2666,7 +2622,7 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
26662622 memset (& bs , 0 , sizeof (struct boot_status ));
26672623#endif
26682624
2669- close_all_flash_areas (state );
2625+ boot_close_all_flash_areas (state );
26702626 FIH_RET (fih_rc );
26712627}
26722628
@@ -2751,7 +2707,6 @@ static int
27512707boot_get_slot_usage (struct boot_loader_state * state )
27522708{
27532709 uint32_t slot ;
2754- int fa_id ;
27552710 int rc ;
27562711 struct image_header * hdr = NULL ;
27572712
@@ -2761,14 +2716,6 @@ boot_get_slot_usage(struct boot_loader_state *state)
27612716 continue ;
27622717 }
27632718#endif
2764- /* Open all the slots */
2765- for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
2766- fa_id = flash_area_id_from_multi_image_slot (
2767- BOOT_CURR_IMG (state ), slot );
2768- rc = flash_area_open (fa_id , & BOOT_IMG_AREA (state , slot ));
2769- assert (rc == 0 );
2770- }
2771-
27722719 /* Attempt to read an image header from each slot. */
27732720 rc = boot_read_image_headers (state , false, NULL );
27742721 if (rc != 0 ) {
@@ -3079,18 +3026,23 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
30793026 int rc ;
30803027 FIH_DECLARE (fih_rc , FIH_FAILURE );
30813028
3082- rc = boot_get_slot_usage (state );
3029+ rc = boot_open_all_flash_areas (state );
30833030 if (rc != 0 ) {
30843031 goto out ;
30853032 }
30863033
3034+ rc = boot_get_slot_usage (state );
3035+ if (rc != 0 ) {
3036+ goto close ;
3037+ }
3038+
30873039#if (BOOT_IMAGE_NUMBER > 1 )
30883040 while (true) {
30893041#endif
30903042 FIH_CALL (boot_load_and_validate_images , fih_rc , state );
30913043 if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
30923044 FIH_SET (fih_rc , FIH_FAILURE );
3093- goto out ;
3045+ goto close ;
30943046 }
30953047
30963048#if (BOOT_IMAGE_NUMBER > 1 )
@@ -3116,13 +3068,13 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
31163068 rc = boot_update_hw_rollback_protection (state );
31173069 if (rc != 0 ) {
31183070 FIH_SET (fih_rc , FIH_FAILURE );
3119- goto out ;
3071+ goto close ;
31203072 }
31213073
31223074 rc = boot_add_shared_data (state , (uint8_t )state -> slot_usage [BOOT_CURR_IMG (state )].active_slot );
31233075 if (rc != 0 ) {
31243076 FIH_SET (fih_rc , FIH_FAILURE );
3125- goto out ;
3077+ goto close ;
31263078 }
31273079 }
31283080
@@ -3133,9 +3085,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
31333085
31343086 fill_rsp (state , rsp );
31353087
3136- out :
3137- close_all_flash_areas (state );
3088+ close :
3089+ boot_close_all_flash_areas (state );
31383090
3091+ out :
31393092 if (rc != 0 ) {
31403093 FIH_SET (fih_rc , FIH_FAILURE );
31413094 }
@@ -3209,6 +3162,78 @@ void boot_state_clear(struct boot_loader_state *state)
32093162 }
32103163}
32113164
3165+ int
3166+ boot_open_all_flash_areas (struct boot_loader_state * state )
3167+ {
3168+ size_t slot ;
3169+ int rc = 0 ;
3170+ int fa_id ;
3171+ int image_index ;
3172+
3173+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
3174+ #if BOOT_IMAGE_NUMBER > 1
3175+ if (state -> img_mask [BOOT_CURR_IMG (state )]) {
3176+ continue ;
3177+ }
3178+ #endif
3179+ image_index = BOOT_CURR_IMG (state );
3180+
3181+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
3182+ fa_id = flash_area_id_from_multi_image_slot (image_index , slot );
3183+ rc = flash_area_open (fa_id , & BOOT_IMG_AREA (state , slot ));
3184+ assert (rc == 0 );
3185+
3186+ if (rc != 0 ) {
3187+ BOOT_LOG_ERR ("Failed to open flash area ID %d (image %d slot %zu): %d" ,
3188+ fa_id , image_index , slot , rc );
3189+ goto out ;
3190+ }
3191+ }
3192+ }
3193+
3194+ #if MCUBOOT_SWAP_USING_SCRATCH
3195+ rc = flash_area_open (FLASH_AREA_IMAGE_SCRATCH , & BOOT_SCRATCH_AREA (state ));
3196+ assert (rc == 0 );
3197+
3198+ if (rc != 0 ) {
3199+ BOOT_LOG_ERR ("Failed to open scratch flash area: %d" , rc );
3200+ goto out ;
3201+ }
3202+ #endif
3203+
3204+ out :
3205+ if (rc != 0 ) {
3206+ boot_close_all_flash_areas (state );
3207+ }
3208+
3209+ return rc ;
3210+ }
3211+
3212+ void
3213+ boot_close_all_flash_areas (struct boot_loader_state * state )
3214+ {
3215+ uint32_t slot ;
3216+
3217+ #if MCUBOOT_SWAP_USING_SCRATCH
3218+ if (BOOT_SCRATCH_AREA (state ) != NULL ) {
3219+ flash_area_close (BOOT_SCRATCH_AREA (state ));
3220+ }
3221+ #endif
3222+
3223+ IMAGES_ITER (BOOT_CURR_IMG (state )) {
3224+ #if BOOT_IMAGE_NUMBER > 1
3225+ if (state -> img_mask [BOOT_CURR_IMG (state )]) {
3226+ continue ;
3227+ }
3228+ #endif
3229+ for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
3230+ if (BOOT_IMG_AREA (state , BOOT_NUM_SLOTS - 1 - slot ) != NULL ) {
3231+ flash_area_close (BOOT_IMG_AREA (state , BOOT_NUM_SLOTS - 1 - slot ));
3232+ }
3233+ }
3234+ }
3235+ }
3236+
32123237#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
32133238/**
32143239 * Reads image data to find out the maximum application sizes. Only needs to
@@ -3217,11 +3242,15 @@ void boot_state_clear(struct boot_loader_state *state)
32173242 */
32183243static void boot_fetch_slot_state_sizes (void )
32193244{
3220- size_t slot ;
32213245 int rc = -1 ;
3222- int fa_id ;
32233246 int image_index ;
32243247
3248+ rc = boot_open_all_flash_areas (& boot_data );
3249+ if (rc != 0 ) {
3250+ BOOT_LOG_DBG ("boot_fetch_slot_state_sizes: error %d while opening flash areas" , rc );
3251+ goto finish ;
3252+ }
3253+
32253254 IMAGES_ITER (BOOT_CURR_IMG (& boot_data )) {
32263255 int max_size = 0 ;
32273256
@@ -3235,31 +3264,6 @@ static void boot_fetch_slot_state_sizes(void)
32353264 boot_data .scratch .sectors = sector_buffers .scratch ;
32363265#endif
32373266
3238- /* Open primary and secondary image areas for the duration
3239- * of this call.
3240- */
3241- for (slot = 0 ; slot < BOOT_NUM_SLOTS ; slot ++ ) {
3242- fa_id = flash_area_id_from_multi_image_slot (image_index , slot );
3243- rc = flash_area_open (fa_id , & BOOT_IMG_AREA (& boot_data , slot ));
3244- assert (rc == 0 );
3245-
3246- if (rc != 0 ) {
3247- BOOT_LOG_DBG ("boot_fetch_slot_state_sizes: error %d for %d" ,
3248- rc , fa_id );
3249- goto finish ;
3250- }
3251- }
3252-
3253- #if MCUBOOT_SWAP_USING_SCRATCH
3254- rc = flash_area_open (FLASH_AREA_IMAGE_SCRATCH ,
3255- & BOOT_SCRATCH_AREA (& boot_data ));
3256- assert (rc == 0 );
3257-
3258- if (rc != 0 ) {
3259- goto finish ;
3260- }
3261- #endif
3262-
32633267 /* Determine the sector layout of the image slots and scratch area. */
32643268 rc = boot_read_sectors_recovery (& boot_data );
32653269
@@ -3274,7 +3278,7 @@ static void boot_fetch_slot_state_sizes(void)
32743278 }
32753279
32763280finish :
3277- close_all_flash_areas (& boot_data );
3281+ boot_close_all_flash_areas (& boot_data );
32783282 memset (& boot_data , 0x00 , sizeof (boot_data ));
32793283}
32803284#endif
0 commit comments