Skip to content

Commit 61d280b

Browse files
taltenbachd3zd3z
authored andcommitted
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 17b56a0 commit 61d280b

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
@@ -477,18 +477,27 @@ uint32_t bootutil_max_image_size(struct boot_loader_state *state, const struct f
477477

478478
return slot_trailer_off - trailer_padding;
479479
#elif defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET)
480-
(void) state;
480+
(void) fap;
481481

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

0 commit comments

Comments
 (0)