1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 //#include "soc.h"
11 #include "drv/spiflash.h"
12 //#include "dut/hal/common.h"
13 //#include "dut/hal/ble.h"
14 #include "aos/hal/flash.h"
15 #include "aos/kernel.h"
16 #include "common/log.h"
17 
18 #define FLASH_ALIGN_MASK ~(sizeof(uint32_t) - 1)
19 #define FLASH_ALIGN sizeof(uint32_t)
20 
21 //#define MAC_PARAMS_OFFSET (0)
22 //#define MAC_PARAMS_SIZE (0x10)
23 
24 //#define RSV_PARAMS_OFFSET (MAC_PARAMS_OFFSET + MAC_PARAMS_SIZE)
25 //#define RSV_PARAMS_SIZE (0x10)
26 
27 //#define FREQ_PARAMS_OFFSET (RSV_PARAMS_OFFSET + RSV_PARAMS_SIZE)
28 //#define FREQ_PARAMS_SIZE (0x8)
29 
30 //#define TRIPLE_OFFSET (FREQ_PARAMS_OFFSET + FREQ_PARAMS_SIZE)
31 #define TRIPLE_OFFSET (0)
32 #define TRIPLE_SIZE (32)
33 
34 #define GROUP_ADDR_OFFSET (TRIPLE_OFFSET + TRIPLE_SIZE)
35 #define GROUP_ADDR_SIZE (18) //CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2 + 2
36 
37 #define SN_PARAMS_OFFSET (GROUP_ADDR_OFFSET + GROUP_ADDR_SIZE)
38 #define SN_PARAMS_SIZE (32)
39 
40 //#define OTP_TOTAL_DATA_SIZE (MAC_PARAMS_SIZE + RSV_PARAMS_SIZE + FREQ_PARAMS_SIZE + TRIPLE_SIZE + GROUP_ADDR_SIZE + SN_PARAMS_SIZE)
41 #define OTP_TOTAL_DATA_SIZE (TRIPLE_SIZE + GROUP_ADDR_SIZE + SN_PARAMS_SIZE)
42 
hal_flash_read_otp(uint32_t off_set,void * out_buf,uint32_t out_buf_len)43 int32_t hal_flash_read_otp(uint32_t off_set, void *out_buf, uint32_t out_buf_len)
44 {
45     //return dut_hal_factorydata_read(off_set, out_buf, out_buf_len);
46 }
47 
hal_flash_write_otp(uint32_t off_set,const void * in_buf,uint32_t in_buf_len)48 int32_t hal_flash_write_otp(uint32_t off_set, const void *in_buf, uint32_t in_buf_len)
49 {
50     int32_t retval = 0;
51 
52     if (off_set + in_buf_len > OTP_TOTAL_DATA_SIZE)
53     {
54         printf("param err\n");
55         return -1;
56     }
57 
58     //retval = dut_hal_factorydata_store(off_set, (uint8_t *)in_buf, in_buf_len);
59 
60     return retval;
61 }
62 
hal_flash_read_xtalcap_params(void * out_buf,uint32_t out_buf_len)63 int32_t hal_flash_read_xtalcap_params(void *out_buf, uint32_t out_buf_len)
64 {
65     uint32_t xtal_cap = 0;
66     int ret = -1;
67     if (out_buf_len != sizeof(uint32_t)) {
68         return -1;
69     }
70 
71     //ret = dut_hal_xtalcap_get(&xtal_cap);
72 
73     memcpy(out_buf, &xtal_cap, sizeof(uint32_t));
74 
75     return ret;
76 }
77 
hal_flash_write_xtalcap_params(const void * in_buf,uint32_t in_buf_len)78 int32_t hal_flash_write_xtalcap_params(const void *in_buf, uint32_t in_buf_len)
79 {
80     if (in_buf_len != sizeof(uint32_t)) {
81         return -1;
82     }
83 
84     //return dut_hal_xtalcap_store(*((uint32_t *)in_buf));
85 }
86 
hal_flash_read_sn_params(void * out_buf,uint32_t out_buf_len)87 int32_t hal_flash_read_sn_params(void *out_buf, uint32_t out_buf_len)
88 {
89     return hal_flash_read_otp(SN_PARAMS_OFFSET, out_buf, out_buf_len);
90 }
91 
hal_flash_write_sn_params(const void * in_buf,uint32_t in_buf_len)92 int32_t hal_flash_write_sn_params(const void *in_buf, uint32_t in_buf_len)
93 {
94     return hal_flash_write_otp(SN_PARAMS_OFFSET, in_buf, in_buf_len);
95 }
96 
hal_flash_read_mac_params(void * out_buf,uint32_t out_buf_len)97 int32_t hal_flash_read_mac_params(void *out_buf, uint32_t out_buf_len)
98 {
99     if (!out_buf || out_buf_len < 6)
100         return -1;
101 
102     //return dut_hal_mac_get((uint8_t *)out_buf);
103 }
104 
hal_flash_write_mac_params(const void * in_buf,uint32_t in_buf_len)105 int32_t hal_flash_write_mac_params(const void *in_buf, uint32_t in_buf_len)
106 {
107     if (!in_buf || in_buf_len != 6)
108         return -1;
109 
110     //return dut_hal_mac_store((uint8_t *)in_buf);
111 }
112 
hal_flash_read_triples(void * out_buf,uint32_t out_buf_len)113 int32_t hal_flash_read_triples(void *out_buf, uint32_t out_buf_len)
114 {
115     return hal_flash_read_otp(TRIPLE_OFFSET, out_buf, out_buf_len);
116 }
117 
hal_flash_write_triples(const void * in_buf,uint32_t in_buf_len)118 int32_t hal_flash_write_triples(const void *in_buf, uint32_t in_buf_len)
119 {
120     return hal_flash_write_otp(TRIPLE_OFFSET, in_buf, in_buf_len);
121 }
122 
hal_flash_read_group_addr(void * out_buf,uint32_t out_buf_len)123 int32_t hal_flash_read_group_addr(void *out_buf, uint32_t out_buf_len)
124 {
125     return hal_flash_read_otp(GROUP_ADDR_OFFSET, out_buf, out_buf_len);
126 }
127 
hal_flash_write_group_addr(const void * in_buf,uint32_t in_buf_len)128 int32_t hal_flash_write_group_addr(const void *in_buf, uint32_t in_buf_len)
129 {
130     return hal_flash_write_otp(GROUP_ADDR_OFFSET, in_buf, in_buf_len);
131 }
132 
hal_flash_add2offset(hal_partition_t * in_partition,uint32_t * off_set,uint32_t addr)133 int32_t hal_flash_add2offset(hal_partition_t *in_partition, uint32_t *off_set, uint32_t addr)
134 { //partition_start_addr
135     hal_logic_partition_t partition_info;
136     int ret;
137     for (int i = 0; i < HAL_PARTITION_MAX; i++)
138     {
139         ret = hal_flash_info_get(i, &partition_info);
140         if (ret)
141             continue;
142 
143         if ((addr >= partition_info.partition_start_addr) && (addr < (partition_info.partition_start_addr + partition_info.partition_length)))
144         {
145             *in_partition = i;
146             *off_set = addr - partition_info.partition_start_addr;
147             return 0;
148         }
149     }
150     *in_partition = -1;
151     *off_set = 0;
152     return -1;
153 }
154 
hal_flash_write_test()155 void hal_flash_write_test()
156 {
157     int ret = 0;
158     uint32_t offset = 0;
159     uint8_t data[32] = {0};
160     uint8_t data_read[32] = {0};
161     int i = 0;
162     for (i = 0; i < sizeof(data); i++)
163     {
164         data[i] = i;
165     }
166 
167     hal_flash_erase(HAL_PARTITION_PARAMETER_2, 0, 4096);
168 
169     offset = 0;
170 
171     //printf("write test 32 by 32 %d, offset %d size %d, data %p\n", HAL_PARTITION_PARAMETER_2, offset, 4096, data);
172     for (i = 0; i < 4096 / 32; i++)
173     {
174         ret = hal_flash_write(HAL_PARTITION_PARAMETER_2, &offset, (uint8_t *)(data), 32);
175         if (ret)
176         {
177             printf("write 1 fail %d\n", ret);
178             break;
179         }
180     }
181 
182     offset = 0;
183     memset(data_read, 0, 32);
184     for (i = 0; i < 4096 / 32; i++)
185     {
186         memset(data_read, 0, 32);
187         ret = hal_flash_read(HAL_PARTITION_PARAMETER_2, &offset, (uint8_t *)(data_read), 32);
188         if (ret)
189         {
190             printf("read 1 fail %d\n", ret);
191             break;
192         }
193         if (memcmp(data, data_read, 32))
194         {
195             printf("write test fail, data missmatch\n");
196             break;
197         }
198     }
199 
200     hal_flash_erase(HAL_PARTITION_PARAMETER_2, 0, 4096);
201 
202     offset = 0;
203 
204     //printf("write test 1 by 1 %d, offset %d size %d, data %p\n", HAL_PARTITION_PARAMETER_2, offset, 4096, data);
205     for (i = 0; i < 4096; i++)
206     {
207         ret = hal_flash_write(HAL_PARTITION_PARAMETER_2, &offset, (uint8_t *)(data + i % 32), 1);
208         if (ret)
209         {
210             printf("write 2 fail %d\n", ret);
211             break;
212         }
213     }
214 
215     offset = 0;
216     memset(data_read, 0, 32);
217     for (i = 0; i < 4096; i++)
218     {
219         ret = hal_flash_read(HAL_PARTITION_PARAMETER_2, &offset, (uint8_t *)(data_read + i % 32), 1);
220         if (ret)
221         {
222             printf("read 2 fail %d\n", ret);
223             break;
224         }
225         if ((i + 1) % 32 == 0)
226         {
227             if (memcmp(data, data_read, 32))
228             {
229                 printf("write 2 1 by 1 test fail, data missmatch\n");
230                 break;
231             }
232             memset(data_read, 0, 32);
233         }
234     }
235 
236     hal_flash_erase(HAL_PARTITION_PARAMETER_2, 0, 4096);
237 }
238