使用带有ATA_PASS_THROUGH_EX的DeviceIoControl()的SECURITY_SET_PASSWORD失败

时间:2015-09-22 09:57:05

标签: deviceiocontrol

我已经使用ATA命令SECURITY_SET_PASSWORD在DeviceIoControl()函数中尝试了ATA_PASS_THROUGH_EX或ATA_PASS_THROUGH_DIRECT,但它始终因GetLastError()= 87(参数无效)而失败。但是ATA命令IDENTIFY总是成功的。

操作系统环境:Windows 8.1 64位,Visual Studio 2010,我的应用程序具有管理员权限。

我的目标是在64位Windows 8.1中发布安全删除不像此页面:https://ata.wiki.kernel.org/index.php/ATA_Secure_Erase

像上面的网页一样,当我尝试使用' hdparm'在ubuntu live CD中发出安全擦除对于相同的硬件和相同的SSD都没有问题。

请您指导一下我的代码有什么问题吗?

    m_h = CreateFile("\\\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  
    if ( m_h == INVALID_HANDLE_VALUE ) 
        return 0;

#if 1
    char buffer[512 + sizeof(ATA_PASS_THROUGH_EX)] = { 0 };

    ATA_PASS_THROUGH_EX& PTE = *(ATA_PASS_THROUGH_EX *)buffer;

    PTE.Length = sizeof(PTE);
    PTE.DataTransferLength = 512;
    PTE.DataBufferOffset = sizeof(ATA_PASS_THROUGH_EX);
    PTE.TimeOutValue = 10;
    PTE.AtaFlags = ATA_FLAGS_DATA_OUT | ATA_FLAGS_DRDY_REQUIRED;

    IDEREGS* ir = (IDEREGS*)PTE.CurrentTaskFile;
    ir->bCommandReg = 0xF1;

    strcpy(buffer + sizeof(ATA_PASS_THROUGH_EX) + 2, "test");

    DWORD bytes = 0;
    if (DeviceIoControl(m_h, IOCTL_ATA_PASS_THROUGH, &buffer, sizeof(buffer), &buffer, sizeof(buffer), &bytes, 0) == 0 ) {
        DWORD er = GetLastError();
        printf("error: %d\n", GetLastError());
        return false;
    }
#else   
    DWORD dataSize = sizeof(ATA_PASS_THROUGH_DIRECT) + 512;
    ATA_PASS_THROUGH_DIRECT* pPTD = (ATA_PASS_THROUGH_DIRECT*) VirtualAlloc(NULL, dataSize, MEM_COMMIT, PAGE_READWRITE);

    pPTD->Length = sizeof(ATA_PASS_THROUGH_DIRECT); 
    pPTD->DataTransferLength = 512; 
    pPTD->DataBuffer = ((char*)pPTD) + sizeof(ATA_PASS_THROUGH_DIRECT); // sizeof(ATA_PASS_THROUGH_DIRECT);
    pPTD->TimeOutValue =  10;
    pPTD->AtaFlags = ATA_FLAGS_DATA_OUT | ATA_FLAGS_DRDY_REQUIRED;
    pPTD->CurrentTaskFile[1] = 0x01;
    pPTD->CurrentTaskFile[6] = 0xF1;

    strcpy( ((char*)pPTD->DataBuffer) + 2, "test");

    DWORD bytes = 0;
    if (!DeviceIoControl(m_h, IOCTL_ATA_PASS_THROUGH_DIRECT,  pPTD,  dataSize, pPTD, dataSize, &bytes, NULL)) {
        printf("error: %d\n", GetLastError());
        return false;
    }
#endif  
    return true;

1 个答案:

答案 0 :(得分:0)

我认为这些ATA命令被操作系统阻止:安全设置密码,安全删除单元。因为我在Windows 7 32位和Windows 7 64位但在Windows 8(32位/ 64位)和Windows 10(32位/ 64位)上成功运行了以上程序,所以同一程序显示无效参数错误(错误代码87)。