1 /*
2 * Copyright (c) 2022 Talkweb Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <sys/mount.h>
17 #include "littlefs.h"
18 #include "los_config.h"
19 #include "hdf_log.h"
20 #include "hdf_device_desc.h"
21 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
22 #include "hcs_macro.h"
23 #include "hdf_config_macro.h"
24 #else
25 #include "device_resource_if.h"
26 #endif
27 #include <sys/stat.h>
28 #include <dirent.h>
29
30 #define LITTLEFS_PHYS_ADDR 0x800000
31
32 #define READ_SIZE 64
33 #define PROG_SIZE 64
34 #define BLOCK_SIZE 4096
35 #define BLOCK_COUNT 2048
36 #define CACHE_SIZE 64
37 #define LOOKAHEAD_SIZE 64
38 #define BLOCK_CYCLES 16
39 #define ERASE_FLASH_BULK 0
40
41 struct fs_cfg {
42 char *mount_point;
43 struct lfs_config lfs_cfg;
44 };
45
46 static struct fs_cfg fs[LOSCFG_LFS_MAX_MOUNT_SIZE] = {0};
47 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
48 #define DISPLAY_MISC_FS_LITTLEFS_CONFIG HCS_NODE(HCS_NODE(HCS_NODE(HCS_ROOT, misc), fs_config), littlefs_config)
FsGetResource(struct fs_cfg * fs)49 static uint32_t FsGetResource(struct fs_cfg *fs)
50 {
51 int32_t num = HCS_ARRAYS_SIZE(HCS_NODE(DISPLAY_MISC_FS_LITTLEFS_CONFIG, mount_points));
52 if (num < 0 || num > LOSCFG_LFS_MAX_MOUNT_SIZE) {
53 HDF_LOGE("%s: invalid mount_points num %d", __func__, num);
54 return HDF_FAILURE;
55 }
56 char * mount_points[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_FS_LITTLEFS_CONFIG, mount_points));
57 uint32_t partitions[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_FS_LITTLEFS_CONFIG, partitions));
58 uint32_t block_size[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_FS_LITTLEFS_CONFIG, block_size));
59 uint32_t block_count[] = HCS_ARRAYS(HCS_NODE(DISPLAY_MISC_FS_LITTLEFS_CONFIG, block_count));
60 for (int32_t i = 0; i < num; i++) {
61 fs[i].mount_point = mount_points[i];
62 fs[i].lfs_cfg.context = partitions[i];
63 fs[i].lfs_cfg.block_size = block_size[i];
64 fs[i].lfs_cfg.block_count = block_count[i];
65
66 HDF_LOGI("%s: fs[%d] mount_point=%s, partition=%u, block_size=%u, block_count=%u",
67 __func__, i, fs[i].mount_point, (uint32_t)fs[i].lfs_cfg.context,
68 fs[i].lfs_cfg.block_size, fs[i].lfs_cfg.block_count);
69 }
70 return HDF_SUCCESS;
71 }
72 #else
FsGetResource(struct fs_cfg * fs,const struct DeviceResourceNode * resourceNode)73 static uint32_t FsGetResource(struct fs_cfg *fs, const struct DeviceResourceNode *resourceNode)
74 {
75 struct DeviceResourceIface *resource = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
76 if (resource == NULL) {
77 HDF_LOGE("Invalid DeviceResourceIface");
78 return HDF_FAILURE;
79 }
80 int32_t num = resource->GetElemNum(resourceNode, "mount_points");
81 if (num < 0 || num > LOSCFG_LFS_MAX_MOUNT_SIZE) {
82 HDF_LOGE("%s: invalid mount_points num %d", __func__, num);
83 return HDF_FAILURE;
84 }
85 for (int32_t i = 0; i < num; i++) {
86 if (resource->GetStringArrayElem(resourceNode, "mount_points",
87 i, &fs[i].mount_point, NULL) != HDF_SUCCESS) {
88 HDF_LOGE("%s: failed to get mount_points", __func__);
89 return HDF_FAILURE;
90 }
91 if (resource->GetUint32ArrayElem(resourceNode, "partitions",
92 i, (uint32_t *)&fs[i].lfs_cfg.context, 0) != HDF_SUCCESS) {
93 HDF_LOGE("%s: failed to get partitions", __func__);
94 return HDF_FAILURE;
95 }
96 if (resource->GetUint32ArrayElem(resourceNode, "block_size",
97 i, &fs[i].lfs_cfg.block_size, 0) != HDF_SUCCESS) {
98 HDF_LOGE("%s: failed to get block_size", __func__);
99 return HDF_FAILURE;
100 }
101 if (resource->GetUint32ArrayElem(resourceNode, "block_count",
102 i, &fs[i].lfs_cfg.block_count, 0) != HDF_SUCCESS) {
103 HDF_LOGE("%s: failed to get block_count", __func__);
104 return HDF_FAILURE;
105 }
106 HDF_LOGI("%s: fs[%d] mount_point=%s, partition=%u, block_size=%u, block_count=%u",
107 __func__, i,fs[i].mount_point, (uint32_t)fs[i].lfs_cfg.context,
108 fs[i].lfs_cfg.block_size, fs[i].lfs_cfg.block_count);
109 }
110 return HDF_SUCCESS;
111 }
112 #endif
FsDriverCheck(struct HdfDeviceObject * object)113 static int32_t FsDriverCheck(struct HdfDeviceObject *object)
114 {
115 HDF_LOGI("Fs Driver Init\n");
116 if (object == NULL) {
117 return HDF_FAILURE;
118 }
119 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
120 if (FsGetResource(fs) != HDF_SUCCESS) {
121 HDF_LOGE("%s: FsGetResource failed", __func__);
122 return HDF_FAILURE;
123 }
124 #else
125 if (object->property) {
126 if (FsGetResource(fs, object->property) != HDF_SUCCESS) {
127 HDF_LOGE("%s: FsGetResource failed", __func__);
128 return HDF_FAILURE;
129 }
130 }
131 #endif
132
133 if (W25x_InitSpiFlash(0, 0) != 0) {
134 HDF_LOGI("InitSpiFlash failed\n");
135 return HDF_FAILURE;
136 }
137
138 #if (ERASE_FLASH_BULK == 1)
139 W25x_BulkErase();
140 #endif
141 return HDF_SUCCESS;
142 }
143
FsDriverInit(struct HdfDeviceObject * object)144 static int32_t FsDriverInit(struct HdfDeviceObject *object)
145 {
146 if (HDF_SUCCESS != FsDriverCheck(object))
147 return HDF_FAILURE;
148
149 DIR *dir = NULL;
150 for (int i = 0; i < sizeof(fs) / sizeof(fs[0]); i++) {
151 if (fs[i].mount_point == NULL)
152 continue;
153
154 fs[i].lfs_cfg.read = LittlefsRead;
155 fs[i].lfs_cfg.prog = LittlefsProg;
156 fs[i].lfs_cfg.erase = LittlefsErase;
157 fs[i].lfs_cfg.sync = LittlefsSync;
158 fs[i].lfs_cfg.read_size = READ_SIZE;
159 fs[i].lfs_cfg.prog_size = PROG_SIZE;
160 fs[i].lfs_cfg.cache_size = CACHE_SIZE;
161 fs[i].lfs_cfg.lookahead_size = LOOKAHEAD_SIZE;
162 fs[i].lfs_cfg.block_cycles = BLOCK_CYCLES;
163
164 int ret = mount(NULL, fs[i].mount_point, "littlefs", 0, &fs[i].lfs_cfg);
165 HDF_LOGI("%s: mount fs on '%s' %s\n", __func__, fs[i].mount_point, (ret == 0) ? "succeed" : "failed");
166 if ((dir = opendir(fs[i].mount_point)) == NULL) {
167 HDF_LOGI("first time create file %s\n", fs[i].mount_point);
168 ret = mkdir(fs[i].mount_point, S_IRUSR | S_IWUSR);
169 if (ret != LOS_OK) {
170 HDF_LOGE("Mkdir failed %d\n", ret);
171 return HDF_FAILURE;
172 } else {
173 HDF_LOGI("mkdir success %d\n", ret);
174 }
175 } else {
176 HDF_LOGI("open dir success!\n");
177 closedir(dir);
178 }
179 }
180 return HDF_SUCCESS;
181 }
182
FsDriverBind(struct HdfDeviceObject * device)183 static int32_t FsDriverBind(struct HdfDeviceObject *device)
184 {
185 (void)device;
186 return HDF_SUCCESS;
187 }
188
FsDriverRelease(struct HdfDeviceObject * device)189 static void FsDriverRelease(struct HdfDeviceObject *device)
190 {
191 (void)device;
192 W25x_DeInitSpiFlash();
193 }
194
195 static struct HdfDriverEntry g_fsDriverEntry = {
196 .moduleVersion = 1,
197 .moduleName = "HDF_FS_LITTLEFS",
198 .Bind = FsDriverBind,
199 .Init = FsDriverInit,
200 .Release = FsDriverRelease,
201 };
202
203 HDF_INIT(g_fsDriverEntry);
204
205