@@ -15,7 +15,10 @@ use crate::{
15
15
debug_println,
16
16
devices:: pci:: write_pci_command,
17
17
events:: { current_running_event, futures:: devices:: SDCardReq , get_runner_time} ,
18
- filesys:: { BlockDevice , FsError } ,
18
+ filesys:: {
19
+ ext2:: block_io:: { BlockError , BlockIO , BlockResult } ,
20
+ BlockDevice , FsError ,
21
+ } ,
19
22
memory:: paging,
20
23
} ;
21
24
use bitflags:: bitflags;
@@ -49,7 +52,7 @@ pub struct SDCardInfo {
49
52
/// Stores the block size of this sd card
50
53
block_size : usize ,
51
54
/// Stores total blocks of this sd card
52
- total_blocks : u64 ,
55
+ total_sectors : u64 ,
53
56
/// Stores the relative card address. This is used as an argument in some
54
57
/// sd commands
55
58
reletave_card_address : u32 ,
@@ -195,12 +198,12 @@ const SD_SUB_CLASS: u8 = 0x5;
195
198
const SD_NO_DMA_INTERFACE : u8 = 0x0 ;
196
199
const SD_DMA_INTERFACE : u8 = 0x1 ;
197
200
const MAX_ITERATIONS : usize = 1_000 ;
198
- const SD_BLOCK_SIZE : u32 = 512 ;
201
+ const SD_SECTOR_SIZE : u32 = 512 ;
199
202
200
203
#[ async_trait]
201
204
impl BlockDevice for SDCardInfo {
202
205
async fn read_block ( & self , block_num : u64 , buf : & mut [ u8 ] ) -> Result < ( ) , FsError > {
203
- if block_num > self . total_blocks {
206
+ if block_num > self . total_sectors {
204
207
return Result :: Err ( FsError :: IOError ) ;
205
208
}
206
209
let data = read_sd_card (
@@ -217,7 +220,7 @@ impl BlockDevice for SDCardInfo {
217
220
}
218
221
219
222
async fn write_block ( & mut self , block_num : u64 , buf : & [ u8 ] ) -> Result < ( ) , FsError > {
220
- if block_num > self . total_blocks {
223
+ if block_num > self . total_sectors {
221
224
return Result :: Err ( FsError :: IOError ) ;
222
225
}
223
226
let mut data: [ u8 ; 512 ] = [ 0 ; 512 ] ;
@@ -235,11 +238,66 @@ impl BlockDevice for SDCardInfo {
235
238
}
236
239
237
240
fn block_size ( & self ) -> usize {
238
- SD_BLOCK_SIZE . try_into ( ) . expect ( "To be on 64 bit system" )
241
+ SD_SECTOR_SIZE . try_into ( ) . expect ( "To be on 64 bit system" )
239
242
}
240
243
241
244
fn total_blocks ( & self ) -> u64 {
242
- self . total_blocks
245
+ self . total_sectors
246
+ }
247
+ }
248
+
249
+ const SECTORS_PER_BLOCK : u32 = 2 ;
250
+ const SD_BLOCK_IO_SIZE : u32 = SD_SECTOR_SIZE * SECTORS_PER_BLOCK ;
251
+
252
+ #[ async_trait]
253
+ impl BlockIO for SDCardInfo {
254
+ async fn read_block ( & self , block_num : u64 , buf : & mut [ u8 ] ) -> BlockResult < ( ) > {
255
+ if ( block_num + 1 ) * SECTORS_PER_BLOCK as u64 > self . total_sectors {
256
+ return BlockResult :: Err ( BlockError :: InvalidBlock ) ;
257
+ }
258
+ for start_block in 0 ..SECTORS_PER_BLOCK {
259
+ let block_num_32: u32 = block_num
260
+ . try_into ( )
261
+ . expect ( "Total blocks is less than 32 bits long" ) ;
262
+ let data = read_sd_card ( self , block_num_32 * SECTORS_PER_BLOCK + start_block)
263
+ . await
264
+ . map_err ( |_| BlockError :: DeviceError ) ?;
265
+ let start_block_size: usize = ( start_block * SD_SECTOR_SIZE ) . try_into ( ) . unwrap ( ) ;
266
+ // Copy the data from buff to data (Blame clippy for this abomination)
267
+ buf[ start_block_size..( SD_SECTOR_SIZE as usize + start_block_size) ]
268
+ . copy_from_slice ( & data[ ..( SD_SECTOR_SIZE as usize ) ] ) ;
269
+ }
270
+
271
+ Result :: Ok ( ( ) )
272
+ }
273
+
274
+ async fn write_block ( & self , block_num : u64 , buf : & [ u8 ] ) -> BlockResult < ( ) > {
275
+ if ( block_num + 1 ) * SECTORS_PER_BLOCK as u64 > self . total_sectors {
276
+ return BlockResult :: Err ( BlockError :: InvalidBlock ) ;
277
+ }
278
+ let mut data: [ u8 ; 512 ] = [ 0 ; 512 ] ;
279
+ for start_block in 0 ..SECTORS_PER_BLOCK {
280
+ let start_block_size: usize = ( start_block * SD_SECTOR_SIZE ) . try_into ( ) . unwrap ( ) ;
281
+ // Copy the data from data to buff (Blame clippy for this abomination)
282
+ data[ ..( SD_SECTOR_SIZE as usize ) ] . copy_from_slice (
283
+ & buf[ start_block_size..( SD_SECTOR_SIZE as usize + start_block_size) ] ,
284
+ ) ;
285
+ let block_num_32: u32 = block_num
286
+ . try_into ( )
287
+ . expect ( "Total blocks is less than 32 bits long" ) ;
288
+ write_sd_card ( self , block_num_32 * SECTORS_PER_BLOCK + start_block, data)
289
+ . await
290
+ . map_err ( |_| BlockError :: DeviceError ) ?;
291
+ }
292
+ Result :: Ok ( ( ) )
293
+ }
294
+
295
+ fn block_size ( & self ) -> u64 {
296
+ SD_BLOCK_IO_SIZE as u64 //.try_into().expect("To be on 64 bit system")
297
+ }
298
+
299
+ fn size_in_bytes ( & self ) -> u64 {
300
+ self . total_sectors * SD_SECTOR_SIZE as u64
243
301
}
244
302
}
245
303
@@ -549,7 +607,7 @@ fn get_full_sd_card_info(
549
607
rca : u32 ,
550
608
csd : u128 ,
551
609
) -> Result < SDCardInfo , SDCardError > {
552
- // Currently only supports CSD Version 1 .0
610
+ // Currently only supports CSD Version 2 .0
553
611
let csd_structre: u32 = ( csd >> 126 )
554
612
. try_into ( )
555
613
. expect ( "Higher bits to be masked out" ) ;
@@ -564,8 +622,8 @@ fn get_full_sd_card_info(
564
622
let info = SDCardInfo {
565
623
internal_info : sd_card. clone ( ) ,
566
624
reletave_card_address : rca,
567
- block_size : SD_BLOCK_SIZE . try_into ( ) . expect ( "To be on 64 bit system" ) ,
568
- total_blocks : ( c_size + 1 ) . into ( ) ,
625
+ block_size : SD_SECTOR_SIZE . try_into ( ) . expect ( "To be on 64 bit system" ) ,
626
+ total_sectors : ( ( c_size + 1 ) * 1024 ) . into ( ) ,
569
627
} ;
570
628
571
629
Result :: Ok ( info)
@@ -722,7 +780,7 @@ pub async fn read_sd_card(sd_card: &SDCardInfo, block: u32) -> Result<[u8; 512],
722
780
unsafe { core:: ptr:: write_volatile ( block_count_register_addr, 1 ) } ;
723
781
sending_command_valid ( internal_info) ?;
724
782
let argument_register_addr = ( internal_info. base_address_register + 0x8 ) as * mut u32 ;
725
- unsafe { core:: ptr:: write_volatile ( argument_register_addr, block * SD_BLOCK_SIZE ) } ;
783
+ unsafe { core:: ptr:: write_volatile ( argument_register_addr, block) } ;
726
784
let transfer_mode_register_adder = ( internal_info. base_address_register + 0xC ) as * mut u16 ;
727
785
unsafe {
728
786
core:: ptr:: write_volatile (
@@ -778,7 +836,7 @@ pub async fn write_sd_card(
778
836
unsafe { core:: ptr:: write_volatile ( block_count_register_addr, 1 ) } ;
779
837
sending_command_valid ( internal_info) ?;
780
838
let argument_register_addr = ( internal_info. base_address_register + 0x8 ) as * mut u32 ;
781
- unsafe { core:: ptr:: write_volatile ( argument_register_addr, block * SD_BLOCK_SIZE ) } ;
839
+ unsafe { core:: ptr:: write_volatile ( argument_register_addr, block) } ;
782
840
let transfer_mode_register_adder = ( internal_info. base_address_register + 0xC ) as * mut u16 ;
783
841
unsafe { core:: ptr:: write_volatile ( transfer_mode_register_adder, 0 ) } ;
784
842
0 commit comments