1 /*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <string.h>
32 #include <stdlib.h>
33 #include "stdio.h"
34 #include "pthread.h"
35 #include "securec.h"
36 #include "los_interrupt.h"
37 #include "ram_virt_flash.h"
38
39 /* Logic partition on flash devices, if open at the same time, needed modify by user */
40 HalLogicPartition g_halPartitions[] = {
41 [FLASH_PARTITION_DATA0] = {
42 .partitionDescription = "littlefs",
43 .partitionStartAddr = 0x21800000,
44 .partitionLength = 0x800000, // size is 8M
45 },
46 [FLASH_PARTITION_DATA1] = {
47 .partitionDescription = "vfat",
48 .partitionStartAddr = 0x21800000,
49 .partitionLength = 0x800000, // size is 8M
50 },
51 };
52
getPartitionInfo(VOID)53 HalLogicPartition *getPartitionInfo(VOID)
54 {
55 UINTPTR partitionMem;
56 #if (LOSCFG_SUPPORT_LITTLEFS == 1)
57 partitionMem = (UINTPTR)malloc(g_halPartitions[FLASH_PARTITION_DATA0].partitionLength);
58 if (!partitionMem) {
59 return NULL;
60 }
61 g_halPartitions[FLASH_PARTITION_DATA0].partitionStartAddr = partitionMem;
62 #endif
63 #if (LOSCFG_SUPPORT_FATFS == 1)
64 partitionMem = (UINTPTR)malloc(g_halPartitions[FLASH_PARTITION_DATA1].partitionLength);
65 if (!partitionMem) {
66 #if (LOSCFG_SUPPORT_LITTLEFS == 1)
67 free(g_halPartitions[FLASH_PARTITION_DATA0].partitionStartAddr);
68 #endif
69 return NULL;
70 }
71 g_halPartitions[FLASH_PARTITION_DATA1].partitionStartAddr = partitionMem;
72 #endif
73
74 return g_halPartitions;
75 }
76
virt_flash_erase(HalPartition pdrv,UINT32 offSet,UINT32 size)77 INT32 virt_flash_erase(HalPartition pdrv, UINT32 offSet, UINT32 size)
78 {
79 UINT32 startAddr = offSet;
80 UINT32 partitionEnd;
81
82 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
83 if (startAddr >= partitionEnd) {
84 printf("flash over erase, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
85 printf("flash over write\r\n");
86 return -1;
87 }
88 if ((startAddr + size) > partitionEnd) {
89 size = partitionEnd - startAddr;
90 printf("flash over write, new len is %d\r\n", size);
91 }
92
93 UINT32 intSave = LOS_IntLock();
94 (VOID)memset_s((VOID *)startAddr, size, 0, size);
95 LOS_IntRestore(intSave);
96 return 0;
97 }
98
virt_flash_write(HalPartition pdrv,UINT32 * offSet,const VOID * buf,UINT32 bufLen)99 INT32 virt_flash_write(HalPartition pdrv, UINT32 *offSet, const VOID *buf, UINT32 bufLen)
100 {
101 UINT32 startAddr = *offSet;
102 UINT32 partitionEnd;
103
104 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
105 if (startAddr >= partitionEnd) {
106 printf("flash over write, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
107 return -1;
108 }
109 if ((startAddr + bufLen) > partitionEnd) {
110 bufLen = partitionEnd - startAddr;
111 printf("flash over write, new len is %d\r\n", bufLen);
112 }
113
114 UINT32 intSave = LOS_IntLock();
115 (VOID)memcpy_s((VOID *)startAddr, bufLen, buf, bufLen);
116 LOS_IntRestore(intSave);
117
118 return 0;
119 }
120
virt_flash_erase_write(HalPartition pdrv,UINT32 * offSet,const VOID * buf,UINT32 bufLen)121 INT32 virt_flash_erase_write(HalPartition pdrv, UINT32 *offSet, const VOID *buf, UINT32 bufLen)
122 {
123 UINT32 startAddr = *offSet;
124 UINT32 partitionEnd;
125
126 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
127 if (startAddr >= partitionEnd) {
128 printf("flash over e&w, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
129 return -1;
130 }
131 if ((startAddr + bufLen) > partitionEnd) {
132 bufLen = partitionEnd - startAddr;
133 printf("flash over erase&write, new len is %d\r\n", bufLen);
134 }
135
136 UINT32 intSave = LOS_IntLock();
137 (VOID)memset_s((VOID *)startAddr, bufLen, 0, bufLen);
138 (VOID)memcpy_s((VOID *)startAddr, bufLen, buf, bufLen);
139 LOS_IntRestore(intSave);
140 return 0;
141 }
142
virt_flash_read(HalPartition pdrv,UINT32 * offSet,VOID * buf,UINT32 bufLen)143 INT32 virt_flash_read(HalPartition pdrv, UINT32 *offSet, VOID *buf, UINT32 bufLen)
144 {
145 UINT32 startAddr = *offSet;
146 UINT32 partitionEnd;
147
148 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
149 if (startAddr >= partitionEnd) {
150 printf("flash over read, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
151 return -1;
152 }
153 if ((startAddr + bufLen) > partitionEnd) {
154 bufLen = partitionEnd - startAddr;
155 printf("flash over read, new len is %d\r\n", bufLen);
156 }
157
158 UINT32 intSave = LOS_IntLock();
159 (VOID)memcpy_s(buf, bufLen, (VOID *)startAddr, bufLen);
160 LOS_IntRestore(intSave);
161 return 0;
162 }
163