如何使用ATA命令读取磁盘的特定扇区?

时间:2015-05-08 04:59:32

标签: c++ winapi hard-drive sata

我想在vc ++中使用ATA命令读取磁盘的特定扇区(MBR扇区)。我是VC ++的新手,所以当使用 DeviceIoControl 将命令发送到磁盘时,我遇到了问题。我提供的代码用于使用命令Read Sector(s)(0x20)读出扇区。

  BOOL status = FALSE;
 PATA_PASS_THROUGH_EX pATAData;
DWORD dataSize = sizeof(ATA_PASS_THROUGH_EX) + 512;
BYTE Buffer[sizeof(ATA_PASS_THROUGH_EX) + 512];
DWORD bytescopied = 0;

    pATAData = (ATA_PASS_THROUGH_EX*)Buffer;

    ZeroMemory(pATAData,dataSize); // clears the buffer

    pATAData->Length = sizeof(ATA_PASS_THROUGH_EX);
    pATAData->DataBufferOffset = sizeof(ATA_PASS_THROUGH_EX);
    pATAData->DataTransferLength = 512;
    pATAData->TimeOutValue = 2;

    pATAData->CurrentTaskFile[1] = 0x01;
    pATAData->CurrentTaskFile[2] = 0x00;
    pATAData->CurrentTaskFile[3] = 0x00;
    pATAData->CurrentTaskFile[4] = 0x00;
    pATAData->AtaFlags =ATA_FLAGS_DATA_IN;

    pATAData->CurrentTaskFile[6] = 0x20; // command Read Sector(s)(0x20)
    /* sends the command to the device, **hDevice** is device handle*/
    status = DeviceIoControl(hDevice, IOCTL_ATA_PASS_THROUGH, pATAData, dataSize,Buffer, dataSize, &bytescopied, NULL );

我不能理解这段代码中的错误以及我在这里缺少的东西,但它不起作用。 我在这里缺少什么?如果PATA_PASS_THROUGH_EX结构的参数存在问题,而不是告诉如何读取第一个扇区(MBR)。

3 个答案:

答案 0 :(得分:0)

感谢您的帮助。我得到了解决方案。我没有在CurrentTaskFile中分配设备句柄。

pATAData->CurrentTaskFile[5] = (UCHAR)hDevice;

但IDENTIFY_DEVICE(ECh)命令在没有这个的情况下成功发送。我不知道这是对还是错,但这是有效的。

答案 1 :(得分:0)

谢谢你们的帮助。经过长时间的讨论后,我发现我必须为大磁盘发送扩展命令。但现在我发送Read Sector(s)Ext(0x24 oppcode)命令。

BOOL status = FALSE;
PATA_PASS_THROUGH_EX pATAData;
DWORD dataSize = sizeof(ATA_PASS_THROUGH_EX) + 512;
BYTE Buffer[sizeof(ATA_PASS_THROUGH_EX) + 512];
DWORD bytescopied = 0;

pATAData = (ATA_PASS_THROUGH_EX*)Buffer;

ZeroMemory(pATAData,dataSize); // clears the buffer

pATAData->Length = sizeof(ATA_PASS_THROUGH_EX);
pATAData->DataBufferOffset = sizeof(ATA_PASS_THROUGH_EX);
pATAData->DataTransferLength = 512;
pATAData->TimeOutValue = 2;

pATAData->CurrentTaskFile[0] = 0x00;
pATAData->CurrentTaskFile[1] = 0x01;
pATAData->CurrentTaskFile[2] = 0x01;
pATAData->CurrentTaskFile[3] = 0x00;
pATAData->CurrentTaskFile[4] = 0x00;
pATAData->CurrentTaskFile[5] = 0x40;
pATAData->CurrentTaskFile[7]=  0x00;
pATAData->AtaFlags =ATA_FLAGS_48BIT_COMMAND;

pATAData->PreviousTaskFile[0] = 0x00;
pATAData->PreviousTaskFile[1] = 0x00;
pATAData->PreviousTaskFile[2] = 0x00;
pATAData->PreviousTaskFile[3] = 0x00;
pATAData->PreviousTaskFile[4] = 0x00;
pATAData->PreviousTaskFile[5] = 0x04;
pATAData->PreviousTaskFile[7]=  0x00;

pATAData->CurrentTaskFile[6] = 0x24; // command Read Sector(s) Ext(0x24)
/* sends the command to the device, **hDevice** is device handle*/
status = DeviceIoControl(hDevice, IOCTL_ATA_PASS_THROUGH, pATAData, dataSize,Buffer, dataSize, &bytescopied, NULL );

但这是同样的问题。命令执行成功但它没有读取任何扇区。我无法找到任何错误。

答案 2 :(得分:0)

谢谢大家的帮助。我得到了解决方案。我没有注意到的一点点。那是Ata Flags。我必须发送多个标志。例如。

pATAData->ataFlags = ATA_FLAGS_48BIT_COMMAND | ATA_FLAGS_DRDY_REQUIRED | ATA_FLAGS_DATA_IN

并且还在PreviousTaskFile [6]中发送ATA命令Opcode。 需要注意的几件事情是 硬盘的块大小是多少?它可能超过512个字节,特别是如果它是一个像(1TB ... 4TB)的大磁盘。因此,您需要相应地调整大小。您可以通过执行EC识别ATA命令来查看大小,然后查看生成的数据结构。