Skip to content

Commit 5978691

Browse files
authored
feat(filesystem): add init in memory fatfs (#568)
* add Debug for fatfs Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix error Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * add init in memory fatfs Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi <koichi.imai.2@tier4.jp> * fix Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> --------- Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> Signed-off-by: Koichi <koichi.imai.2@tier4.jp>
1 parent c739a2b commit 5978691

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

awkernel_lib/src/allocator.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg(feature = "std")]
2+
pub use std::alloc::System;
3+
4+
#[cfg(not(feature = "std"))]
5+
pub use super::heap::TALLOC as System;

awkernel_lib/src/file/fatfs.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,73 @@ pub mod file;
66
pub mod fs;
77
pub mod table;
88
pub mod time;
9+
10+
use crate::{
11+
allocator::System,
12+
file::{
13+
fatfs::{
14+
fs::{format_volume, FileSystem, FormatVolumeOptions, FsOptions, LossyOemCpConverter},
15+
time::NullTimeProvider,
16+
},
17+
memfs::InMemoryDisk,
18+
},
19+
paging::PAGESIZE,
20+
sync::rwlock::RwLock,
21+
};
22+
23+
use alloc::{format, string::String, sync::Arc, vec::Vec};
24+
use core::alloc::{GlobalAlloc, Layout};
25+
26+
pub const MEMORY_FILESYSTEM_SIZE: usize = 1024 * 1024;
27+
28+
static FAT_FS_INSTANCE: RwLock<
29+
Option<Arc<FileSystem<InMemoryDisk, NullTimeProvider, LossyOemCpConverter>>>,
30+
> = RwLock::new(None);
31+
32+
pub fn init_memory_fatfs() -> Result<(), String> {
33+
let mut fs_guard = FAT_FS_INSTANCE.write();
34+
if fs_guard.is_some() {
35+
return Err("FAT filesystem has already been initialized.".into());
36+
}
37+
38+
let disk_layout = Layout::from_size_align(MEMORY_FILESYSTEM_SIZE, PAGESIZE)
39+
.map_err(|_| "Invalid layout for memory filesystem allocation.")?;
40+
41+
let raw_disk_memory = unsafe { System.alloc(disk_layout) };
42+
if raw_disk_memory.is_null() {
43+
return Err("Failed to allocate memory for the in-memory disk.".into());
44+
}
45+
46+
let disk_data = unsafe {
47+
Vec::from_raw_parts(
48+
raw_disk_memory,
49+
MEMORY_FILESYSTEM_SIZE,
50+
MEMORY_FILESYSTEM_SIZE,
51+
)
52+
};
53+
54+
let mut in_memory_disk = InMemoryDisk::new(disk_data, 0);
55+
56+
if let Err(e) = format_volume(&mut in_memory_disk, FormatVolumeOptions::new()) {
57+
return Err(format!("Failed to format FAT volume: {e:?}"));
58+
}
59+
60+
let file_system = match FileSystem::new(in_memory_disk, FsOptions::new()) {
61+
Ok(fs) => fs,
62+
Err(e) => {
63+
return Err(format!("Failed to create FileSystem instance: {e:?}"));
64+
}
65+
};
66+
67+
*fs_guard = Some(Arc::new(file_system));
68+
69+
Ok(())
70+
}
71+
72+
pub fn get_memory_fatfs() -> Arc<FileSystem<InMemoryDisk, NullTimeProvider, LossyOemCpConverter>> {
73+
let fs_guard = FAT_FS_INSTANCE.read();
74+
75+
(*fs_guard)
76+
.clone()
77+
.expect("FAT filesystem has not been initialized. Call init_fatfs() first.")
78+
}

awkernel_lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use core::{cell::Cell, marker::PhantomData};
88
use alloc::rc::Rc;
99

1010
pub mod addr;
11+
pub mod allocator;
1112
pub mod arch;
1213
pub mod config;
1314
pub mod console;

0 commit comments

Comments
 (0)