Skip to content

Commit 2cd598b

Browse files
committed
fix(bootutil): correct copy size when bootstrapping and swapping with MOVE
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]>
1 parent 85aecf6 commit 2cd598b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

boot/bootutil/src/loader.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,19 @@ 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+
size = 0;
999+
sect_count = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
1000+
for (sect = 0, size = 0; sect < sect_count; sect++) {
1001+
this_size = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, sect);
1002+
size += this_size;
1003+
}
1004+
#endif
1005+
9931006
#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
9941007
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
9951008
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)