33void printf (char * );
44void printHex (uint8_t );
55
6- AdvancedTechnologyAttachment::AdvancedTechnologyAttachment (bool master, uint16_t portBase )
7- : dataPort(portBase),
8- errorPort(portBase + 0x1 ),
9- sectorCountPort(portBase + 0x2 ),
10- lbaLowPort(portBase + 0x3 ),
11- lbaMidPort(portBase + 0x4 ),
12- lbaHiPort(portBase + 0x5 ),
13- devicePort(portBase + 0x6 ),
14- commandPort(portBase + 0x7 ),
15- controlPort(portBase + 0x206 )
6+ AdvancedTechnologyAttachment::AdvancedTechnologyAttachment (uint16_t portBase, bool master )
7+ : dataPort(portBase),
8+ errorPort(portBase + 1 ),
9+ sectorCountPort(portBase + 2 ),
10+ lbaLowPort(portBase + 3 ),
11+ lbaMidPort(portBase + 4 ),
12+ lbaHiPort(portBase + 5 ),
13+ devicePort(portBase + 6 ),
14+ commandPort(portBase + 7 ),
15+ controlPort(portBase + 0x206 )
1616{
17+ bytesPerSector = 512 ;
1718 this ->master = master;
1819}
1920
@@ -28,137 +29,135 @@ void AdvancedTechnologyAttachment::Identify()
2829
2930 devicePort.WriteToPort (0xA0 );
3031 uint8_t status = commandPort.ReadFromPort ();
31- if (status == 0xFF )
32+ if (status == 0xFF )
3233 return ;
3334
34-
3535 devicePort.WriteToPort (master ? 0xA0 : 0xB0 );
3636 sectorCountPort.WriteToPort (0 );
3737 lbaLowPort.WriteToPort (0 );
3838 lbaMidPort.WriteToPort (0 );
3939 lbaHiPort.WriteToPort (0 );
40- commandPort.WriteToPort (0xEC ); // identify command
41-
40+ commandPort.WriteToPort (0xEC );
4241
4342 status = commandPort.ReadFromPort ();
44- if (status == 0x00 )
45- return ;
43+ if (status == 0x00 )
44+ return ; // no device
4645
47- while (((status & 0x80 ) == 0x80 )
48- && ((status & 0x01 ) != 0x01 ))
46+ while (((status & 0x80 ) == 0x80 ) && ((status & 0x01 ) != 0x01 ))
4947 status = commandPort.ReadFromPort ();
5048
51- if (status & 0x01 )
49+ if (status & 0x01 )
5250 {
5351 printf (" ERROR" );
5452 return ;
5553 }
5654
57- for ( int i = 0 ; i < 35 ; i++)
55+ for ( uint16_t i = 0 ; i < 256 ; i++)
5856 {
59-
6057 uint16_t data = dataPort.ReadFromPort ();
61- char *text = " \0 \0" ;
62- text[ 0 ] = (data >> 8 ) & 0xFF ;
63- text[ 1 ] = data & 0xFF ;
64- printf (text );
58+ char *foo = " \0 " ;
59+ foo[ 1 ] = (data >> 8 ) & 0x00FF ;
60+ foo[ 0 ] = data & 0x00FF ;
61+ printf (foo );
6562 }
66- printf (" \n " );
6763}
6864
69- void AdvancedTechnologyAttachment::Read28 (uint32_t sectorNum , uint8_t * data, int count)
65+ void AdvancedTechnologyAttachment::Read28 (uint32_t sector , uint8_t * data, int count)
7066{
71- if (sectorNum > 0x0FFFFFFF )
67+ if (sector & 0xF0000000 )
68+ return ;
69+ if (count > bytesPerSector)
7270 return ;
7371
74- devicePort.WriteToPort ( (master ? 0xE0 : 0xF0 ) | ((sectorNum & 0x0F000000 ) >> 24 ) );
72+ devicePort.WriteToPort ((master ? 0xE0 : 0xF0 ) | ((sector & 0x0F000000 ) >> 24 ));
7573 errorPort.WriteToPort (0 );
7674 sectorCountPort.WriteToPort (1 );
77- lbaLowPort.WriteToPort ( sectorNum & 0x000000FF );
78- lbaMidPort.WriteToPort ( (sectorNum & 0x0000FF00 ) >> 8 );
79- lbaLowPort.WriteToPort ( (sectorNum & 0x00FF0000 ) >> 16 );
75+
76+ lbaLowPort.WriteToPort (sector & 0x000000FF );
77+ lbaMidPort.WriteToPort ((sector & 0x0000FF00 ) >> 8 );
78+ lbaHiPort.WriteToPort ((sector & 0x00FF0000 ) >> 16 );
8079 commandPort.WriteToPort (0x20 );
8180
8281 uint8_t status = commandPort.ReadFromPort ();
83- while (((status & 0x80 ) == 0x80 )
84- && ((status & 0x01 ) != 0x01 ))
82+ while (((status & 0x80 ) == 0x80 ) && ((status & 0x01 ) != 0x01 ))
8583 status = commandPort.ReadFromPort ();
8684
87- if (status & 0x01 )
85+ if (status & 0x01 )
8886 {
8987 printf (" ERROR" );
9088 return ;
9189 }
9290
91+ printf (" Reading from ATA: " );
9392
94- printf (" Reading ATA Drive: " );
95-
96- for (uint16_t i = 0 ; i < count; i += 2 )
93+ for (uint16_t i = 0 ; i < count; i += 2 )
9794 {
9895 uint16_t wdata = dataPort.ReadFromPort ();
9996
97+ /*
98+ char* foo = " \0";
99+ foo[1] = (wdata >> 8) & 0x00FF;
100+ foo[0] = wdata & 0x00FF;
101+ printf(foo);
102+ */
103+
100104 data[i] = wdata & 0x00FF ;
101- if (i+ 1 < count)
102- data[i+ 1 ] = (wdata >> 8 ) & 0x00FF ;
103- }
105+ if (i + 1 < count)
106+ data[i + 1 ] = (wdata >> 8 ) & 0x00FF ;
107+ }
104108
105- for ( int i = count + (count% 2 ); i < 512 ; i += 2 )
109+ for ( uint16_t i = count + (count % 2 ); i < bytesPerSector ; i += 2 )
106110 dataPort.ReadFromPort ();
107111}
108112
109- void AdvancedTechnologyAttachment::Write28 (uint32_t sectorNum , uint8_t * data, uint32_t count)
113+ void AdvancedTechnologyAttachment::Write28 (uint32_t sector , uint8_t * data, int count)
110114{
111- if (sectorNum > 0x0FFFFFFF )
115+ if (sector & 0xF0000000 )
112116 return ;
113- if (count > 512 )
117+ if (count > bytesPerSector )
114118 return ;
115119
116-
117- devicePort.WriteToPort ( (master ? 0xE0 : 0xF0 ) | ((sectorNum & 0x0F000000 ) >> 24 ) );
120+ devicePort.WriteToPort ((master ? 0xE0 : 0xF0 ) | ((sector & 0x0F000000 ) >> 24 ));
118121 errorPort.WriteToPort (0 );
119122 sectorCountPort.WriteToPort (1 );
120- lbaLowPort.WriteToPort ( sectorNum & 0x000000FF );
121- lbaMidPort.WriteToPort ( (sectorNum & 0x0000FF00 ) >> 8 );
122- lbaLowPort.WriteToPort ( (sectorNum & 0x00FF0000 ) >> 16 );
123- commandPort.WriteToPort (0x30 );
124123
124+ lbaLowPort.WriteToPort (sector & 0x000000FF );
125+ lbaMidPort.WriteToPort ((sector & 0x0000FF00 ) >> 8 );
126+ lbaHiPort.WriteToPort ((sector & 0x00FF0000 ) >> 16 );
127+ commandPort.WriteToPort (0x30 );
125128
126- printf (" Writing to ATA Drive : " );
129+ printf (" Writing to ATA: " );
127130
128- for ( int i = 0 ; i < count; i += 2 )
131+ for ( uint16_t i = 0 ; i < count; i += 2 )
129132 {
130133 uint16_t wdata = data[i];
131- if (i+1 < count)
132- wdata |= ((uint16_t )data[i+1 ]) << 8 ;
133- dataPort.WriteToPort (wdata);
134+ if (i + 1 < count)
135+ wdata |= ((uint16_t )data[i + 1 ]) << 8 ;
134136
135- char *text = " \0 " ;
136- text[0 ] = (wdata >> 8 ) & 0xFF ;
137- text[1 ] = wdata & 0xFF ;
138- printf (text);
137+ char *foo = " \0 " ;
138+ foo[1 ] = (wdata >> 8 ) & 0x00FF ;
139+ foo[0 ] = wdata & 0x00FF ;
140+ printf (foo);
141+
142+ dataPort.WriteToPort (wdata);
139143 }
140144
141- for ( int i = count + (count% 2 ); i < 512 ; i += 2 )
145+ for ( uint16_t i = count + (count % 2 ); i < bytesPerSector ; i += 2 )
142146 dataPort.WriteToPort (0x0000 );
143-
144147}
145148
146149void AdvancedTechnologyAttachment::Flush ()
147150{
148- devicePort.WriteToPort ( master ? 0xE0 : 0xF0 );
151+ devicePort.WriteToPort (master ? 0xE0 : 0xF0 );
149152 commandPort.WriteToPort (0xE7 );
150153
151154 uint8_t status = commandPort.ReadFromPort ();
152- if (status == 0x00 )
153- return ;
154-
155- while (((status & 0x80 ) == 0x80 )
156- && ((status & 0x01 ) != 0x01 ))
155+ while (((status & 0x80 ) == 0x80 ) && ((status & 0x01 ) != 0x01 ))
157156 status = commandPort.ReadFromPort ();
158157
159- if (status & 0x01 )
158+ if (status & 0x01 )
160159 {
161160 printf (" ERROR" );
162161 return ;
163162 }
164- }
163+ }
0 commit comments