blob: d02f93a40f7fc979abfce4deddb7950bdfa9d79e [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 McCraeeaae6502024-07-25 14:36:46 +0100366function(align_up num align result)
367 math(EXPR out "(((${num}) + ((${align}) - 1)) & ~((${align}) - 1))")
368 set(${result} "${out}" PARENT_SCOPE)
369endfunction()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000370
Jamie McCraeeaae6502024-07-25 14:36:46 +0100371function(dt_get_parent node)
372 string(FIND "${${node}}" "/" pos REVERSE)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000373
Jamie McCraeeaae6502024-07-25 14:36:46 +0100374 if(pos EQUAL -1)
375 message(ERROR "Unable to get parent of node: ${${node}}")
376 endif()
377
378 string(SUBSTRING "${${node}}" 0 ${pos} ${node})
379 set(${node} "${${node}}" PARENT_SCOPE)
380endfunction()
381
382if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
383 dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
384 dt_prop(slot0_size PATH "${slot0_flash}" PROPERTY "reg" INDEX 1)
385 dt_get_parent(slot0_flash)
386 dt_get_parent(slot0_flash)
387 dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
388
389 if(NOT DEFINED slot0_size)
390 message(WARNING "Unable to determine size of slot0 partition, cannot calculate minimum sector usage")
391 elseif(NOT DEFINED erase_size_slot0)
392 message(WARNING "Unable to determine erase size of slot0 partition, cannot calculate minimum sector usage")
393 else()
394 math(EXPR slot_min_sectors "${slot0_size} / ${erase_size_slot0}")
395 endif()
396
397 if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
398 dt_nodelabel(slot1_flash NODELABEL "slot1_partition")
399 dt_prop(slot1_size PATH "${slot1_flash}" PROPERTY "reg" INDEX 1)
400 dt_get_parent(slot1_flash)
401 dt_get_parent(slot1_flash)
402 dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
403
404 if(NOT DEFINED slot1_size)
405 message(WARNING "Unable to determine size of slot1 partition, cannot calculate minimum sector usage")
406 elseif(NOT DEFINED erase_size_slot1)
407 message(WARNING "Unable to determine erase size of slot1 partition, cannot calculate minimum sector usage")
408 else()
409 math(EXPR slot1_min_sectors "${slot1_size} / ${erase_size_slot1}")
410
411 if("${slot1_min_sectors}" GREATER "${slot_min_sectors}")
412 set(slot_min_sectors ${slot1_min_sectors})
413 endif()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000414 endif()
Jamie McCraeeaae6502024-07-25 14:36:46 +0100415 endif()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000416
Jamie McCraeeaae6502024-07-25 14:36:46 +0100417 if(DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
418 zephyr_compile_definitions("MIN_SECTOR_COUNT=${slot_min_sectors}")
419 message("Calculated maximum number of sectors: ${slot_min_sectors}")
420 else()
421 message(WARNING "Unable to calculate minimum number of sector sizes, falling back to 128 sector default. Please disable CONFIG_BOOT_MAX_IMG_SECTORS_AUTO and set CONFIG_BOOT_MAX_IMG_SECTORS to the required value")
422 endif()
423endif()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000424
Jamie McCraeeaae6502024-07-25 14:36:46 +0100425if(SYSBUILD)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000426 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)
427 # TODO: RAM LOAD support
428 dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
429 dt_get_parent(slot0_flash)
430 dt_get_parent(slot0_flash)
431
432 if(NOT CONFIG_SINGLE_APPLICATION_SLOT)
433 dt_nodelabel(slot1_flash NODELABEL "slot1_partition")
434 dt_get_parent(slot1_flash)
435 dt_get_parent(slot1_flash)
436
437 if(NOT "${slot0_flash}" STREQUAL "${slot1_flash}")
438 # Check both slots for the one with the largest write/erase block size
439 dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
440 dt_prop(write_size_slot0 PATH "${slot0_flash}" PROPERTY "write-block-size")
441 dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
442 dt_prop(write_size_slot1 PATH "${slot1_flash}" PROPERTY "write-block-size")
443
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000444 if(DEFINED erase_size_slot0 AND DEFINED erase_size_slot1)
445 if(${erase_size_slot0} GREATER ${erase_size_slot1})
446 set(erase_size ${erase_size_slot0})
447 else()
448 set(erase_size ${erase_size_slot1})
449 endif()
450 elseif(DEFINED erase_size_slot0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000451 set(erase_size ${erase_size_slot0})
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000452 elseif(DEFINED erase_size_slot1)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000453 set(erase_size ${erase_size_slot1})
454 endif()
455
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000456 if(DEFINED write_size_slot0 AND DEFINED write_size_slot1)
457 if(${write_size_slot0} GREATER ${write_size_slot1})
458 set(write_size ${write_size_slot0})
459 else()
460 set(write_size ${write_size_slot1})
461 endif()
462 elseif(DEFINED write_size_slot0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000463 set(write_size ${write_size_slot0})
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000464 elseif(DEFINED write_size_slot1)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000465 set(write_size ${write_size_slot1})
466 endif()
467 else()
468 dt_prop(erase_size PATH "${slot0_flash}" PROPERTY "erase-block-size")
469 dt_prop(write_size PATH "${slot0_flash}" PROPERTY "write-block-size")
470 endif()
471 else()
472 dt_prop(erase_size PATH "${slot0_flash}" PROPERTY "erase-block-size")
473 dt_prop(write_size PATH "${slot0_flash}" PROPERTY "write-block-size")
474 endif()
475
Jamie McCraedb9a7f52023-12-18 13:45:27 +0000476 if(NOT DEFINED erase_size)
477 message(WARNING "Unable to determine erase size of slot0 or slot1 partition, setting to 1 (this is probably wrong)")
478 set(erase_size 1)
479 endif()
480
481 if(NOT DEFINED write_size)
482 message(WARNING "Unable to determine write size of slot0 or slot1 partition, setting to 8 (this is probably wrong)")
483 set(write_size 8)
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000484 endif()
485
486 if(${write_size} LESS 8)
487 set(max_align_size 8)
488 else()
489 set(max_align_size ${write_size})
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000490 endif()
491
492 set(key_size 0)
493
494 # Boot trailer magic size
495 set(boot_magic_size 16)
496
497 # Estimates for trailer TLV data size, this was taken from hello world builds for nrf52840dk
498 if(CONFIG_BOOT_SIGNATURE_TYPE_RSA)
499 if(CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN EQUAL 3072)
500 set(boot_tlv_estimate 464)
501 else()
502 set(boot_tlv_estimate 336)
503 endif()
504 elseif(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
505 set(boot_tlv_estimate 150)
506 elseif(CONFIG_BOOT_SIGNATURE_TYPE_ED25519)
507 set(boot_tlv_estimate 144)
508 else()
509 set(boot_tlv_estimate 40)
510 endif()
511
512 if(CONFIG_BOOT_ENCRYPT_RSA OR CONFIG_BOOT_ENCRYPT_EC256 OR CONFIG_BOOT_ENCRYPT_X25519)
513 # 128-bit AES key size
514 set(boot_enc_key_size 16)
515
516 if(CONFIG_BOOT_SWAP_SAVE_ENCTLV)
517 if(CONFIG_BOOT_ENCRYPT_RSA)
518 set(key_size 256)
519 elseif(CONFIG_BOOT_ENCRYPT_EC256)
520 math(EXPR key_size "65 + 32 + ${boot_enc_key_size}")
521 elseif(CONFIG_BOOT_ENCRYPT_X25519)
522 math(EXPR key_size "32 + 32 + ${boot_enc_key_size}")
523 endif()
524 else()
525 set(key_size "${boot_enc_key_size}")
526 endif()
527
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000528 align_up(${key_size} ${max_align_size} key_size)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000529 math(EXPR key_size "${key_size} * 2")
530 endif()
531
532 align_up(${boot_magic_size} ${write_size} boot_magic_size)
533
534 if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER)
535 set(boot_swap_data_size 0)
536 else()
Jamie McCrae9fb7ce52024-03-12 13:51:22 +0000537 math(EXPR boot_swap_data_size "${max_align_size} * 4")
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000538 endif()
539
540 if(CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE)
Jamie McCraeeaae6502024-07-25 14:36:46 +0100541 if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO AND DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
542 math(EXPR boot_status_data_size "${slot_min_sectors} * (3 * ${write_size})")
543 else()
544 math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (3 * ${write_size})")
545 endif()
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000546 else()
547 set(boot_status_data_size 0)
548 endif()
549
550 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 +0000551 align_up(${required_size} ${erase_size} required_size)
552
Jamie McCraea4eda302024-01-29 11:00:25 +0000553 if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER)
554 set(required_upgrade_size "0")
555 else()
556 math(EXPR required_upgrade_size "${boot_magic_size} + ${boot_swap_data_size} + ${boot_status_data_size}")
557 align_up(${required_upgrade_size} ${erase_size} required_upgrade_size)
558 endif()
559
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000560 if(CONFIG_BOOT_SWAP_USING_MOVE)
561 math(EXPR required_size "${required_size} + ${erase_size}")
Jamie McCraea4eda302024-01-29 11:00:25 +0000562 math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000563 endif()
564 else()
565 set(required_size 0)
Jamie McCraea4eda302024-01-29 11:00:25 +0000566 set(required_upgrade_size 0)
Jamie McCrae4c7942e2023-10-30 11:59:57 +0000567 endif()
568
569 set(mcuboot_image_footer_size ${required_size} CACHE INTERNAL "Estimated MCUboot image trailer size" FORCE)
Jamie McCraea4eda302024-01-29 11:00:25 +0000570 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 +0000571endif()