blob: 45548e0c3e33a69ca4a68d1486c404f76f8bbf3c [file] [log] [blame]
Marti Bolivarbf909a12017-11-13 19:43:46 -05001# CMakeLists.txt for building mcuboot as a Zephyr project
2#
3# Copyright (c) 2017 Open Source Foundries Limited
Jamie McCrae4c7942e2023-10-30 11:59:57 +00004# Copyright (c) 2023 Nordic Semiconductor ASA
Marti Bolivarbf909a12017-11-13 19:43:46 -05005#
6# SPDX-License-Identifier: Apache-2.0
7
Martí Bolívar0e3fa722019-10-22 14:39:33 -06008cmake_minimum_required(VERSION 3.13.1)
Rajavardhan Gundi40c28e32018-12-09 13:32:01 +05309
Torsten Rasmussen43004b82020-05-28 12:34:15 +020010# find_package(Zephyr) in order to load application boilerplate:
Marti Bolivarbf909a12017-11-13 19:43:46 -050011# http://docs.zephyrproject.org/application/application.html
Torsten Rasmussen43004b82020-05-28 12:34:15 +020012find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
Marti Bolivarbf909a12017-11-13 19:43:46 -050013project(NONE)
14
15# Path to "boot" subdirectory of repository root.
16get_filename_component(BOOT_DIR ${APPLICATION_SOURCE_DIR} DIRECTORY)
17# Path to top-level repository root directory.
18get_filename_component(MCUBOOT_DIR ${BOOT_DIR} DIRECTORY)
19# Path to tinycrypt library source subdirectory of MCUBOOT_DIR.
20set(TINYCRYPT_DIR "${MCUBOOT_DIR}/ext/tinycrypt/lib")
Sigvart Hovlandebd05032019-03-21 10:47:32 +010021assert_exists(TINYCRYPT_DIR)
Fabio Utzig34e93a52020-02-03 09:59:53 -030022set(TINYCRYPT_SHA512_DIR "${MCUBOOT_DIR}/ext/tinycrypt-sha512/lib")
23assert_exists(TINYCRYPT_SHA512_DIR)
Fabio Utzig1171df92019-05-10 19:26:38 -030024# Path to crypto-fiat
25set(FIAT_DIR "${MCUBOOT_DIR}/ext/fiat")
26assert_exists(FIAT_DIR)
Fabio Utzig28ee5b02017-12-12 08:10:40 -020027# Path to mbed-tls' asn1 parser library.
David Brownb748f6f2019-10-11 10:07:31 -060028set(MBEDTLS_ASN1_DIR "${MCUBOOT_DIR}/ext/mbedtls-asn1")
Sigvart Hovlandebd05032019-03-21 10:47:32 +010029assert_exists(MBEDTLS_ASN1_DIR)
30set(NRF_DIR "${MCUBOOT_DIR}/ext/nrf")
31
32if(CONFIG_BOOT_USE_NRF_CC310_BL)
Torsten Rasmussen33fbef52020-06-03 20:21:13 +020033set(NRFXLIB_DIR ${ZEPHYR_BASE}/../nrfxlib)
Andrzej Puzdrowskif0ef8b62020-10-01 13:51:48 +020034if(NOT EXISTS ${NRFXLIB_DIR})
35 message(FATAL_ERROR "
36 ------------------------------------------------------------------------
37 No such file or directory: ${NRFXLIB_DIR}
38 The current configuration enables nRF CC310 crypto accelerator hardware
39 with the `CONFIG_BOOT_USE_NRF_CC310_BL` option. Please follow
40 `ext/nrf/README.md` guide to fix your setup or use tinycrypt instead of
41 the HW accelerator.
42 To use the tinycrypt set `CONFIG_BOOT_ECDSA_TINYCRYPT` to y.
43 ------------------------------------------------------------------------")
44endif()
Sigvart Hovlandebd05032019-03-21 10:47:32 +010045# Don't include this if we are using west
46 add_subdirectory(${NRFXLIB_DIR} ${PROJECT_BINARY_DIR}/nrfxlib)
47endif()
Marti Bolivarbf909a12017-11-13 19:43:46 -050048
Sebastian Bøebe972172019-01-22 14:05:14 +010049zephyr_library_include_directories(
50 include
51 targets
52 )
53if(EXISTS targets/${BOARD}.h)
54 zephyr_library_compile_definitions(MCUBOOT_TARGET_CONFIG="${BOARD}.h")
Marti Bolivarbf909a12017-11-13 19:43:46 -050055endif()
56
57# Zephyr port-specific sources.
Sebastian Bøebe972172019-01-22 14:05:14 +010058zephyr_library_sources(
59 main.c
Jamie McCrae433b8482023-08-16 07:33:24 +010060 io.c
Sebastian Bøebe972172019-01-22 14:05:14 +010061 flash_map_extended.c
62 os.c
63 keys.c
64 )
65
Dominik Ermel428d3ee2021-08-17 07:55:54 +000066if(DEFINED CONFIG_ENABLE_MGMT_PERUSER)
67 zephyr_library_sources(
68 boot_serial_extensions.c
69 )
Jamie McCrae268433e2023-08-29 15:37:15 +010070
71 zephyr_linker_sources_ifdef(
72 CONFIG_ENABLE_MGMT_PERUSER
73 SECTIONS include/boot_serial/boot_serial.ld
74 )
75
76 if(DEFINED CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE OR DEFINED CONFIG_BOOT_MGMT_CUSTOM_IMG_LIST)
77 zephyr_library_sources(
78 boot_serial_extension_zephyr_basic.c
79 )
80 endif()
Dominik Ermel428d3ee2021-08-17 07:55:54 +000081endif()
82
Marti Bolivarbf909a12017-11-13 19:43:46 -050083if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
Sebastian Bøebe972172019-01-22 14:05:14 +010084 zephyr_library_sources(
Fabio Utzigccc02802019-11-05 07:55:14 -030085 flash_map_legacy.c
86 )
Marti Bolivarbf909a12017-11-13 19:43:46 -050087endif()
88
Jamie McCrae4da51012023-08-03 16:23:02 +010089if(DEFINED CONFIG_BOOT_SHARE_BACKEND_RETENTION)
90 zephyr_library_sources(
91 shared_data.c
92 )
93endif()
94
Marti Bolivarbf909a12017-11-13 19:43:46 -050095# Generic bootutil sources and includes.
Sebastian Bøebe972172019-01-22 14:05:14 +010096zephyr_library_include_directories(${BOOT_DIR}/bootutil/include)
97zephyr_library_sources(
Dominik Ermel8101c0c2020-05-19 13:01:16 +000098 ${BOOT_DIR}/bootutil/src/image_validate.c
99 ${BOOT_DIR}/bootutil/src/tlv.c
100 ${BOOT_DIR}/bootutil/src/encrypted.c
101 ${BOOT_DIR}/bootutil/src/image_rsa.c
Antonio de Angelis10529d32023-04-21 21:43:14 +0100102 ${BOOT_DIR}/bootutil/src/image_ecdsa.c
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000103 ${BOOT_DIR}/bootutil/src/image_ed25519.c
Dominik Ermel9b48d082020-06-08 12:40:06 +0000104 ${BOOT_DIR}/bootutil/src/bootutil_misc.c
Tamas Banfce87332020-07-10 12:40:11 +0100105 ${BOOT_DIR}/bootutil/src/fault_injection_hardening.c
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000106 )
107
Jamie McCrae4da51012023-08-03 16:23:02 +0100108if(DEFINED CONFIG_MEASURED_BOOT OR DEFINED CONFIG_BOOT_SHARE_DATA)
109 zephyr_library_sources(
110 ${BOOT_DIR}/bootutil/src/boot_record.c
111 )
112
113 # Set a define for this file which will allow inclusion of the Zephyr version
114 # include file
115 set_source_files_properties(
116 ${BOOT_DIR}/bootutil/src/boot_record.c
117 PROPERTIES COMPILE_FLAGS -DZEPHYR_VER_INCLUDE=1
118 )
119endif()
120
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100121# library which might be common source code for MCUBoot and an application
122zephyr_link_libraries(MCUBOOT_BOOTUTIL)
123
Tamas Banfce87332020-07-10 12:40:11 +0100124if(CONFIG_BOOT_FIH_PROFILE_HIGH)
125zephyr_library_sources(
126 ${BOOT_DIR}/bootutil/src/fault_injection_hardening_delay_rng_mbedtls.c
127 )
128endif()
129
Andrzej Puzdrowskifdff3e12020-09-15 08:23:25 +0200130if(CONFIG_SINGLE_APPLICATION_SLOT)
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000131zephyr_library_sources(
132 ${BOOT_DIR}/zephyr/single_loader.c
133 )
134zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
Jamie McCrae215345f2023-08-16 07:37:18 +0100135elseif(CONFIG_BOOT_FIRMWARE_LOADER)
136zephyr_library_sources(
137 ${BOOT_DIR}/zephyr/firmware_loader.c
138 )
139zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000140else()
141zephyr_library_sources(
Sebastian Bøebe972172019-01-22 14:05:14 +0100142 ${BOOT_DIR}/bootutil/src/loader.c
Fabio Utzigc58842e2019-11-28 10:30:01 -0300143 ${BOOT_DIR}/bootutil/src/swap_misc.c
144 ${BOOT_DIR}/bootutil/src/swap_scratch.c
145 ${BOOT_DIR}/bootutil/src/swap_move.c
Sebastian Bøebe972172019-01-22 14:05:14 +0100146 ${BOOT_DIR}/bootutil/src/caps.c
147 )
Dominik Ermel8101c0c2020-05-19 13:01:16 +0000148endif()
149
Jamie McCraec9fa6082023-07-21 10:23:17 +0100150if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256)
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100151 zephyr_library_include_directories(
Fabio Utzigccc02802019-11-05 07:55:14 -0300152 ${MBEDTLS_ASN1_DIR}/include
153 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100154 zephyr_library_sources(
Fabio Utzigccc02802019-11-05 07:55:14 -0300155 # Additionally pull in just the ASN.1 parser from mbedTLS.
156 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
157 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
158 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100159 if(CONFIG_BOOT_USE_TINYCRYPT)
Marti Bolivara4818a52018-04-12 13:02:38 -0400160 # When using ECDSA signatures, pull in our copy of the tinycrypt library.
Sebastian Bøebe972172019-01-22 14:05:14 +0100161 zephyr_library_include_directories(
Fabio Utzigccc02802019-11-05 07:55:14 -0300162 ${BOOT_DIR}/zephyr/include
163 ${TINYCRYPT_DIR}/include
164 )
Wouter Cappelle953a7612021-05-03 16:53:05 +0200165 zephyr_include_directories(${TINYCRYPT_DIR}/include)
Marti Bolivarbf909a12017-11-13 19:43:46 -0500166
Sebastian Bøebe972172019-01-22 14:05:14 +0100167 zephyr_library_sources(
Fabio Utzigccc02802019-11-05 07:55:14 -0300168 ${TINYCRYPT_DIR}/source/ecc.c
169 ${TINYCRYPT_DIR}/source/ecc_dsa.c
170 ${TINYCRYPT_DIR}/source/sha256.c
171 ${TINYCRYPT_DIR}/source/utils.c
172 )
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100173 elseif(CONFIG_BOOT_USE_NRF_CC310_BL)
174 zephyr_library_sources(${NRF_DIR}/cc310_glue.c)
175 zephyr_library_include_directories(${NRF_DIR})
176 zephyr_link_libraries(nrfxlib_crypto)
177 endif()
Fabio Utzig28ee5b02017-12-12 08:10:40 -0200178
Ding Taof97cb712018-06-08 14:37:13 +0000179 # Since here we are not using Zephyr's mbedTLS but rather our own, we need
Carles Cufi69c61d02018-06-05 15:56:08 +0200180 # to set MBEDTLS_CONFIG_FILE ourselves. When using Zephyr's copy, this
181 # variable is set by its Kconfig in the Zephyr codebase.
Sebastian Bøebe972172019-01-22 14:05:14 +0100182 zephyr_library_compile_definitions(
Fabio Utzigccc02802019-11-05 07:55:14 -0300183 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
184 )
Arvin Farahmandfb5ec182020-05-05 11:44:12 -0400185elseif(CONFIG_BOOT_SIGNATURE_TYPE_NONE)
186 zephyr_library_include_directories(
187 ${BOOT_DIR}/zephyr/include
188 ${TINYCRYPT_DIR}/include
189 )
190
191 zephyr_library_sources(
192 ${TINYCRYPT_DIR}/source/sha256.c
193 ${TINYCRYPT_DIR}/source/utils.c
194 )
Marti Bolivara4818a52018-04-12 13:02:38 -0400195elseif(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
196 # Use mbedTLS provided by Zephyr for RSA signatures. (Its config file
197 # is set using Kconfig.)
198 zephyr_include_directories(include)
Andrzej Puzdrowski5a325922021-11-08 14:07:56 +0100199 if(CONFIG_BOOT_ENCRYPT_RSA)
200 set_source_files_properties(
201 ${BOOT_DIR}/bootutil/src/encrypted.c
202 PROPERTIES
203 INCLUDE_DIRECTORIES ${ZEPHYR_MBEDTLS_MODULE_DIR}/library
204 )
205 endif()
Fabio Utzigb6f014c2020-04-02 13:25:01 -0300206elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519 OR CONFIG_BOOT_ENCRYPT_X25519)
Fabio Utzig34e93a52020-02-03 09:59:53 -0300207 if(CONFIG_BOOT_USE_TINYCRYPT)
208 zephyr_library_include_directories(
209 ${MBEDTLS_ASN1_DIR}/include
210 ${BOOT_DIR}/zephyr/include
211 ${TINYCRYPT_DIR}/include
212 ${TINYCRYPT_SHA512_DIR}/include
213 )
214 zephyr_library_sources(
215 ${TINYCRYPT_DIR}/source/sha256.c
216 ${TINYCRYPT_DIR}/source/utils.c
217 ${TINYCRYPT_SHA512_DIR}/source/sha512.c
218 # Additionally pull in just the ASN.1 parser from mbedTLS.
219 ${MBEDTLS_ASN1_DIR}/src/asn1parse.c
220 ${MBEDTLS_ASN1_DIR}/src/platform_util.c
221 )
222 zephyr_library_compile_definitions(
223 MBEDTLS_CONFIG_FILE="${CMAKE_CURRENT_LIST_DIR}/include/mcuboot-mbedtls-cfg.h"
224 )
225 else()
226 zephyr_include_directories(include)
227 endif()
Fabio Utzig1171df92019-05-10 19:26:38 -0300228
229 zephyr_library_include_directories(
230 ${BOOT_DIR}/zephyr/include
231 ${FIAT_DIR}/include/
232 )
233
234 zephyr_library_sources(
235 ${FIAT_DIR}/src/curve25519.c
236 )
Marti Bolivarbf909a12017-11-13 19:43:46 -0500237endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200238
Jamie McCraec9fa6082023-07-21 10:23:17 +0100239if(CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
Fabio Utzig42cc29a2019-11-05 07:54:41 -0300240 zephyr_library_sources(
241 ${TINYCRYPT_DIR}/source/aes_encrypt.c
242 ${TINYCRYPT_DIR}/source/aes_decrypt.c
243 ${TINYCRYPT_DIR}/source/ctr_mode.c
244 ${TINYCRYPT_DIR}/source/hmac.c
245 ${TINYCRYPT_DIR}/source/ecc_dh.c
246 )
247endif()
248
Fabio Utzigb6f014c2020-04-02 13:25:01 -0300249if(CONFIG_BOOT_ENCRYPT_EC256)
250 zephyr_library_sources(
251 ${TINYCRYPT_DIR}/source/ecc_dh.c
252 )
253endif()
254
Sebastian Bøebe972172019-01-22 14:05:14 +0100255if(CONFIG_MCUBOOT_SERIAL)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200256 zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
257 zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
Jamie McCraecb07e882023-04-14 09:28:24 +0100258 zephyr_sources(${BOOT_DIR}/boot_serial/src/zcbor_bulk.c)
Dominik Ermel88bd5672022-06-07 15:17:06 +0000259
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200260 zephyr_include_directories(${BOOT_DIR}/bootutil/include)
261 zephyr_include_directories(${BOOT_DIR}/boot_serial/include)
262 zephyr_include_directories(include)
Andrzej Puzdrowskic2e30cf2018-07-20 16:19:09 +0200263
Sebastian Bøebe972172019-01-22 14:05:14 +0100264 zephyr_include_directories_ifdef(
Fabio Utzigccc02802019-11-05 07:55:14 -0300265 CONFIG_BOOT_ERASE_PROGRESSIVELY
266 ${BOOT_DIR}/bootutil/src
267 )
Jamie McCraec9fa6082023-07-21 10:23:17 +0100268
269 if(CONFIG_BOOT_ENCRYPT_IMAGE)
270 zephyr_library_sources(
271 ${BOOT_DIR}/boot_serial/src/boot_serial_encryption.c
272 )
273 endif()
Andrzej Puzdrowski8e96b832017-09-08 16:49:14 +0200274endif()
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300275
276if(NOT CONFIG_BOOT_SIGNATURE_KEY_FILE STREQUAL "")
Nico Lastzkae16f52c2021-04-13 16:04:00 +0200277 # CONF_FILE points to the KConfig configuration files of the bootloader.
278 foreach (filepath ${CONF_FILE})
279 file(READ ${filepath} temp_text)
280 string(FIND "${temp_text}" ${CONFIG_BOOT_SIGNATURE_KEY_FILE} match)
281 if (${match} GREATER_EQUAL 0)
282 if (NOT DEFINED CONF_DIR)
283 get_filename_component(CONF_DIR ${filepath} DIRECTORY)
284 else()
285 message(FATAL_ERROR "Signature key file defined in multiple conf files")
286 endif()
287 endif()
288 endforeach()
289
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300290 if(IS_ABSOLUTE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
291 set(KEY_FILE ${CONFIG_BOOT_SIGNATURE_KEY_FILE})
Marek Pietac1cdcae2020-08-12 04:29:12 -0700292 elseif((DEFINED CONF_DIR) AND
293 (EXISTS ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE}))
Marek Pietabdcfc852020-08-04 02:22:55 -0700294 set(KEY_FILE ${CONF_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300295 else()
296 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_SIGNATURE_KEY_FILE})
297 endif()
Marek Pietac1cdcae2020-08-12 04:29:12 -0700298 message("MCUBoot bootloader key file: ${KEY_FILE}")
299
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300300 set(GENERATED_PUBKEY ${ZEPHYR_BINARY_DIR}/autogen-pubkey.c)
301 add_custom_command(
302 OUTPUT ${GENERATED_PUBKEY}
303 COMMAND
304 ${PYTHON_EXECUTABLE}
305 ${MCUBOOT_DIR}/scripts/imgtool.py
306 getpub
307 -k
308 ${KEY_FILE}
309 > ${GENERATED_PUBKEY}
310 DEPENDS ${KEY_FILE}
311 )
Sebastian Bøebe972172019-01-22 14:05:14 +0100312 zephyr_library_sources(${GENERATED_PUBKEY})
Fabio Utzigb1e0dc52018-04-26 10:53:19 -0300313endif()
Sigvart Hovlandebd05032019-03-21 10:47:32 +0100314
Wouter Cappelle10a877c2022-01-28 08:40:28 +0100315if(CONFIG_BOOT_ENCRYPTION_KEY_FILE AND NOT CONFIG_BOOT_ENCRYPTION_KEY_FILE STREQUAL "")
316 # CONF_FILE points to the KConfig configuration files of the bootloader.
317 unset(CONF_DIR)
318 foreach(filepath ${CONF_FILE})
319 file(READ ${filepath} temp_text)
320 string(FIND "${temp_text}" ${CONFIG_BOOT_ENCRYPTION_KEY_FILE} match)
321 if(${match} GREATER_EQUAL 0)
322 if(NOT DEFINED CONF_DIR)
323 get_filename_component(CONF_DIR ${filepath} DIRECTORY)
324 else()
325 message(FATAL_ERROR "Encryption key file defined in multiple conf files")
326 endif()
Wouter Cappelle953a7612021-05-03 16:53:05 +0200327 endif()
Wouter Cappelle10a877c2022-01-28 08:40:28 +0100328 endforeach()
Wouter Cappelle953a7612021-05-03 16:53:05 +0200329
Wouter Cappelle953a7612021-05-03 16:53:05 +0200330 if(IS_ABSOLUTE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
331 set(KEY_FILE ${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
332 elseif((DEFINED CONF_DIR) AND
333 (EXISTS ${CONF_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE}))
334 set(KEY_FILE ${CONF_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
335 else()
336 set(KEY_FILE ${MCUBOOT_DIR}/${CONFIG_BOOT_ENCRYPTION_KEY_FILE})
337 endif()
Wouter Cappelle10a877c2022-01-28 08:40:28 +0100338 message("MCUBoot bootloader encryption key file: ${KEY_FILE}")
Wouter Cappelle953a7612021-05-03 16:53:05 +0200339
340 set(GENERATED_ENCKEY ${ZEPHYR_BINARY_DIR}/autogen-enckey.c)
341 add_custom_command(
342 OUTPUT ${GENERATED_ENCKEY}
343 COMMAND
344 ${PYTHON_EXECUTABLE}
345 ${MCUBOOT_DIR}/scripts/imgtool.py
346 getpriv
347 -k
348 ${KEY_FILE}
349 > ${GENERATED_ENCKEY}
350 DEPENDS ${KEY_FILE}
351 )
352 zephyr_library_sources(${GENERATED_ENCKEY})
353endif()
354
Andrzej Puzdrowski9a605b62020-03-16 13:34:30 +0100355if(CONFIG_MCUBOOT_CLEANUP_ARM_CORE)
356zephyr_library_sources(
357 ${BOOT_DIR}/zephyr/arm_cleanup.c
358)
359endif()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000360
Jamie McCrae14961292024-04-18 09:12:09 +0100361if(CONFIG_MCUBOOT_BOOT_BANNER)
362 # Replace Zephyr's boot banner with the MCUboot one
363 zephyr_sources(kernel/banner.c)
364endif()
365
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000366if(SYSBUILD)
367 function(align_up num align result)
368 math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
369 set(${result} "${out}" PARENT_SCOPE)
370 endfunction()
371
372 function(dt_get_parent node)
373 string(FIND "${${node}}" "/" pos REVERSE)
374
375 if(pos EQUAL -1)
376 message(ERROR "Unable to get parent of node: ${${node}}")
377 endif()
378
379 string(SUBSTRING "${${node}}" 0 ${pos} ${node})
380 set(${node} "${${node}}" PARENT_SCOPE)
381 endfunction()
382
383 if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
384 # TODO: RAM LOAD support
385 dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
386 dt_get_parent(slot0_flash)
387 dt_get_parent(slot0_flash)
388
389 if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
390 dt_nodelabel(slot1_flash NODELABEL "slot1_partition")
391 dt_get_parent(slot1_flash)
392 dt_get_parent(slot1_flash)
393
394 if(NOT "${slot0_flash}" STREQUAL "${slot1_flash}")
395 # Check both slots for the one with the largest write/erase block size
396 dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
397 dt_prop(write_size_slot0 PATH "${slot0_flash}" PROPERTY "write-block-size")
398 dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
399 dt_prop(write_size_slot1 PATH "${slot1_flash}" PROPERTY "write-block-size")
400
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000401 if(DEFINED erase_size_slot0 AND DEFINED erase_size_slot1)
402 if(${erase_size_slot0} GREATER ${erase_size_slot1})
403 set(erase_size ${erase_size_slot0})
404 else()
405 set(erase_size ${erase_size_slot1})
406 endif()
407 elseif(DEFINED erase_size_slot0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000408 set(erase_size ${erase_size_slot0})
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000409 elseif(DEFINED erase_size_slot1)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000410 set(erase_size ${erase_size_slot1})
411 endif()
412
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000413 if(DEFINED write_size_slot0 AND DEFINED write_size_slot1)
414 if(${write_size_slot0} GREATER ${write_size_slot1})
415 set(write_size ${write_size_slot0})
416 else()
417 set(write_size ${write_size_slot1})
418 endif()
419 elseif(DEFINED write_size_slot0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000420 set(write_size ${write_size_slot0})
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000421 elseif(DEFINED write_size_slot1)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000422 set(write_size ${write_size_slot1})
423 endif()
424 else()
425 dt_prop(erase_size PATH "${slot0_flash}" PROPERTY "erase-block-size")
426 dt_prop(write_size PATH "${slot0_flash}" PROPERTY "write-block-size")
427 endif()
428 else()
429 dt_prop(erase_size PATH "${slot0_flash}" PROPERTY "erase-block-size")
430 dt_prop(write_size PATH "${slot0_flash}" PROPERTY "write-block-size")
431 endif()
432
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000433 if(NOT DEFINED erase_size)
434 message(WARNING "Unable to determine erase size of slot0 or slot1 partition, setting to 1 (this is probably wrong)")
435 set(erase_size 1)
436 endif()
437
438 if(NOT DEFINED write_size)
439 message(WARNING "Unable to determine write size of slot0 or slot1 partition, setting to 8 (this is probably wrong)")
440 set(write_size 8)
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000441 endif()
442
443 if(${write_size} LESS 8)
444 set(max_align_size 8)
445 else()
446 set(max_align_size ${write_size})
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000447 endif()
448
449 set(key_size 0)
450
451 # Boot trailer magic size
452 set(boot_magic_size 16)
453
454 # Estimates for trailer TLV data size, this was taken from hello world builds for nrf52840dk
455 if(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
456 if(CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN EQUAL 3072)
457 set(boot_tlv_estimate 464)
458 else()
459 set(boot_tlv_estimate 336)
460 endif()
461 elseif(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
462 set(boot_tlv_estimate 150)
463 elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
464 set(boot_tlv_estimate 144)
465 else()
466 set(boot_tlv_estimate 40)
467 endif()
468
469 if(CONFIG_BOOT_ENCRYPT_RSA OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
470 # 128-bit AES key size
471 set(boot_enc_key_size 16)
472
473 if(CONFIG_BOOT_SWAP_SAVE_ENCTLV)
474 if(CONFIG_BOOT_ENCRYPT_RSA)
475 set(key_size 256)
476 elseif(CONFIG_BOOT_ENCRYPT_EC256)
477 math(EXPR key_size "65 + 32 + ${boot_enc_key_size}")
478 elseif(CONFIG_BOOT_ENCRYPT_X25519)
479 math(EXPR key_size "32 + 32 + ${boot_enc_key_size}")
480 endif()
481 else()
482 set(key_size "${boot_enc_key_size}")
483 endif()
484
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000485 align_up(${key_size} ${max_align_size} key_size)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000486 math(EXPR key_size "${key_size} * 2")
487 endif()
488
489 align_up(${boot_magic_size} ${write_size} boot_magic_size)
490
491 if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER)
492 set(boot_swap_data_size 0)
493 else()
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000494 math(EXPR boot_swap_data_size "${max_align_size} * 4")
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000495 endif()
496
497 if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE)
498 math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (3 * ${write_size})")
499 else()
500 set(boot_status_data_size 0)
501 endif()
502
503 math(EXPR required_size "${key_size} + ${boot_magic_size} + ${boot_swap_data_size} + ${boot_status_data_size} + ${boot_tlv_estimate}")
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000504 align_up(${required_size} ${erase_size} required_size)
505
Jamie McCraea4eda302024-01-29 11:00:25 +0000506 if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER)
507 set(required_upgrade_size "0")
508 else()
509 math(EXPR required_upgrade_size "${boot_magic_size} + ${boot_swap_data_size} + ${boot_status_data_size}")
510 align_up(${required_upgrade_size} ${erase_size} required_upgrade_size)
511 endif()
512
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000513 if(CONFIG_BOOT_SWAP_USING_MOVE)
514 math(EXPR required_size "${required_size} + ${erase_size}")
Jamie McCraea4eda302024-01-29 11:00:25 +0000515 math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000516 endif()
517 else()
518 set(required_size 0)
Jamie McCraea4eda302024-01-29 11:00:25 +0000519 set(required_upgrade_size 0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000520 endif()
521
522 set(mcuboot_image_footer_size ${required_size} CACHE INTERNAL "Estimated MCUboot image trailer size" FORCE)
Jamie McCraea4eda302024-01-29 11:00:25 +0000523 set(mcuboot_image_upgrade_footer_size ${required_upgrade_size} CACHE INTERNAL "Estimated MCUboot update image trailer size" FORCE)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000524endif()