3
3
void printf (char * );
4
4
void printHex (uint8_t );
5
5
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 )
16
16
{
17
+ bytesPerSector = 512 ;
17
18
this ->master = master;
18
19
}
19
20
@@ -28,137 +29,135 @@ void AdvancedTechnologyAttachment::Identify()
28
29
29
30
devicePort.WriteToPort (0xA0 );
30
31
uint8_t status = commandPort.ReadFromPort ();
31
- if (status == 0xFF )
32
+ if (status == 0xFF )
32
33
return ;
33
34
34
-
35
35
devicePort.WriteToPort (master ? 0xA0 : 0xB0 );
36
36
sectorCountPort.WriteToPort (0 );
37
37
lbaLowPort.WriteToPort (0 );
38
38
lbaMidPort.WriteToPort (0 );
39
39
lbaHiPort.WriteToPort (0 );
40
- commandPort.WriteToPort (0xEC ); // identify command
41
-
40
+ commandPort.WriteToPort (0xEC );
42
41
43
42
status = commandPort.ReadFromPort ();
44
- if (status == 0x00 )
45
- return ;
43
+ if (status == 0x00 )
44
+ return ; // no device
46
45
47
- while (((status & 0x80 ) == 0x80 )
48
- && ((status & 0x01 ) != 0x01 ))
46
+ while (((status & 0x80 ) == 0x80 ) && ((status & 0x01 ) != 0x01 ))
49
47
status = commandPort.ReadFromPort ();
50
48
51
- if (status & 0x01 )
49
+ if (status & 0x01 )
52
50
{
53
51
printf (" ERROR" );
54
52
return ;
55
53
}
56
54
57
- for ( int i = 0 ; i < 35 ; i++)
55
+ for ( uint16_t i = 0 ; i < 256 ; i++)
58
56
{
59
-
60
57
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 );
65
62
}
66
- printf (" \n " );
67
63
}
68
64
69
- void AdvancedTechnologyAttachment::Read28 (uint32_t sectorNum , uint8_t * data, int count)
65
+ void AdvancedTechnologyAttachment::Read28 (uint32_t sector , uint8_t * data, int count)
70
66
{
71
- if (sectorNum > 0x0FFFFFFF )
67
+ if (sector & 0xF0000000 )
68
+ return ;
69
+ if (count > bytesPerSector)
72
70
return ;
73
71
74
- devicePort.WriteToPort ( (master ? 0xE0 : 0xF0 ) | ((sectorNum & 0x0F000000 ) >> 24 ) );
72
+ devicePort.WriteToPort ((master ? 0xE0 : 0xF0 ) | ((sector & 0x0F000000 ) >> 24 ));
75
73
errorPort.WriteToPort (0 );
76
74
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 );
80
79
commandPort.WriteToPort (0x20 );
81
80
82
81
uint8_t status = commandPort.ReadFromPort ();
83
- while (((status & 0x80 ) == 0x80 )
84
- && ((status & 0x01 ) != 0x01 ))
82
+ while (((status & 0x80 ) == 0x80 ) && ((status & 0x01 ) != 0x01 ))
85
83
status = commandPort.ReadFromPort ();
86
84
87
- if (status & 0x01 )
85
+ if (status & 0x01 )
88
86
{
89
87
printf (" ERROR" );
90
88
return ;
91
89
}
92
90
91
+ printf (" Reading from ATA: " );
93
92
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 )
97
94
{
98
95
uint16_t wdata = dataPort.ReadFromPort ();
99
96
97
+ /*
98
+ char* foo = " \0";
99
+ foo[1] = (wdata >> 8) & 0x00FF;
100
+ foo[0] = wdata & 0x00FF;
101
+ printf(foo);
102
+ */
103
+
100
104
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
+ }
104
108
105
- for ( int i = count + (count% 2 ); i < 512 ; i += 2 )
109
+ for ( uint16_t i = count + (count % 2 ); i < bytesPerSector ; i += 2 )
106
110
dataPort.ReadFromPort ();
107
111
}
108
112
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)
110
114
{
111
- if (sectorNum > 0x0FFFFFFF )
115
+ if (sector & 0xF0000000 )
112
116
return ;
113
- if (count > 512 )
117
+ if (count > bytesPerSector )
114
118
return ;
115
119
116
-
117
- devicePort.WriteToPort ( (master ? 0xE0 : 0xF0 ) | ((sectorNum & 0x0F000000 ) >> 24 ) );
120
+ devicePort.WriteToPort ((master ? 0xE0 : 0xF0 ) | ((sector & 0x0F000000 ) >> 24 ));
118
121
errorPort.WriteToPort (0 );
119
122
sectorCountPort.WriteToPort (1 );
120
- lbaLowPort.WriteToPort ( sectorNum & 0x000000FF );
121
- lbaMidPort.WriteToPort ( (sectorNum & 0x0000FF00 ) >> 8 );
122
- lbaLowPort.WriteToPort ( (sectorNum & 0x00FF0000 ) >> 16 );
123
- commandPort.WriteToPort (0x30 );
124
123
124
+ lbaLowPort.WriteToPort (sector & 0x000000FF );
125
+ lbaMidPort.WriteToPort ((sector & 0x0000FF00 ) >> 8 );
126
+ lbaHiPort.WriteToPort ((sector & 0x00FF0000 ) >> 16 );
127
+ commandPort.WriteToPort (0x30 );
125
128
126
- printf (" Writing to ATA Drive : " );
129
+ printf (" Writing to ATA: " );
127
130
128
- for ( int i = 0 ; i < count; i += 2 )
131
+ for ( uint16_t i = 0 ; i < count; i += 2 )
129
132
{
130
133
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 ;
134
136
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);
139
143
}
140
144
141
- for ( int i = count + (count% 2 ); i < 512 ; i += 2 )
145
+ for ( uint16_t i = count + (count % 2 ); i < bytesPerSector ; i += 2 )
142
146
dataPort.WriteToPort (0x0000 );
143
-
144
147
}
145
148
146
149
void AdvancedTechnologyAttachment::Flush ()
147
150
{
148
- devicePort.WriteToPort ( master ? 0xE0 : 0xF0 );
151
+ devicePort.WriteToPort (master ? 0xE0 : 0xF0 );
149
152
commandPort.WriteToPort (0xE7 );
150
153
151
154
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 ))
157
156
status = commandPort.ReadFromPort ();
158
157
159
- if (status & 0x01 )
158
+ if (status & 0x01 )
160
159
{
161
160
printf (" ERROR" );
162
161
return ;
163
162
}
164
- }
163
+ }
0 commit comments