From 0ad8ba9adba26abfd30b884b3abd3bab81277737 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Sun, 25 Dec 2022 12:58:56 +0100 Subject: [PATCH] refacto directory entries traversal Signed-off-by: Julien CLEMENT --- src/drivers/atapi/mod.rs | 4 ++++ src/fs/iso/mod.rs | 35 +++++++++-------------------------- src/utils/mod.rs | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/drivers/atapi/mod.rs b/src/drivers/atapi/mod.rs index 4f0730a..bbb04cc 100644 --- a/src/drivers/atapi/mod.rs +++ b/src/drivers/atapi/mod.rs @@ -325,3 +325,7 @@ pub async fn print_block(lba: u32) { let block = DRIVE.lock().await.as_mut().unwrap().read_block(lba).await; serial_println!("{:x?}", block); } + +pub async fn read_block(lba: u32) -> [u8; CD_SECTOR_SIZE] { + DRIVE.lock().await.as_mut().unwrap().read_block(lba).await +} \ No newline at end of file diff --git a/src/fs/iso/mod.rs b/src/fs/iso/mod.rs index dc4fcb8..8ef8d39 100644 --- a/src/fs/iso/mod.rs +++ b/src/fs/iso/mod.rs @@ -1,7 +1,7 @@ mod fd; pub mod iso9660; -use crate::drivers::atapi::DRIVE; +use crate::drivers::atapi::read_block; use crate::fd::{FDt, FD_TABLE}; use crate::println; use crate::serial_println; @@ -32,13 +32,7 @@ impl FileSystem for IsoFS { } let root: IsoDir = voldesc.root_dir; - let mut curr_entry_block: [u8; iso9660::ISO_BLOCK_SIZE] = DRIVE - .lock() - .await - .as_mut() - .unwrap() - .read_block(root.data_blk.le) - .await; + let mut curr_entry_block: [u8; iso9660::ISO_BLOCK_SIZE] = read_block(root.data_blk.le).await; let mut curr_entry: &IsoDir = unserialize(curr_entry_block.as_ptr()); @@ -54,22 +48,13 @@ impl FileSystem for IsoFS { if curr_entry.get_idf() == path_component.as_bytes() { serial_println!("Found {}", path_component); - curr_entry_block = DRIVE - .lock() - .await - .as_mut() - .unwrap() - .read_block(curr_entry.data_blk.le) - .await; + curr_entry_block = read_block(curr_entry.data_blk.le).await; curr_entry = unserialize(curr_entry_block.as_ptr()); break; } // Next entry - unsafe { - let curr_ptr: *const IsoDir = curr_entry; - curr_entry = &*curr_ptr.cast::().offset(curr_entry.dir_size as isize).cast::(); - } + curr_entry = next_entry(curr_entry); } } @@ -79,13 +64,11 @@ impl FileSystem for IsoFS { } } +pub fn next_entry(entry: &IsoDir) -> &IsoDir { + crate::utils::ref_raw_offset(entry, entry.dir_size as isize) +} + pub async fn get_prim_vol_desc() -> IsoPrimVolDesc { - let desc_block = DRIVE - .lock() - .await - .as_mut() - .unwrap() - .read_block(iso9660::ISO_PRIM_VOLDESC_BLOCK) - .await; + let desc_block = read_block(iso9660::ISO_PRIM_VOLDESC_BLOCK).await; *unserialize::(desc_block.as_ptr()) } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index feab11c..9b88c2c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -3,3 +3,17 @@ pub mod serialize; pub use mutex::AsyncMutex; pub use serialize::unserialize; + +pub fn ref_offset(r: &T, off: isize) -> &T { + let ref_ptr: *const T = r; + unsafe { + return &*ref_ptr.offset(off); + } +} + +pub fn ref_raw_offset(r: &T, off: isize) -> &T { + let ref_ptr: *const T = r; + unsafe { + return &*ref_ptr.cast::().offset(off).cast::(); + } +} \ No newline at end of file