@@ -237,31 +237,6 @@ fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
237
237
rsp -> br_hdr = boot_img_hdr (state , active_slot );
238
238
}
239
239
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
-
265
240
#if (BOOT_IMAGE_NUMBER > 1 ) || \
266
241
defined(MCUBOOT_DIRECT_XIP ) || \
267
242
defined(MCUBOOT_RAM_LOAD ) || \
@@ -2365,11 +2340,9 @@ check_downgrade_prevention(struct boot_loader_state *state)
2365
2340
fih_ret
2366
2341
context_boot_go (struct boot_loader_state * state , struct boot_rsp * rsp )
2367
2342
{
2368
- size_t slot ;
2369
2343
struct boot_status bs ;
2370
2344
int rc = -1 ;
2371
2345
FIH_DECLARE (fih_rc , FIH_FAILURE );
2372
- int fa_id ;
2373
2346
int image_index ;
2374
2347
bool has_upgrade ;
2375
2348
volatile int fih_cnt ;
@@ -2395,6 +2368,15 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2395
2368
(void )has_upgrade ;
2396
2369
#endif
2397
2370
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
+
2398
2380
/* Iterate over all the images. By the end of the loop the swap type has
2399
2381
* to be determined for each image and all aborted swaps have to be
2400
2382
* completed.
@@ -2432,32 +2414,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2432
2414
state -> scratch .sectors = scratch_sectors ;
2433
2415
#endif
2434
2416
#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
-
2461
2417
/* Determine swap type and complete swap if it has been aborted. */
2462
2418
boot_prepare_image_for_update (state , & bs );
2463
2419
@@ -2666,7 +2622,7 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2666
2622
memset (& bs , 0 , sizeof (struct boot_status ));
2667
2623
#endif
2668
2624
2669
- close_all_flash_areas (state );
2625
+ boot_close_all_flash_areas (state );
2670
2626
FIH_RET (fih_rc );
2671
2627
}
2672
2628
@@ -2751,7 +2707,6 @@ static int
2751
2707
boot_get_slot_usage (struct boot_loader_state * state )
2752
2708
{
2753
2709
uint32_t slot ;
2754
- int fa_id ;
2755
2710
int rc ;
2756
2711
struct image_header * hdr = NULL ;
2757
2712
@@ -2761,14 +2716,6 @@ boot_get_slot_usage(struct boot_loader_state *state)
2761
2716
continue ;
2762
2717
}
2763
2718
#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
-
2772
2719
/* Attempt to read an image header from each slot. */
2773
2720
rc = boot_read_image_headers (state , false, NULL );
2774
2721
if (rc != 0 ) {
@@ -3079,18 +3026,23 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3079
3026
int rc ;
3080
3027
FIH_DECLARE (fih_rc , FIH_FAILURE );
3081
3028
3082
- rc = boot_get_slot_usage (state );
3029
+ rc = boot_open_all_flash_areas (state );
3083
3030
if (rc != 0 ) {
3084
3031
goto out ;
3085
3032
}
3086
3033
3034
+ rc = boot_get_slot_usage (state );
3035
+ if (rc != 0 ) {
3036
+ goto close ;
3037
+ }
3038
+
3087
3039
#if (BOOT_IMAGE_NUMBER > 1 )
3088
3040
while (true) {
3089
3041
#endif
3090
3042
FIH_CALL (boot_load_and_validate_images , fih_rc , state );
3091
3043
if (FIH_NOT_EQ (fih_rc , FIH_SUCCESS )) {
3092
3044
FIH_SET (fih_rc , FIH_FAILURE );
3093
- goto out ;
3045
+ goto close ;
3094
3046
}
3095
3047
3096
3048
#if (BOOT_IMAGE_NUMBER > 1 )
@@ -3116,13 +3068,13 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3116
3068
rc = boot_update_hw_rollback_protection (state );
3117
3069
if (rc != 0 ) {
3118
3070
FIH_SET (fih_rc , FIH_FAILURE );
3119
- goto out ;
3071
+ goto close ;
3120
3072
}
3121
3073
3122
3074
rc = boot_add_shared_data (state , (uint8_t )state -> slot_usage [BOOT_CURR_IMG (state )].active_slot );
3123
3075
if (rc != 0 ) {
3124
3076
FIH_SET (fih_rc , FIH_FAILURE );
3125
- goto out ;
3077
+ goto close ;
3126
3078
}
3127
3079
}
3128
3080
@@ -3133,9 +3085,10 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3133
3085
3134
3086
fill_rsp (state , rsp );
3135
3087
3136
- out :
3137
- close_all_flash_areas (state );
3088
+ close :
3089
+ boot_close_all_flash_areas (state );
3138
3090
3091
+ out :
3139
3092
if (rc != 0 ) {
3140
3093
FIH_SET (fih_rc , FIH_FAILURE );
3141
3094
}
@@ -3209,6 +3162,78 @@ void boot_state_clear(struct boot_loader_state *state)
3209
3162
}
3210
3163
}
3211
3164
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
+
3212
3237
#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
3213
3238
/**
3214
3239
* 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)
3217
3242
*/
3218
3243
static void boot_fetch_slot_state_sizes (void )
3219
3244
{
3220
- size_t slot ;
3221
3245
int rc = -1 ;
3222
- int fa_id ;
3223
3246
int image_index ;
3224
3247
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
+
3225
3254
IMAGES_ITER (BOOT_CURR_IMG (& boot_data )) {
3226
3255
int max_size = 0 ;
3227
3256
@@ -3235,31 +3264,6 @@ static void boot_fetch_slot_state_sizes(void)
3235
3264
boot_data .scratch .sectors = sector_buffers .scratch ;
3236
3265
#endif
3237
3266
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
-
3263
3267
/* Determine the sector layout of the image slots and scratch area. */
3264
3268
rc = boot_read_sectors_recovery (& boot_data );
3265
3269
@@ -3274,7 +3278,7 @@ static void boot_fetch_slot_state_sizes(void)
3274
3278
}
3275
3279
3276
3280
finish :
3277
- close_all_flash_areas (& boot_data );
3281
+ boot_close_all_flash_areas (& boot_data );
3278
3282
memset (& boot_data , 0x00 , sizeof (boot_data ));
3279
3283
}
3280
3284
#endif
0 commit comments