1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 #include "2ndboot.h"
5 #include "flash.h"
6 #include "cmsis.h"
7 #include "hal_trace.h"
8 #include "hal_norflash.h"
9 
10 extern const hal_logic_partition_t hal_partitions[];
11 
flash_get_info(hal_partition_t pno)12 hal_logic_partition_t *flash_get_info(hal_partition_t pno)
13 {
14     if(pno >= HAL_PARTITION_MAX)
15     {
16         return NULL;
17     }
18     return &hal_partitions[pno];
19 }
20 
flash_erase(unsigned int offset,unsigned int len)21 int flash_erase(unsigned int offset, unsigned int len)
22 {
23     int                    ret = 0;
24     unsigned int           addr;
25     unsigned int           start_addr;
26     unsigned int           end_addr;
27     uint32_t lock = 0;
28 
29     start_addr = ROUND_DOWN(offset, SECTOR_SIZE);
30     end_addr = ROUND_DOWN((offset + len - 1), SECTOR_SIZE);
31 
32     for (addr = start_addr; addr <= end_addr; addr += SECTOR_SIZE) {
33         lock = int_lock();
34         pmu_flash_write_config();
35         hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
36         ret = hal_norflash_erase(HAL_NORFLASH_ID_0, addr, SECTOR_SIZE);
37         hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
38         if (ret != HAL_NORFLASH_OK) {
39             printf("error %s %d, ret:%d", __func__, __LINE__, ret);
40         }
41         pmu_flash_read_config();
42         int_unlock(lock);
43     }
44 
45     return 0;
46 }
47 
flash_read_data(unsigned int offset,unsigned char * buf,unsigned int len)48 int flash_read_data(unsigned int offset, unsigned char *buf, unsigned int len)
49 {
50     int                     ret = 0;
51     unsigned int           lock = 0;
52     volatile char *flashPointer = NULL;
53 
54     if (buf == NULL) {
55         return -1;
56     }
57 
58     if (len == 0) {
59         return -1;
60     }
61 
62     lock = int_lock();
63     hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
64 	flashPointer = (volatile char *)(FLASH_NC_BASE + offset);
65 	memcpy(buf, (void *)flashPointer, len);
66     hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
67 	int_unlock(lock);
68 
69     if (ret != HAL_NORFLASH_OK) {
70         printf("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
71     }
72 
73     return 0;
74 }
75 
flash_write_data(unsigned int offset,unsigned char * buf,unsigned int len)76 int flash_write_data(unsigned int offset, unsigned char *buf, unsigned int len)
77 {
78     int           ret = 0;
79     unsigned int lock = 0;
80 
81     if (buf == NULL) {
82         return -1;
83     }
84 
85     if (len == 0) {
86         return -1;
87     }
88 
89     lock = int_lock();
90     pmu_flash_write_config();
91     hal_norflash_disable_remap(HAL_NORFLASH_ID_0);
92     ret = hal_norflash_write(HAL_NORFLASH_ID_0, offset, buf, len);
93     hal_norflash_re_enable_remap(HAL_NORFLASH_ID_0);
94     pmu_flash_read_config();
95     int_unlock(lock);
96 
97     if (ret != HAL_NORFLASH_OK) {
98         printf("error %s %d, hal_norflash_write ret:%d", __func__, __LINE__, ret);
99     }
100 
101     return 0;
102 }