• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
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 <sys/stat.h>
18 #include <sys/types.h>
19 #include "wm_littlefs.h"
20 #include "wm_internal_flash.h"
21 #include "los_config.h"
22 #include "los_fs.h"
23 #include "hdf_log.h"
24 #include "hdf_device_desc.h"
25 #include "device_resource_if.h"
26 #include "lfs_adapter.h"
27 
28 
29 #define LFS_CFG_READ_SIZE       1
30 #define LFS_CFG_AHEAD_SIZE      16
31 #define LFS_CFG_PROG_SIZE       256
32 #define LFS_CFG_BLOCK_SIZE      256
33 #define LFS_CFG_CACHE_SIZE      1024
34 #define LFS_CFG_BLOCK_CYCLES    500
35 
36 struct fs_cfg {
37     char *mount_point;
38     struct PartitionCfg lfs_cfg;
39 };
40 
41 static struct fs_cfg fs[LOSCFG_LFS_MAX_MOUNT_SIZE] = {0};
42 
lfs_flash_block_read(int partition,UINT32 * offset,void * buffer,UINT32 size)43 static int lfs_flash_block_read(int partition, UINT32 * offset, void *buffer, UINT32 size)
44 {
45     tls_fls_read(*offset, buffer, size);
46     return 0;
47 }
48 
lfs_flash_block_prog(int partition,UINT32 * offset,void * buffer,UINT32 size)49 static int lfs_flash_block_prog(int partition, UINT32 * offset, void *buffer, UINT32 size)
50 {
51     tls_fls_write(*offset, (void *)buffer, size);
52     return 0;
53 }
54 
lfs_flash_block_erase(int partition,UINT32 offset,UINT32 size)55 static int lfs_flash_block_erase(int partition, UINT32 offset, UINT32 size)
56 {
57     tls_fls_erase(offset / INSIDE_FLS_SECTOR_SIZE);
58     return 0;
59 }
60 
FsGetResource(struct fs_cfg * fs,const struct DeviceResourceNode * resourceNode)61 static uint32_t FsGetResource(struct fs_cfg *fs, const struct DeviceResourceNode *resourceNode)
62 {
63     struct DeviceResourceIface *resource = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
64     if (resource == NULL) {
65         HDF_LOGE("Invalid DeviceResourceIface");
66         return HDF_FAILURE;
67     }
68     int32_t num = resource->GetElemNum(resourceNode, "mount_points");
69     if (num < 0 || num > LOSCFG_LFS_MAX_MOUNT_SIZE) {
70         HDF_LOGE("%s: invalid mount_points num %d", __func__, num);
71         return HDF_FAILURE;
72     }
73     for (int32_t i = 0; i < num; i++) {
74         if (resource->GetStringArrayElem(resourceNode, "mount_points", i,
75             &fs[i].mount_point, NULL) != HDF_SUCCESS) {
76             HDF_LOGE("%s: failed to get mount_points", __func__);
77             return HDF_FAILURE;
78         }
79         if (resource->GetUint32ArrayElem(resourceNode, "block_start_positions", i,
80             (uint32_t *)&fs[i].lfs_cfg.partNo, 0) != HDF_SUCCESS) {
81             HDF_LOGE("%s: failed to get partitions", __func__);
82             return HDF_FAILURE;
83         }
84         if (resource->GetUint32ArrayElem(resourceNode, "block_size", i,
85             &fs[i].lfs_cfg.blockSize, 0) != HDF_SUCCESS) {
86             HDF_LOGE("%s: failed to get block_size", __func__);
87             return HDF_FAILURE;
88         }
89         if (resource->GetUint32ArrayElem(resourceNode, "block_count", i,
90             &fs[i].lfs_cfg.blockCount, 0) != HDF_SUCCESS) {
91             HDF_LOGE("%s: failed to get block_count", __func__);
92             return HDF_FAILURE;
93         }
94         HDF_LOGD("%s: fs[%d] mount_point=%s, partition=%u, block_size=%u, block_count=%u", __func__, i,
95                  fs[i].mount_point, (uint32_t)fs[i].lfs_cfg.partNo,
96                  fs[i].lfs_cfg.blockSize, fs[i].lfs_cfg.blockCount);
97     }
98     return HDF_SUCCESS;
99 }
100 
FsDriverInit(struct HdfDeviceObject * object)101 static int32_t FsDriverInit(struct HdfDeviceObject *object)
102 {
103     int ret = HDF_FAILURE;
104     int lfs_space[1] = {0};
105     int lfs_addr[1] = {0};
106     if (object == NULL) {
107         return HDF_FAILURE;
108     }
109     if (object->property) {
110         if (FsGetResource(fs, object->property) != HDF_SUCCESS) {
111             HDF_LOGE("%s: FsGetResource failed", __func__);
112             return HDF_FAILURE;
113         }
114     }
115 
116     for (int i = 0; i < sizeof(fs) / sizeof(fs[0]); i++) {
117         if (fs[i].mount_point == NULL)
118             continue;
119 
120         fs[i].lfs_cfg.readFunc  = lfs_flash_block_read;
121         fs[i].lfs_cfg.writeFunc  = lfs_flash_block_prog;
122         fs[i].lfs_cfg.eraseFunc = lfs_flash_block_erase;
123 
124         fs[i].lfs_cfg.readSize = LFS_CFG_READ_SIZE;
125         fs[i].lfs_cfg.writeSize = LFS_CFG_PROG_SIZE;
126         fs[i].lfs_cfg.cacheSize = LFS_CFG_CACHE_SIZE;
127         fs[i].lfs_cfg.lookaheadSize = LFS_CFG_AHEAD_SIZE;
128         fs[i].lfs_cfg.blockCycles = LFS_CFG_BLOCK_CYCLES;
129 
130         lfs_space[0] = fs[i].lfs_cfg.blockSize * fs[i].lfs_cfg.blockCount;
131         lfs_addr[0] = fs[i].lfs_cfg.partNo * fs[i].lfs_cfg.blockSize + INSIDE_FLS_BASE_ADDR;
132         fs[i].lfs_cfg.partNo = 0;
133         ret = LOS_DiskPartition("lfs_dev0", "littlefs", &lfs_space, &lfs_addr, 1);
134         HDF_LOGI("%s: DiskPartition %s\n", __func__, (ret == 0) ? "succeed" : "failed");
135         if (ret != 0) {
136             return -1;
137         }
138 
139         ret = mount(NULL, fs[i].mount_point, "littlefs", 0, &fs[i].lfs_cfg);
140         HDF_LOGI("FsDriverInit mount ret %d\n", ret);
141         if (!ret) {
142             ret = mkdir(fs[i].mount_point, S_IRUSR | S_IWUSR | S_IXUSR);
143             HDF_LOGI("%s: mkdir %s %s\n", __func__, fs[i].mount_point, (ret == 0) ? "succeed" : "failed");
144         }
145         if (ret) {
146             break;
147         }
148     }
149     HDF_LOGI("%s: mount littlefs %s\n", __func__, (ret == 0) ? "succeed" : "failed");
150     return (ret == 0) ? HDF_SUCCESS : HDF_FAILURE;
151 }
152 
FsDriverBind(struct HdfDeviceObject * device)153 static int32_t FsDriverBind(struct HdfDeviceObject *device)
154 {
155     (void)device;
156     return HDF_SUCCESS;
157 }
158 
FsDriverRelease(struct HdfDeviceObject * device)159 static void FsDriverRelease(struct HdfDeviceObject *device)
160 {
161     (void)device;
162 }
163 
164 static struct HdfDriverEntry g_fsDriverEntry = {
165     .moduleVersion = 1,
166     .moduleName = "W800_FS_LITTLEFS",
167     .Bind = FsDriverBind,
168     .Init = FsDriverInit,
169     .Release = FsDriverRelease,
170 };
171 
172 HDF_INIT(g_fsDriverEntry);