@@ -234,6 +234,28 @@ boot_status_internal_off(const struct boot_status *bs, int elem_sz)
234
234
return off ;
235
235
}
236
236
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
+
237
259
int
238
260
boot_slots_compatible (struct boot_loader_state * state )
239
261
{
@@ -242,20 +264,31 @@ boot_slots_compatible(struct boot_loader_state *state)
242
264
size_t sector_sz_pri = 0 ;
243
265
size_t sector_sz_sec = 0 ;
244
266
size_t i ;
267
+ size_t num_usable_sectors_pri ;
245
268
246
269
num_sectors_pri = boot_img_num_sectors (state , BOOT_PRIMARY_SLOT );
247
270
num_sectors_sec = boot_img_num_sectors (state , BOOT_SECONDARY_SLOT );
271
+ num_usable_sectors_pri = app_max_sectors (state );
272
+
248
273
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 ))) {
250
276
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 ));
251
280
return 0 ;
252
- }
253
-
254
- if (num_sectors_pri > BOOT_MAX_IMG_SECTORS ) {
281
+ } else if (num_sectors_pri > BOOT_MAX_IMG_SECTORS ) {
255
282
BOOT_LOG_WRN ("Cannot upgrade: more sectors than allowed" );
256
283
return 0 ;
257
284
}
258
285
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
+
259
292
for (i = 0 ; i < num_sectors_sec ; i ++ ) {
260
293
sector_sz_pri = boot_img_sector_size (state , BOOT_PRIMARY_SLOT , i );
261
294
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,
544
577
545
578
int app_max_size (struct boot_loader_state * state )
546
579
{
547
- uint32_t sz = 0 ;
548
580
uint32_t sector_sz ;
549
- uint32_t trailer_sz ;
550
- uint32_t first_trailer_idx ;
551
581
552
582
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
- }
563
583
564
- return (first_trailer_idx * sector_sz );
584
+ return (app_max_sectors ( state ) * sector_sz );
565
585
}
566
586
567
587
#endif
0 commit comments