Skip to content

Commit 0e76aeb

Browse files
committed
bootutil: swap-move: Update maximum size to allow unaligned trailers
This commit updates the computation of the maximum firmware image size for the swap-move strategy to allow the first sector containing part of the trailer to also hold firmware data. This means it is no more necessary to allocate a sector-aligned area for the trailer when using swap-move, the area to allocate is now simply equal to the maximum trailer size. The space required for writing the fallback trailer is also taken into account. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent 5703b6e commit 0e76aeb

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,31 @@ uint32_t bootutil_max_image_size(struct boot_loader_state *state, const struct f
477477
}
478478

479479
return slot_trailer_off - trailer_padding;
480-
#elif defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET)
480+
#elif defined(MCUBOOT_SWAP_USING_MOVE)
481+
(void) fap;
482+
483+
const struct flash_area *fap_pri = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
484+
assert(fap_pri != NULL);
485+
486+
/* Swap-move needs to reserve in the primary slot the size of one full sector plus some space at
487+
* the end of the slot to write the trailer. Also, it must be ensured at least
488+
* BOOT_MAGIC_ALIGN_SIZE bytes are available in the last sector that is not containing part of
489+
* the slot's trailer to be able to write the fallback trailer. That is always the case unless
490+
* the trailer is only a few bytes larger than the size of a sector, i.e. unless:
491+
* 0 < trailer_sz % sector_sz < BOOT_MAGIC_ALIGN_SIZE
492+
*/
493+
size_t trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
494+
size_t sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
495+
size_t padding = sector_sz;
496+
497+
size_t trailer_sz_in_padding_sector = trailer_sz % sector_sz;
498+
499+
if (trailer_sz_in_padding_sector > 0 && trailer_sz_in_padding_sector < BOOT_MAGIC_ALIGN_SIZE) {
500+
padding += BOOT_MAGIC_ALIGN_SIZE - trailer_sz_in_padding_sector;
501+
}
502+
503+
return flash_area_get_size(fap_pri) - trailer_sz - padding;
504+
#elif defined(MCUBOOT_SWAP_USING_OFFSET)
481505
(void) state;
482506

483507
struct flash_sector sector;

0 commit comments

Comments
 (0)