17#ifdef HAVE_APP_STORAGE
19#include "app_storage.h"
25#define APP_STORAGE_ERASE_BLOCK_SIZE 256
27CONST app_storage_t app_storage_real
__attribute__((section(
".storage_section")));
28#define app_storage (*(volatile app_storage_t *) PIC(&app_storage_real))
33STATIC int32_t app_storage_is_initalized(
void)
35 int32_t status = APP_STORAGE_ERR_INVALID_ARGUMENT;
36 if (memcmp((
const void *) &app_storage.header.tag, APP_STORAGE_TAG, APP_STORAGE_TAG_LEN) == 0) {
37 status = APP_STORAGE_SUCCESS;
40 status = APP_STORAGE_ERR_INVALID_HEADER;
44 uint32_t crc = cx_crc32((
void *) &app_storage.header,
45 sizeof(app_storage.header) + app_storage.header.size);
46 if (crc != app_storage.crc) {
48 status = APP_STORAGE_ERR_CORRUPTED;
51 status = APP_STORAGE_SUCCESS;
58static inline void update_crc(
void)
60 uint32_t crc = cx_crc32((
void *) &app_storage.header,
61 sizeof(app_storage.header) + app_storage.header.size);
62 nvm_write((
void *) &app_storage.crc, &crc,
sizeof(app_storage.crc));
68static inline void system_header_reset(
void)
71 app_storage_header_t header = {0};
73 memcpy(&header.tag, (
void *) APP_STORAGE_TAG, APP_STORAGE_TAG_LEN);
75 header.struct_version = APP_STORAGE_HEADER_STRUCT_VERSION;
77 header.data_version = APP_STORAGE_INITIAL_APP_DATA_VERSION;
79 header.properties = ((HAVE_APP_STORAGE_PROP_SETTINGS << 0) | (HAVE_APP_STORAGE_PROP_DATA << 1));
80 nvm_write((
void *) &app_storage.header, &header,
sizeof(header));
97int32_t app_storage_init(
void)
99 int32_t status = app_storage_is_initalized();
101 case APP_STORAGE_ERR_INVALID_HEADER:
103 system_header_reset();
104 status = APP_STORAGE_SUCCESS;
106 case APP_STORAGE_ERR_CORRUPTED:
107 system_header_reset();
109 case APP_STORAGE_SUCCESS:
121void app_storage_reset(
void)
123 system_header_reset();
125 uint8_t erase_buf[APP_STORAGE_ERASE_BLOCK_SIZE] = {0};
127 bool need_update =
false;
129 if (APP_STORAGE_SIZE > APP_STORAGE_ERASE_BLOCK_SIZE) {
130 for (; offset < APP_STORAGE_SIZE - APP_STORAGE_ERASE_BLOCK_SIZE;
131 offset += APP_STORAGE_ERASE_BLOCK_SIZE) {
132 nvm_write((
void *) &app_storage.data[offset],
134 APP_STORAGE_ERASE_BLOCK_SIZE);
138 if (APP_STORAGE_SIZE > offset) {
140 (
void *) &app_storage.data[offset], (
void *) erase_buf, APP_STORAGE_SIZE - offset);
151uint32_t app_storage_get_size(
void)
153 return app_storage.header.size;
159uint32_t app_storage_get_data_version(
void)
161 return app_storage.header.data_version;
167uint16_t app_storage_get_properties(
void)
169 return app_storage.header.properties;
175void app_storage_increment_data_version(
void)
177 uint32_t data_version = app_storage.header.data_version;
179 if (data_version == 0) {
184 data_version = APP_STORAGE_INITIAL_APP_DATA_VERSION;
187 (
void *) &app_storage.header.data_version, (
void *) &data_version,
sizeof(data_version));
194void app_storage_set_data_version(uint32_t data_version)
197 (
void *) &app_storage.header.data_version, (
void *) &data_version,
sizeof(data_version));
204int32_t app_storage_write(
const void *buf, uint32_t nbyte, uint32_t offset)
208 return APP_STORAGE_ERR_INVALID_ARGUMENT;
211 return APP_STORAGE_ERR_INVALID_ARGUMENT;
213 uint32_t max_offset = 0;
214 if (__builtin_add_overflow(offset, nbyte, &max_offset)) {
215 return APP_STORAGE_ERR_INVALID_ARGUMENT;
219 if (max_offset > APP_STORAGE_SIZE) {
220 return APP_STORAGE_ERR_OVERFLOW;
224 nvm_write((
void *) &app_storage.data[offset], (
void *) buf, nbyte);
227 if (app_storage.header.size < max_offset) {
228 nvm_write((
void *) &app_storage.header.size, (
void *) &max_offset,
sizeof(max_offset));
238int32_t app_storage_read(
void *buf, uint32_t nbyte, uint32_t offset)
242 return APP_STORAGE_ERR_INVALID_ARGUMENT;
245 return APP_STORAGE_ERR_INVALID_ARGUMENT;
247 uint32_t max_offset = 0;
248 if (__builtin_add_overflow(offset, nbyte, &max_offset)) {
249 return APP_STORAGE_ERR_INVALID_ARGUMENT;
253 if (max_offset > app_storage.header.size) {
254 return APP_STORAGE_ERR_NO_DATA_AVAILABLE;
258 memcpy((
void *) buf, (
void *) &app_storage.data[offset], nbyte);
CRC (Cyclic Redundancy Check).
__attribute__((section("._nbgl_fonts_"))) const
return the non-unicode font corresponding to the given font ID