1Trusted Firmware-M Integration
2##############################
3
4The Trusted Firmware-M (TF-M) section contains information about the
5integration between TF-M and Zephyr RTOS. Use this information to help
6understand how to integrate TF-M with Zephyr for Cortex-M platforms and make
7use of its secure run-time services in Zephyr applications.
8
9Board Definitions
10*****************
11
12TF-M will be built for the secure processing environment along with Zephyr if
13the :kconfig:option:`CONFIG_BUILD_WITH_TFM` flag is set to ``y``.
14
15Generally, this value should never be set at the application level, however,
16and all config flags required for TF-M should be set in a board variant with
17the ``_ns`` suffix.
18
19This board variant must define an appropriate flash, SRAM and peripheral
20configuration that takes into account the initialisation process in the secure
21processing environment. :kconfig:option:`CONFIG_TFM_BOARD` must also be set via
22`modules/trusted-firmware-m/Kconfig.tfm <https://github.com/zephyrproject-rtos/zephyr/blob/main/modules/trusted-firmware-m/Kconfig.tfm>`__
23to the board name that TF-M expects for this target, so that it knows which
24target to build for the secure processing environment.
25
26Example: ``mps2_an521_ns``
27==========================
28
29The ``mps2_an521`` target is a dual-core Arm Cortex-M33 evaluation board that,
30when using the default board variant, would generate a secure Zephyr binary.
31
32The optional ``mps2_an521_ns`` target, however, sets these additional
33kconfig flags that indicate that Zephyr should be built as a
34non-secure image, linked with TF-M as an external project, and optionally the
35secure bootloader:
36
37* :kconfig:option:`CONFIG_TRUSTED_EXECUTION_NONSECURE` ``y``
38* :kconfig:option:`CONFIG_ARM_TRUSTZONE_M` ``y``
39
40Comparing the ``mps2_an521.dts`` and ``mps2_an521_ns.dts`` files, we can see
41that the ``_ns`` version defines offsets in flash and SRAM memory, which leave
42the required space for TF-M and the secure bootloader:
43
44::
45
46    reserved-memory {
47		#address-cells = <1>;
48		#size-cells = <1>;
49		ranges;
50
51		/* The memory regions defined below must match what the TF-M
52		 * project has defined for that board - a single image boot is
53		 * assumed. Please see the memory layout in:
54		 * https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git/tree/platform/ext/target/mps2/an521/partition/flash_layout.h
55		 */
56
57		code: memory@100000 {
58			reg = <0x00100000 DT_SIZE_K(512)>;
59		};
60
61		ram: memory@28100000 {
62			reg = <0x28100000 DT_SIZE_M(1)>;
63		};
64	};
65
66This reserves 1 MB of code memory and 1 MB of RAM for secure boot and TF-M,
67such that our non-secure Zephyr application code will start at 0x10000, with
68RAM at 0x28100000. 512 KB code memory is available for the NS zephyr image,
69along with 1 MB of RAM.
70
71This matches the flash memory layout we see in ``flash_layout.h`` in TF-M:
72
73::
74
75    * 0x0000_0000 BL2 - MCUBoot (0.5 MB)
76    * 0x0008_0000 Secure image     primary slot (0.5 MB)
77    * 0x0010_0000 Non-secure image primary slot (0.5 MB)
78    * 0x0018_0000 Secure image     secondary slot (0.5 MB)
79    * 0x0020_0000 Non-secure image secondary slot (0.5 MB)
80    * 0x0028_0000 Scratch area (0.5 MB)
81    * 0x0030_0000 Protected Storage Area (20 KB)
82    * 0x0030_5000 Internal Trusted Storage Area (16 KB)
83    * 0x0030_9000 NV counters area (4 KB)
84    * 0x0030_A000 Unused (984 KB)
85
86``mps2/an521`` will be passed in to Tf-M as the board target, specified via
87:kconfig:option:`CONFIG_TFM_BOARD`.
88