1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
8cmake_minimum_required(VERSION 3.15)
9
10project("Bootloader" VERSION 0.1.0 LANGUAGES C ASM)
11
12add_executable(bl2
13    src/flash_map.c
14    $<$<BOOL:${DEFAULT_MCUBOOT_SECURITY_COUNTERS}>:src/security_cnt.c>
15    $<$<BOOL:${DEFAULT_MCUBOOT_FLASH_MAP}>:src/default_flash_map.c>
16    $<$<BOOL:${MCUBOOT_DATA_SHARING}>:src/shared_data.c>
17    $<$<BOOL:${PLATFORM_DEFAULT_PROVISIONING}>:src/provisioning.c>
18    $<$<BOOL:${CONFIG_GNU_SYSCALL_STUB_ENABLED}>:${CMAKE_SOURCE_DIR}/platform/ext/common/syscalls_stub.c>
19)
20
21add_subdirectory(ext/mcuboot)
22
23set_target_properties(bl2
24    PROPERTIES
25        SUFFIX ".axf"
26        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
27)
28
29target_include_directories(bl2
30    PRIVATE
31        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
32        $<BUILD_INTERFACE:${MCUBOOT_PATH}/boot/bootutil/src>
33)
34
35target_link_libraries(bl2
36    PRIVATE
37        tfm_boot_status
38        $<$<BOOL:${TEST_BL2}>:mcuboot_tests>
39        platform_bl2
40)
41
42target_compile_options(bl2
43    PRIVATE
44        ${BL2_COMPILER_CP_FLAG}
45)
46
47target_link_options(bl2
48    PRIVATE
49        $<$<C_COMPILER_ID:GNU>:-Wl,-Map=${CMAKE_BINARY_DIR}/bin/bl2.map>
50        $<$<C_COMPILER_ID:ARMClang>:--map;${BL2_LINKER_CP_OPTION}>
51        $<$<C_COMPILER_ID:IAR>:--map\;${CMAKE_BINARY_DIR}/bin/bl2.map>
52)
53
54target_compile_definitions(bl2
55    PRIVATE
56        $<$<BOOL:${DEFAULT_MCUBOOT_FLASH_MAP}>:DEFAULT_MCUBOOT_FLASH_MAP>
57        $<$<BOOL:${PLATFORM_PSA_ADAC_SECURE_DEBUG}>:PLATFORM_PSA_ADAC_SECURE_DEBUG>
58        $<$<BOOL:${TEST_BL2}>:TEST_BL2>
59        $<$<BOOL:${TFM_PARTITION_FIRMWARE_UPDATE}>:TFM_PARTITION_FIRMWARE_UPDATE>
60        $<$<AND:$<BOOL:${CONFIG_TFM_BOOT_STORE_MEASUREMENTS}>,$<NOT:$<BOOL:${CONFIG_TFM_BOOT_STORE_ENCODED_MEASUREMENTS}>>>:TFM_MEASURED_BOOT_API>
61)
62
63add_convert_to_bin_target(bl2)
64
65############################### BOOT HAL # #####################################
66
67add_library(bl2_hal INTERFACE)
68
69target_include_directories(bl2_hal
70    INTERFACE
71        include
72)
73
74############################### MBEDCRYPTO #####################################
75
76add_library(bl2_mbedcrypto_config INTERFACE)
77
78if(NOT ${MCUBOOT_SIGNATURE_TYPE} STREQUAL "")
79    string(REGEX MATCH "[0-9]*$" SIG_LEN ${MCUBOOT_SIGNATURE_TYPE})
80    string(REGEX MATCH "^[A-Z]*" SIG_TYPE ${MCUBOOT_SIGNATURE_TYPE})
81endif()
82
83target_compile_definitions(bl2_mbedcrypto_config
84    INTERFACE
85        $<$<STREQUAL:${SIG_TYPE},RSA>:MCUBOOT_SIGN_RSA>
86        $<$<STREQUAL:${SIG_TYPE},RSA>:MCUBOOT_SIGN_RSA_LEN=${SIG_LEN}>
87        $<$<BOOL:${MCUBOOT_USE_PSA_CRYPTO}>:MCUBOOT_USE_PSA_CRYPTO>
88        $<$<STREQUAL:${SIG_TYPE},EC>:MCUBOOT_SIGN_EC${SIG_LEN}>
89        MBEDTLS_CONFIG_FILE="${MCUBOOT_MBEDCRYPTO_CONFIG_FILEPATH}"
90        # Workaround for https://github.com/ARMmbed/mbedtls/issues/1077
91        $<$<OR:$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv8-m.base>,$<STREQUAL:${TFM_SYSTEM_ARCHITECTURE},armv6-m>>:MULADDC_CANNOT_USE_R7>
92        $<$<BOOL:${CC312_LEGACY_DRIVER_API_ENABLED}>:CC312_LEGACY_DRIVER_API_ENABLED>
93)
94
95cmake_policy(SET CMP0079 NEW)
96
97set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
98set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
99set(ENABLE_TESTING OFF)
100set(ENABLE_PROGRAMS OFF)
101set(MBEDTLS_FATAL_WARNINGS OFF)
102set(ENABLE_DOCS OFF)
103set(INSTALL_MBEDTLS_HEADERS OFF)
104set(LIB_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/mbedcrypto/install)
105set(GEN_FILES OFF)
106
107# Set the prefix to be used by mbedTLS targets
108set(MBEDTLS_TARGET_PREFIX bl2_)
109
110# Mbedcrypto is quite a large lib, and it uses too much memory for it to be
111# reasonable to build it in debug info. As a compromise, if `debug` build type
112# is selected mbedcrypto will build under `relwithdebinfo` which preserved debug
113# symbols whild optimizing space.
114set(SAVED_BUILD_TYPE ${CMAKE_BUILD_TYPE})
115set(CMAKE_BUILD_TYPE ${MBEDCRYPTO_BUILD_TYPE})
116add_subdirectory(${MBEDCRYPTO_PATH} ${CMAKE_CURRENT_BINARY_DIR}/mbedcrypto EXCLUDE_FROM_ALL)
117set(CMAKE_BUILD_TYPE ${SAVED_BUILD_TYPE} CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
118
119if(NOT TARGET ${MBEDTLS_TARGET_PREFIX}mbedcrypto)
120    message(FATAL_ERROR "Target ${MBEDTLS_TARGET_PREFIX}mbedcrypto does not exist. Have the patches in ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto been applied to the mbedcrypto repo at ${MBEDCRYPTO_PATH} ?
121    Hint: The command might be `cd ${MBEDCRYPTO_PATH} && git apply ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto/*.patch`")
122endif()
123
124target_link_libraries(${MBEDTLS_TARGET_PREFIX}mbedcrypto
125    PUBLIC
126        bl2_mbedcrypto_config
127)
128
129target_include_directories(${MBEDTLS_TARGET_PREFIX}mbedcrypto
130    PUBLIC
131        ${MBEDCRYPTO_PATH}/library
132)
133
134target_compile_options(bl2_mbedcrypto
135    PRIVATE
136        ${BL2_COMPILER_CP_FLAG}
137)
138
139target_compile_options(bl2_mbedtls
140    PRIVATE
141        ${BL2_COMPILER_CP_FLAG}
142)
143
144target_compile_options(bl2_mbedx509
145    PRIVATE
146        ${BL2_COMPILER_CP_FLAG}
147)
148
149############################### CODE SHARING ###################################
150
151if (TFM_CODE_SHARING)
152    target_share_symbols(bl2 ${CMAKE_CURRENT_SOURCE_DIR}/bl2_shared_symbols.txt)
153
154    if (NOT EXISTS ${MBEDCRYPTO_PATH}/library/code_share.c)
155        message(FATAL_ERROR "File ${MBEDCRYPTO_PATH}/library/code_share.c does not exist.
156        Have the patch ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto/0005-Enable-crypto-code-sharing-between-independent-binar.patch
157        been applied to the mbedcrypto repo at ${MBEDCRYPTO_PATH}?
158        Hint: The command might be `cd ${MBEDCRYPTO_PATH} && git apply ${CMAKE_SOURCE_DIR}/lib/ext/mbedcrypto/*.patch`")
159    endif()
160endif()
161