Skip to content

Commit 0474ea0

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 c23b117 commit 0474ea0

File tree

11 files changed

+94
-1
lines changed

11 files changed

+94
-1
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
@@ -4,6 +4,47 @@
44

55
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/bootloader_dts_utils.cmake)
66

7+
function(check_image_boundaries merged_partition images)
8+
# Predefine the MCUboot header size.
9+
set(MCUBOOT_HEADER_SIZE 0x800)
10+
11+
# Fetch merged slot details from the mcuboot image.
12+
dt_chosen(flash_node TARGET mcuboot PROPERTY "zephyr,flash")
13+
dt_nodelabel(slot_path TARGET mcuboot NODELABEL "${merged_partition}" REQUIRED)
14+
dt_partition_addr(slot_addr PATH "${slot_path}" TARGET mcuboot REQUIRED)
15+
dt_reg_size(slot_size TARGET mcuboot PATH ${slot_path})
16+
17+
# Calculate boundaries of the usable area.
18+
sysbuild_get(mcuboot_image_footer_size IMAGE mcuboot CACHE)
19+
math(EXPR slot_max_addr "${slot_addr} + ${slot_size} - ${mcuboot_image_footer_size}" OUTPUT_FORMAT HEXADECIMAL)
20+
math(EXPR slot_min_addr "${slot_addr} + ${MCUBOOT_HEADER_SIZE}" OUTPUT_FORMAT HEXADECIMAL)
21+
22+
# Iterate over images and check that they fit in the merged slots.
23+
foreach(image ${images})
24+
set(start_offset)
25+
set(end_offset)
26+
sysbuild_get(start_offset IMAGE ${image} VAR CONFIG_ROM_START_OFFSET
27+
KCONFIG)
28+
sysbuild_get(end_offset IMAGE ${image} VAR CONFIG_ROM_END_OFFSET
29+
KCONFIG)
30+
dt_chosen(code_flash TARGET ${image} PROPERTY "zephyr,code-partition")
31+
dt_partition_addr(code_addr PATH "${code_flash}" TARGET ${image} REQUIRED)
32+
dt_reg_size(code_size TARGET ${image} PATH ${code_flash})
33+
34+
math(EXPR code_end_addr "${code_addr} + ${code_size} - ${end_offset}" OUTPUT_FORMAT HEXADECIMAL)
35+
math(EXPR code_start_addr "${code_addr} + ${start_offset}" OUTPUT_FORMAT HEXADECIMAL)
36+
37+
if((${code_end_addr} GREATER ${slot_max_addr}) OR
38+
(${code_start_addr} LESS ${slot_min_addr}))
39+
message(FATAL_ERROR "Variant image ${image} "
40+
"(${code_start_addr}, ${code_end_addr}) "
41+
"does not fit in the merged ${merged_partition} "
42+
"(${slot_min_addr}, ${slot_max_addr})")
43+
return()
44+
endif()
45+
endforeach()
46+
endfunction()
47+
748
function(merge_images_nrf54h20 output_artifact images)
849
find_program(MERGEHEX mergehex.py HINTS ${ZEPHYR_BASE}/scripts/build/ NAMES
950
mergehex NAMES_PER_DIR)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Add a room for the MCUboot trailer
8+
CONFIG_ROM_END_OFFSET=224
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Add a room for the MCUboot trailer
8+
CONFIG_ROM_END_OFFSET=224
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Add a room for the MCUboot trailer
8+
CONFIG_ROM_END_OFFSET=224
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Add a room for the MCUboot trailer
8+
CONFIG_ROM_END_OFFSET=224
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# Add a room for the MCUboot trailer
8+
CONFIG_ROM_END_OFFSET=224

samples/zephyr/subsys/mgmt/mcumgr/smp_svr/sysbuild/ipc_radio.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CONFIG_MCUBOOT_IMGTOOL_UUID_VID=y
22
CONFIG_MCUBOOT_IMGTOOL_UUID_VID_NAME="nordicsemi.com"
33
CONFIG_MCUBOOT_IMGTOOL_UUID_CID=y
44
CONFIG_MCUBOOT_IMGTOOL_UUID_CID_NAME="nRF54H20_sample_rad"
5+
6+
# Add a room for the MCUboot trailer
7+
CONFIG_ROM_END_OFFSET=224

subsys/bootloader/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,9 @@ config MCUBOOT_BOOTLOADER_SIGNATURE_TYPE_PURE
170170
help
171171
This is a Kconfig which is informative only, the value should not be changed.
172172

173+
config NCS_MCUBOOT_BOOTLOADER_SIGN_MERGED_BINARY
174+
bool "Sign merged binary instead of individual images"
175+
help
176+
This is a Kconfig which is informative only, the value should not be changed.
177+
173178
endmenu

sysbuild/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
693693
if(SB_CONFIG_MCUBOOT_SIGN_MERGED_BINARY AND SB_CONFIG_SOC_NRF54H20)
694694
UpdateableImage_Get(images ALL)
695695
foreach(image ${images})
696+
set_config_bool(${image} CONFIG_NCS_MCUBOOT_BOOTLOADER_SIGN_MERGED_BINARY
697+
true)
696698
set(${image}_SIGNING_SCRIPT
697699
"${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/image_signing_nrf54h20.cmake"
698700
CACHE INTERNAL "MCUboot signing script" FORCE)

0 commit comments

Comments
 (0)