1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-12-13     balanceTWK   add sdcard port file
9  * 2021-05-10     Meco Man     fix a bug that cannot use fatfs in the main thread at starting up
10  * 2021-07-28     Meco Man     implement romfs as the root filesystem
11  */
12 
13 #include <rtthread.h>
14 #include <dfs_romfs.h>
15 #include <dfs_fs.h>
16 #include <dfs_file.h>
17 
18 #if DFS_FILESYSTEMS_MAX < 4
19 #error "Please define DFS_FILESYSTEMS_MAX more than 4"
20 #endif
21 #if DFS_FILESYSTEM_TYPES_MAX < 4
22 #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
23 #endif
24 
25 #define DBG_TAG "app.filesystem"
26 #define DBG_LVL DBG_INFO
27 #include <rtdbg.h>
28 
29 #ifdef BSP_USING_SDCARD_FATFS
onboard_sdcard_mount(void)30 static int onboard_sdcard_mount(void)
31 {
32     if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
33     {
34         LOG_I("SD card mount to '/sdcard'");
35     }
36     else
37     {
38         LOG_E("SD card mount to '/sdcard' failed!");
39     }
40 
41     return RT_EOK;
42 }
43 #endif /* BSP_USING_SDCARD_FATFS */
44 
45 #ifdef BSP_USING_SPI_FLASH_LITTLEFS
46 #include <fal.h>
47 #define FS_PARTITION_NAME              "spiflash0"
48 
onboard_spiflash_mount(void)49 static int onboard_spiflash_mount(void)
50 {
51     struct rt_device *mtd_dev = RT_NULL;
52 
53     fal_init();
54 
55     mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
56     if (!mtd_dev)
57     {
58         LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
59     }
60 
61     if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
62     {
63         LOG_I("spi flash mount to '/spiflash'");
64     }
65     else
66     {
67         dfs_mkfs("lfs", FS_PARTITION_NAME);
68         if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
69         {
70             LOG_I("spi flash mount to '/spiflash'");
71         }
72         else
73         {
74             LOG_E("spi flash failed to mount to '/spiflash'");
75         }
76     }
77 
78     return RT_EOK;
79 }
80 #endif /* BSP_USING_SPI_FLASH_LITTLEFS */
81 
82 static const struct romfs_dirent _romfs_root[] =
83 {
84 #ifdef BSP_USING_SDCARD_FATFS
85     {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
86 #endif
87 
88 #ifdef BSP_USING_SPI_FLASH_LITTLEFS
89     {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
90 #endif
91 };
92 
93 static const struct romfs_dirent romfs_root =
94 {
95     ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
96 };
97 
filesystem_mount(void)98 static int filesystem_mount(void)
99 {
100     if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
101     {
102         LOG_E("rom mount to '/' failed!");
103     }
104 #ifdef BSP_USING_SDCARD_FATFS
105     onboard_sdcard_mount();
106 #endif
107 
108 #ifdef BSP_USING_SPI_FLASH_LITTLEFS
109     onboard_spiflash_mount();
110 #endif
111 
112     return RT_EOK;
113 }
114 INIT_APP_EXPORT(filesystem_mount);
115