I want to read a particular sector(MBR Sector) of a disk using ATA commands in vc++. I am new to VC++ so i am facing a problem when sending command to disk using DeviceIoControl. I am providing a code that i am using to read out a sector using command 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 );
I can't undersatnd that what's wrong in this code and what i am missing here, but it is not working. What i am missing here ? If there is a problem with parametres of PATA_PASS_THROUGH_EX structure than tell how to read first sector(MBR).
Thankyou everyone for your help. I got the solution. A little bit thing that i have not noticed. That is Ata Flags. I have to send multiple flags. Eg.
pATAData->ataFlags = ATA_FLAGS_48BIT_COMMAND | ATA_FLAGS_DRDY_REQUIRED | ATA_FLAGS_DATA_IN
and also send the ATA command Opcode in PreviousTaskFile[6] also. And the few things to be noticed is What is the block size of the HDD? It could be more than 512 bytes, especially if it is a large disk like(1TB...4TB). As such, you'll need to adjust the sizes accordingly. You can see what the size is by doing the EC identify ATA command, and then look at the resulting data structure.