Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 2 | # Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | cmake_minimum_required(VERSION 3.7) |
| 9 | |
| 10 | function(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 Ban | 57bfa43 | 2018-04-13 16:05:49 +0100 | [diff] [blame] | 32 | if (DEFINED _MY_PARAMS_POSTFIX) |
Tamas Ban | bba8564 | 2019-06-06 09:31:59 +0100 | [diff] [blame] | 33 | if (${_MY_PARAMS_POSTFIX} STREQUAL "_1") |
Tamas Ban | 57bfa43 | 2018-04-13 16:05:49 +0100 | [diff] [blame] | 34 | set(MY_POSTFIX "1") |
Tamas Ban | bba8564 | 2019-06-06 09:31:59 +0100 | [diff] [blame] | 35 | else() |
| 36 | message(FATAL_ERROR "Unknown artefacts postfix: ${_MY_PARAMS_POSTFIX}") |
Tamas Ban | 57bfa43 | 2018-04-13 16:05:49 +0100 | [diff] [blame] | 37 | endif() |
| 38 | endif() |
| 39 | |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 40 | #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 Ban | 7801ed4 | 2019-05-20 13:21:53 +0100 | [diff] [blame] | 50 | 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 Vincze | 54d0555 | 2019-08-05 12:58:47 +0200 | [diff] [blame] | 58 | if (SECURITY_COUNTER) |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 59 | set (ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}") |
| 60 | else() |
| 61 | set (ADD_SECURITY_COUNTER "") |
| 62 | endif() |
| 63 | |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 64 | 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, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame] | 67 | "/* 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, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 70 | "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, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame] | 74 | "\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, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 79 | ) |
| 80 | |
| 81 | #Create a file that will be preprocessed later in order to be able to handle nested macros |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame] | 82 | #in header files for certain macros |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 83 | 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 Vincze | 63eda7a | 2019-08-09 17:42:51 +0200 | [diff] [blame] | 89 | TARGET_PREFIX ${_MY_PARAMS_NS_BIN} |
| 90 | DEFINES "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}") |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 91 | |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 92 | add_custom_command(TARGET ${_MY_PARAMS_NS_BIN} |
| 93 | POST_BUILD |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 94 | #Create concatenated binary image from the two binary file |
| 95 | COMMAND ${PYTHON_EXECUTABLE} ${MCUBOOT_DIR}/scripts/assemble.py |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame] | 96 | ARGS --layout ${PREPROCESSED_FILE} |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 97 | -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, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame] | 104 | --layout ${PREPROCESSED_FILE} |
Tamas Ban | 7801ed4 | 2019-05-20 13:21:53 +0100 | [diff] [blame] | 105 | -k ${KEY_FILE} |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 106 | --align 1 |
Oliver Swede | 2144044 | 2018-07-10 09:31:32 +0100 | [diff] [blame] | 107 | -v ${IMAGE_VERSION} |
David Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 108 | ${ADD_SECURITY_COUNTER} |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 109 | -H 0x400 |
Tamas Ban | db69d52 | 2018-03-01 10:04:41 +0000 | [diff] [blame] | 110 | ${CMAKE_BINARY_DIR}/${_MY_PARAMS_FULL_BIN}.bin |
| 111 | ${CMAKE_BINARY_DIR}/${_MY_PARAMS_SIGN_BIN}.bin) |
Tamas Ban | 57bfa43 | 2018-04-13 16:05:49 +0100 | [diff] [blame] | 112 | |
| 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 Swede | 05e5ded | 2018-07-19 16:40:49 +0100 | [diff] [blame] | 127 | DESTINATION outputs/${TARGET_PLATFORM}/) |
Tamas Ban | 57bfa43 | 2018-04-13 16:05:49 +0100 | [diff] [blame] | 128 | |
| 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 Vincze | db32b21 | 2019-04-16 17:43:57 +0200 | [diff] [blame] | 136 | endfunction() |
David Vincze | 63eda7a | 2019-08-09 17:42:51 +0200 | [diff] [blame] | 137 | |
| 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 | # |
| 153 | function(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) |
| 170 | endfunction() |