blob: 5261c02407f1664e98b8158815d94e50c4ea724d [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")
51 set(KEY_FILE "${MCUBOOT_DIR}/root-rsa-3072.pem")
52 elseif(MCUBOOT_SIGNATURE_TYPE STREQUAL "RSA-2048")
53 set(KEY_FILE "${MCUBOOT_DIR}/root-rsa-2048.pem")
54 else()
55 message(FATAL_ERROR "${MCUBOOT_SIGNATURE_TYPE} is not supported as firmware signing algorithm")
56 endif()
57
Tamas Band0f4e1d2019-07-11 09:39:03 +010058 #Configure in which format (full or hash) include the public key to the image manifest
59 #
60 #|-----------------------|-----------------------|-------------------|--------------------|
61 #| |Key format in manifest |Key in MCUBoot code| Key in HW |
62 #|-----------------------|-----------------------|-------------------|--------------------|
63 #|MCUBOOT_HW_KEY == On | Full public key | No key embedded | Hash of public key |
64 #|-----------------------|-----------------------|-------------------|--------------------|
65 #|MCUBOOT_HW_KEY == Off | Hash of public key | Full public key | No key in HW |
66 #|-----------------------|-----------------------|-------------------|--------------------|
67 if (MCUBOOT_HW_KEY)
68 set(PUBLIC_KEY_FORMAT "full")
69 else()
70 set(PUBLIC_KEY_FORMAT "hash")
71 endif()
72
David Vinczed8fbe0e2019-08-12 15:58:57 +020073 set(PARTIAL_CONTENT_FOR_PREPROCESSING "#include \"${FLASH_LAYOUT}\"\n\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020074 "/* Enumeration that is used by the assemble.py and imgtool.py scripts\n"
75 " * for correct binary generation when nested macros are used\n"
76 " */\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020077 "enum image_attributes {\n"
78 "\tRE_SECURE_IMAGE_OFFSET = SECURE_IMAGE_OFFSET,\n"
79 "\tRE_SECURE_IMAGE_MAX_SIZE = SECURE_IMAGE_MAX_SIZE,\n"
80 "\tRE_NON_SECURE_IMAGE_OFFSET = NON_SECURE_IMAGE_OFFSET,\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020081 "\tRE_NON_SECURE_IMAGE_MAX_SIZE = NON_SECURE_IMAGE_MAX_SIZE,\n"
82 "#ifdef IMAGE_LOAD_ADDRESS\n"
83 "\tRE_IMAGE_LOAD_ADDRESS = IMAGE_LOAD_ADDRESS,\n"
84 "#endif\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020085 )
86
David Vinczed8fbe0e2019-08-12 15:58:57 +020087if (MCUBOOT_IMAGE_NUMBER GREATER 1)
88 if (SECURITY_COUNTER_S)
89 set(ADD_SECURITY_COUNTER_S "-s ${SECURITY_COUNTER_S}")
90 else()
91 set(ADD_SECURITY_COUNTER_S "")
92 endif()
93 if (SECURITY_COUNTER_NS)
94 set(ADD_SECURITY_COUNTER_NS "-s ${SECURITY_COUNTER_NS}")
95 else()
96 set(ADD_SECURITY_COUNTER_NS "")
97 endif()
98 if (DEFINED SECURITY_COUNTER)
99 message(WARNING "In case of multiple updatable images the security counter value can be specified"
100 " for the Secure and Non-secure images separately with the SECURITY_COUNTER_S and SECURITY_COUNTER_NS"
101 " defines. The value of SECURITY_COUNTER was ignored.")
102 set(SECURITY_COUNTER "")
103 endif()
104
105 if (NOT IMAGE_VERSION_S)
106 set(IMAGE_VERSION_S 0.0.0+0)
107 endif()
108 if (NOT IMAGE_VERSION_NS)
109 set(IMAGE_VERSION_NS 0.0.0+0)
110 endif()
111 if (DEFINED IMAGE_VERSION)
112 message(WARNING "In case of multiple updatable images the image version can be specified"
113 " for the Secure and Non-secure images separately with the IMAGE_VERSION_S and IMAGE_VERSION_NS"
114 " defines. The value of IMAGE_VERSION was ignored.")
115 set(IMAGE_VERSION "")
116 endif()
117
David Vincze9ec0f542019-07-03 18:09:47 +0200118 if (S_IMAGE_MIN_VER)
119 set(ADD_S_IMAGE_MIN_VER "-d \"(0,${S_IMAGE_MIN_VER})\"")
120 else()
121 set(ADD_S_IMAGE_MIN_VER "")
122 endif()
123 if (NS_IMAGE_MIN_VER)
124 set(ADD_NS_IMAGE_MIN_VER "-d \"(1,${NS_IMAGE_MIN_VER})\"")
125 else()
126 set(ADD_NS_IMAGE_MIN_VER "")
127 endif()
128
David Vinczed8fbe0e2019-08-12 15:58:57 +0200129 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess)
130 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed)
131
132 #Create files that will be preprocessed later in order to be able to handle
133 # nested macros in header files for certain macros
134 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
135 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
136 file(WRITE ${FILE_TO_PREPROCESS}_s.c ${CONTENT_FOR_PREPROCESSING})
137 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
138 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_1_SIZE,\n}\;")
139 file(WRITE ${FILE_TO_PREPROCESS}_ns.c ${CONTENT_FOR_PREPROCESSING})
140
141 #Preprocess the _s.c file that contains the secure image related macros
142 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_s.c
143 DST ${PREPROCESSED_FILE}_s.c
144 BEFORE_TARGET ${_MY_PARAMS_S_BIN}
145 TARGET_PREFIX ${_MY_PARAMS_S_BIN}
146 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
147
148 #Preprocess the _ns.c file that contains the non-secure image related macros
149 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_ns.c
150 DST ${PREPROCESSED_FILE}_ns.c
151 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
152 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
153 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
154
155 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
156 POST_BUILD
157
158 #Sign secure binary image with default public key in mcuboot folder
159 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
160 ARGS sign
161 --layout ${PREPROCESSED_FILE}_s.c
162 -k ${KEY_FILE}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100163 --public-key-format ${PUBLIC_KEY_FORMAT}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200164 --align 1
165 -v ${IMAGE_VERSION_S}
David Vincze9ec0f542019-07-03 18:09:47 +0200166 ${ADD_NS_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200167 ${ADD_SECURITY_COUNTER_S}
168 -H 0x400
169 $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
170 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
171
172 #Sign non-secure binary image with default public key in mcuboot folder
173 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
174 ARGS sign
175 --layout ${PREPROCESSED_FILE}_ns.c
176 -k ${KEY_FILE}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100177 --public-key-format ${PUBLIC_KEY_FORMAT}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200178 --align 1
179 -v ${IMAGE_VERSION_NS}
David Vincze9ec0f542019-07-03 18:09:47 +0200180 ${ADD_S_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200181 ${ADD_SECURITY_COUNTER_NS}
182 -H 0x400
183 $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
184 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
185
186 #Create concatenated binary image from the two independently signed binary file
187 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
188 ARGS --layout ${PREPROCESSED_FILE}_s.c
189 -s ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
190 -n ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
191 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
192
193else() # MCUBOOT_IMAGE_NUMBER = 1
194 if (SECURITY_COUNTER)
195 set(ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}")
196 else()
197 set(ADD_SECURITY_COUNTER "")
198 endif()
199 if (DEFINED SECURITY_COUNTER_S OR
200 DEFINED SECURITY_COUNTER_NS)
201 message(WARNING "In case of a single updatable image the security counter value can be specified with"
202 " the SECURITY_COUNTER define. The values of SECURITY_COUNTER_S and/or SECURITY_COUNTER_NS were ignored.")
203 set(SECURITY_COUNTER_S "")
204 set(SECURITY_COUNTER_NS "")
205 endif()
206
207 if (NOT IMAGE_VERSION)
208 set(IMAGE_VERSION 0.0.0+0)
209 endif()
210 if (DEFINED IMAGE_VERSION_S OR
211 DEFINED IMAGE_VERSION_NS)
212 message(WARNING "In case of a single updatable image the image version can be specified with"
213 " the IMAGE_VERSION define. The values of IMAGE_VERSION_S and/or IMAGE_VERSION_NS were ignored.")
214 set(IMAGE_VERSION_S "")
215 set(IMAGE_VERSION_NS "")
216 endif()
217
David Vincze9ec0f542019-07-03 18:09:47 +0200218 if (DEFINED S_IMAGE_MIN_VER OR
219 DEFINED NS_IMAGE_MIN_VER)
220 message(WARNING "WARNING: In case of a single updatable image a dependency cannot be specified between"
221 " the S and NS images. The S_IMAGE_MIN_VER and/or NS_IMAGE_MIN_VER defines were ignored.")
222 set(S_IMAGE_MIN_VER "")
223 set(NS_IMAGE_MIN_VER "")
224 endif()
225
David Vinczed8fbe0e2019-08-12 15:58:57 +0200226 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess.c)
227 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed.c)
228 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
229 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
230
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200231 #Create a file that will be preprocessed later in order to be able to handle nested macros
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200232 #in header files for certain macros
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200233 file(WRITE ${FILE_TO_PREPROCESS} ${CONTENT_FOR_PREPROCESSING})
234
235 #Preprocess the .c file that contains the image related macros
236 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}
237 DST ${PREPROCESSED_FILE}
238 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
David Vincze63eda7a2019-08-09 17:42:51 +0200239 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
240 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200241
Tamas Bandb69d522018-03-01 10:04:41 +0000242 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
243 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +0000244 #Create concatenated binary image from the two binary file
245 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200246 ARGS --layout ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000247 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
248 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
249 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
250
251 #Sign concatenated binary image with default public key in mcuboot folder
252 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
253 ARGS sign
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200254 --layout ${PREPROCESSED_FILE}
Tamas Ban7801ed42019-05-20 13:21:53 +0100255 -k ${KEY_FILE}
Tamas Band0f4e1d2019-07-11 09:39:03 +0100256 --public-key-format ${PUBLIC_KEY_FORMAT}
Tamas Bandb69d522018-03-01 10:04:41 +0000257 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100258 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200259 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000260 -H 0x400
Tamas Bandb69d522018-03-01 10:04:41 +0000261 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
262 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200263endif()
Tamas Ban57bfa432018-04-13 16:05:49 +0100264
265 #Collect executables to common location: build/install/outputs/
Tamas Ban57bfa432018-04-13 16:05:49 +0100266 set(TFM_SIGN_NAME tfm_s_ns_signed)
267
268 if (DEFINED MY_POSTFIX)
269 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
270 RENAME tfm_sig${MY_POSTFIX}.bin
271 DESTINATION outputs/${TARGET_PLATFORM}/)
272 else()
273 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
274 DESTINATION outputs/${TARGET_PLATFORM}/)
275 endif()
276
Tamas Ban57bfa432018-04-13 16:05:49 +0100277 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
278 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
279 DESTINATION outputs/fvp/)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200280
281if (MCUBOOT_IMAGE_NUMBER GREATER 1)
282 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
283 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
284 DESTINATION outputs/${TARGET_PLATFORM}/)
285 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
286 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
287 DESTINATION outputs/fvp/)
288
289else() # MCUBOOT_IMAGE_NUMBER = 1
290 set(TFM_FULL_NAME tfm_s_ns_concatenated)
291
292 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
293 DESTINATION outputs/${TARGET_PLATFORM}/)
294 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
295 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
296 DESTINATION outputs/fvp/)
297endif()
David Vinczedb32b212019-04-16 17:43:57 +0200298endfunction()
David Vincze63eda7a2019-08-09 17:42:51 +0200299
300#Validate and override the upgrade strategy to be used by the bootloader.
301#
302# If the given upgrade strategy is not supported with the current value
303# of the MCUBOOT_IMAGE_NUMBER variable then the function will override its
304# previously set value.
305#
306#Examples:
307# mcuboot_override_upgrade_strategy("SWAP")
308#
309#INPUTS:
310# strategy - (mandatory) - Upgrade strategy to be used.
311#
312#OUTPUTS:
313# MCUBOOT_UPGRADE_STRATEGY variable is set to the new strategy.
314#
315function(mcuboot_override_upgrade_strategy strategy)
316 if ((${strategy} STREQUAL "NO_SWAP" OR
317 ${strategy} STREQUAL "RAM_LOADING") AND
318 NOT (MCUBOOT_IMAGE_NUMBER EQUAL 1))
319 message(WARNING "The number of separately updatable images with the NO_SWAP or the RAM_LOADING"
320 " upgrade strategy can be only '1'. Your choice was overriden.")
321 set(MCUBOOT_IMAGE_NUMBER 1 PARENT_SCOPE)
322 endif()
323 get_property(_validation_list CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
324 #Check if validation list is set.
325 if (NOT _validation_list)
326 #Set the default upgrade strategy if the CACHE variable has not been set yet.
327 set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
328 set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
329 endif()
330 set(MCUBOOT_UPGRADE_STRATEGY ${strategy} PARENT_SCOPE)
331 validate_cache_value(MCUBOOT_UPGRADE_STRATEGY STRINGS)
332endfunction()