-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sdk.cmake
383 lines (340 loc) · 12.6 KB
/
sdk.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#
# Copyright (c) 2022-2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# @file
# CMake for Open IoT SDK configuration
#
include(FetchContent)
get_filename_component(OPEN_IOT_SDK_SOURCE ${CHIP_ROOT}/third_party/open-iot-sdk/sdk REALPATH)
get_filename_component(OPEN_IOT_SDK_STORAGE_SOURCE ${CHIP_ROOT}/third_party/open-iot-sdk/storage REALPATH)
# Open IoT SDK targets passed to CHIP build
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS)
# Additional Open IoT SDK build configuration
set(TFM_SUPPORT NO CACHE BOOL "Add Trusted Firmware-M (TF-M) support to application")
set(TFM_NS_APP_VERSION "0.0.0" CACHE STRING "TF-M non-secure application version (in the x.x.x format)")
set(CONFIG_CHIP_OPEN_IOT_SDK_LWIP_DEBUG NO CACHE BOOL "Enable LwIP debug logs")
# Default LwIP options directory (should contain user_lwipopts.h file)
if (NOT LWIP_PROJECT_OPTS_DIR)
set(LWIP_PROJECT_OPTS_DIR ${OPEN_IOT_SDK_CONFIG}/lwip)
endif()
# Overwrite versions of Open IoT SDK components
# Add a Matter specific version of Mbedtls
FetchContent_Declare(
mbedtls
GIT_REPOSITORY https://github.com/ARMmbed/mbedtls
GIT_TAG v3.2.1
GIT_SHALLOW ON
GIT_PROGRESS ON
)
# Apply a patch to TF-M to support GCC 12
FetchContent_Declare(
trusted-firmware-m
GIT_REPOSITORY https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git
GIT_TAG d0c0a67f1b412e89d09b0987091c12998c4e4660
GIT_SHALLOW OFF
GIT_PROGRESS ON
# Note: This prevents FetchContent_MakeAvailable() from calling
# add_subdirectory() on the fetched repository. TF-M needs a
# standalone build because it relies on functions defined in its
# own toolchain files and contains paths that reference the
# top-level project instead of its own project.
SOURCE_SUBDIR NONE
PATCH_COMMAND git reset --hard --quiet && git clean --force -dx --quiet && git apply ${CMAKE_CURRENT_LIST_DIR}/tf-m.patch
)
# Open IoT SDK configuration
set(IOTSDK_FETCH_LIST
mcu-driver-reference-platforms-for-arm
cmsis-5
cmsis-freertos
mbedtls
lwip
cmsis-sockets-api
)
set(MDH_PLATFORM ARM_AN552_MPS3)
set(VARIANT "FVP")
set(FETCHCONTENT_QUIET OFF)
if(TFM_SUPPORT)
list(APPEND IOTSDK_FETCH_LIST trusted-firmware-m)
set(TFM_PLATFORM ${OPEN_IOT_SDK_EXAMPLE_COMMON}/tf-m/targets/an552)
set(TFM_PSA_FIRMWARE_UPDATE ON)
set(MCUBOOT_IMAGE_VERSION_NS ${TFM_NS_APP_VERSION})
set(TFM_CMAKE_ARGS "-DCONFIG_TFM_ENABLE_FP=ON;-DTFM_PROFILE=profile_medium;-DTFM_EXCEPTION_INFO_DUMP=ON;-DCONFIG_TFM_HALT_ON_CORE_PANIC=ON;-DTFM_ISOLATION_LEVEL=1;-DTFM_MBEDCRYPTO_PLATFORM_EXTRA_CONFIG_PATH=${OPEN_IOT_SDK_CONFIG}/mbedtls/mbedtls_config_psa.h;-DMBEDCRYPTO_BUILD_TYPE=${CMAKE_BUILD_TYPE};-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
set(TFM_CMAKE_ARGS "${TFM_CMAKE_ARGS};-DMCUBOOT_LOG_LEVEL=INFO;-DTFM_SPM_LOG_LEVEL=TFM_SPM_LOG_LEVEL_DEBUG;-DTFM_PARTITION_LOG_LEVEL=TFM_PARTITION_LOG_LEVEL_INFO")
else()
set(TFM_CMAKE_ARGS "${TFM_CMAKE_ARGS};-DMCUBOOT_LOG_LEVEL=ERROR;-DTFM_SPM_LOG_LEVEL=TFM_SPM_LOG_LEVEL_DEBUG;-DTFM_PARTITION_LOG_LEVEL=TFM_PARTITION_LOG_LEVEL_ERROR")
endif()
if(TFM_PROJECT_CONFIG_HEADER_FILE)
set(TFM_CMAKE_ARGS "${TFM_CMAKE_ARGS};-DPROJECT_CONFIG_HEADER_FILE=${TFM_PROJECT_CONFIG_HEADER_FILE}")
endif()
set(LINKER_SCRIPT ${OPEN_IOT_SDK_CONFIG}/ld/cs300_gcc_tfm.ld)
endif()
# Add Open IoT SDK source
add_subdirectory(${OPEN_IOT_SDK_SOURCE} ./sdk_build)
# Add Open IoT SDK modules to path
list(APPEND CMAKE_MODULE_PATH ${open-iot-sdk_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${open-iot-sdk_SOURCE_DIR}/components/trusted-firmware-m)
# Configure component properties
# CMSIS 5 require projects to provide configuration macros via RTE_Components.h
# and CMSIS_device_header. The macro CMSIS_device_header is not automatically set
# based on CMAKE_SYSTEM_PROCESSOR in the place where cmsis-core is first defined,
# because a project may want to provide its own device header.
if(TARGET cmsis-core)
target_compile_definitions(cmsis-core
INTERFACE
$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},cortex-m55>:CMSIS_device_header="ARMCM55.h">
)
endif()
# Add RTOS configuration headers
# Link cmsis-rtos-api against a concrete implementation
if(TARGET cmsis-rtos-api)
target_include_directories(cmsis-core
INTERFACE
cmsis-config
)
target_compile_definitions(cmsis-rtos-api
PUBLIC
DOMAIN_NS=$<IF:$<BOOL:${TFM_SUPPORT}>,1,0>
)
if(TARGET freertos-kernel)
target_include_directories(freertos-kernel
PUBLIC
freertos-config
)
target_link_libraries(freertos-kernel
PUBLIC
cmsis-core
)
target_link_libraries(cmsis-rtos-api
PUBLIC
freertos-cmsis-rtos
)
target_compile_definitions(cmsis-rtos-api
INTERFACE
CONFIG_RUN_FREERTOS_SECURE_ONLY=$<IF:$<BOOL:${TFM_SUPPORT}>,0,1>
)
elseif(TARGET cmsis-rtx)
target_link_libraries(cmsis-rtos-api
INTERFACE
cmsis-rtx
)
endif()
endif()
# LwIP configuration
if(TARGET lwip-cmsis-port)
# lwipcore requires the config defined by lwip-cmsis-port
target_link_libraries(lwipcore
PUBLIC
lwip-cmsis-port
)
# provide method to use for tracing by the lwip port (optional)
target_compile_definitions(lwipopts
INTERFACE
DEBUG_PRINT=printf
$<$<BOOL:${CONFIG_CHIP_OPEN_IOT_SDK_LWIP_DEBUG}>:LWIP_DEBUG>
$<$<BOOL:${CONFIG_CHIP_LIB_TESTS}>:CHIP_LIB_TESTS>
)
target_include_directories(lwipopts
INTERFACE
${LWIP_PROJECT_OPTS_DIR}
)
# Link the emac factory to LwIP port
target_link_libraries(lwip-cmsis-port PUBLIC iotsdk-emac-factory)
endif()
# MDH configuration
if(TARGET ethernet-lan91c111)
target_compile_definitions(ethernet-lan91c111
INTERFACE
LAN91C111_RFS_MULTICAST_SUPPORT
)
endif()
if(TARGET mcu-driver-hal)
target_compile_definitions(mcu-driver-hal
INTERFACE
DOMAIN_NS=$<IF:$<BOOL:${TFM_SUPPORT}>,1,0>
)
# Fixing the optimization issue for mcu-driver-hal target in the release build.
# The default -Os optimization causes performance issues for the application.
# We need to replace it with -O2 which is suitable for performance.
# This fix can be removed in the future when the issue will be fixed in SDK directly.
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
target_compile_options(mcu-driver-hal INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-O2>)
target_compile_options(mcu-driver-hal INTERFACE $<$<COMPILE_LANGUAGE:C>:-O2>)
endif()
endif()
# Mbedtls config
if(TARGET mbedtls-config)
target_include_directories(mbedtls-config
INTERFACE
${OPEN_IOT_SDK_CONFIG}/mbedtls
)
target_sources(mbedtls-config
INTERFACE
${OPEN_IOT_SDK_CONFIG}/mbedtls/platform_alt.cpp
)
target_compile_definitions(mbedtls-config
INTERFACE
MBEDTLS_CONFIG_FILE="${OPEN_IOT_SDK_CONFIG}/mbedtls/mbedtls_config.h"
)
target_link_libraries(mbedtls-config
INTERFACE
mbedtls-threading-cmsis-rtos
)
endif()
if("mcu-driver-reference-platforms-for-arm" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
mcu-driver-bootstrap
mcu-driver-hal
mdh-arm-corstone-300-common
target-interface
)
endif()
if("cmsis-5" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
cmsis-core
cmsis-rtos-api
iotsdk-ip-network-api
)
endif()
if("cmsis-freertos" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
freertos-cmsis-rtos
)
endif()
if("lwip" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
lwipcore
lwip-cmsis-port
lwip-cmsis-sys
lwip-cmsis-port-low-input-latency
lwipopts
)
endif()
if("cmsis-sockets-api" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
cmsis-sockets-api
lwip-sockets
)
endif()
if("trusted-firmware-m" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
tfm-ns-interface
tfm-ns-interface-cmsis-rtos
)
endif()
# Note: Mbed TLS must appear after TF-M otherwise psa from mbed TLS is used
if("mbedtls" IN_LIST IOTSDK_FETCH_LIST)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
mbedtls
mbedtls-config
mbedtls-threading-cmsis-rtos
)
endif()
# Additional Open IoT SDK port components
# Add Open IoT SDK storage source
add_subdirectory(${OPEN_IOT_SDK_STORAGE_SOURCE} ./sdk_storage_build)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
iotsdk-blockdevice
iotsdk-tdbstore
)
# Add custom storage library
add_subdirectory(${OPEN_IOT_SDK_CONFIG}/storage storage_build)
list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS
openiotsdk-storage
)
function(sdk_post_build target)
string(REPLACE "_ns" "" APP_NAME ${target})
if(TFM_SUPPORT)
include(ConvertElfToBin)
include(SignTfmImage)
ExternalProject_Get_Property(trusted-firmware-m-build BINARY_DIR)
target_elf_to_bin(${target})
add_custom_command(
TARGET
${target}
POST_BUILD
DEPENDS
$<TARGET_FILE_DIR:${target}>/${target}.bin
COMMAND
# Sign the non-secure (application) image for TF-M bootloader (BL2)"
python3 ${BINARY_DIR}/install/image_signing/scripts/wrapper/wrapper.py
--layout ${BINARY_DIR}/install/image_signing/layout_files/signing_layout_ns.o
-v ${MCUBOOT_IMAGE_VERSION_NS}
-k ${BINARY_DIR}/install/image_signing/keys/root-RSA-3072_1.pem
--public-key-format full
--align 1 --pad --pad-header -H 0x400 -s auto -d "(0, 0.0.0+0)"
$<TARGET_FILE_DIR:${target}>/${target}.bin
--overwrite-only
--measured-boot-record
$<TARGET_FILE_DIR:${target}>/${target}_signed.bin
VERBATIM
)
iotsdk_tf_m_merge_images(${target} 0x10000000 0x38000000 0x28060000)
# Cleanup
add_custom_command(
TARGET
${target}
POST_BUILD
DEPENDS
$<TARGET_FILE_DIR:${target}>/${target}.bin
$<TARGET_FILE_DIR:${target}>/${target}_signed.bin
$<TARGET_FILE_DIR:${target}>/${target}_merged.hex
$<TARGET_FILE_DIR:${target}>/${target}_merged.elf
COMMAND
# Copy the bootloader and TF-M secure image for debugging purposes
${CMAKE_COMMAND} -E copy
${BINARY_DIR}/install/outputs/bl2.elf
${BINARY_DIR}/install/outputs/tfm_s.elf
$<TARGET_FILE_DIR:${target}>/
COMMAND
# Rename output file
${CMAKE_COMMAND} -E copy
$<TARGET_FILE_DIR:${target}>/${target}_merged.elf
$<TARGET_FILE_DIR:${target}>/${APP_NAME}.elf
COMMAND rm
ARGS -Rf
$<TARGET_FILE_DIR:${target}>/${target}.bin
$<TARGET_FILE_DIR:${target}>/${target}_signed.bin
$<TARGET_FILE_DIR:${target}>/${target}_merged.hex
$<TARGET_FILE_DIR:${target}>/${target}_merged.elf
VERBATIM
)
else()
add_custom_command(
TARGET
${target}
POST_BUILD
DEPENDS
$<TARGET_FILE_DIR:${target}>/${target}.elf
$<TARGET_FILE_DIR:${target}>/${target}.map
COMMAND
# Rename output elf file
${CMAKE_COMMAND} -E copy
$<TARGET_FILE_DIR:${target}>/${target}.elf
$<TARGET_FILE_DIR:${target}>/${APP_NAME}.elf
COMMAND
# Rename output map file
${CMAKE_COMMAND} -E copy
$<TARGET_FILE_DIR:${target}>/${target}.map
$<TARGET_FILE_DIR:${target}>/${APP_NAME}.map
COMMAND rm
ARGS -Rf
$<TARGET_FILE_DIR:${target}>/${target}.elf
$<TARGET_FILE_DIR:${target}>/${target}.map
VERBATIM
)
endif() #TFM_SUPPORT
endfunction()