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