Build: Port b_u585i_iot02A on split build

Change-Id: I88e54e43a9b3289735f385513f956a5dbfe74945
Signed-off-by: Michel JAOUEN <michel.jaouen@st.com>
diff --git a/docs/platform/stm/common/stm32u5xx/readme.rst b/docs/platform/stm/common/stm32u5xx/readme.rst
index 97c34f9..9db6e52 100644
--- a/docs/platform/stm/common/stm32u5xx/readme.rst
+++ b/docs/platform/stm/common/stm32u5xx/readme.rst
@@ -34,7 +34,7 @@
 
 Write software on target
 ^^^^^^^^^^^^^^^^^^^^^^^^
-In build folder:
+In secure build folder directory api_ns:
 
   - ``postbuild.sh``: Updates regression.sh and TFM_UPDATE.sh scripts according to flash_layout.h
   - ``regression.sh``: Sets platform option bytes config and erase platform
diff --git a/platform/ext/target/stm/b_u585i_iot02a/CMakeLists.txt b/platform/ext/target/stm/b_u585i_iot02a/CMakeLists.txt
index 3182703..1001166 100644
--- a/platform/ext/target/stm/b_u585i_iot02a/CMakeLists.txt
+++ b/platform/ext/target/stm/b_u585i_iot02a/CMakeLists.txt
@@ -29,12 +29,37 @@
     include(${STM_COMMON_DIR}/secure_element/CMakeLists.txt)
 endif()
 
-if(NS)
-    target_sources(tfm_ns
-        PRIVATE
-            ${STM_COMMON_DIR}/stm32u5xx/Device/Source/startup_stm32u5xx_ns.c
-    )
-endif()
+# cpuarch.cmake is used to set things that related to the platform that are both
+install(FILES
+    ${TARGET_PLATFORM_PATH}/cpuarch.cmake
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}
+)
+
+install(FILES
+    ${STM_COMMON_DIR}/stm32u5xx/Device/Source/startup_stm32u5xx_ns.c
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/Device/Source
+)
+
+install(DIRECTORY
+    ${TARGET_PLATFORM_PATH}/ns/
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}
+)
+
+install(DIRECTORY
+    ${TARGET_PLATFORM_PATH}/include
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}
+)
+
+install(FILES
+    ${TARGET_PLATFORM_PATH}//accelerator/crypto_accelerator_config.h
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/include
+)
+
+install(FILES
+    ${B_U585I_IOT02A_DIR}/partition/flash_layout.h
+    ${B_U585I_IOT02A_DIR}/partition/region_defs.h
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/partition
+)
 
 if(BL2)
     target_sources(bl2
@@ -46,11 +71,16 @@
             ${B_U585I_IOT02A_DIR}/src/b_u585i_iot02a_ospi.c
     )
 endif()
-
-install(FILES ${B_U585I_IOT02A_DIR}/include/flash_layout.h
-              ${B_U585I_IOT02A_DIR}/include/region_defs.h
-        DESTINATION  ${CMAKE_BINARY_DIR} )
+#install flash layout for postbuild.sh
+install(FILES
+    ${B_U585I_IOT02A_DIR}/partition/flash_layout.h
+    ${B_U585I_IOT02A_DIR}/partition/region_defs.h
+    DESTINATION ${CMAKE_INSTALL_PREFIX}
+)
 set (BL2_FILE_TO_PREPROCESS ${CMAKE_CURRENT_BINARY_DIR}/image_macros_to_preprocess_bl2.c)
 file(WRITE ${BL2_FILE_TO_PREPROCESS} ${BL2_PREPROCESSING})
