• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: SOC KEY VALUE STORAGE IMPLEMENTATION
15  */
16 
17 #include "nv.h"
18 #include "nv_storage.h"
19 #include "nv_store.h"
20 #include "nv_reset.h"
21 #include "securec.h"
22 #include "nv_porting.h"
23 #include "common_def.h"
24 #include "nv_config.h"
25 #include "systick.h"
26 #include "nv_async_store.h"
27 #include "nv_update.h"
28 #include "nv_notify.h"
29 #include "nv_task_adapt.h"
30 #include "nv_storage_handle.h"
31 
uapi_nv_init(void)32 void uapi_nv_init(void)
33 {
34     (void)nv_direct_ctrl_init();
35 }
36 
uapi_nv_write_with_attr(uint16_t key,const uint8_t * kvalue,uint16_t kvalue_length,nv_key_attr_t * attr,nv_storage_completed_callback func)37 errcode_t uapi_nv_write_with_attr(uint16_t key, const uint8_t *kvalue, uint16_t kvalue_length, nv_key_attr_t *attr,
38                                   nv_storage_completed_callback func)
39 {
40     errcode_t ret;
41 
42     if (kvalue == NULL || kvalue_length == 0) {
43         return ERRCODE_NV_INVALID_PARAMS;
44     }
45 
46 #if (CONFIG_NV_SUPPORT_ENCRYPT != NV_YES)
47     if ((attr != NULL && attr->encrypted == true)) {
48         nv_log_err("[NV] nv_direct_write_with_attr: encryption not support!\r\n");
49         return ERRCODE_NV_ILLEGAL_OPERATION;
50     }
51 #endif
52 
53 #ifdef KV_DEBUG
54     uint64_t start_time = uapi_systick_get_ms();
55 #endif
56 
57 #if (CONFIG_NV_SUPPORT_ASYNCHRONOUS_STORE == NV_YES)
58     ret = nv_async_write_with_attr(key, kvalue, kvalue_length, attr, func);
59 #else
60     ret = nv_direct_write_with_attr(key, kvalue, kvalue_length, attr, func);
61 #endif
62 
63 #ifdef KV_DEBUG
64     uint64_t end_time = uapi_systick_get_ms();
65     if (ret != ERRCODE_SUCC) {
66         nv_log_err("[NV] write key fail! key_id= 0x%x, ret 0x%x\r\n", key, ret);
67     }
68 
69     nv_log_debug("[NV] write key (key_id = 0x%x len = %d) ret = %d. takes %lld ms\r\n",
70         key, kvalue_length, ret, (end_time - start_time));
71     unused(start_time);
72     unused(end_time);
73 #endif
74     return ret;
75 }
76 
uapi_nv_write(uint16_t key,const uint8_t * kvalue,uint16_t kvalue_length)77 errcode_t uapi_nv_write(uint16_t key, const uint8_t *kvalue, uint16_t kvalue_length)
78 {
79     return uapi_nv_write_with_attr(key, kvalue, kvalue_length, NULL, NULL);
80 }
81 
uapi_nv_write_force(uint16_t key,const uint8_t * kvalue,uint16_t kvalue_length)82 errcode_t uapi_nv_write_force(uint16_t key, const uint8_t *kvalue, uint16_t kvalue_length)
83 {
84     if (kvalue == NULL || kvalue_length == 0) {
85         return ERRCODE_NV_INVALID_PARAMS;
86     }
87 #ifdef CONFIG_NV_SUPPORT_WRITE_FORCE
88     return nv_direct_write_force(key, kvalue, kvalue_length);
89 #endif
90     unused(key);
91     return ERRCODE_NOT_SUPPORT;
92 }
93 
uapi_nv_update_key_attr(uint16_t key,nv_key_attr_t * attr,nv_storage_completed_callback func)94 errcode_t uapi_nv_update_key_attr(uint16_t key, nv_key_attr_t *attr, nv_storage_completed_callback func)
95 {
96     if (attr == NULL) {
97         return ERRCODE_NV_INVALID_PARAMS;
98     }
99 #ifdef CONFIG_NV_SUPPORT_UPDATE_ATTR
100     return nv_direct_update_key_attr(key, attr, func);
101 #endif
102     unused(key);
103     unused(func);
104     return ERRCODE_NOT_SUPPORT;
105 }
106 
uapi_nv_get_key_attr(uint16_t key,uint16_t * length,nv_key_attr_t * attr)107 errcode_t uapi_nv_get_key_attr(uint16_t key, uint16_t *length, nv_key_attr_t *attr)
108 {
109     if (attr == NULL) {
110         return ERRCODE_NV_INVALID_PARAMS;
111     }
112 
113     return nv_direct_get_key_attr(key, length, attr);
114 }
115 
uapi_nv_read_with_attr(uint16_t key,uint16_t kvalue_max_length,uint16_t * kvalue_length,uint8_t * kvalue,nv_key_attr_t * attr)116 errcode_t uapi_nv_read_with_attr(uint16_t key, uint16_t kvalue_max_length, uint16_t *kvalue_length, uint8_t *kvalue,
117                                  nv_key_attr_t *attr)
118 {
119     errcode_t ret;
120     if (kvalue_length == NULL || kvalue == NULL || attr == NULL) {
121         return ERRCODE_NV_INVALID_PARAMS;
122     }
123 
124 #ifdef KV_DEBUG
125     uint64_t start_time = uapi_systick_get_ms();
126 #endif
127 
128 #if (CONFIG_NV_SUPPORT_ASYNCHRONOUS_STORE == NV_YES)
129     ret = nv_async_read_with_attr(key, kvalue_max_length, kvalue_length, kvalue, attr);
130 #else
131     ret = nv_direct_get_key_data(key, kvalue_max_length, kvalue_length, kvalue, attr);
132 #endif
133 
134 #ifdef KV_DEBUG
135     uint64_t end_time = uapi_systick_get_ms();
136     if (ret != ERRCODE_SUCC) {
137         nv_log_err("[NV] read key fail! key_id= 0x%x, ret 0x%x\r\n", key, ret);
138     }
139     nv_log_debug("[NV] read key (key_id = 0x%x len = %d) ret = 0x%x. takes %lld ms\r\n",
140         key, *kvalue_length, ret, (end_time - start_time));
141     unused(start_time);
142     unused(end_time);
143 #endif
144     return ret;
145 }
146 
uapi_nv_read(uint16_t key,uint16_t kvalue_max_length,uint16_t * kvalue_length,uint8_t * kvalue)147 errcode_t uapi_nv_read(uint16_t key, uint16_t kvalue_max_length, uint16_t *kvalue_length, uint8_t *kvalue)
148 {
149     nv_key_attr_t attr;
150     return uapi_nv_read_with_attr(key, kvalue_max_length, kvalue_length, kvalue, &attr);
151 }
152 
uapi_nv_delete_key(uint16_t key)153 errcode_t uapi_nv_delete_key(uint16_t key)
154 {
155 #ifdef CONFIG_NV_SUPPORT_DELETE_KEY
156     return nv_direct_erase(key);
157 #else
158     unused(key);
159     return ERRCODE_NOT_SUPPORT;
160 #endif
161 }
162 
uapi_nv_is_stored(uint16_t key,uint16_t kvalue_length,const uint8_t * kvalue)163 bool uapi_nv_is_stored(uint16_t key, uint16_t kvalue_length, const uint8_t *kvalue)
164 {
165     if (kvalue_length == 0 || kvalue == NULL) {
166         return false;
167     }
168 
169     errcode_t ret = nv_direct_stored(key, kvalue_length, kvalue);
170     if (ret == ERRCODE_SUCC) {
171         return true;
172     } else {
173         return false;
174     }
175 }
176 
uapi_nv_get_store_status(nv_store_status_t * status)177 errcode_t uapi_nv_get_store_status(nv_store_status_t *status)
178 {
179     if (status == NULL) {
180         return ERRCODE_NV_INVALID_PARAMS;
181     }
182 
183 #ifdef KV_DEBUG
184     uint64_t start_time = uapi_systick_get_ms();
185 #endif
186     errcode_t ret = nv_direct_get_store_status(status);
187 #ifdef KV_DEBUG
188     uint64_t end_time = uapi_systick_get_ms();
189     nv_log_debug("[NV] get store status take %lld ms\r\n", (end_time - start_time));
190     unused(start_time);
191     unused(end_time);
192 #endif
193     return ret;
194 }
195 
uapi_nv_backup(const nv_backup_mode_t * backup_mode)196 errcode_t uapi_nv_backup(const nv_backup_mode_t *backup_mode)
197 {
198 #if (CONFIG_NV_SUPPORT_BACKUP_RESTORE == NV_YES)
199     if (backup_mode == NULL) {
200         return ERRCODE_NV_INVALID_PARAMS;
201     }
202 
203     return nv_direct_backup_keys(backup_mode);
204 #else
205     unused(backup_mode);
206     return ERRCODE_NOT_SUPPORT;
207 #endif
208 }
209 
uapi_nv_set_restore_mode_all(void)210 errcode_t uapi_nv_set_restore_mode_all(void)
211 {
212 #if (CONFIG_NV_SUPPORT_BACKUP_RESTORE == NV_YES)
213     return nv_direct_set_restore_flag_all();
214 #else
215     return ERRCODE_NOT_SUPPORT;
216 #endif
217 }
218 
uapi_nv_set_restore_mode_partitial(const nv_restore_mode_t * restore_mode)219 errcode_t uapi_nv_set_restore_mode_partitial(const nv_restore_mode_t *restore_mode)
220 {
221 #if (CONFIG_NV_SUPPORT_BACKUP_RESTORE == NV_YES)
222     return nv_direct_set_restore_flag_partitial(restore_mode);
223 #else
224     unused(restore_mode);
225     return ERRCODE_NOT_SUPPORT;
226 #endif
227 }
228 
uapi_nv_flush(void)229 errcode_t uapi_nv_flush(void)
230 {
231 #if (CONFIG_NV_SUPPORT_ASYNCHRONOUS_STORE == NV_YES)
232     return nv_async_flush();
233 #else
234     return ERRCODE_NOT_SUPPORT;
235 #endif
236 }
237 
uapi_nv_register_change_notify_proc(uint16_t min_key,uint16_t max_key,nv_changed_notify_func func)238 errcode_t uapi_nv_register_change_notify_proc(uint16_t min_key, uint16_t max_key, nv_changed_notify_func func)
239 {
240     if ((min_key > max_key) || (func == NULL)) {
241         return ERRCODE_NV_INVALID_PARAMS;
242     }
243 #if (CONFIG_NV_SUPPORT_CHANGE_NOTIFY == NV_YES)
244     if (nv_direct_get_nv_ctrl()->notify_regitser_max_nums == 0) {
245         return ERRCODE_NV_INVALID_PARAMS;
246     }
247 
248     return nv_direct_add_func_to_notify_list(min_key, max_key, func);
249 #endif
250     return ERRCODE_NOT_SUPPORT;
251 }
252