Skip to content

Commit 47c70f7

Browse files
FWEO-1481 app_storage: Update init API to return a status
1 parent 1a09699 commit 47c70f7

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
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 -4 ///< 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: 21 additions & 9 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,24 @@ 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 Erroc, 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+
if (status == APP_STORAGE_SUCCESS) {
101+
goto exit;
95102
}
103+
96104
system_header_reset();
105+
status = status == APP_STORAGE_ERR_INVALID_HEADER ? APP_STORAGE_SUCCESS : status;
106+
107+
exit:
108+
return status;
97109
}
98110

99111
/**

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 Erroc, 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: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ _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);
82+
int32_t app_storage_is_initalized(void);
8383

8484
/* Local prototypes */
8585
static void test_write_read_from_empty(void **state __attribute__((unused)));
@@ -88,7 +88,7 @@ static void test_app_style_from_empty(void **state __attribute__((unused)));
8888
/* Functions */
8989
static int setup_from_empty(void **state)
9090
{
91-
app_storage_init();
91+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
9292
return 0;
9393
}
9494

@@ -101,23 +101,23 @@ static int teardown(void **state)
101101
static int setup_from_prepared(void **state)
102102
{
103103
/* Prepare storage */
104-
app_storage_init();
104+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
105105
test_write_read_from_empty(state);
106106

107107
/* Reinit storage */
108-
app_storage_init();
108+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
109109

110110
return 0;
111111
}
112112

113113
static int setup_from_prepared_app_style(void **state)
114114
{
115115
/* Prepare storage */
116-
app_storage_init();
116+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
117117
test_app_style_from_empty(state);
118118

119119
/* Reinit storage */
120-
app_storage_init();
120+
assert_int_equal(app_storage_init(), APP_STORAGE_SUCCESS);
121121
return 0;
122122
}
123123

@@ -135,50 +135,46 @@ static void test_getters_from_empty(void **state __attribute__((unused)))
135135
/* Test that corruption from empty storage is detected */
136136
static void test_corrupted_storage_from_empty(void **state __attribute__((unused)))
137137
{
138-
assert_true(app_storage_is_initalized());
139138
// --- Simulate corrupted header
140139
app_storage_header_t header = app_storage_real.header;
141140
header.data_version += 1;
142141
// Change header with no CRC update
143142
nvm_write((void *) &app_storage_real.header, &header, sizeof(header));
144143
// Ensure invalid CRC
145-
assert_false(app_storage_is_initalized());
144+
assert_int_equal(app_storage_is_initalized(), APP_STORAGE_ERR_CORRUPTED);
146145

147146
// --- Simulate corrupted data
148147
setup_from_empty(NULL);
149-
assert_true(app_storage_is_initalized());
150148
uint8_t buf[20] = {0};
151149
memset(buf, 0xAA, sizeof(buf));
152150
assert_int_equal(app_storage_write(buf, sizeof(buf), 0), sizeof(buf));
153151
// Change data with no CRC update
154152
buf[sizeof(buf) - 1] = 0xAB;
155153
nvm_write((void *) &app_storage_real.data, buf, sizeof(buf));
156154
// Ensure invalid CRC
157-
assert_false(app_storage_is_initalized());
155+
assert_int_equal(app_storage_is_initalized(), APP_STORAGE_ERR_CORRUPTED);
158156
}
159157

160158
/* Test that corruption from prepared storage is detected */
161159
static void test_corrupted_storage_from_prepared(void **state __attribute__((unused)))
162160
{
163-
assert_true(app_storage_is_initalized());
164161
// --- Simulate corrupted header
165162
app_storage_header_t header = app_storage_real.header;
166163
header.data_version += 1;
167164
// Change header with no CRC update
168165
nvm_write((void *) &app_storage_real.header, &header, sizeof(header));
169166
// Ensure invalid CRC
170-
assert_false(app_storage_is_initalized());
167+
assert_int_equal(app_storage_is_initalized(), APP_STORAGE_ERR_CORRUPTED);
171168

172169
// --- Simulate corrupted data
173170
setup_from_prepared(NULL);
174-
assert_true(app_storage_is_initalized());
175171
uint8_t data[INITIAL_SIZE + ADDITIONALL_SIZE] = {0};
176172
app_storage_read(data, INITIAL_SIZE + ADDITIONALL_SIZE, 0);
177173
// Change data with no CRC update
178174
data[INITIAL_SIZE + ADDITIONALL_SIZE - 1]++;
179175
nvm_write((void *) &app_storage_real.data, data, INITIAL_SIZE + ADDITIONALL_SIZE);
180176
// Ensure invalid CRC
181-
assert_false(app_storage_is_initalized());
177+
assert_int_equal(app_storage_is_initalized(), APP_STORAGE_ERR_CORRUPTED);
182178
}
183179

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

0 commit comments

Comments
 (0)