Skip to content

Commit 66d41e7

Browse files
taltenbachnordicjm
authored andcommitted
boot: bootutil: Fix scratch trailer overwritten if image trailer is large
When using swap-scratch and the image trailer doesn't fit in a single sector, some padding might be necessary between the end of the firmware data and the beginning of the image trailer. Indeed, when the trailer fit in a single sector, it is guaranteed that when copying the firmware data from this sector to the scratch area, it won't overwrite the trailer in the scratch trailer since that trailer is always smaller the image trailer. However, when the trailer is larger than a single sector, the sector containing the last part of the firmware data might only contain a very small part of the trailer. There is no more guarantee that the scratch trailer won't get overwritten when copying that sector to the sratch area. Therefore, a check must be added to handle that case. Signed-off-by: Thomas Altenbach <[email protected]>
1 parent 8975d5c commit 66d41e7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

boot/bootutil/src/swap_scratch.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,18 @@ boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
609609
if ((img_off + sz) >
610610
boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
611611
copy_sz = flash_area_get_size(fap_primary_slot) - img_off - trailer_sz;
612+
613+
/* Check if the computed copy size would cause the beginning of the trailer in the scratch
614+
* area to be overwritten. If so, adjust the copy size to avoid this.
615+
*
616+
* This could happen if the trailer is larger than a single sector since in that case the
617+
* first part of the trailer may be smaller than the trailer in the scratch area.
618+
*/
619+
scratch_trailer_off = boot_status_off(fap_scratch);
620+
621+
if (copy_sz > scratch_trailer_off) {
622+
copy_sz = scratch_trailer_off;
623+
}
612624
}
613625

614626
bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);

0 commit comments

Comments
 (0)