我想使用VC 中的ATA命令读取磁盘的特定扇区(MBR扇区)。我是VC 的新手,因此使用 deviceiocontrol 将命令发送到磁盘时,我正面临问题。我正在提供使用命令读取扇区(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)。
谢谢大家的帮助。我得到了解决方案。我没有注意到的一点。那是ATA标志。我必须发送多个标志。例如。
pATAData->ataFlags = ATA_FLAGS_48BIT_COMMAND | ATA_FLAGS_DRDY_REQUIRED | ATA_FLAGS_DATA_IN
,还将在上taskFile [6]中发送ATA命令OPCODE。要注意的几件事是HDD的块大小是多少?它可能超过512个字节,尤其是如果它是一个大磁盘,例如(1TB ... 4TB)。因此,您需要相应地调整尺寸。您可以通过执行EC标识ATA命令查看大小是什么,然后查看结果数据结构。
感谢您的帮助。我得到了解决方案。我没有在CurrentTaskFile中分配设备句柄。
pATAData->CurrentTaskFile[5] = (UCHAR)hDevice;
但是,sideen_device(ech)命令正在成功地发送。我不知道这是对与错,但这在起作用。
感谢您的帮助。经过长时间的讨论,我发现我必须发送大磁盘的扩展命令。但是现在我要发送读取扇区(S)ext(0x24 oppode)命令。
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 );
,但这是同样的问题。命令成功执行,但没有读取任何扇区。我找不到任何错误。