Skip to content

Commit

Permalink
fatfs on ramdisk
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyukai0403 committed Jan 25, 2025
1 parent 586f40b commit dc6c5d0
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 9 deletions.
28 changes: 19 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ spin = "0.9.8"
x2apic = "0.4.3"
x86_64 = "0.15.2"
os-terminal = "0.5.8"
fatfs = { git = "https://github.com/rafalh/rust-fatfs", rev = "c4bb76929eb115f228720631b4110f827b998653", default-features = false, features = ["alloc", "lfn"] }
Empty file added kernel/src/blockdev/ahci.rs
Empty file.
2 changes: 2 additions & 0 deletions kernel/src/blockdev/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ramdisk;
pub mod ahci;
117 changes: 117 additions & 0 deletions kernel/src/blockdev/ramdisk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use alloc::alloc::{alloc, dealloc, Layout};
use crate::println;
use fatfs::{format_volume, FormatVolumeOptions, FileSystem, FsOptions, IoBase, Read, Write, Seek, Error, SeekFrom};

pub struct RamDisk {
size_in_blocks: usize,
layout: Layout,
ptr: *mut u8,
cur_pos: usize,
}

impl RamDisk {
pub fn new(size_in_blocks: usize) -> Self {
unsafe {
let layout = Layout::from_size_align(size_in_blocks * 512, 4).unwrap();
let allocated = alloc(layout);
Self {
size_in_blocks,
layout,
ptr: allocated,
cur_pos: 0,
}
}
}
}

impl Drop for RamDisk {
fn drop(&mut self) {
unsafe {
// println!("{:#?} {:?}", self.layout, self.ptr);
dealloc(self.ptr, self.layout);
}
}
}

impl IoBase for RamDisk {
type Error = Error<()>;
}

impl Read for RamDisk {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let will_read = if buf.len() < 512 { buf.len() } else { 512 };
if self.cur_pos + will_read < self.size_in_blocks * 512 {
unsafe {
core::ptr::copy(self.ptr.add(self.cur_pos), buf.as_mut_ptr(), will_read);
}
self.cur_pos += will_read;
Ok(will_read)
} else {
Err(Error::Io(()))
}
}
}

impl Write for RamDisk {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let will_write = if buf.len() < 512 { buf.len() } else { 512 };
if self.cur_pos <= self.size_in_blocks * 512 {
unsafe {
core::ptr::copy(buf.as_ptr(), self.ptr.add(self.cur_pos), will_write);
}
self.cur_pos += will_write;
Ok(will_write)
} else {
Err(Error::Io(()))
}
}

fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}

impl Seek for RamDisk {
fn seek(&mut self, frm: SeekFrom) -> Result<u64, Self::Error> {
let new_pos: i64;
match frm {
SeekFrom::Start(offset) => {
new_pos = offset as i64;
},
SeekFrom::End(offset) => {
new_pos = (self.size_in_blocks * 512) as i64 + offset;
},
SeekFrom::Current(offset) => {
new_pos = self.cur_pos as i64 + offset;
}
}
if new_pos < 0 || new_pos > (self.size_in_blocks * 512) as i64 {
Err(Error::Io(()))
} else {
self.cur_pos = new_pos as usize;
Ok(self.cur_pos as u64)
}
}
}

pub fn test() {
let mut ramdisk = RamDisk::new(128);
format_volume(&mut ramdisk, FormatVolumeOptions::new()).expect("format volume failed");
let fs = FileSystem::new(ramdisk, FsOptions::new()).expect("create fs failed");
let root_dir = fs.root_dir();
macro_rules! file_content {
($name:expr, $content:expr) => {
{
let mut file = root_dir.create_file($name).expect("cre");
file.write_all($content);
}
}
}
file_content!("zzjrabbit.txt", b"Hello, FAT!");
file_content!("text.txt", b"ADG");
for f in root_dir.iter() {
let e = f.unwrap();
println!("Name: {}, Size: {}", e.file_name(), e.len());
}
// println!("{:#?}", root_dir);
}
2 changes: 2 additions & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![feature(abi_x86_interrupt)]
#![feature(slice_take)]
#![feature(array_ptr_get)]

extern crate alloc;
pub mod acpi;
Expand All @@ -10,3 +11,4 @@ pub mod cpu;
pub mod int;
pub mod mm;
pub mod pcie;
pub mod blockdev;
2 changes: 2 additions & 0 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ extern "C" fn kmain() -> ! {
init_ioapic(parse_madt());
show_cpu_info();
doit();
println!("\x1b[31mCOLOR\x1b[0m");
DoglinkOS_2nd::blockdev::ramdisk::test();
hang();
}

Expand Down

0 comments on commit dc6c5d0

Please sign in to comment.