-install(FILES ${BL2_FILE_TO_PREPROCESS}
-        DESTINATION ${CMAKE_BINARY_DIR} )
+
+install(FILES
+    ${BL2_FILE_TO_PREPROCESS}
+    DESTINATION ${CMAKE_INSTALL_PREFIX}
+)
diff --git a/platform/ext/target/stm/b_u585i_iot02a/cpuarch.cmake b/platform/ext/target/stm/b_u585i_iot02a/cpuarch.cmake
new file mode 100644
index 0000000..dcb92e4
--- /dev/null
+++ b/platform/ext/target/stm/b_u585i_iot02a/cpuarch.cmake
@@ -0,0 +1,21 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2020, Arm Limited. All rights reserved.
+# Copyright (c) 2021 STMicroelectronics. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+# preload.cmake is used to set things that related to the platform that are both
+# immutable and global, which is to say they should apply to any kind of project
+# that uses this platform. In practise this is normally compiler definitions and
+# variables related to hardware.
+
+# Set architecture and CPU
+set(TFM_SYSTEM_PROCESSOR cortex-m33)
+set(TFM_SYSTEM_ARCHITECTURE armv8-m.main)
+set(CRYPTO_HW_ACCELERATOR_TYPE stm)
+add_compile_definitions(
+    STM32U585xx
+    USE_HAL_DRIVER
+)
diff --git a/platform/ext/target/stm/b_u585i_iot02a/ns/CMakeLists.txt b/platform/ext/target/stm/b_u585i_iot02a/ns/CMakeLists.txt
new file mode 100644
index 0000000..8abbfef
--- /dev/null
+++ b/platform/ext/target/stm/b_u585i_iot02a/ns/CMakeLists.txt
@@ -0,0 +1,56 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+# This file is exported to NS side during CMake installation phase and renamed
+# to CMakeLists.txt. It instructs how to build a platform on non-secture side.
+# The structure and sources list are fully platform specific.
+
+add_library(platform_ns)
+
+target_sources(platform_ns
+    PRIVATE
+        Device/Source/startup_stm32u5xx_ns.c
+        hal/Src/stm32u5xx_hal.c
+        Device/Source/system_stm32u5xx.c
+        CMSIS_Driver/low_level_com.c
+        hal/Src/stm32u5xx_hal_dma.c
+        hal/Src/stm32u5xx_hal_dma_ex.c
+        hal/Src/stm32u5xx_hal_pwr.c
+        hal/Src/stm32u5xx_hal_pwr_ex.c
+        hal/Src/stm32u5xx_hal_rcc.c
+        hal/Src/stm32u5xx_hal_gpio.c
+        hal/Src/stm32u5xx_hal_uart.c
+        hal/Src/stm32u5xx_hal_uart_ex.c
+        hal/Src/stm32u5xx_hal_cortex.c
+        hal/Src/stm32u5xx_hal_rcc_ex.c
+)
+
+target_include_directories(platform_ns
+    PUBLIC
+        include
+        ext/cmsis
+        Device/Include
+        hal/Inc
+)
+
+# Include region_defs.h and flash_layout.h
+target_include_directories(platform_region_defs
+    INTERFACE
+        partition
+)
+
+if(${CMAKE_C_COMPILER_ID} STREQUAL GNU)
+    set(scatter_file ${CMAKE_CURRENT_LIST_DIR}/linker_scripts/appli_ns.ld)
+elseif(${CMAKE_C_COMPILER_ID} STREQUAL ARMClang)
+    set(scatter_file ${CMAKE_CURRENT_LIST_DIR}/linker_scripts/appli_ns.sct)
+elseif(${CMAKE_C_COMPILER_ID} STREQUAL IAR)
+    set(scatter_file ${CMAKE_CURRENT_LIST_DIR}/linker_scripts/appli_ns.icf)
+endif()
+set_target_properties(platform_ns
+                      PROPERTIES INTERFACE_LINK_DEPENDS
+                      ${scatter_file}
+)
diff --git a/platform/ext/target/stm/b_u585i_iot02a/include/flash_layout.h b/platform/ext/target/stm/b_u585i_iot02a/partition/flash_layout.h
similarity index 100%
rename from platform/ext/target/stm/b_u585i_iot02a/include/flash_layout.h
rename to platform/ext/target/stm/b_u585i_iot02a/partition/flash_layout.h
diff --git a/platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h b/platform/ext/target/stm/b_u585i_iot02a/partition/region_defs.h
similarity index 100%
rename from platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h
rename to platform/ext/target/stm/b_u585i_iot02a/partition/region_defs.h
diff --git a/platform/ext/target/stm/b_u585i_iot02a/tests/psa_arch_tests_config.cmake b/platform/ext/target/stm/b_u585i_iot02a/tests/psa_arch_tests_config.cmake
new file mode 100644
index 0000000..cc45044
--- /dev/null
+++ b/platform/ext/target/stm/b_u585i_iot02a/tests/psa_arch_tests_config.cmake
@@ -0,0 +1,7 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023 STMicroelectronics. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+set(PSA_API_TEST_TARGET stm32l562e_dk)
diff --git a/platform/ext/target/stm/common/scripts/TFM_UPDATE.sh b/platform/ext/target/stm/common/scripts/TFM_UPDATE.sh
index 2d02596..7fe7f9d 100644
--- a/platform/ext/target/stm/common/scripts/TFM_UPDATE.sh
+++ b/platform/ext/target/stm/common/scripts/TFM_UPDATE.sh
@@ -21,7 +21,10 @@
 SCRIPT=$(readlink -f $0)
 # Absolute path this script
 SCRIPTPATH=`dirname $SCRIPT`
