From 446aff49ef4a4a415565875a5639cfd24d40632e Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Sun, 25 Dec 2022 15:38:45 +0100 Subject: [PATCH] add ISO read Signed-off-by: Julien CLEMENT --- src/fd/mod.rs | 2 +- src/fs/iso/fd.rs | 40 ++++++++++++++++++++++++++++++++++------ src/fs/iso/iso9660.rs | 2 +- src/fs/iso/mod.rs | 19 ++++++++++--------- src/lib.rs | 6 +++++- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/fd/mod.rs b/src/fd/mod.rs index 7aaec5e..c79edca 100644 --- a/src/fd/mod.rs +++ b/src/fd/mod.rs @@ -47,5 +47,5 @@ impl FDTable { pub trait FileDescriptor { fn get_fd(&self) -> FDId; async fn write(&mut self, buf: &[u8], count: usize) -> isize; - async fn read(&mut self, buf: &[u8], count: usize) -> isize; + async fn read(&mut self, buf: &mut [u8], count: usize) -> isize; } diff --git a/src/fs/iso/fd.rs b/src/fs/iso/fd.rs index 1134950..0b0a2a4 100644 --- a/src/fs/iso/fd.rs +++ b/src/fs/iso/fd.rs @@ -1,6 +1,8 @@ +use crate::drivers::atapi::read_block; use crate::fd::{FDId, FDt, FileDescriptor}; use crate::println; -use crate::utils::mutex::AsyncMutex; + +use super::iso9660::{IsoDir, ISO_BLOCK_SIZE}; use alloc::{boxed::Box, sync::Arc}; use async_trait::async_trait; @@ -8,11 +10,19 @@ use core::cell::RefCell; pub struct IsoFD { pub fd: FDId, + offset: u32, + lba: u32, + size: u32 } impl IsoFD { - pub fn new() -> FDt { - Arc::new(RefCell::new(IsoFD { fd: FDId::new() })) + pub fn new(entry: &IsoDir) -> FDt { + Arc::new(RefCell::new(IsoFD { + fd: FDId::new(), + offset: 0, + lba: entry.data_blk.le, + size: entry.file_size.le, + })) } } @@ -26,8 +36,26 @@ impl FileDescriptor for IsoFD { 0 } - async fn read(&mut self, buf: &[u8], count: usize) -> isize { - println!("Read from fd"); - 0 + #[allow(unaligned_references)] + async fn read(&mut self, buf: &mut [u8], count: usize) -> isize { + let mut block_offset = self.offset / ISO_BLOCK_SIZE; + let mut content = read_block(self.lba + block_offset).await; + let mut read: isize = 0; + for _ in 0..count { + if self.offset >= self.size { + break; + } + + buf[read as usize] = content[(self.offset % ISO_BLOCK_SIZE) as usize]; + read += 1; + self.offset += 1; + + if self.offset % ISO_BLOCK_SIZE == 0 { + block_offset += 1; + content = read_block(self.lba + block_offset).await; + } + } + + read } } diff --git a/src/fs/iso/iso9660.rs b/src/fs/iso/iso9660.rs index 34afb61..793118d 100644 --- a/src/fs/iso/iso9660.rs +++ b/src/fs/iso/iso9660.rs @@ -1,4 +1,4 @@ -pub const ISO_BLOCK_SIZE: usize = 2048; +pub const ISO_BLOCK_SIZE: u32 = 2048; // Twin values structs diff --git a/src/fs/iso/mod.rs b/src/fs/iso/mod.rs index 73e2ae0..ef7b7fe 100644 --- a/src/fs/iso/mod.rs +++ b/src/fs/iso/mod.rs @@ -17,6 +17,7 @@ use async_trait::async_trait; pub struct IsoFS {} #[async_trait(?Send)] +#[allow(unaligned_references)] impl FileSystem for IsoFS { async fn open(&mut self, path: &str, flags: u32) -> Option { // ISO is a read only file system @@ -31,12 +32,13 @@ impl FileSystem for IsoFS { return None; } - let root: IsoDir = voldesc.root_dir; - let mut curr_entry_block: [u8; iso9660::ISO_BLOCK_SIZE] = read_block(root.data_blk.le).await; + let root: &IsoDir = &voldesc.root_dir; + let mut curr_entry_block: [u8; iso9660::ISO_BLOCK_SIZE as usize] = read_block(root.data_blk.le).await; let mut curr_entry: &IsoDir = unserialize(curr_entry_block.as_ptr()); let path_s: String = String::from(path); + let path_split: Vec<&str> = path_s.split("/").collect(); let path_it = path_s .split("/") .filter(|p| p != &""); @@ -44,15 +46,14 @@ impl FileSystem for IsoFS { for path_component in path_it { let mut found = false; while curr_entry.idf_len != 0 { - serial_println!("{:?}", curr_entry.idf_len); - serial_println!("{:?}", alloc::str::from_utf8(curr_entry.get_idf()).unwrap()); // Found entry if curr_entry.matches(path_component) { - serial_println!("Found {}", path_component); found = true; - curr_entry_block = read_block(curr_entry.data_blk.le).await; - curr_entry = unserialize(curr_entry_block.as_ptr()); + if path_component != path_split[path_split.len() - 1] { + curr_entry_block = read_block(curr_entry.data_blk.le).await; + curr_entry = unserialize(curr_entry_block.as_ptr()); + } break; } @@ -64,9 +65,9 @@ impl FileSystem for IsoFS { } } - let fd = IsoFD::new(); + let fd = IsoFD::new(curr_entry); FD_TABLE.lock().await.register_fd(fd.clone()); - Some(fd.clone()) + Some(fd) } } diff --git a/src/lib.rs b/src/lib.rs index 32c7641..4aa5b95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,5 +71,9 @@ async fn get_file() { .open("///boot/grub//grub.cfg", syscalls::io::O_RDONLY) .await .unwrap(); - fd.borrow_mut().read(&[], 0).await; + let mut buf: [u8; 100] = [0; 100]; + let read = fd.borrow_mut().read(&mut buf, 100).await; + + println!("{:?}", read); + println!("{:?}", alloc::str::from_utf8(&buf).unwrap()); }