Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


ATA Pass through read using DeviceIoControl returns error : error_invalid_function

kernelboikernelboi Member Posts: 19

I'm trying to use ATA pass through to read the disk sectors, but DeviceIoControl is returns 0, and the GetLastError is 1 which is error_invalid_function, what am i doing wrong here? i just want to read the first sector of disk. tried the command 0x20 instead of 0x24 too and still no luck

This is my code :

    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;


    HANDLE hDevice = CreateFileA("\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

    if (hDevice == NULL) return 0;

    pATAData = (ATA_PASS_THROUGH_EX*)Buffer;

    ZeroMemory(pATAData, dataSize); 

    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[6] = 0x24;
    pATAData->CurrentTaskFile[7] = 0x00;
    pATAData->AtaFlags = ATA_FLAGS_48BIT_COMMAND | ATA_FLAGS_DRDY_REQUIRED | ATA_FLAGS_DATA_IN;

    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[6] = 0x00;
    pATAData->PreviousTaskFile[7] = 0x00;


    status = DeviceIoControl(hDevice, IOCTL_ATA_PASS_THROUGH, pATAData, dataSize, Buffer, dataSize, &bytescopied, NULL);

Comments

  • kernelboikernelboi Member Posts: 19
    edited July 1

    Sorry guys there is a mistake in the code, in my actual code the if (hDevice == NULL) is changed to if (hDevice == INVALID_HANDLE_VALUE) but i forgot to edit it.

    and this change doesn't effect the result, still the same GetLastError message.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Internals & Software Drivers 30 Nov 2020 LIVE ONLINE
Writing WDF Drivers 7 Dec 2020 LIVE ONLINE
Developing Minifilters Early 2021 LIVE ONLINE