blob: c6d5b94ba4bc72450c280bc8552240aa285da990 [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
David Vinczed8fbe0e2019-08-12 15:58:57 +020058 set(PARTIAL_CONTENT_FOR_PREPROCESSING "#include \"${FLASH_LAYOUT}\"\n\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020059 "/* Enumeration that is used by the assemble.py and imgtool.py scripts\n"
60 " * for correct binary generation when nested macros are used\n"
61 " */\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020062 "enum image_attributes {\n"
63 "\tRE_SECURE_IMAGE_OFFSET = SECURE_IMAGE_OFFSET,\n"
64 "\tRE_SECURE_IMAGE_MAX_SIZE = SECURE_IMAGE_MAX_SIZE,\n"
65 "\tRE_NON_SECURE_IMAGE_OFFSET = NON_SECURE_IMAGE_OFFSET,\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020066 "\tRE_NON_SECURE_IMAGE_MAX_SIZE = NON_SECURE_IMAGE_MAX_SIZE,\n"
67 "#ifdef IMAGE_LOAD_ADDRESS\n"
68 "\tRE_IMAGE_LOAD_ADDRESS = IMAGE_LOAD_ADDRESS,\n"
69 "#endif\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020070 )
71
David Vinczed8fbe0e2019-08-12 15:58:57 +020072if (MCUBOOT_IMAGE_NUMBER GREATER 1)
73 if (SECURITY_COUNTER_S)
74 set(ADD_SECURITY_COUNTER_S "-s ${SECURITY_COUNTER_S}")
75 else()
76 set(ADD_SECURITY_COUNTER_S "")
77 endif()
78 if (SECURITY_COUNTER_NS)
79 set(ADD_SECURITY_COUNTER_NS "-s ${SECURITY_COUNTER_NS}")
80 else()
81 set(ADD_SECURITY_COUNTER_NS "")
82 endif()
83 if (DEFINED SECURITY_COUNTER)
84 message(WARNING "In case of multiple updatable images the security counter value can be specified"
85 " for the Secure and Non-secure images separately with the SECURITY_COUNTER_S and SECURITY_COUNTER_NS"
86 " defines. The value of SECURITY_COUNTER was ignored.")
87 set(SECURITY_COUNTER "")
88 endif()
89
90 if (NOT IMAGE_VERSION_S)
91 set(IMAGE_VERSION_S 0.0.0+0)
92 endif()
93 if (NOT IMAGE_VERSION_NS)
94 set(IMAGE_VERSION_NS 0.0.0+0)
95 endif()
96 if (DEFINED IMAGE_VERSION)
97 message(WARNING "In case of multiple updatable images the image version can be specified"
98 " for the Secure and Non-secure images separately with the IMAGE_VERSION_S and IMAGE_VERSION_NS"
99 " defines. The value of IMAGE_VERSION was ignored.")
100 set(IMAGE_VERSION "")
101 endif()
102
David Vincze9ec0f542019-07-03 18:09:47 +0200103 if (S_IMAGE_MIN_VER)
104 set(ADD_S_IMAGE_MIN_VER "-d \"(0,${S_IMAGE_MIN_VER})\"")
105 else()
106 set(ADD_S_IMAGE_MIN_VER "")
107 endif()
108 if (NS_IMAGE_MIN_VER)
109 set(ADD_NS_IMAGE_MIN_VER "-d \"(1,${NS_IMAGE_MIN_VER})\"")
110 else()
111 set(ADD_NS_IMAGE_MIN_VER "")
112 endif()
113
David Vinczed8fbe0e2019-08-12 15:58:57 +0200114 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess)
115 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed)
116
117 #Create files that will be preprocessed later in order to be able to handle
118 # nested macros in header files for certain macros
119 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
120 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
121 file(WRITE ${FILE_TO_PREPROCESS}_s.c ${CONTENT_FOR_PREPROCESSING})
122 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
123 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_1_SIZE,\n}\;")
124 file(WRITE ${FILE_TO_PREPROCESS}_ns.c ${CONTENT_FOR_PREPROCESSING})
125
126 #Preprocess the _s.c file that contains the secure image related macros
127 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_s.c
128 DST ${PREPROCESSED_FILE}_s.c
129 BEFORE_TARGET ${_MY_PARAMS_S_BIN}
130 TARGET_PREFIX ${_MY_PARAMS_S_BIN}
131 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
132
133 #Preprocess the _ns.c file that contains the non-secure image related macros
134 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}_ns.c
135 DST ${PREPROCESSED_FILE}_ns.c
136 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
137 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
138 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
139
140 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
141 POST_BUILD
142
143 #Sign secure binary image with default public key in mcuboot folder
144 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
145 ARGS sign
146 --layout ${PREPROCESSED_FILE}_s.c
147 -k ${KEY_FILE}
148 --align 1
149 -v ${IMAGE_VERSION_S}
David Vincze9ec0f542019-07-03 18:09:47 +0200150 ${ADD_NS_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200151 ${ADD_SECURITY_COUNTER_S}
152 -H 0x400
153 $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
154 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
155
156 #Sign non-secure binary image with default public key in mcuboot folder
157 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
158 ARGS sign
159 --layout ${PREPROCESSED_FILE}_ns.c
160 -k ${KEY_FILE}
161 --align 1
162 -v ${IMAGE_VERSION_NS}
David Vincze9ec0f542019-07-03 18:09:47 +0200163 ${ADD_S_IMAGE_MIN_VER}
David Vinczed8fbe0e2019-08-12 15:58:57 +0200164 ${ADD_SECURITY_COUNTER_NS}
165 -H 0x400
166 $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
167 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
168
169 #Create concatenated binary image from the two independently signed binary file
170 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
171 ARGS --layout ${PREPROCESSED_FILE}_s.c
172 -s ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
173 -n ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
174 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
175
176else() # MCUBOOT_IMAGE_NUMBER = 1
177 if (SECURITY_COUNTER)
178 set(ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}")
179 else()
180 set(ADD_SECURITY_COUNTER "")
181 endif()
182 if (DEFINED SECURITY_COUNTER_S OR
183 DEFINED SECURITY_COUNTER_NS)
184 message(WARNING "In case of a single updatable image the security counter value can be specified with"
185 " the SECURITY_COUNTER define. The values of SECURITY_COUNTER_S and/or SECURITY_COUNTER_NS were ignored.")
186 set(SECURITY_COUNTER_S "")
187 set(SECURITY_COUNTER_NS "")
188 endif()
189
190 if (NOT IMAGE_VERSION)
191 set(IMAGE_VERSION 0.0.0+0)
192 endif()
193 if (DEFINED IMAGE_VERSION_S OR
194 DEFINED IMAGE_VERSION_NS)
195 message(WARNING "In case of a single updatable image the image version can be specified with"
196 " the IMAGE_VERSION define. The values of IMAGE_VERSION_S and/or IMAGE_VERSION_NS were ignored.")
197 set(IMAGE_VERSION_S "")
198 set(IMAGE_VERSION_NS "")
199 endif()
200
David Vincze9ec0f542019-07-03 18:09:47 +0200201 if (DEFINED S_IMAGE_MIN_VER OR
202 DEFINED NS_IMAGE_MIN_VER)
203 message(WARNING "WARNING: In case of a single updatable image a dependency cannot be specified between"
204 " the S and NS images. The S_IMAGE_MIN_VER and/or NS_IMAGE_MIN_VER defines were ignored.")
205 set(S_IMAGE_MIN_VER "")
206 set(NS_IMAGE_MIN_VER "")
207 endif()
208
David Vinczed8fbe0e2019-08-12 15:58:57 +0200209 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess.c)
210 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed.c)
211 string(CONCAT CONTENT_FOR_PREPROCESSING ${PARTIAL_CONTENT_FOR_PREPROCESSING}
212 "\tRE_SIGN_BIN_SIZE = FLASH_AREA_0_SIZE,\n}\;")
213
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200214 #Create a file that will be preprocessed later in order to be able to handle nested macros
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200215 #in header files for certain macros
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200216 file(WRITE ${FILE_TO_PREPROCESS} ${CONTENT_FOR_PREPROCESSING})
217
218 #Preprocess the .c file that contains the image related macros
219 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}
220 DST ${PREPROCESSED_FILE}
221 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
David Vincze63eda7a2019-08-09 17:42:51 +0200222 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
223 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +0200224
Tamas Bandb69d522018-03-01 10:04:41 +0000225 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
226 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +0000227 #Create concatenated binary image from the two binary file
228 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200229 ARGS --layout ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000230 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
231 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
232 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
233
234 #Sign concatenated binary image with default public key in mcuboot folder
235 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
236 ARGS sign
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200237 --layout ${PREPROCESSED_FILE}
Tamas Ban7801ed42019-05-20 13:21:53 +0100238 -k ${KEY_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000239 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100240 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200241 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000242 -H 0x400
Tamas Bandb69d522018-03-01 10:04:41 +0000243 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
244 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200245endif()
Tamas Ban57bfa432018-04-13 16:05:49 +0100246
247 #Collect executables to common location: build/install/outputs/
Tamas Ban57bfa432018-04-13 16:05:49 +0100248 set(TFM_SIGN_NAME tfm_s_ns_signed)
249
250 if (DEFINED MY_POSTFIX)
251 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
252 RENAME tfm_sig${MY_POSTFIX}.bin
253 DESTINATION outputs/${TARGET_PLATFORM}/)
254 else()
255 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
256 DESTINATION outputs/${TARGET_PLATFORM}/)
257 endif()
258
Tamas Ban57bfa432018-04-13 16:05:49 +0100259 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
260 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
261 DESTINATION outputs/fvp/)
David Vinczed8fbe0e2019-08-12 15:58:57 +0200262
263if (MCUBOOT_IMAGE_NUMBER GREATER 1)
264 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
265 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
266 DESTINATION outputs/${TARGET_PLATFORM}/)
267 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_S_BIN}_signed.bin
268 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_NS_BIN}_signed.bin
269 DESTINATION outputs/fvp/)
270
271else() # MCUBOOT_IMAGE_NUMBER = 1
272 set(TFM_FULL_NAME tfm_s_ns_concatenated)
273
274 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
275 DESTINATION outputs/${TARGET_PLATFORM}/)
276 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
277 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
278 DESTINATION outputs/fvp/)
279endif()
David Vinczedb32b212019-04-16 17:43:57 +0200280endfunction()
David Vincze63eda7a2019-08-09 17:42:51 +0200281
282#Validate and override the upgrade strategy to be used by the bootloader.
283#
284# If the given upgrade strategy is not supported with the current value
285# of the MCUBOOT_IMAGE_NUMBER variable then the function will override its
286# previously set value.
287#
288#Examples:
289# mcuboot_override_upgrade_strategy("SWAP")
290#
291#INPUTS:
292# strategy - (mandatory) - Upgrade strategy to be used.
293#
294#OUTPUTS:
295# MCUBOOT_UPGRADE_STRATEGY variable is set to the new strategy.
296#
297function(mcuboot_override_upgrade_strategy strategy)
298 if ((${strategy} STREQUAL "NO_SWAP" OR
299 ${strategy} STREQUAL "RAM_LOADING") AND
300 NOT (MCUBOOT_IMAGE_NUMBER EQUAL 1))
301 message(WARNING "The number of separately updatable images with the NO_SWAP or the RAM_LOADING"
302 " upgrade strategy can be only '1'. Your choice was overriden.")
303 set(MCUBOOT_IMAGE_NUMBER 1 PARENT_SCOPE)
304 endif()
305 get_property(_validation_list CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
306 #Check if validation list is set.
307 if (NOT _validation_list)
308 #Set the default upgrade strategy if the CACHE variable has not been set yet.
309 set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
310 set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
311 endif()
312 set(MCUBOOT_UPGRADE_STRATEGY ${strategy} PARENT_SCOPE)
313 validate_cache_value(MCUBOOT_UPGRADE_STRATEGY STRINGS)
314endfunction()