blob: d8a14cbe8679cb037087145bcf7634e4c94a18ce [file] [log] [blame]
Tamas Ban581034a2017-12-19 19:54:37 +00001#------------------------------------------------------------------------------
David Vinczec3e313a2020-01-06 17:31:11 +01002# Copyright (c) 2017-2020, Arm Limited. All rights reserved.
Tamas Ban581034a2017-12-19 19:54:37 +00003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.7)
9
David Hu857bfa52019-05-21 13:54:50 +080010set(TFM_BUILD_IN_SPE ON)
11
Tamas Ban581034a2017-12-19 19:54:37 +000012#Tell cmake where our modules can be found
13list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../cmake)
14
15#Include common stuff to control cmake.
16include("Common/BuildSys")
17
18#Start an embedded project.
David Vinczec3e313a2020-01-06 17:31:11 +010019get_filename_component(TFM_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
Ken Liue40f9a22019-06-03 16:42:47 +080020embedded_project_start(CONFIG "${TFM_ROOT_DIR}/configs/ConfigDefault.cmake")
Tamas Ban581034a2017-12-19 19:54:37 +000021project(mcuboot LANGUAGES ASM C)
22embedded_project_fixup()
23
David Vinczec3e313a2020-01-06 17:31:11 +010024#Set the appropriate MCUBoot path
25if (MCUBOOT_REPO STREQUAL "TF-M")
26 get_filename_component(MCUBOOT_DIR ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
27else()
28 get_filename_component(MCUBOOT_DIR "${TFM_ROOT_DIR}/../mcuboot/boot" ABSOLUTE)
29 if (NOT EXISTS ${MCUBOOT_DIR})
30 message(FATAL_ERROR "Missing MCUBoot. Please clone the MCUBoot repo to directory \"${MCUBOOT_DIR}\".")
31 endif()
32endif()
33
Tamas Ban581034a2017-12-19 19:54:37 +000034#Check input variables
35if (NOT DEFINED BL2)
36 message(FATAL ERROR "Incomplete build configuration: BL2 is undefined.")
37elseif(NOT BL2)
38 #If mcuboot is not need to be built then stop further processing.
39 return()
40endif()
41
Raef Coles1bb168e2019-10-17 09:04:55 +010042if (NOT DEFINED MBEDCRYPTO_C_FLAGS_BL2)
43 message(FATAL_ERROR "Incomplete build configuration: MBEDCRYPTO_C_FLAGS_BL2 is undefined.")
Tamas Ban581034a2017-12-19 19:54:37 +000044endif()
45
Marc Moreno Berenguea1f296f2018-01-25 15:21:22 +000046set(BUILD_CMSIS_CORE On)
47set(BUILD_RETARGET On)
48set(BUILD_NATIVE_DRIVERS On)
49set(BUILD_STARTUP On)
50set(BUILD_TARGET_CFG Off)
David Vincze060968d2019-05-23 01:13:14 +020051set(BUILD_TARGET_NV_COUNTERS On)
Marc Moreno Berenguea1f296f2018-01-25 15:21:22 +000052set(BUILD_CMSIS_DRIVERS On)
53set(BUILD_TIME Off)
54set(BUILD_UART_STDOUT On)
55set(BUILD_FLASH On)
Mate Toth-Pald3c77662019-02-20 16:23:00 +010056set(BUILD_PLAT_TEST Off)
Tamas Band4bf3472019-09-06 12:59:56 +010057set(BUILD_BOOT_HAL On)
Tamas Band0f4e1d2019-07-11 09:39:03 +010058
59if (MCUBOOT_HW_KEY)
60 set(BUILD_TARGET_HARDWARE_KEYS On)
61else()
62 set(BUILD_TARGET_HARDWARE_KEYS Off)
63endif()
64
Marc Moreno Berenguea1f296f2018-01-25 15:21:22 +000065if(NOT DEFINED PLATFORM_CMAKE_FILE)
66 message (FATAL_ERROR "Platform specific CMake is not defined. Please set PLATFORM_CMAKE_FILE.")
67elseif(NOT EXISTS ${PLATFORM_CMAKE_FILE})
68 message (FATAL_ERROR "Platform specific CMake \"${PLATFORM_CMAKE_FILE}\" file does not exist. Please fix value of PLATFORM_CMAKE_FILE.")
69else()
70 include(${PLATFORM_CMAKE_FILE})
71endif()
72
David Hu857bfa52019-05-21 13:54:50 +080073#Add platform specific definitions in SPE
74if (DEFINED TFM_PLATFORM_SECURE_DEFS)
75 embedded_set_target_compile_defines(TARGET ${PROJECT_NAME} LANGUAGE C DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
76 embedded_set_target_compile_defines(TARGET ${PROJECT_NAME} LANGUAGE ASM DEFINES ${TFM_PLATFORM_SECURE_DEFS} APPEND)
77endif()
78
Tamas Ban581034a2017-12-19 19:54:37 +000079#Append all our source files to global lists.
Tamas Bana9de4a62018-09-18 08:09:45 +010080list(APPEND ALL_SRC_C
David Vinczec3e313a2020-01-06 17:31:11 +010081 "${TFM_ROOT_DIR}/bl2/ext/mcuboot/bl2_main.c"
82 "${TFM_ROOT_DIR}/bl2/ext/mcuboot/flash_map_extended.c"
83 "${TFM_ROOT_DIR}/bl2/ext/mcuboot/flash_map_legacy.c"
84 "${TFM_ROOT_DIR}/bl2/ext/mcuboot/keys.c"
85 "${TFM_ROOT_DIR}/bl2/src/flash_map.c"
Tamas Ban581034a2017-12-19 19:54:37 +000086 "${MCUBOOT_DIR}/bootutil/src/loader.c"
87 "${MCUBOOT_DIR}/bootutil/src/bootutil_misc.c"
88 "${MCUBOOT_DIR}/bootutil/src/image_validate.c"
89 "${MCUBOOT_DIR}/bootutil/src/image_rsa.c"
David Vincze07706a42019-12-12 18:20:12 +010090 "${MCUBOOT_DIR}/bootutil/src/tlv.c"
Tamas Ban581034a2017-12-19 19:54:37 +000091 )
92
David Vinczec3e313a2020-01-06 17:31:11 +010093if (MCUBOOT_REPO STREQUAL "TF-M")
94 list(APPEND ALL_SRC_C
95 "${TFM_ROOT_DIR}/bl2/src/boot_record.c"
96 "${TFM_ROOT_DIR}/bl2/src/security_cnt.c"
97 )
98endif()
99
Raef Coles1bb168e2019-10-17 09:04:55 +0100100#Define location of Mbed Crypto source, build, and installation directory.
David Vinczecea8b592019-10-29 16:09:51 +0100101set(MBEDTLS_CONFIG_FILE "config-rsa.h")
Raef Coles0e82adc2019-10-17 15:06:26 +0100102set(MBEDTLS_CONFIG_PATH "${TFM_ROOT_DIR}/bl2/ext/mcuboot/include")
Raef Coles1bb168e2019-10-17 09:04:55 +0100103get_filename_component(MBEDCRYPTO_SOURCE_DIR "${TFM_ROOT_DIR}/../mbed-crypto" ABSOLUTE)
104if(NOT EXISTS ${MBEDCRYPTO_SOURCE_DIR})
105 message(FATAL_ERROR "Missing mbed-crypto. Please clone the mbed-crypto repo to directory \"${MBEDCRYPTO_SOURCE_DIR}\".")
Tamas Ban581034a2017-12-19 19:54:37 +0000106endif()
Raef Coles1bb168e2019-10-17 09:04:55 +0100107set (MBEDCRYPTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbed-crypto/build")
108set (MBEDCRYPTO_INSTALL_DIR ${MBEDCRYPTO_BINARY_DIR}/../install)
Tamas Ban581034a2017-12-19 19:54:37 +0000109
Tamas Banf824e742019-10-25 21:22:26 +0100110if (CRYPTO_HW_ACCELERATOR OR CRYPTO_HW_ACCELERATOR_OTP_STATE STREQUAL "PROVISIONING")
Raef Coles0e82adc2019-10-17 15:06:26 +0100111 if(NOT DEFINED CRYPTO_HW_ACCELERATOR_CMAKE_BUILD)
112 message(FATAL_ERROR "CRYPTO_HW_ACCELERATOR_CMAKE_BUILD not defined.")
113 endif()
114 include(${CRYPTO_HW_ACCELERATOR_CMAKE_BUILD})
115endif()
116
Raef Coles1bb168e2019-10-17 09:04:55 +0100117#Build Mbed Crypto as external project.
118#This ensures Mbed Crypto is built with exactly defined settings.
Raef Coles0e82adc2019-10-17 15:06:26 +0100119#Mbed Crypto will be used from its install location
120string(APPEND MBEDCRYPTO_C_FLAGS " ${MBEDCRYPTO_C_FLAGS_BL2}")
Raef Coles1bb168e2019-10-17 09:04:55 +0100121set(MBEDCRYPTO_TARGET_NAME "mbedcrypto_mcuboot_lib")
122include(${TFM_ROOT_DIR}/BuildMbedCrypto.cmake)
Tamas Ban581034a2017-12-19 19:54:37 +0000123
124#Setting include directories
Tamas Bandb69d522018-03-01 10:04:41 +0000125embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${TFM_ROOT_DIR} ABSOLUTE APPEND)
Raef Coles4d6ea2f2019-10-15 14:30:40 +0100126embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${TFM_ROOT_DIR}/interface/include ABSOLUTE APPEND)
Tamas Bana9de4a62018-09-18 08:09:45 +0100127embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${TFM_ROOT_DIR}/bl2/include ABSOLUTE APPEND)
Tamas Bandb69d522018-03-01 10:04:41 +0000128embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${TFM_ROOT_DIR}/bl2/ext/mcuboot/include ABSOLUTE APPEND)
David Vinczec3e313a2020-01-06 17:31:11 +0100129embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${MCUBOOT_DIR}/bootutil/include ABSOLUTE APPEND)
Raef Coles1bb168e2019-10-17 09:04:55 +0100130embedded_target_include_directories(TARGET ${PROJECT_NAME} PATH ${MBEDCRYPTO_INSTALL_DIR}/include ABSOLUTE APPEND)
Tamas Ban581034a2017-12-19 19:54:37 +0000131
132#Define linker file
Mate Toth-Pal48fc6a02018-01-24 09:50:14 +0100133if(NOT DEFINED BL2_LINKER_CONFIG)
134 message(FATAL_ERROR "ERROR: Incomplete Configuration: BL2_LINKER_CONFIG is not defined.")
135endif()
Gabor Kerteszd7d7d742018-07-04 11:50:05 +0200136embedded_set_target_linker_file(TARGET ${PROJECT_NAME} PATH "${BL2_LINKER_CONFIG}")
137
138if(NOT DEFINED PLATFORM_LINK_INCLUDES)
139 message(FATAL_ERROR "ERROR: Incomplete Configuration: PLATFORM_LINK_INCLUDES is not defined.")
140endif()
141embedded_set_target_link_includes(TARGET ${PROJECT_NAME} INCLUDES "${PLATFORM_LINK_INCLUDES}")
Tamas Ban581034a2017-12-19 19:54:37 +0000142
David Hu857bfa52019-05-21 13:54:50 +0800143add_executable(${PROJECT_NAME} ${ALL_SRC_ASM} ${ALL_SRC_C_BL2} ${ALL_SRC_ASM_BL2} ${ALL_SRC_C} ${ALL_SRC_CXX})
Tamas Ban581034a2017-12-19 19:54:37 +0000144
Tamas Bandb69d522018-03-01 10:04:41 +0000145#Set common compiler and linker flags
146config_setting_shared_compiler_flags(${PROJECT_NAME})
147config_setting_shared_linker_flags(${PROJECT_NAME})
148
David Vincze63eda7a2019-08-09 17:42:51 +0200149#Add BL2 and MCUBOOT_IMAGE_NUMBER defines to linker to resolve symbols in region_defs.h and flash_layout.h
150embedded_set_target_link_defines(TARGET ${PROJECT_NAME} DEFINES "BL2" "MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}")
Tamas Ban581034a2017-12-19 19:54:37 +0000151
Marc Moreno Berenguecae2c532018-10-09 12:58:46 +0100152if(NOT DEFINED TEST_FRAMEWORK_S)
153 message(FATAL_ERROR "Incomplete build configuration: TEST_FRAMEWORK_S is undefined.")
154elseif(TEST_FRAMEWORK_S)
155 embedded_set_target_link_defines(TARGET ${PROJECT_NAME} DEFINES "TEST_FRAMEWORK_S")
156endif()
157
158if(NOT DEFINED TEST_FRAMEWORK_NS)
159 message(FATAL_ERROR "Incomplete build configuration: TEST_FRAMEWORK_NS is undefined.")
160elseif(TEST_FRAMEWORK_NS)
161 embedded_set_target_link_defines(TARGET ${PROJECT_NAME} DEFINES "TEST_FRAMEWORK_NS")
162endif()
163
Tamas Ban581034a2017-12-19 19:54:37 +0000164#Link mbedcrypto library to project
Raef Coles1bb168e2019-10-17 09:04:55 +0100165target_link_libraries(${PROJECT_NAME} "${MBEDCRYPTO_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX_C}mbedcrypto${CMAKE_STATIC_LIBRARY_SUFFIX_C}")
166add_dependencies(${PROJECT_NAME} ${MBEDCRYPTO_TARGET_NAME}_install)
Tamas Ban581034a2017-12-19 19:54:37 +0000167
Raef Coles0e82adc2019-10-17 15:06:26 +0100168#Link crypto accelerator libraries if applicable
Tamas Banf824e742019-10-25 21:22:26 +0100169if (CRYPTO_HW_ACCELERATOR OR CRYPTO_HW_ACCELERATOR_OTP_STATE STREQUAL "PROVISIONING")
Raef Coles0e82adc2019-10-17 15:06:26 +0100170 if(NOT DEFINED CRYPTO_HW_ACCELERATOR_CMAKE_LINK)
171 message(FATAL_ERROR "CRYPTO_HW_ACCELERATOR_CMAKE_LINK not defined.")
172 endif()
173 include(${CRYPTO_HW_ACCELERATOR_CMAKE_LINK})
174endif()
175
Tamas Ban581034a2017-12-19 19:54:37 +0000176#Generate binary file from axf
177compiler_generate_binary_output(${PROJECT_NAME})
178
David Vinczec3e313a2020-01-06 17:31:11 +0100179message("- MCUBOOT_REPO: '${MCUBOOT_REPO}'.")
David Vincze73dfbc52019-10-11 13:54:58 +0200180message("- MCUBOOT_IMAGE_NUMBER: '${MCUBOOT_IMAGE_NUMBER}'.")
181message("- MCUBOOT_UPGRADE_STRATEGY: '${MCUBOOT_UPGRADE_STRATEGY}'.")
182message("- MCUBOOT_SIGNATURE_TYPE: '${MCUBOOT_SIGNATURE_TYPE}'.")
183message("- MCUBOOT_HW_KEY: '${MCUBOOT_HW_KEY}'.")
184message("- MCUBOOT_LOG_LEVEL: '${MCUBOOT_LOG_LEVEL}'.")
David Vincze54d05552019-08-05 12:58:47 +0200185
Tamas Ban581034a2017-12-19 19:54:37 +0000186#Set macro definitions for the project.
187target_compile_definitions(${PROJECT_NAME} PRIVATE
David Vincze8bdfc2d2019-03-18 15:49:23 +0100188 MCUBOOT_VALIDATE_PRIMARY_SLOT
Tamas Ban581034a2017-12-19 19:54:37 +0000189 MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Raef Coles0e82adc2019-10-17 15:06:26 +0100190 MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}"
Tamas Bandb69d522018-03-01 10:04:41 +0000191 MCUBOOT_TARGET_CONFIG="flash_layout.h")
192
David Vinczec3e313a2020-01-06 17:31:11 +0100193if (MCUBOOT_REPO STREQUAL "UPSTREAM")
194 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_USE_UPSTREAM)
195endif()
196
Tamas Ban7801ed42019-05-20 13:21:53 +0100197if (MCUBOOT_SIGNATURE_TYPE STREQUAL "RSA-3072")
198 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_SIGN_RSA MCUBOOT_SIGN_RSA_LEN=3072)
199elseif(MCUBOOT_SIGNATURE_TYPE STREQUAL "RSA-2048")
200 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_SIGN_RSA MCUBOOT_SIGN_RSA_LEN=2048)
201else()
202 message(FATAL_ERROR "${MCUBOOT_SIGNATURE_TYPE} is not supported as firmware signing algorithm")
203endif()
204
David Vincze4638b2a2019-05-24 10:14:23 +0200205if (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "OVERWRITE_ONLY")
206 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_OVERWRITE_ONLY)
207elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP")
Tamas Bandb69d522018-03-01 10:04:41 +0000208 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_NO_SWAP)
David Vincze4638b2a2019-05-24 10:14:23 +0200209elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
Oliver Swedef9982442018-08-24 18:37:44 +0100210 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_RAM_LOADING)
David Vincze4638b2a2019-05-24 10:14:23 +0200211elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "SWAP")
212 #No compile definition needs to be specified for this upgrade strategy
213else()
David Vincze54d05552019-08-05 12:58:47 +0200214 get_property(_upgrade_strategies CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
215 message(FATAL_ERROR "ERROR: MCUBoot supports the ${_upgrade_strategies} upgrade strategies only.")
Oliver Swedef9982442018-08-24 18:37:44 +0100216endif()
217
Tamas Band0f4e1d2019-07-11 09:39:03 +0100218if (MCUBOOT_HW_KEY)
219 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_HW_KEY)
220endif()
221
David Vincze219a1752019-10-14 11:35:09 +0200222if (ATTEST_BOOT_INTERFACE STREQUAL "INDIVIDUAL_CLAIMS")
223 target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_INDIVIDUAL_CLAIMS)
224 message(WARNING "ATTEST_BOOT_INTERFACE was set to ${ATTEST_BOOT_INTERFACE}. This configuration is "
225 "deprecated and this feature will probably be removed from MCUBoot in the future.")
226endif()
227
David Vincze73dfbc52019-10-11 13:54:58 +0200228#Configure log level for MCUBoot.
229get_property(_log_levels CACHE MCUBOOT_LOG_LEVEL PROPERTY STRINGS)
230list(FIND _log_levels ${MCUBOOT_LOG_LEVEL} LOG_LEVEL_ID)
David Vincze99f1b362019-12-12 16:17:35 +0100231target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_LOG_LEVEL=${LOG_LEVEL_ID})
David Vincze73dfbc52019-10-11 13:54:58 +0200232
Tamas Bandb69d522018-03-01 10:04:41 +0000233#Set install location. Keep original value to avoid overriding command line settings.
234if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
235 set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Default install location for MCUBoot." FORCE)
236endif()
237
238#Collect executables to common location: build/install/outputs/
239install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.axf
240 ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin
241 DESTINATION outputs/${TARGET_PLATFORM}/)
242
243install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.axf
244 ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin
245 DESTINATION outputs/fvp/)
Tamas Ban581034a2017-12-19 19:54:37 +0000246
247#Finally let cmake system apply changes after the whole project is defined.
248embedded_project_end(${PROJECT_NAME})