Skip to content

Commit

Permalink
zephyr: Add estimated image footer size to cache in sysbuild
Browse files Browse the repository at this point in the history
Adds MCUboot's estimated overhead footer size to the application's
cache when using sysbuild, this allows that information to be
propagated to applications which can use the information to reduce
the available size for an application, preventing the MCUboot
error of image too large to swap.

Signed-off-by: Jamie McCrae <[email protected]>
  • Loading branch information
nordicjm committed Oct 30, 2023
1 parent 304fd41 commit 3f178d2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
69 changes: 69 additions & 0 deletions boot/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,75 @@ if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQ
zephyr_library_sources(${GENERATED_ENCKEY})
endif()

function(mathup num align result)
math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
set(${result} "${out}" PARENT_SCOPE)
endfunction()

if(SYSBUILD)
if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
dt_chosen(chosen_flash PROPERTY "zephyr,flash")
dt_prop(erase_size PATH "${chosen_flash}" PROPERTY "erase-block-size")
dt_prop(write_size PATH "${chosen_flash}" PROPERTY "write-block-size")

if(write_size LESS 8)
set(write_size 8)
endif()

set(key_size 0)

if(CONFIG_BOOT_ENCRYPT_RSA OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
#ifdef MCUBOOT_AES_256
#set(BOOT_ENC_KEY_SIZE 32)
#else
set(BOOT_ENC_KEY_SIZE 16)
#endif

if(CONFIG_BOOT_SWAP_SAVE_ENCTLV)
if(CONFIG_BOOT_ENCRYPT_RSA)
set(key_size 256)
elseif(CONFIG_BOOT_ENCRYPT_EC256)
math(EXPR key_size "65 + 32 + ${BOOT_ENC_KEY_SIZE}")
elseif(CONFIG_BOOT_ENCRYPT_X25519)
math(EXPR key_size "32 + 32 + ${BOOT_ENC_KEY_SIZE}")
else()
math(EXPR key_size "${BOOT_ENC_KEY_SIZE} + 8")
endif()
else()
set(key_size "${BOOT_ENC_KEY_SIZE}")
endif()

mathup(${key_size} ${write_size} key_size)
math(EXPR key_size "${key_size} * 2")
endif()

set(boot_magic_size 16)
mathup(${boot_magic_size} ${write_size} boot_magic_size)

math(EXPR boot_swap_data_size "${write_size} * 4")

if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE)
math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (3 * ${write_size})")
else()
set(boot_status_data_size 0)
endif()

# 352 is rough for trailer TLV
math(EXPR required_size "${key_size} + ${boot_magic_size} + ${boot_swap_data_size} + ${boot_status_data_size} + 352")

mathup(${required_size} ${erase_size} required_size)

if(CONFIG_BOOT_SWAP_USING_MOVE)
math(EXPR required_size "${required_size} + ${erase_size}")
endif()
else()
set(required_size 0)
endif()

message(WARNING "required size: ${required_size}")
set(mcuboot_image_footer_size ${required_size} CACHE INTERNAL "" FORCE)
endif()

if(CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
zephyr_library_sources(
${BOOT_DIR}/zephyr/arm_cleanup.c
Expand Down
40 changes: 40 additions & 0 deletions boot/zephyr/sysbuild/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function(mathup num align result)
math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
set(${result} "${out}" PARENT_SCOPE)
endfunction()

function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_image_cmake)
cmake_parse_arguments(POST_IMAGE_CMAKE "" "IMAGE" "IMAGES" ${ARGN})

if(NOT "${POST_IMAGE_CMAKE_IMAGE}" STREQUAL "mcuboot")
return()
endif()

set_property(
DIRECTORY APPEND PROPERTY
CMAKE_CONFIGURE_DEPENDS
${CMAKE_BINARY_DIR}/mcuboot/CMakeCache.txt
${CMAKE_BINARY_DIR}/mcuboot/zephyr/.config
)
endfunction(${SYSBUILD_CURRENT_MODULE_NAME}_pre_image_cmake)

function(${SYSBUILD_CURRENT_MODULE_NAME}_post_image_cmake)
cmake_parse_arguments(POST_IMAGE_CMAKE "" "IMAGE" "IMAGES" ${ARGN})

if(NOT "${POST_IMAGE_CMAKE_IMAGE}" STREQUAL "mcuboot")
return()
endif()

foreach(image ${IMAGES})
set(app_type)
get_property(app_type TARGET ${image} PROPERTY APP_TYPE)

if("${app_type}" STREQUAL "MAIN")
sysbuild_get(mcuboot_image_footer_size IMAGE mcuboot CACHE)
math(EXPR mcuboot_image_footer_size "${mcuboot_image_footer_size}" OUTPUT_FORMAT HEXADECIMAL)

set_property(TARGET ${image} APPEND_STRING PROPERTY CONFIG "CONFIG_FLASH_LOAD_OVERHEAD_SIZE=${mcuboot_image_footer_size}\n")
return()
endif()
endforeach()
endfunction(${SYSBUILD_CURRENT_MODULE_NAME}_post_image_cmake)
1 change: 1 addition & 0 deletions zephyr/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ samples:
- boot/zephyr
build:
cmake: ./boot/bootutil/zephyr
sysbuild-cmake: boot/zephyr/sysbuild

0 comments on commit 3f178d2

Please sign in to comment.