Skip to content

Commit 82e5a10

Browse files
david-hazi-armaggarg
authored andcommitted
build: Keyword detection application with split-build
The application is built similarly to blinky. The update image generation is unchanged. The mbedtls config file needs the DOMAIN_NS check because S and NS clients are not separated. The minimal config is added for S clients. Signed-off-by: Gergely Kovacs <[email protected]>
1 parent 4740e53 commit 82e5a10

File tree

8 files changed

+394
-89
lines changed

8 files changed

+394
-89
lines changed

applications/helpers/provisioning/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2024 Arm Limited and/or its affiliates
1+
# Copyright 2023-2025 Arm Limited and/or its affiliates
22
33
# SPDX-License-Identifier: MIT
44

@@ -19,9 +19,8 @@ else()
1919
${CMAKE_CURRENT_LIST_DIR}/inc
2020
)
2121

22-
target_link_libraries(provisioning_data PRIVATE
23-
fri-bsp
24-
)
22+
# Only use interface includes for fri-bsp for provisioning config, public sources are not needed
23+
target_include_directories(provisioning_data PRIVATE $<TARGET_PROPERTY:fri-bsp,INTERFACE_INCLUDE_DIRECTORIES>)
2524

2625
set(
2726
CODE_SIGNING_PUBLIC_KEY_PEM_PATH
@@ -45,13 +44,14 @@ else()
4544
${CMAKE_CURRENT_BINARY_DIR}/aws_clientcredential_keys.h
4645
)
4746

48-
add_dependencies(aws_clientcredential_keys_header trusted_firmware-m-build)
4947
add_dependencies(provisioning_data aws_clientcredential_keys_header)
5048

5149
if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
5250
target_link_options(provisioning_data
5351
PRIVATE
54-
"-nostartfiles"
52+
# We should define an entry point to override the default Reset_Handler
53+
"--entry=provisioningBundle"
54+
"-nostartfiles"
5555
)
5656
target_add_scatter_file(provisioning_data ${CMAKE_CURRENT_LIST_DIR}/provisioning_data.ld)
5757
else()

applications/helpers/provisioning/cmake/SetProvisioningLinkOptions.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
# Copyright 2023 Arm Limited and/or its affiliates
1+
# Copyright 2023-2025 Arm Limited and/or its affiliates
22
33
# SPDX-License-Identifier: MIT
44

55
macro(target_add_scatter_file target)
66
add_library(${target}_scatter OBJECT)
77

8-
target_link_libraries(${target}_scatter PRIVATE
9-
fri-bsp
10-
)
8+
# Only use interface includes for fri-bsp, the linker script does not need public sources
9+
target_include_directories(${target}_scatter PRIVATE $<TARGET_PROPERTY:fri-bsp,INTERFACE_INCLUDE_DIRECTORIES>)
1110

