4242#ifdef MCUBOOT_ENC_IMAGES
4343#include "bootutil/enc_key.h"
4444#endif
45+ #if defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_OFFSET ) || \
46+ defined(MCUBOOT_SWAP_USING_SCRATCH )
47+ #include "swap_priv.h"
48+ #endif
4549
4650#if defined(MCUBOOT_DECOMPRESS_IMAGES )
4751#include <nrf_compress/implementation.h>
@@ -240,8 +244,7 @@ int boot_header_scramble_off_sz(const struct flash_area *fa, int slot, size_t *o
240244 * status during the swap of the last sector from primary/secondary (which
241245 * is the first swap operation) and thus only requires space for one swap.
242246 */
243- static uint32_t
244- boot_scratch_trailer_sz (uint32_t min_write_sz )
247+ uint32_t boot_scratch_trailer_sz (uint32_t min_write_sz )
245248{
246249 return boot_status_entry_sz (min_write_sz ) + boot_trailer_info_sz ();
247250}
@@ -427,44 +430,6 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
427430}
428431#endif
429432
430- #ifdef MCUBOOT_SWAP_USING_SCRATCH
431- size_t
432- boot_get_first_trailer_sector (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
433- {
434- size_t first_trailer_sector = boot_img_num_sectors (state , slot ) - 1 ;
435- size_t sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
436- size_t trailer_sector_sz = sector_sz ;
437-
438- while (trailer_sector_sz < trailer_sz ) {
439- /* Consider that the image trailer may span across sectors of different sizes */
440- -- first_trailer_sector ;
441- sector_sz = boot_img_sector_size (state , slot , first_trailer_sector );
442-
443- trailer_sector_sz += sector_sz ;
444- }
445-
446- return first_trailer_sector ;
447- }
448-
449- /**
450- * Returns the offset to the end of the first sector of a given slot that holds image trailer data.
451- *
452- * @param state Current bootloader's state.
453- * @param slot The index of the slot to consider.
454- * @param trailer_sz The size of the trailer, in bytes.
455- *
456- * @return The offset to the end of the first sector of the slot that holds image trailer data.
457- */
458- static uint32_t
459- get_first_trailer_sector_end_off (struct boot_loader_state * state , size_t slot , size_t trailer_sz )
460- {
461- size_t first_trailer_sector = boot_get_first_trailer_sector (state , slot , trailer_sz );
462-
463- return boot_img_sector_off (state , slot , first_trailer_sector ) +
464- boot_img_sector_size (state , slot , first_trailer_sector );
465- }
466- #endif /* MCUBOOT_SWAP_USING_SCRATCH */
467-
468433uint32_t bootutil_max_image_size (struct boot_loader_state * state , const struct flash_area * fap )
469434{
470435#if defined(CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) && CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
@@ -484,61 +449,10 @@ uint32_t bootutil_max_image_size(struct boot_loader_state *state, const struct f
484449 defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD )
485450 (void ) state ;
486451 return boot_status_off (fap );
487- #elif defined(MCUBOOT_SWAP_USING_SCRATCH )
488- size_t slot_trailer_sz = boot_trailer_sz (BOOT_WRITE_SZ (state ));
489- size_t slot_trailer_off = flash_area_get_size (fap ) - slot_trailer_sz ;
490-
491- /* If the trailer doesn't fit in the last sector of the primary or secondary slot, some padding
492- * might have to be inserted between the end of the firmware image and the beginning of the
493- * trailer to ensure there is enough space for the trailer in the scratch area when the last
494- * sector of the secondary will be copied to the scratch area.
495- *
496- * The value of the padding depends on the amount of trailer data that is contained in the first
497- * trailer containing part of the trailer in the primary and secondary slot.
498- */
499- size_t trailer_sector_primary_end_off =
500- get_first_trailer_sector_end_off (state , BOOT_PRIMARY_SLOT , slot_trailer_sz );
501- size_t trailer_sector_secondary_end_off =
502- get_first_trailer_sector_end_off (state , BOOT_SECONDARY_SLOT , slot_trailer_sz );
503-
504- size_t trailer_sz_in_first_sector ;
505-
506- if (trailer_sector_primary_end_off > trailer_sector_secondary_end_off ) {
507- trailer_sz_in_first_sector = trailer_sector_primary_end_off - slot_trailer_off ;
508- } else {
509- trailer_sz_in_first_sector = trailer_sector_secondary_end_off - slot_trailer_off ;
510- }
511-
512- size_t trailer_padding = 0 ;
513- size_t scratch_trailer_sz = boot_scratch_trailer_sz (BOOT_WRITE_SZ (state ));
514-
515- if (scratch_trailer_sz > trailer_sz_in_first_sector ) {
516- trailer_padding = scratch_trailer_sz - trailer_sz_in_first_sector ;
517- }
518-
519- return slot_trailer_off - trailer_padding ;
520- #elif defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_OFFSET )
452+ #elif defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_OFFSET ) \
453+ || defined(MCUBOOT_SWAP_USING_SCRATCH )
521454 (void ) fap ;
522-
523- /* The slot whose size is used to compute the maximum image size must be the one containing the
524- * padding required for the swap. */
525- #ifdef MCUBOOT_SWAP_USING_MOVE
526- size_t slot = BOOT_PRIMARY_SLOT ;
527- #else
528- size_t slot = BOOT_SECONDARY_SLOT ;
529- #endif
530-
531- const struct flash_area * fap_padded_slot = BOOT_IMG_AREA (state , slot );
532- assert (fap_padded_slot != NULL );
533-
534- size_t trailer_sz = boot_trailer_sz (BOOT_WRITE_SZ (state ));
535- size_t sector_sz = boot_img_sector_size (state , slot , 0 );
536- size_t padding_sz = sector_sz ;
537-
538- /* The trailer size needs to be sector-aligned */
539- trailer_sz = ALIGN_UP (trailer_sz , sector_sz );
540-
541- return flash_area_get_size (fap_padded_slot ) - trailer_sz - padding_sz ;
455+ return app_max_size (state );
542456#elif defined(MCUBOOT_OVERWRITE_ONLY )
543457 (void ) state ;
544458 return boot_swap_info_off (fap );
0 commit comments