blob: f68512129ff0e5af7c9bda0c425b701f4315bfc6 [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"
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}
89 TARGET_PREFIX ${_MY_PARAMS_NS_BIN})
90
Tamas Bandb69d522018-03-01 10:04:41 +000091 add_custom_command(TARGET ${_MY_PARAMS_NS_BIN}
92 POST_BUILD
Tamas Bandb69d522018-03-01 10:04:41 +000093 #Create concatenated binary image from the two binary file
94 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +020095 ARGS --layout ${PREPROCESSED_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +000096 -s $<TARGET_FILE_DIR:${_MY_PARAMS_S_BIN}>/${_MY_PARAMS_S_BIN}.bin
97 -n $<TARGET_FILE_DIR:${_MY_PARAMS_NS_BIN}>/${_MY_PARAMS_NS_BIN}.bin
98 -o ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
99
100 #Sign concatenated binary image with default public key in mcuboot folder
101 COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/imgtool.py
102 ARGS sign
Sverteczky, Marcell7d069e82019-07-04 18:17:33 +0200103 --layout ${PREPROCESSED_FILE}
Tamas Ban7801ed42019-05-20 13:21:53 +0100104 -k ${KEY_FILE}
Tamas Bandb69d522018-03-01 10:04:41 +0000105 --align 1
Oliver Swede21440442018-07-10 09:31:32 +0100106 -v ${IMAGE_VERSION}
David Vinczedb32b212019-04-16 17:43:57 +0200107 ${ADD_SECURITY_COUNTER}
Tamas Bandb69d522018-03-01 10:04:41 +0000108 -H 0x400
Tamas Bandb69d522018-03-01 10:04:41 +0000109 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
110 ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin)
Tamas Ban57bfa432018-04-13 16:05:49 +0100111
112 #Collect executables to common location: build/install/outputs/
113 set(TFM_FULL_NAME tfm_s_ns_concatenated)
114 set(TFM_SIGN_NAME tfm_s_ns_signed)
115
116 if (DEFINED MY_POSTFIX)
117 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
118 RENAME tfm_sig${MY_POSTFIX}.bin
119 DESTINATION outputs/${TARGET_PLATFORM}/)
120 else()
121 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
122 DESTINATION outputs/${TARGET_PLATFORM}/)
123 endif()
124
125 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
Oliver Swede05e5ded2018-07-19 16:40:49 +0100126 DESTINATION outputs/${TARGET_PLATFORM}/)
Tamas Ban57bfa432018-04-13 16:05:49 +0100127
128 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin
129 RENAME ${TFM_FULL_NAME}${_MY_PARAMS_POSTFIX}.bin
130 DESTINATION outputs/fvp/)
131
132 install(FILES ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin
133 RENAME ${TFM_SIGN_NAME}${_MY_PARAMS_POSTFIX}.bin
134 DESTINATION outputs/fvp/)
David Vinczedb32b212019-04-16 17:43:57 +0200135endfunction()