diff --git a/rpi/src/drivers/disk.rs b/rpi/src/drivers/disk.rs index 2aaddba9..c73a1709 100644 --- a/rpi/src/drivers/disk.rs +++ b/rpi/src/drivers/disk.rs @@ -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"); @@ -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); @@ -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..]); diff --git a/rpi/src/drivers/fat32.rs b/rpi/src/drivers/fat32.rs index b9690b4d..a145a243 100644 --- a/rpi/src/drivers/fat32.rs +++ b/rpi/src/drivers/fat32.rs @@ -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(); @@ -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{ @@ -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); } @@ -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)->&'a [u8]{ + unsafe{core::slice::from_raw_parts(vec.as_ptr() as *const u8, vec.len() * core::mem::size_of::())} + } } \ No newline at end of file diff --git a/rpi/src/drivers/mod.rs b/rpi/src/drivers/mod.rs index 5f6ba12a..8b130aa1 100644 --- a/rpi/src/drivers/mod.rs +++ b/rpi/src/drivers/mod.rs @@ -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::()); - 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::())} } \ No newline at end of file