LBA PIO reading not working
Posted: Thu Aug 07, 2008 10:55 am
When I try to read data from a hard drive with LBA28, I get only zeros as result.
Oddly, when I use 0xA0 instead of 0xE0, I get a good result, but only for the first sector. I haven't got a clue where to look for the error, I've tried so many things. Have you got an idea?
Code: Select all
int waituntilbusy(unsigned int base)
{
unsigned int maxtry = 300000;
unsigned char r;
while(((r = inb(base + 0x07)) & 0x80) || !(r & 0x40))
{
maxtry--;
if (maxtry == 0)
{
puts("Error: read time-out.\n");
return 0;
}
}
return 1;
}
int GetSector(unsigned char ide, unsigned long drive, unsigned long sector)
{
unsigned char buffer[512];
unsigned int idx;
unsigned short tmpword;
unsigned short base;
if (ide == 0)
base = 0x1F0;
else
if (ide == 1)
base = 0x170;
else
return 0;
// first set the read parameters
outb(base + 1, 0x0); // reset the error flag
outb(base + 2, 0x1); // read 1 sector
outb(base + 3, (unsigned char)sector); // first address byte
outb(base + 4, (unsigned char)(sector >> 8)); // second address byte
outb(base + 5, (unsigned char)(sector >> 16)); // third address byte
outb(base + 6, 0xE0 | (drive << 4) | ((sector >> 24) & 0x0F)); // select drive
outb(base + 7, 0x20); // read with retry
if (!waituntilbusy(base))
return 0;
for (idx = 0; idx < 256; idx++)
{
tmpword = inw(base); // read a word
buffer[idx * 2] = (unsigned char)tmpword;
buffer[idx * 2 + 1] = (unsigned char)(tmpword >> 8);
}
for (idx = 0; idx < 512; idx++)
putch(buffer[idx]);
return 1;
}