@@ -331,12 +331,87 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
331
331
}
332
332
#endif
333
333
334
- uint32_t bootutil_max_image_size (const struct flash_area * fap )
334
+ #ifdef MCUBOOT_SWAP_USING_SCRATCH
335
+ size_t
336
+ boot_get_first_trailer_sector (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
335
337
{
336
- #if defined(MCUBOOT_SWAP_USING_SCRATCH ) || defined(MCUBOOT_SINGLE_APPLICATION_SLOT ) || \
337
- defined(MCUBOOT_FIRMWARE_LOADER ) || defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD )
338
+ size_t first_trailer_sector = boot_img_num_sectors (state , slot ) - 1 ;
339
+ size_t sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
340
+ size_t trailer_sector_sz = sector_sz ;
341
+
342
+ while (trailer_sector_sz < trailer_sz ) {
343
+ /* Consider that the image trailer may span across sectors of different sizes */
344
+ -- first_trailer_sector ;
345
+ sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
346
+
347
+ trailer_sector_sz += sector_sz ;
348
+ }
349
+
350
+ return first_trailer_sector ;
351
+ }
352
+
353
+ /**
354
+ * Returns the offset to the end of the first sector of a given slot that holds image trailer data.
355
+ *
356
+ * @param state Current bootloader's state.
357
+ * @param slot The index of the slot to consider.
358
+ * @param trailer_sz The size of the trailer, in bytes.
359
+ *
360
+ * @return The offset to the end of the first sector of the slot that holds image trailer data.
361
+ */
362
+ static uint32_t
363
+ get_first_trailer_sector_end_off (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
364
+ {
365
+ size_t first_trailer_sector = boot_get_first_trailer_sector (state , slot , trailer_sz );
366
+
367
+ return boot_img_sector_off (state , slot , first_trailer_sector ) +
368
+ boot_img_sector_size (state , slot , first_trailer_sector );
369
+ }
370
+ #endif /* MCUBOOT_SWAP_USING_SCRATCH */
371
+
372
+ uint32_t bootutil_max_image_size (struct boot_loader_state * state , const struct flash_area * fap )
373
+ {
374
+ #if defined(MCUBOOT_SINGLE_APPLICATION_SLOT ) || \
375
+ defined(MCUBOOT_FIRMWARE_LOADER ) || \
376
+ defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD )
377
+ (void ) state ;
338
378
return boot_status_off (fap );
379
+ #elif defined(MCUBOOT_SWAP_USING_SCRATCH )
380
+ size_t slot_trailer_sz = boot_trailer_sz (BOOT_WRITE_SZ (state ));
381
+ size_t slot_trailer_off = flash_area_get_size (fap ) - slot_trailer_sz ;
382
+
383
+ /* If the trailer doesn't fit in the last sector of the primary or secondary slot, some padding
384
+ * might have to be inserted between the end of the firmware image and the beginning of the
385
+ * trailer to ensure there is enough space for the trailer in the scratch area when the last
386
+ * sector of the secondary will be copied to the scratch area.
387
+ *
388
+ * The value of the padding depends on the amount of trailer data that is contained in the first
389
+ * trailer containing part of the trailer in the primary and secondary slot.
390
+ */
391
+ size_t trailer_sector_primary_end_off =
392
+ get_first_trailer_sector_end_off (state , BOOT_PRIMARY_SLOT , slot_trailer_sz );
393
+ size_t trailer_sector_secondary_end_off =
394
+ get_first_trailer_sector_end_off (state , BOOT_SECONDARY_SLOT , slot_trailer_sz );
395
+
396
+ size_t trailer_sz_in_first_sector ;
397
+
398
+ if (trailer_sector_primary_end_off > trailer_sector_secondary_end_off ) {
399
+ trailer_sz_in_first_sector = trailer_sector_primary_end_off - slot_trailer_off ;
400
+ } else {
401
+ trailer_sz_in_first_sector = trailer_sector_secondary_end_off - slot_trailer_off ;
402
+ }
403
+
404
+ size_t trailer_padding = 0 ;
405
+ size_t scratch_trailer_sz = boot_scratch_trailer_sz (BOOT_WRITE_SZ (state ));
406
+
407
+ if (scratch_trailer_sz > trailer_sz_in_first_sector ) {
408
+ trailer_padding = scratch_trailer_sz - trailer_sz_in_first_sector ;
409
+ }
410
+
411
+ return slot_trailer_off - trailer_padding ;
339
412
#elif defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_OFFSET )
413
+ (void ) state ;
414
+
340
415
struct flash_sector sector ;
341
416
/* get the last sector offset */
342
417
int rc = flash_area_get_sector (fap , boot_status_off (fap ), & sector );
@@ -348,10 +423,13 @@ uint32_t bootutil_max_image_size(const struct flash_area *fap)
348
423
}
349
424
return flash_sector_get_off (& sector );
350
425
#elif defined(MCUBOOT_OVERWRITE_ONLY )
426
+ (void ) state ;
351
427
return boot_swap_info_off (fap );
352
428
#elif defined(MCUBOOT_DIRECT_XIP )
429
+ (void ) state ;
353
430
return boot_swap_info_off (fap );
354
431
#elif defined(MCUBOOT_RAM_LOAD )
432
+ (void ) state ;
355
433
return boot_swap_info_off (fap );
356
434
#endif
357
435
}
0 commit comments