Embedded SDK
Embedded SDK
app_storage.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * (c) 2024 Ledger SAS.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifdef HAVE_APP_STORAGE
18 #include <string.h>
19 #include "app_storage.h"
20 #include "os_nvm.h"
21 #include "os_pic.h"
22 
23 // TODO: Create new section for the linker script
24 /*app_storage app_storage_real __attribute__((section(".bss.app_storage")));*/
25 const app_storage_t N_app_storage_real;
26 
35 void app_storage_init(uint32_t data_version)
36 {
37  app_storage_header_t header = {0};
38 
39  memcpy(header.tag, (void *) "NVRA", 4);
40  // APP_STORAGE_DATA_STRUCT_VERSION and APP_STORAGE_PROPERTIES must be defined in
41  // app_storage_data.h
42  header.struct_version = APP_STORAGE_DATA_STRUCT_VERSION;
43  header.data_version = data_version;
44  header.properties = APP_STORAGE_PROPERTIES;
45  // TODO: Doing this lead to have app storage bigger than needed
46  header.size = sizeof(app_storage_data_t);
47  nvm_write((void *) &N_app_storage.header, &header, sizeof(header));
48 }
49 
53 uint32_t app_storage_get_size(void)
54 {
55  return N_app_storage.header.size;
56 }
57 
61 uint16_t app_storage_get_struct_version(void)
62 {
63  return N_app_storage.header.struct_version;
64 }
65 
69 uint32_t app_storage_get_data_version(void)
70 {
71  return N_app_storage.header.data_version;
72 }
73 
77 uint16_t app_storage_get_properties(void)
78 {
79  return N_app_storage.header.properties;
80 }
81 
85 bool app_storage_is_initalized(void)
86 {
87  if (memcmp((void *) N_app_storage.header.tag, "NVRA", 4)) {
88  return false;
89  }
90  if (N_app_storage.header.size == 0) {
91  return false;
92  }
93  return true;
94 }
95 
99 void app_storage_set_data_version(uint32_t data_version)
100 {
101  nvm_write((void *) &N_app_storage.header.data_version,
102  (void *) &data_version,
103  sizeof(N_app_storage.header.data_version));
104 }
105 
106 #endif // HAVE_APP_STORAGE
unsigned short uint16_t
Definition: usbd_conf.h:54