1211
if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
1312
target_link_options(${target}

applications/keyword_detection/CMakeLists.txt

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
# SPDX-License-Identifier: MIT
44

55
cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)
6+
7+
# NS target name the TF-M api_ns CMakeLists.txt uses
8+
set(NS_TARGET_NAME keyword-detection)
9+
# Toolchain file has to be included before the very first project() call
10+
include(${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/components/security/trusted_firmware-m/integration/cmake/TfmNsToolchain.cmake)
11+
12+
613
project(keyword-detection LANGUAGES C CXX)
714

815
set(ML_INFERENCE_ENGINE "ETHOS" CACHE STRING "Machine Learning inference engine (ETHOS | SOFTWARE)")
@@ -23,20 +30,13 @@ set(AWS_OTA_SIGNATURE_TYPE "RSA-3072" CACHE STRING "Supported algorithms for si
2330
# Because of this, if only PATCH version is changed then the OTA will be rejected
2431
# due to same firmware version.
2532
# We will therefore change the build version from TF-M.
26-
set(MCUBOOT_IMAGE_VERSION_NS "0.0.1+10")
33+
# MCUBOOT_IMAGE_VERSION_NS is passed to the TF-M build in TfmInitialCache.cmake
2734
set(MCUBOOT_IMAGE_VERSION_NS_UPDATE "0.0.1+20")
2835

29-
# These variables are only defined in case of GNU toolchain as it is currently the only toolchain
30-
# that supports the ML Model component OTA update feature where these variables are needed.
36+
# This variable is only defined in case of GNU toolchain as it is currently the only toolchain
37+
# that supports the ML Model component OTA update feature where this variable is needed.
3138
if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
32-
set(MCUBOOT_IMAGE_VERSION_NS_ML_MODEL "0.0.1+11")
3339
set(MCUBOOT_IMAGE_VERSION_NS_ML_MODEL_UPDATE "0.0.1+42")
34-
set(MCUBOOT_IMAGE_NUMBER 3 CACHE STRING "Total number of firmware images")
35-
set(MCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM "1_0")
36-
set(DEFAULT_MCUBOOT_FLASH_MAP OFF)
37-
else()
38-
set(DEFAULT_MCUBOOT_FLASH_MAP ON)
39-
set(MCUBOOT_IMAGE_NUMBER 2 CACHE STRING "Total number of firmware images")
4040
endif()
4141

4242
if (${ML_INFERENCE_ENGINE} STREQUAL "ETHOS")
@@ -48,42 +48,9 @@ endif()
4848
set(ML_USE_CASE "kws")
4949
set(ML_MODEL "GenerateKWSModel")
5050
set(ML_USE_CASE_RESOURCES_FILE "${CMAKE_CURRENT_LIST_DIR}/resources/use_case_resources.json")
51-
set(TFM_PLATFORM_UPGRADE_STRATEGY "SWAP_USING_SCRATCH")
52-
set(TFM_PLATFORM_CONFIRM_IMAGE ON)
53-
54-
# Trusted Firmware-M setup
55-
set(TFM_CMAKE_APP_ARGS
56-
-DPROJECT_CONFIG_HEADER_FILE=${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/applications/keyword_detection/configs/tfm_config/project_config.h
57-
-DMCUBOOT_CONFIRM_IMAGE=${TFM_PLATFORM_CONFIRM_IMAGE}
58-
-DMCUBOOT_UPGRADE_STRATEGY=${TFM_PLATFORM_UPGRADE_STRATEGY}
59-
-DMCUBOOT_IMAGE_VERSION_NS=${MCUBOOT_IMAGE_VERSION_NS}
60-
-DMCUBOOT_IMAGE_VERSION_NS_ML_MODEL=${MCUBOOT_IMAGE_VERSION_NS_ML_MODEL}
61-
-DMCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM=${MCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM}
62-
-DMCUBOOT_SECURITY_COUNTER_NS_ML_MODEL=1
63-
-DCONFIG_TFM_HALT_ON_CORE_PANIC=ON
64-
-DMCUBOOT_DATA_SHARING=ON
65-
-DPLATFORM_HAS_FIRMWARE_UPDATE_SUPPORT=ON
66-
-DTFM_PARTITION_FIRMWARE_UPDATE=ON
67-
-DTFM_PARTITION_LOG_LEVEL=TFM_PARTITION_LOG_LEVEL_INFO
68-
)
69-
70-
# These definitions are only defined in case of GNU toolchain as it is currently the only toolchain
71-
# that supports the ML Model component OTA update feature where these definitions are needed.
72-
if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
73-
list(APPEND TFM_CMAKE_APP_ARGS
74-
-DMCUBOOT_IMAGE_VERSION_NS_ML_MODEL=${MCUBOOT_IMAGE_VERSION_NS_ML_MODEL}
75-
-DMCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM=${MCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM}
76-
-DMCUBOOT_SECURITY_COUNTER_NS_ML_MODEL=1
77-
)
78-
endif()
79-
80-
# Set global optimization level to reduce code size while keeping the debug experience.
81-
if(${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
82-
add_compile_options(-Og)
83-
elseif(${CMAKE_C_COMPILER_ID} STREQUAL "ARMClang")
84-
add_compile_options(-O1)
85-
endif()
8651

52+
set_compiler_and_linker_flags()
53+
include(${CONFIG_SPE_PATH}/config/cp_check.cmake)
8754

8855
add_subdirectory(${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR} ${CMAKE_BINARY_DIR}/iot_reference_arm_corstone3xx)
8956

@@ -105,6 +72,7 @@ add_executable(keyword-detection
10572
main.c
10673
ml_interface.cc
10774
model_config.cc
75+
${CONFIG_SPE_PATH}/interface/src/os_wrapper/tfm_ns_interface_rtos.c
10876
)
10977

11078
# These definitions are only defined in case of GNU toolchain as it is currently the only toolchain
@@ -157,10 +125,6 @@ target_compile_options(keyword-detection
157125
$<$<COMPILE_LANGUAGE:C>:-std=c99>
158126
)
159127

160-
# Trusted Firmware-M must be built before the application, because
161-
# the application depends on the NS interface and the BL2 signing scripts,
162-
# both of which are generated as parts of the Trusted Firmware-M build process.
163-
add_dependencies(keyword-detection trusted_firmware-m-build)
164128
# The provision data must be built before the application because
165129
# it provides credentials to connect to AWS.
166130
add_dependencies(keyword-detection provisioning_data_bin)
@@ -181,10 +145,12 @@ target_link_libraries(keyword-detection
181145
mbedtls
182146
ota-update
183147
provisioning-lib
184-
tfm-ns-interface
148+
tfm_api_ns
185149
toolchain-override
186150
kws_api
187151
kws_model
152+
# FRI always uses TrustZone
153+
tfm_api_ns_tz
188154
)
189155

190156
# sntp helper library depends on FreeRTOS-Plus-TCP connectivity stack as it
@@ -203,8 +169,6 @@ set_linker_script(keyword-detection)
203169

204170
list(APPEND CMAKE_MODULE_PATH ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR}/tools/cmake)
205171
include(ConvertElfToBin)
206-
include(ExternalProject)
207-
ExternalProject_Get_Property(trusted_firmware-m-build BINARY_DIR)
208172

209173
# The ML Model is only extracted in case of GNU toolchain as it is currently the only toolchain
210174
# that supports the ML Model component OTA update feature.
@@ -220,26 +184,31 @@ extract_sections_from_axf(
220184
OUTPUT_BIN_NAME "ns_image"
221185
)
222186

223-
# The non-secure application, and ML model images should be padded while being signed
224-
# Hence, passing "TRUE" as the input parameter to the pad option of sign function.
225-
iot_reference_arm_corstone3xx_tf_m_sign_image(
226-
keyword-detection
227-
"ns_image"
228-
keyword-detection_signed
229-
${MCUBOOT_IMAGE_VERSION_NS}
230-
"${BINARY_DIR}/api_ns/image_signing/layout_files/signing_layout_ns.o"
231-
TRUE
187+
# Copy the binary flash content to the location expected by default signing
188+
# Signing is implemented in the exported TF-M NS CMakeLists.txt (in the
189+
# ${CONFIG_SPE_PATH} directory)
190+
add_custom_target(keyword-detection_bin
191+
SOURCES ${CMAKE_BINARY_DIR}/keyword-detection.bin
192+
DEPENDS keyword-detection
193+
)
194+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/keyword-detection.bin
195+
DEPENDS keyword-detection
196+
COMMAND ${CMAKE_COMMAND}
197+
-E copy ${SECTORS_BIN_DIR}/ns_image.bin
198+
${CMAKE_BINARY_DIR}/keyword-detection.bin
232199
)
233200

234201
# The ML Model image is only signed in case of GNU toolchain as it is currently the only toolchain
235202
# that supports the ML Model component OTA update feature.
203+
# The ML model image should be padded while being signed
204+
# Hence, passing "TRUE" as the input parameter to the pad option of sign function
236205
if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
237206
iot_reference_arm_corstone3xx_tf_m_sign_image(
238207
keyword-detection
239208
"model"
240209
keyword-detection-model_signed
241210
${MCUBOOT_IMAGE_VERSION_NS_ML_MODEL}
242-
"${BINARY_DIR}/api_ns/image_signing/layout_files/signing_layout_ns_ml_model.o"
211+
"${CONFIG_SPE_PATH}/image_signing/layout_files/signing_layout_ns_ml_model.o"
243212
TRUE
244213
)
245214
endif()
@@ -253,7 +222,7 @@ iot_reference_arm_corstone3xx_tf_m_sign_image(
253222
"ns_image"
254223
keyword-detection-update_signed
255224
${MCUBOOT_IMAGE_VERSION_NS_UPDATE}
256-
"${BINARY_DIR}/api_ns/image_signing/layout_files/signing_layout_ns.o"
225+
"${CONFIG_SPE_PATH}/image_signing/layout_files/signing_layout_ns.o"
257226
FALSE
258227
)
259228

@@ -265,7 +234,7 @@ if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU")
265234
"model"
266235
keyword-detection-model-update_signed
267236
${MCUBOOT_IMAGE_VERSION_NS_ML_MODEL_UPDATE}
268-
"${BINARY_DIR}/api_ns/image_signing/layout_files/signing_layout_ns_ml_model.o"
237+
"${CONFIG_SPE_PATH}/image_signing/layout_files/signing_layout_ns_ml_model.o"
269238
FALSE
270239
)
271240
endif()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2025 Arm Limited and/or its affiliates
2+
3+
# SPDX-License-Identifier: MIT
4+
5+
include(${ROOT}/cmake/TfmInitialCacheCommon.cmake)
6+
7+
set(TFM_MBEDCRYPTO_CONFIG_CLIENT_PATH "${ROOT}/applications/keyword_detection/configs/mbedtls_config/aws_mbedtls_config.h" CACHE FILEPATH "TFM_MBEDCRYPTO_CONFIG_CLIENT_PATH" FORCE)
8+
set(PROJECT_CONFIG_HEADER_FILE ${ROOT}/applications/keyword_detection/configs/tfm_config/project_config.h CACHE FILEPATH "PROJECT_CONFIG_HEADER_FILE" FORCE)
9+
set(MCUBOOT_UPGRADE_STRATEGY "SWAP_USING_SCRATCH" CACHE STRING "MCUBOOT_UPGRADE_STRATEGY" FORCE)
10+
set(MCUBOOT_IMAGE_VERSION_NS "0.0.1+10" CACHE STRING "MCUBOOT_IMAGE_VERSION_NS" FORCE)
11+
set(CONFIG_TFM_HALT_ON_CORE_PANIC ON CACHE BOOL "CONFIG_TFM_HALT_ON_CORE_PANIC" FORCE)
12+
set(MCUBOOT_DATA_SHARING ON CACHE BOOL "MCUBOOT_DATA_SHARING" FORCE)
13+
set(PLATFORM_HAS_FIRMWARE_UPDATE_SUPPORT ON CACHE BOOL "PLATFORM_HAS_FIRMWARE_UPDATE_SUPPORT" FORCE)
14+
set(TFM_PARTITION_FIRMWARE_UPDATE ON CACHE BOOL "TFM_PARTITION_FIRMWARE_UPDATE" FORCE)
15+
set(TFM_PARTITION_LOG_LEVEL TFM_PARTITION_LOG_LEVEL_INFO CACHE STRING "TFM_PARTITION_LOG_LEVEL" FORCE)
16+
set(CONFIG_TFM_ENABLE_MVE ON CACHE STRING "CONFIG_TFM_ENABLE_MVE" FORCE)
17+
set(CONFIG_TFM_ENABLE_MVE_FP ON CACHE STRING "CONFIG_TFM_ENABLE_MVE_FP" FORCE)
18+
19+
# These variables are only defined in case of GNU toolchain as it is currently the only toolchain
20+
# that supports the ML Model component OTA update feature where these variables are needed.
21+
if (${TOOLCHAIN} STREQUAL "GNU")
22+
set(MCUBOOT_IMAGE_VERSION_NS_ML_MODEL "0.0.1+11" CACHE STRING "MCUBOOT_IMAGE_VERSION_NS_ML_MODEL" FORCE)
23+
set(MCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM "1_0" CACHE STRING "MCUBOOT_NS_ML_MODEL_IMAGE_FLASH_AREA_NUM" FORCE)
24+
set(MCUBOOT_SECURITY_COUNTER_NS_ML_MODEL 1 CACHE STRING "MCUBOOT_SECURITY_COUNTER_NS_ML_MODEL" FORCE)
25+
set(MCUBOOT_IMAGE_NUMBER 3 CACHE STRING "MCUBOOT_IMAGE_NUMBER" FORCE)
26+
set(DEFAULT_MCUBOOT_FLASH_MAP OFF CACHE BOOL "DEFAULT_MCUBOOT_FLASH_MAP" FORCE)
27+
else()
28+
set(DEFAULT_MCUBOOT_FLASH_MAP ON CACHE BOOL "DEFAULT_MCUBOOT_FLASH_MAP" FORCE)
29+
set(MCUBOOT_IMAGE_NUMBER 2 CACHE STRING "MCUBOOT_IMAGE_NUMBER" FORCE)
30+
endif()

applications/keyword_detection/configs/freertos_config/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 Arm Limited and/or its affiliates
1+
# Copyright 2023-2025 Arm Limited and/or its affiliates
22
33
# SPDX-License-Identifier: MIT
44

@@ -14,6 +14,6 @@ target_compile_definitions(freertos_config
1414

1515
target_link_libraries(freertos_config
1616
INTERFACE
17-
tfm-ns-interface
17+
tfm_api_ns
1818
app-config
1919
)

applications/keyword_detection/configs/mbedtls_config/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 Arm Limited and/or its affiliates
1+
# Copyright 2023-2025 Arm Limited and/or its affiliates
22
33
# SPDX-License-Identifier: MIT
44

@@ -7,11 +7,6 @@ target_include_directories(mbedtls-config
77
.
88
)
99

10-
target_compile_definitions(mbedtls-config
11-
INTERFACE
12-
MBEDTLS_CONFIG_FILE="aws_mbedtls_config.h"
13-
)
14-
1510
target_link_libraries(mbedtls-config
1611
INTERFACE
1712
freertos_kernel

applications/keyword_detection/configs/mbedtls_config/aws_mbedtls_config.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/*
1212
* Copyright The Mbed TLS Contributors
1313
* SPDX-License-Identifier: Apache-2.0
14-
* Copyright 2024 Arm Limited and/or its affiliates
14+
* Copyright 2024-2025 Arm Limited and/or its affiliates
1515
1616
*
1717
* Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -27,15 +27,21 @@
2727
* limitations under the License.
2828
*/
2929

30-
#include "app_config.h"
30+
#if DOMAIN_NS == 1
31+
#include "app_config.h"
3132

3233
/* AWS IoT Core Device Advisor validation is not supported on ARMClang because
3334
* ARMClang compiler does not support gmtime() function which is needed when
3435
* MBEDTLS_HAVE_TIME macro is defined. MBEDTLS_HAVE_TIME should be defined to
3536
* pass TLS Expired Server Cert test which is part of AWS IoT Core Device Advisor validation tests. */
36-
#if ( ( appCONFIG_DEVICE_ADVISOR_TEST_ACTIVE == 1 ) && ( defined( __ARMCC_VERSION ) ) )
37-
#error "AWS IoT Core Device Advisor validation is not supported on Arm Compiler For Embedded (ARMClang)"
38-
#endif
37+
#if ( ( appCONFIG_DEVICE_ADVISOR_TEST_ACTIVE == 1 ) && ( defined( __ARMCC_VERSION ) ) )
38+
#error "AWS IoT Core Device Advisor validation is not supported on Arm Compiler For Embedded (ARMClang)"
39+
#endif
40+
41+
#else /* DOMAIN_NS != 1 */
42+
/* Set if this config file is currently used for TF-M secure clients */
43+
#define PSA_CRYPTO_TFM_SECURE_CONFIG
44+
#endif /* DOMAIN_NS == 1 */
3945

4046
/**
4147
* This is an optional version symbol that enables compatibility handling of
@@ -1175,7 +1181,7 @@ void mbedtls_platform_free( void * ptr );
11751181
* \warning This interface is experimental and may change or be removed
11761182
* without notice.
11771183
*/
1178-
#ifdef PSA_CRYPTO_IMPLEMENTATION_TFM
1184+
#if defined( PSA_CRYPTO_IMPLEMENTATION_TFM ) || defined( PSA_CRYPTO_TFM_SECURE_CONFIG )
11791185
#define MBEDTLS_PSA_CRYPTO_CLIENT
11801186
#endif
11811187

@@ -1775,7 +1781,9 @@ void mbedtls_platform_free( void * ptr );
17751781
* This feature is still experimental and is not ready for production since
17761782
* it is not completed.
17771783
*/
1778-
/*#define MBEDTLS_PSA_CRYPTO_CONFIG */
1784+
#ifdef PSA_CRYPTO_TFM_SECURE_CONFIG
1785+
#define MBEDTLS_PSA_CRYPTO_CONFIG
1786+
#endif
17791787

17801788
/**
17811789
* \def MBEDTLS_VERSION_FEATURES

0 commit comments

Comments
 (0)