blob: 621c9baaa1692e324177c5f03953a7d666e860b8 [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)
11 set( _OPTIONS_ARGS) #Option (on/off) arguments (e.g. IGNORE_CASE)
12 set( _ONE_VALUE_ARGS S_BIN NS_BIN FULL_BIN SIGN_BIN POSTFIX) #Single option arguments (e.g. PATH "./foo/bar")
13 set( _MULTI_VALUE_ARGS) #List arguments (e.g. LANGUAGES C ASM CXX)
14 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 Vincze54d05552019-08-05 12:58:47 +020058 if (SECURITY_COUNTER)
David Vinczedb32b212019-04-16 17:43:57 +020059 set (ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}")
60 else()
61 set (ADD_SECURITY_COUNTER "")
62 endif()
63
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020064 set(FILE_TO_PREPROCESS ${CMAKE_BINARY_DIR}/image_macros_to_preprocess.c)
65 set(PREPROCESSED_FILE ${CMAKE_BINARY_DIR}/image_macros_preprocessed.c)
66 set(CONTENT_FOR_PREPROCESSING "#include \"${FLASH_LAYOUT}\"\n\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020067 "/* Enumeration that is used by the assemble.py and imgtool.py scripts\n"
68 " * for correct binary generation when nested macros are used\n"
69 " */\n"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020070 "enum image_attributes {\n"
71 "\tRE_SECURE_IMAGE_OFFSET = SECURE_IMAGE_OFFSET,\n"
72 "\tRE_SECURE_IMAGE_MAX_SIZE = SECURE_IMAGE_MAX_SIZE,\n"
73 "\tRE_NON_SECURE_IMAGE_OFFSET = NON_SECURE_IMAGE_OFFSET,\n"
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020074 "\tRE_NON_SECURE_IMAGE_MAX_SIZE = NON_SECURE_IMAGE_MAX_SIZE,\n"
75 "#ifdef IMAGE_LOAD_ADDRESS\n"
76 "\tRE_IMAGE_LOAD_ADDRESS = IMAGE_LOAD_ADDRESS,\n"
77 "#endif\n"
78 "\tRE_SIGN_BIN_SIZE = SIGN_BIN_SIZE\n}\;"
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020079 )
80
81 #Create a file that will be preprocessed later in order to be able to handle nested macros
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020082 #in header files for certain macros
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020083 file(WRITE ${FILE_TO_PREPROCESS} ${CONTENT_FOR_PREPROCESSING})
84
85 #Preprocess the .c file that contains the image related macros
86 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}
87 DST ${PREPROCESSED_FILE}
88 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
David Vincze63eda7a2019-08-09 17:42:51 +020089 TARGET_PREFIX ${_MY_PARAMS_NS_BIN}
90 DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020091
Tamas Bandb69d522018-03-01 10:04:41 +000092 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
93 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +000094 #Create concatenated binary image from the two binary file
95 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020096 ARGS --layout ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +000097 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
98 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
99 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
100
101 #Sign concatenated binary image with default public key in mcuboot folder
102 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
103 ARGS sign
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200104 --layout ${PREPROCESSED_FILE}
Tamas Ban7801ed42019-05-20 13:21:53 +0100105 -k ${KEY_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000106 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100107 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200108 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000109 -H 0x400
Tamas Bandb69d522018-03-01 10:04:41 +0000110 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
111 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
Tamas Ban57bfa432018-04-13 16:05:49 +0100112
113 #Collect executables to common location: build/install/outputs/
114 set(TFM_FULL_NAME tfm_s_ns_concatenated)
115 set(TFM_SIGN_NAME tfm_s_ns_signed)
116
117 if (DEFINED MY_POSTFIX)
118 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
119 RENAME tfm_sig${MY_POSTFIX}.bin
120 DESTINATION outputs/${TARGET_PLATFORM}/)
121 else()
122 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
123 DESTINATION outputs/${TARGET_PLATFORM}/)
124 endif()
125
126 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
Oliver Swede05e5ded2018-07-19 16:40:49 +0100127 DESTINATION outputs/${TARGET_PLATFORM}/)
Tamas Ban57bfa432018-04-13 16:05:49 +0100128
129 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
130 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
131 DESTINATION outputs/fvp/)
132
133 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
134 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
135 DESTINATION outputs/fvp/)
David Vinczedb32b212019-04-16 17:43:57 +0200136endfunction()
David Vincze63eda7a2019-08-09 17:42:51 +0200137
138#Validate and override the upgrade strategy to be used by the bootloader.
139#
140# If the given upgrade strategy is not supported with the current value
141# of the MCUBOOT_IMAGE_NUMBER variable then the function will override its
142# previously set value.
143#
144#Examples:
145# mcuboot_override_upgrade_strategy("SWAP")
146#
147#INPUTS:
148# strategy - (mandatory) - Upgrade strategy to be used.
149#
150#OUTPUTS:
151# MCUBOOT_UPGRADE_STRATEGY variable is set to the new strategy.
152#
153function(mcuboot_override_upgrade_strategy strategy)
154 if ((${strategy} STREQUAL "NO_SWAP" OR
155 ${strategy} STREQUAL "RAM_LOADING") AND
156 NOT (MCUBOOT_IMAGE_NUMBER EQUAL 1))
157 message(WARNING "The number of separately updatable images with the NO_SWAP or the RAM_LOADING"
158 " upgrade strategy can be only '1'. Your choice was overriden.")
159 set(MCUBOOT_IMAGE_NUMBER 1 PARENT_SCOPE)
160 endif()
161 get_property(_validation_list CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
162 #Check if validation list is set.
163 if (NOT _validation_list)
164 #Set the default upgrade strategy if the CACHE variable has not been set yet.
165 set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
166 set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
167 endif()
168 set(MCUBOOT_UPGRADE_STRATEGY ${strategy} PARENT_SCOPE)
169 validate_cache_value(MCUBOOT_UPGRADE_STRATEGY STRINGS)
170endfunction()