-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathata.c
384 lines (336 loc) · 9.82 KB
/
ata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* ata.c
*
* Created: 7/2/2018 11:18:32 PM
* Author: lucah
*/
#ifndef F_CPU
#define F_CPU 20000000UL
#endif
#include <avr/io.h>
#include "ata.h"
#include <stdint.h>
#include <util/delay.h>
typeDriveInfo driveInfo;
partition partitions[4];
uint8_t ata_init() {
//Set up ports
DDRA = 0x00;
DDRC = 0x00;
DDRD |= (1 << PD7) | (1 << PD6);
PORTD |= (1 << PD7) | (1 << PD6);
DDRD &= ~(1 << PD5);
DDRB = 0x1F;
PORTB = NOP;
//Wait for drive to be ready
for(uint8_t i = 0; i < 10; i++) _delay_ms(10);
while((PIND & (1 << PD5)) == 0){
_delay_ms(2);
}
ata_reset();
//Enable LBA mode
ata_writeByte(REG_DRIVE_HEAD, DRV_HEAD_BASE);
_delay_ms(1);
ata_busyWait();
//Get info
ata_writeByte(REG_STAT_CMD, CMD_IDENT);
_delay_us(10);
ata_busyWait();
ata_waitForData();
_delay_us(200);
uint8_t buff[512];
ata_readBuffer(buff, 512);
driveInfo.LBAsupported = (buff[49 * 2 + 1] & 2) == 2 ? 1 : 0;
if(!driveInfo.LBAsupported) return ERR_LBA_NOT_SUPPORTED;
driveInfo.sizeinsectors = * ((unsigned long*) (buff + 120));
for(uint8_t i = 0; i < 20; i+=2) {
driveInfo.serial[i] = buff[20 + i + 1];
driveInfo.serial[i + 1] = buff[20 + i];
}
for(uint8_t i = 0; i < 40; i+=2) {
driveInfo.model[i] = buff[54 + i + 1];
driveInfo.model[i + 1] = buff[54 + i];
}
if((buff[53 * 2] & 1) == 0) return ERR_DATA_INVALID;
//Detect partitions
uint8_t err = ata_readSector(buff, 0); //Read sector 0 (contains MBR)
if(err != 0) return err | 0b00100000; //Make sure bit 5 is always one, so this error code isn't mistaken with any other errors this function can return
if(buff[510] != 0x55 || buff[511] != 0xAA){
return ERR_INVALID_MBR;
}
for(uint8_t i = 0; i < 4; i++){
partitions[i].id = i;
partitions[i].start = * ((unsigned long*) (buff + (446 + i * 16 + 8)));
partitions[i].size = * ((unsigned long*) (buff + (446 + i * 16 + 12)));
partitions[i].end = partitions[i].start + partitions[i].size;
partitions[i].type = buff[446 + i * 16 + 4];
if(partitions[i].start > driveInfo.sizeinsectors || partitions[i].end > driveInfo.sizeinsectors || partitions[i].type == 0){ //Invalid partition
partitions[i].start = 0; //Clear info
partitions[i].size = 0;
partitions[i].end = 0;
partitions[i].type = 0;
}
}
return ata_runDiag();
}
//Does a software reset
void ata_reset() {
//Software reset
ata_writeByte(REG_ASTA_CTRL, 0x06);
for(uint8_t i = 0; i < 10; i++) _delay_ms(10);
ata_writeByte(REG_ASTA_CTRL, 0x00);
for(uint8_t i = 0; i < 10; i++) _delay_ms(10);
ata_busyWait();
}
//Run drive diagnostics
//Returns: 0 when no error was found, error code otherwise
uint8_t ata_runDiag() {
//Check for errors
ata_writeByte(REG_STAT_CMD, CMD_DIAG);
_delay_ms(1);
ata_busyWait();
uint8_t errRes = (uint8_t)ata_readByte(REG_ERR_FEAT);
return errRes == 0x01 ? 0x00 : errRes;
}
//Writes a single byte to the drive
//Arguments: target address, data
void ata_writeByte(uint8_t addr, uint8_t dat) {
PORTB = addr;
DDRC = 0xFF;
DDRA = 0xFF;
PORTA = dat;
PORTC = 0;
PORTD &= ~(1 << PD7);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
PORTD |= (1 << PD7);
DDRC = 0x00;
DDRA = 0x00;
PORTB = NOP;
}
//Reads a single byte from the drive
//Arguments: source address
//Returns: read byte
uint8_t ata_readByte(uint8_t addr){
PORTB = addr;
DDRC = 0x00;
DDRA = 0x00;
PORTC = 0xFF;
PORTA = 0xFF;
PORTD &= ~(1 << PD6);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
char l = PINA;
PORTD |= (1 << PD6);
PORTB = NOP;
return l;
}
//Wait for the BSY bit to be 0 and for the DRDY bit to be 1
void ata_busyWait(){
while(1){
uint16_t t = ata_readByte(REG_STAT_CMD);
if((t & 0b10000000) == 0 && (t & 0b01000000) != 0){
break;
}
_delay_us(25);
}
}
//Wait for the DRY bit to be 1
void ata_waitForData(){
while(1){
uint8_t t = ata_readByte(REG_STAT_CMD);
if((t & 0b00001000) != 0){
break;
}
_delay_us(25);
}
}
//Read the drive's buffer
//Arguments: destination array, number of bytes to read (must be divisible by 2)
void ata_readBuffer(uint8_t *Buffer, uint16_t numBytes){
DDRC = 0x00;
DDRA = 0x00;
PORTC = 0xFF;
PORTA = 0xFF;
for(uint16_t i = 0; i < numBytes; i+=2){
while((ata_readByte(REG_STAT_CMD) & 0b00001000) == 0) {}
PORTB = REG_DATA;
PORTD &= ~(1 << PD6);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
Buffer[i] = PINA;
Buffer[i + 1] = PINC;
PORTD |= (1 << PD6);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
PORTB = NOP;
}
//Write to the drive's buffer
//Arguments: source array, number of bytes to write (must be divisible by 2)
void ata_writeBuffer(uint8_t *Buffer, uint16_t numBytes) {
DDRC = 0xFF;
DDRA = 0xFF;
PORTB = REG_DATA;
for(uint16_t i = 0; i < numBytes; i+=2){
PORTA = Buffer[i];
PORTC = Buffer[i + 1];
PORTD &= ~(1 << PD7);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
PORTD |= (1 << PD7);
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
PORTB = NOP;
}
//Reads a single sector
//Parameters: destination array, LBA address
//Returns: error code (0 = no error)
uint8_t ata_readSector(uint8_t *Buffer, uint32_t lba) {
return ata_readSectors(Buffer, lba, 1);
}
//Reads multiple sectors
//Parameters: destination array, LBA address, ammount of sectors to read
//Returns: error code (0 = no error)
uint8_t ata_readSectors(uint8_t *Buffer, uint32_t lba, uint8_t count) {
ata_busyWait();
ata_writeByte(REG_SECTOR_NUM, lba & 0x000000FFL);
ata_writeByte(REG_CYLINDER_LOW, (lba >> 8) & 0x000000FFL);
ata_writeByte(REG_CYLINDER_HIGH, (lba >> 16) & 0x000000FFL);
ata_writeByte(REG_DRIVE_HEAD, DRV_HEAD_BASE | ((lba >> 24) & 0x0FL));
ata_writeByte(REG_SECTOR_COUNT, count);
ata_writeByte(REG_STAT_CMD, CMD_READ_SEC);
_delay_us(0.25);
ata_busyWait();
if(ata_readByte(REG_STAT_CMD) & 0x01)
return ata_readByte(REG_ERR_FEAT);
ata_waitForData();
ata_readBuffer(Buffer, 512 * (uint16_t)count);
return (ata_readByte(REG_STAT_CMD) & 0x01) ? ata_readByte(REG_ERR_FEAT) : 0;
}
//Writes a single sector
//Parameters: source array, LBA address
//Returns: error code (0 = no error)
uint8_t ata_writeSector(uint8_t *Buffer, uint32_t lba) {
return ata_writeSectors(Buffer, lba, 1);
}
//Writes multiple sectors
//Parameters: source array, LBA address, ammount of sectors to write
//Returns: error code (0 = no error)
uint8_t ata_writeSectors(uint8_t *Buffer, uint32_t lba, uint8_t count) {
ata_busyWait();
ata_writeByte(REG_SECTOR_NUM, lba & 0x000000FFL);
ata_writeByte(REG_CYLINDER_LOW, (lba >> 8) & 0x000000FFL);
ata_writeByte(REG_CYLINDER_HIGH, (lba >> 16) & 0x000000FFL);
ata_writeByte(REG_DRIVE_HEAD, DRV_HEAD_BASE | ((lba >> 24) & 0x0FL));
ata_writeByte(REG_SECTOR_COUNT, count);
ata_writeByte(REG_STAT_CMD, CMD_WRITE_SEC);
_delay_us(0.15);
ata_busyWait();
ata_waitForData();
ata_writeBuffer(Buffer, (uint16_t)count * 512);
ata_busyWait();
return (ata_readByte(REG_STAT_CMD) & 0x01) ? ata_readByte(REG_ERR_FEAT) : 0;
}
//Rewrites this drive's MBR
//THE PREVIOUS MBR, INCLUDING THE PARTITION TABLE WILL BE OVERWRITEN! BE CAREFULL WITH THIS!
//Returns returned error code from write operation (0 = no error)
uint8_t ata_rewriteMBR() {
uint8_t newMBR[512];
for(uint64_t i = 0; i < 512; i++) newMBR[i] = 0x00;
newMBR[510] = 0x55;
newMBR[511] = 0xAA;
uint8_t err = ata_writeSector(newMBR, 0);
if(err != 0) return err;
//Clear local partition table
for(uint8_t i = 0; i < 4; i++){
partitions[i].type = 0x00;
partitions[i].start = 0x00;
partitions[i].start = 0x00;
partitions[i].end = 0x00;
}
return 0;
}
//Creates a new primary partition
//THE PREVIOUS PARTITION ENTRY AT id WILL BE OVERWRITEN! BE CAREFULL WITH THIS ALSO!
//Returns error code (0 = no error)
uint8_t ata_createPartition(uint8_t id, uint32_t start, uint32_t size, uint8_t type) {
if(id > 4 || type == 0x00 || start + size > driveInfo.sizeinsectors || start == 0 || size == 0) return ERR_INVALID_PARTITION; //There can't be more then 4 partitions and type 0x00 stands for unallocated space (can't be a partition)
//Check if new partition does not overlap with existing ones
for(uint8_t i = 0; i < 4; i++){
if(i == id || partitions[i].size == 0) continue;
if(start >= partitions[i].start && start <= partitions[i].end) return ERR_INVALID_PARTITION;
if(start + size >= partitions[i].start) return ERR_INVALID_PARTITION;
}
//Edit MBR to include partition
uint8_t MBR[512];
uint8_t err = ata_readSector(MBR, 0);
if(err != 0) return err;
MBR[446 + id * 16 + 8 + 3] = (uint8_t)(start >> (uint32_t)24);
MBR[446 + id * 16 + 8 + 2] = (uint8_t)(start >> (uint32_t)16);
MBR[446 + id * 16 + 8 + 1] = (uint8_t)(start >> (uint32_t)8);
MBR[446 + id * 16 + 8 + 0] = (uint8_t)(start);
MBR[446 + id * 16 + 12 + 3] = (uint8_t)(size >> (uint32_t)24);
MBR[446 + id * 16 + 12 + 2] = (uint8_t)(size >> (uint32_t)16);
MBR[446 + id * 16 + 12 + 1] = (uint8_t)(size >> (uint32_t)8);
MBR[446 + id * 16 + 12 + 0] = (uint8_t)(size);
MBR[446 + id * 16] = 0x80;
MBR[446 + id * 16 + 4] = type;
//Rewrite modded MBR
err = ata_writeSector(MBR, 0);
if(err != 0) return err;
//Update local partition table
partitions[id].start = start;
partitions[id].size = size;
partitions[id].end = start + size;
partitions[id].type = type;
return 0;
}
//Getters
char *ata_getModel() {
return driveInfo.model;
}
char *ata_getSerial() {
return driveInfo.serial;
}
uint32_t ata_getSectorCount() {
return driveInfo.sizeinsectors;
}
uint8_t ata_isLBASupported() {
return driveInfo.LBAsupported;
}
uint64_t ata_getSizeInBytes() {
return (uint64_t)driveInfo.sizeinsectors * (uint64_t)512;
}
uint32_t ata_getPartitionLocation(uint8_t partitionID) {
return partitions[partitionID].start;
}
uint32_t ata_getPartitionSize(uint8_t partitionID) {
return partitions[partitionID].size;
}
uint32_t ata_getEndOfPartition(uint8_t partitionID) {
return partitions[partitionID].end;
}
uint8_t ata_getPartitionType(uint8_t partitionID) {
return partitions[partitionID].type;
}
uint8_t ata_getPartitionCount() {
return 4;
}