1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3# Copyright (c) 2022 Cypress Semiconductor Corporation (an Infineon company)
4# or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
5#
6# SPDX-License-Identifier: BSD-3-Clause
7#
8#-------------------------------------------------------------------------------
9cmake_minimum_required(VERSION 3.15)
10cmake_policy(SET CMP0076 NEW)
11cmake_policy(SET CMP0079 NEW)
12
13add_subdirectory(ns)
14
15add_library(platform_s STATIC)
16add_library(platform_region_defs INTERFACE)
17add_library(platform_common_interface INTERFACE)
18
19if (BL2)
20    add_library(platform_bl2 STATIC)
21endif()
22
23if (BL1 AND PLATFORM_DEFAULT_BL1)
24    add_library(platform_bl1 STATIC)
25    add_library(platform_bl1_interface INTERFACE)
26endif()
27
28if (TFM_PARTITION_CRYPTO)
29    add_library(platform_crypto_keys STATIC)
30endif()
31
32set(PLATFORM_DIR ${CMAKE_CURRENT_LIST_DIR})
33
34add_subdirectory(ext/target/${TFM_PLATFORM} target)
35
36#====================== CMSIS stack override interface ========================#
37
38# NS linker scripts using the default CMSIS style naming conventions, while the
39# secure and bl2 linker scripts remain untouched (region.h compatibility).
40# To be compatible with the untouched files (which using ARMCLANG naming style),
41# we have to override __INITIAL_SP and __STACK_LIMIT labels.
42
43set(CMSIS_OVERRIDE_HEADER "${CMAKE_CURRENT_LIST_DIR}/include/cmsis_override.h")
44add_library(cmsis_stack_override INTERFACE)
45target_compile_options(cmsis_stack_override
46    INTERFACE
47        "$<$<C_COMPILER_ID:GNU>:SHELL:-include ${CMSIS_OVERRIDE_HEADER}>"
48        "$<$<C_COMPILER_ID:IAR>:SHELL:--preinclude ${CMSIS_OVERRIDE_HEADER}>"
49)
50
51#========================= Platform Common interface ==========================#
52
53target_include_directories(platform_common_interface
54    INTERFACE
55        ./ext
56        ./ext/cmsis
57        ./ext/common
58        ./ext/driver
59        ./include
60)
61
62#========================= Platform Secure ====================================#
63
64target_include_directories(platform_s
65    PUBLIC
66        $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/accelerator/interface>
67)
68
69target_sources(platform_s
70    PRIVATE
71        $<$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_ps.c>
72        $<$<BOOL:${TFM_PARTITION_INTERNAL_TRUSTED_STORAGE}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_its.c>
73        $<$<BOOL:${PLATFORM_DEFAULT_SYSTEM_RESET_HALT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_reset_halt.c>
74        $<$<BOOL:${PLATFORM_DEFAULT_UART_STDOUT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/uart_stdout.c>
75        $<$<BOOL:${TFM_SPM_LOG_RAW_ENABLED}>:ext/common/tfm_hal_spm_logdev_peripheral.c>
76        $<$<BOOL:${TFM_EXCEPTION_INFO_DUMP}>:ext/common/exception_info.c>
77        $<$<BOOL:${PLATFORM_DEFAULT_ATTEST_HAL}>:ext/common/template/attest_hal.c>
78        $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
79        $<$<BOOL:${PLATFORM_DEFAULT_ROTPK}>:ext/common/template/tfm_rotpk.c>
80        $<$<BOOL:${PLATFORM_DEFAULT_NV_SEED}>:ext/common/template/crypto_nv_seed.c>
81        $<$<AND:$<NOT:$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>>,$<BOOL:${TEST_S_ATTESTATION}>>:ext/common/template/tfm_initial_attest_pub_key.c>
82        $<$<OR:$<AND:$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>,$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>>,$<BOOL:${PLATFORM_DEFAULT_OTP}>>:ext/common/template/flash_otp_nv_counters_backend.c>
83        $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
84        $<$<BOOL:${PLATFORM_DEFAULT_PROVISIONING}>:ext/common/provisioning.c>
85        $<$<OR:$<BOOL:${TEST_S_FPU}>,$<BOOL:${TEST_NS_FPU}>>:${CMAKE_SOURCE_DIR}/platform/ext/common/test_interrupt.c>
86)
87
88# If this is not added to the tfm_s it will not correctly override the weak
89# default handlers declared in assemebly, and will instead be discarded as they
90# are not in use.
91target_sources(tfm_s
92    PRIVATE
93        ext/common/faults.c
94)
95
96target_link_libraries(platform_s
97    PUBLIC
98        platform_common_interface
99        platform_region_defs
100        tfm_fih_headers
101        cmsis_stack_override
102    PRIVATE
103        psa_interface
104        tfm_partition_defs
105        tfm_spm_defs # For tfm_spm_log.h
106        $<$<BOOL:${TFM_PARTITION_CRYPTO}>:platform_crypto_keys>
107        $<$<BOOL:${PLATFORM_DEFAULT_ATTEST_HAL}>:tfm_sprt>
108        $<$<BOOL:${TFM_PARTITION_CRYPTO}>:crypto_service_mbedcrypto>
109        $<$<BOOL:${TFM_PARTITION_INITIAL_ATTESTATION}>:tfm_attestation_defs>
110        $<$<NOT:$<STREQUAL:${TFM_FIH_PROFILE},OFF>>:tfm_fih>
111)
112
113target_compile_definitions(platform_s
114    PUBLIC
115        TFM_SPM_LOG_LEVEL=${TFM_SPM_LOG_LEVEL}
116        $<$<BOOL:${TFM_SPM_LOG_RAW_ENABLED}>:TFM_SPM_LOG_RAW_ENABLED>
117        $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION>
118        $<$<BOOL:${TFM_EXCEPTION_INFO_DUMP}>:TFM_EXCEPTION_INFO_DUMP>
119        $<$<OR:$<VERSION_GREATER:${TFM_ISOLATION_LEVEL},1>,$<STREQUAL:"${TEST_PSA_API}","IPC">>:CONFIG_TFM_ENABLE_MEMORY_PROTECT>
120        $<$<BOOL:${TFM_PXN_ENABLE}>:TFM_PXN_ENABLE>
121        $<$<STREQUAL:${CONFIG_TFM_FLOAT_ABI},hard>:CONFIG_TFM_FLOAT_ABI=2>
122        $<$<STREQUAL:${CONFIG_TFM_FLOAT_ABI},soft>:CONFIG_TFM_FLOAT_ABI=0>
123        $<$<BOOL:${CONFIG_TFM_LAZY_STACKING}>:CONFIG_TFM_LAZY_STACKING>
124        $<$<BOOL:${CONFIG_TFM_ENABLE_CP10CP11}>:CONFIG_TFM_ENABLE_CP10CP11>
125        $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:PLATFORM_DEFAULT_CRYPTO_KEYS>
126        $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
127    PRIVATE
128        $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
129        $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
130        $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
131        $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
132)
133
134target_compile_options(platform_s
135    PUBLIC
136        ${COMPILER_CP_FLAG}
137)
138
139#========================= Platform Crypto Keys ===============================#
140
141
142if(TFM_PARTITION_CRYPTO)
143    target_sources(platform_crypto_keys
144        PRIVATE
145            $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:ext/common/template/crypto_keys.c>
146    )
147
148    target_link_libraries(platform_crypto_keys
149        PRIVATE
150            crypto_service_mbedcrypto
151            tfm_psa_rot_partition_crypto
152            platform_region_defs
153            tfm_partition_defs
154    )
155
156    target_include_directories(platform_crypto_keys
157        PRIVATE
158            $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:${CMAKE_SOURCE_DIR}/interface/include/crypto_keys>
159            ${CMAKE_BINARY_DIR}/generated/interface/include
160    )
161
162    target_compile_definitions(platform_crypto_keys
163        PRIVATE
164            MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
165            $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
166            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
167            $<$<BOOL:${TEST_S_ATTESTATION}>:TEST_S_ATTESTATION>
168            $<$<BOOL:${TEST_NS_ATTESTATION}>:TEST_NS_ATTESTATION>
169    )
170
171    target_compile_options(platform_crypto_keys
172        PUBLIC
173            ${COMPILER_CP_FLAG}
174    )
175endif()
176
177#========================= Platform BL2 =======================================#
178if(BL2)
179    #TODO import policy
180    target_include_directories(platform_bl2
181        PUBLIC
182            $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/accelerator/interface>
183            ${MCUBOOT_PATH}/boot/bootutil/include
184            ${CMAKE_SOURCE_DIR}/bl2/ext/mcuboot/include
185    )
186
187    target_sources(platform_bl2
188        PRIVATE
189            ext/common/boot_hal_bl2.c
190            $<$<BOOL:${PLATFORM_DEFAULT_UART_STDOUT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/uart_stdout.c>
191            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
192            $<$<BOOL:${PLATFORM_DEFAULT_ROTPK}>:ext/common/template/tfm_rotpk.c>
193            $<$<OR:$<AND:$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>,$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>>,$<BOOL:${PLATFORM_DEFAULT_OTP}>>:ext/common/template/flash_otp_nv_counters_backend.c>
194            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
195    )
196
197    target_link_libraries(platform_bl2
198        PUBLIC
199            platform_common_interface
200            platform_region_defs
201            cmsis_stack_override
202        PRIVATE
203            bl2_hal
204            mcuboot_config
205    )
206
207    target_compile_definitions(platform_bl2
208        PUBLIC
209            BL2
210            MCUBOOT_${MCUBOOT_UPGRADE_STRATEGY}
211            $<$<BOOL:${MCUBOOT_DIRECT_XIP_REVERT}>:MCUBOOT_DIRECT_XIP_REVERT>
212            $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
213            $<$<BOOL:${MCUBOOT_HW_KEY}>:MCUBOOT_HW_KEY>
214            MCUBOOT_FIH_PROFILE_${MCUBOOT_FIH_PROFILE}
215            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
216            $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION>
217            $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
218            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
219            $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
220    )
221
222    target_compile_options(platform_bl2
223        PUBLIC
224            ${BL2_COMPILER_CP_FLAG}
225    )
226endif()
227
228#========================= Platform BL1_1 =====================================#
229
230if(BL1 AND PLATFORM_DEFAULT_BL1)
231    target_include_directories(platform_bl1_interface
232        INTERFACE
233            .
234            ./include
235            ./ext/cmsis
236            ./ext/driver
237    )
238
239    target_link_libraries(platform_bl1_interface
240        INTERFACE
241            platform_region_defs
242            platform_common_interface
243            cmsis_stack_override
244    )
245
246    target_compile_definitions(platform_bl1_interface
247        INTERFACE
248            $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:CRYPTO_HW_ACCELERATOR>
249            $<$<BOOL:${TFM_BL1_LOGGING}>:TFM_BL1_LOGGING>
250            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
251            $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION>
252            $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
253            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
254            $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
255            $<$<BOOL:${TFM_BL1_MEMORY_MAPPED_FLASH}>:TFM_BL1_MEMORY_MAPPED_FLASH>
256    )
257
258    target_sources(platform_bl1
259        PRIVATE
260            ./ext/common/boot_hal_bl1.c
261            ./ext/common/uart_stdout.c
262            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
263            $<$<OR:$<AND:$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>,$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>>,$<BOOL:${PLATFORM_DEFAULT_OTP}>>:ext/common/template/flash_otp_nv_counters_backend.c>
264            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
265    )
266
267    target_link_libraries(platform_bl1
268        PUBLIC
269            platform_bl1_interface
270        PRIVATE
271            tfm_fih
272            tfm_fih_headers
273            $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:bl1_crypto_hw>
274            tfm_boot_status
275    )
276
277endif()
278
279#========================= Platform region defs ===============================#
280
281#TODO maybe just link the other platforms to this
282target_compile_definitions(platform_region_defs
283    INTERFACE
284        $<$<BOOL:${BL1}>:BL1>
285        $<$<BOOL:${BL2}>:BL2>
286        BL2_HEADER_SIZE=${BL2_HEADER_SIZE}
287        BL2_TRAILER_SIZE=${BL2_TRAILER_SIZE}
288        BL1_HEADER_SIZE=${BL1_HEADER_SIZE}
289        BL1_TRAILER_SIZE=${BL1_TRAILER_SIZE}
290        $<$<BOOL:${PLATFORM_DEFAULT_BL1}>:PLATFORM_DEFAULT_BL1>
291        $<$<BOOL:${SECURE_UART1}>:SECURE_UART1>
292        DAUTH_${DEBUG_AUTHENTICATION}
293        $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
294        $<$<STREQUAL:${MCUBOOT_SIGNATURE_TYPE},RSA>:MCUBOOT_SIGN_RSA>
295        $<$<STREQUAL:${MCUBOOT_SIGNATURE_TYPE},RSA>:MCUBOOT_SIGN_RSA_LEN=${MCUBOOT_SIGNATURE_KEY_LEN}>
296        $<$<STREQUAL:${MCUBOOT_EXECUTION_SLOT},2>:LINK_TO_SECONDARY_PARTITION>
297        $<$<BOOL:${TEST_PSA_API}>:PSA_API_TEST_${TEST_PSA_API}>
298        $<$<BOOL:${TFM_CODE_SHARING}>:CODE_SHARING>
299        $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:ENABLE_HEAP>
300        PLATFORM_NS_NV_COUNTERS=${TFM_NS_NV_COUNTER_AMOUNT}
301)
302
303target_include_directories(platform_region_defs
304    INTERFACE
305        ${CMAKE_CURRENT_LIST_DIR}/ext/common
306)
307
308target_link_libraries(platform_region_defs
309    INTERFACE
310        tfm_config
311)
312