1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef AOS_HAL_FLASH_H
6 #define AOS_HAL_FLASH_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include <stdint.h>
13 
14 #define PAR_OPT_READ_POS  (0)
15 #define PAR_OPT_WRITE_POS (1)
16 
17 #define PAR_OPT_READ_MASK  (0x1u << PAR_OPT_READ_POS)
18 #define PAR_OPT_WRITE_MASK (0x1u << PAR_OPT_WRITE_POS)
19 
20 #define PAR_OPT_READ_DIS  (0x0u << PAR_OPT_READ_POS)
21 #define PAR_OPT_READ_EN   (0x1u << PAR_OPT_READ_POS)
22 #define PAR_OPT_WRITE_DIS (0x0u << PAR_OPT_WRITE_POS)
23 #define PAR_OPT_WRITE_EN  (0x1u << PAR_OPT_WRITE_POS)
24 
25 typedef enum {
26     HAL_PARTITION_ERROR = -1,
27     HAL_PARTITION_BOOTLOADER,
28     HAL_PARTITION_APPLICATION,
29     HAL_PARTITION_ATE,
30     HAL_PARTITION_OTA_TEMP,
31     HAL_PARTITION_RF_FIRMWARE,
32     HAL_PARTITION_PARAMETER_1,
33     HAL_PARTITION_PARAMETER_2,
34     HAL_PARTITION_PARAMETER_3,
35     HAL_PARTITION_PARAMETER_4,
36     HAL_PARTITION_BT_FIRMWARE,
37     HAL_PARTITION_SPIFFS,
38     HAL_PARTITION_LITTLEFS,
39     HAL_PARTITION_LITTLEFS2,
40     HAL_PARTITION_LITTLEFS3,
41     HAL_PARTITION_CUSTOM_1,
42     HAL_PARTITION_CUSTOM_2,
43     HAL_PARTITION_2ND_BOOT,
44     HAL_PARTITION_MBINS_APP,
45     HAL_PARTITION_MBINS_KERNEL,
46     HAL_PARTITION_GPT,
47     HAL_PARTITION_ENV,
48     HAL_PARTITION_ENV_REDUND,
49     HAL_PARTITION_RTOSA,
50     HAL_PARTITION_RTOSB,
51     HAL_PARTITION_BOOT1,
52     HAL_PARTITION_BOOT1_REDUND,
53     HAL_PARTITION_FTL,
54     HAL_PARTITION_MAX,
55     HAL_PARTITION_NONE,
56 } hal_partition_t;
57 
58 typedef enum {
59     HAL_FLASH_EMBEDDED,
60     HAL_FLASH_SPI,
61     HAL_FLASH_QSPI,
62     HAL_FLASH_MAX,
63     HAL_FLASH_NONE,
64 } hal_flash_t;
65 
66 typedef enum {
67     HAL_FLASH_ERR_OK,           /* operation success */
68     HAL_FLASH_ERR_NAND_BAD,     /* Bad block */
69     HAL_FLASH_ERR_NAND_READ,    /* Read fail, can't correct */
70     HAL_FLASH_ERR_NAND_WRITE,   /* Write fail */
71     HAL_FLASH_ERR_NAND_ERASE,   /* Erase fail */
72     HAL_FLASH_ERR_NAND_FLIPS,   /* Too many bitflips, uncorrected */
73     /* add more hereafter */
74 } hal_flash_err_t;
75 
76 typedef struct {
77     hal_flash_t  partition_owner;
78     const char  *partition_description;
79     uint32_t     partition_start_addr;
80     uint32_t     partition_length;
81     uint32_t     partition_options;
82 } hal_logic_partition_t;
83 
84 typedef struct {
85     hal_partition_t p;
86     char *descr;
87     uint32_t offset;
88     uint32_t siz;
89 } hal_mtdpart_info_t;
90 
91 /**
92  * init flash partition
93  *
94  * @param[in]  in_partition     The target flash logical partition
95  *
96  * @return  0: On success, otherwise is error
97  */
98 int32_t aos_hal_flash_init(hal_partition_t in_partition);
99 
100 /**
101  * Get the information of the specified flash area
102  *
103  * @param[in]  in_partition     The target flash logical partition
104  * @param[in]  partition        The buffer to store partition info
105  *
106  * @return  0: On success, otherwise is error
107  */
108 int32_t aos_hal_flash_info_get(hal_partition_t in_partition, hal_logic_partition_t **partition);
109 
110 /**
111  * Get the information of all the flash partitions
112  *
113  * @param[in/out]  result_errar  The pointer to the array of MTD partition info
114  * @param[in/out]  cnt           The count of the items in the result array
115  *
116  * @return  0 on success, otherwise failure.
117  *
118  * @note Caller is responsible to free the result array memory.
119  */
120 int32_t aos_hal_flash_mtdpart_info_get(hal_mtdpart_info_t **result_array, int *cnt);
121 
122 /**
123  * Erase an area on a Flash logical partition
124  *
125  * @note  Erase on an address will erase all data on a sector that the
126  *        address is belonged to, this function does not save data that
127  *        beyond the address area but in the affected sector, the data
128  *        will be lost.
129  *
130  * @param[in]  in_partition  The target flash logical partition which should be erased
131  * @param[in]  off_set       Start address of the erased flash area
132  * @param[in]  size          Size of the erased flash area
133  *
134  * @return  0 : On success, EIO : If an error occurred with any step
135  */
136 int32_t aos_hal_flash_erase(hal_partition_t in_partition, uint32_t off_set, uint32_t size);
137 
138 /**
139  * Write data to an area on a flash logical partition without erase
140  *
141  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
142  * @param[in]  off_set         Point to the start address that the data is written to, and
143  *                             point to the last unwritten address after this function is
144  *                             returned, so you can call this function serval times without
145  *                             update this start address.
146  * @param[in]  inBuffer        point to the data buffer that will be written to flash
147  * @param[in]  inBufferLength  The length of the buffer
148  *
149  * @return  0 : On success, EIO : If an error occurred with any step
150  */
151 int32_t aos_hal_flash_write(hal_partition_t in_partition, uint32_t *off_set,
152                         const void *in_buf, uint32_t in_buf_len);
153 
154 /**
155  * Write data to an area on a flash logical partition with erase first
156  *
157  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
158  * @param[in]  off_set         Point to the start address that the data is written to, and
159  *                             point to the last unwritten address after this function is
160  *                             returned, so you can call this function serval times without
161  *                             update this start address.
162  * @param[in]  inBuffer        point to the data buffer that will be written to flash
163  * @param[in]  inBufferLength  The length of the buffer
164  *
165  * @return  0 : On success, EIO : If an error occurred with any step
166  */
167 int32_t aos_hal_flash_erase_write(hal_partition_t in_partition, uint32_t *off_set,
168                               const void *in_buf, uint32_t in_buf_len);
169 
170 /**
171  * Read data from an area on a Flash to data buffer in RAM
172  *
173  * @param[in]  in_partition    The target flash logical partition which should be read
174  * @param[in]  off_set         Point to the start address that the data is read, and
175  *                             point to the last unread address after this function is
176  *                             returned, so you can call this function serval times without
177  *                             update this start address.
178  * @param[in]  outBuffer       Point to the data buffer that stores the data read from flash
179  * @param[in]  inBufferLength  The length of the buffer
180  *
181  * @return  0 : On success, EIO : If an error occurred with any step
182  */
183 int32_t aos_hal_flash_read(hal_partition_t in_partition, uint32_t *off_set,
184                        void *out_buf, uint32_t in_buf_len);
185 
186 /**
187  * Set security options on a logical partition
188  *
189  * @param[in]  partition  The target flash logical partition
190  * @param[in]  offset     Point to the start address that the data is read, and
191  *                        point to the last unread address after this function is
192  *                        returned, so you can call this function serval times without
193  *                        update this start address.
194  * @param[in]  size       Size of enabled flash area
195  *
196  * @return  0 : On success, EIO : If an error occurred with any step
197  */
198 int32_t aos_hal_flash_enable_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
199 
200 /**
201  * Disable security options on a logical partition
202  *
203  * @param[in]  partition  The target flash logical partition
204  * @param[in]  offset     Point to the start address that the data is read, and
205  *                        point to the last unread address after this function is
206  *                        returned, so you can call this function serval times without
207  *                        update this start address.
208  * @param[in]  size       Size of disabled flash area
209  *
210  * @return  0 : On success, EIO : If an error occurred with any step
211  */
212 int32_t aos_hal_flash_dis_secure(hal_partition_t partition, uint32_t off_set, uint32_t size);
213 
214 /**
215  * Convert physical address to logic partition id and offset in partition
216  *
217  * @param[out]  in_partition Point to the logic partition id
218  * @param[out]  off_set      Point to the offset in logic partition
219  * @param[in]   addr         The physical address
220  *
221  * @return 0 : On success, EIO : If an error occurred with any step
222  */
223 int32_t aos_hal_flash_addr2offset(hal_partition_t *in_partition, uint32_t *off_set, uint32_t addr);
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif /* AOS_HAL_FLASH_H */
230 
231