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 return g_halPartitions;
56 }
57
virt_flash_erase(HalPartition pdrv,UINT32 offSet,UINT32 size)58 INT32 virt_flash_erase(HalPartition pdrv, UINT32 offSet, UINT32 size)
59 {
60 UINT32 startAddr = offSet;
61 UINT32 partitionEnd;
62
63 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
64 if (startAddr >= partitionEnd) {
65 printf("flash over erase, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
66 printf("flash over write\r\n");
67 return -1;
68 }
69 if ((startAddr + size) > partitionEnd) {
70 size = partitionEnd - startAddr;
71 printf("flash over write, new len is %d\r\n", size);
72 }
73
74 UINT32 intSave = LOS_IntLock();
75 (VOID)memset_s((VOID *)startAddr, size, 0, size);
76 LOS_IntRestore(intSave);
77 return 0;
78 }
79
virt_flash_write(HalPartition pdrv,UINT32 * offSet,const VOID * buf,UINT32 bufLen)80 INT32 virt_flash_write(HalPartition pdrv, UINT32 *offSet, const VOID *buf, UINT32 bufLen)
81 {
82 UINT32 startAddr = *offSet;
83 UINT32 partitionEnd;
84
85 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
86 if (startAddr >= partitionEnd) {
87 printf("flash over write, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
88 return -1;
89 }
90 if ((startAddr + bufLen) > partitionEnd) {
91 bufLen = partitionEnd - startAddr;
92 printf("flash over write, new len is %d\r\n", bufLen);
93 }
94
95 UINT32 intSave = LOS_IntLock();
96 (VOID)memcpy_s((VOID *)startAddr, bufLen, buf, bufLen);
97 LOS_IntRestore(intSave);
98
99 return 0;
100 }
101
virt_flash_erase_write(HalPartition pdrv,UINT32 * offSet,const VOID * buf,UINT32 bufLen)102 INT32 virt_flash_erase_write(HalPartition pdrv, UINT32 *offSet, const VOID *buf, UINT32 bufLen)
103 {
104 UINT32 startAddr = *offSet;
105 UINT32 partitionEnd;
106
107 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
108 if (startAddr >= partitionEnd) {
109 printf("flash over e&w, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
110 return -1;
111 }
112 if ((startAddr + bufLen) > partitionEnd) {
113 bufLen = partitionEnd - startAddr;
114 printf("flash over erase&write, new len is %d\r\n", bufLen);
115 }
116
117 UINT32 intSave = LOS_IntLock();
118 (VOID)memset_s((VOID *)startAddr, bufLen, 0, bufLen);
119 (VOID)memcpy_s((VOID *)startAddr, bufLen, buf, bufLen);
120 LOS_IntRestore(intSave);
121 return 0;
122 }
123
virt_flash_read(HalPartition pdrv,UINT32 * offSet,VOID * buf,UINT32 bufLen)124 INT32 virt_flash_read(HalPartition pdrv, UINT32 *offSet, VOID *buf, UINT32 bufLen)
125 {
126 UINT32 startAddr = *offSet;
127 UINT32 partitionEnd;
128
129 partitionEnd = g_halPartitions[pdrv].partitionStartAddr + g_halPartitions[pdrv].partitionLength;
130 if (startAddr >= partitionEnd) {
131 printf("flash over read, 0x%x, 0x%x\r\n", startAddr, partitionEnd);
132 return -1;
133 }
134 if ((startAddr + bufLen) > partitionEnd) {
135 bufLen = partitionEnd - startAddr;
136 printf("flash over read, new len is %d\r\n", bufLen);
137 }
138
139 UINT32 intSave = LOS_IntLock();
140 (VOID)memcpy_s(buf, bufLen, (VOID *)startAddr, bufLen);
141 LOS_IntRestore(intSave);
142 return 0;
143 }
144