Skip to content

Commit 9ac7296

Browse files
TomLiernordicjm
authored andcommitted
boot: bootutil: loader: Fix bootstrap copying in swap move mode
Previously, the copy size was calculated using the primary region size, which could be larger than the secondary region. This fix ensures that the size of the secondary region (excluding the swap sector) is used, preventing over-copying and related issues during image upgrade or bootstrap operations. Signed-off-by: LIERMAN Tom <[email protected]> Signed-off-by: David Brown <[email protected]> Signed-off-by: Jamie McCrae <[email protected]>
1 parent 3f69203 commit 9ac7296

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

boot/bootutil/src/loader.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,27 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
991991
size += this_size;
992992
}
993993

994+
#if defined(MCUBOOT_SWAP_USING_MOVE)
995+
/* When using MCUBOOT_SWAP_USING_MOVE, primary region is larger then the secondary region
996+
* Optimal region configuration: # useful regions in primary region = # regions in secondary region + 1
997+
* This means that we have to use the size of the secondary region (so without the swap sector)
998+
*/
999+
sect_count = boot_img_num_sectors(state, BOOT_SLOT_SECONDARY);
1000+
for (sect = 0, size = 0; sect < sect_count; sect++) {
1001+
this_size = boot_img_sector_size(state, BOOT_SLOT_SECONDARY, sect);
1002+
1003+
#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1004+
if ((size + this_size) >= src_size) {
1005+
size += src_size - size;
1006+
size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
1007+
break;
1008+
}
1009+
#endif
1010+
1011+
size += this_size;
1012+
}
1013+
#endif
1014+
9941015
#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
9951016
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
9961017
sector = boot_img_num_sectors(state, BOOT_SLOT_PRIMARY) - 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix: Corrected the copy size calculation when bootstrapping and swapping using MCUBOOT_SWAP_USING_MOVE.
2+
Previously, the primary region size was used, which could be larger than the secondary region, when using the optimal region sizes. Now, the size of the secondary region (excluding the swap sector and sectors needed for swapping) is used, ensuring only the valid image area is copied. This prevents potential over-copying and related issues during image upgrade or bootstrap operations.

0 commit comments

Comments
 (0)