Skip to content

Commit 58538dd

Browse files
bobwolff68nordicjm
authored andcommitted
swap_move: correct appsize calcs and warning
app_max_sectors() is always off by one in its calculation. This corrects that calculation as well as the warning which should only be comparing the two slot sector counts and checking to see if they are different by one sector for the swap sector. The size of the application is not relevant in that warning for optimal sector distribution. Additionally app_max_size() suffers similarly in that it is not taking into account the fact that the secondary slot does also contain a trailer. Finally makes a minor addition to the design document which further clarifies the primary and secondary partition size differences. Signed-off-by: Robert Wolff <[email protected]> (cherry picked from commit 5aed424)
1 parent 5ce8b31 commit 58538dd

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

boot/bootutil/src/swap_move.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ static int app_max_sectors(struct boot_loader_state *state)
243243

244244
sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
245245
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
246-
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
246+
/* subtract 1 for swap and at least 1 for trailer */
247+
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 2;
247248

248249
while (1) {
249250
sz += sector_sz;
@@ -283,9 +284,10 @@ boot_slots_compatible(struct boot_loader_state *state)
283284
return 0;
284285
}
285286

286-
if (num_usable_sectors_pri != (num_sectors_sec + 1)) {
287+
/* Optimal says primary has one more than secondary. Always. Both have trailers. */
288+
if (num_sectors_pri != (num_sectors_sec + 1)) {
287289
BOOT_LOG_DBG("Non-optimal sector distribution, slot0 has %d usable sectors (%d assigned) "
288-
"but slot1 has %d assigned", (int)(num_usable_sectors_pri - 1),
290+
"but slot1 has %d assigned", (int)num_usable_sectors_pri,
289291
(int)num_sectors_pri, (int)num_sectors_sec);
290292
}
291293

@@ -587,7 +589,7 @@ int app_max_size(struct boot_loader_state *state)
587589

588590
/* Account for image flags and move sector */
589591
sz_primary = app_max_sectors(state) * sector_sz_primary - sector_sz_primary;
590-
sz_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) * sector_sz_secondary;
592+
sz_secondary = app_max_sectors(state) * sector_sz_secondary;
591593

592594
return (sz_primary <= sz_secondary ? sz_primary : sz_secondary);
593595
}

docs/design.md

+6
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ Where:
269269
is equal to 1056 B and the sector size is equal to 1024 B, then
270270
`image-trailer-sectors-size` will be equal to 2048 B.
271271

272+
This does imply, if there is any doubt, that the primary slot will be exactly
273+
one sector larger than the secondary slot due to the swap sector alone. It is
274+
the case that both the primary and secondary slots both have a trailer in
275+
addition to the application payload and these trailers are identical in size
276+
to one another.
277+
272278
The algorithm does two erase cycles on the primary slot and one on the secondary
273279
slot during each swap. Assuming that receiving a new image by the DFU
274280
application requires 1 erase cycle on the secondary slot, this should result in

0 commit comments

Comments
 (0)