TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 1 | ####################################################### |
| 2 | TF-M Internal Trusted Storage Service Integration Guide |
| 3 | ####################################################### |
| 4 | |
| 5 | ************ |
| 6 | Introduction |
| 7 | ************ |
| 8 | TF-M Internal Trusted Storage (ITS) service implements PSA Internal Trusted |
| 9 | Storage APIs. |
| 10 | |
| 11 | The service is backed by hardware isolation of the flash access domain and |
Jamie Fox | dd3de95 | 2019-11-25 17:45:40 +0000 | [diff] [blame] | 12 | relies on hardware to isolate the flash area from access by the Non-secure |
| 13 | Processing Environment, as well as the Application Root of Trust at higher |
| 14 | levels of isolation. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 15 | |
| 16 | The current ITS service design relies on hardware abstraction provided by TF-M. |
| 17 | The ITS service provides a non-hierarchical storage model, as a filesystem, |
| 18 | where all the assets are managed by a linearly indexed list of metadata. |
| 19 | |
| 20 | The design addresses the following high level requirements as well: |
| 21 | |
| 22 | - **Confidentiality** - Resistance to unauthorised accesses through |
| 23 | hardware/software attacks. Assumed to be provided by the internal flash |
| 24 | device, backed by hardware isolation. |
| 25 | |
| 26 | - **Access Authentication** - Mechanism to establish requester's identity (a |
| 27 | non-secure entity, secure entity, or a remote server). |
| 28 | |
| 29 | - **Integrity** - Resistance to tampering by attackers with physical access is |
| 30 | assumed to be provided by the internal flash device itself, while resistance |
| 31 | to tampering by Non-secure or App RoT attackers also requires hardware |
| 32 | isolation. |
| 33 | |
| 34 | - **Reliability** - Resistance to power failure scenarios and incomplete write |
| 35 | cycles. |
| 36 | |
| 37 | - **Configurability** - High level of configurability to scale up/down memory |
| 38 | footprint to cater for a variety of devices with varying requirements. |
| 39 | |
| 40 | - **Performance** - Optimized to be used for resource constrained devices with |
| 41 | very small silicon footprint, the PPA (power, performance, area) should be |
| 42 | optimal. |
| 43 | |
| 44 | ******************************* |
| 45 | Current ITS Service Limitations |
| 46 | ******************************* |
| 47 | - **Fragmentation** - The current design does not support fragmentation, as an |
| 48 | asset is stored in a contiguous space in a block. This means that the maximum |
| 49 | asset size can only be up-to a block size. Each block can potentially store |
| 50 | multiple assets. |
| 51 | A delete operation implicitly moves all the assets towards the top of the |
| 52 | block to avoid fragmentation within block. However, this may also result in |
| 53 | unutilized space at the end of each block. |
| 54 | |
| 55 | - **Non-hierarchical storage model** - The current design uses a |
| 56 | non-hierarchical storage model, as a filesystem, where all the assets are |
| 57 | managed by a linearly indexed list of metadata. This model locates the |
| 58 | metadata in blocks which are always stored in the same flash location. That |
| 59 | increases the number of writes in a specific flash location as every change in |
| 60 | the storage area requires a metadata update. |
| 61 | |
| 62 | - **Protection against physical storage medium failure** - Complete handling of |
| 63 | inherent failures of storage mediums (e.g. bad blocks in a NAND based device) |
| 64 | is not supported by the current design. |
| 65 | |
| 66 | - **Lifecycle management** - Currently, it does not support any subscription |
| 67 | based keys and certificates required in a secure lifecycle management. Hence, |
| 68 | an asset's validity time-stamp can not be invalidated based on the system |
| 69 | time. |
| 70 | |
| 71 | - **Provisioning vs user/device data** - In the current design, all assets are |
| 72 | treated in the same manner. In an alternative design, it may be required to |
| 73 | create separate partitions for provisioning content and user/device generated |
| 74 | content. This is to allow safe update of provisioning data during firmware |
| 75 | updates without the need to wipe out the user/device generated data. |
| 76 | |
| 77 | ************** |
| 78 | Code Structure |
| 79 | ************** |
| 80 | TF-M Internal Trusted Storage service code is located in |
Ken Liu | 738a4b0 | 2020-06-04 14:52:38 +0800 | [diff] [blame] | 81 | ``secure_fw/partitions/internal_trusted_storage/`` and is divided as follows: |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 82 | |
| 83 | - Core files |
| 84 | - Flash filesystem interfaces |
| 85 | - Flash interfaces |
| 86 | |
| 87 | The PSA ITS interfaces for the TF-M ITS service are located in |
| 88 | ``interface/include/psa``. |
| 89 | |
| 90 | PSA Internal Trusted Storage Interfaces |
| 91 | ======================================= |
| 92 | |
| 93 | The TF-M ITS service exposes the following mandatory PSA ITS interfaces |
| 94 | version 1.0: |
| 95 | |
| 96 | .. code-block:: c |
| 97 | |
| 98 | psa_status_t psa_its_set(psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags); |
| 99 | psa_status_t psa_its_get(psa_storage_uid_t uid, size_t data_offset, size_t data_size, void *p_data, size_t *p_data_length); |
| 100 | psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info); |
| 101 | psa_status_t psa_its_remove(psa_storage_uid_t uid); |
| 102 | |
| 103 | These PSA ITS interfaces and TF-M ITS types are defined and documented in |
| 104 | ``interface/include/psa/storage_common.h``, |
| 105 | ``interface/include/psa/internal_trusted_storage.h``, and |
| 106 | ``interface/include/tfm_its_defs.h`` |
| 107 | |
| 108 | Core Files |
| 109 | ========== |
| 110 | - ``tfm_its_req_mngr.c`` - Contains the ITS request manager implementation which |
| 111 | handles all requests which arrive to the service. This layer extracts the |
| 112 | arguments from the input and output vectors, and it calls the internal trusted |
| 113 | storage layer with the provided parameters. |
| 114 | |
| 115 | - ``tfm_internal_trusted_storage.c`` - Contains the TF-M internal trusted |
| 116 | storage API implementations which are the entry points to the ITS service. |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 117 | Constructs a filesystem configuration using information from the ITS HAL, |
| 118 | allocates a filesystem context for ITS and makes appropriate FS calls. Also |
| 119 | handles requests from the PS partition with a separate FS config and context. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 120 | |
| 121 | - ``its_utils.c`` - Contains common and basic functionalities used across the |
| 122 | ITS service code. |
| 123 | |
| 124 | Flash Filesystem Interface |
| 125 | ========================== |
| 126 | - ``flash_fs/its_flash_fs.h`` - Abstracts the flash filesystem operations used |
| 127 | by the internal trusted storage service. The purpose of this abstraction is to |
Jamie Fox | dd3de95 | 2019-11-25 17:45:40 +0000 | [diff] [blame] | 128 | have the ability to plug-in other filesystems or filesystem proxies |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 129 | (supplicant). |
| 130 | |
| 131 | - ``flash_fs/its_flash_fs.c`` - Contains the ``its_flash_fs`` implementation for |
| 132 | the required interfaces. |
| 133 | |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 134 | - ``flash_fs/its_flash_fs_mblock.c`` - Contains the metadata block manipulation |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 135 | functions required to implement the ``its_flash_fs`` interfaces in |
| 136 | ``flash_fs/its_flash_fs.c``. |
| 137 | |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 138 | - ``flash_fs/its_flash_fs_dblock.c`` - Contains the data block manipulation |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 139 | functions required to implement the ``its_flash_fs`` interfaces in |
| 140 | ``flash_fs/its_flash_fs.c``. |
| 141 | |
| 142 | The system integrator **may** replace this implementation with its own |
| 143 | flash filesystem implementation or filesystem proxy (supplicant). |
| 144 | |
| 145 | Flash Interface |
| 146 | =============== |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 147 | The ITS filesystem flash interface is defined by ``struct its_flash_fs_ops_t`` |
| 148 | in ``flash_fs/its_flash_fs.h``. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 149 | |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 150 | Implementations of the ITS filesystem flash interface for different types of |
| 151 | storage can be found in the ```internal_trusted_storage/flash`` directory. |
| 152 | |
| 153 | - ``flash/its_flash.h`` - Helper header that includes the correct ITS flash |
| 154 | interface implementation for the target, and abstracts the allocation of |
| 155 | different flash device types. |
Jamie Fox | 9fb2838 | 2019-11-26 17:03:18 +0000 | [diff] [blame] | 156 | |
Jamie Fox | d70da21 | 2019-11-28 14:41:45 +0000 | [diff] [blame] | 157 | - ``flash/its_flash_nand.c`` - Implements the ITS flash interface for a NAND |
| 158 | flash device, on top of the CMSIS flash interface implemented by the target. |
| 159 | This implementation writes entire block updates in one-shot, so the CMSIS |
| 160 | flash implementation **must** be able to detect incomplete writes and return |
| 161 | an error the next time the block is read. |
| 162 | |
Jamie Fox | 9fb2838 | 2019-11-26 17:03:18 +0000 | [diff] [blame] | 163 | - ``flash/its_flash_nor.c`` - Implements the ITS flash interface for a NOR flash |
| 164 | device, on top of the CMSIS flash interface implemented by the target. |
| 165 | |
| 166 | - ``flash/its_flash_ram.c`` - Implements the ITS flash interface for an emulated |
| 167 | flash device using RAM, on top of the CMSIS flash interface implemented by the |
| 168 | target. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 169 | |
Jamie Fox | 9fb2838 | 2019-11-26 17:03:18 +0000 | [diff] [blame] | 170 | The CMSIS flash interface **must** be implemented for each target based on its |
| 171 | flash controller. |
| 172 | |
David Hu | 74977d2 | 2019-10-22 10:26:03 +0800 | [diff] [blame] | 173 | The ITS flash interface depends on target-specific definitions from |
| 174 | ``platform/ext/target/<TARGET_NAME>/partition/flash_layout.h``. |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 175 | Please see the `Internal Trusted Storage Service HAL` section for details. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 176 | |
| 177 | ***************************** |
| 178 | ITS Service Integration Guide |
| 179 | ***************************** |
| 180 | This section describes mandatory (i.e. **must** implement) or optional |
| 181 | (i.e. **may** implement) interfaces which the system integrator has to take in |
| 182 | to account in order to integrate the internal trusted storage service in a new |
| 183 | platform. |
| 184 | |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 185 | Flash Interface |
| 186 | =============== |
| 187 | For ITS service operations, a contiguous set of blocks must be earmarked for |
| 188 | the internal trusted storage area. The design requires either 2 blocks, or any |
| 189 | number of blocks greater than or equal to 4. Total number of blocks can not be |
| 190 | 0, 1 or 3. This is a design choice limitation to provide power failure safe |
| 191 | update operations. |
| 192 | |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 193 | Maximum Asset Size |
| 194 | ================== |
| 195 | An asset is stored in a contiguous space in a logical filesystem block. The |
| 196 | maximum size of an asset can be up-to the size of the data block. Typically, |
| 197 | each logical block corresponds to one physical flash erase sector (the smallest |
| 198 | unit that can erased), but the ``TFM_HAL_ITS_SECTORS_PER_BLOCK`` configuration |
| 199 | below allows a number of contiguous erase sectors to form one logical block. |
| 200 | |
| 201 | Internal Trusted Storage Service HAL |
| 202 | ==================================== |
| 203 | The ITS service requires the platform to implement the ITS HAL, defined in |
| 204 | ``platform/include/tfm_hal_its.h``. |
| 205 | |
| 206 | The following C definitions in the HAL are mandatory, and must be defined by the |
| 207 | platform in a header named ``flash_layout.h``: |
| 208 | |
| 209 | - ``TFM_HAL_ITS_FLASH_DRIVER`` - Defines the identifier of the CMSIS Flash |
| 210 | ARM_DRIVER_FLASH object to use for ITS. It must have been allocated by the |
| 211 | platform and will be declared extern in the HAL header. |
| 212 | |
| 213 | - ``TFM_HAL_ITS_PROGRAM_UNIT`` - Defines the size of the ITS flash device's |
| 214 | physical program unit (the smallest unit of data that can be individually |
| 215 | programmed to flash). It must be equal to |
| 216 | ``TFM_HAL_ITS_FLASH_DRIVER.GetInfo()->program_unit``, but made available at |
| 217 | compile time so that filesystem structures can be statically sized. Valid |
| 218 | values are powers of two between 1 and the flash sector size, inclusive. |
| 219 | |
| 220 | The following C definitions in the HAL may optionally be defined by the platform |
| 221 | in the ``flash_layout.h`` header: |
| 222 | |
| 223 | - ``TFM_HAL_ITS_FLASH_AREA_ADDR`` - Defines the base address of the dedicated |
| 224 | flash area for ITS. |
| 225 | |
| 226 | - ``TFM_HAL_ITS_FLASH_AREA_SIZE`` - Defines the size of the dedicated flash area |
| 227 | for ITS in bytes. |
| 228 | |
| 229 | - ``TFM_HAL_ITS_SECTORS_PER_BLOCK`` - Defines the number of contiguous physical |
| 230 | flash erase sectors that form a logical erase block in the filesystem. The |
| 231 | typical value is ``1``, but it may be increased so that the maximum required |
| 232 | asset size will fit in one logical block. |
| 233 | |
| 234 | If any of the above definitions are not provided by the platform, then the |
| 235 | ``tfm_hal_its_fs_info()`` HAL API must be implemented instead. This function is |
| 236 | documented in ``tfm_hal_its.h``. |
| 237 | |
| 238 | The sectors reserved to be used for Internal Trusted Storage **must** be |
| 239 | contiguous. |
| 240 | |
| 241 | Internal Trusted Storage Service Optional Platform Definitions |
| 242 | ============================================================== |
| 243 | The following optional platform definitions may be defined in |
| 244 | ``flash_layout.h``: |
| 245 | |
| 246 | - ``ITS_RAM_FS_SIZE`` - Defines the size of the RAM FS buffer when using the |
| 247 | RAM FS emulated flash implementation. The buffer must be at least as large as |
| 248 | the area earmarked for the filesystem by the HAL. |
| 249 | - ``ITS_FLASH_NAND_BUF_SIZE`` - Defines the size of the write buffer when using |
| 250 | the NAND flash implementation. The buffer must be at least as large as a |
| 251 | logical filesystem block. |
| 252 | - ``ITS_MAX_BLOCK_DATA_COPY`` - Defines the buffer size used when copying data |
| 253 | between blocks, in bytes. If not provided, defaults to 256. Increasing this |
| 254 | value will increase the memory footprint of the service. |
| 255 | |
| 256 | More information about the ``flash_layout.h`` content, not ITS related, is |
| 257 | available in :doc:`platform readme </platform/ext/readme>` along with other |
| 258 | platform information. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 259 | |
Jamie Fox | 865778b | 2020-10-23 19:52:51 +0100 | [diff] [blame] | 260 | ITS Service Build Definitions |
| 261 | ============================= |
| 262 | The ITS service uses a set of C definitions to compile in/out certain features, |
| 263 | as well as to configure certain service parameters. When using the TF-M build |
| 264 | system, these definitions are controlled by build flags of the same name. The |
| 265 | ``config/config_default.cmake`` file sets the default values of those flags, but |
| 266 | they can be overwritten based on platform capabilities by setting them in |
| 267 | ``platform/ext/target/<TARGET_NAME>/config.cmake``. The list of ITS service |
| 268 | build definitions is: |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 269 | |
| 270 | - ``ITS_CREATE_FLASH_LAYOUT``- this flag indicates that it is required |
| 271 | to create an ITS flash layout. If this flag is set, ITS service will |
| 272 | generate an empty and valid ITS flash layout to store assets. It will |
| 273 | erase all data located in the assigned ITS memory area before generating |
| 274 | the ITS layout. This flag is required to be set if the ITS memory area |
| 275 | is located in a non-persistent memory. This flag can be set if the ITS |
| 276 | memory area is located in a persistent memory without a valid ITS flash |
| 277 | layout in it. That is the case when it is the first time in the device |
| 278 | life that the ITS service is executed. |
| 279 | - ``ITS_VALIDATE_METADATA_FROM_FLASH``- this flag allows to |
| 280 | enable/disable the validation mechanism to check the metadata store in flash |
| 281 | every time the flash data is read from flash. This validation is required |
| 282 | if the flash is not hardware protected against data corruption. |
Jamie Fox | f58bd22 | 2020-06-12 18:21:25 +0100 | [diff] [blame] | 283 | - ``ITS_RAM_FS``- setting this flag to ``ON`` enables the use of RAM instead of |
| 284 | the persistent storage device to store the FS in the Internal Trusted Storage |
| 285 | service. This flag is ``OFF`` by default. The ITS regression tests write/erase |
| 286 | storage multiple time, so enabling this flag can increase the life of flash |
| 287 | memory when testing. |
Chris Brand | c47d710 | 2020-02-20 11:12:18 -0800 | [diff] [blame] | 288 | If this flag is set to ``ON``, ITS_RAM_FS_SIZE must also be provided. This |
| 289 | specifies the size of the block of RAM to be used to simulate the flash. |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 290 | |
Soby Mathew | 728a20a | 2020-03-25 13:33:35 +0000 | [diff] [blame] | 291 | .. Note:: |
Jamie Fox | f58bd22 | 2020-06-12 18:21:25 +0100 | [diff] [blame] | 292 | If this flag is disabled when running the regression tests, then it is |
| 293 | recommended that the persistent storage area is erased before running the |
| 294 | tests to ensure that all tests can run to completion. The type of persistent |
| 295 | storage area is platform specific (eFlash, MRAM, etc.) and it is described |
| 296 | in corresponding flash_layout.h |
Soby Mathew | 728a20a | 2020-03-25 13:33:35 +0000 | [diff] [blame] | 297 | |
Jamie Fox | 865778b | 2020-10-23 19:52:51 +0100 | [diff] [blame] | 298 | - ``ITS_MAX_ASSET_SIZE`` - Defines the maximum asset size to be stored in the |
| 299 | ITS area. This size is used to define the temporary buffers used by ITS to |
| 300 | read/write the asset content from/to flash. The memory used by the temporary |
| 301 | buffers is allocated statically as ITS does not use dynamic memory allocation. |
| 302 | - ``ITS_NUM_ASSETS`` - Defines the maximum number of assets to be stored in the |
| 303 | ITS area. This number is used to dimension statically the filesystem metadata |
| 304 | tables in RAM (fast access) and flash (persistent storage). The memory used by |
| 305 | the filesystem metadata tables is allocated statically as ITS does not use |
| 306 | dynamic memory allocation. |
| 307 | - ``ITS_BUF_SIZE``- Defines the size of the partition's internal data transfer |
| 308 | buffer. If not provided, then ``ITS_MAX_ASSET_SIZE`` is used to allow asset |
| 309 | data to be copied between the client and the filesystem in one iteration. |
| 310 | Reducing the buffer size will decrease the RAM usage of the partition at the |
| 311 | expense of latency, as data will be copied in multiple iterations. *Note:* |
| 312 | when data is copied in multiple iterations, the atomicity property of the |
| 313 | filesystem is lost in the case of an asynchronous power failure. |
| 314 | |
TudorCretu | c0e4bee | 2019-07-23 11:38:21 +0100 | [diff] [blame] | 315 | -------------- |
| 316 | |
Jamie Fox | a7e73b7 | 2021-01-15 13:39:31 +0000 | [diff] [blame] | 317 | *Copyright (c) 2019-2021, Arm Limited. All rights reserved.* |
Chris Brand | 2603c29 | 2020-02-05 16:44:11 -0800 | [diff] [blame] | 318 | *Copyright (c) 2020, Cypress Semiconductor Corporation. All rights reserved.* |