Skip to content

Commit 7c71640

Browse files
FWEO-1481 app_storage: Update init API to return a status
1 parent d1dc8f3 commit 7c71640

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

include/app_storage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
#define APP_STORAGE_HEADER_STRUCT_VERSION 1
3535

3636
///< Error codes
37+
#define APP_STORAGE_SUCCESS 0 ///< No error
3738
#define APP_STORAGE_ERR_INVALID_ARGUMENT -1 ///< Invalid argument
3839
#define APP_STORAGE_ERR_NO_DATA_AVAILABLE -2 ///< Address not available for reading
3940
#define APP_STORAGE_ERR_OVERFLOW -3 ///< Value too large to be stored
41+
#define APP_STORAGE_ERR_INVALID_HEADER -4 ///< Invalid storage header
42+
#define APP_STORAGE_ERR_CORRUPTED -5 ///< App storage is corrupted
4043

4144
/// Initial app data storage version
4245
#define APP_STORAGE_INITIAL_APP_DATA_VERSION 1

lib_standard_app/app_storage.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,29 @@ CONST app_storage_t app_storage_real __attribute__((section(".storage_section"))
3030
/**
3131
* @brief checks if the app storage struct is initialized and valid
3232
*/
33-
STATIC bool app_storage_is_initalized(void)
33+
STATIC int32_t app_storage_is_initalized(void)
3434
{
35-
bool is_initialized = false;
35+
int32_t status = APP_STORAGE_ERR_INVALID_ARGUMENT;
3636
if (memcmp((const void *) &app_storage.header.tag, APP_STORAGE_TAG, APP_STORAGE_TAG_LEN) == 0) {
37-
is_initialized = true;
37+
status = APP_STORAGE_SUCCESS;
3838
}
3939
else {
40+
status = APP_STORAGE_ERR_INVALID_HEADER;
4041
goto error;
4142
}
4243

4344
uint32_t crc = cx_crc32((void *) &app_storage.header,
4445
sizeof(app_storage.header) + app_storage.header.size);
4546
if (crc != app_storage.crc) {
4647
// Invalid CRC, force reset
47-
is_initialized = false;
48+
status = APP_STORAGE_ERR_CORRUPTED;
4849
}
4950
else {
50-
is_initialized = true;
51+
status = APP_STORAGE_SUCCESS;
5152
}
5253

5354
error:
54-
return is_initialized;
55+
return status;
5556
}
5657

5758
static inline void update_crc(void)
@@ -87,13 +88,31 @@ static inline void system_header_reset(void)
8788
* - sets initial size (0)
8889
* - sets struct and data versions (1)
8990
* - sets properties (from Makefile)
91+
*
92+
* @returns int32_t
93+
*
94+
* @retval APP_STORAGE_SUCCESS Application storage is successfully initialized.
95+
* @retval APP_STORAGE_ERR_CORRUPTED Error, application storage is corrupted.
9096
*/
91-
void app_storage_init(void)
97+
int32_t app_storage_init(void)
9298
{
93-
if (app_storage_is_initalized()) {
94-
return;
99+
int32_t status = app_storage_is_initalized();
100+
switch (status) {
101+
case APP_STORAGE_ERR_INVALID_HEADER:
102+
// Invalid tag or uninitialized storage, reset the HEADER
103+
system_header_reset();
104+
status = APP_STORAGE_SUCCESS;
105+
break;
106+
case APP_STORAGE_ERR_CORRUPTED:
107+
system_header_reset();
108+
break;
109+
case APP_STORAGE_SUCCESS:
110+
default:
111+
// Return status as-is
112+
break;
95113
}
96-
system_header_reset();
114+
115+
return status;
97116
}
98117

99118
/**

lib_standard_app/app_storage_internal.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ typedef struct app_storage_s {
2828
uint8_t data[APP_STORAGE_SIZE];
2929
} app_storage_t;
3030

31-
void app_storage_init(void);
31+
/**
32+
* @brief initializes the application storage.
33+
*
34+
* @returns int32_t Initialization status.
35+
*
36+
* @retval APP_STORAGE_SUCCESS Application storage is successfully initialized.
37+
* @retval APP_STORAGE_ERR_CORRUPTED Error, application storage is corrupted.
38+
*
39+
*/
40+
int32_t app_storage_init(void);
3241

3342
#endif /* HAVE_APP_STORAGE || HAVE_BOLOS */

unit-tests/app_storage/test_app_storage.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ _Static_assert(sizeof(app_storage_data_t) <= APP_STORAGE_SIZE,
7979

8080
// app_storage.h private
8181
extern app_storage_t app_storage_real;
82-
bool app_storage_is_initalized(void);
8382

8483
/* Local prototypes */
8584
static void test_write_read_from_empty(void **state __attribute__((unused)));
@@ -88,7 +87,7 @@ static void test_app_style_from_empty(void **state __attribute__((unused)));
8887
/* Functions */
8988
static int setup_from_empty(void **state)
9089
{
91-
app_storage_init();
90+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
9291
return 0;
9392
}
9493

@@ -101,23 +100,23 @@ static int teardown(void **state)
101100
static int setup_from_prepared(void **state)
102101
{
103102
/* Prepare storage */
104-
app_storage_init();
103+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
105104
test_write_read_from_empty(state);
106105

107106
/* Reinit storage */
108-
app_storage_init();
107+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
109108

110109
return 0;
111110
}
112111

113112
static int setup_from_prepared_app_style(void **state)
114113
{
115114
/* Prepare storage */
116-
app_storage_init();
115+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
117116
test_app_style_from_empty(state);
118117

119118
/* Reinit storage */
120-
app_storage_init();
119+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
121120
return 0;
122121
}
123122

@@ -135,50 +134,46 @@ static void test_getters_from_empty(void **state __attribute__((unused)))
135134
/* Test that corruption from empty storage is detected */
136135
static void test_corrupted_storage_from_empty(void **state __attribute__((unused)))
137136
{
138-
assert_true(app_storage_is_initalized());
139137
// --- Simulate corrupted header
140138
app_storage_header_t header = app_storage_real.header;
141139
header.data_version += 1;
142140
// Change header with no CRC update
143141
nvm_write((void *) &app_storage_real.header, &header, sizeof(header));
144142
// Ensure invalid CRC
145-
assert_false(app_storage_is_initalized());
143+
assert_int_equal(app_storage_init(), APP_STORAGE_ERR_CORRUPTED);
146144

147145
// --- Simulate corrupted data
148146
setup_from_empty(NULL);
149-
assert_true(app_storage_is_initalized());
150147
uint8_t buf[20] = {0};
151148
memset(buf, 0xAA, sizeof(buf));
152149
assert_int_equal(app_storage_write(buf, sizeof(buf), 0), sizeof(buf));
153150
// Change data with no CRC update
154151
buf[sizeof(buf) - 1] = 0xAB;
155152
nvm_write((void *) &app_storage_real.data, buf, sizeof(buf));
156153
// Ensure invalid CRC
157-
assert_false(app_storage_is_initalized());
154+
assert_int_equal(app_storage_init(), APP_STORAGE_ERR_CORRUPTED);
158155
}
159156

160157
/* Test that corruption from prepared storage is detected */
161158
static void test_corrupted_storage_from_prepared(void **state __attribute__((unused)))
162159
{
163-
assert_true(app_storage_is_initalized());
164160
// --- Simulate corrupted header
165161
app_storage_header_t header = app_storage_real.header;
166162
header.data_version += 1;
167163
// Change header with no CRC update
168164
nvm_write((void *) &app_storage_real.header, &header, sizeof(header));
169165
// Ensure invalid CRC
170-
assert_false(app_storage_is_initalized());
166+
assert_int_equal(app_storage_init(), APP_STORAGE_ERR_CORRUPTED);
171167

172168
// --- Simulate corrupted data
173169
setup_from_prepared(NULL);
174-
assert_true(app_storage_is_initalized());
175170
uint8_t data[INITIAL_SIZE + ADDITIONALL_SIZE] = {0};
176171
app_storage_read(data, INITIAL_SIZE + ADDITIONALL_SIZE, 0);
177172
// Change data with no CRC update
178173
data[INITIAL_SIZE + ADDITIONALL_SIZE - 1]++;
179174
nvm_write((void *) &app_storage_real.data, data, INITIAL_SIZE + ADDITIONALL_SIZE);
180175
// Ensure invalid CRC
181-
assert_false(app_storage_is_initalized());
176+
assert_int_equal(app_storage_init(), APP_STORAGE_ERR_CORRUPTED);
182177
}
183178

184179
/* Read error cases with initially empty storage */

0 commit comments

Comments
 (0)