33# SPDX-License-Identifier: MIT
44
55cmake_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+
613project (keyword-detection LANGUAGES C CXX)
714
815set (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
2734set (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.
3138if (${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" )
4040endif ()
4141
4242if (${ML_INFERENCE_ENGINE} STREQUAL "ETHOS" )
@@ -48,42 +48,9 @@ endif()
4848set (ML_USE_CASE "kws" )
4949set (ML_MODEL "GenerateKWSModel" )
5050set (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
8855add_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.
166130add_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
204170list (APPEND CMAKE_MODULE_PATH ${IOT_REFERENCE_ARM_CORSTONE3XX_SOURCE_DIR} /tools/cmake)
205171include (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
236205if (${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 )
245214endif ()
@@ -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 )
271240endif ()
0 commit comments