22
22
#include " psa_prot_internal_storage.h"
23
23
#include " pits_impl.h"
24
24
#include " mbed_error.h"
25
+ #include " mbed_toolchain.h"
25
26
26
27
#ifdef __cplusplus
27
28
extern " C"
@@ -48,52 +49,101 @@ const uint8_t base64_coding_table[] = {
48
49
' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' +' , ' -'
49
50
};
50
51
51
-
52
+ /*
53
+ * \brief Get default KVStore instance for internal flesh storage
54
+ *
55
+ * \return valid pointer to KVStore
56
+ */
52
57
static KVStore *get_kvstore_instance (void )
53
58
{
54
59
KVMap &kv_map = KVMap::get_instance ();
55
60
56
- return kv_map.get_main_kv_instance (STR_EXPAND (MBED_CONF_STORAGE_DEFAULT_KV));
61
+ KVStore *kvstore = kv_map.get_internal_kv_instance (STR_EXPAND (MBED_CONF_STORAGE_DEFAULT_KV));
62
+ if (!kvstore) {
63
+ // Can only happen due to system misconfiguration.
64
+ // Thus considered as unrecoverable error for runtime.
65
+ error (" Failed getting kvstore instance\n " );
66
+ }
67
+ return kvstore;
57
68
}
58
69
59
- static void generate_fn (char *tdb_filename, uint32_t tdb_file_len, uint32_t uid, uint32_t pid)
70
+ /*
71
+ * \brief Convert KVStore stauts codes to PSA internal storage status codes
72
+ *
73
+ * \param[in] status - KVStore status code
74
+ * \return PSA internal storage status code
75
+ */
76
+ static psa_its_status_t convert_status (int status)
77
+ {
78
+ switch (status) {
79
+ case MBED_SUCCESS:
80
+ return PSA_ITS_SUCCESS;
81
+ case MBED_ERROR_WRITE_PROTECTED:
82
+ return PSA_ITS_ERROR_WRITE_ONCE;
83
+ case MBED_ERROR_MEDIA_FULL:
84
+ return PSA_ITS_ERROR_INSUFFICIENT_SPACE;
85
+ case MBED_ERROR_ITEM_NOT_FOUND:
86
+ return PSA_ITS_ERROR_KEY_NOT_FOUND;
87
+ default :
88
+ return PSA_ITS_ERROR_STORAGE_FAILURE;
89
+ }
90
+ }
91
+
92
+ /*
93
+ * \brief Logic shift right
94
+ *
95
+ * \note must operate on unsinged integers to prevent negative carry
96
+ * \param x[in] input number for shifting
97
+ * \param n[in] number of bits to shift right
98
+ * \return the result
99
+ */
100
+ MBED_FORCEINLINE uint32_t lsr (uint32_t x, uint32_t n)
101
+ {
102
+ return x >> n;
103
+ }
104
+
105
+ /*
106
+ * \breif Generate KVStore file name
107
+ *
108
+ * Generate KVStore file name by Base64 encoding PID and UID with a delimiter.
109
+ * Delimiter is required for determining between PID and UID.
110
+ *
111
+ * \param[out] tdb_filename - pointer to a buffer for the file name
112
+ * \param[in] tdb_filename_size - output buffer size
113
+ * \param[in] uid - PSA internal storage unique ID
114
+ * \param[in] pid - owner PSA partition ID
115
+ */
116
+ static void generate_fn (char *tdb_filename, uint32_t tdb_filename_size, uint32_t uid, int32_t pid)
60
117
{
61
118
MBED_ASSERT (tdb_filename != NULL );
62
- MBED_ASSERT (tdb_file_len > = PSA_ITS_FILENAME_MAX_LEN);
119
+ MBED_ASSERT (tdb_filename_size = = PSA_ITS_FILENAME_MAX_LEN);
63
120
64
121
uint8_t filename_idx = 0 ;
65
- uint32_t tmp_uid = uid;
66
- uint32_t tmp_pid = pid;
122
+ uint32_t unsigned_pid = (uint32_t )pid; // binary only representation for bitwise operations
67
123
68
- // Iterate on UID ; each time convert 6 bits of UID into a character; first iteration must be done
124
+ // Iterate on PID ; each time convert 6 bits of PID into a character; first iteration must be done
69
125
do {
70
- MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
71
- tdb_filename[filename_idx++] = base64_coding_table[tmp_uid & 0x3F ];
72
- tmp_uid = tmp_uid >> 6 ;
73
- } while (tmp_uid != 0 );
126
+ tdb_filename[filename_idx++] = base64_coding_table[unsigned_pid & 0x3F ];
127
+ unsigned_pid = lsr (unsigned_pid, 6 );
128
+ } while (unsigned_pid != 0 );
74
129
75
130
// Write delimiter
76
- MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
77
131
tdb_filename[filename_idx++] = ' #' ;
78
132
79
- // Iterate on PID ; each time convert 6 bits of PID into a character; first iteration must be done
133
+ // Iterate on UID ; each time convert 6 bits of UID into a character; first iteration must be done
80
134
do {
81
- MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
82
- tdb_filename[filename_idx++] = base64_coding_table[tmp_pid & 0x3F ];
83
- tmp_pid = tmp_pid >> 6 ;
84
- } while (tmp_pid != 0 );
135
+ tdb_filename[filename_idx++] = base64_coding_table[uid & 0x3F ];
136
+ uid = lsr (uid, 6 );
137
+ } while (uid != 0 );
85
138
139
+ tdb_filename[filename_idx++] = ' \0 ' ;
86
140
MBED_ASSERT (filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
87
- tdb_filename[filename_idx] = ' \0 ' ;
88
141
}
89
142
90
-
91
- psa_its_status_t psa_its_set_impl (uint32_t pid, uint32_t uid, uint32_t data_length, const void *p_data, psa_its_create_flags_t create_flags)
143
+ psa_its_status_t psa_its_set_impl (int32_t pid, uint32_t uid, uint32_t data_length, const void *p_data, psa_its_create_flags_t create_flags)
92
144
{
93
145
KVStore *kvstore = get_kvstore_instance ();
94
- if (!kvstore) {
95
- error (" psa_its_set_impl() - Failed getting kvstore instance\n " );
96
- }
146
+ MBED_ASSERT (kvstore);
97
147
98
148
if ((create_flags != 0 ) && (create_flags != PSA_ITS_WRITE_ONCE_FLAG)) {
99
149
return PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED;
@@ -108,51 +158,24 @@ psa_its_status_t psa_its_set_impl(uint32_t pid, uint32_t uid, uint32_t data_leng
108
158
kv_create_flags = KVStore::WRITE_ONCE_FLAG;
109
159
}
110
160
111
- int kvstore_status = kvstore->set (kv_key, p_data, data_length, kv_create_flags);
112
-
113
- psa_its_status_t status = PSA_ITS_SUCCESS;
114
- if (kvstore_status != MBED_SUCCESS) {
115
- switch (kvstore_status) {
116
- case MBED_ERROR_WRITE_PROTECTED:
117
- status = PSA_ITS_ERROR_WRITE_ONCE;
118
- break ;
119
- case MBED_ERROR_MEDIA_FULL:
120
- status = PSA_ITS_ERROR_INSUFFICIENT_SPACE;
121
- break ;
122
- default :
123
- status = PSA_ITS_ERROR_STORAGE_FAILURE;
124
- }
125
- }
161
+ int status = kvstore->set (kv_key, p_data, data_length, kv_create_flags);
126
162
127
- return status;
163
+ return convert_status ( status) ;
128
164
}
129
165
130
- psa_its_status_t psa_its_get_impl (uint32_t pid, uint32_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
166
+ psa_its_status_t psa_its_get_impl (int32_t pid, uint32_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
131
167
{
132
168
KVStore *kvstore = get_kvstore_instance ();
133
- if (!kvstore) {
134
- error (" psa_its_get_impl() - Failed getting kvstore instance\n " );
135
- }
169
+ MBED_ASSERT (kvstore);
136
170
137
171
// Generate KVStore key
138
172
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {' \0 ' };
139
173
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
140
174
141
175
KVStore::info_t kv_info;
142
- int kvstore_status = kvstore->get_info (kv_key, &kv_info);
143
-
144
- psa_its_status_t status = PSA_ITS_SUCCESS;
145
- if (kvstore_status != MBED_SUCCESS) {
146
- switch (kvstore_status) {
147
- case MBED_ERROR_ITEM_NOT_FOUND:
148
- status = PSA_ITS_ERROR_KEY_NOT_FOUND;
149
- break ;
150
- default :
151
- status = PSA_ITS_ERROR_STORAGE_FAILURE;
152
- }
153
- }
176
+ int status = kvstore->get_info (kv_key, &kv_info);
154
177
155
- if (kvstore_status == MBED_SUCCESS) {
178
+ if (status == MBED_SUCCESS) {
156
179
if (data_offset > kv_info.size ) {
157
180
return PSA_ITS_ERROR_OFFSET_INVALID;
158
181
}
@@ -167,90 +190,53 @@ psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offs
167
190
}
168
191
169
192
size_t actual_size = 0 ;
170
- kvstore_status = kvstore->get (kv_key, p_data, data_length, &actual_size, data_offset);
193
+ status = kvstore->get (kv_key, p_data, data_length, &actual_size, data_offset);
171
194
172
- if (kvstore_status == MBED_SUCCESS) {
195
+ if (status == MBED_SUCCESS) {
173
196
if (actual_size < data_length) {
174
197
status = PSA_ITS_ERROR_INCORRECT_SIZE;
175
198
}
176
- } else {
177
- switch (kvstore_status) {
178
- case MBED_ERROR_ITEM_NOT_FOUND:
179
- status = PSA_ITS_ERROR_KEY_NOT_FOUND;
180
- break ;
181
- default :
182
- status = PSA_ITS_ERROR_STORAGE_FAILURE;
183
- }
184
199
}
185
200
}
186
201
187
- return status;
202
+ return convert_status ( status) ;
188
203
}
189
204
190
- psa_its_status_t psa_its_get_info_impl (uint32_t pid, uint32_t uid, struct psa_its_info_t *p_info)
205
+ psa_its_status_t psa_its_get_info_impl (int32_t pid, uint32_t uid, struct psa_its_info_t *p_info)
191
206
{
192
207
KVStore *kvstore = get_kvstore_instance ();
193
- if (!kvstore) {
194
- error (" psa_its_get_info_impl() - Failed getting kvstore instance\n " );
195
- }
208
+ MBED_ASSERT (kvstore);
196
209
197
210
// Generate KVStore key
198
211
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {' \0 ' };
199
212
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
200
213
201
214
KVStore::info_t kv_info;
202
- int kvstore_status = kvstore->get_info (kv_key, &kv_info);
203
-
204
- psa_its_status_t status = PSA_ITS_SUCCESS;
205
- if (kvstore_status != MBED_SUCCESS) {
206
- switch (kvstore_status) {
207
- case MBED_ERROR_ITEM_NOT_FOUND:
208
- status = PSA_ITS_ERROR_KEY_NOT_FOUND;
209
- break ;
210
- default :
211
- status = PSA_ITS_ERROR_STORAGE_FAILURE;
212
- }
213
- }
215
+ int status = kvstore->get_info (kv_key, &kv_info);
214
216
215
- if (kvstore_status == MBED_SUCCESS) {
217
+ if (status == MBED_SUCCESS) {
216
218
p_info->flags = 0 ;
217
219
if (kv_info.flags & KVStore::WRITE_ONCE_FLAG) {
218
220
p_info->flags |= PSA_ITS_WRITE_ONCE_FLAG;
219
221
}
220
222
p_info->size = (uint32_t )(kv_info.size ); // kv_info.size is of type size_t
221
223
}
222
224
223
- return status;
225
+ return convert_status ( status) ;
224
226
}
225
227
226
- psa_its_status_t psa_its_remove_impl (uint32_t pid, uint32_t uid)
228
+ psa_its_status_t psa_its_remove_impl (int32_t pid, uint32_t uid)
227
229
{
228
230
KVStore *kvstore = get_kvstore_instance ();
229
- if (!kvstore) {
230
- error (" psa_its_remove_impl() - Failed getting kvstore instance\n " );
231
- }
231
+ MBED_ASSERT (kvstore);
232
232
233
233
// Generate KVStore key
234
234
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {' \0 ' };
235
235
generate_fn (kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
236
236
237
- int kvstore_status = kvstore->remove (kv_key);
238
-
239
- psa_its_status_t status = PSA_ITS_SUCCESS;
240
- if (kvstore_status != MBED_SUCCESS) {
241
- switch (kvstore_status) {
242
- case MBED_ERROR_WRITE_PROTECTED:
243
- status = PSA_ITS_ERROR_WRITE_ONCE;
244
- break ;
245
- case MBED_ERROR_ITEM_NOT_FOUND:
246
- status = PSA_ITS_ERROR_KEY_NOT_FOUND;
247
- break ;
248
- default :
249
- status = PSA_ITS_ERROR_STORAGE_FAILURE;
250
- }
251
- }
237
+ int status = kvstore->remove (kv_key);
252
238
253
- return status;
239
+ return convert_status ( status) ;
254
240
}
255
241
256
242
#ifdef __cplusplus
0 commit comments