• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file hi_nvm.h
3  *
4  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __HI_NVM_H__
19 #define __HI_NVM_H__
20 
21 #include <hi_types.h>
22 #include <hi_boot_rom.h>
23 
24 #define hi_make_identifier(a, b, c, d)      hi_makeu32(hi_makeu16(a, b), hi_makeu16(c, d))
25 #define HNV_FILE_SIGNATURE               hi_make_identifier('H', 'N', 'V', '$')
26 #define FNV_FILE_SIGNATURE               hi_make_identifier('F', 'N', 'V', '#')
27 
28 #define FACTORY_NV_SIZE   0x2000
29 #define FLASH_BLOCK_SIZE  0x1000
30 #define HNV_NCRC_SIZE     8
31 #define NV_TOTAL_MAX_NUM  255
32 #define HNV_FAULT_TOLERANT_TIMES  3
33 
34 #define HNV_MANAGE_FIXED_LEN  24
35 
36 /* The NV structure in the factory area must be the same as that in the kernel and
37    cannot be modified after being fixed. */
38 typedef struct _hi_nvm_manage_s_ {
39     hi_u32  magic;
40     hi_u32  crc;
41     hi_u8   ver;
42     hi_u8   head_len;
43     hi_u16  total_num;
44     hi_u32  seq;
45     hi_u32  ver_magic;
46     hi_u32  flash_size;
47     hi_u8   keep_id_range[2];  /* Reserved upg id, size:2 bytes, byte0:upper boundary, byte1:lower boundary */
48     hi_u8   reserve[2];        /* Reserved 2 bytes */
49     hi_u8   nv_item_data[0];
50 } hi_nv_manage;
51 
52 typedef struct hi_nv_item_index_s_ {
53     hi_u8 nv_id;
54     hi_u8 nv_len;
55     hi_u16 nv_offset;
56 } hi_nv_item_index;
57 
58 typedef struct _hi_nv_ctrl_s_ {
59     hi_u32 base_addr;
60     hi_u32 block_size;
61     hi_u32 total_block_size;
62     hi_u32 current_addr;
63     hi_u32 seq;
64     hi_u32 sem_handle;
65 
66     hi_u8 init_flag;
67     hi_u8 reserve;
68     hi_u16 total_num;
69     hi_u32 ver_magic;
70     hi_nv_item_index* index;
71 } hi_nv_ctrl;
72 
73 typedef enum _hi_nv_type_e_ {
74     HI_TYPE_NV = 0,
75     HI_TYPE_FACTORY_NV,
76     HI_TYPE_TEMP,
77     HI_TYPE_NV_MAX,
78 } hi_nv_type;
79 
80 hi_u32 hi_nv_flush_keep_ids(hi_u8* addr, hi_u32 len);
81 hi_u32 hi_nv_block_write(hi_u8* nv_file, hi_u32 len, hi_u32 flag);
82 
83 /** @defgroup iot_nv NV Management APIs
84 * @ingroup  iot_flashboot
85 */
86 /**
87 * @ingroup  iot_nv
88 * Maximum length of an NV item (unit: byte). CNcomment:NV项最大长度(单位:byte)。CNend
89 */
90 #define HNV_ITEM_MAXLEN             (256 - 4)
91 
92 /**
93 * @ingroup  iot_nv
94 */
95 #define PRODUCT_CFG_NV_REG_NUM_MAX  20
96 
97 /**
98 * @ingroup  iot_nv
99 */
100 #define HI_FNV_DEFAULT_ADDR         0x8000
101 
102 /**
103 * @ingroup  iot_nv
104 */
105 #define HI_NV_DEFAULT_TOTAL_SIZE    0x2000
106 
107 /**
108 * @ingroup  iot_nv
109 */
110 #define HI_NV_DEFAULT_BLOCK_SIZE    0x1000
111 
112 /**
113 * @ingroup  iot_nv
114 *
115 * Factory NV area user area end ID. CNcomment:工厂区NV结束ID。CNend
116 */
117 #define HI_NV_FACTORY_USR_ID_END    0x20
118 
119 /**
120 * @ingroup  iot_nv
121 */
122 #define  HI_NV_FTM_FLASH_PARTIRION_TABLE_ID  0x02
123 
124 /**
125 * @ingroup  iot_nv
126 * @brief Initializes NV management in the factory partition.CNcomment:工厂区NV初始化。CNend
127 *
128 * @par 描述:
129 *          Initializes NV management in the factory partition.CNcomment: 工厂区NV管理初始化。CNend
130 *
131 * @attention The parameters cannot be set randomly and must match the product delivery plan.
132 CNcomment:参数不能随意配置,需要与产品出厂规划匹配。CNend
133 * @param  addr [IN] type #hi_u32,Start address of the NV factory partition in the flash. The address is planned by
134 *                   the factory and set by the boot macro #FACTORY_NV_ADDR.
135 CNcomment:设置工厂区NV FLASH首地址,由出厂规划,boot宏定义FACTORY_NV_ADDR 统一设置。CNend
136 *
137 * @retval #0            Success.
138 * @retval #Other        Failure. For details, see hi_errno.h.
139 * @par 依赖:
140 *           @li hi_flashboot.h:Describes NV APIs.CNcomment:文件用于描述NV相关接口。CNend
141 * @see hi_factory_nv_write | hi_factory_nv_read。
142 */
143 hi_u32 hi_factory_nv_init(hi_u32 addr, hi_u32 total_size, hi_u32 block_size);
144 
145 /**
146 * @ingroup  iot_nv
147 * @brief Sets the NV value in the factory partition. CNcomment:设置工厂区NV值。CNend
148 *
149 * @par 描述:
150 *           Sets the NV value in the factory partition.CNcomment:设置工厂区NV值。CNend
151 *
152 * @attention None
153 * @param  id    [IN] type #hi_u8,NV item ID, ranging from #HI_NV_FACTORY_ID_START to #HI_NV_FACTORY_USR_ID_END.
154 CNcomment:NV项ID,范围从#HI_NV_FACTORY_ID_START到#HI_NV_FACTORY_USR_ID_END。CNend
155 * @param  pdata [IN] type #hi_pvoid,NV item data.CNcomment:NV项数据。CNend
156 * @param  len   [IN] type #hi_u8,Length of an NV item (unit: byte). The maximum value is #HNV_ITEM_MAXLEN.
157 CNcomment:NV项长度(单位:byte),最大不允许超过HNV_ITEM_MAXLEN。CNend
158 *
159 * @retval #0            Success.
160 * @retval #Other        Failure. For details, see hi_errno.h.
161 * @par 依赖:
162 *           @li hi_flashboot.h:Describes NV APIs.CNcomment:文件用于描述NV相关接口。CNend
163 * @see hi_factory_nv_read。
164 */
165 hi_u32 hi_factory_nv_write(hi_u8 id, hi_pvoid pdata, hi_u8 len, hi_u32 flag);
166 
167 /**
168 * @ingroup  iot_nv
169 * @brief Reads the NV value in the factory partition.CNcomment:读取工厂区NV值。CNend
170 *
171 * @par 描述:
172 *           Reads the NV value in the factory partition.读取工厂区NV值。CNend
173 *
174 * @attention None
175 *
176 * @param  id      [IN] type #hi_u8,NV item ID, ranging from #HI_NV_NORMAL_ID_START to #HI_NV_NORMAL_USR_ID_END.
177 CNcomment:NV项ID,范围从#HI_NV_NORMAL_ID_START到#HI_NV_NORMAL_USR_ID_END。CNend
178 * @param  pdata   [IN] type #hi_pvoid,NV item data.CNcomment:NV项数据。CNend
179 * @param  len     [IN] type #hi_u8,Length of an NV item (unit: byte). The maximum value is HNV_ITEM_MAXLEN.
180 CNcomment:NV项长度(单位:byte),最大不允许超过HNV_ITEM_MAXLEN。CNend
181 *
182 * @retval #0            Success.
183 * @retval #Other        Failure. For details, see hi_errno.h.
184 * @par 依赖:
185 *           @li hi_flashboot.h:Describes NV APIs.CNcomment:文件用于描述NV相关接口。CNend
186 * @see hi_factory_nv_write。
187 */
188 hi_u32 hi_factory_nv_read(hi_u8 id, hi_pvoid pdata, hi_u8 len, hi_u32 flag);
189 
190 /** @defgroup iot_crc32 CRC32 APIs
191 * @ingroup iot_flashboot
192 */
193 /**
194 * @ingroup  iot_crc32
195 * @brief  Generates a 16-bit CRC value.CNcomment:生成16位CRC校验值。CNend
196 *
197 * @par 描述:
198 *           Generates a 16-bit CRC value.CNcomment:生成16位CRC校验值。CNend
199 *
200 * @attention None
201 * @param  crc               [IN] type #hi_u16,The CRC initial value.CNcomment:CRC初始值。CNend
202 * @param  p                 [IN] type #const hi_u8*,Pointer to the buffer to be verified.
203 CNcomment:被校验Buffer指针。CNend
204 * @param  len               [IN] type #hi_u32,Length of the buffer to be verified (unit: Byte).
205 CNcomment:被校验Buffer长度(单位:byte)。CNend
206 *
207 * @retval #HI_ERR_SUCCESS   Success
208 * @retval #Other            Failure. For details, see hi_boot_err.h.
209 *
210 * @par 依赖:
211 *            @li hi_flashboot.h:Describes CRC APIs.CNcomment:文件包含CRC校验接口。CNend
212 * @see  None
213 */
214 hi_u32  hi_crc32 (hi_u32 crc, const hi_u8 *p, hi_u32 len);
215 
216 #endif   /* __HI_NVM_H__ */
217 
218