|  | 
| 42 | 42 | #ifdef MCUBOOT_ENC_IMAGES | 
| 43 | 43 | #include "bootutil/enc_key.h" | 
| 44 | 44 | #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 | 
| 45 | 49 | 
 | 
| 46 | 50 | #if defined(MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO) || defined(MCUBOOT_SWAP_USING_SCRATCH) | 
| 47 | 51 | #include "swap_priv.h" | 
| @@ -240,8 +244,7 @@ int boot_header_scramble_off_sz(const struct flash_area *fa, int slot, size_t *o | 
| 240 | 244 |  * status during the swap of the last sector from primary/secondary (which | 
| 241 | 245 |  * is the first swap operation) and thus only requires space for one swap. | 
| 242 | 246 |  */ | 
| 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) | 
| 245 | 248 | { | 
| 246 | 249 |     return boot_status_entry_sz(min_write_sz) + boot_trailer_info_sz(); | 
| 247 | 250 | } | 
| @@ -427,106 +430,17 @@ boot_write_enc_key(const struct flash_area *fap, uint8_t slot, | 
| 427 | 430 | } | 
| 428 | 431 | #endif | 
| 429 | 432 | 
 | 
| 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 |  | - | 
| 468 | 433 | uint32_t bootutil_max_image_size(struct boot_loader_state *state, const struct flash_area *fap) | 
| 469 | 434 | { | 
| 470 | 435 | #if defined(MCUBOOT_SINGLE_APPLICATION_SLOT) ||      \ | 
| 471 | 436 |     defined(MCUBOOT_FIRMWARE_LOADER) ||              \ | 
| 472 | 437 |     defined(MCUBOOT_SINGLE_APPLICATION_SLOT_RAM_LOAD) | 
| 473 | 438 |     (void) state; | 
| 474 | 439 |     return boot_status_off(fap); | 
| 475 |  | -#elif defined(MCUBOOT_SWAP_USING_SCRATCH) | 
| 476 |  | -    size_t slot_trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); | 
| 477 |  | -    size_t slot_trailer_off = flash_area_get_size(fap) - slot_trailer_sz; | 
| 478 |  | - | 
| 479 |  | -    /* If the trailer doesn't fit in the last sector of the primary or secondary slot, some padding | 
| 480 |  | -     * might have to be inserted between the end of the firmware image and the beginning of the | 
| 481 |  | -     * trailer to ensure there is enough space for the trailer in the scratch area when the last | 
| 482 |  | -     * sector of the secondary will be copied to the scratch area. | 
| 483 |  | -     * | 
| 484 |  | -     * The value of the padding depends on the amount of trailer data that is contained in the first | 
| 485 |  | -     * trailer containing part of the trailer in the primary and secondary slot. | 
| 486 |  | -     */ | 
| 487 |  | -    size_t trailer_sector_primary_end_off = | 
| 488 |  | -        get_first_trailer_sector_end_off(state, BOOT_PRIMARY_SLOT, slot_trailer_sz); | 
| 489 |  | -    size_t trailer_sector_secondary_end_off = | 
| 490 |  | -        get_first_trailer_sector_end_off(state, BOOT_SECONDARY_SLOT, slot_trailer_sz); | 
| 491 |  | - | 
| 492 |  | -    size_t trailer_sz_in_first_sector; | 
| 493 |  | - | 
| 494 |  | -    if (trailer_sector_primary_end_off > trailer_sector_secondary_end_off) { | 
| 495 |  | -        trailer_sz_in_first_sector = trailer_sector_primary_end_off - slot_trailer_off; | 
| 496 |  | -    } else { | 
| 497 |  | -        trailer_sz_in_first_sector = trailer_sector_secondary_end_off - slot_trailer_off; | 
| 498 |  | -    } | 
| 499 |  | - | 
| 500 |  | -    size_t trailer_padding = 0; | 
| 501 |  | -    size_t scratch_trailer_sz = boot_scratch_trailer_sz(BOOT_WRITE_SZ(state)); | 
| 502 |  | - | 
| 503 |  | -    if (scratch_trailer_sz > trailer_sz_in_first_sector) { | 
| 504 |  | -        trailer_padding = scratch_trailer_sz - trailer_sz_in_first_sector; | 
| 505 |  | -    } | 
| 506 |  | - | 
| 507 |  | -    return slot_trailer_off - trailer_padding; | 
| 508 |  | -#elif defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET) | 
|  | 440 | +#elif defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_OFFSET) \ | 
|  | 441 | +      || defined(MCUBOOT_SWAP_USING_SCRATCH) | 
| 509 | 442 |     (void) fap; | 
| 510 |  | - | 
| 511 |  | -    /* The slot whose size is used to compute the maximum image size must be the one containing the | 
| 512 |  | -     * padding required for the swap. */ | 
| 513 |  | -#ifdef MCUBOOT_SWAP_USING_MOVE | 
| 514 |  | -    size_t slot = BOOT_PRIMARY_SLOT; | 
| 515 |  | -#else | 
| 516 |  | -    size_t slot = BOOT_SECONDARY_SLOT; | 
| 517 |  | -#endif | 
| 518 |  | - | 
| 519 |  | -    const struct flash_area *fap_padded_slot = BOOT_IMG_AREA(state, slot); | 
| 520 |  | -    assert(fap_padded_slot != NULL); | 
| 521 |  | - | 
| 522 |  | -    size_t trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); | 
| 523 |  | -    size_t sector_sz = boot_img_sector_size(state, slot, 0); | 
| 524 |  | -    size_t padding_sz = sector_sz; | 
| 525 |  | - | 
| 526 |  | -    /* The trailer size needs to be sector-aligned */ | 
| 527 |  | -    trailer_sz = ALIGN_UP(trailer_sz, sector_sz); | 
| 528 |  | - | 
| 529 |  | -    return flash_area_get_size(fap_padded_slot) - trailer_sz - padding_sz; | 
|  | 443 | +    return app_max_size(state); | 
| 530 | 444 | #elif defined(MCUBOOT_OVERWRITE_ONLY) | 
| 531 | 445 |     (void) state; | 
| 532 | 446 |     return boot_swap_info_off(fap); | 
|  | 
0 commit comments