-BINPATH="$SCRIPTPATH/bin"
+BINPATH_SPE="$SCRIPTPATH/bin"
+BINPATH_BL2="$SCRIPTPATH/bin"
+BINPATH_NSPE="$SCRIPTPATH/image_signing/scripts"
+
 PATH="/C/Program Files/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin/":$PATH
 stm32programmercli="STM32_Programmer_CLI"
 # part ot be updated according to flash_layout.h
@@ -60,12 +63,12 @@
 echo "Write TFM_Appli Secure"
 # part ot be updated according to flash_layout.h
 
-$stm32programmercli $connect -d $BINPATH/tfm_s_signed.bin $slot_s -v
+$stm32programmercli $connect -d $BINPATH_SPE/tfm_s_signed.bin $slot_s -v
 echo "TFM_Appli Secure Written"
 echo "Write TFM_Appli NonSecure"
-$stm32programmercli $connect -d $BINPATH/tfm_ns_signed.bin $slot_ns -v
+$stm32programmercli $connect -d $BINPATH_NSPE/tfm_ns_signed.bin $slot_ns -v
 echo "TFM_Appli NonSecure Written"
 echo "Write TFM_SBSFU_Boot"
-$stm32programmercli $connect -d $BINPATH/bl2.bin $boot -v
+$stm32programmercli $connect -d $BINPATH_BL2/bl2.bin $boot -v
 echo "TFM_SBSFU_Boot Written"
 echo "TFM_UPDATE Done"
\ No newline at end of file
diff --git a/platform/ext/target/stm/common/secure_element/CMakeLists.txt b/platform/ext/target/stm/common/secure_element/CMakeLists.txt
index 4b8b8e8..2944d2e 100644
--- a/platform/ext/target/stm/common/secure_element/CMakeLists.txt
+++ b/platform/ext/target/stm/common/secure_element/CMakeLists.txt
@@ -35,4 +35,5 @@
 )
 
 install(FILES  ${SE_PSA_DIR}/se_psa_id.h
-    DESTINATION ${TFM_INSTALL_PATH}/interface/include)
+    DESTINATION ${INSTALL_INTERFACE_INC_DIR}
+)
diff --git a/platform/ext/target/stm/common/stm32u5xx/CMakeLists.txt b/platform/ext/target/stm/common/stm32u5xx/CMakeLists.txt
index d586863..9072049 100644
--- a/platform/ext/target/stm/common/stm32u5xx/CMakeLists.txt
+++ b/platform/ext/target/stm/common/stm32u5xx/CMakeLists.txt
@@ -13,6 +13,7 @@
 
 target_include_directories(platform_region_defs
     INTERFACE
+        partition
         include
         ${CMAKE_CURRENT_SOURCE_DIR}/boards
 )
@@ -31,17 +32,6 @@
     $<$<AND:$<VERSION_EQUAL:${TFM_ISOLATION_LEVEL},3>,$<C_COMPILER_ID:GNU>>:${CMAKE_BINARY_DIR}/generated/platform/ext/common/gcc/tfm_isolation_l3.ld>
 )
 
