@@ -32,7 +32,6 @@ use crate::fs::devfs::install_device;
3232use crate :: fs:: { Path , Result , MOUNT_MANAGER } ;
3333
3434use crate :: fs:: ext2:: Ext2 ;
35- use crate :: mem:: paging:: { align_down, align_up} ;
3635use crate :: utils:: sync:: Mutex ;
3736
3837use super :: devfs:: { alloc_device_marker, Device } ;
@@ -45,20 +44,27 @@ pub trait BlockDeviceInterface: Send + Sync {
4544 fn write_block ( & self , sector : usize , buf : & [ u8 ] ) -> Option < usize > ;
4645
4746 fn read ( & self , offset : usize , dest : & mut [ MaybeUninit < u8 > ] ) -> Option < usize > {
48- let aligned_offset = align_down ( offset as u64 , self . block_size ( ) as u64 ) as usize ;
49- let sector = aligned_offset / self . block_size ( ) ;
47+ let mut progress = 0 ;
48+ let block_size = self . block_size ( ) ;
5049
51- let aligned_size = align_up ( dest. len ( ) as u64 , self . block_size ( ) as u64 ) as usize ;
52- let mut buffer = Box :: < [ u8 ] > :: new_uninit_slice ( aligned_size) ;
50+ while progress < dest. len ( ) {
51+ let block = ( offset + progress) / block_size;
52+ let loc = ( offset + progress) % block_size;
5353
54- self . read_block ( sector, MaybeUninit :: slice_as_bytes_mut ( & mut buffer) ) ?;
55- // SAFETY: We have initialized the buffer above.
56- let buffer = unsafe { buffer. assume_init ( ) } ;
54+ let mut chunk = dest. len ( ) - progress;
5755
58- let offset = offset - aligned_offset;
59- MaybeUninit :: write_slice ( dest, & buffer[ offset..( offset + dest. len ( ) ) ] ) ;
56+ if chunk > ( block_size - loc) {
57+ chunk = block_size - loc;
58+ }
59+
60+ let mut buffer = Box :: < [ u8 ] > :: new_uninit_slice ( block_size) ;
61+ self . read_block ( block, MaybeUninit :: slice_as_bytes_mut ( & mut buffer) ) ?;
62+
63+ dest[ progress..( progress + chunk) ] . copy_from_slice ( & buffer[ loc..loc + chunk] ) ;
64+ progress += chunk;
65+ }
6066
61- Some ( dest . len ( ) )
67+ Some ( progress )
6268 }
6369}
6470
0 commit comments