1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2022, Arm Limited. All rights reserved.
3# Copyright (c) 2021 STMicroelectronics. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9cmake_policy(SET CMP0076 NEW)
10set(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR})
11
12#========================= Platform region defs ===============================#
13
14target_include_directories(platform_region_defs
15    INTERFACE
16        include
17        ${CMAKE_CURRENT_SOURCE_DIR}/boards
18)
19
20target_compile_definitions(platform_region_defs
21    INTERFACE
22        $<$<OR:$<BOOL:${TEST_NS}>,$<BOOL:${TEST_S}>>:FLASH_LAYOUT_FOR_TEST>
23)
24
25#========================= Platform common defs ===============================#
26target_add_scatter_file(tfm_s
27    $<$<AND:$<VERSION_LESS:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:ARMClang>>:${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/armclang/tfm_common_s.sct>
28    $<$<AND:$<VERSION_LESS:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:GNU>>:${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/gcc/tfm_common_s.ld>
29    $<$<AND:$<VERSION_LESS:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:IAR>>:${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/iar/tfm_common_s.icf>
30    $<$<AND:$<VERSION_EQUAL:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:ARMClang>>:${CMAKE_BINARY_DIR}/generated/platform/ext/common/armclang/tfm_isolation_l3.sct>
31    $<$<AND:$<VERSION_EQUAL:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:GNU>>:${CMAKE_BINARY_DIR}/generated/platform/ext/common/gcc/tfm_isolation_l3.ld>
32)
33
34if(NS)
35    target_add_scatter_file(tfm_ns
36        $<$<C_COMPILER_ID:GNU>:${STM_COMMON_DIR}/hal/template/gcc/appli_ns.ld>
37        $<$<C_COMPILER_ID:ARMClang>:${STM_COMMON_DIR}/hal/template/armclang/appli_ns.sct>
38        $<$<C_COMPILER_ID:IAR>:${STM_COMMON_DIR}/hal/template/iar/appli_ns.icf>
39    )
40    target_link_libraries(CMSIS_5_tfm_ns
41        INTERFACE
42            CMSIS_5_RTX_V8MMN
43    )
44endif()
45
46if(BL2)
47    target_add_scatter_file(bl2
48        $<$<C_COMPILER_ID:GNU>:${STM_COMMON_DIR}/hal/template/gcc/bl2.ld>
49        $<$<C_COMPILER_ID:ARMClang>:${STM_COMMON_DIR}/hal/template/armclang/bl2.sct>
50        $<$<C_COMPILER_ID:IAR>:${STM_COMMON_DIR}/hal/template/iar/bl2.icf>
51    )
52endif()
53
54#========================= Platform Secure ====================================#
55
56target_include_directories(platform_s
57    PUBLIC
58        ${CMAKE_CURRENT_SOURCE_DIR}
59        ${STM_COMMON_DIR}/hal/Native_Driver
60        ${STM_COMMON_DIR}/hal/CMSIS_Driver
61        ${CMAKE_CURRENT_SOURCE_DIR}/Device/Include
62        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Inc
63        ${CMAKE_CURRENT_SOURCE_DIR}/secure
64        ${MBEDCRYPTO_PATH}/library
65        ${CMAKE_SOURCE_DIR}/include
66)
67
68target_sources(platform_s
69    PRIVATE
70        ${CMAKE_CURRENT_SOURCE_DIR}/secure/system_stm32u5xx.c
71        ${STM_COMMON_DIR}/hal/Native_Driver/mpu_armv8m_drv.c
72        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal.c
73        ${CMAKE_CURRENT_SOURCE_DIR}/secure/low_level_device.c
74        ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_flash.c
75        ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_com.c
76        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal.c
77        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_flash.c
78        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_flash_ex.c
79        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma.c
80        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma_ex.c
81        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr.c
82        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr_ex.c
83        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_gpio.c
84        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart.c
85        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart_ex.c
86        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc.c
87        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc_ex.c
88        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_gtzc.c
89        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cortex.c
90        $<$<BOOL:${TFM_PARTITION_PLATFORM}>:${CMAKE_CURRENT_SOURCE_DIR}/secure/tfm_platform_system.c>
91        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rng.c
92        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rng_ex.c
93        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_ospi.c
94        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_ll_dlyb.c
95    PUBLIC
96        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_hash.c
97        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_hash_ex.c
98        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pka.c
99        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cryp.c
100        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cryp_ex.c
101        ${STM_COMMON_DIR}/hal/Native_Driver/low_level_rng.c
102        ${STM_COMMON_DIR}/hal/Native_Driver/nv_counters.c
103)
104
105target_compile_options(platform_s
106    PUBLIC
107        ${COMPILER_CMSE_FLAG}
108)
109
110# GNU ld and IAR does not deal well with strong functions in static libraries
111# overriding weak functions in object files. For this reason, files with strong
112# functions are linked as an object file. This issue does not affect armclang,
113# but the fix is compatible.
114target_sources(tfm_s
115    PRIVATE
116        ${STM_COMMON_DIR}/hal/Native_Driver/tick.c
117)
118
119#========================= Platform Non-Secure ================================#
120
121target_include_directories(platform_ns
122    PUBLIC
123        ${CMAKE_CURRENT_SOURCE_DIR}/Device/Include
124        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Inc
125)
126
127target_sources(platform_ns
128    PRIVATE
129        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal.c
130        ${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/Templates/system_stm32u5xx.c
131        ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_com.c
132        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma.c
133        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma_ex.c
134        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr.c
135        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr_ex.c
136        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc.c
137        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_gpio.c
138        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart.c
139        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart_ex.c
140        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cortex.c
141        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc.c
142        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc_ex.c
143)
144
145#========================= Platform BL2 =======================================#
146
147if(BL2)
148    target_include_directories(platform_bl2
149        PUBLIC
150            ${CMAKE_CURRENT_SOURCE_DIR}
151            ${STM_COMMON_DIR}/hal/Native_Driver
152            ${STM_COMMON_DIR}/hal/CMSIS_Driver
153            ${CMAKE_CURRENT_SOURCE_DIR}/Device/Include
154            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Inc
155            ${MCUBOOT_PATH}/boot/bootutil/include
156            $<TARGET_PROPERTY:bl2,BINARY_DIR>/ext/mcuboot
157            ${CMAKE_SOURCE_DIR}/bl2/ext/mcuboot/include
158            ${CMAKE_CURRENT_SOURCE_DIR}/bl2
159            ${CMAKE_SOURCE_DIR}/interface/include
160    )
161
162    target_sources(platform_bl2
163        PRIVATE
164            ${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/Templates/system_stm32u5xx.c
165            ${CMAKE_CURRENT_SOURCE_DIR}/bl2/boot_hal_bl2.c
166            ${STM_COMMON_DIR}/hal/Native_Driver/mpu_armv8m_drv.c
167            ${CMAKE_CURRENT_SOURCE_DIR}/bl2/low_level_security.c
168            ${CMAKE_CURRENT_SOURCE_DIR}/bl2/low_level_device.c
169            ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_flash.c
170            ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_com.c
171            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal.c
172            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_flash.c
173            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_flash_ex.c
174            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr_ex.c
175            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma.c
176            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma_ex.c
177            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr.c
178            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc.c
179            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc_ex.c
180            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cortex.c
181            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_gpio.c
182            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart.c
183            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart_ex.c
184            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_gtzc.c
185            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_hash.c
186            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_hash_ex.c
187            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pka.c
188            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cryp.c
189            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cryp_ex.c
190            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rng.c
191            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rng_ex.c
192            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rtc.c
193            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rtc_ex.c
194            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_icache.c
195            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_ospi.c
196            ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_ll_dlyb.c
197            ${STM_COMMON_DIR}/hal/Native_Driver/low_level_rng.c
198            ${STM_COMMON_DIR}/hal/provision/otp_provision.c
199            ${STM_COMMON_DIR}/hal/Native_Driver/nv_counters.c
200            ${STM_COMMON_DIR}/hal/provision/nvm_init.c
201            ${STM_COMMON_DIR}/hal/provision/nvmcnt_init.c
202    )
203
204    target_compile_options(platform_bl2
205        PUBLIC
206            ${COMPILER_CMSE_FLAG}
207    )
208
209    # GNU ld and IAR does not deal well with strong functions in static libraries
210    # overriding weak functions in object files. For this reason, files with strong
211    # functions are linked as an object file. This issue does not affect armclang,
212    # but the fix is compatible.
213    target_sources(bl2
214        PRIVATE
215            ${STM_COMMON_DIR}/hal/Native_Driver/tick.c
216            ${CMAKE_CURRENT_SOURCE_DIR}/bl2/stm32u5xx_hal_msp.c
217    )
218endif()
219
220#========================= tfm_spm ============================================#
221
222target_sources(tfm_spm
223    PRIVATE
224        ${CMAKE_CURRENT_SOURCE_DIR}/secure/target_cfg.c
225        ${CMAKE_CURRENT_SOURCE_DIR}/secure/tfm_hal_isolation.c
226        ${CMAKE_CURRENT_SOURCE_DIR}/secure/tfm_hal_platform.c
227)
228
229install(FILES
230        ${STM_COMMON_DIR}/scripts/stm_tool.py
231        ${STM_COMMON_DIR}/scripts/bin2hex.py
232        ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../bl2/ext/mcuboot/scripts/macro_parser.py
233        DESTINATION ${CMAKE_BINARY_DIR}/scripts/ )
234
235if (${CMAKE_C_COMPILER_ID} STREQUAL GNU)
236    set(PREPROCESSOR_FILE ${STM_COMMON_DIR}/scripts/gcc/preprocess.sh)
237elseif (${CMAKE_C_COMPILER_ID} STREQUAL ARMClang)
238    set(PREPROCESSOR_FILE ${STM_COMMON_DIR}/scripts/armclang/preprocess.sh)
239else()
240    set(PREPROCESSOR_FILE ${STM_COMMON_DIR}/scripts/iar/preprocess.sh)
241endif()
242
243install(FILES ${STM_COMMON_DIR}/scripts/regression.sh
244         ${PREPROCESSOR_FILE}
245         ${STM_COMMON_DIR}/scripts/TFM_UPDATE.sh
246         ${STM_COMMON_DIR}/scripts/TFM_BIN2HEX.sh
247         ${STM_COMMON_DIR}/scripts/postbuild.sh
248         PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
249         DESTINATION ${CMAKE_BINARY_DIR} )
250
251if (${TEST_S} OR ${TEST_NS})
252    #the define FLASH_LAYOUT_FOR_TEST
253    #increase SECURE and NON SECURE IMAGE and modify the  content of  low_level_device.c from BL2 (low_level_flash.c)
254    set(FLAGS_FOR_BL2_PREPROCESSING
255        "#define FLASH_LAYOUT_FOR_TEST\n"
256        "#define BL2\n"
257        "#define MCUBOOT_IMAGE_NUMBER 2\n"
258        "#define MCUBOOT_OVERWRITE_ONLY\n"
259         )
260    add_definitions("-DFLASH_LAYOUT_FOR_TEST")
261else()
262    set(FLAGS_FOR_BL2_PREPROCESSING
263        "#define BL2\n"
264        "#define MCUBOOT_IMAGE_NUMBER 2\n"
265        "#define MCUBOOT_OVERWRITE_ONLY\n"
266       )
267endif()
268
269set(CONTENT_FOR_BL2_PREPROCESSING
270    "#include \"flash_layout.h\"\n\n"
271    "enum bl2_image_attributes {\n"
272    "\tRE_IMAGE_FLASH_ADDRESS_SECURE = (FLASH_BASE_ADDRESS+FLASH_AREA_0_OFFSET),\n"
273    "\tRE_IMAGE_FLASH_ADDRESS_NON_SECURE = (FLASH_BASE_ADDRESS+FLASH_AREA_1_OFFSET),\n"
274    "\tRE_BL2_BIN_ADDRESS = (FLASH_BASE_ADDRESS+FLASH_AREA_BL2_BIN_OFFSET),\n"
275    "\tRE_BL2_NVMCNT_ADDRESS = (FLASH_BASE_ADDRESS+FLASH_BL2_NVCNT_AREA_OFFSET),\n"
276    "\tRE_BL2_PROVISION_ADDRESS = (FLASH_BASE_ADDRESS+FLASH_AREA_OTP_OFFSET),\n"
277    "\tRE_BL2_BOOT_ADDRESS = (FLASH_BASE_ADDRESS+FLASH_AREA_BL2_OFFSET),\n"
278    "\tRE_BL2_WRP_START = (FLASH_AREA_BL2_OFFSET),\n"
279    "\tRE_BL2_WRP_END = (FLASH_AREA_BL2_NOHDP_OFFSET+FLASH_AREA_BL2_NOHDP_SIZE-0x1),\n"
280    "\tRE_BL2_HDP_END = (FLASH_AREA_BL2_NOHDP_OFFSET-0x1),\n"
281    "#if defined(EXTERNAL_FLASH)\n"
282    "\tRE_IMAGE_FLASH_SECURE_UPDATE = (OSPI_FLASH_BASE_ADDRESS+FLASH_AREA_2_OFFSET),\n"
283    "\tRE_IMAGE_FLASH_NON_SECURE_UPDATE = (OSPI_FLASH_BASE_ADDRESS+FLASH_AREA_3_OFFSET),\n"
284    "\tRE_IMAGE_FLASH_UNUSED = (FLASH_BASE_ADDRESS+FLASH_AREA_1_OFFSET+FLASH_AREA_1_SIZE),\n"
285    "#else\n"
286    "\tRE_IMAGE_FLASH_SECURE_UPDATE = (FLASH_BASE_ADDRESS+FLASH_AREA_2_OFFSET),\n"
287    "\tRE_IMAGE_FLASH_NON_SECURE_UPDATE = (FLASH_BASE_ADDRESS+FLASH_AREA_3_OFFSET),\n"
288    "\tRE_IMAGE_FLASH_UNUSED = (FLASH_BASE_ADDRESS+FLASH_AREA_3_OFFSET+FLASH_AREA_3_SIZE),\n"
289    "#endif\n"
290    "\tRE_IMAGE_FLASH_SCRATCH = (FLASH_BASE_ADDRESS+FLASH_AREA_SCRATCH_OFFSET),\n"
291    "\tRE_IMAGE_FLASH_NV_COUNTERS = (FLASH_BASE_ADDRESS+FLASH_NV_COUNTERS_AREA_OFFSET),\n"
292    "\tRE_IMAGE_FLASH_NV_PS = (FLASH_BASE_ADDRESS+FLASH_PS_AREA_OFFSET),\n"
293    "\tRE_IMAGE_FLASH_NV_ITS = (FLASH_BASE_ADDRESS+FLASH_ITS_AREA_OFFSET),\n"
294)
295if(MCUBOOT_ENC_IMAGES)
296    set(CONTENT_ENCRYPTED
297    "\tRE_IMAGE_ENCRYPTED = 0x01\n}\;"
298    )
299else()
300    set(CONTENT_ENCRYPTED
301    "\tRE_IMAGE_ENCRYPTED = 0x0\n}\;"
302    )
303endif()
304
305string(CONCAT BL2_PREPROCESSING ${FLAGS_FOR_BL2_PREPROCESSING}
306       ${CONTENT_FOR_BL2_PREPROCESSING}
307       ${CONTENT_ENCRYPTED})
308