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