Skip to content

Commit 80e2ed6

Browse files
nordicjmeivindj-nordic
authored andcommitted
sysbuild: Add initial files
Adds files for setting BM up via sysbuild Signed-off-by: Jamie McCrae <[email protected]>
1 parent 1c14263 commit 80e2ed6

File tree

12 files changed

+479
-10
lines changed

12 files changed

+479
-10
lines changed

CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
zephyr_include_directories(include)
7+
if(CONFIG_NCS_BM)
8+
zephyr_include_directories(include)
89

9-
include(cmake/version.cmake)
10+
include(cmake/version.cmake)
1011

11-
add_subdirectory(drivers)
12-
add_subdirectory(lib)
13-
add_subdirectory(subsys)
12+
add_subdirectory(drivers)
13+
add_subdirectory(lib)
14+
add_subdirectory(subsys)
15+
endif()

Kconfig.nrf_bm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
config NRF_BM
8-
bool
7+
config NCS_BM
8+
bool "NCS Bare Metal"
99
default y
1010

11-
if NRF_BM
11+
if NCS_BM
1212

1313
# zephyr-keep-sorted-start
1414
rsource "drivers/Kconfig"
@@ -17,4 +17,4 @@ rsource "samples/Kconfig"
1717
rsource "subsys/Kconfig"
1818
# zephyr-keep-sorted-stop
1919

