Skip to content

Commit 544b25a

Browse files
committed
cmake: Check image boundaries in merged slot
Add routine that checks if all merged images are configured within the boundary of the merged partition. Ref: NCSDK-35612 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 022ae4b commit 544b25a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

cmake/sysbuild/mcuboot_nrf54h20.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ if(SB_CONFIG_MCUBOOT_SIGN_MERGED_BINARY)
99
set(MERGED_IMAGES_HEX "mcuboot_merged.hex")
1010
UpdateableImage_Get(images GROUP "DEFAULT")
1111

12+
check_image_boundaries("slot0_partition" "${images}")
1213
merge_images_nrf54h20(${MERGED_IMAGES_HEX} "${images}")
1314
# Since all bootloader-enabled images are merged, disable programming subimages.
1415
list(REMOVE_ITEM images "${DEFAULT_IMAGE}")
@@ -19,6 +20,7 @@ if(SB_CONFIG_MCUBOOT_SIGN_MERGED_BINARY)
1920
UpdateableImage_Get(variants GROUP "VARIANT")
2021

2122
if(variants)
23+
check_image_boundaries("slot1_partition" "${variants}")
2224
merge_images_nrf54h20(${MERGED_IMAGES_SECONDARY_HEX} "${variants}")
2325
list(REMOVE_ITEM variants "mcuboot_secondary_app")
2426
disable_programming_nrf54h20("${variants}")

cmake/sysbuild/sign_nrf54h20.cmake

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@
22
#
33
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
44

5+
function(check_image_boundaries merged_partition images)
6+
# Predefine the MCUboot header size.
7+
set(MCUBOOT_HEADER_SIZE 0x800)
8+
9+
# Fetch merged slot details from the mcuboot image.
10+
dt_chosen(flash_node TARGET mcuboot PROPERTY "zephyr,flash")
11+
dt_nodelabel(slot_path TARGET mcuboot NODELABEL "${merged_partition}" REQUIRED)
12+
dt_reg_addr(slot_addr TARGET mcuboot PATH ${slot_path})
13+
dt_reg_size(slot_size TARGET mcuboot PATH ${slot_path})
14+
15+
# Calculate boundaries of the usable area.
16+
sysbuild_get(mcuboot_image_footer_size IMAGE mcuboot CACHE)
17+
math(EXPR slot_max_addr "${slot_addr} + ${slot_size} - ${mcuboot_image_footer_size}" OUTPUT_FORMAT HEXADECIMAL)
18+
math(EXPR slot_min_addr "${slot_addr} + ${MCUBOOT_HEADER_SIZE}" OUTPUT_FORMAT HEXADECIMAL)
19+
20+
# Iterate over images and check that they fit in the merged slots.
21+
foreach(image ${images})
22+
set(start_offset)
23+
set(end_offset)
24+
sysbuild_get(start_offset IMAGE ${image} VAR CONFIG_ROM_START_OFFSET
25+
KCONFIG)
26+
sysbuild_get(end_offset IMAGE ${image} VAR CONFIG_ROM_END_OFFSET
27+
KCONFIG)
28+
dt_chosen(code_flash TARGET ${image} PROPERTY "zephyr,code-partition")
29+
dt_reg_addr(code_addr TARGET ${image} PATH ${code_flash})
30+
dt_reg_size(code_size TARGET ${image} PATH ${code_flash})
31+
32+
math(EXPR code_end_addr "${code_addr} + ${code_size} - ${end_offset}" OUTPUT_FORMAT HEXADECIMAL)
33+
math(EXPR code_start_addr "${code_addr} + ${start_offset}" OUTPUT_FORMAT HEXADECIMAL)
34+
35+
if((${code_end_addr} GREATER ${slot_max_addr}) OR
36+
(${code_start_addr} LESS ${slot_min_addr}))
37+
message(FATAL_ERROR "Variant image ${image} "
38+
"(${code_start_addr}, ${code_end_addr}) "
39+
"does not fit in the merged ${merged_partition} "
40+
"(${slot_min_addr}, ${slot_max_addr})")
41+
return()
42+
endif()
43+
endforeach()
44+
endfunction()
45+
546
function(merge_images_nrf54h20 output_artifact images)
647
find_program(MERGEHEX mergehex.py HINTS ${ZEPHYR_BASE}/scripts/build/ NAMES
748
mergehex NAMES_PER_DIR)

0 commit comments

Comments
 (0)