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_FS_AUTO_MOUNT
30 #ifdef BSP_USING_SDCARD_FATFS
onboard_sdcard_mount(void)31 static int onboard_sdcard_mount(void)
32 {
33 if (dfs_mount("sd", "/sdcard", "elm", 0, 0) == RT_EOK)
34 {
35 LOG_I("SD card mount to '/sdcard'");
36 }
37 else
38 {
39 LOG_E("SD card mount to '/sdcard' failed!");
40 }
41
42 return RT_EOK;
43 }
44 #endif /* BSP_USING_SDCARD_FATFS */
45 #endif /* BSP_USING_FS_AUTO_MOUNT */
46
47 #ifdef BSP_USING_FLASH_FS_AUTO_MOUNT
48 #ifdef BSP_USING_FLASH_FATFS
49 #define FS_PARTITION_NAME "filesystem"
50
onboard_fal_mount(void)51 static int onboard_fal_mount(void)
52 {
53 /* 初始化 fal 功能 */
54 extern int fal_init(void);
55 extern struct rt_device *fal_blk_device_create(const char *parition_name);
56 fal_init();
57 /* 在 spi flash 中名为 "filesystem" 的分区上创建一个块设备 */
58 struct rt_device *flash_dev = fal_blk_device_create(FS_PARTITION_NAME);
59 if (flash_dev == NULL)
60 {
61 LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
62 }
63 else
64 {
65 LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
66 }
67
68 /* 挂载 spi flash 中名为 "filesystem" 的分区上的文件系统 */
69 if (dfs_mount(flash_dev->parent.name, "/fal", "elm", 0, 0) == 0)
70 {
71 LOG_I("Filesystem initialized!");
72 }
73 else
74 {
75 LOG_E("Failed to initialize filesystem!");
76 LOG_D("You should create a filesystem on the block device first!");
77 }
78
79 return RT_EOK;
80 }
81 #endif /*BSP_USING_FLASH_FATFS*/
82 #endif /*BSP_USING_FLASH_FS_AUTO_MOUNT*/
83
84
85 const struct romfs_dirent _romfs_root[] =
86 {
87 #ifdef BSP_USING_SDCARD_FATFS
88 {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
89 #endif
90
91 #ifdef BSP_USING_FLASH_FATFS
92 {ROMFS_DIRENT_DIR, "fal", RT_NULL, 0},
93 #endif
94 };
95
96 const struct romfs_dirent romfs_root =
97 {
98 ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
99 };
100
filesystem_mount(void)101 static int filesystem_mount(void)
102 {
103
104 #ifdef BSP_USING_FS
105 if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
106 {
107 LOG_E("rom mount to '/' failed!");
108 }
109
110 /* 确保块设备注册成功之后再挂载文件系统 */
111 rt_thread_delay(500);
112 #endif
113 #ifdef BSP_USING_FS_AUTO_MOUNT
114 onboard_sdcard_mount();
115 #endif /* BSP_USING_FS_AUTO_MOUNT */
116
117 #ifdef BSP_USING_FLASH_FS_AUTO_MOUNT
118 onboard_fal_mount();
119 #endif
120
121 return RT_EOK;
122 }
123 INIT_APP_EXPORT(filesystem_mount);
124