bootutil: Support trailers larger than sector size for swap scratch
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
diff --git a/boot/bootutil/src/swap_scratch.c b/boot/bootutil/src/swap_scratch.c
index 1ce0c62..0979ff5 100644
--- a/boot/bootutil/src/swap_scratch.c
+++ b/boot/bootutil/src/swap_scratch.c
@@ -490,6 +490,7 @@
const struct flash_area *fap_scratch;
uint32_t copy_sz;
uint32_t trailer_sz;
+ uint32_t sector_sz;
uint32_t img_off;
uint32_t scratch_trailer_off;
struct boot_swap_state swap_state;
@@ -514,6 +515,21 @@
* controls if special handling is needed (swapping last sector).
*/
last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
+ sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, last_sector);
+
+ if (sector_sz < trailer_sz) {
+ uint32_t trailer_sector_sz = sector_sz;
+
+ while (trailer_sector_sz < trailer_sz) {
+ /* Consider that the image trailer may span across sectors of
+ * different sizes.
+ */
+ sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, --last_sector);
+
+ trailer_sector_sz += sector_sz;
+ }
+ }
+
if ((img_off + sz) >
boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
copy_sz -= trailer_sz;