Skip to content

Commit 6ab6adb

Browse files
committed
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 <tom.lierman@psicontrol.com> Signed-off-by: David Brown <david.brown@linaro.org> Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 234c66e commit 6ab6adb

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
@@ -990,6 +990,27 @@ boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
990990
size += this_size;
991991
}
992992

993+
#if defined(MCUBOOT_SWAP_USING_MOVE)
994+
/* When using MCUBOOT_SWAP_USING_MOVE, primary region is larger then the secondary region
995+
* Optimal region configuration: # useful regions in primary region = # regions in secondary region + 1
996+
* This means that we have to use the size of the secondary region (so without the swap sector)
997+
*/
998+
sect_count = boot_img_num_sectors(state, BOOT_SLOT_SECONDARY);
999+
for (sect = 0, size = 0; sect < sect_count; sect++) {
1000+
this_size = boot_img_sector_size(state, BOOT_SLOT_SECONDARY, sect);
1001+
1002+
#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1003+
if ((size + this_size) >= src_size) {
1004+
size += src_size - size;
1005+
size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
1006+
break;
1007+
}
1008+
#endif
1009+
1010+
size += this_size;
1011+
}
1012+
#endif
1013+
9931014
#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
9941015
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
9951016
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)