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 <jamie.mccrae@nordicsemi.no>
diff --git a/boot/zephyr/sysbuild/CMakeLists.txt b/boot/zephyr/sysbuild/CMakeLists.txt
new file mode 100644
index 0000000..adcd744
--- /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_ROM_END_OFFSET=${mcuboot_image_footer_size}\n")
+      return()
+    endif()
+  endforeach()
+endfunction(${SYSBUILD_CURRENT_MODULE_NAME}_post_image_cmake)