Skip to content

Commit bd9e33c

Browse files
committed
boot: bootutil: Fix max image size computation for swap-move/swap-offset
When computing the maximum image size in bootutil_max_image_size for swap-move or swap-offset strategy, the computation was using the size of the flash area provided as argument and was not taking into account the size of the padding sector. This was causing an incorrect size to be returned in some cases, for example when the two slots have the same size or when the slots haven't the same size but the routine is called for the slot containing the padding sector. For example, let's imagine swap-move is being used on a device having a sector size S and two slots of N bytes. This is valid configuration and the maximum image size is N - S - T, T being the size of the trailer rounded up to the next multiple of S. When calling bootutil_max_image_size with either the primary or secondary slot, the size N - T is returned, which is incorrect. This commit fixes the issue by computing always the maximum image using the size of the slot containing the padding and substracting the size of the padding and of the aligned trailer. Signed-off-by: Thomas Altenbach <thomas.altenbach@legrand.com>
1 parent a98bff9 commit bd9e33c

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,27 @@ uint32_t bootutil_max_image_size(struct boot_loader_state *state, const struct f
476476

477477
return slot_trailer_off - trailer_padding;
478478
#elif defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET)
479-
(void) state;
479+
(void) fap;
480480

481-
struct flash_sector sector;
482-
/* get the last sector offset */
483-
int rc = flash_area_get_sector(fap, boot_status_off(fap), &sector);
484-
if (rc) {
485-
BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
486-
return 0; /* Returning of zero here should cause any check which uses
487-
* this value to fail.
488-
*/
489-
}
490-
return flash_sector_get_off(&sector);
481+
/* The slot whose size is used to compute the maximum image size must be the one containing the
482+
* padding required for the swap. */
483+
#ifdef MCUBOOT_SWAP_USING_MOVE
484+
size_t slot = BOOT_PRIMARY_SLOT;
485+
#else
486+
size_t slot = BOOT_SECONDARY_SLOT;
487+
#endif
488+
489+
const struct flash_area *fap_padded_slot = BOOT_IMG_AREA(state, slot);
490+
assert(fap_padded_slot != NULL);
491+
492+
size_t trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
493+
size_t sector_sz = boot_img_sector_size(state, slot, 0);
494+
size_t padding_sz = sector_sz;
495+
496+
/* The trailer size needs to be sector-aligned */
497+
trailer_sz = ALIGN_UP(trailer_sz, sector_sz);
498+
499+
return flash_area_get_size(fap_padded_slot) - trailer_sz - padding_sz;
491500
#elif defined(MCUBOOT_OVERWRITE_ONLY)
492501
(void) state;
493502
return boot_swap_info_off(fap);

0 commit comments

Comments
 (0)