@@ -32,7 +32,6 @@ use crate::fs::devfs::install_device;
32
32
use crate :: fs:: { Path , Result , MOUNT_MANAGER } ;
33
33
34
34
use crate :: fs:: ext2:: Ext2 ;
35
- use crate :: mem:: paging:: { align_down, align_up} ;
36
35
use crate :: utils:: sync:: Mutex ;
37
36
38
37
use super :: devfs:: { alloc_device_marker, Device } ;
@@ -45,20 +44,27 @@ pub trait BlockDeviceInterface: Send + Sync {
45
44
fn write_block ( & self , sector : usize , buf : & [ u8 ] ) -> Option < usize > ;
46
45
47
46
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 ( ) ;
50
49
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;
53
53
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;
57
55
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
+ }
60
66
61
- Some ( dest . len ( ) )
67
+ Some ( progress )
62
68
}
63
69
}
64
70
0 commit comments