Skip to content

Commit 30109df

Browse files
committed
boot: bootutil: loader: Fix slot info for directXIP/RAM load
Fixes an issue when either of these modes is used with serial recovery slot info enabled Signed-off-by: Jamie McCrae <[email protected]>
1 parent 71bccff commit 30109df

File tree

1 file changed

+88
-61
lines changed

1 file changed

+88
-61
lines changed

boot/bootutil/src/loader.c

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,93 @@ boot_version_cmp(const struct image_version *ver1,
301301
}
302302
#endif
303303

304+
#if (!defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)) || \
305+
defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO)
306+
#if !defined(__BOOTSIM__)
307+
static void boot_get_sector_buffers(struct sector_buffer_t *buffers)
308+
{
309+
/* The array of slot sectors are defined here (as opposed to file scope) so
310+
* that they don't get allocated for non-boot-loader apps. This is
311+
* necessary because the gcc option "-fdata-sections" doesn't seem to have
312+
* any effect in older gcc versions (e.g., 4.8.4).
313+
*/
314+
static boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
315+
static boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
316+
#if MCUBOOT_SWAP_USING_SCRATCH
317+
static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
318+
#endif
319+
320+
buffers->primary = (boot_sector_t *)&primary_slot_sectors;
321+
buffers->secondary = (boot_sector_t *)&secondary_slot_sectors;
322+
#if MCUBOOT_SWAP_USING_SCRATCH
323+
buffers->scratch = (boot_sector_t *)&scratch_sectors;
324+
#endif
325+
}
326+
#endif
327+
328+
static int
329+
boot_initialize_area(struct boot_loader_state *state, int flash_area)
330+
{
331+
uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
332+
boot_sector_t *out_sectors;
333+
uint32_t *out_num_sectors;
334+
int rc;
335+
336+
num_sectors = BOOT_MAX_IMG_SECTORS;
337+
338+
if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
339+
out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
340+
out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
341+
} else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
342+
out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
343+
out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
344+
#if MCUBOOT_SWAP_USING_SCRATCH
345+
} else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
346+
out_sectors = state->scratch.sectors;
347+
out_num_sectors = &state->scratch.num_sectors;
348+
#endif
349+
} else {
350+
return BOOT_EFLASH;
351+
}
352+
353+
#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
354+
rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
355+
#else
356+
_Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
357+
rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
358+
#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
359+
if (rc != 0) {
360+
return rc;
361+
}
362+
*out_num_sectors = num_sectors;
363+
return 0;
364+
}
365+
#endif
366+
367+
#if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO)
368+
static int
369+
boot_read_sectors_recovery(struct boot_loader_state *state)
370+
{
371+
uint8_t image_index;
372+
int rc;
373+
374+
image_index = BOOT_CURR_IMG(state);
375+
376+
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
377+
if (rc != 0) {
378+
return BOOT_EFLASH;
379+
}
380+
381+
rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
382+
if (rc != 0) {
383+
/* We need to differentiate from the primary image issue */
384+
return BOOT_EFLASH_SEC;
385+
}
386+
387+
return 0;
388+
}
389+
#endif
390+
304391

305392
#if (BOOT_IMAGE_NUMBER > 1)
306393

@@ -617,44 +704,6 @@ boot_write_sz(struct boot_loader_state *state)
617704
return elem_sz;
618705
}
619706

620-
static int
621-
boot_initialize_area(struct boot_loader_state *state, int flash_area)
622-
{
623-
uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
624-
boot_sector_t *out_sectors;
625-
uint32_t *out_num_sectors;
626-
int rc;
627-
628-
num_sectors = BOOT_MAX_IMG_SECTORS;
629-
630-
if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
631-
out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
632-
out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
633-
} else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
634-
out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
635-
out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
636-
#if MCUBOOT_SWAP_USING_SCRATCH
637-
} else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
638-
out_sectors = state->scratch.sectors;
639-
out_num_sectors = &state->scratch.num_sectors;
640-
#endif
641-
} else {
642-
return BOOT_EFLASH;
643-
}
644-
645-
#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
646-
rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
647-
#else
648-
_Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
649-
rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
650-
#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
651-
if (rc != 0) {
652-
return rc;
653-
}
654-
*out_num_sectors = num_sectors;
655-
return 0;
656-
}
657-
658707
/**
659708
* Determines the sector layout of both image slots and the scratch area.
660709
* This information is necessary for calculating the number of bytes to erase
@@ -2155,28 +2204,6 @@ check_downgrade_prevention(struct boot_loader_state *state)
21552204
#endif
21562205
}
21572206

2158-
#if !defined(__BOOTSIM__)
2159-
static void boot_get_sector_buffers(struct sector_buffer_t *buffers)
2160-
{
2161-
/* The array of slot sectors are defined here (as opposed to file scope) so
2162-
* that they don't get allocated for non-boot-loader apps. This is
2163-
* necessary because the gcc option "-fdata-sections" doesn't seem to have
2164-
* any effect in older gcc versions (e.g., 4.8.4).
2165-
*/
2166-
static boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2167-
static boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2168-
#if MCUBOOT_SWAP_USING_SCRATCH
2169-
static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
2170-
#endif
2171-
2172-
buffers->primary = (boot_sector_t *)&primary_slot_sectors;
2173-
buffers->secondary = (boot_sector_t *)&secondary_slot_sectors;
2174-
#if MCUBOOT_SWAP_USING_SCRATCH
2175-
buffers->scratch = (boot_sector_t *)&scratch_sectors;
2176-
#endif
2177-
}
2178-
#endif
2179-
21802207
fih_ret
21812208
context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
21822209
{
@@ -3486,7 +3513,7 @@ static void boot_fetch_slot_state_sizes(void)
34863513
#endif
34873514

34883515
/* Determine the sector layout of the image slots and scratch area. */
3489-
rc = boot_read_sectors(&boot_data);
3516+
rc = boot_read_sectors_recovery(&boot_data);
34903517

34913518
if (rc == 0) {
34923519
max_size = app_max_size(&boot_data);

0 commit comments

Comments
 (0)