I am trying to setup simple PIO access to my hard drive but I've hit a wall on the very first step towards the goal.
First step to working with ATA device is to read it's state register and wait until it's BSY (7th) bit is low. I've got the program doing that, but for some reason when reading the state register it always gives me 0xFF instead. Here is the program sample written in C++:
#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>
#define DRDY_OFFSET 6
#define BSY_OFFSET 7
const int STATE[2] = { 0x1F7, 0x177 };
bool requestPrivilege() {
if (iopl(3) == -1) {
printf("Unable to request privilege level III. Exiting.\n");
exit(1);
}
}
bool wait(auto lambda) {
int maxAttempts = 30 * 1000;
while((maxAttempts--)) {
if (lambda()) return true;
}
return false;
}
bool waitIdle(int channel) {
auto lambda = [=]() -> bool {
printf("%x\n", inb_p(STATE[channel]));
return !(inb_p(STATE[channel]) & (1 << BSY_OFFSET));
};
return wait(lambda);
}
bool waitReady(int channel) {
auto lambda = [=]() -> bool {
return inb_p(STATE[channel]) & (1 << DRDY_OFFSET);
};
return wait(lambda);
}
int main() {
requestPrivilege();
if (!waitIdle(0)) {
printf("BSY waiting timeout.\n");
exit(1);
};
if (!waitReady(0)) {
printf("DRDY waiting timeout.\n");
exit(1);
};
// //
// DO SOMETHING WITH READY DEVICE HERE //
// //
return 0;
}
Would you please look at the snippet and tell me what's wrong?