• 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 
83 typedef struct {
84     int rdDataLen;                /* 研发模式明文长度 */
85     char rdData[RD_DATA_LEN_MAX]; /* 研发模式明文 */
86     char rdSign[RD_SIGN_LEN_MAX]; /* 研发模式签名 */
87 } RdModeInfo;
88 
89 typedef struct {
90     int headerMagic;    /* Header魔术字 */
91     int crcVal;         /* CRC值 */
92     int len;            /* 数据长度 */
93     int curbootArea;    /* 当前启动区 */
94     int upgMode;        /* 升级模式 */
95     int quietUpgFlg;    /* 静默升级标记 */
96     int timerRebootFlg; /* 定时重启标记 */
97     int bootTimes;      /* 当前分区异常启动次数 */
98     RdModeInfo rdMode;  /* 研发模式 */
99 } MiscDataInfo;
100 
101 typedef enum {
102     BOOT_AREA_A = 0,
103     BOOT_AREA_B = 1,
104 } BootAreaVal;
105 
106 typedef enum {
107     UPG_MODE_NORMAL = 0, /* normal模式 */
108     UPG_MODE_OTA = 1,    /* OTA升级模式 */
109 } UpgModeVal;
110 
111 typedef enum {
112     NORMAL_OTA = 0,         /* 非静默升级 */
113     QUIET_FIRST_STAGE = 1,  /* 静默升级第一阶段 */
114     QUIET_SECOND_STAGE = 2, /* 静默升级第二阶段 */
115 } OtaUpgTypeVal;
116 
117 typedef enum {
118     NORMAL_REBOOT = 0, /* 非定时重启 */
119     TIMER_REBOOT = 1,  /* 定时重启 */
120 } RebootTypeVal;
121 
122 typedef enum {
123     MISC_CRC_VALUE = 0,
124     MISC_DATA_LENGTH,
125     MISC_CUR_BOOT_AREA,
126     MISC_UPG_MODE,
127     MISC_QUIET_UPG_FLAG,
128     MISC_TIMER_REBOOT_FLAG,
129     MISC_BOOT_TIMES,
130     MISC_RD_MODE_INFO,
131     MISC_DATA_MAX,
132 } MiscDataType;
133 
134 typedef enum {
135     BOOTINFO_ORIGIN,
136     BOOTINFO_BACKUP,
137     BOOTINFO_INVALID
138 } bootinfo_block;
139 
140 typedef enum {
141     BOOTINFO_ZONE_0,
142     BOOTINFO_ZONE_1,
143     BOOTINFO_ZONE_2,
144     BOOTINFO_ZONE_3,
145     BOOTINFO_ZONE_4,
146     BOOTINFO_ZONE_5,
147     BOOTINFO_ZONE_MAX
148 } bootinfo_zone;
149 
150 typedef enum {
151     SECURE_CHECK_BOOT2A,
152     SECURE_CHECK_BOOT2B,
153     SECURE_CHECK_CM33_MAIN,
154     SECURE_CHECK_CM33_MINI,
155     SECURE_CHECK_MAX
156 } secure_check_zone;
157 
158 typedef enum {
159     LINK_BOOT2A,
160     LINK_BOOT2B,
161     LINK_CM33_MAIN,
162     LINK_CM33_MINI,
163     LINK_MAX
164 } link_entry;
165 
166 typedef enum {
167     /** No error, everything OK.   */
168     ERR_OK = 0,
169     /** parameter error.           */
170     ERR_PARAMETER = -1,
171     /** function return error.     */
172     ERR_RETURN = -2,
173     /** software exception error.  */
174     ERR_SWEXCEPTION = -3,
175 } err_enum;
176 
177 /**
178  * Get the information of the specified flash area
179  *
180  * @param[in]  in_partition     The target flash logical partition
181  * @param[in]  partition        The buffer to store partition info
182  *
183  * @return  0: On success, otherwise is error
184  */
185 int32_t hal_flash_info_get(hal_partition_t in_partition, hal_logic_partition_t *partition);
186 
187 /**
188  * Erase an area on a Flash logical partition
189  *
190  * @note  Erase on an address will erase all data on a sector that the
191  *        address is belonged to, this function does not save data that
192  *        beyond the address area but in the affected sector, the data
193  *        will be lost.
194  *
195  * @param[in]  in_partition  The target flash logical partition which should be erased
196  * @param[in]  off_set       Start address of the erased flash area
197  * @param[in]  size          Size of the erased flash area
198  *
199  * @return  0 : On success, EIO : If an error occurred with any step
200  */
201 int32_t hal_flash_erase(hal_partition_t in_partition, uint32_t off_set, uint32_t size);
202 
203 /**
204  * Write data to an area on a flash logical partition without erase
205  *
206  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
207  * @param[in]  off_set         Point to the start address that the data is written to, and
208  *                             point to the last unwritten address after this function is
209  *                             returned, so you can call this function serval times without
210  *                             update this start address.
211  * @param[in]  inBuffer        point to the data buffer that will be written to flash
212  * @param[in]  inBufferLength  The length of the buffer
213  *
214  * @return  0 : On success, EIO : If an error occurred with any step
215  */
216 int32_t hal_flash_write(hal_partition_t in_partition, uint32_t *off_set,
217                         const void *in_buf, uint32_t in_buf_len);
218 
219 /**
220  * Write data to an area on a flash logical partition with erase first
221  *
222  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
223  * @param[in]  off_set         Point to the start address that the data is written to, and
224  *                             point to the last unwritten address after this function is
225  *                             returned, so you can call this function serval times without
226  *                             update this start address.
227  * @param[in]  inBuffer        point to the data buffer that will be written to flash
228  * @param[in]  inBufferLength  The length of the buffer
229  *
230  * @return  0 : On success, EIO : If an error occurred with any step
231  */
232 int32_t hal_flash_erase_write(hal_partition_t in_partition, uint32_t *off_set,
233                               const void *in_buf, uint32_t in_buf_len);
234 
235 /**
236  * Read data from an area on a Flash to data buffer in RAM
237  *
238  * @param[in]  in_partition    The target flash logical partition which should be read
239  * @param[in]  off_set         Point to the start address that the data is read, and
240  *                             point to the last unread address after this function is
241  *                             returned, so you can call this function serval times without
242  *                             update this start address.
243  * @param[in]  outBuffer       Point to the data buffer that stores the data read from flash
244  * @param[in]  inBufferLength  The length of the buffer
245  *
246  * @return  0 : On success, EIO : If an error occurred with any step
247  */
248 int32_t hal_flash_read(hal_partition_t in_partition, uint32_t *off_set,
249                        void *out_buf, uint32_t in_buf_len);
250 
251 /**
252  * Set security options on a logical partition
253  *
254  * @param[in]  partition  The target flash logical partition
255  * @param[in]  offset     Point to the start address that the data is read, and
256  *                        point to the last unread address after this function is
257  *                        returned, so you can call this function serval times without
258  *                        update this start address.
259  * @param[in]  size       Size of enabled flash area
260  *
261  * @return  0 : On success, EIO : If an error occurred with any step
262  */
263 int32_t hal_flash_enable_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
264 
265 /**
266  * Disable security options on a logical partition
267  *
268  * @param[in]  partition  The target flash logical partition
269  * @param[in]  offset     Point to the start address that the data is read, and
270  *                        point to the last unread address after this function is
271  *                        returned, so you can call this function serval times without
272  *                        update this start address.
273  * @param[in]  size       Size of disabled flash area
274  *
275  * @return  0 : On success, EIO : If an error occurred with any step
276  */
277 int32_t hal_flash_dis_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
278 
279 /**
280  * Convert physical address to logic partition id and offset in partition
281  *
282  * @param[out]  in_partition Point to the logic partition id
283  * @param[out]  off_set      Point to the offset in logic partition
284  * @param[in]   addr         The physical address
285  *
286  * @return 0 : On success, EIO : If an error occurred with any step
287  */
288 int32_t hal_flash_addr2offset(hal_partition_t *in_partition, uint32_t *off_set, uint32_t addr);
289 
290 int SetMiscData(MiscDataType type, const void *data, uint32_t dataLen);
291 
292 int GetMiscData(MiscDataType type, void *data, uint32_t dataLen);
293 
294 int ota_flash_read(const hal_partition_t partition, const uint32_t addr, uint8_t *dst, const uint32_t size);
295 
296 int ota_flash_write(const hal_partition_t partition, const uint32_t addr, const uint8_t *src, const uint32_t size);
297 
298 int32_t GetFlashBadBlock(hal_partition_t *partition, uint32_t *offset, uint32_t *blockNum);
299 
300 int32_t GetFlashType(int32_t *factoryID, int32_t *deviceID);
301 
302 int32_t GetFlashStatisticInfo(hal_partition_t partition, uint32_t blockNum, uint32_t op, uint32_t *blockTimes);
303 
304 int32_t GetFlashRewriteCycle(hal_partition_t partition, uint32_t blockNum, uint32_t op, uint32_t *times);
305 
306 int32_t GetFlashBadBlockNum(hal_partition_t partition, uint32_t *blockNum);
307 
308 #ifdef __cplusplus
309 }
310 #endif
311 
312 #endif /* HAL_FLASH_H */
313