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_library(platform_s STATIC)
14add_library(platform_region_defs INTERFACE)
15add_library(platform_common_interface INTERFACE)
16
17if (BL2)
18    add_library(platform_bl2 STATIC)
19endif()
20
21if (BL1 AND PLATFORM_DEFAULT_BL1)
22    add_library(platform_bl1_1 STATIC)
23    add_library(platform_bl1_2 STATIC)
24    add_library(platform_bl1_1_interface INTERFACE)
25endif()
26
27if (TFM_PARTITION_CRYPTO)
28    add_library(platform_crypto_keys STATIC)
29endif()
30
31set(PLATFORM_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path to platform directory")
32
33add_subdirectory(ext/target/${TFM_PLATFORM} target)
34
35#====================== CMSIS stack override interface ========================#
36
37# NS linker scripts using the default CMSIS style naming conventions, while the
38# secure and bl2 linker scripts remain untouched (region.h compatibility).
39# To be compatible with the untouched files (which using ARMCLANG naming style),
40# we have to override __INITIAL_SP and __STACK_LIMIT labels.
41
42set(CMSIS_OVERRIDE_HEADER "${CMAKE_CURRENT_LIST_DIR}/include/cmsis_override.h")
43add_library(cmsis_stack_override INTERFACE)
44target_compile_options(cmsis_stack_override
45    INTERFACE
46        "$<$<C_COMPILER_ID:GNU>:SHELL:-include ${CMSIS_OVERRIDE_HEADER}>"
47        "$<$<C_COMPILER_ID:IAR>:SHELL:--preinclude ${CMSIS_OVERRIDE_HEADER}>"
48)
49
50#========================= Platform Common interface ==========================#
51
52target_include_directories(platform_common_interface
53    INTERFACE
54        ext
55        ext/cmsis
56        ext/common
57        ext/driver
58        include
59)
60
61#========================= Platform Secure ====================================#
62
63target_include_directories(platform_s
64    PUBLIC
65        $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/accelerator/interface>
66)
67
68target_sources(platform_s
69    PRIVATE
70        $<$<BOOL:${TFM_PARTITION_PROTECTED_STORAGE}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_ps.c>
71        $<$<BOOL:${TFM_PARTITION_INTERNAL_TRUSTED_STORAGE}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_its.c>
72        $<$<BOOL:${PLATFORM_DEFAULT_SYSTEM_RESET_HALT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/tfm_hal_reset_halt.c>
73        $<$<BOOL:${PLATFORM_DEFAULT_UART_STDOUT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/uart_stdout.c>
74        $<$<BOOL:${TFM_SPM_LOG_RAW_ENABLED}>:ext/common/tfm_hal_spm_logdev_peripheral.c>
75        $<$<BOOL:${TFM_EXCEPTION_INFO_DUMP}>:ext/common/exception_info.c>
76        $<$<BOOL:${PLATFORM_DEFAULT_ATTEST_HAL}>:ext/common/template/attest_hal.c>
77        $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
78        $<$<BOOL:${PLATFORM_DEFAULT_ROTPK}>:ext/common/template/tfm_rotpk.c>
79        $<$<BOOL:${PLATFORM_DEFAULT_NV_SEED}>:ext/common/template/crypto_nv_seed.c>
80        $<$<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>
81        $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
82        $<$<BOOL:${PLATFORM_DEFAULT_PROVISIONING}>:ext/common/provisioning.c>
83        $<$<OR:$<BOOL:${TEST_S_FPU}>,$<BOOL:${TEST_NS_FPU}>>:${CMAKE_SOURCE_DIR}/platform/ext/common/test_interrupt.c>
84)
85
86# If this is not added to the tfm_s it will not correctly override the weak
87# default handlers declared in assemebly, and will instead be discarded as they
88# are not in use.
89target_sources(tfm_s
90    PRIVATE
91        ext/common/faults.c
92)
93
94target_link_libraries(platform_s
95    PUBLIC
96        platform_common_interface
97        platform_region_defs
98        tfm_fih_headers
99        cmsis_stack_override
100    PRIVATE
101        psa_interface
102        tfm_config
103        tfm_spm_defs # For tfm_spm_log.h
104        $<$<BOOL:${TFM_PARTITION_CRYPTO}>:platform_crypto_keys>
105        $<$<BOOL:${PLATFORM_DEFAULT_ATTEST_HAL}>:tfm_sprt>
106        $<$<BOOL:${TFM_PARTITION_CRYPTO}>:crypto_service_mbedcrypto>
107        $<$<BOOL:${TFM_PARTITION_INITIAL_ATTESTATION}>:tfm_attestation_defs>
108        $<$<NOT:$<STREQUAL:${TFM_FIH_PROFILE},OFF>>:tfm_fih>
109)
110
111target_compile_definitions(platform_s
112    PUBLIC
113        TFM_SPM_LOG_LEVEL=${TFM_SPM_LOG_LEVEL}
114        $<$<BOOL:${TFM_SPM_LOG_RAW_ENABLED}>:TFM_SPM_LOG_RAW_ENABLED>
115        $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION=1>
116        $<$<BOOL:${TFM_EXCEPTION_INFO_DUMP}>:TFM_EXCEPTION_INFO_DUMP>
117        $<$<OR:$<VERSION_GREATER:${TFM_ISOLATION_LEVEL},1>,$<STREQUAL:"${TEST_PSA_API}","IPC">>:CONFIG_TFM_ENABLE_MEMORY_PROTECT>
118        $<$<BOOL:${TFM_PXN_ENABLE}>:TFM_PXN_ENABLE>
119        $<$<STREQUAL:${CONFIG_TFM_FLOAT_ABI},hard>:CONFIG_TFM_FLOAT_ABI=2>
120        $<$<STREQUAL:${CONFIG_TFM_FLOAT_ABI},soft>:CONFIG_TFM_FLOAT_ABI=0>
121        $<$<BOOL:${CONFIG_TFM_LAZY_STACKING}>:CONFIG_TFM_LAZY_STACKING>
122        $<$<BOOL:${CONFIG_TFM_ENABLE_CP10CP11}>:CONFIG_TFM_ENABLE_CP10CP11>
123        $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:PLATFORM_DEFAULT_CRYPTO_KEYS>
124        $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
125        $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
126    PRIVATE
127        $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
128        $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
129        $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
130)
131
132target_compile_options(platform_s
133    PUBLIC
134        ${COMPILER_CP_FLAG}
135)
136
137#========================= Platform Crypto Keys ===============================#
138
139
140if(TFM_PARTITION_CRYPTO)
141    target_sources(platform_crypto_keys
142        PRIVATE
143            $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:ext/common/template/crypto_keys.c>
144    )
145
146    target_link_libraries(platform_crypto_keys
147        PRIVATE
148            crypto_service_mbedcrypto
149            tfm_psa_rot_partition_crypto
150            platform_region_defs
151            tfm_config
152    )
153
154    target_include_directories(platform_crypto_keys
155        PRIVATE
156            $<$<BOOL:${PLATFORM_DEFAULT_CRYPTO_KEYS}>:${CMAKE_SOURCE_DIR}/interface/include/crypto_keys>
157            ${CMAKE_BINARY_DIR}/generated/interface/include
158    )
159
160    target_compile_definitions(platform_crypto_keys
161        PRIVATE
162            MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
163            $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
164            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
165            $<$<BOOL:${TEST_S_ATTESTATION}>:TEST_S_ATTESTATION>
166            $<$<BOOL:${TEST_NS_ATTESTATION}>:TEST_NS_ATTESTATION>
167    )
168
169    target_compile_options(platform_crypto_keys
170        PUBLIC
171            ${COMPILER_CP_FLAG}
172    )
173endif()
174
175#========================= Platform BL2 =======================================#
176if(BL2)
177    #TODO import policy
178    target_include_directories(platform_bl2
179        PUBLIC
180            $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/accelerator/interface>
181            ${MCUBOOT_PATH}/boot/bootutil/include
182            ${CMAKE_SOURCE_DIR}/bl2/ext/mcuboot/include
183    )
184
185    target_sources(platform_bl2
186        PRIVATE
187            ext/common/boot_hal_bl2.c
188            $<$<BOOL:${PLATFORM_DEFAULT_UART_STDOUT}>:${CMAKE_CURRENT_SOURCE_DIR}/ext/common/uart_stdout.c>
189            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
190            $<$<BOOL:${PLATFORM_DEFAULT_ROTPK}>:ext/common/template/tfm_rotpk.c>
191            $<$<OR:$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>,$<BOOL:${PLATFORM_DEFAULT_OTP}>>:ext/common/template/flash_otp_nv_counters_backend.c>
192            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
193    )
194
195    target_link_libraries(platform_bl2
196        PUBLIC
197            platform_common_interface
198            platform_region_defs
199            cmsis_stack_override
200        PRIVATE
201            bl2_hal
202            mcuboot_config
203            $<$<AND:$<BOOL:${CONFIG_TFM_BOOT_STORE_MEASUREMENTS}>,$<NOT:$<BOOL:${CONFIG_TFM_BOOT_STORE_ENCODED_MEASUREMENTS}>>>:tfm_boot_status>
204    )
205
206    target_compile_definitions(platform_bl2
207        PUBLIC
208            BL2
209            MCUBOOT_${MCUBOOT_UPGRADE_STRATEGY}
210            $<$<BOOL:${MCUBOOT_DIRECT_XIP_REVERT}>:MCUBOOT_DIRECT_XIP_REVERT>
211            $<$<BOOL:${SYMMETRIC_INITIAL_ATTESTATION}>:SYMMETRIC_INITIAL_ATTESTATION>
212            $<$<BOOL:${MCUBOOT_HW_KEY}>:MCUBOOT_HW_KEY>
213            MCUBOOT_FIH_PROFILE_${MCUBOOT_FIH_PROFILE}
214            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
215            $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION=1>
216            $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
217            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
218            $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
219            $<$<AND:$<BOOL:${CONFIG_TFM_BOOT_STORE_MEASUREMENTS}>,$<NOT:$<BOOL:${CONFIG_TFM_BOOT_STORE_ENCODED_MEASUREMENTS}>>>:TFM_MEASURED_BOOT_API>
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_1_interface
232        INTERFACE
233            .
234            include
235            ext/cmsis
236            ext/driver
237    )
238
239    target_link_libraries(platform_bl1_1_interface
240        INTERFACE
241            platform_region_defs
242            platform_common_interface
243            cmsis_stack_override
244    )
245
246    target_compile_definitions(platform_bl1_1_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=1>
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            $<$<BOOL:${TFM_BL1_2_IN_OTP}>:TFM_BL1_2_IN_OTP>
257            $<$<AND:$<BOOL:${CONFIG_TFM_BOOT_STORE_MEASUREMENTS}>,$<NOT:$<BOOL:${CONFIG_TFM_BOOT_STORE_ENCODED_MEASUREMENTS}>>>:TFM_MEASURED_BOOT_API>
258    )
259
260    target_sources(platform_bl1_1
261        PRIVATE
262            ext/common/boot_hal_bl1_1.c
263            ext/common/uart_stdout.c
264            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/flash_otp_nv_counters_backend.c>
265            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
266    )
267
268    target_link_libraries(platform_bl1_1
269        PUBLIC
270            platform_bl1_1_interface
271            platform_region_defs
272        PRIVATE
273            tfm_fih
274            tfm_fih_headers
275            tfm_boot_status
276    )
277endif()
278
279#========================= Platform BL1_2 =====================================#
280
281if(BL1 AND PLATFORM_DEFAULT_BL1)
282    target_include_directories(platform_bl1_2
283        PRIVATE
284            .
285            include
286            ext/cmsis
287            ext/driver
288    )
289
290    target_link_libraries(platform_bl1_2
291        INTERFACE
292            platform_region_defs
293            platform_common_interface
294            cmsis_stack_override
295    )
296
297    target_compile_definitions(platform_bl1_2
298        INTERFACE
299            $<$<BOOL:${CRYPTO_HW_ACCELERATOR}>:CRYPTO_HW_ACCELERATOR>
300            $<$<BOOL:${TFM_BL1_LOGGING}>:TFM_BL1_LOGGING>
301            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:PLATFORM_DEFAULT_OTP>
302            $<$<BOOL:${OTP_NV_COUNTERS_RAM_EMULATION}>:OTP_NV_COUNTERS_RAM_EMULATION=1>
303            $<$<BOOL:${TFM_DUMMY_PROVISIONING}>:TFM_DUMMY_PROVISIONING>
304            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:PLATFORM_DEFAULT_NV_COUNTERS>
305            $<$<BOOL:${PLATFORM_DEFAULT_OTP_WRITEABLE}>:OTP_WRITEABLE>
306            $<$<BOOL:${TFM_BL1_MEMORY_MAPPED_FLASH}>:TFM_BL1_MEMORY_MAPPED_FLASH>
307            $<$<BOOL:${TFM_BL1_2_IN_OTP}>:TFM_BL1_2_IN_OTP>
308            $<$<AND:$<BOOL:${CONFIG_TFM_BOOT_STORE_MEASUREMENTS}>,$<NOT:$<BOOL:${CONFIG_TFM_BOOT_STORE_ENCODED_MEASUREMENTS}>>>:TFM_MEASURED_BOOT_API>
309    )
310
311    target_sources(platform_bl1_2
312        PRIVATE
313            ext/common/boot_hal_bl1_2.c
314            $<$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>:ext/common/template/nv_counters.c>
315            $<$<OR:$<BOOL:${PLATFORM_DEFAULT_NV_COUNTERS}>,$<BOOL:${PLATFORM_DEFAULT_OTP}>>:ext/common/template/flash_otp_nv_counters_backend.c>
316            $<$<BOOL:${PLATFORM_DEFAULT_OTP}>:ext/common/template/otp_flash.c>
317    )
318
319    target_link_libraries(platform_bl1_2
320        PUBLIC
321            platform_bl1_1_interface
322        PRIVATE
323            tfm_fih
324            tfm_fih_headers
325            tfm_boot_status
326            platform_region_defs
327            bl1_1_shared_lib_interface
328    )
329endif()
330
331#========================= Platform region defs ===============================#
332
333#TODO maybe just link the other platforms to this
334target_compile_definitions(platform_region_defs
335    INTERFACE
336        $<$<BOOL:${BL1}>:BL1>
337        $<$<BOOL:${BL2}>:BL2>
338        BL2_HEADER_SIZE=${BL2_HEADER_SIZE}
339        BL2_TRAILER_SIZE=${BL2_TRAILER_SIZE}
340        BL1_HEADER_SIZE=${BL1_HEADER_SIZE}
341        BL1_TRAILER_SIZE=${BL1_TRAILER_SIZE}
342        $<$<BOOL:${PLATFORM_DEFAULT_BL1}>:PLATFORM_DEFAULT_BL1>
343        $<$<BOOL:${SECURE_UART1}>:SECURE_UART1>
344        DAUTH_${DEBUG_AUTHENTICATION}
345        $<$<BOOL:${MCUBOOT_IMAGE_NUMBER}>:MCUBOOT_IMAGE_NUMBER=${MCUBOOT_IMAGE_NUMBER}>
346        $<$<BOOL:${BL2}>:MCUBOOT_SIGNATURE_TYPE=${MCUBOOT_SIGNATURE_TYPE}>
347        $<$<STREQUAL:${SIG_TYPE},EC>:MCUBOOT_SIGN_EC${SIG_LEN}>
348        $<$<STREQUAL:${MCUBOOT_EXECUTION_SLOT},2>:LINK_TO_SECONDARY_PARTITION>
349        $<$<BOOL:${TEST_PSA_API}>:PSA_API_TEST_${TEST_PSA_API}>
350        $<$<BOOL:${TFM_CODE_SHARING}>:CODE_SHARING>
351        $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:ENABLE_HEAP>
352        PLATFORM_NS_NV_COUNTERS=${TFM_NS_NV_COUNTER_AMOUNT}
353)
354
355target_include_directories(platform_region_defs
356    INTERFACE
357        ${CMAKE_CURRENT_LIST_DIR}/ext/common
358)
359
360target_link_libraries(platform_region_defs
361    INTERFACE
362        tfm_config
363)
364