blob: ad977247f774b6075890cb4ce9d43678a9e75894 [file] [log] [blame]
Tamas Bandb69d522018-03-01 10:04:41 +00001#-------------------------------------------------------------------------------
David Vinczedb32b212019-04-16 17:43:57 +02002# Copyright (c) 2018-2019, Arm Limited. All rights reserved.
Tamas Bandb69d522018-03-01 10:04:41 +00003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.7)
9
10function(mcuboot_create_boot_payload)
David Vinczed8fbe0e2019-08-12 15:58:57 +020011 set( _OPTIONS_ARGS) #Option (on/off) arguments (e.g. IGNORE_CASE)
Tamas Bandb69d522018-03-01 10:04:41 +000012 set( _ONE_VALUE_ARGS S_BIN NS_BIN FULL_BIN SIGN_BIN POSTFIX) #Single option arguments (e.g. PATH "./foo/bar")
David Vinczed8fbe0e2019-08-12 15:58:57 +020013 set( _MULTI_VALUE_ARGS) #List arguments (e.g. LANGUAGES C ASM CXX)
Tamas Bandb69d522018-03-01 10:04:41 +000014 cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN})
15
16 if (NOT DEFINED _MY_PARAMS_S_BIN)
17 message(FATAL_ERROR "mcuboot_create_boot_payload(): mandatory parameter 'S_BIN' missing.")
18 endif()
19
20 if (NOT DEFINED _MY_PARAMS_NS_BIN)
21 message(FATAL_ERROR "mcuboot_create_boot_payload(): mandatory parameter 'NS_BIN' missing.")
22 endif()
23
24 if (NOT DEFINED _MY_PARAMS_FULL_BIN)
25 message(FATAL_ERROR "mcuboot_create_boot_payload(): mandatory parameter 'FULL_BIN' missing.")
26 endif()
27
28 if (NOT DEFINED _MY_PARAMS_SIGN_BIN)
29 message(FATAL_ERROR "mcuboot_create_boot_payload(): mandatory parameter 'SIGN_BIN' missing.")
30 endif()
31
Tamas Ban57bfa432018-04-13 16:05:49 +010032 if (DEFINED _MY_PARAMS_POSTFIX)
Tamas Banbba85642019-06-06 09:31:59 +010033 if (${_MY_PARAMS_POSTFIX} STREQUAL "_1")
Tamas Ban57bfa432018-04-13 16:05:49 +010034 set(MY_POSTFIX "1")
Tamas Banbba85642019-06-06 09:31:59 +010035 else()
36 message(FATAL_ERROR "Unknown artefacts postfix: ${_MY_PARAMS_POSTFIX}")
Tamas Ban57bfa432018-04-13 16:05:49 +010037 endif()
38 endif()
39
Tamas Bandb69d522018-03-01 10:04:41 +000040 #Find Python3.x interpreter
41 find_package(PythonInterp 3)
42 if (NOT PYTHONINTERP_FOUND)
43 message(FATAL_ERROR "Failed to find Python3.x interpreter. Pyhton3 must be installed and available on the PATH.")
44 endif()
45
46 if(NOT DEFINED FLASH_LAYOUT)
47 message(FATAL_ERROR "ERROR: Incomplete Configuration: FLASH_LAYOUT is not defined.")
48 endif()
49
Tamas Ban7801ed42019-05-20 13:21:53 +010050 if (MCUBOOT_SIGNATURE_TYPE STREQUAL "RSA-3072")
Tamas Ban78676ac2019-07-11 09:05:54 +010051 set(KEY_FILE "${MCUBOOT_DIR}/root-rsa-3072.pem")
52 set(KEY_FILE_S "${MCUBOOT_DIR}/root-rsa-3072.pem")
53 set(KEY_FILE_NS "${MCUBOOT_DIR}/root-rsa-3072_1.pem")
Tamas Ban7801ed42019-05-20 13:21:53 +010054 elseif(MCUBOOT_SIGNATURE_TYPE STREQUAL "RSA-2048")
Tamas Ban78676ac2019-07-11 09:05:54 +010055 set(KEY_FILE "${MCUBOOT_DIR}/root-rsa-2048.pem")
56 set(KEY_FILE_S "${MCUBOOT_DIR}/root-rsa-2048.pem")
57 set(KEY_FILE_NS "${MCUBOOT_DIR}/root-rsa-2048_1.pem")
Tamas Ban7801ed42019-05-20 13:21:53 +010058 else()
59 message(FATAL_ERROR "${MCUBOOT_SIGNATURE_TYPE} is not supported as firmware signing algorithm")
60 endif()
61
Tamas Band0f4e1d2019-07-11 09:39:03 +010062 #Configure in which format (full or hash) include the public key to the image manifest
63 #
64 #|-----------------------|-----------------------|-------------------|--------------------|
65 #| |Key format in manifest |Key in MCUBoot code| Key in HW |
66 #|-----------------------|-----------------------|-------------------|--------------------|
67 #|MCUBOOT_HW_KEY == On | Full public key | No key embedded | Hash of public key |
68 #|-----------------------|-----------------------|-------------------|--------------------|
69 #|MCUBOOT_HW_KEY == Off | Hash of public key | Full public key | No key in HW |
70 #|-----------------------|-----------------------|-------------------|--------------------|
71 if (MCUBOOT_HW_KEY)
72 set(PUBLIC_KEY_FORMAT "full")
73 else()
74 set(PUBLIC_KEY_FORMAT "hash")
75 endif()
76
David Vinczed8fbe0e2019-08-12 15:58:57 +020077 set(PARTIAL_CONTENT_FOR_PREPROCESSING "#include \"${FLASH_LAYOUT}\"\n\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020078 "/* Enumeration that is used by the assemble.py and imgtool.py scripts\n"
79 " * for correct binary generation when nested macros are used\n"
80 " */\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020081 "enum image_attributes {\n"
82 "\tRE_SECURE_IMAGE_OFFSET = SECURE_IMAGE_OFFSET,\n"
83 "\tRE_SECURE_IMAGE_MAX_SIZE = SECURE_IMAGE_MAX_SIZE,\n"
84 "\tRE_NON_SECURE_IMAGE_OFFSET = NON_SECURE_IMAGE_OFFSET,\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020085 "\tRE_NON_SECURE_IMAGE_MAX_SIZE = NON_SECURE_IMAGE_MAX_SIZE,\n"
86 "#ifdef IMAGE_LOAD_ADDRESS\n"
87 "\tRE_IMAGE_LOAD_ADDRESS = IMAGE_LOAD_ADDRESS,\n"
88 "#endif\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020089 )
90
David Vinczed8fbe0e2019-08-12 15:58:57 +020091if (MCUBOOT_IMAGE_NUMBER GREATER 1)
92 if (SECURITY_COUNTER_S)
93 set(ADD_SECURITY_COUNTER_S "-s ${SECURITY_COUNTER_S}")
94 else()
95 set(ADD_SECURITY_COUNTER_S "")
96 endif()
97 if (SECURITY_COUNTER_NS)
98 set(ADD_SECURITY_COUNTER_NS "-s ${SECURITY_COUNTER_NS}")
99 else()
100 set(ADD_SECURITY_COUNTER_NS "")
101 endif()
102 if (DEFINED SECURITY_COUNTER)
103 message(WARNING "In case of multiple updatable images the security counter value can be specified"
104 " for the Secure and Non-secure images separately with the SECURITY_COUNTER_S and SECURITY_COUNTER_NS"
105 " defines. The value of SECURITY_COUNTER was ignored.")
106 set(SECURITY_COUNTER "")
107 endif()
108
109 if (NOT IMAGE_VERSION_S)
110 set(IMAGE_VERSION_S 0.0.0+0)
111 endif()
112 if (NOT IMAGE_VERSION_NS)
113 set(IMAGE_VERSION_NS 0.0.0+0)
114 endif()
115 if (DEFINED IMAGE_VERSION)
116 message(WARNING "In case of multiple updatable images the image version can be specified"
117 " for the Secure and Non-secure images separately with the IMAGE_VERSION_S and IMAGE_VERSION_NS"
118 " defines. The value of IMAGE_VERSION was ignored.")
119 set(IMAGE_VERSION "")
120 endif()
121
David Vincze9ec0f542019-07-03 18:09:47 +0200122 if (S_IMAGE_MIN_VER)
123 set(ADD_S_IMAGE_MIN_VER "-d \"(0,${S_IMAGE_MIN_VER})\"")
124 else()
125 set(ADD_S_IMAGE_MIN_VER "")
126 endif()
127 if (NS_IMAGE_MIN_VER)
128 set(ADD_NS_IMAGE_MIN_VER "-d \"(1,${NS_IMAGE_MIN_VER})\"")
129 else()
130 set(ADD_NS_IMAGE_MIN_VER "")
131 endif()
132
David Vinczed8fbe0e2019-08-12 15:58:57 +0200133 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess)
134 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed)
135
136 #Create files that will be preprocessed later in order to be able to handle
137 # nested macros in header files for certain macros
138 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
139 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
140 file(WRITE ${FILE_TO_PREPROCESS}_s.c ${CONTENT_FOR_PREPROCESSING})
141 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
142 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_1_SIZE,\n}\;")
143 file(WRITE ${FILE_TO_PREPROCESS}_ns.c ${CONTENT_FOR_PREPROCESSING})
144
145 #Preprocess the _s.c file that contains the secure image related macros
146 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_s.c
147 DST ${PREPROCESSED_FILE}_s.c
148 BEFORE_TARGET ${_MY_PARAMS_S_BIN}
149 TARGET_PREFIX ${_MY_PARAMS_S_BIN}
150 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
151
152 #Preprocess the _ns.c file that contains the non-secure image related macros
153 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_ns.c
154 DST ${PREPROCESSED_FILE}_ns.c
155 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
156 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
157 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
158
159 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
160 POST_BUILD
161
162 #Sign secure binary image with default public key in mcuboot folder
163 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
164 ARGS sign
165 --layout ${PREPROCESSED_FILE}_s.c
Tamas Ban78676ac2019-07-11 09:05:54 +0100166 -k ${KEY_FILE_S}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100167 --public-key-format ${PUBLIC_KEY_FORMAT}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200168 --align 1
169 -v ${IMAGE_VERSION_S}
David Vincze9ec0f542019-07-03 18:09:47 +0200170 ${ADD_NS_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200171 ${ADD_SECURITY_COUNTER_S}
172 -H 0x400
173 $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
174 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
175
176 #Sign non-secure binary image with default public key in mcuboot folder
177 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
178 ARGS sign
179 --layout ${PREPROCESSED_FILE}_ns.c
Tamas Ban78676ac2019-07-11 09:05:54 +0100180 -k ${KEY_FILE_NS}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100181 --public-key-format ${PUBLIC_KEY_FORMAT}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200182 --align 1
183 -v ${IMAGE_VERSION_NS}
David Vincze9ec0f542019-07-03 18:09:47 +0200184 ${ADD_S_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200185 ${ADD_SECURITY_COUNTER_NS}
186 -H 0x400
187 $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
188 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
189
190 #Create concatenated binary image from the two independently signed binary file
191 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
192 ARGS --layout ${PREPROCESSED_FILE}_s.c
193 -s ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
194 -n ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
195 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
196
197else() # MCUBOOT_IMAGE_NUMBER = 1
198 if (SECURITY_COUNTER)
199 set(ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}")
200 else()
201 set(ADD_SECURITY_COUNTER "")
202 endif()
203 if (DEFINED SECURITY_COUNTER_S OR
204 DEFINED SECURITY_COUNTER_NS)
205 message(WARNING "In case of a single updatable image the security counter value can be specified with"
206 " the SECURITY_COUNTER define. The values of SECURITY_COUNTER_S and/or SECURITY_COUNTER_NS were ignored.")
207 set(SECURITY_COUNTER_S "")
208 set(SECURITY_COUNTER_NS "")
209 endif()
210
211 if (NOT IMAGE_VERSION)
212 set(IMAGE_VERSION 0.0.0+0)
213 endif()
214 if (DEFINED IMAGE_VERSION_S OR
215 DEFINED IMAGE_VERSION_NS)
216 message(WARNING "In case of a single updatable image the image version can be specified with"
217 " the IMAGE_VERSION define. The values of IMAGE_VERSION_S and/or IMAGE_VERSION_NS were ignored.")
218 set(IMAGE_VERSION_S "")
219 set(IMAGE_VERSION_NS "")
220 endif()
221
David Vincze9ec0f542019-07-03 18:09:47 +0200222 if (DEFINED S_IMAGE_MIN_VER OR
223 DEFINED NS_IMAGE_MIN_VER)
224 message(WARNING "WARNING: In case of a single updatable image a dependency cannot be specified between"
225 " the S and NS images. The S_IMAGE_MIN_VER and/or NS_IMAGE_MIN_VER defines were ignored.")
226 set(S_IMAGE_MIN_VER "")
227 set(NS_IMAGE_MIN_VER "")
228 endif()
229
David Vinczed8fbe0e2019-08-12 15:58:57 +0200230 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess.c)
231 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed.c)
232 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
233 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
234
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200235 #Create a file that will be preprocessed later in order to be able to handle nested macros
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200236 #in header files for certain macros
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200237 file(WRITE ${FILE_TO_PREPROCESS} ${CONTENT_FOR_PREPROCESSING})
238
239 #Preprocess the .c file that contains the image related macros
240 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}
241 DST ${PREPROCESSED_FILE}
242 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
David Vincze63eda7a2019-08-09 17:42:51 +0200243 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
244 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200245
Tamas Bandb69d522018-03-01 10:04:41 +0000246 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
247 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +0000248 #Create concatenated binary image from the two binary file
249 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200250 ARGS --layout ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000251 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
252 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
253 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
254
255 #Sign concatenated binary image with default public key in mcuboot folder
256 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
257 ARGS sign
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200258 --layout ${PREPROCESSED_FILE}
Tamas Ban7801ed42019-05-20 13:21:53 +0100259 -k ${KEY_FILE}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100260 --public-key-format ${PUBLIC_KEY_FORMAT}
Tamas Bandb69d522018-03-01 10:04:41 +0000261 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100262 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200263 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000264 -H 0x400
Tamas Bandb69d522018-03-01 10:04:41 +0000265 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
266 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200267endif()
Tamas Ban57bfa432018-04-13 16:05:49 +0100268
269 #Collect executables to common location: build/install/outputs/
Tamas Ban57bfa432018-04-13 16:05:49 +0100270 set(TFM_SIGN_NAME tfm_s_ns_signed)
271
272 if (DEFINED MY_POSTFIX)
273 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
274 RENAME tfm_sig${MY_POSTFIX}.bin
275 DESTINATION outputs/${TARGET_PLATFORM}/)
276 else()
277 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
278 DESTINATION outputs/${TARGET_PLATFORM}/)
279 endif()
280
Tamas Ban57bfa432018-04-13 16:05:49 +0100281 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
282 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
283 DESTINATION outputs/fvp/)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200284
285if (MCUBOOT_IMAGE_NUMBER GREATER 1)
286 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
287 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
288 DESTINATION outputs/${TARGET_PLATFORM}/)
289 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
290 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
291 DESTINATION outputs/fvp/)
292
293else() # MCUBOOT_IMAGE_NUMBER = 1
294 set(TFM_FULL_NAME tfm_s_ns_concatenated)
295
296 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
297 DESTINATION outputs/${TARGET_PLATFORM}/)
298 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
299 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
300 DESTINATION outputs/fvp/)
301endif()
David Vinczedb32b212019-04-16 17:43:57 +0200302endfunction()
David Vincze63eda7a2019-08-09 17:42:51 +0200303
304#Validate and override the upgrade strategy to be used by the bootloader.
305#
306# If the given upgrade strategy is not supported with the current value
307# of the MCUBOOT_IMAGE_NUMBER variable then the function will override its
308# previously set value.
309#
310#Examples:
311# mcuboot_override_upgrade_strategy("SWAP")
312#
313#INPUTS:
314# strategy - (mandatory) - Upgrade strategy to be used.
315#
316#OUTPUTS:
317# MCUBOOT_UPGRADE_STRATEGY variable is set to the new strategy.
318#
319function(mcuboot_override_upgrade_strategy strategy)
320 if ((${strategy} STREQUAL "NO_SWAP" OR
321 ${strategy} STREQUAL "RAM_LOADING") AND
322 NOT (MCUBOOT_IMAGE_NUMBER EQUAL 1))
323 message(WARNING "The number of separately updatable images with the NO_SWAP or the RAM_LOADING"
324 " upgrade strategy can be only '1'. Your choice was overriden.")
325 set(MCUBOOT_IMAGE_NUMBER 1 PARENT_SCOPE)
326 endif()
327 get_property(_validation_list CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
328 #Check if validation list is set.
329 if (NOT _validation_list)
330 #Set the default upgrade strategy if the CACHE variable has not been set yet.
331 set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
332 set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
333 endif()
334 set(MCUBOOT_UPGRADE_STRATEGY ${strategy} PARENT_SCOPE)
335 validate_cache_value(MCUBOOT_UPGRADE_STRATEGY STRINGS)
336endfunction()