forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
358 lines (299 loc) · 14.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#
# Copyright (c) 2023 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
# Usage:
# configure_cache(IMAGE <name>)
#
# This function is similar in nature to configure file, except that it takes
# an image as argument to work on the CMakeCache of said image.
#
# The CMake cache of the image will only be re-written in case there are
# variables which has been updated as part of the call.
#
# Only variable of the form `@VAR_NAME@` will be updated.
#
# IMAGE: image name identifying the cache to update.
#
function(configure_cache)
set(single_args IMAGE)
cmake_parse_arguments(CONFIGURE_CACHE "" "${single_args}" "" ${ARGN})
zephyr_check_arguments_required(configure_cache CONFIGURE_CACHE IMAGE)
ExternalProject_Get_Property(${CONFIGURE_CACHE_IMAGE} BINARY_DIR)
# Copy the cache file to avoid a dependency to the image's cache file.
# A dependency on the image's cache file cause unneccessary CMake re-runs for
# all sysbuild images whenever a setting in the cache changes, even for not
# sysbuild relevant changes.
zephyr_file_copy("${BINARY_DIR}/CMakeCache.txt" "${BINARY_DIR}/CMakeCache.tmp" ONLY_IF_DIFFERENT)
configure_file("${BINARY_DIR}/CMakeCache.tmp" "${BINARY_DIR}/CMakeCache.txt.tmp" @ONLY)
zephyr_file_copy("${BINARY_DIR}/CMakeCache.txt.tmp" "${BINARY_DIR}/CMakeCache.txt" ONLY_IF_DIFFERENT)
endfunction()
function(store_ncs_vars)
get_property(ncs_vars DIRECTORY ${SYSBUILD_CURRENT_CMAKE_DIR} PROPERTY VARIABLES)
list(FILTER ncs_vars INCLUDE REGEX "NCS_.*")
foreach(var ${ncs_vars})
set_property(GLOBAL APPEND PROPERTY NCS_VARS ${var})
set_property(GLOBAL PROPERTY ${var} ${${var}})
endforeach()
endfunction()
function(restore_ncs_vars)
get_property(ncs_vars GLOBAL PROPERTY NCS_VARS)
foreach(var ${ncs_vars})
get_property(var_value GLOBAL PROPERTY ${var})
set(${var} ${var_value} PARENT_SCOPE)
endforeach()
endfunction()
function(include_provision_hex)
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/provision_hex.cmake)
endfunction()
function(include_packaging)
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/b0_mcuboot_signing.cmake)
include(${ZEPHYR_NRF_MODULE_DIR}/subsys/bootloader/cmake/packaging.cmake)
endfunction()
function(include_fast_pair_hex)
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/fast_pair_hex.cmake)
endfunction()
function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
cmake_parse_arguments(PRE_CMAKE "" "" "IMAGES" ${ARGN})
restore_ncs_vars()
if(NOT SB_CONFIG_PARTITION_MANAGER)
# Disable any Kconfigs for child images in the main application
foreach(config NCS_SAMPLE_EMPTY_APP_CORE_CHILD_IMAGE NCS_INCLUDE_RPMSG_CHILD_IMAGE NCS_SAMPLE_REMOTE_SHELL_CHILD_IMAGE
NCS_SAMPLE_DTM_REMOTE_HCI_CHILD_IMAGE NCS_SAMPLE_PERIPHERAL_RADIO_TEST_CHILD_IMAGE B0_BUILD_STRATEGY_USE_HEX_FILE
B0_BUILD_STRATEGY_SKIP_BUILD B0_BUILD_STRATEGY_FROM_SOURCE PM_SINGLE_IMAGE MCUBOOT_BUILD_STRATEGY_USE_HEX_FILE
MCUBOOT_BUILD_STRATEGY_SKIP_BUILD MCUBOOT_BUILD_STRATEGY_FROM_SOURCE)
set_config_bool(${DEFAULT_IMAGE} CONFIG_${config} n)
endforeach()
endif()
foreach(image ${PRE_CMAKE_IMAGES})
if(SB_CONFIG_PARTITION_MANAGER)
set_config_bool(${image} CONFIG_PARTITION_MANAGER_ENABLED y)
else()
set_config_bool(${image} CONFIG_PARTITION_MANAGER_ENABLED n)
endif()
if(SB_CONFIG_BUILD_OUTPUT_BIN)
set_config_bool(${image} CONFIG_BUILD_OUTPUT_BIN y)
else()
set_config_bool(${image} CONFIG_BUILD_OUTPUT_BIN n)
endif()
if(SB_CONFIG_BUILD_OUTPUT_HEX)
set_config_bool(${image} CONFIG_BUILD_OUTPUT_HEX y)
else()
set_config_bool(${image} CONFIG_BUILD_OUTPUT_HEX n)
endif()
endforeach()
if(BOARD STREQUAL "nrf9160dk_nrf9160_ns")
set(board_secure nrf9160dk_nrf9160)
elseif(BOARD STREQUAL "thingy91_nrf9160_ns")
set(board_secure thingy91_nrf9160)
elseif(BOARD STREQUAL "thingy91x_nrf9151_ns")
set(board_secure thingy91x_nrf9151)
elseif(BOARD STREQUAL "nrf5340dk_nrf5340_cpuapp_ns")
set(board_secure nrf5340dk_nrf5340_cpuapp)
elseif(BOARD STREQUAL "thingy53_nrf5340_cpuapp_ns")
set(board_secure thingy53_nrf5340_cpuapp)
elseif(BOARD STREQUAL "nrf7002dk_nrf5340_cpuapp_ns")
set(board_secure nrf7002dk_nrf5340_cpuapp)
endif()
if(DEFINED board_secure AND DEFINED BOARD_REVISION)
set(board_secure ${board_secure}@${BOARD_REVISION})
endif()
if(SB_CONFIG_BOOTLOADER_MCUBOOT)
if(SB_CONFIG_PARTITION_MANAGER)
# Make mcuboot a build only target as the main application will flash this from the
# merged hex file
set_target_properties(mcuboot PROPERTIES BUILD_ONLY true)
endif()
set_property(TARGET mcuboot APPEND_STRING PROPERTY CONFIG "CONFIG_UPDATEABLE_IMAGE_NUMBER=${SB_CONFIG_MCUBOOT_UPDATEABLE_IMAGES}\n")
set_property(TARGET ${DEFAULT_IMAGE} APPEND_STRING PROPERTY CONFIG "CONFIG_UPDATEABLE_IMAGE_NUMBER=${SB_CONFIG_MCUBOOT_UPDATEABLE_IMAGES}\n")
# Set MCUboot operating mode in application and MCUboot
# TODO: NCSDK-24488 add RAM LOAD mode
set(mcuboot_modes "CONFIG_SINGLE_APPLICATION_SLOT;CONFIG_BOOT_SWAP_USING_MOVE;CONFIG_BOOT_SWAP_USING_SCRATCH;CONFIG_BOOT_UPGRADE_ONLY;CONFIG_BOOT_DIRECT_XIP;CONFIG_BOOT_DIRECT_XIP_REVERT")
set(application_mcuboot_modes "CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP;CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_WITHOUT_SCRATCH;CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_SCRATCH;CONFIG_MCUBOOT_BOOTLOADER_MODE_OVERWRITE_ONLY;CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP;CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP_WITH_REVERT")
if(SB_CONFIG_MCUBOOT_MODE_SINGLE_APP)
set(mcuboot_mode CONFIG_SINGLE_APPLICATION_SLOT)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP)
elseif(SB_CONFIG_MCUBOOT_MODE_SWAP_WITHOUT_SCRATCH)
set(mcuboot_mode CONFIG_BOOT_SWAP_USING_MOVE)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_WITHOUT_SCRATCH)
elseif(SB_CONFIG_MCUBOOT_MODE_SWAP_SCRATCH)
set(mcuboot_mode CONFIG_BOOT_SWAP_USING_SCRATCH)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_SCRATCH)
elseif(SB_CONFIG_MCUBOOT_MODE_OVERWRITE_ONLY)
set(mcuboot_mode CONFIG_BOOT_UPGRADE_ONLY)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_OVERWRITE_ONLY)
elseif(SB_CONFIG_MCUBOOT_MODE_DIRECT_XIP)
set(mcuboot_mode CONFIG_BOOT_DIRECT_XIP)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP)
elseif(SB_CONFIG_MCUBOOT_MODE_DIRECT_XIP_WITH_REVERT)
set(mcuboot_mode CONFIG_BOOT_DIRECT_XIP;CONFIG_BOOT_DIRECT_XIP_REVERT)
set(application_mcuboot_mode CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP_WITH_REVERT)
endif()
# Apply configuration to MCUboot
foreach(mode ${mcuboot_modes})
if("${mode}" IN_LIST mcuboot_mode)
set_config_bool(mcuboot ${mode} y)
else()
set_config_bool(mcuboot ${mode} n)
endif()
endforeach()
# Apply configuration to application
foreach(mode ${application_mcuboot_modes})
if("${mode}" IN_LIST application_mcuboot_mode)
set_config_bool(${DEFAULT_IMAGE} ${mode} y)
else()
set_config_bool(${DEFAULT_IMAGE} ${mode} n)
endif()
endforeach()
# Check if the board is a non-secure one
string(LENGTH "${BOARD}" board_len)
math(EXPR board_len "${board_len} - 3")
string(SUBSTRING "${BOARD}" ${board_len} 3 ns_board_check)
if("${ns_board_check}" STREQUAL "_ns")
# Configure MCUboot before application so that TF-M can read MCUboot configuration
sysbuild_add_dependencies(CONFIGURE ${DEFAULT_IMAGE} mcuboot)
# Configure MCUBoot to be able to boot TFM
add_overlay_config(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/modules/mcuboot/tfm.conf
)
# Add fault injection protection to MCUBOOT
add_overlay_config(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/modules/mcuboot/fih_low_enable.conf
)
endif()
if(SB_CONFIG_PARTITION_MANAGER)
# Use NCS signing script with support for PM
set(${DEFAULT_IMAGE}_SIGNING_SCRIPT "${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/image_signing.cmake" CACHE INTERNAL "MCUboot signing script" FORCE)
endif()
set(imgtool_extra)
if(SB_CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION)
set(imgtool_extra " --security-counter ${SB_CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE}")
set_property(TARGET ${DEFAULT_IMAGE} APPEND_STRING PROPERTY CONFIG "CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION=${SB_CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION}\n")
set_property(TARGET ${DEFAULT_IMAGE} APPEND_STRING PROPERTY CONFIG "CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_SLOTS=${SB_CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_SLOTS}\n")
set_property(TARGET ${DEFAULT_IMAGE} APPEND_STRING PROPERTY CONFIG "CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE=${SB_CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE}\n")
add_overlay_config(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/modules/mcuboot/hw_counters.conf
)
endif()
if(NOT DEFINED mcuboot_BOARD AND DEFINED board_secure)
# MCUboot must run in secure mode on the nRF9160/nRF5340
set_target_properties(mcuboot PROPERTIES BOARD ${board_secure})
endif()
endif()
if(SB_CONFIG_SECURE_BOOT)
if(NOT SB_CONFIG_SECURE_BOOT_APPCORE AND NOT SB_CONFIG_SECURE_BOOT_NETCORE)
# Error out early, this shortcuts to a later error which complains that the secure boot image name is missing
message(FATAL_ERROR "Error: No secure boot options have been selected, at least one core must be selected for secure boot to function.")
endif()
if(SB_CONFIG_SECURE_BOOT_APPCORE)
if(NOT DEFINED b0_BOARD AND DEFINED board_secure)
# b0 must run in secure mode on the nRF9160/nRF5340
set_target_properties(b0 PROPERTIES BOARD ${board_secure})
if(SB_CONFIG_BOOTLOADER_MCUBOOT)
set_target_properties(s1_image PROPERTIES BOARD ${board_secure})
endif()
endif()
if(SB_CONFIG_BOOTLOADER_MCUBOOT)
add_overlay_config(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/sysbuild/mcuboot_enable_secure_bootloader.overlay
)
add_overlay_config(
mcuboot
${ZEPHYR_MCUBOOT_MODULE_DIR}/boot/zephyr/external_crypto.conf
)
endif()
endif()
if(SB_CONFIG_SECURE_BOOT_NETCORE AND SB_CONFIG_BOOTLOADER_MCUBOOT)
if(SB_CONFIG_NETCORE_APP_UPDATE)
set_config_bool(mcuboot CONFIG_PCD_APP y)
add_overlay_config(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/subsys/partition_manager/ext_flash_mcuboot_secondary.conf
)
add_overlay_dts(
mcuboot
${ZEPHYR_NRF_MODULE_DIR}/modules/mcuboot/flash_sim.overlay
)
else()
set_config_bool(mcuboot CONFIG_PCD_APP n)
endif()
endif()
endif()
if(SB_CONFIG_BT_FAST_PAIR)
set_config_bool(${DEFAULT_IMAGE} CONFIG_BT_FAST_PAIR y)
if(DEFINED FP_MODEL_ID AND DEFINED FP_ANTI_SPOOFING_KEY)
include_fast_pair_hex()
set(FP_DATA_PRESENT "y" CACHE INTERNAL "Fast Pair provisioning data provided" FORCE)
else()
message(WARNING "Fast Pair support is enabled but `FP_MODEL_ID` or `FP_ANTI_SPOOFING_KEY` were not provided, this is likely to cause a build error")
set(FP_DATA_PRESENT "n" CACHE INTERNAL "Fast Pair provisioning data provided" FORCE)
endif()
else()
set_config_bool(${DEFAULT_IMAGE} CONFIG_BT_FAST_PAIR n)
endif()
endfunction(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
# Sysbuild function hooks used by nRF Connect SDK
function(${SYSBUILD_CURRENT_MODULE_NAME}_post_cmake)
cmake_parse_arguments(POST_CMAKE "" "" "IMAGES" ${ARGN})
restore_ncs_vars()
set(SYSBUILD y)
set_property(GLOBAL PROPERTY DOMAIN_APP_APP ${DEFAULT_IMAGE})
include_packaging()
if(SB_CONFIG_SECURE_BOOT OR SB_CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION)
include_provision_hex()
endif()
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/extensions.cmake)
if(SB_CONFIG_PARTITION_MANAGER)
# Run partition manager for each image before running CMake.
include(${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/partition_manager.cmake OPTIONAL)
# Set PM variables up that applications need prior to PM having ran
get_property(PM_MCUBOOT_PRIMARY_SIZE TARGET partition_manager PROPERTY PM_MCUBOOT_PRIMARY_SIZE)
get_property(PM_MCUBOOT_PAD_SIZE TARGET partition_manager PROPERTY PM_MCUBOOT_PAD_SIZE)
if(SB_CONFIG_SUPPORT_NETCORE AND NOT SB_CONFIG_NETCORE_EMPTY)
get_property(PM_MCUBOOT_PRIMARY_1_SIZE TARGET partition_manager PROPERTY PM_MCUBOOT_PRIMARY_1_SIZE)
endif()
endif()
foreach(image ${IMAGES})
configure_cache(IMAGE ${image})
endforeach()
endfunction(${SYSBUILD_CURRENT_MODULE_NAME}_post_cmake)
# Enable use of partition manager with sysbuild.
# Consider if this shoulc come through Sysbuild Kconfig flag.
set(NCS_SYSBUILD_PARTITION_MANAGER TRUE)
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_NRF_MODULE_DIR}/cmake/sysbuild/modules)
include(ncs_sysbuild_extensions)
include(${CMAKE_CURRENT_LIST_DIR}/extensions.cmake)
if(SB_CONFIG_SECURE_BOOT)
set(secure_boot_image_dir ${ZEPHYR_NRF_MODULE_DIR}/subsys/bootloader/image)
if(SB_CONFIG_SECURE_BOOT_NETCORE AND NOT SB_CONFIG_NETCORE_NONE)
add_overlay_config(
${SB_CONFIG_NETCORE_IMAGE_NAME}
"${secure_boot_image_dir}/secure_boot.conf"
)
add_overlay_config(mcuboot "${ZEPHYR_NRF_MODULE_DIR}/subsys/pcd/pcd.conf")
endif()
if(SB_CONFIG_SECURE_BOOT_APPCORE)
if(SB_CONFIG_BOOTLOADER_MCUBOOT)
add_overlay_config(mcuboot ${secure_boot_image_dir}/multi_image_mcuboot.conf)
# Include a kconfig file which enables CONFIG_FW_INFO in the image
# which is booted by B0.
add_overlay_config(mcuboot ${secure_boot_image_dir}/fw_info.conf)
add_overlay_config(mcuboot ${ZEPHYR_NRF_MODULE_DIR}/subsys/bootloader/image/log_minimal.conf)
if(SB_CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK)
add_overlay_config(mcuboot ${CMAKE_CURRENT_SOURCE_DIR}/override_external_mcuboot.conf)
endif()
else()
# Include a kconfig file which enables CONFIG_FW_INFO in the image
# which is booted by B0.
add_overlay_config(${DEFAULT_IMAGE} ${secure_boot_image_dir}/fw_info.conf)
endif()
endif()
endif()
include(${CMAKE_CURRENT_LIST_DIR}/netcore.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/secureboot.cmake)
store_ncs_vars()