@@ -399,12 +399,87 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
399
399
}
400
400
#endif
401
401
402
- uint32_t bootutil_max_image_size (const struct flash_area * fap )
402
+ #ifdef MCUBOOT_SWAP_USING_SCRATCH
403
+ size_t
404
+ boot_get_first_trailer_sector (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
403
405
{
404
- #if defined(MCUBOOT_SWAP_USING_SCRATCH ) || defined(MCUBOOT_SINGLE_APPLICATION_SLOT ) || \
405
- defined(MCUBOOT_FIRMWARE_LOADER ) || defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD )
406
+ size_t first_trailer_sector = boot_img_num_sectors (state , slot ) - 1 ;
407
+ size_t sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
408
+ size_t trailer_sector_sz = sector_sz ;
409
+
410
+ while (trailer_sector_sz < trailer_sz ) {
411
+ /* Consider that the image trailer may span across sectors of different sizes */
412
+ -- first_trailer_sector ;
413
+ sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
414
+
415
+ trailer_sector_sz += sector_sz ;
416
+ }
417
+
418
+ return first_trailer_sector ;
419
+ }
420
+
421
+ /**
422
+ * Returns the offset to the end of the first sector of a given slot that holds image trailer data.
423
+ *
424
+ * @param state Current bootloader's state.
425
+ * @param slot The index of the slot to consider.
426
+ * @param trailer_sz The size of the trailer, in bytes.
427
+ *
428
+ * @return The offset to the end of the first sector of the slot that holds image trailer data.
429
+ */
430
+ static uint32_t
431
+ get_first_trailer_sector_end_off (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
432
+ {
433
+ size_t first_trailer_sector = boot_get_first_trailer_sector (state , slot , trailer_sz );
434
+
435
+ return boot_img_sector_off (state , slot , first_trailer_sector ) +
436
+ boot_img_sector_size (state , slot , first_trailer_sector );
437
+ }
438
+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
439
+
440
+ uint32_t bootutil_max_image_size (struct boot_loader_state * state , const struct flash_area * fap )
441
+ {
442
+ #if defined(MCUBOOT_SINGLE_APPLICATION_SLOT ) || \
443
+ defined(MCUBOOT_FIRMWARE_LOADER ) || \
444
+ defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD )
445
+ (void ) state ;
406
446
return boot_status_off (fap );
447
+ #elif defined(MCUBOOT_SWAP_USING_SCRATCH )
448
+ size_t slot_trailer_sz = boot_trailer_sz (BOOT_WRITE_SZ (state ));
449
+ size_t slot_trailer_off = flash_area_get_size (fap ) - slot_trailer_sz ;
450
+
451
+ /* If the trailer doesn't fit in the last sector of the primary or secondary slot, some padding
452
+ * might have to be inserted between the end of the firmware image and the beginning of the
453
+ * trailer to ensure there is enough space for the trailer in the scratch area when the last
454
+ * sector of the secondary will be copied to the scratch area.
455
+ *
456
+ * The value of the padding depends on the amount of trailer data that is contained in the first
457
+ * trailer containing part of the trailer in the primary and secondary slot.
458
+ */
459
+ size_t trailer_sector_primary_end_off =
460
+ get_first_trailer_sector_end_off (state , BOOT_PRIMARY_SLOT , slot_trailer_sz );
461
+ size_t trailer_sector_secondary_end_off =
462
+ get_first_trailer_sector_end_off (state , BOOT_SECONDARY_SLOT , slot_trailer_sz );
463
+
464
+ size_t trailer_sz_in_first_sector ;
465
+
466
+ if (trailer_sector_primary_end_off > trailer_sector_secondary_end_off ) {
467
+ trailer_sz_in_first_sector = trailer_sector_primary_end_off - slot_trailer_off ;
468
+ } else {
469
+ trailer_sz_in_first_sector = trailer_sector_secondary_end_off - slot_trailer_off ;
470
+ }
471
+
472
+ size_t trailer_padding = 0 ;
473
+ size_t scratch_trailer_sz = boot_scratch_trailer_sz (BOOT_WRITE_SZ (state ));
474
+
475
+ if (scratch_trailer_sz > trailer_sz_in_first_sector ) {
476
+ trailer_padding = scratch_trailer_sz - trailer_sz_in_first_sector ;
477
+ }
478
+
479
+ return slot_trailer_off - trailer_padding ;
407
480
#elif defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_OFFSET )
481
+ (void ) state ;
482
+
408
483
struct flash_sector sector ;
409
484
/* get the last sector offset */
410
485
int rc = flash_area_get_sector (fap , boot_status_off (fap ), & sector );
@@ -416,10 +491,13 @@ uint32_t bootutil_max_image_size(const struct flash_area *fap)
416
491
}
417
492
return flash_sector_get_off (& sector );
418
493
#elif defined(MCUBOOT_OVERWRITE_ONLY )
494
+ (void ) state ;
419
495
return boot_swap_info_off (fap );
420
496
#elif defined(MCUBOOT_DIRECT_XIP )
497
+ (void ) state ;
421
498
return boot_swap_info_off (fap );
422
499
#elif defined(MCUBOOT_RAM_LOAD )
500
+ (void ) state ;
423
501
return boot_swap_info_off (fap );
424
502
#endif
425
503
}
0 commit comments