From 3f178d2a1f4c18e842990a6e96cf15dd60f3ba91 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Mon, 30 Oct 2023 11:59:57 +0000 Subject: [PATCH] zephyr: Add estimated image footer size to cache in sysbuild 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 --- boot/zephyr/CMakeLists.txt | 69 +++++++++++++++++++++++++++++ boot/zephyr/sysbuild/CMakeLists.txt | 40 +++++++++++++++++ zephyr/module.yml | 1 + 3 files changed, 110 insertions(+) create mode 100644 boot/zephyr/sysbuild/CMakeLists.txt diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt index 159ef5d3de..692d9acc32 100644 --- a/boot/zephyr/CMakeLists.txt +++ b/boot/zephyr/CMakeLists.txt @@ -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 diff --git a/boot/zephyr/sysbuild/CMakeLists.txt b/boot/zephyr/sysbuild/CMakeLists.txt new file mode 100644 index 0000000000..af8961a05c --- /dev/null +++ b/boot/zephyr/sysbuild/CMakeLists.txt @@ -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) diff --git a/zephyr/module.yml b/zephyr/module.yml index c4293e3877..014a219567 100644 --- a/zephyr/module.yml +++ b/zephyr/module.yml @@ -2,3 +2,4 @@ samples: - boot/zephyr build: cmake: ./boot/bootutil/zephyr + sysbuild-cmake: boot/zephyr/sysbuild