0xFF while reading the ATA status register

I'm trying to set up easy PIO access to my hard drive, but I hit the wall in the very first step towards the goal.

The first step to working with an ATA device is to read the status register and wait until the BSY bit (7th) is low. I have a program that does this, but for some reason, when reading the status register, it always gives me 0xFF. Here is a sample program 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;
}

      

Could you take a look at the snippet and tell me what is wrong?

+3


source to share





All Articles