SimpleKernel 1.17.0
Loading...
Searching...
No Matches
filesystem.cpp
Go to the documentation of this file.
1
5#include "device_manager.hpp"
6#include "device_node.hpp"
7#include "fatfs.hpp"
8#include "kernel_log.hpp"
9#include "mount.hpp"
10#include "ramfs.hpp"
11#include "vfs.hpp"
12
14static constexpr uint8_t kRootFsDriveId = 0;
15
17auto FileSystemInit() -> void {
18 // 初始化 VFS
19 auto init_result = vfs::Init();
20 if (!init_result.has_value()) {
21 klog::Err("FileSystemInit: vfs::Init failed: {}",
22 init_result.error().message());
23 return;
24 }
25
26 // 创建 ramfs 根文件系统并挂载到 "/"
27 static ramfs::RamFs root_ramfs;
28 auto mount_result = vfs::GetMountTable().Mount("/", &root_ramfs, nullptr);
29 if (!mount_result.has_value()) {
30 klog::Err("FileSystemInit: failed to mount ramfs at /: {}",
31 mount_result.error().message());
32 return;
33 }
34
35 // Mount FatFS on the first available block device at /mnt/fat
36 DeviceNode* blk_nodes[4]{};
37 const size_t blk_count = DeviceManagerSingleton::instance().FindDevicesByType(
38 DeviceType::kBlock, blk_nodes, 4);
39
40 if (blk_count > 0 && blk_nodes[0]->block_device != nullptr) {
41 auto* blk = blk_nodes[0]->block_device;
43 auto fat_mount = fat_fs.Mount(blk);
44 if (!fat_mount.has_value()) {
45 klog::Err("FileSystemInit: FatFsFileSystem::Mount failed: {}",
46 fat_mount.error().message());
47 } else {
48 auto vfs_mount = vfs::GetMountTable().Mount("/mnt/fat", &fat_fs, blk);
49 if (!vfs_mount.has_value()) {
50 klog::Err("FileSystemInit: vfs mount at /mnt/fat failed: {}",
51 vfs_mount.error().message());
52 } else {
53 klog::Info("FileSystemInit: FatFS mounted at /mnt/fat (device: {})",
54 blk->GetName());
55 }
56 }
57 }
58
59 klog::Info("FileSystemInit: complete");
60}
FatFS VFS 适配器
Definition fatfs.hpp:25
auto Mount(vfs::BlockDevice *device) -> Expected< vfs::Inode * > override
挂载 FatFS 卷
Definition fatfs.cpp:109
ramfs 文件系统实现
Definition ramfs.hpp:16
auto Mount(vfs::BlockDevice *device) -> Expected< vfs::Inode * > override
挂载 ramfs
Definition ramfs.cpp:36
@ kBlock
块设备(磁盘等)
auto FileSystemInit() -> void
文件系统子系统初始化入口
static constexpr uint8_t kRootFsDriveId
FatFS 逻辑驱动器号(对应 rootfs.img)
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto GetMountTable() -> MountTable &
获取全局挂载表实例
Definition mount.cpp:234
auto Init() -> Expected< void >
VFS 全局初始化
Definition vfs.cpp:80
单个设备的硬件资源描述。
vfs::BlockDevice * block_device