• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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  */
15 #ifndef HAL_FLASH_H
16 #define HAL_FLASH_H
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <stdint.h>
23 
24 #define PAR_OPT_READ_POS (0)
25 #define PAR_OPT_WRITE_POS (1)
26 
27 #define PAR_OPT_READ_MASK (0x1u << PAR_OPT_READ_POS)
28 #define PAR_OPT_WRITE_MASK (0x1u << PAR_OPT_WRITE_POS)
29 
30 #define PAR_OPT_READ_DIS (0x0u << PAR_OPT_READ_POS)
31 #define PAR_OPT_READ_EN (0x1u << PAR_OPT_READ_POS)
32 #define PAR_OPT_WRITE_DIS (0x0u << PAR_OPT_WRITE_POS)
33 #define PAR_OPT_WRITE_EN (0x1u << PAR_OPT_WRITE_POS)
34 
35 typedef enum {
36     HAL_PARTITION_ERROR = -1,
37     HAL_PARTITION_BOOTLOADER = 2,
38     HAL_PARTITION_BOOT2A = 3,
39     HAL_PARTITION_BOOT2B = 4,
40     HAL_PARTITION_TRUSTZONEA = 5,
41     HAL_PARTITION_TRUSTZONEB = 6,
42     HAL_PARTITION_TZ_INFO = 7,
43     HAL_PARTITION_CM33_MAIN = 8,
44     HAL_PARTITION_SYSTEM_MINI = 9,
45     HAL_PARTITION_RESOURCE = 10,
46     HAL_PARTITION_LOG = 11,
47     HAL_PARTITION_DATA = 12,
48     HAL_PARTITION_MISC = 13,
49     HAL_PARTITION_USERDATA = 14,
50     HAL_PARTITION_ENV = 15,
51     HAL_PARTITION_ENV_REDUND = 16,
52     HAL_PARTITION_MAX,
53 } hal_partition_t;
54 
55 typedef enum {
56     HAL_FLASH_EMBEDDED,
57     HAL_FLASH_SPI,
58     HAL_FLASH_QSPI,
59     HAL_FLASH_MAX,
60     HAL_FLASH_NONE,
61 } hal_flash_t;
62 
63 typedef struct {
64     hal_flash_t partition_owner;
65     const char *partition_description;
66     uint32_t partition_start_addr;
67     uint32_t partition_length;
68     uint32_t partition_options;
69 } hal_logic_partition_t;
70 
71 #define BOOT_INFO_A_ADDR 0xFD0000
72 #define BOOT_INFO_A_B_SIZE 0x6000
73 #define BOOT_INFO_B_ADDR (BOOT_INFO_A_ADDR + BOOT_INFO_A_B_SIZE)
74 #define SECURE_CHECK_MAX_TIME 3
75 #define EXCEPTION_REBOOT_COUNT_MAX 3
76 #define CMU_BOOTMODE_WATCHDOG_BIT (1 << 0)
77 #define OTA_BOOT_INFO_HEAD_LEN (4 + 4)
78 #define OTA_BOOT_INFO_BODY_LEN 20
79 #define RD_DATA_LEN_MAX 200
80 #define RD_SIGN_LEN_MAX 384
81 #define MISC_HEADER_MAGIC 0x6564636A
82 #define DEV_FLASH_NAME "flash0"
83 
84 typedef struct {
85     int rdDataLen;                /* 研发模式明文长度 */
86     char rdData[RD_DATA_LEN_MAX]; /* 研发模式明文 */
87     char rdSign[RD_SIGN_LEN_MAX]; /* 研发模式签名 */
88 } RdModeInfo;
89 
90 typedef struct {
91     int headerMagic;    /* Header魔术字 */
92     int crcVal;         /* CRC值 */
93     int len;            /* 数据长度 */
94     int curbootArea;    /* 当前启动区 */
95     int upgMode;        /* 升级模式 */
96     int quietUpgFlg;    /* 静默升级标记 */
97     int timerRebootFlg; /* 定时重启标记 */
98     int bootTimes;      /* 当前分区异常启动次数 */
99     RdModeInfo rdMode;  /* 研发模式 */
100 } MiscDataInfo;
101 
102 typedef enum {
103     BOOT_AREA_A = 0,
104     BOOT_AREA_B = 1,
105 } BootAreaVal;
106 
107 typedef enum {
108     UPG_MODE_NORMAL = 0, /* normal模式 */
109     UPG_MODE_OTA = 1,    /* OTA升级模式 */
110 } UpgModeVal;
111 
112 typedef enum {
113     NORMAL_OTA = 0,         /* 非静默升级 */
114     QUIET_FIRST_STAGE = 1,  /* 静默升级第一阶段 */
115     QUIET_SECOND_STAGE = 2, /* 静默升级第二阶段 */
116 } OtaUpgTypeVal;
117 
118 typedef enum {
119     NORMAL_REBOOT = 0, /* 非定时重启 */
120     TIMER_REBOOT = 1,  /* 定时重启 */
121 } RebootTypeVal;
122 
123 typedef enum {
124     MISC_CRC_VALUE = 0,
125     MISC_DATA_LENGTH,
126     MISC_CUR_BOOT_AREA,
127     MISC_UPG_MODE,
128     MISC_QUIET_UPG_FLAG,
129     MISC_TIMER_REBOOT_FLAG,
130     MISC_BOOT_TIMES,
131     MISC_RD_MODE_INFO,
132     MISC_DATA_MAX,
133 } MiscDataType;
134 
135 typedef enum {
136     BOOTINFO_ORIGIN,
137     BOOTINFO_BACKUP,
138     BOOTINFO_INVALID
139 } bootinfo_block;
140 
141 typedef enum {
142     BOOTINFO_ZONE_0,
143     BOOTINFO_ZONE_1,
144     BOOTINFO_ZONE_2,
145     BOOTINFO_ZONE_3,
146     BOOTINFO_ZONE_4,
147     BOOTINFO_ZONE_5,
148     BOOTINFO_ZONE_MAX
149 } bootinfo_zone;
150 
151 typedef enum {
152     SECURE_CHECK_BOOT2A,
153     SECURE_CHECK_BOOT2B,
154     SECURE_CHECK_CM33_MAIN,
155     SECURE_CHECK_CM33_MINI,
156     SECURE_CHECK_MAX
157 } secure_check_zone;
158 
159 typedef enum {
160     LINK_BOOT2A,
161     LINK_BOOT2B,
162     LINK_CM33_MAIN,
163     LINK_CM33_MINI,
164     LINK_MAX
165 } link_entry;
166 
167 typedef enum {
168     /** No error, everything OK.   */
169     ERR_OK = 0,
170     /** parameter error.           */
171     ERR_PARAMETER = -1,
172     /** function return error.     */
173     ERR_RETURN = -2,
174     /** software exception error.  */
175     ERR_SWEXCEPTION = -3,
176 } err_enum;
177 
178 /**
179  * Get the information of the specified flash area
180  *
181  * @param[in]  in_partition     The target flash logical partition
182  * @param[in]  partition        The buffer to store partition info
183  *
184  * @return  0: On success, otherwise is error
185  */
186 int32_t hal_flash_info_get(hal_partition_t in_partition, hal_logic_partition_t *partition);
187 
188 /**
189  * Erase an area on a Flash logical partition
190  *
191  * @note  Erase on an address will erase all data on a sector that the
192  *        address is belonged to, this function does not save data that
193  *        beyond the address area but in the affected sector, the data
194  *        will be lost.
195  *
196  * @param[in]  in_partition  The target flash logical partition which should be erased
197  * @param[in]  off_set       Start address of the erased flash area
198  * @param[in]  size          Size of the erased flash area
199  *
200  * @return  0 : On success, EIO : If an error occurred with any step
201  */
202 int32_t hal_flash_erase(hal_partition_t in_partition, uint32_t off_set, uint32_t size);
203 
204 /**
205  * Write data to an area on a flash logical partition without erase
206  *
207  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
208  * @param[in]  off_set         Point to the start address that the data is written to, and
209  *                             point to the last unwritten address after this function is
210  *                             returned, so you can call this function serval times without
211  *                             update this start address.
212  * @param[in]  inBuffer        point to the data buffer that will be written to flash
213  * @param[in]  inBufferLength  The length of the buffer
214  *
215  * @return  0 : On success, EIO : If an error occurred with any step
216  */
217 int32_t hal_flash_write(hal_partition_t in_partition, uint32_t *off_set,
218                         const void *in_buf, uint32_t in_buf_len);
219 
220 /**
221  * Write data to an area on a flash logical partition with erase first
222  *
223  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
224  * @param[in]  off_set         Point to the start address that the data is written to, and
225  *                             point to the last unwritten address after this function is
226  *                             returned, so you can call this function serval times without
227  *                             update this start address.
228  * @param[in]  inBuffer        point to the data buffer that will be written to flash
229  * @param[in]  inBufferLength  The length of the buffer
230  *
231  * @return  0 : On success, EIO : If an error occurred with any step
232  */
233 int32_t hal_flash_erase_write(hal_partition_t in_partition, uint32_t *off_set,
234                               const void *in_buf, uint32_t in_buf_len);
235 
236 /**
237  * Read data from an area on a Flash to data buffer in RAM
238  *
239  * @param[in]  in_partition    The target flash logical partition which should be read
240  * @param[in]  off_set         Point to the start address that the data is read, and
241  *                             point to the last unread address after this function is
242  *                             returned, so you can call this function serval times without
243  *                             update this start address.
244  * @param[in]  outBuffer       Point to the data buffer that stores the data read from flash
245  * @param[in]  inBufferLength  The length of the buffer
246  *
247  * @return  0 : On success, EIO : If an error occurred with any step
248  */
249 int32_t hal_flash_read(hal_partition_t in_partition, uint32_t *off_set,
250                        void *out_buf, uint32_t in_buf_len);
251 
252 /**
253  * Set security options on a logical partition
254  *
255  * @param[in]  partition  The target flash logical partition
256  * @param[in]  offset     Point to the start address that the data is read, and
257  *                        point to the last unread address after this function is
258  *                        returned, so you can call this function serval times without
259  *                        update this start address.
260  * @param[in]  size       Size of enabled flash area
261  *
262  * @return  0 : On success, EIO : If an error occurred with any step
263  */
264 int32_t hal_flash_enable_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
265 
266 /**
267  * Disable security options on a logical partition
268  *
269  * @param[in]  partition  The target flash logical partition
270  * @param[in]  offset     Point to the start address that the data is read, and
271  *                        point to the last unread address after this function is
272  *                        returned, so you can call this function serval times without
273  *                        update this start address.
274  * @param[in]  size       Size of disabled flash area
275  *
276  * @return  0 : On success, EIO : If an error occurred with any step
277  */
278 int32_t hal_flash_dis_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
279 
280 /**
281  * Convert physical address to logic partition id and offset in partition
282  *
283  * @param[out]  in_partition Point to the logic partition id
284  * @param[out]  off_set      Point to the offset in logic partition
285  * @param[in]   addr         The physical address
286  *
287  * @return 0 : On success, EIO : If an error occurred with any step
288  */
289 int32_t hal_flash_addr2offset(hal_partition_t *in_partition, uint32_t *off_set, uint32_t addr);
290 
291 int SetMiscData(MiscDataType type, const void *data, uint32_t dataLen);
292 
293 int GetMiscData(MiscDataType type, void *data, uint32_t dataLen);
294 
295 int ota_flash_read(const hal_partition_t partition, const uint32_t addr, uint8_t *dst, const uint32_t size);
296 
297 int ota_flash_write(const hal_partition_t partition, const uint32_t addr, const uint8_t *src, const uint32_t size);
298 
299 int32_t GetFlashBadBlock(hal_partition_t *partition, uint32_t *offset, uint32_t *blockNum);
300 
301 int32_t GetFlashType(int32_t *factoryID, int32_t *deviceID);
302 
303 int32_t GetFlashStatisticInfo(hal_partition_t partition, uint32_t blockNum, uint32_t op, uint32_t *blockTimes);
304 
305 int32_t GetFlashRewriteCycle(hal_partition_t partition, uint32_t blockNum, uint32_t op, uint32_t *times);
306 
307 int32_t GetFlashBadBlockNum(hal_partition_t partition, uint32_t *blockNum);
308 
309 #ifdef __cplusplus
310 }
311 #endif
312 
313 #endif /* HAL_FLASH_H */
314