1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include <stdlib.h>
6 #include "aos/hal/flash.h"
7 #include "cmsis_os.h"
8 #include "cmsis.h"
9 #include "hal_trace.h"
10 #include "hal_norflash.h"
11 #define BES_HAL_DEBUG 0
12 #include "app_hal.h"
13 #include "ota_port.h"
14 
15 #define FLASH_PAGE_SIZE_IN_BYTES 256
16 
17 extern const size_t hal_partitions_amount;
18 extern const hal_logic_partition_t hal_partitions[];
19 enum ota_link ota_current_link = OTA_LINK_ERR;
20 osMutexId FlashMutex = NULL;
21 osMutexDef_t os_mutex_def_flash;
FlashosMutexWait(void)22 static void FlashosMutexWait(void)
23 {
24     if(FlashMutex == NULL)
25     {
26         FlashMutex = osMutexCreate(&os_mutex_def_flash);
27     }
28 
29     osMutexWait(FlashMutex, osWaitForever);
30 }
31 
my_memcpy(void * dst,void * src,size_t num)32 void * my_memcpy(void *dst, void *src, size_t num)
33 {
34 	int offset1 = (4 - ((uint32)dst & 3)) & 3;
35 	int offset2 = (4 - ((uint32)src & 3)) & 3;
36 
37 	if(offset1 != offset2) {
38 		return memcpy(dst, src, num);
39 	}
40 
41     int wordnum = num > offset1 ? (num - offset1) /8 : 0;
42     int slice = num > offset1 ? (num-offset1) % 8 : 0;
43 	char *pdst = (char *)dst;
44 	char *psrc = (char *)src;
45     long long * pintsrc;
46     long long * pintdst;
47 
48 	while(offset1--) {
49 		*pdst++ = *psrc++;
50 	}
51 
52 	pintdst = (long long*)pdst;
53 	pintsrc = (long long*)psrc;
54     while(wordnum--) {
55 		*pintdst++ = *pintsrc++;
56     }
57 
58 	pdst = (char*)pintdst;
59 	psrc = (char*)pintsrc;
60     while(slice--) {
61 		*pdst++ = *psrc++;
62     }
63 
64     return dst;
65 }
66 
bes_hal_flash_read(const hal_partition_t partition,const uint32_t addr,uint8_t * dst,const uint32_t size)67 int bes_hal_flash_read(const hal_partition_t partition, const uint32_t addr, uint8_t *dst, const uint32_t size)
68 {
69     int ret = 0;
70     uint32_t lock = 0;
71     volatile char *flashPointer = NULL;
72 
73 	if (NULL == dst) {
74         ret = -1;
75         goto RETURN;
76     }
77     watchdog_feeddog();
78     FlashosMutexWait();
79 	if (ota_current_link == OTA_LINK_B) {
80 		if (partition == HAL_PARTITION_APPLICATION) {
81 			hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
82 		}
83 	}
84 
85     flashPointer = (volatile char *)(FLASH_NC_BASE + addr);
86     //memcpy(dst, (void *)flashPointer, size);
87     my_memcpy(dst, (void *)flashPointer, size);
88 
89 	if (ota_current_link == OTA_LINK_B) {
90 		if (partition == HAL_PARTITION_APPLICATION) {
91 			hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
92         }
93 	}
94     osMutexRelease(FlashMutex);
95 RETURN:
96     return ret;
97 }
98 
99 #if 0
100 int bes_hal_flash_write(const uint32_t addr, const uint8_t *src, const uint32_t size)
101 {
102     int ret = 0;
103     uint32_t lock;
104     uint32_t num = 0;
105     uint32_t left_len = 0;
106     uint32_t align_len = 0;
107     uint32_t fill_len = 0;
108     uint32_t flash_offset = addr;
109     uint32_t lengthToBurn = size;
110     uint8_t *ptrSource = NULL;
111     volatile char *flashPointer = NULL;
112     uint8_t *buf = NULL;
113 
114     ptrSource = (uint8_t *)src;
115     if (NULL == ptrSource) {
116         ret = -2;
117         goto RETURN;
118     }
119 
120     FlashosMutexWait();
121     if (addr % FLASH_BLOCK_SIZE_IN_BYTES != 0) {
122         //buf = (uint8_t *)rt_malloc(FLASH_BLOCK_SIZE_IN_BYTES);
123         buf = (uint8_t *)malloc(FLASH_BLOCK_SIZE_IN_BYTES);
124         if (!buf) {
125             TRACE("%s %d, rt_malloc error", __func__, __LINE__);
126             ret = -1;
127             goto end;
128         }
129     } else {
130         if (lengthToBurn % FLASH_BLOCK_SIZE_IN_BYTES != 0) {
131             //buf = (uint8_t *)rt_malloc(FLASH_BLOCK_SIZE_IN_BYTES);
132             buf = (uint8_t *)malloc(FLASH_BLOCK_SIZE_IN_BYTES);
133             if (!buf) {
134                 TRACE("%s %d, rt_malloc error", __func__, __LINE__);
135                 ret = -1;
136                 goto end;
137             }
138         }
139     }
140 
141     pmu_flash_write_config();
142     if (flash_offset % FLASH_BLOCK_SIZE_IN_BYTES != 0) {
143         left_len = FLASH_BLOCK_SIZE_IN_BYTES - flash_offset % FLASH_BLOCK_SIZE_IN_BYTES;
144         fill_len = (left_len >= lengthToBurn) ? lengthToBurn : left_len;
145 
146         align_len = flash_offset;
147         align_len -= align_len % FLASH_BLOCK_SIZE_IN_BYTES;
148         memset(buf, 0, FLASH_BLOCK_SIZE_IN_BYTES);
149         // read first
150         lock = int_lock();
151         flashPointer = (volatile char *)(FLASH_NC_BASE + align_len);
152         memcpy(buf, (void *)flashPointer, FLASH_BLOCK_SIZE_IN_BYTES);
153 
154         ret = hal_norflash_erase(HAL_NORFLASH_ID_0, align_len, FLASH_BLOCK_SIZE_IN_BYTES);
155         if (ret != HAL_NORFLASH_OK) {
156             TRACE("error %s %d, hal_norflash_erase ret:%d", __func__, __LINE__, ret);
157             pmu_flash_read_config();
158             int_unlock(lock);
159             goto end;
160         }
161 
162         memcpy((buf + flash_offset % FLASH_BLOCK_SIZE_IN_BYTES), ptrSource, fill_len);
163         ret = hal_norflash_write(HAL_NORFLASH_ID_0, align_len, buf, FLASH_BLOCK_SIZE_IN_BYTES);
164         if (ret != HAL_NORFLASH_OK) {
165             TRACE("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
166             pmu_flash_read_config();
167             int_unlock(lock);
168             goto end;
169         }
170         int_unlock(lock);
171 
172 #ifdef __WATCHER_DOG_RESET__
173         app_wdt_ping();
174 #endif
175 
176         lengthToBurn -= fill_len;
177         flash_offset += fill_len;
178         ptrSource += fill_len;
179     }
180 
181     if (lengthToBurn > 0) {
182         for (num = 0; num < lengthToBurn / FLASH_BLOCK_SIZE_IN_BYTES; num ++) {
183             lock = int_lock();
184             ret = hal_norflash_erase(HAL_NORFLASH_ID_0, flash_offset, FLASH_BLOCK_SIZE_IN_BYTES);
185             if (ret != HAL_NORFLASH_OK) {
186                 TRACE("error %s %d, hal_norflash_erase ret:%d", __func__, __LINE__, ret);
187                 pmu_flash_read_config();
188                 int_unlock(lock);
189                 goto end;
190             }
191             ret = hal_norflash_write(HAL_NORFLASH_ID_0, flash_offset, ptrSource, FLASH_BLOCK_SIZE_IN_BYTES);
192             if (ret != HAL_NORFLASH_OK) {
193                 TRACE("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
194                 pmu_flash_read_config();
195                 int_unlock(lock);
196                 goto end;
197             }
198             int_unlock(lock);
199 
200 #ifdef __WATCHER_DOG_RESET__
201             app_wdt_ping();
202 #endif
203             flash_offset += FLASH_BLOCK_SIZE_IN_BYTES;
204             ptrSource += FLASH_BLOCK_SIZE_IN_BYTES;
205         }
206 
207         left_len = lengthToBurn % FLASH_BLOCK_SIZE_IN_BYTES;
208         if (left_len) {
209             memset(buf, 0, FLASH_BLOCK_SIZE_IN_BYTES);
210             // read first
211             lock = int_lock();
212             flashPointer = (volatile char *)(FLASH_NC_BASE + flash_offset);
213             memcpy(buf, (void *)flashPointer, FLASH_BLOCK_SIZE_IN_BYTES);
214 
215             ret = hal_norflash_erase(HAL_NORFLASH_ID_0, flash_offset, FLASH_BLOCK_SIZE_IN_BYTES);
216             if (ret != HAL_NORFLASH_OK) {
217                 TRACE("error %s %d, hal_norflash_erase ret:%d", __func__, __LINE__, ret);
218                 pmu_flash_read_config();
219                 int_unlock(lock);
220                 goto end;
221             }
222 
223             memcpy(buf, ptrSource, left_len);
224             ret = hal_norflash_write(HAL_NORFLASH_ID_0, flash_offset, buf, FLASH_BLOCK_SIZE_IN_BYTES);
225             if (ret != HAL_NORFLASH_OK) {
226                 TRACE("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
227                 pmu_flash_read_config();
228                 int_unlock(lock);
229                 goto end;
230             }
231             int_unlock(lock);
232 
233 #ifdef __WATCHER_DOG_RESET__
234             app_wdt_ping();
235 #endif
236         }
237     }
238     pmu_flash_read_config();
239 
240 end:
241     if (addr % FLASH_BLOCK_SIZE_IN_BYTES != 0) {
242         //rt_free(buf);
243         free(buf);
244         buf = NULL;
245     } else {
246         if (lengthToBurn % FLASH_BLOCK_SIZE_IN_BYTES != 0) {
247             //rt_free(buf);
248             free(buf);
249             buf = NULL;
250         }
251     }
252     osMutexRelease(FlashMutex);
253 
254 RETURN:
255     return ret;
256 }
257 #else
bes_hal_flash_write(const hal_partition_t partition,const uint32_t addr,const uint8_t * src,const uint32_t size)258 int bes_hal_flash_write(const hal_partition_t partition, const uint32_t addr, const uint8_t *src, const uint32_t size)
259 {
260     int ret = 0;
261     uint32_t lock;
262 
263     watchdog_feeddog();
264     FlashosMutexWait();
265     lock = int_lock();
266     pmu_flash_write_config();
267 	if (partition == HAL_PARTITION_APPLICATION) {
268         hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
269     }
270 
271     ret = hal_norflash_write(HAL_NORFLASH_ID_0, addr, src, size);
272 
273 	if (partition == HAL_PARTITION_APPLICATION) {
274         hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
275     }
276 
277     pmu_flash_read_config();
278     int_unlock(lock);
279     osMutexRelease(FlashMutex);
280 
281     if (ret != HAL_NORFLASH_OK) {
282         TRACE("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
283     }
284     return ret;
285 }
286 #endif
287 
288 #if 0
289 int bes_hal_flash_erase(const uint32_t addr, const uint32_t size)
290 {
291     int i = 0;
292     int ret = 0;
293     uint32_t lock = 0;
294     uint32_t start_addr = addr;
295     uint16_t start_sec = (addr / FLASH_SECTOR_SIZE_IN_BYTES);
296     uint16_t end_sec = ((addr + size - 1) / FLASH_SECTOR_SIZE_IN_BYTES);
297 
298     if (addr % FLASH_SECTOR_SIZE_IN_BYTES != 0) {
299         TRACE("%s %d, unaligned addr:%d", __func__, __LINE__, addr);
300         //return -2;  // allow to erase from unaligned addr
301     }
302 
303     FlashosMutexWait();
304     lock = int_lock();
305     pmu_flash_write_config();
306 
307     for (i = start_sec; i <= end_sec; i++) {
308         ret = hal_norflash_erase(HAL_NORFLASH_ID_0, start_addr, FLASH_SECTOR_SIZE_IN_BYTES);
309         if (ret != HAL_NORFLASH_OK) {
310             TRACE("error %s %d, ret:%d", __func__, __LINE__, ret);
311             goto end;
312         }
313         start_addr += FLASH_SECTOR_SIZE_IN_BYTES;
314     }
315 
316 end:
317     pmu_flash_read_config();
318     int_unlock(lock);
319     osMutexRelease(FlashMutex);
320 
321     return ret;
322 }
323 #else
bes_hal_flash_erase(const hal_partition_t partition,const uint32_t addr,const uint32_t size)324 int bes_hal_flash_erase(const hal_partition_t partition, const uint32_t addr, const uint32_t size)
325 {
326     int i = 0;
327     int ret = 0;
328     uint32_t lock = 0;
329 
330     FlashosMutexWait();
331     lock = int_lock();
332     pmu_flash_write_config();
333 
334 	if (partition == HAL_PARTITION_APPLICATION) {
335         hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
336     }
337 
338     ret = hal_norflash_erase(HAL_NORFLASH_ID_0, addr, size);
339     if (ret != HAL_NORFLASH_OK) {
340         TRACE("error %s %d, ret:%d", __func__, __LINE__, ret);
341     }
342 
343 	if (partition == HAL_PARTITION_APPLICATION) {
344         hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
345     }
346 
347     pmu_flash_read_config();
348     int_unlock(lock);
349     osMutexRelease(FlashMutex);
350 
351     return ret;
352 }
353 #endif
354 
bes_get_current_link(void)355 enum ota_link bes_get_current_link(void)
356 {
357 	int32_t ret = 0;
358     struct ota_boot_info info;
359 	enum bootinfo_zone zone;
360 
361 	zone = ota_get_valid_bootinfo_zone();
362 	ret = ota_get_bootinfo(&info, zone);
363 	if (ret) {
364 		return -1;
365 	}
366 
367 	return info.update_link;
368 }
369 
bes_get_operate_partition(const hal_partition_t in_partition)370 hal_partition_t bes_get_operate_partition(const hal_partition_t in_partition)
371 {
372 	hal_partition_t out_partition = 0;
373 	int32_t ret = 0;
374     hal_partition_t partition_index;
375     hal_logic_partition_t partition_info;
376     struct ota_boot_info info;
377 	enum bootinfo_zone zone;
378 
379 	if (in_partition >= HAL_PARTITION_MAX) {
380 		return HAL_PARTITION_ERROR;
381 	}
382 
383 	if (ota_current_link == OTA_LINK_ERR) {
384 		ota_current_link = bes_get_current_link();
385 	}
386 
387 	if (in_partition == HAL_PARTITION_OTA_TEMP) {
388 		if (ota_current_link == OTA_LINK_A) { //Now run RTOS zoneA, update zoneB.
389 			out_partition = HAL_PARTITION_OTA_TEMP;
390 		} else if (ota_current_link == OTA_LINK_B) { //Now run RTOS zoneB, update zoneA.
391 			out_partition = HAL_PARTITION_APPLICATION;
392 		}
393 	} else {
394 		out_partition = in_partition;
395 	}
396 
397 	return out_partition;
398 }
399 
hal_flash_mtdpart_info_get(hal_mtdpart_info_t ** result_array,int * cnt)400 int32_t hal_flash_mtdpart_info_get(hal_mtdpart_info_t **result_array, int *cnt)
401 {
402 #if CONFIG_U_FLASH_CORE
403     return -1;
404 #else
405     hal_logic_partition_t *p;
406     hal_mtdpart_info_t *arr;
407     size_t total = hal_partitions_amount;
408 
409     /* get the actual cnt */
410     *cnt = 0;
411     for (int i = 0; i < total; i++) {
412         p = &hal_partitions[i];
413         if (p->partition_description == NULL) continue;
414         *cnt += 1;
415     }
416 
417     /* memory alloc */
418     *result_array = (hal_mtdpart_info_t *)malloc(sizeof(hal_mtdpart_info_t) * (*cnt));
419     if (*result_array == NULL) {
420         return -1;
421     }
422 
423     /* fetch result */
424     int idx = 0;
425     arr = *result_array;
426     for (int i = 0; i < total; i++) {
427         p = &hal_partitions[i];
428         if (p->partition_description == NULL) continue;
429         arr[idx].p = i;
430         arr[idx].descr = (char *)p->partition_description;
431         arr[idx].offset = p->partition_start_addr;
432         arr[idx].siz = p->partition_length;
433         arr[idx].ssiz = CONFIG_LFS_PROG_SIZE;
434         arr[idx].bsiz = CONFIG_LFS_PROG_SIZE * CONFIG_LFS_PAGE_NUM_PER_BLOCK;
435         idx++;
436     }
437 
438     return 0;
439 #endif
440 }
441 
442 /**
443  * Get the information of the specified flash area
444  *
445  * @param[in]  in_partition     The target flash logical partition
446  * @param[in]  partition        The buffer to store partition info
447  *
448  * @return  0: On success, otherwise is error
449  */
hal_flash_info_get(hal_partition_t in_partition,hal_logic_partition_t * partition)450 int32_t hal_flash_info_get(hal_partition_t in_partition, hal_logic_partition_t *partition)
451 {
452     int32_t ret = -1;
453     hal_logic_partition_t *logic_partition;
454     hal_partition_t partition_id = 0;
455 
456     if ((partition != NULL) && (in_partition < hal_partitions_amount)) {
457         ENTER_FUNCTION();
458         partition_id = bes_get_operate_partition(in_partition);
459         if (partition_id < 0) {
460             TRACE("bes_get_operate_partition fail\n");
461         }
462         else {
463             logic_partition = (hal_logic_partition_t *)&hal_partitions[partition_id];
464             memcpy(partition, logic_partition, sizeof(hal_logic_partition_t));
465             ret = 0;
466         }
467         LEAVE_FUNCTION();
468     }
469 	return ret;
470 }
471 
472 /**
473  * Erase an area on a Flash logical partition
474  *
475  * @note  Erase on an address will erase all data on a sector that the
476  *        address is belonged to, this function does not save data that
477  *        beyond the address area but in the affected sector, the data
478  *        will be lost.
479  *
480  * @param[in]  in_partition  The target flash logical partition which should be erased
481  * @param[in]  off_set       Start address of the erased flash area
482  * @param[in]  size          Size of the erased flash area
483  *
484  * @return  0 : On success, EIO : If an error occurred with any step
485  */
hal_flash_erase(hal_partition_t in_partition,uint32_t off_set,uint32_t size)486 int32_t hal_flash_erase(hal_partition_t in_partition, uint32_t off_set, uint32_t size)
487 {
488     uint32_t start_addr;
489     hal_logic_partition_t info;
490     hal_partition_t operate_partition_id = 0;
491     int32_t ret = 0;
492     watchdog_feeddog();
493     ENTER_FUNCTION();
494     if (hal_flash_info_get(in_partition, &info) != 0) {
495         TRACEME("hal_flash_info_get fail\n");
496         ret = -1;
497         goto RETURN;
498     }
499     start_addr = info.partition_start_addr + off_set;
500     operate_partition_id = bes_get_operate_partition(in_partition);
501     TRACEME("hal_flash_erase part start0x%x offset 0x%x  size 0x%x\n", info.partition_start_addr, off_set, size);
502     ret = bes_hal_flash_erase(operate_partition_id, start_addr, size);
503 
504 RETURN:
505     LEAVE_FUNCTION();
506     return ret;
507 }
508 
509 /**
510  * Write data to an area on a flash logical partition without erase
511  *
512  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
513  * @param[in]  off_set         Point to the start address that the data is written to, and
514  *                             point to the last unwritten address after this function is
515  *                             returned, so you can call this function serval times without
516  *                             update this start address.
517  * @param[in]  inBuffer        point to the data buffer that will be written to flash
518  * @param[in]  inBufferLength  The length of the buffer
519  *
520  * @return  0 : On success, EIO : If an error occurred with any step
521  */
hal_flash_write(hal_partition_t in_partition,uint32_t * off_set,const void * in_buf,uint32_t in_buf_len)522 int32_t hal_flash_write(hal_partition_t in_partition, uint32_t *off_set,
523                         const void *in_buf, uint32_t in_buf_len)
524 {
525     int32_t ret = 0;
526     uint32_t start_addr;
527     uint32_t partition_end;
528     hal_logic_partition_t info;
529     hal_partition_t operate_partition_id = 0;
530 
531     ENTER_FUNCTION();
532     if (hal_flash_info_get(in_partition, &info) != 0) {
533         TRACEME("hal_flash_info_get fail\n");
534         ret = -1;
535         goto RETURN;
536     }
537 
538     start_addr = info.partition_start_addr + *off_set;
539     partition_end = info.partition_start_addr + info.partition_length;
540     TRACEME("hal_flash_write partstart 0x%x offset 0x%x len 0x%x\n", info.partition_start_addr, *off_set, in_buf_len);
541     if(start_addr >= partition_end){
542         TRACEME("flash over write\r\n");
543         ret = -1;
544         goto RETURN;
545     }
546 
547     if((start_addr + in_buf_len) > partition_end){
548         in_buf_len = partition_end - start_addr;
549         TRACEME("flash over write, new len is %d\r\n", in_buf_len);
550     }
551     operate_partition_id = bes_get_operate_partition(in_partition);
552     ret = bes_hal_flash_write(operate_partition_id, start_addr, in_buf, in_buf_len);
553     if (!ret) {
554         *off_set += in_buf_len;
555     }
556 
557 RETURN:
558     LEAVE_FUNCTION();
559 	return ret;
560 }
561 
562 /**
563  * Write data to an area on a flash logical partition with erase first
564  *
565  * @param[in]  in_partition    The target flash logical partition which should be read which should be written
566  * @param[in]  off_set         Point to the start address that the data is written to, and
567  *                             point to the last unwritten address after this function is
568  *                             returned, so you can call this function serval times without
569  *                             update this start address.
570  * @param[in]  inBuffer        point to the data buffer that will be written to flash
571  * @param[in]  inBufferLength  The length of the buffer
572  *
573  * @return  0 : On success, EIO : If an error occurred with any step
574  */
hal_flash_erase_write(hal_partition_t in_partition,uint32_t * off_set,const void * in_buf,uint32_t in_buf_len)575 int32_t hal_flash_erase_write(hal_partition_t in_partition, uint32_t *off_set,
576                               const void *in_buf, uint32_t in_buf_len)
577 {
578     int32_t ret = 0;
579     uint32_t start_addr;
580     uint32_t partition_end;
581     hal_logic_partition_t info;
582     hal_partition_t operate_partition_id = 0;
583     ENTER_FUNCTION();
584     if (hal_flash_info_get(in_partition, &info) != 0) {
585         TRACEME("hal_flash_info_get fail\n");
586         ret = -1;
587         goto RETURN;
588     }
589 
590     start_addr = info.partition_start_addr + *off_set;
591     partition_end = info.partition_start_addr + info.partition_length;
592 
593     if(start_addr >= partition_end){
594         TRACEME("flash over write\r\n");
595         ret = -1;
596         goto RETURN;
597     }
598 
599     if((start_addr + in_buf_len) > partition_end){
600         in_buf_len = partition_end - start_addr;
601         TRACEME("flash over write, new len is %d\r\n", in_buf_len);
602     }
603     operate_partition_id = bes_get_operate_partition(in_partition);
604     ret = bes_hal_flash_erase(operate_partition_id, start_addr, in_buf_len);
605     if (ret) {
606         TRACEME("flash erase fail\r\n");
607         ret = -1;
608         goto RETURN;
609     }
610 
611     ret = bes_hal_flash_write(operate_partition_id, start_addr, in_buf, in_buf_len);
612     if (!ret) {
613         *off_set += in_buf_len;
614     }
615 
616 RETURN:
617     LEAVE_FUNCTION();
618     return ret;
619 }
620 
621 /**
622  * Read data from an area on a Flash to data buffer in RAM
623  *
624  * @param[in]  in_partition    The target flash logical partition which should be read
625  * @param[in]  off_set         Point to the start address that the data is read, and
626  *                             point to the last unread address after this function is
627  *                             returned, so you can call this function serval times without
628  *                             update this start address.
629  * @param[in]  outBuffer       Point to the data buffer that stores the data read from flash
630  * @param[in]  inBufferLength  The length of the buffer
631  *
632  * @return  0 : On success, EIO : If an error occurred with any step
633  */
hal_flash_read(hal_partition_t in_partition,uint32_t * off_set,void * out_buf,uint32_t in_buf_len)634 int32_t hal_flash_read(hal_partition_t in_partition, uint32_t *off_set,
635                        void *out_buf, uint32_t in_buf_len)
636 {
637     int32_t ret = 0;
638     uint32_t start_addr;
639     hal_logic_partition_t info;
640     hal_partition_t operate_partition_id = 0;
641     ENTER_FUNCTION();
642     if (hal_flash_info_get(in_partition, &info) != 0) {
643         TRACEME("hal_flash_info_get fail\n");
644         ret = -1;
645         goto RETURN;
646     }
647     start_addr = info.partition_start_addr + *off_set;
648     operate_partition_id = bes_get_operate_partition(in_partition);
649     ret = bes_hal_flash_read(operate_partition_id, start_addr, out_buf, in_buf_len);
650     if(ret == 0) {
651         *off_set += in_buf_len;
652     }
653 RETURN:
654     LEAVE_FUNCTION();
655     return ret;
656 }
657 /**
658  * Set security options on a logical partition
659  *
660  * @param[in]  partition  The target flash logical partition
661  * @param[in]  offset     Point to the start address that the data is read, and
662  *                        point to the last unread address after this function is
663  *                        returned, so you can call this function serval times without
664  *                        update this start address.
665  * @param[in]  size       Size of enabled flash area
666  *
667  * @return  0 : On success, EIO : If an error occurred with any step
668  */
hal_flash_enable_secure(hal_partition_t partition,uint32_t off_set,uint32_t size)669 int32_t hal_flash_enable_secure(hal_partition_t partition, uint32_t off_set, uint32_t size)
670 {
671     //FAIL_FUNCTION();
672     return 0;
673 }
674 
675 /**
676  * Disable security options on a logical partition
677  *
678  * @param[in]  partition  The target flash logical partition
679  * @param[in]  offset     Point to the start address that the data is read, and
680  *                        point to the last unread address after this function is
681  *                        returned, so you can call this function serval times without
682  *                        update this start address.
683  * @param[in]  size       Size of disabled flash area
684  *
685  * @return  0 : On success, EIO : If an error occurred with any step
686  */
hal_flash_dis_secure(hal_partition_t partition,uint32_t off_set,uint32_t size)687 int32_t hal_flash_dis_secure(hal_partition_t partition, uint32_t off_set, uint32_t size)
688 {
689     //FAIL_FUNCTION();
690     return 0;
691 }
692 
693 /**
694  * Convert physical address to logic partition id and offset in partition
695  *
696  * @param[out]  in_partition Point to the logic partition id
697  * @param[out]  off_set      Point to the offset in logic partition
698  * @param[in]   addr         The physical address
699  *
700  * @return 0 : On success, EIO : If an error occurred with any step
701  */
hal_flash_addr2offset(hal_partition_t * in_partition,uint32_t * off_set,uint32_t addr)702 int32_t hal_flash_addr2offset(hal_partition_t *in_partition, uint32_t *off_set, uint32_t addr)
703 {
704     //FAIL_FUNCTION();
705     return 0;
706 }