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 "los_config.h"
32 #include "ram_virt_flash.h"
33 #include "los_fs.h"
34
35 #if (LOSCFG_SUPPORT_LITTLEFS == 1)
36
37 struct fs_cfg {
38 CHAR *mount_point;
39 struct PartitionCfg partCfg;
40 };
41
LfsLowLevelInit()42 INT32 LfsLowLevelInit()
43 {
44 INT32 ret;
45 struct fs_cfg fs[LOSCFG_LFS_MAX_MOUNT_SIZE] = {0};
46 HalLogicPartition *halPartitionsInfo = getPartitionInfo();
47 if (halPartitionsInfo == NULL) {
48 printf("%s: getPartitionInfo failed!\n", __func__);
49 return -1;
50 }
51
52 INT32 lengthArray = halPartitionsInfo[FLASH_PARTITION_DATA0].partitionLength;
53 INT32 addrArray = halPartitionsInfo[FLASH_PARTITION_DATA0].partitionStartAddr;
54 ret = LOS_DiskPartition("flash0", "littlefs", &lengthArray, &addrArray, 1);
55 printf("%s: DiskPartition %s\n", __func__, (ret == 0) ? "succeed" : "failed");
56 if (ret != 0) {
57 return -1;
58 }
59
60 fs[0].mount_point = "/littlefs";
61 fs[0].partCfg.partNo = FLASH_PARTITION_DATA0;
62 fs[0].partCfg.blockSize = 4096; /* 4096, lfs block size */
63 fs[0].partCfg.blockCount = 2048; /* 2048, lfs block count */
64 fs[0].partCfg.readFunc = virt_flash_read;
65 fs[0].partCfg.writeFunc = virt_flash_write;
66 fs[0].partCfg.eraseFunc = virt_flash_erase;
67
68 fs[0].partCfg.readSize = 256; /* 256, lfs read size */
69 fs[0].partCfg.writeSize = 256; /* 256, lfs prog size */
70 fs[0].partCfg.cacheSize = 256; /* 256, lfs cache size */
71 fs[0].partCfg.lookaheadSize = 16; /* 16, lfs lookahead size */
72 fs[0].partCfg.blockCycles = 1000; /* 1000, lfs block cycles */
73
74 ret = LOS_PartitionFormat("flash0", "littlefs", &fs[0].partCfg);
75 printf("%s: PartitionFormat %s\n", __func__, (ret == 0) ? "succeed" : "failed");
76 if (ret != 0) {
77 return -1;
78 }
79
80 ret = mount(NULL, fs[0].mount_point, "littlefs", 0, &fs[0].partCfg);
81 printf("%s: mount fs on '%s' %s\n", __func__, fs[0].mount_point, (ret == 0) ? "succeed" : "failed");
82 if (ret != 0) {
83 return -1;
84 }
85
86 ret = mkdir(fs[0].mount_point, 0777); /* 0777, set dir permissions */
87 printf("%s: mkdir '%s' %s\n", __func__, fs[0].mount_point, (ret == 0) ? "succeed" : "failed");
88 if (ret != 0) {
89 return -1;
90 }
91
92 return 0;
93 }
94 #endif
95