Skip to content

Commit 0b1b8e0

Browse files
author
Cruz Monrreal
authored
Merge pull request #8908 from kfnta/alzix_fix_its
Fix PSA internal storage configuration
2 parents 6edc81d + 44ec2aa commit 0b1b8e0

File tree

2 files changed

+95
-109
lines changed

2 files changed

+95
-109
lines changed

components/TARGET_PSA/services/psa_prot_internal_storage/COMPONENT_PSA_SRV_IMPL/pits_impl.cpp

Lines changed: 91 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "psa_prot_internal_storage.h"
2323
#include "pits_impl.h"
2424
#include "mbed_error.h"
25+
#include "mbed_toolchain.h"
2526

2627
#ifdef __cplusplus
2728
extern "C"
@@ -48,52 +49,101 @@ const uint8_t base64_coding_table[] = {
4849
'4', '5', '6', '7', '8', '9', '+', '-'
4950
};
5051

51-
52+
/*
53+
* \brief Get default KVStore instance for internal flesh storage
54+
*
55+
* \return valid pointer to KVStore
56+
*/
5257
static KVStore *get_kvstore_instance(void)
5358
{
5459
KVMap &kv_map = KVMap::get_instance();
5560

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;
5768
}
5869

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)
60117
{
61118
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);
63120

64121
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
67123

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
69125
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);
74129

75130
// Write delimiter
76-
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
77131
tdb_filename[filename_idx++] = '#';
78132

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
80134
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);
85138

139+
tdb_filename[filename_idx++] = '\0';
86140
MBED_ASSERT(filename_idx <= PSA_ITS_FILENAME_MAX_LEN);
87-
tdb_filename[filename_idx] = '\0';
88141
}
89142

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)
92144
{
93145
KVStore *kvstore = get_kvstore_instance();
94-
if (!kvstore) {
95-
error("psa_its_set_impl() - Failed getting kvstore instance\n");
96-
}
146+
MBED_ASSERT(kvstore);
97147

98148
if ((create_flags != 0) && (create_flags != PSA_ITS_WRITE_ONCE_FLAG)) {
99149
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
108158
kv_create_flags = KVStore::WRITE_ONCE_FLAG;
109159
}
110160

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);
126162

127-
return status;
163+
return convert_status(status);
128164
}
129165

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)
131167
{
132168
KVStore *kvstore = get_kvstore_instance();
133-
if (!kvstore) {
134-
error("psa_its_get_impl() - Failed getting kvstore instance\n");
135-
}
169+
MBED_ASSERT(kvstore);
136170

137171
// Generate KVStore key
138172
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {'\0'};
139173
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
140174

141175
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);
154177

155-
if (kvstore_status == MBED_SUCCESS) {
178+
if (status == MBED_SUCCESS) {
156179
if (data_offset > kv_info.size) {
157180
return PSA_ITS_ERROR_OFFSET_INVALID;
158181
}
@@ -167,90 +190,53 @@ psa_its_status_t psa_its_get_impl(uint32_t pid, uint32_t uid, uint32_t data_offs
167190
}
168191

169192
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);
171194

172-
if (kvstore_status == MBED_SUCCESS) {
195+
if (status == MBED_SUCCESS) {
173196
if (actual_size < data_length) {
174197
status = PSA_ITS_ERROR_INCORRECT_SIZE;
175198
}
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-
}
184199
}
185200
}
186201

187-
return status;
202+
return convert_status(status);
188203
}
189204

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)
191206
{
192207
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);
196209

197210
// Generate KVStore key
198211
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {'\0'};
199212
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
200213

201214
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);
214216

215-
if (kvstore_status == MBED_SUCCESS) {
217+
if (status == MBED_SUCCESS) {
216218
p_info->flags = 0;
217219
if (kv_info.flags & KVStore::WRITE_ONCE_FLAG) {
218220
p_info->flags |= PSA_ITS_WRITE_ONCE_FLAG;
219221
}
220222
p_info->size = (uint32_t)(kv_info.size); // kv_info.size is of type size_t
221223
}
222224

223-
return status;
225+
return convert_status(status);
224226
}
225227

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)
227229
{
228230
KVStore *kvstore = get_kvstore_instance();
229-
if (!kvstore) {
230-
error("psa_its_remove_impl() - Failed getting kvstore instance\n");
231-
}
231+
MBED_ASSERT(kvstore);
232232

233233
// Generate KVStore key
234234
char kv_key[PSA_ITS_FILENAME_MAX_LEN] = {'\0'};
235235
generate_fn(kv_key, PSA_ITS_FILENAME_MAX_LEN, uid, pid);
236236

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);
252238

253-
return status;
239+
return convert_status(status);
254240
}
255241

256242
#ifdef __cplusplus

components/TARGET_PSA/services/psa_prot_internal_storage/COMPONENT_PSA_SRV_IMPL/pits_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ extern "C"
2828

2929
#define PITS_DATA_PTR_AT_OFFSET(ptr, offset) ((void *)(((uintptr_t)ptr) + ((uintptr_t)offset)))
3030

31-
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);
32-
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);
33-
psa_its_status_t psa_its_get_info_impl(uint32_t pid, uint32_t uid, struct psa_its_info_t *p_info);
34-
psa_its_status_t psa_its_remove_impl(uint32_t pid, uint32_t uid);
31+
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);
32+
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);
33+
psa_its_status_t psa_its_get_info_impl(int32_t pid, uint32_t uid, struct psa_its_info_t *p_info);
34+
psa_its_status_t psa_its_remove_impl(int32_t pid, uint32_t uid);
3535

3636
#ifdef __cplusplus
3737
}

0 commit comments

Comments
 (0)