Skip to content

Commit 4d75fc8

Browse files
committed
bootutil: Fix compatible sector checking
Fixes an issue whereby slot sizes were checked but the check was not done properly. This also adds debug log messages to show the sector configuration including if slot sizes are not optimal Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 35e9931 commit 4d75fc8

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

boot/bootutil/src/swap_move.c

+38-18
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,28 @@ boot_status_internal_off(const struct boot_status *bs, int elem_sz)
234234
return off;
235235
}
236236

237+
static int app_max_sectors(struct boot_loader_state *state)
238+
{
239+
uint32_t sz = 0;
240+
uint32_t sector_sz;
241+
uint32_t trailer_sz;
242+
uint32_t first_trailer_idx;
243+
244+
sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
245+
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
246+
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
247+
248+
while (1) {
249+
sz += sector_sz;
250+
if (sz >= trailer_sz) {
251+
break;
252+
}
253+
first_trailer_idx--;
254+
}
255+
256+
return first_trailer_idx;
257+
}
258+
237259
int
238260
boot_slots_compatible(struct boot_loader_state *state)
239261
{
@@ -242,20 +264,31 @@ boot_slots_compatible(struct boot_loader_state *state)
242264
size_t sector_sz_pri = 0;
243265
size_t sector_sz_sec = 0;
244266
size_t i;
267+
size_t num_usable_sectors_pri;
245268

246269
num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
247270
num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
271+
num_usable_sectors_pri = app_max_sectors(state);
272+
248273
if ((num_sectors_pri != num_sectors_sec) &&
249-
(num_sectors_pri != (num_sectors_sec + 1))) {
274+
(num_sectors_pri != (num_sectors_sec + 1)) &&
275+
(num_usable_sectors_pri != (num_sectors_sec + 1))) {
250276
BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
277+
BOOT_LOG_DBG("slot0 sectors: %d, slot1 sectors: %d, usable slot0 sectors: %d",
278+
(int)num_sectors_pri, (int)num_sectors_sec,
279+
(int)(num_usable_sectors_pri - 1));
251280
return 0;
252-
}
253-
254-
if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
281+
} else if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
255282
BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
256283
return 0;
257284
}
258285

286+
if (num_usable_sectors_pri != (num_sectors_sec + 1)) {
287+
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),
289+
(int)num_sectors_pri, (int)num_sectors_sec);
290+
}
291+
259292
for (i = 0; i < num_sectors_sec; i++) {
260293
sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
261294
sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
@@ -544,24 +577,11 @@ swap_run(struct boot_loader_state *state, struct boot_status *bs,
544577

545578
int app_max_size(struct boot_loader_state *state)
546579
{
547-
uint32_t sz = 0;
548580
uint32_t sector_sz;
549-
uint32_t trailer_sz;
550-
uint32_t first_trailer_idx;
551581

552582
sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
553-
trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
554-
first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
555-
556-
while (1) {
557-
sz += sector_sz;
558-
if (sz >= trailer_sz) {
559-
break;
560-
}
561-
first_trailer_idx--;
562-
}
563583

564-
return (first_trailer_idx * sector_sz);
584+
return (app_max_sectors(state) * sector_sz);
565585
}
566586

567587
#endif

0 commit comments

Comments
 (0)