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: Function headers of different chips in the NV module.
15 */
16
17 #ifndef NV_PORTING_H
18 #define NV_PORTING_H
19
20 #include "stdint.h"
21 #include "stdbool.h"
22 #include "errcode.h"
23 #include "securec.h"
24 #include "soc_osal.h"
25 #include "osal_addr.h"
26 #include "nv_config.h"
27 #include "nv_key.h"
28
kv_malloc(uint32_t x)29 static inline void *kv_malloc(uint32_t x)
30 {
31 return osal_kmalloc(x, 0);
32 }
33
kv_zalloc(uint32_t x)34 static inline void *kv_zalloc(uint32_t x)
35 {
36 return osal_kzalloc(x, 0);
37 }
38
kv_free(void * x)39 static inline void kv_free(void *x)
40 {
41 osal_kfree(x);
42 }
43
44 /*
45 * nv_psram_malloc:从psram上分配内存存放NV数据
46 * NV支持异步存储时(CONFIG_NV_SUPPORT_ASYNCHRONOUS_STORE特性宏设置为NV_YES)有效
47 * 根据实际NV支持异步存储时NV数据存放器件进行配置
48 * 默认内存分配函数为kv_malloc
49 */
nv_psram_malloc(uint32_t x)50 static inline void *nv_psram_malloc(uint32_t x)
51 {
52 return kv_malloc(x);
53 }
54
55 /*
56 * nv_psram_free:从psram上释放内存
57 * NV支持异步存储时(CONFIG_NV_SUPPORT_ASYNCHRONOUS_STORE特性宏设置为NV_YES)有效
58 * 根据实际NV支持异步存储时NV数据存放器件进行配置
59 * 默认内存释放函数为kv_free
60 */
nv_psram_free(void * x)61 static inline void nv_psram_free(void *x)
62 {
63 kv_free(x);
64 }
65
66 errcode_t kv_flash_read(const uint32_t flash_offset, const uint32_t read_size, uint8_t *read_buffer);
67 errcode_t kv_flash_write(const uint32_t flash_offset, uint32_t write_size, const uint8_t *write_data, bool do_erase);
68 errcode_t kv_flash_erase(const uint32_t flash_addr, uint32_t size);
69
70 /* 创建加解密通道 */
71 errcode_t nv_crypto_claim_aes(uint32_t *crypto_handle, const kv_key_header_t *header);
72
73 /* 释放加解密通道 */
74 void nv_crypto_release_aes(uint32_t crypto_handle);
75
76 /* NV数据加密 */
77 errcode_t nv_crypto_encode(uint32_t crypto_handle, const uintptr_t src, uintptr_t dest, uint32_t length);
78
79 /* NV数据解密 */
80 errcode_t nv_crypto_decode(uint32_t crypto_handle, const uintptr_t src, uintptr_t dest, uint32_t length);
81
82 /* 获取加密Tag */
83 errcode_t nv_crypto_get_tag(uint32_t crypto_handle, uint8_t *tag, uint32_t *tag_len);
84
85 /* 设置当前NV的Tag */
86 errcode_t nv_crypto_set_tag(uint32_t crypto_handle, uint8_t *tag, uint32_t tag_len);
87
88 /* 校验Tag */
89 errcode_t nv_crypto_validate_tag(uint32_t crypto_handle);
90
91 /* 获取随机数 */
92 void nv_crypto_generate_random(uint32_t *rnd);
93
94 /* 启动HASH计算 */
95 errcode_t nv_crypto_start_hash(void);
96
97 /* 更新HASH */
98 errcode_t nv_crypto_update_hash(const uint8_t *src, uint32_t length);
99
100 /* 完成HASH计算并返回HASH值 */
101 errcode_t nv_crypto_complete_hash(uint8_t *hash);
102
103 #if (defined(CONFIG_NV_SUPPORT_DEBUG) && (CONFIG_NV_SUPPORT_DEBUG == NV_YES))
104 #define nv_log_err(fmt, arg...) osal_printk(fmt, ##arg)
105 #define nv_log_info(fmt, arg...) osal_printk(fmt, ##arg)
106 #define nv_log_debug(fmt, arg...) osal_printk(fmt, ##arg)
107 #else
108 #define nv_log_err(fmt, arg...)
109 #define nv_log_info(fmt, arg...)
110 #define nv_log_debug(fmt, arg...)
111 #endif
112
113 #endif /* NV_PORTING_H */
114
115