@@ -330,6 +330,93 @@ boot_version_cmp(const struct image_version *ver1,
330330}
331331#endif
332332
333+ #if (!defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )) || \
334+ defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
335+ #if !defined(__BOOTSIM__ )
336+ static void boot_get_sector_buffers (struct sector_buffer_t * buffers )
337+ {
338+ /* The array of slot sectors are defined here (as opposed to file scope) so
339+ * that they don't get allocated for non-boot-loader apps. This is
340+ * necessary because the gcc option "-fdata-sections" doesn't seem to have
341+ * any effect in older gcc versions (e.g., 4.8.4).
342+ */
343+ static boot_sector_t primary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
344+ static boot_sector_t secondary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
345+ #if MCUBOOT_SWAP_USING_SCRATCH
346+ static boot_sector_t scratch_sectors [BOOT_MAX_IMG_SECTORS ];
347+ #endif
348+
349+ buffers -> primary = (boot_sector_t * )& primary_slot_sectors ;
350+ buffers -> secondary = (boot_sector_t * )& secondary_slot_sectors ;
351+ #if MCUBOOT_SWAP_USING_SCRATCH
352+ buffers -> scratch = (boot_sector_t * )& scratch_sectors ;
353+ #endif
354+ }
355+ #endif
356+
357+ static int
358+ boot_initialize_area (struct boot_loader_state * state , int flash_area )
359+ {
360+ uint32_t num_sectors = BOOT_MAX_IMG_SECTORS ;
361+ boot_sector_t * out_sectors ;
362+ uint32_t * out_num_sectors ;
363+ int rc ;
364+
365+ num_sectors = BOOT_MAX_IMG_SECTORS ;
366+
367+ if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
368+ out_sectors = BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors ;
369+ out_num_sectors = & BOOT_IMG (state , BOOT_PRIMARY_SLOT ).num_sectors ;
370+ } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY (BOOT_CURR_IMG (state ))) {
371+ out_sectors = BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors ;
372+ out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
373+ #if MCUBOOT_SWAP_USING_SCRATCH
374+ } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
375+ out_sectors = state -> scratch .sectors ;
376+ out_num_sectors = & state -> scratch .num_sectors ;
377+ #endif
378+ } else {
379+ return BOOT_EFLASH ;
380+ }
381+
382+ #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
383+ rc = flash_area_get_sectors (flash_area , & num_sectors , out_sectors );
384+ #else
385+ _Static_assert (sizeof (int ) <= sizeof (uint32_t ), "Fix needed" );
386+ rc = flash_area_to_sectors (flash_area , (int * )& num_sectors , out_sectors );
387+ #endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
388+ if (rc != 0 ) {
389+ return rc ;
390+ }
391+ * out_num_sectors = num_sectors ;
392+ return 0 ;
393+ }
394+ #endif
395+
396+ #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO )
397+ static int
398+ boot_read_sectors_recovery (struct boot_loader_state * state )
399+ {
400+ uint8_t image_index ;
401+ int rc ;
402+
403+ image_index = BOOT_CURR_IMG (state );
404+
405+ rc = boot_initialize_area (state , FLASH_AREA_IMAGE_PRIMARY (image_index ));
406+ if (rc != 0 ) {
407+ return BOOT_EFLASH ;
408+ }
409+
410+ rc = boot_initialize_area (state , FLASH_AREA_IMAGE_SECONDARY (image_index ));
411+ if (rc != 0 ) {
412+ /* We need to differentiate from the primary image issue */
413+ return BOOT_EFLASH_SEC ;
414+ }
415+
416+ return 0 ;
417+ }
418+ #endif
419+
333420
334421#if (BOOT_IMAGE_NUMBER > 1 )
335422
@@ -690,44 +777,6 @@ boot_write_sz(struct boot_loader_state *state)
690777 return elem_sz ;
691778}
692779
693- static int
694- boot_initialize_area (struct boot_loader_state * state , int flash_area )
695- {
696- uint32_t num_sectors = BOOT_MAX_IMG_SECTORS ;
697- boot_sector_t * out_sectors ;
698- uint32_t * out_num_sectors ;
699- int rc ;
700-
701- num_sectors = BOOT_MAX_IMG_SECTORS ;
702-
703- if (flash_area == FLASH_AREA_IMAGE_PRIMARY (BOOT_CURR_IMG (state ))) {
704- out_sectors = BOOT_IMG (state , BOOT_PRIMARY_SLOT ).sectors ;
705- out_num_sectors = & BOOT_IMG (state , BOOT_PRIMARY_SLOT ).num_sectors ;
706- } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY (BOOT_CURR_IMG (state ))) {
707- out_sectors = BOOT_IMG (state , BOOT_SECONDARY_SLOT ).sectors ;
708- out_num_sectors = & BOOT_IMG (state , BOOT_SECONDARY_SLOT ).num_sectors ;
709- #if MCUBOOT_SWAP_USING_SCRATCH
710- } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH ) {
711- out_sectors = state -> scratch .sectors ;
712- out_num_sectors = & state -> scratch .num_sectors ;
713- #endif
714- } else {
715- return BOOT_EFLASH ;
716- }
717-
718- #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
719- rc = flash_area_get_sectors (flash_area , & num_sectors , out_sectors );
720- #else
721- _Static_assert (sizeof (int ) <= sizeof (uint32_t ), "Fix needed" );
722- rc = flash_area_to_sectors (flash_area , (int * )& num_sectors , out_sectors );
723- #endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
724- if (rc != 0 ) {
725- return rc ;
726- }
727- * out_num_sectors = num_sectors ;
728- return 0 ;
729- }
730-
731780/**
732781 * Determines the sector layout of both image slots and the scratch area.
733782 * This information is necessary for calculating the number of bytes to erase
@@ -2496,28 +2545,6 @@ check_downgrade_prevention(struct boot_loader_state *state)
24962545#endif
24972546}
24982547
2499- #if !defined(__BOOTSIM__ )
2500- static void boot_get_sector_buffers (struct sector_buffer_t * buffers )
2501- {
2502- /* The array of slot sectors are defined here (as opposed to file scope) so
2503- * that they don't get allocated for non-boot-loader apps. This is
2504- * necessary because the gcc option "-fdata-sections" doesn't seem to have
2505- * any effect in older gcc versions (e.g., 4.8.4).
2506- */
2507- static boot_sector_t primary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
2508- static boot_sector_t secondary_slot_sectors [BOOT_IMAGE_NUMBER ][BOOT_MAX_IMG_SECTORS ];
2509- #if MCUBOOT_SWAP_USING_SCRATCH
2510- static boot_sector_t scratch_sectors [BOOT_MAX_IMG_SECTORS ];
2511- #endif
2512-
2513- buffers -> primary = (boot_sector_t * )& primary_slot_sectors ;
2514- buffers -> secondary = (boot_sector_t * )& secondary_slot_sectors ;
2515- #if MCUBOOT_SWAP_USING_SCRATCH
2516- buffers -> scratch = (boot_sector_t * )& scratch_sectors ;
2517- #endif
2518- }
2519- #endif
2520-
25212548fih_ret
25222549context_boot_go (struct boot_loader_state * state , struct boot_rsp * rsp )
25232550{
@@ -3850,7 +3877,7 @@ static void boot_fetch_slot_state_sizes(void)
38503877#endif
38513878
38523879 /* Determine the sector layout of the image slots and scratch area. */
3853- rc = boot_read_sectors (& boot_data );
3880+ rc = boot_read_sectors_recovery (& boot_data );
38543881
38553882 if (rc == 0 ) {
38563883 max_size = app_max_size (& boot_data );
0 commit comments