blob: 9770f99ff02f65286645fa64d41bf2fb3cb0cb2c [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 Vinczedb32b212019-04-16 17:43:57 +020058 if (DEFINED SECURITY_COUNTER)
59 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"
67 "/* Enumeration that is used by the assemble.py script for correct binary generation when nested macros are used */\n"
68 "enum image_attributes {\n"
69 "\tRE_SECURE_IMAGE_OFFSET = SECURE_IMAGE_OFFSET,\n"
70 "\tRE_SECURE_IMAGE_MAX_SIZE = SECURE_IMAGE_MAX_SIZE,\n"
71 "\tRE_NON_SECURE_IMAGE_OFFSET = NON_SECURE_IMAGE_OFFSET,\n"
72 "\tRE_NON_SECURE_IMAGE_MAX_SIZE = NON_SECURE_IMAGE_MAX_SIZE\n}\;"
73 )
74
75 #Create a file that will be preprocessed later in order to be able to handle nested macros
76 #in the flash_layout.h file for certain macros
77 file(WRITE ${FILE_TO_PREPROCESS} ${CONTENT_FOR_PREPROCESSING})
78
79 #Preprocess the .c file that contains the image related macros
80 compiler_preprocess_file(SRC ${FILE_TO_PREPROCESS}
81 DST ${PREPROCESSED_FILE}
82 BEFORE_TARGET ${_MY_PARAMS_NS_BIN}
83 TARGET_PREFIX ${_MY_PARAMS_NS_BIN})
84
Tamas Bandb69d522018-03-01 10:04:41 +000085 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
86 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +000087 #Create concatenated binary image from the two binary file
88 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020089 ARGS -l ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +000090 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
91 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
92 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
93
94 #Sign concatenated binary image with default public key in mcuboot folder
95 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
96 ARGS sign
Oliver Swede05e5ded2018-07-19 16:40:49 +010097 --layout ${FLASH_LAYOUT}
Tamas Ban7801ed42019-05-20 13:21:53 +010098 -k ${KEY_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +000099 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100100 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200101 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000102 -H 0x400
103 --pad ${SIGN_BIN_SIZE}
104 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
105 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
Tamas Ban57bfa432018-04-13 16:05:49 +0100106
107 #Collect executables to common location: build/install/outputs/
108 set(TFM_FULL_NAME tfm_s_ns_concatenated)
109 set(TFM_SIGN_NAME tfm_s_ns_signed)
110
111 if (DEFINED MY_POSTFIX)
112 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
113 RENAME tfm_sig${MY_POSTFIX}.bin
114 DESTINATION outputs/${TARGET_PLATFORM}/)
115 else()
116 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
117 DESTINATION outputs/${TARGET_PLATFORM}/)
118 endif()
119
120 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
Oliver Swede05e5ded2018-07-19 16:40:49 +0100121 DESTINATION outputs/${TARGET_PLATFORM}/)
Tamas Ban57bfa432018-04-13 16:05:49 +0100122
123 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
124 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
125 DESTINATION outputs/fvp/)
126
127 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
128 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
129 DESTINATION outputs/fvp/)
David Vinczedb32b212019-04-16 17:43:57 +0200130endfunction()