1- use x86_64:: instructions:: port:: Port ;
21use crate :: println;
2+ use x86_64:: instructions:: port:: Port ;
33
4- const ATA_DATA : u16 = 0x170 ;
4+ const ATA_DATA : u16 = 0x170 ;
55#[ allow( dead_code) ]
6- const ATA_ERROR : u16 = 0x171 ;
7- const ATA_SECTOR_CNT : u16 = 0x172 ;
8- const ATA_LBA_LO : u16 = 0x173 ;
9- const ATA_LBA_MID : u16 = 0x174 ;
10- const ATA_LBA_HI : u16 = 0x175 ;
11- const ATA_DRIVE_HEAD : u16 = 0x176 ;
12- const ATA_STATUS : u16 = 0x177 ;
13- const ATA_COMMAND : u16 = 0x177 ;
6+ const ATA_ERROR : u16 = 0x171 ;
7+ const ATA_SECTOR_CNT : u16 = 0x172 ;
8+ const ATA_LBA_LO : u16 = 0x173 ;
9+ const ATA_LBA_MID : u16 = 0x174 ;
10+ const ATA_LBA_HI : u16 = 0x175 ;
11+ const ATA_DRIVE_HEAD : u16 = 0x176 ;
12+ const ATA_STATUS : u16 = 0x177 ;
13+ const ATA_COMMAND : u16 = 0x177 ;
1414
15- const CMD_READ : u8 = 0x20 ;
15+ const CMD_READ : u8 = 0x20 ;
1616const CMD_WRITE : u8 = 0x30 ;
1717
1818#[ allow( dead_code) ]
@@ -25,8 +25,12 @@ fn wait_ready() -> bool {
2525 let mut status: Port < u8 > = unsafe { Port :: new ( ATA_STATUS ) } ;
2626 for _ in 0 ..100_000 {
2727 let s = unsafe { status. read ( ) } ;
28- if s & STATUS_BSY == 0 && s & STATUS_DRQ != 0 { return true ; }
29- if s & STATUS_ERR != 0 { return false ; }
28+ if s & STATUS_BSY == 0 && s & STATUS_DRQ != 0 {
29+ return true ;
30+ }
31+ if s & STATUS_ERR != 0 {
32+ return false ;
33+ }
3034 }
3135 false
3236}
@@ -35,7 +39,9 @@ fn wait_ready() -> bool {
3539fn wait_not_busy ( ) {
3640 let mut status: Port < u8 > = unsafe { Port :: new ( ATA_STATUS ) } ;
3741 for _ in 0 ..100_000 {
38- if unsafe { status. read ( ) } & STATUS_BSY == 0 { return ; }
42+ if unsafe { status. read ( ) } & STATUS_BSY == 0 {
43+ return ;
44+ }
3945 }
4046}
4147
@@ -57,13 +63,17 @@ pub fn read_sector(lba: u32, buf: &mut [u8; 512]) -> bool {
5763 Port :: < u8 > :: new ( ATA_COMMAND ) . write ( CMD_READ ) ;
5864 // Fixed delay - read status 400 times
5965 let mut sp = Port :: < u8 > :: new ( ATA_STATUS ) ;
60- for _ in 0 ..400usize { sp. read ( ) ; }
66+ for _ in 0 ..400usize {
67+ sp. read ( ) ;
68+ }
6169 let s = sp. read ( ) ;
62- if s & STATUS_ERR != 0 || s & STATUS_DRQ == 0 { return false ; }
70+ if s & STATUS_ERR != 0 || s & STATUS_DRQ == 0 {
71+ return false ;
72+ }
6373 let mut data: Port < u16 > = Port :: new ( ATA_DATA ) ;
6474 for i in 0 ..256usize {
6575 let word = data. read ( ) ;
66- buf[ i * 2 ] = ( word & 0xFF ) as u8 ;
76+ buf[ i * 2 ] = ( word & 0xFF ) as u8 ;
6777 buf[ i * 2 + 1 ] = ( word >> 8 ) as u8 ;
6878 }
6979 true
@@ -79,9 +89,13 @@ pub fn write_sector(lba: u32, buf: &[u8; 512]) -> bool {
7989 Port :: < u8 > :: new ( ATA_LBA_HI ) . write ( ( lba >> 16 ) as u8 ) ;
8090 Port :: < u8 > :: new ( ATA_COMMAND ) . write ( CMD_WRITE ) ;
8191 let mut sp = Port :: < u8 > :: new ( ATA_STATUS ) ;
82- for _ in 0 ..400usize { sp. read ( ) ; }
92+ for _ in 0 ..400usize {
93+ sp. read ( ) ;
94+ }
8395 let s = sp. read ( ) ;
84- if s & STATUS_ERR != 0 || s & STATUS_DRQ == 0 { return false ; }
96+ if s & STATUS_ERR != 0 || s & STATUS_DRQ == 0 {
97+ return false ;
98+ }
8599 let mut data: Port < u16 > = Port :: new ( ATA_DATA ) ;
86100 for i in 0 ..256usize {
87101 let word = ( buf[ i * 2 ] as u16 ) | ( ( buf[ i * 2 + 1 ] as u16 ) << 8 ) ;
@@ -96,7 +110,9 @@ pub fn detect() -> bool {
96110 // Select slave drive (0xB0)
97111 Port :: < u8 > :: new ( ATA_DRIVE_HEAD ) . write ( 0xA0 ) ;
98112 // Read status 4 times to let drive respond
99- for _ in 0 ..4 { Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ; }
113+ for _ in 0 ..4 {
114+ Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
115+ }
100116 let status = Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
101117 // 0xFF = floating bus = no drive, 0x00 = also no drive
102118 status != 0xFF && status != 0x00
@@ -107,7 +123,9 @@ pub fn init() -> bool {
107123 unsafe {
108124 // Check secondary channel master (our axiom-disk.img is index=1 = secondary master)
109125 Port :: < u8 > :: new ( ATA_DRIVE_HEAD ) . write ( 0xA0 ) ;
110- for _ in 0 ..15usize { Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ; }
126+ for _ in 0 ..15usize {
127+ Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
128+ }
111129 let s1 = Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
112130 let s2 = Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
113131 let s3 = Port :: < u8 > :: new ( ATA_STATUS ) . read ( ) ;
0 commit comments