@@ -70,20 +70,10 @@ static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0};
70
70
71
71
#if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
72
72
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
73
- #if !defined(__BOOTSIM__ )
74
73
/* Used for holding static buffers in multiple functions to work around issues
75
74
* in older versions of gcc (e.g. 4.8.4)
76
75
*/
77
- struct sector_buffer_t {
78
- boot_sector_t primary [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
79
- boot_sector_t secondary [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
80
- #if MCUBOOT_SWAP_USING_SCRATCH
81
- boot_sector_t scratch [BOOT_MAX_IMG_SECTORS ];
82
- #endif
83
- };
84
-
85
- static struct sector_buffer_t sector_buffers ;
86
- #endif
76
+ static struct boot_sector_buffer sector_buffers ;
87
77
#endif
88
78
89
79
#if (BOOT_IMAGE_NUMBER > 1 )
@@ -622,20 +612,26 @@ boot_write_sz(struct boot_loader_state *state)
622
612
return elem_sz ;
623
613
}
624
614
625
- /**
626
- * Determines the sector layout of both image slots and the scratch area.
627
- * This information is necessary for calculating the number of bytes to erase
628
- * and copy during an image swap. The information collected during this
629
- * function is used to populate the state.
630
- */
631
- static int
632
- boot_read_sectors (struct boot_loader_state * state )
615
+ int
616
+ boot_read_sectors (struct boot_loader_state * state , struct boot_sector_buffer * sectors )
633
617
{
634
618
uint8_t image_index ;
635
619
int rc ;
636
620
621
+ if (sectors == NULL ) {
622
+ sectors = & sector_buffers ;
623
+ }
624
+
637
625
image_index = BOOT_CURR_IMG (state );
638
626
627
+ BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
628
+ sectors -> primary [image_index ];
629
+ BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors =
630
+ sectors -> secondary [image_index ];
631
+ #if MCUBOOT_SWAP_USING_SCRATCH
632
+ state -> scratch .sectors = sectors -> scratch ;
633
+ #endif
634
+
639
635
rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
640
636
if (rc != 0 ) {
641
637
return BOOT_EFLASH ;
@@ -2067,22 +2063,6 @@ boot_prepare_image_for_update(struct boot_loader_state *state,
2067
2063
int max_size ;
2068
2064
#endif
2069
2065
2070
- /* Determine the sector layout of the image slots and scratch area. */
2071
- rc = boot_read_sectors (state );
2072
- if (rc != 0 ) {
2073
- BOOT_LOG_WRN ("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2074
- " - too small?" , BOOT_MAX_IMG_SECTORS );
2075
- /* Unable to determine sector layout, continue with next image
2076
- * if there is one.
2077
- */
2078
- BOOT_SWAP_TYPE (state ) = BOOT_SWAP_TYPE_NONE ;
2079
- if (rc == BOOT_EFLASH )
2080
- {
2081
- /* Only return on error from the primary image flash */
2082
- return ;
2083
- }
2084
- }
2085
-
2086
2066
/* Attempt to read an image header from each slot. */
2087
2067
rc = boot_read_image_headers (state , false, NULL );
2088
2068
if (rc != 0 ) {
@@ -2341,25 +2321,17 @@ fih_ret
2341
2321
context_boot_go (struct boot_loader_state * state , struct boot_rsp * rsp )
2342
2322
{
2343
2323
struct boot_status bs ;
2324
+ struct boot_sector_buffer * sectors = NULL ;
2344
2325
int rc = -1 ;
2345
2326
FIH_DECLARE (fih_rc , FIH_FAILURE );
2346
- int image_index ;
2347
2327
bool has_upgrade ;
2348
2328
volatile int fih_cnt ;
2349
2329
2350
2330
BOOT_LOG_DBG ("context_boot_go" );
2351
2331
2352
2332
#if defined(__BOOTSIM__ )
2353
- /* The array of slot sectors are defined here (as opposed to file scope) so
2354
- * that they don't get allocated for non-boot-loader apps. This is
2355
- * necessary because the gcc option "-fdata-sections" doesn't seem to have
2356
- * any effect in older gcc versions (e.g., 4.8.4).
2357
- */
2358
- TARGET_STATIC boot_sector_t primary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
2359
- TARGET_STATIC boot_sector_t secondary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
2360
- #if MCUBOOT_SWAP_USING_SCRATCH
2361
- TARGET_STATIC boot_sector_t scratch_sectors [BOOT_MAX_IMG_SECTORS ];
2362
- #endif
2333
+ struct boot_sector_buffer sector_buf ;
2334
+ sectors = & sector_buf ;
2363
2335
#endif
2364
2336
2365
2337
has_upgrade = false;
@@ -2394,28 +2366,22 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2394
2366
*/
2395
2367
boot_enc_zeroize (BOOT_CURR_ENC (state ));
2396
2368
#endif
2369
+ /* Determine the sector layout of the image slots and scratch area. */
2370
+ rc = boot_read_sectors (state , sectors );
2371
+ if (rc != 0 ) {
2372
+ BOOT_LOG_WRN ("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2373
+ " - too small?" , BOOT_MAX_IMG_SECTORS );
2374
+ BOOT_SWAP_TYPE (state ) = BOOT_SWAP_TYPE_NONE ;
2375
+ }
2397
2376
2398
- image_index = BOOT_CURR_IMG (state );
2399
-
2400
- #if !defined(__BOOTSIM__ )
2401
- BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
2402
- sector_buffers .primary [image_index ];
2403
- BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors =
2404
- sector_buffers .secondary [image_index ];
2405
- #if MCUBOOT_SWAP_USING_SCRATCH
2406
- state -> scratch .sectors = sector_buffers .scratch ;
2407
- #endif
2408
- #else
2409
- BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors =
2410
- primary_slot_sectors [image_index ];
2411
- BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors =
2412
- secondary_slot_sectors [image_index ];
2413
- #if MCUBOOT_SWAP_USING_SCRATCH
2414
- state -> scratch .sectors = scratch_sectors ;
2415
- #endif
2416
- #endif
2417
- /* Determine swap type and complete swap if it has been aborted. */
2418
- boot_prepare_image_for_update (state , & bs );
2377
+ /* Unless there was an error when determining the sector layout of the primary slot,
2378
+ * determine swap type and complete swap if it has been aborted.
2379
+ *
2380
+ * Note boot_read_sectors returns BOOT_EFLASH_SEC for errors regarding the secondary slot.
2381
+ */
2382
+ if (rc != BOOT_EFLASH ) {
2383
+ boot_prepare_image_for_update (state , & bs );
2384
+ }
2419
2385
2420
2386
if (BOOT_IS_UPGRADE (BOOT_SWAP_TYPE (state ))) {
2421
2387
has_upgrade = true;
@@ -2629,19 +2595,17 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2629
2595
fih_ret
2630
2596
split_go (int loader_slot , int split_slot , void * * entry )
2631
2597
{
2632
- boot_sector_t * sectors ;
2598
+ struct boot_sector_buffer * sectors ;
2633
2599
uintptr_t entry_val ;
2634
2600
int loader_flash_id ;
2635
2601
int split_flash_id ;
2636
2602
int rc ;
2637
2603
FIH_DECLARE (fih_rc , FIH_FAILURE );
2638
2604
2639
- sectors = malloc (BOOT_MAX_IMG_SECTORS * 2 * sizeof * sectors );
2605
+ sectors = malloc (sizeof ( struct boot_sector_buffer ) );
2640
2606
if (sectors == NULL ) {
2641
2607
FIH_RET (FIH_FAILURE );
2642
2608
}
2643
- BOOT_IMG (& boot_data , loader_slot ).sectors = sectors + 0 ;
2644
- BOOT_IMG (& boot_data , split_slot ).sectors = sectors + BOOT_MAX_IMG_SECTORS ;
2645
2609
2646
2610
loader_flash_id = flash_area_id_from_image_slot (loader_slot );
2647
2611
rc = flash_area_open (loader_flash_id ,
@@ -2653,7 +2617,7 @@ split_go(int loader_slot, int split_slot, void **entry)
2653
2617
assert (rc == 0 );
2654
2618
2655
2619
/* Determine the sector layout of the image slots and scratch area. */
2656
- rc = boot_read_sectors (& boot_data );
2620
+ rc = boot_read_sectors (& boot_data , sectors );
2657
2621
if (rc != 0 ) {
2658
2622
rc = SPLIT_GO_ERR ;
2659
2623
goto done ;
0 commit comments