Skip to content

Commit

Permalink
Refactor write root dir to use the new disk write
Browse files Browse the repository at this point in the history
  • Loading branch information
alloncm committed Oct 3, 2023
1 parent ec8825e commit 22cc146
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
4 changes: 1 addition & 3 deletions rpi/src/drivers/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Disk{
emmc.init();

let mut mbr = MasterBootRecord::default();
let buffer = unsafe{as_mut_buffer(&mut mbr)};
let buffer = as_mut_buffer(&mut mbr);

if !emmc.read(buffer){
core::panic!("Cant read MBR from disk");
Expand All @@ -68,7 +68,6 @@ impl Disk{
// early return if the buffer is aligned
if buffer_len_reminder == 0 {return};
}
log::warn!("Triggered unaligned read");
// handle the case buffer length is not aligned for block size
let mut temp_buffer:[u8;Self::BLOCK_SIZE as usize] = [0;Self::BLOCK_SIZE as usize];
self.emmc.seek(((block_index + (max_aligned_buffer_len as u32 / Self::BLOCK_SIZE)) * Self::BLOCK_SIZE) as u64);
Expand All @@ -93,7 +92,6 @@ impl Disk{
// early return since the buffer is aligned
if buffer_len_reminder == 0 {return};
}
log::warn!("Triggered unaligned write: len: {}", buffer.len());
// handle the case buffer length is not aligned for block size
let mut temp_buffer:[u8;Self::BLOCK_SIZE as usize] = [0;Self::BLOCK_SIZE as usize];
temp_buffer[..buffer_len_reminder].copy_from_slice(&buffer[max_aligned_buffer_len..]);
Expand Down
22 changes: 8 additions & 14 deletions rpi/src/drivers/fat32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl Fat32Fs{
let bpb_sector_index = disk.get_partition_first_sector_index(DISK_PARTITION_INDEX);

let mut boot_sector:Fat32BootSector = Default::default();
let buffer = unsafe{as_mut_buffer(&mut boot_sector)};
let buffer = as_mut_buffer(&mut boot_sector);
disk.read(bpb_sector_index, buffer);

let fs_type_label = boot_sector.fs_type_label.clone();
Expand Down Expand Up @@ -355,7 +355,7 @@ impl Fat32Fs{
let mut sector_offset = 0;
'search: loop{
let mut root_dir = [FatShortDirEntry::default();FAT_DIR_ENTRIES_PER_SECTOR];
let buffer = unsafe{as_mut_buffer(&mut root_dir)};
let buffer = as_mut_buffer(&mut root_dir);
self.disk.read(root_start_sector_index + sector_offset, buffer);
sector_offset += 1; // Since root_dir buffer contains enough entries for exactly 1 sector
for dir in root_dir{
Expand Down Expand Up @@ -579,18 +579,8 @@ impl Fat32Fs{
}

fn write_root_dir_cache(&mut self){
let mut root_sector_index = self.get_cluster_start_sector_index(self.boot_sector.fat32_bpb.root_dir_first_cluster);
let mut chunks = self.root_dir_cache.chunks_exact_mut(FAT_DIR_ENTRIES_PER_SECTOR);
let mut buffer = [FatShortDirEntry::default(); FAT_DIR_ENTRIES_PER_SECTOR];
while let Some(chunk) = chunks.next(){
buffer.copy_from_slice(chunk);
let mut buffer = unsafe{as_mut_buffer(&mut buffer)};
self.disk.write(root_sector_index, &mut buffer);
root_sector_index += 1; // Since the buffer contains exactly single sector
}
let reminder = chunks.into_remainder();
buffer[..reminder.len()].copy_from_slice(reminder);
let buffer = unsafe{as_mut_buffer(&mut buffer)};
let root_sector_index = self.get_cluster_start_sector_index(self.boot_sector.fat32_bpb.root_dir_first_cluster);
let buffer = Self::arrayvec_as_buffer(&self.root_dir_cache);
self.disk.write(root_sector_index, buffer);
}

Expand All @@ -601,4 +591,8 @@ impl Fat32Fs{
((cluster - FIRST_DATA_CLUSTER) * self.boot_sector.fat32_bpb.sectors_per_cluster as u32) +
(self.boot_sector.fat32_bpb.sectors_per_fat_32 * self.boot_sector.fat32_bpb.fats_count as u32)
}

fn arrayvec_as_buffer<'a, T, const CAP:usize>(vec:&'a ArrayVec<T, CAP>)->&'a [u8]{
unsafe{core::slice::from_raw_parts(vec.as_ptr() as *const u8, vec.len() * core::mem::size_of::<T>())}
}
}
5 changes: 2 additions & 3 deletions rpi/src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub use ili9341_gfx_device::*;


#[cfg(not(feature = "os"))]
pub(crate) unsafe fn as_mut_buffer<'a, T>(t:&'a mut T)->&'a mut [u8]{
let buffer = &mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>());
return buffer;
pub(crate) fn as_mut_buffer<'a, T>(t:&'a mut T)->&'a mut [u8]{
unsafe{&mut *core::ptr::slice_from_raw_parts_mut(t as *mut T as *mut _, core::mem::size_of::<T>())}
}

0 comments on commit 22cc146

Please sign in to comment.