|
| 1 | +.. _ug_tfm_building: |
| 2 | + |
| 3 | +Building and configuring TF-M |
| 4 | +############################# |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +TF-M is one of the images that are built as part of a multi-image application. |
| 11 | + |
| 12 | +To add TF-M to your build, enable the :kconfig:option:`CONFIG_BUILD_WITH_TFM` configuration option by adding it to your :file:`prj.conf` file. |
| 13 | + |
| 14 | +.. note:: |
| 15 | + If you use menuconfig to enable :kconfig:option:`CONFIG_BUILD_WITH_TFM`, you must also enable its dependencies. |
| 16 | + |
| 17 | +By default, TF-M is configured to build the :ref:`minimal version <tfm_minimal_build>`. |
| 18 | +To use the full TF-M, you must disable the :kconfig:option:`CONFIG_TFM_PROFILE_TYPE_MINIMAL` option. |
| 19 | + |
| 20 | +You must build TF-M using a non-secure board target. |
| 21 | +The following platforms are currently supported: |
| 22 | + |
| 23 | +* nRF54L15 |
| 24 | +* nRF5340 |
| 25 | +* nRF91 Series |
| 26 | + |
| 27 | +TF-M uses UART1 for logging from the secure application. |
| 28 | +To disable logging, enable the :kconfig:option:`CONFIG_TFM_LOG_LEVEL_SILENCE` option. |
| 29 | +When building TF-M with logging enabled, UART1 must be disabled in the non-secure application, otherwise the non-secure application will fail to run. |
| 30 | +The recommended way to do this is to copy the .overlay file from the :ref:`tfm_hello_world` sample. |
| 31 | + |
| 32 | +Enabling secure services |
| 33 | +======================== |
| 34 | + |
| 35 | +When using the :ref:`nrf_security`, if :kconfig:option:`CONFIG_BUILD_WITH_TFM` is enabled together with :kconfig:option:`CONFIG_NORDIC_SECURITY_BACKEND`, the TF-M secure image will enable the use of the hardware acceleration of Arm CryptoCell. |
| 36 | +In such case, the Kconfig configurations in the Nordic Security Backend control the features enabled through TF-M. |
| 37 | + |
| 38 | +See :ref:`tfm_partition_crypto` for more information about the TF-M Crypto partition. |
| 39 | + |
| 40 | +.. _tfm_minimal_build: |
| 41 | + |
| 42 | +Minimal build |
| 43 | +============= |
| 44 | + |
| 45 | +.. include:: tfm_supported_services.rst |
| 46 | + :start-after: minimal_build_overview_start |
| 47 | + :end-before: minimal_build_overview_end |
| 48 | + |
| 49 | +The minimal build uses an image of 32 kB. |
| 50 | +It is set with the :kconfig:option:`CONFIG_TFM_PROFILE_TYPE_MINIMAL` Kconfig option that is enabled by default on the nRF53 and nRF91 Series devices. |
| 51 | + |
| 52 | +With the minimal build, the configuration of TF-M is severely limited. |
| 53 | +Hence, it is not possible to modify the TF-M minimal configuration to create your own variant of the minimal configuration. |
| 54 | +Instead, the default configuration must be used as a starting point. |
| 55 | + |
| 56 | +.. _tfm_configurable_build: |
| 57 | + |
| 58 | +Configurable build |
| 59 | +================== |
| 60 | + |
| 61 | +.. include:: tfm_supported_services.rst |
| 62 | + :start-after: configurable_build_overview_start |
| 63 | + :end-before: configurable_build_overview_end |
| 64 | + |
| 65 | +To enable the configurable, full TF-M build, make sure the following Kconfig options are configured: |
| 66 | + |
| 67 | +* :kconfig:option:`CONFIG_BUILD_WITH_TFM` is enabled |
| 68 | +* :kconfig:option:`CONFIG_TFM_PROFILE_TYPE_NOT_SET` is enabled |
| 69 | +* :kconfig:option:`CONFIG_TFM_PROFILE_TYPE_MINIMAL` is disabled |
| 70 | + |
| 71 | +For description of the build profiles, see :ref:`tf-m_profiles`. |
| 72 | +It is not recommended to use predefined TF-M profiles as they might result in a larger memory footprint than necessary. |
| 73 | + |
| 74 | +When the :kconfig:option:`CONFIG_TFM_PROFILE_TYPE_NOT_SET` Kconfig option is enabled, the build process will not set a specific TF-M profile type. |
| 75 | +This allows for a more flexible configuration where individual TF-M features can be enabled or disabled as needed. |
| 76 | +It also provides more control over the build process and allows for a more fine-grained configuration of the TF-M secure image. |
| 77 | + |
| 78 | +To configure the features of the TF-M secure image, you must choose which TF-M partitions and which secure services to include in the build. |
| 79 | + |
| 80 | +.. note:: |
| 81 | + A "TF-M partition" in this context refers to a secure partition within the Trusted Firmware-M architecture. |
| 82 | + These partitions are isolated from each other and from the non-secure application code. |
| 83 | + A service running inside TF-M would typically be implemented within one of these secure partitions. |
| 84 | + |
| 85 | +Each service can be a separate partition, or multiple related services might be grouped into a single partition. |
| 86 | +The partition provides the execution environment for the service. |
| 87 | +It handles secure function calls and ensures that the service's code and data are protected from unauthorized access. |
| 88 | + |
| 89 | +Following are the available Kconfig options for TF-M partitions: |
| 90 | + |
| 91 | +.. list-table:: Available TF-M Partitions |
| 92 | + :header-rows: 1 |
| 93 | + |
| 94 | + * - Option name |
| 95 | + - Description |
| 96 | + - Default value |
| 97 | + - Dependencies |
| 98 | + * - :kconfig:option:`CONFIG_TFM_PARTITION_PLATFORM` |
| 99 | + - Provides platform services. |
| 100 | + - Enabled |
| 101 | + - |
| 102 | + * - :kconfig:option:`CONFIG_TFM_PARTITION_CRYPTO` |
| 103 | + - Provides cryptographic services. |
| 104 | + - Enabled |
| 105 | + - INTERNAL_TRUSTED_STORAGE |
| 106 | + * - :kconfig:option:`CONFIG_TFM_PARTITION_PROTECTED_STORAGE` |
| 107 | + - Provides secure storage services. |
| 108 | + - Enabled |
| 109 | + - PLATFORM, CRYPTO |
| 110 | + * - :kconfig:option:`CONFIG_TFM_PARTITION_INTERNAL_TRUSTED_STORAGE` |
| 111 | + - Provides internal trusted storage services. |
| 112 | + - Enabled |
| 113 | + - |
| 114 | + * - :kconfig:option:`CONFIG_TFM_PARTITION_INITIAL_ATTESTATION` |
| 115 | + - Provides initial attestation services. |
| 116 | + - Disabled |
| 117 | + - CRYPTO |
| 118 | + |
| 119 | +Secure Partition Manager backend configuration |
| 120 | +---------------------------------------------- |
| 121 | + |
| 122 | +TF-M's Secure Partition Manager (SPM) backend may also be configured, depending on the isolation requirements of the application. |
| 123 | + |
| 124 | +.. note:: |
| 125 | + Do not confuse TF-M's Secure Partition Manager with Secure Partition Manager that was removed in the |NCS| v2.1.0. |
| 126 | + See also `Migrating from Secure Partition Manager to Trusted Firmware-M`_ in the |NCS| v2.0.0 documentation. |
| 127 | + |
| 128 | +.. list-table:: SPM backends |
| 129 | + :header-rows: 1 |
| 130 | + |
| 131 | + * - Option |
| 132 | + - Description |
| 133 | + - Allowed isolation levels |
| 134 | + * - :kconfig:option:`CONFIG_TFM_SFN` |
| 135 | + - With SFN, the Secure Partition is made up of a collection of callback functions that implement secure services. |
| 136 | + - Level 1 |
| 137 | + * - :kconfig:option:`CONFIG_TFM_IPC` |
| 138 | + - With IPC, each Secure Partition processes signals in any order, and can defer responding to a message while continuing to process other signals. |
| 139 | + - Levels 1, 2 and 3 |
| 140 | + |
| 141 | +To control the number of logging messages, set the :kconfig:option:`CONFIG_TFM_LOG_LEVEL` Kconfig option. |
| 142 | +To disable logging, set the :kconfig:option:`CONFIG_TFM_LOG_LEVEL_SILENCE` option. |
| 143 | + |
| 144 | +The size of TF-M partitions is affected by multiple configuration options and hardware-related options. |
| 145 | +The code and memory size of TF-M increases when more services are enabled, but the selected hardware also places limitations on how the separation of secure and non-secure is made. |
| 146 | + |
| 147 | +TF-M is linked as a separate partition in the final binary image. |
| 148 | +The reserved sizes of its RAM and flash partitions are configured by the :kconfig:option:`CONFIG_PM_PARTITION_SIZE_TFM` and :kconfig:option:`CONFIG_PM_PARTITION_SIZE_TFM_SRAM` options. |
| 149 | +These configuration options allow you to specify the size allocated for the TF-M partition in the final binary image. |
| 150 | +Default partition sizes vary between device families and are not optimized to any specific use case. |
| 151 | + |
| 152 | +To optimize the TF-M size, find the minimal set of features to satisfy the application needs and then minimize the allocated partition sizes while still conforming to the alignment and granularity requirements of given hardware. |
| 153 | + |
| 154 | +.. _ug_tfm_partition_alignment_requirements: |
| 155 | + |
| 156 | +TF-M partition alignment requirements |
| 157 | +===================================== |
| 158 | + |
| 159 | +TF-M requires that secure and non-secure partition addresses must be aligned to the flash region size :kconfig:option:`CONFIG_NRF_TRUSTZONE_FLASH_REGION_SIZE`. |
| 160 | +The |NCS| ensures that they in fact are aligned and comply with the TF-M requirements. |
| 161 | + |
| 162 | +The following differences apply to the device families: |
| 163 | + |
| 164 | +* On nRF53 and nRF91 Series devices, TF-M uses the SPU to enforce the security policy between the partitions, so the :kconfig:option:`CONFIG_NRF_TRUSTZONE_FLASH_REGION_SIZE` Kconfig option is set to the SPU flash region size. |
| 165 | +* On nRF54L15 devices, TF-M uses the MPC to enforce the security policy between the partitions, so the :kconfig:option:`CONFIG_NRF_TRUSTZONE_FLASH_REGION_SIZE` option is set to the MPC region size. |
| 166 | + |
| 167 | +.. list-table:: Region limits on different hardware |
| 168 | + :header-rows: 1 |
| 169 | + |
| 170 | + * - Family |
| 171 | + - RAM granularity |
| 172 | + - ROM granularity |
| 173 | + * - nRF91 Series |
| 174 | + - 8 kB |
| 175 | + - 32 kB |
| 176 | + * - nRF53 Series |
| 177 | + - 8 kB |
| 178 | + - 16 kB |
| 179 | + * - nRF54 Series |
| 180 | + - 4 kB |
| 181 | + - 4 kB |
| 182 | + |
| 183 | +.. figure:: /images/nrf-secure-rom-granularity.svg |
| 184 | + :alt: Partition alignment granularity |
| 185 | + :width: 60em |
| 186 | + :align: left |
| 187 | + |
| 188 | + Partition alignment granularity on different nRF devices |
| 189 | + |
| 190 | +The imaginary example above shows a worst-case scenario in the nRF91 Series where the flash region size is 32 kB and both the TF-M binary and secure storage are 12 kB. |
| 191 | +This leaves a significant amount of unused space in the flash region. |
| 192 | +In a real-world scenario, the size of the TF-M binary and secure storage is usually much larger. |
| 193 | + |
| 194 | +When the :ref:`partition_manager` is enabled, it will take into consideration the alignment requirements. |
| 195 | +But when the static partitions are used, the user is responsible for following the alignment requirements. |
| 196 | + |
| 197 | +.. figure:: /images/secure-flash-regions.svg |
| 198 | + :alt: Example of aligning partitions with flash regions |
| 199 | + :width: 60em |
| 200 | + :align: left |
| 201 | + |
| 202 | + Example of aligning partitions with flash regions |
| 203 | + |
| 204 | +.. note:: |
| 205 | + If you are experiencing any partition alignment issues when using the Partition Manager, check the `known issues page on the main branch`_. |
| 206 | + |
| 207 | +You need to align the ``tfm_nonsecure``, ``tfm_storage``, and ``nonsecure_storage`` partitions with the TrustZone flash region size. |
| 208 | +Both the partition start address and the partition size need to be aligned with the flash region size :kconfig:option:`CONFIG_NRF_TRUSTZONE_FLASH_REGION_SIZE`. |
| 209 | + |
| 210 | +.. note:: |
| 211 | + The ``tfm_nonsecure`` partition is placed after the ``tfm_secure`` partition, thus the end address of the ``tfm_secure`` partition is the same as the start address of the ``tfm_nonsecure`` partition. |
| 212 | + As a result, altering the size of the ``tfm_secure`` partition affects the start address of the ``tfm_nonsecure`` partition. |
| 213 | + |
| 214 | +The following static partition snippet shows a non-aligned configuration for nRF5340 which has a TrustZone flash region size :kconfig:option:`CONFIG_NRF_TRUSTZONE_FLASH_REGION_SIZE` of 0x4000. |
| 215 | + |
| 216 | +.. code-block:: console |
| 217 | +
|
| 218 | + tfm_secure: |
| 219 | + address: 0x4000 |
| 220 | + size: 0x4200 |
| 221 | + span: [mcuboot_pad, tfm] |
| 222 | + mcuboot_pad: |
| 223 | + address: 0x4000 |
| 224 | + size: 0x200 |
| 225 | + tfm: |
| 226 | + address: 0x4200 |
| 227 | + size: 0x4000 |
| 228 | + tfm_nonsecure: |
| 229 | + address: 0x8200 |
| 230 | + size: 0x4000 |
| 231 | + span: [app] |
| 232 | + app: |
| 233 | + address: 0x8200 |
| 234 | + size: 0x4000 |
| 235 | +
|
| 236 | +In the above example, the ``tfm_nonsecure`` partition starts at address 0x8200, which is not aligned with the requirement of 0x4000. |
| 237 | +Since ``tfm_secure`` spans the ``mcuboot_pad`` and ``tfm`` partitions we can decrease the size of any of them by 0x200 to fix the alignment issue. |
| 238 | +We will decrease the size of the (optional) ``mcuboot_pad`` partition and thus the size of the ``tfm_secure`` partition as follows: |
| 239 | + |
| 240 | +.. code-block:: console |
| 241 | +
|
| 242 | + tfm_secure: |
| 243 | + address: 0x4000 |
| 244 | + size: 0x4000 |
| 245 | + span: [mcuboot_pad, tfm] |
| 246 | + mcuboot_pad: |
| 247 | + address: 0x4000 |
| 248 | + size: 0x0 |
| 249 | + tfm: |
| 250 | + address: 0x4000 |
| 251 | + size: 0x4000 |
| 252 | + tfm_nonsecure: |
| 253 | + address: 0x8000 |
| 254 | + size: 0x4000 |
| 255 | + span: [app] |
| 256 | + app: |
| 257 | + address: 0x8000 |
| 258 | + size: 0x4000 |
| 259 | +
|
| 260 | +Analyzing ``tfm_secure`` partition size |
| 261 | +======================================= |
| 262 | + |
| 263 | +You can analyze the size of the ``tfm_secure`` partition from the build output: |
| 264 | + |
| 265 | +.. code-block:: console |
| 266 | +
|
| 267 | + [71/75] Linking C executable bin/tfm_s.axf |
| 268 | + Memory region Used Size Region Size %age Used |
| 269 | + FLASH: 31972 B 256 KB 12.20% |
| 270 | + RAM: 4804 B 88 KB 5.33% |
| 271 | +
|
| 272 | +The example shows that the :kconfig:option:`CONFIG_PM_PARTITION_SIZE_TFM` Kconfig option for the ``tfm_secure`` partition is set to 256 kB and the TF-M binary uses 32 kB of the available space. |
| 273 | +Similarly, the :kconfig:option:`CONFIG_PM_PARTITION_SIZE_TFM_SRAM` option for the ``tfm_secure`` partition is set to 88 kB and the TF-M binary uses 5 kB of the available space. |
| 274 | +You can use this information to optimize the size of the TF-M, as long as it is within the alignment requirements explained in the previous section. |
| 275 | + |
| 276 | +Tools for analyzing the ``tfm_secure`` partition size |
| 277 | +----------------------------------------------------- |
| 278 | + |
| 279 | +The TF-M build system is compatible with Zephyr's :ref:`zephyr:footprint_tools` tools that let you generate RAM and ROM usage reports (using :ref:`zephyr:sysbuild_dedicated_image_build_targets`). |
| 280 | +You can use the reports to analyze the memory usage of the different TF-M partitions and see how changing the Kconfig options affects the memory usage. |
| 281 | + |
| 282 | +Depending on your development environment, you can generate memory reports for TF-M in the following ways: |
| 283 | + |
| 284 | +.. tabs:: |
| 285 | + |
| 286 | + .. group-tab:: nRF Connect for VS Code |
| 287 | + |
| 288 | + You can use the `Memory report`_ feature in the |nRFVSC| to check the size and percentage of memory that each symbol uses on your device for RAM, ROM, and partitions (when applicable). |
| 289 | + |
| 290 | + .. group-tab:: Command line |
| 291 | + |
| 292 | + You can use the :ref:`zephyr:sysbuild_dedicated_image_build_targets` ``tfm_ram_report`` and ``tfm_rom_report`` targets for analyzing the memory usage of the TF-M partitions inside the ``tfm_secure`` partition. |
| 293 | + For example, after building the :ref:`tfm_hello_world` sample for the ``nrf9151dk/nrf9151/ns`` board target, you can run the following commands from your application root directory to generate the RAM memory report for TF-M in the terminal: |
| 294 | + |
| 295 | + .. code-block:: console |
| 296 | +
|
| 297 | + west build -d build/tfm_hello_world -t tfm_ram_report |
| 298 | +
|
| 299 | +For more information about the ``tfm_ram_report`` and ``tfm_rom_report`` targets, refer to the :ref:`tfm_build_system` documentation. |
0 commit comments