Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions awkernel_lib/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(feature = "std")]
pub use std::alloc::System;

#[cfg(not(feature = "std"))]
pub use super::heap::TALLOC as System;
70 changes: 70 additions & 0 deletions awkernel_lib/src/file/fatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,73 @@ pub mod file;
pub mod fs;
pub mod table;
pub mod time;

use crate::{
allocator::System,
file::{
fatfs::{
fs::{format_volume, FileSystem, FormatVolumeOptions, FsOptions, LossyOemCpConverter},
time::NullTimeProvider,
},
memfs::InMemoryDisk,
},
paging::PAGESIZE,
sync::rwlock::RwLock,
};

use alloc::{format, string::String, sync::Arc, vec::Vec};
use core::alloc::{GlobalAlloc, Layout};

pub const MEMORY_FILESYSTEM_SIZE: usize = 1024 * 1024;

static FAT_FS_INSTANCE: RwLock<
Option<Arc<FileSystem<InMemoryDisk, NullTimeProvider, LossyOemCpConverter>>>,
> = RwLock::new(None);

pub fn init_memory_fatfs() -> Result<(), String> {
let mut fs_guard = FAT_FS_INSTANCE.write();
if fs_guard.is_some() {
return Err("FAT filesystem has already been initialized.".into());
}

let disk_layout = Layout::from_size_align(MEMORY_FILESYSTEM_SIZE, PAGESIZE)
.map_err(|_| "Invalid layout for memory filesystem allocation.")?;

let raw_disk_memory = unsafe { System.alloc(disk_layout) };
if raw_disk_memory.is_null() {
return Err("Failed to allocate memory for the in-memory disk.".into());
}

let disk_data = unsafe {
Vec::from_raw_parts(
raw_disk_memory,
MEMORY_FILESYSTEM_SIZE,
MEMORY_FILESYSTEM_SIZE,
)
};

let mut in_memory_disk = InMemoryDisk::new(disk_data, 0);

if let Err(e) = format_volume(&mut in_memory_disk, FormatVolumeOptions::new()) {
return Err(format!("Failed to format FAT volume: {e:?}"));
}

let file_system = match FileSystem::new(in_memory_disk, FsOptions::new()) {
Ok(fs) => fs,
Err(e) => {
return Err(format!("Failed to create FileSystem instance: {e:?}"));
}
};

*fs_guard = Some(Arc::new(file_system));

Ok(())
}

pub fn get_memory_fatfs() -> Arc<FileSystem<InMemoryDisk, NullTimeProvider, LossyOemCpConverter>> {
let fs_guard = FAT_FS_INSTANCE.read();

(*fs_guard)
.clone()
.expect("FAT filesystem has not been initialized. Call init_fatfs() first.")
}
1 change: 1 addition & 0 deletions awkernel_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::{cell::Cell, marker::PhantomData};
use alloc::rc::Rc;

pub mod addr;
pub mod allocator;
pub mod arch;
pub mod config;
pub mod console;
Expand Down