20-
endif # NRF_BM
20+
endif # NCS_BM
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
3+
4+
function(bm_install_tasks output_hex output_bin)
5+
set(dependencies ${PROJECT_BINARY_DIR}/.config)
6+
7+
find_program(IMGTOOL imgtool.py HINTS ${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/ NAMES imgtool NAMES_PER_DIR)
8+
set(keyfile "${SB_CONFIG_BM_BOOTLOADER_MCUBOOT_SIGNATURE_KEY_FILE}")
9+
string(CONFIGURE "${keyfile}" keyfile)
10+
11+
if(NOT "${SB_CONFIG_BM_BOOTLOADER_MCUBOOT_SIGNATURE_TYPE_NONE}")
12+
# Check for misconfiguration.
13+
if("${keyfile}" STREQUAL "")
14+
# No signature key file, no signed binaries. No error, though:
15+
# this is the documented behavior.
16+
message(WARNING "Neither SB_CONFIG_BM_BOOTLOADER_MCUBOOT_SIGNATURE_TYPE_NONE or "
17+
"SB_CONFIG_BOOT_BM_BOOTLOADER_MCUBOOT_SIGNATURE_KEY_FILE are set, the generated build will not be "
18+
"bootable by MCUboot unless it is signed manually/externally.")
19+
return()
20+
endif()
21+
endif()
22+
23+
# No imgtool, no signed binaries.
24+
if(NOT DEFINED IMGTOOL)
25+
message(FATAL_ERROR "Can't sign images for MCUboot: can't find imgtool. To fix, install imgtool with pip3, or add the mcuboot repository to the west manifest and ensure it has a scripts/imgtool.py file.")
26+
return()
27+
endif()
28+
29+
# Softdevice and firmware loader
30+
list(APPEND dependencies installer_extra_byproducts mcuboot_extra_byproducts)
31+
32+
sysbuild_get(installer_binary_dir IMAGE installer VAR APPLICATION_BINARY_DIR CACHE)
33+
sysbuild_get(installer_kernel_name IMAGE installer VAR CONFIG_KERNEL_BIN_NAME KCONFIG)
34+
set(input ${installer_binary_dir}/zephyr/${installer_kernel_name}.hex)
35+
list(APPEND input ${PROJECT_BINARY_DIR}/../metadata.hex)
36+
list(APPEND input ${PROJECT_BINARY_DIR}/../combined_signed_update_images.hex)
37+
set(output_file ${PROJECT_BINARY_DIR}/../installer_merged.hex)
38+
39+
# Merge files
40+
add_custom_target(combined_images_target
41+
DEPENDS ${output_file}
42+
)
43+
44+
add_custom_command(OUTPUT ${output_file}
45+
COMMAND
46+
${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/build/mergehex.py -o ${output_file} ${input}
47+
DEPENDS ${input} ${dependencies}
48+
)
49+
50+
# Fetch devicetree details for flash and slot information
51+
dt_chosen(flash_node TARGET ${DEFAULT_IMAGE} PROPERTY "zephyr,flash")
52+
dt_prop(write_block_size TARGET ${DEFAULT_IMAGE} PATH "${flash_node}" PROPERTY "write-block-size")
53+
54+
if(NOT write_block_size)
55+
set(write_block_size 4)
56+
message(WARNING "slot0_partition write block size devicetree parameter is missing, assuming write block size is 4")
57+
endif()
58+
59+
sysbuild_get(CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION IMAGE ${DEFAULT_IMAGE} VAR CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION KCONFIG)
60+
sysbuild_get(CONFIG_ROM_START_OFFSET IMAGE ${DEFAULT_IMAGE} VAR CONFIG_ROM_START_OFFSET KCONFIG)
61+
sysbuild_get(CONFIG_FLASH_LOAD_SIZE IMAGE ${DEFAULT_IMAGE} VAR CONFIG_FLASH_LOAD_SIZE KCONFIG)
62+
# sysbuild_get(CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION IMAGE ${DEFAULT_IMAGE} VAR CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION KCONFIG)
63+
64+
# Custom TLV 0x0a is set to 0x01 to indicate that this is an installer image
65+
set(imgtool_sign ${PYTHON_EXECUTABLE} ${IMGTOOL} sign --version ${CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION} --align ${write_block_size} --slot-size ${CONFIG_FLASH_LOAD_SIZE} --header-size ${CONFIG_ROM_START_OFFSET} --custom-tlv 0xa0 0x01)
66+
67+
# if(CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION)
68+
# set(imgtool_args --security-counter ${CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE})
69+
# endif()
70+
71+
if(NOT "${keyfile}" STREQUAL "")
72+
set(imgtool_args -k "${keyfile}" ${imgtool_args})
73+
endif()
74+
75+
# Set up outputs
76+
if(SB_CONFIG_INSTALLER_HEX_OUTPUT)
77+
add_custom_target(signed_installer_target
78+
ALL
79+
DEPENDS ${output_hex} ${output_bin}
80+
)
81+
82+
add_custom_command(OUTPUT ${output_hex}
83+
COMMAND
84+
${imgtool_sign} ${imgtool_args} ${output_file} ${output_hex}
85+
DEPENDS ${output_file} ${dependencies}
86+
)
87+
else()
88+
add_custom_target(signed_installer_target
89+
ALL
90+
DEPENDS ${output_bin}
91+
)
92+
endif()
93+
94+
add_custom_command(OUTPUT ${output_bin}
95+
COMMAND
96+
${imgtool_sign} ${imgtool_args} ${output_file} ${output_bin}
97+
DEPENDS ${output_file} ${dependencies}
98+
)
99+
endfunction()
100+
101+
bm_install_tasks(${CMAKE_BINARY_DIR}/installer_softdevice_firmware_loader.hex ${CMAKE_BINARY_DIR}/installer_softdevice_firmware_loader.bin)

subsys/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
# zephyr-keep-sorted-start
88
add_subdirectory(bluetooth)
9-
add_subdirectory_ifdef(CONFIG_FLASH_MAP storage/flash_map)
9+
add_subdirectory(bm_installs)
1010
add_subdirectory(logging)
1111
add_subdirectory_ifdef(CONFIG_NRF_SDH softdevice_handler)
1212
add_subdirectory_ifdef(CONFIG_SOFTDEVICE softdevice)
13+
add_subdirectory_ifdef(CONFIG_FLASH_MAP storage/flash_map)
1314
# zephyr-keep-sorted-stop

subsys/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ menu "Subsystems"
88

99
# zephyr-keep-sorted-start
1010
rsource "bluetooth/Kconfig"
11+
rsource "bm_installs/Kconfig"
1112
rsource "logging/Kconfig"
1213
rsource "softdevice/Kconfig"
1314
rsource "softdevice_handler/Kconfig"

sysbuild/CMakeLists.txt

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
function(bm_install_setup)
8+
if(NOT DEFINED SB_CONFIG_BM_BOOTLOADER_NONE)
9+
ExternalZephyrProject_Add(
10+
APPLICATION ${SB_CONFIG_BM_BOOTLOADER_IMAGE_NAME}
11+
SOURCE_DIR ${SB_CONFIG_BM_BOOTLOADER_IMAGE_PATH}
12+
)
13+
14+
# Place MCUBoot first in list to ensure it is configured and flashed before other images.
15+
sysbuild_add_dependencies(CONFIGURE ${DEFAULT_IMAGE} ${SB_CONFIG_BM_BOOTLOADER_IMAGE_NAME})
16+
sysbuild_add_dependencies(FLASH ${DEFAULT_IMAGE} ${SB_CONFIG_BM_BOOTLOADER_IMAGE_NAME})
17+
18+
if(NOT DEFINED SB_CONFIG_BM_FIRMWARE_LOADER_NONE)
19+
ExternalZephyrProject_Add(
20+
APPLICATION ${SB_CONFIG_BM_FIRMWARE_LOADER_IMAGE_NAME}
21+
SOURCE_DIR ${SB_CONFIG_BM_FIRMWARE_LOADER_IMAGE_PATH}
22+
)
23+
24+
add_overlay_dts(${SB_CONFIG_BM_FIRMWARE_LOADER_IMAGE_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/image_configurations/FIRMWARE_LOADER_image_default.overlay)
25+
endif()
26+
endif()
27+
endfunction()
28+
29+
function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
30+
cmake_parse_arguments(PRE_CMAKE "" "" "IMAGES" ${ARGN})
31+
32+
if(NOT DEFINED SB_CONFIG_NCS_BM)
33+
# Disable bare-metal on all images
34+
foreach(image ${PRE_CMAKE_IMAGES})
35+
set_config_int(${image} CONFIG_NCS_BM n)
36+
endforeach()
37+
38+
return()
39+
endif()
40+
41+
if(NOT DEFINED SB_CONFIG_BM_BOOTLOADER_NONE)
42+
set(bm_install_images 1)
43+
44+
if(NOT DEFINED SB_CONFIG_BM_FIRMWARE_LOADER_NONE)
45+
# Firmware loader present
46+
set_target_properties(${SB_CONFIG_BM_FIRMWARE_LOADER_IMAGE_NAME} PROPERTIES
47+
IMAGE_CONF_SCRIPT ${ZEPHYR_NRF_BM_MODULE_DIR}/sysbuild/image_configurations/MAIN_image_default.cmake
48+
)
49+
50+
set(bm_install_images 2)
51+
endif()
52+
53+
# Use BM-supplied bootloader image configuration file
54+
get_property(tmp_conf_scripts TARGET ${SB_CONFIG_BM_BOOTLOADER_IMAGE_NAME} PROPERTY IMAGE_CONF_SCRIPT)
55+
set(bootloader_script ${ZEPHYR_NRF_BM_MODULE_DIR}/sysbuild/image_configurations/BOOTLOADER_image_default.cmake)
56+
57+
if(NOT bootloader_script IN_LIST tmp_conf_scripts)
58+
list(APPEND tmp_conf_scripts ${bootloader_script})
59+
set_target_properties(${SB_CONFIG_BM_BOOTLOADER_IMAGE_NAME} PROPERTIES IMAGE_CONF_SCRIPT "${tmp_conf_scripts}")
60+
endif()
61+
else()
62+
set(bm_install_images 0)
63+
endif()
64+
65+
set_target_properties(${DEFAULT_IMAGE} PROPERTIES
66+
IMAGE_CONF_SCRIPT ${ZEPHYR_NRF_BM_MODULE_DIR}/sysbuild/image_configurations/MAIN_image_default.cmake
67+
)
68+
69+
math(EXPR metadata_area_size "${SB_CONFIG_METADATA_SIZE}" OUTPUT_FORMAT DECIMAL)
70+
math(EXPR metadata_slots "${metadata_area_size} / ${SB_CONFIG_METADATA_ENTRY_SIZE}" OUTPUT_FORMAT DECIMAL)
71+
72+
foreach(image ${PRE_CMAKE_IMAGES})
73+
set_config_int(${image} CONFIG_BM_INSTALL_IMAGES ${bm_install_images})
74+
set_config_int(${image} CONFIG_BM_INSTALL_ENTRY_SIZE ${SB_CONFIG_METADATA_ENTRY_SIZE})
75+
set_config_int(${image} CONFIG_BM_INSTALL_ENTRIES ${metadata_slots})
76+
77+
if(image STREQUAL "installer")
78+
set_config_int(${image} CONFIG_BM_METADATA_WRITE y)
79+
else()
80+
set_config_int(${image} CONFIG_BM_METADATA_WRITE n)
81+
endif()
82+
83+
if(NOT DEFINED SB_CONFIG_BM_BOOTLOADER_NONE AND NOT image STREQUAL "mcuboot")
84+
if(SB_CONFIG_SOC_SERIES_NRF54LX)
85+
set_config_int(installer CONFIG_ROM_START_OFFSET 0x800)
86+
else()
87+
set_config_int(installer CONFIG_ROM_START_OFFSET 0x200)
88+
endif()
89+
endif()
90+
endforeach()
91+
endfunction()
92+
93+
function(${SYSBUILD_CURRENT_MODULE_NAME}_post_cmake)
94+
if(NOT DEFINED SB_CONFIG_NCS_BM)
95+
return()
96+
endif()
97+
98+
cmake_parse_arguments(PRE_CMAKE "" "" "IMAGES" ${ARGN})
99+
100+
if(NOT SB_CONFIG_SOFTDEVICE_NONE)
101+
set(dependency_targets)
102+
103+
foreach(image ${PRE_CMAKE_IMAGES})
104+
list(APPEND dependency_targets ${image}_extra_byproducts)
105+
endforeach()
106+
107+
# Installer metadata
108+
add_custom_target(metadata_generation
109+
ALL
110+
DEPENDS ${CMAKE_BINARY_DIR}/metadata.hex ${CMAKE_BINARY_DIR}/metadata.bin
111+
${CMAKE_BINARY_DIR}/combined_signed_update_images.hex
112+
)
113+
114+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/metadata.hex ${CMAKE_BINARY_DIR}/metadata.bin
115+
${CMAKE_BINARY_DIR}/combined_signed_update_images.hex
116+
COMMAND
117+
${PYTHON_EXECUTABLE}
118+
${ZEPHYR_NRF_BM_MODULE_DIR}/scripts/generate_metadata.py
119+
--build-dir ${CMAKE_BINARY_DIR}
120+
DEPENDS ${dependency_targets} ../zephyr/.config
121+
)
122+
123+
if(NOT SB_CONFIG_BM_FIRMWARE_LOADER_NONE)
124+
include(${ZEPHYR_NRF_BM_MODULE_DIR}/cmake/sysbuild/image_signing_installer.cmake)
125+
endif()
126+
endif()
127+
endfunction()
128+
129+
if(SB_CONFIG_NCS_BM)
130+
bm_install_setup()
131+
endif()

0 commit comments

Comments
 (0)