-if(NS)
-    target_add_scatter_file(tfm_ns
-        $<$<C_COMPILER_ID:GNU>:${STM_COMMON_DIR}/hal/template/gcc/appli_ns.ld>
-        $<$<C_COMPILER_ID:ARMClang>:${STM_COMMON_DIR}/hal/template/armclang/appli_ns.sct>
-        $<$<C_COMPILER_ID:IAR>:${STM_COMMON_DIR}/hal/template/iar/appli_ns.icf>
-    )
-    target_link_libraries(CMSIS_5_tfm_ns
-        INTERFACE
-            CMSIS_5_RTX_V8MMN
-    )
-endif()
 
 if(BL2)
     target_add_scatter_file(bl2
@@ -123,17 +113,52 @@
 
 #========================= Platform Non-Secure ================================#
 
-target_include_directories(platform_ns
-    PUBLIC
-        ${CMAKE_CURRENT_SOURCE_DIR}/Device/Include
-        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Inc
+# Install linker scripts
+install(FILES
+    ${STM_COMMON_DIR}/hal/template/iar/appli_ns.icf
+    ${STM_COMMON_DIR}/hal/template/gcc/appli_ns.ld
+    ${STM_COMMON_DIR}/hal/template/armclang/appli_ns.sct
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/linker_scripts
 )
 
-target_sources(platform_ns
-    PRIVATE
+install(DIRECTORY
+    ${CMAKE_CURRENT_SOURCE_DIR}/Device/Include
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/Device
+)
+
+install(FILES
+    ${PLATFORM_DIR}/ext/common/uart_stdout.h
+    ${PLATFORM_DIR}/ext/common/test_interrupt.h
+    ${PLATFORM_DIR}/ext/driver/Driver_USART.h
+    ${PLATFORM_DIR}/ext/driver/Driver_Common.h
+    ${PLATFORM_DIR}/include/tfm_plat_defs.h
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/include
+)
+
+install(DIRECTORY
+    ${CMAKE_CURRENT_SOURCE_DIR}/hal/Inc
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/hal
+)
+
+install(FILES
+    ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_com.c
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/CMSIS_Driver
+)
+install(FILES
+    ${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/Templates/system_stm32u5xx.c
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/Device/Source/
+)
+
+install(FILES
+    ${CMAKE_CURRENT_SOURCE_DIR}/boards/cmsis.h
+    ${CMAKE_CURRENT_SOURCE_DIR}/boards/target_cfg.h
+    ${CMAKE_CURRENT_SOURCE_DIR}/boards/tfm_peripherals_def.h
+    DESTINATION ${INSTALL_PLATFORM_NS_DIR}/include
+)
+
+
+install(FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/Device/Source/Templates/system_stm32u5xx.c
-        ${STM_COMMON_DIR}/hal/CMSIS_Driver/low_level_com.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_dma_ex.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_pwr.c
@@ -143,8 +168,8 @@
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_uart_ex.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_cortex.c
-        ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc.c
         ${CMAKE_CURRENT_SOURCE_DIR}/hal/Src/stm32u5xx_hal_rcc_ex.c
+        DESTINATION ${INSTALL_PLATFORM_NS_DIR}/hal/Src
 )
 
 #========================= Platform BL2 =======================================#
@@ -235,7 +260,7 @@
         ${STM_COMMON_DIR}/scripts/stm_tool.py
         ${STM_COMMON_DIR}/scripts/bin2hex.py
         ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../bl2/ext/mcuboot/scripts/macro_parser.py
-        DESTINATION ${CMAKE_BINARY_DIR}/scripts/ )
+        DESTINATION ${CMAKE_INSTALL_PREFIX}/scripts )
 
 if (${CMAKE_C_COMPILER_ID} STREQUAL GNU)
     set(PREPROCESSOR_FILE ${STM_COMMON_DIR}/scripts/gcc/preprocess.sh)
@@ -251,7 +276,7 @@
          ${STM_COMMON_DIR}/scripts/TFM_BIN2HEX.sh
          ${STM_COMMON_DIR}/scripts/postbuild.sh
          PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
-         DESTINATION ${CMAKE_BINARY_DIR} )
+         DESTINATION ${CMAKE_INSTALL_PREFIX} )
 
 if (${TEST_S} OR ${TEST_NS})
     #the define FLASH_LAYOUT_FOR_TEST