1 /*
2  * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef __TFM_INTERNAL_TRUSTED_STORAGE_H__
9 #define __TFM_INTERNAL_TRUSTED_STORAGE_H__
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 
15 #include "psa/error.h"
16 #include "psa/storage_common.h"
17 
18 #include "flash_fs/its_flash_fs.h"
19 #include "its_utils.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26  * \brief Initializes the internal trusted storage system.
27  *
28  * \return A status indicating the success/failure of the operation as specified
29  *         in \ref psa_status_t
30  *
31  * \retval PSA_SUCCESS                The operation completed successfully
32  * \retval PSA_ERROR_STORAGE_FAILURE  The operation failed because the storage
33  *                                    system initialization has failed (fatal
34  *                                    error)
35  * \retval PSA_ERROR_GENERIC_ERROR    The operation failed because of an
36  *                                    unspecified internal failure
37  */
38 psa_status_t tfm_its_init(void);
39 
40 /**
41  * \brief Create a new, or modify an existing, uid/value pair
42  *
43  * Stores data in the internal storage.
44  *
45  * \param[in] client_id     Identifier of the asset's owner (client)
46  * \param[in] uid           The identifier for the data
47  * \param[in] data_length   The size in bytes of the data in `p_data`
48  * \param[in] create_flags  The flags that the data will be stored with
49  *
50  * \return A status indicating the success/failure of the operation
51  *
52  * \retval PSA_SUCCESS                     The operation completed successfully
53  * \retval PSA_ERROR_NOT_SUPPORTED         The operation failed because one or
54  *                                         more of the flags provided in
55  *                                         `create_flags` is not supported or is
56  *                                         not valid
57  * \retval PSA_ERROR_INSUFFICIENT_STORAGE  The operation failed because there
58  *                                         was insufficient space on the
59  *                                         storage medium
60  * \retval PSA_ERROR_STORAGE_FAILURE       The operation failed because the
61  *                                         physical storage has failed (Fatal
62  *                                         error)
63  * \retval PSA_ERROR_INVALID_ARGUMENT      The operation failed because one
64  *                                         of the provided pointers (`p_data`)
65  *                                         is invalid, for example is `NULL` or
66  *                                         references memory the caller cannot
67  *                                         access
68  */
69 psa_status_t tfm_its_set(int32_t client_id,
70                          psa_storage_uid_t uid,
71                          size_t data_length,
72                          psa_storage_create_flags_t create_flags);
73 
74 /**
75  * \brief Retrieve data associated with a provided UID
76  *
77  * Retrieves up to `data_size` bytes of the data associated with `uid`, starting
78  * at `data_offset` bytes from the beginning of the data. Upon successful
79  * completion, the data will be placed in the `p_data` buffer, which must be at
80  * least `data_size` bytes in size. The length of the data returned will be in
81  * `p_data_length`. If `data_size` is 0, the contents of `p_data_length` will
82  * be set to zero.
83  *
84  * \param[in]  client_id      Identifier of the asset's owner (client)
85  * \param[in]  uid            The uid value
86  * \param[in]  data_offset    The starting offset of the data requested
87  * \param[in]  data_size      The amount of data requested
88  * \param[out] p_data_length  On success, this will contain size of the data
89  *                            placed in `p_data`.
90  *
91  * \return A status indicating the success/failure of the operation
92  *
93  * \retval PSA_SUCCESS                 The operation completed successfully
94  * \retval PSA_ERROR_DOES_NOT_EXIST    The operation failed because the
95  *                                     provided `uid` value was not found in
96  *                                     the storage
97  * \retval PSA_ERROR_STORAGE_FAILURE   The operation failed because the
98  *                                     physical storage has failed (Fatal
99  *                                     error)
100  * \retval PSA_ERROR_INVALID_ARGUMENT  The operation failed because one of the
101  *                                     provided arguments (`p_data`,
102  *                                     `p_data_length`) is invalid, for example
103  *                                     is `NULL` or references memory the
104  *                                     caller cannot access. In addition, this
105  *                                     can also happen if `data_offset` is
106  *                                     larger than the size of the data
107  *                                     associated with `uid`.
108  */
109 psa_status_t tfm_its_get(int32_t client_id,
110                          psa_storage_uid_t uid,
111                          size_t data_offset,
112                          size_t data_size,
113                          size_t *p_data_length);
114 
115 /**
116  * \brief Retrieve the metadata about the provided uid
117  *
118  * Retrieves the metadata stored for a given `uid` as a `psa_storage_info_t`
119  * structure.
120  *
121  * \param[in]  client_id  Identifier of the asset's owner (client)
122  * \param[in]  uid        The `uid` value
123  * \param[out] p_info     A pointer to the `psa_storage_info_t` struct that will
124  *                        be populated with the metadata
125  *
126  * \return A status indicating the success/failure of the operation
127  *
128  * \retval PSA_SUCCESS                 The operation completed successfully
129  * \retval PSA_ERROR_DOES_NOT_EXIST    The operation failed because the provided
130  *                                     uid value was not found in the storage
131  * \retval PSA_ERROR_STORAGE_FAILURE   The operation failed because the physical
132  *                                     storage has failed (Fatal error)
133  * \retval PSA_ERROR_INVALID_ARGUMENT  The operation failed because one of the
134  *                                     provided pointers(`p_info`)
135  *                                     is invalid, for example is `NULL` or
136  *                                     references memory the caller cannot
137  *                                     access
138  */
139 psa_status_t tfm_its_get_info(int32_t client_id, psa_storage_uid_t uid,
140                               struct psa_storage_info_t *p_info);
141 
142 /**
143  * \brief Remove the provided uid and its associated data from the storage
144  *
145  * Deletes the data from internal storage.
146  *
147  * \param[in] client_id  Identifier of the asset's owner (client)
148  * \param[in] uid        The `uid` value
149  *
150  * \return A status indicating the success/failure of the operation
151  *
152  * \retval PSA_SUCCESS                 The operation completed successfully
153  * \retval PSA_ERROR_INVALID_ARGUMENT  The operation failed because one or more
154  *                                     of the given arguments were invalid (null
155  *                                     pointer, wrong flags and so on)
156  * \retval PSA_ERROR_DOES_NOT_EXIST    The operation failed because the provided
157  *                                     uid value was not found in the storage
158  * \retval PSA_ERROR_NOT_PERMITTED     The operation failed because the provided
159  *                                     uid value was created with
160  *                                     PSA_STORAGE_FLAG_WRITE_ONCE
161  * \retval PSA_ERROR_STORAGE_FAILURE   The operation failed because the physical
162  *                                     storage has failed (Fatal error)
163  */
164 psa_status_t tfm_its_remove(int32_t client_id, psa_storage_uid_t uid);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* __TFM_INTERNAL_TRUSTED_STORAGE_H__ */
171