• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
48     INT32 lengthArray = halPartitionsInfo[FLASH_PARTITION_DATA0].partitionLength;
49     INT32 addrArray = halPartitionsInfo[FLASH_PARTITION_DATA0].partitionStartAddr;
50     ret = LOS_DiskPartition("flash0", "littlefs", &lengthArray, &addrArray, 1);
51     printf("%s: DiskPartition %s\n", __func__, (ret == 0) ? "succeed" : "failed");
52     if (ret != 0) {
53         return -1;
54     }
55 
56     fs[0].mount_point = "/littlefs";
57     fs[0].partCfg.partNo = FLASH_PARTITION_DATA0;
58     fs[0].partCfg.blockSize = 4096; /* 4096, lfs block size */
59     fs[0].partCfg.blockCount = 2048; /* 2048, lfs block count */
60     fs[0].partCfg.readFunc = virt_flash_read;
61     fs[0].partCfg.writeFunc = virt_flash_write;
62     fs[0].partCfg.eraseFunc = virt_flash_erase;
63 
64     fs[0].partCfg.readSize = 256; /* 256, lfs read size */
65     fs[0].partCfg.writeSize = 256; /* 256, lfs prog size */
66     fs[0].partCfg.cacheSize = 256; /* 256, lfs cache size */
67     fs[0].partCfg.lookaheadSize = 16; /* 16, lfs lookahead size */
68     fs[0].partCfg.blockCycles = 1000; /* 1000, lfs block cycles */
69 
70     ret = LOS_PartitionFormat("flash0", "littlefs", &fs[0].partCfg);
71     printf("%s: PartitionFormat %s\n", __func__, (ret == 0) ? "succeed" : "failed");
72     if (ret != 0) {
73         return -1;
74     }
75 
76     ret = mount(NULL, fs[0].mount_point, "littlefs", 0, &fs[0].partCfg);
77     printf("%s: mount fs on '%s' %s\n", __func__, fs[0].mount_point, (ret == 0) ? "succeed" : "failed");
78     if (ret != 0) {
79         return -1;
80     }
81 
82     ret = mkdir(fs[0].mount_point, 0777); /* 0777, set dir permissions */
83     printf("%s: mkdir '%s' %s\n", __func__, fs[0].mount_point, (ret == 0) ? "succeed" : "failed");
84     if (ret != 0) {
85         return -1;
86     }
87 
88     return 0;
89 }
90 #endif
91