Skip to content

Commit

Permalink
Merge pull request #139 from Harthann/138-diskio-interface-creation
Browse files Browse the repository at this point in the history
138 diskio interface creation
  • Loading branch information
0x050f authored Feb 8, 2024
2 parents 263417d + e0f7579 commit 437812d
Show file tree
Hide file tree
Showing 11 changed files with 646 additions and 455 deletions.
39 changes: 21 additions & 18 deletions srcs/cli/commands/debugfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::utils::path::Path;
pub static ROOT_INODE: usize = 2;
pub static CURRENTDIR_INODE: Mutex<usize> = Mutex::new(ROOT_INODE);
pub static PWD: Mutex<Option<Path>> = Mutex::new(None);
pub static DISKNO: Mutex<i8> = Mutex::new(-1);
pub static DISKNO: Mutex<Option<ext2::Ext2>> = Mutex::new(None);

fn help() {
crate::kprintln!(
Expand Down Expand Up @@ -55,7 +55,7 @@ fn rm(command: Vec<String>) {
return;
}
ext2::remove_file(
*DISKNO.lock() as u8,
DISKNO.lock().as_mut().unwrap(),
command[1].as_str(),
*CURRENTDIR_INODE.lock()
);
Expand All @@ -67,7 +67,7 @@ fn stat(command: Vec<String>) {
return;
}
ext2::show_inode_info(
*DISKNO.lock() as u8,
DISKNO.lock().as_ref().unwrap(),
command[1].as_str(),
*CURRENTDIR_INODE.lock()
);
Expand All @@ -79,7 +79,7 @@ fn mkdir(command: Vec<String>) {
return;
}
ext2::create_dir(
*DISKNO.lock() as u8,
DISKNO.lock().as_mut().unwrap(),
command[1].as_str(),
*CURRENTDIR_INODE.lock()
);
Expand All @@ -91,7 +91,7 @@ fn touch(command: Vec<String>) {
return;
}
ext2::create_file(
*DISKNO.lock() as u8,
DISKNO.lock().as_mut().unwrap(),
command[1].as_str(),
*CURRENTDIR_INODE.lock()
);
Expand All @@ -103,7 +103,7 @@ fn cat(command: Vec<String>) {
return;
}
let file_content = ext2::get_file_content(
*DISKNO.lock() as u8,
DISKNO.lock().as_ref().unwrap(),
command[1].as_str(),
*CURRENTDIR_INODE.lock()
);
Expand All @@ -113,13 +113,13 @@ fn cat(command: Vec<String>) {
}

fn test() {
let mut ext2 = ext2::Ext2::new(*DISKNO.lock() as u8)
.expect("Disk is not a ext2 filesystem.");
// let binding = DISKNO.lock();
// let ext2 = binding.as_ref().unwrap();
// let mut dentry = crate::fs::ext2::inode::Dentry::default();

let _node = ext2.alloc_node(0);
let _block = ext2.alloc_block(0);
crate::dprintln!("Node {}", _node);
//
// let _node = ext2.alloc_node(0);
// let _block = ext2.alloc_block(0);
// crate::dprintln!("Node {}", _node);
}

fn ls(command: Vec<String>) {
Expand All @@ -128,8 +128,11 @@ fn ls(command: Vec<String>) {
_ => command[1].as_str()
};
crate::dprintln!("Ls: {}", path);
let dentries =
ext2::list_dir(*DISKNO.lock() as u8, path, *CURRENTDIR_INODE.lock());
let dentries = ext2::list_dir(
DISKNO.lock().as_ref().unwrap(),
path,
*CURRENTDIR_INODE.lock()
);

for i in dentries {
crate::kprint!("{} ", i.name);
Expand All @@ -143,8 +146,8 @@ fn cd(command: Vec<String>) {
_ => command[1].as_str()
};
let path = Path::new(path);
let ext2 = ext2::Ext2::new(*DISKNO.lock() as u8)
.expect("Disk is not a ext2 filesystem.");
let binding = DISKNO.lock();
let ext2 = binding.as_ref().unwrap();
let lookup = ext2.recurs_find(path.as_str(), *CURRENTDIR_INODE.lock());
match lookup {
None => crate::kprintln!("Dir not found"),
Expand Down Expand Up @@ -174,8 +177,8 @@ fn imap(command: Vec<String>) {
1 => "/",
_ => command[1].as_str()
};
let ext2 = ext2::Ext2::new(*DISKNO.lock() as u8)
.expect("Disk is not a ext2 filesystem.");
let binding = DISKNO.lock();
let ext2 = binding.as_ref().unwrap();
let lookup = ext2.get_inode_of(path);
match lookup {
None => crate::kprintln!("File not found"),
Expand Down
34 changes: 34 additions & 0 deletions srcs/disk/ide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::DiskIO;
use crate::pci::ide::IDEDevice;

pub struct IDEDisk {
diskno: u8,
device: IDEDevice
}

unsafe impl Send for IDEDisk {}

impl IDEDisk {
pub const fn new(diskno: u8, device: IDEDevice) -> Self {
Self { diskno, device }
}
}

impl DiskIO for IDEDisk {
fn read_sectors(&self, numsects: u8, lba: u32, edi: u32) -> Result<(), u8> {
self.device.read_sectors(numsects, lba, edi)
}

fn write_sectors(
&mut self,
numsects: u8,
lba: u32,
edi: u32
) -> Result<(), u8> {
self.device.write_sectors(numsects, lba, edi)
}

fn sector_size(&self) -> usize {
self.device.sector_size() as usize
}
}
74 changes: 74 additions & 0 deletions srcs/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::alloc::boxed::Box;
use crate::alloc::vec::Vec;
use crate::pci::ide::IDE;

pub mod ide;
use ide::IDEDisk;

pub trait DiskIO {
fn read_sectors(&self, numsects: u8, lba: u32, edi: u32) -> Result<(), u8>;

fn write_sectors(
&mut self,
numsects: u8,
lba: u32,
edi: u32
) -> Result<(), u8>;

fn sector_size(&self) -> usize;
}

pub fn discover() -> Vec<Box<dyn DiskIO + Send>> {
let mut found_disks = Vec::<Box<dyn DiskIO + Send>>::new();

// Discover IDE disks
let binding = IDE.lock();
for i in 0..4 {
let disk = binding.get_device(i);
match disk {
Some(x) => {
let idedisk = IDEDisk::new(i as u8, x.clone());
let diskio = Box::new(idedisk);
found_disks.push(diskio);
},
None => {}
}
}

found_disks
}

#[cfg(test)]
mod test {

use super::ide::IDEDisk;
use super::DiskIO;
use crate::alloc::vec;
use crate::{sys_macros, IDE};

#[sys_macros::test_case]
fn idedisk_read_write_sector() {
let to_write = vec!['C' as u8; 512];
let read_from = vec![0x0 as u8; 512];

let device = IDE.lock().get_device(1).unwrap().clone();
let mut idedisk = IDEDisk::new(1, device);
let _ = idedisk.write_sectors(1, 0x0, to_write.as_ptr() as u32);
let _ = idedisk.read_sectors(1, 0x0, read_from.as_ptr() as u32);

assert_eq!(to_write, read_from);
}

#[sys_macros::test_case]
fn idedisk_read_write_multiple_sectors() {
let to_write = vec!['D' as u8; 1024];
let read_from = vec![0x0 as u8; 1024];

let device = IDE.lock().get_device(1).unwrap().clone();
let mut idedisk = IDEDisk::new(1, device);
let _ = idedisk.write_sectors(2, 0x0, to_write.as_ptr() as u32);
let _ = idedisk.read_sectors(2, 0x0, read_from.as_ptr() as u32);

assert_eq!(to_write, read_from);
}
}
Loading

0 comments on commit 437812d

Please sign in to comment.