Skip to content

Commit 5aed424

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]>
1 parent 454c033 commit 5aed424

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
@@ -236,7 +236,8 @@ static int app_max_sectors(struct boot_loader_state *state)
236236

237237
sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
238238
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
239-
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
239+
/* subtract 1 for swap and at least 1 for trailer */
240+
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 2;
240241

241242
while (1) {
242243
sz += sector_sz;
@@ -276,9 +277,10 @@ boot_slots_compatible(struct boot_loader_state *state)
276277
return 0;
277278
}
278279

279-
if (num_usable_sectors_pri != (num_sectors_sec + 1)) {
280+
/* Optimal says primary has one more than secondary. Always. Both have trailers. */
281+
if (num_sectors_pri != (num_sectors_sec + 1)) {
280282
BOOT_LOG_DBG("Non-optimal sector distribution, slot0 has %d usable sectors (%d assigned) "
281-
"but slot1 has %d assigned", (int)(num_usable_sectors_pri - 1),
283+
"but slot1 has %d assigned", (int)num_usable_sectors_pri,
282284
(int)num_sectors_pri, (int)num_sectors_sec);
283285
}
284286

@@ -598,7 +600,7 @@ int app_max_size(struct boot_loader_state *state)
598600

599601
/* Account for image flags and move sector */
600602
sz_primary = app_max_sectors(state) * sector_sz_primary - sector_sz_primary;
601-
sz_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) * sector_sz_secondary;
603+
sz_secondary = app_max_sectors(state) * sector_sz_secondary;
602604

603605
return (sz_primary <= sz_secondary ? sz_primary : sz_secondary);
604606
}

docs/design.md

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

317+
This does imply, if there is any doubt, that the primary slot will be exactly
318+
one sector larger than the secondary slot due to the swap sector alone. It is
319+
the case that both the primary and secondary slots both have a trailer in
320+
addition to the application payload and these trailers are identical in size
321+
to one another.
322+
317323
The algorithm does two erase cycles on the primary slot and one on the secondary
318324
slot during each swap. Assuming that receiving a new image by the DFU
319325
application requires 1 erase cycle on the secondary slot, this should result in

0 commit comments

Comments
 (0)