在 Windows 中直接向设备发送 ATA 命令?
我正在尝试将 ATA 命令发送到 Windows 中的物理磁盘,并从设备获取响应。
注意:在这种情况下,我想发送
识别设备
(0xEC) 命令。设备将响应 512 字节的数据块。 (在 特别是我对位 0 感兴趣 字 119 - 设备的支持TRIM
命令)。
我知道我需要使用 CreateFile< /code>
打开设备:
handle = CreateFile(
"\\.\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ,
nil, // no security attributes
OPEN_EXISTING,
0, // flags and attributes
nil // no template file
);
但此后我不知道该怎么做。
我想过使用 [DeviceIoControl][4]
发送 0xEC
:
// const ATACommand_IdentifyDevice = 0xEC;
uint bytesReturned = 0;
DeviceIoControl(handle,
0xEC, // IO Control Code
nil, // input buffer not needed
0, // input buffer is zero bytes
@buffer, // output buffer to store the returned 512-bytes
512, // output buffer is 512 bytes long
out bytesReturned,
nil // not an overlapped operation
);
但这完全是错误的。发送到 DeviceIoControl 的 IoControlCode 必须是有效的 IO_CTL,使用宏构建:
#define CTL_CODE(DeviceType, Function, Method, Access) (
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
)
查看 SDK,有一些有效的磁盘管理控制代码 ,例如:
- IOCTL_DISK_CREATE_DISK
- IOCTL_DISK_GET_DRIVE_GEOMETRY
- IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
- IOCTL_DISK_GET_PARTITION_INFO
- IOCTL_STORAGE_QUERY_PROPERTY
但它们都不是 IDENTIFY DEVICE 命令,或返回它返回的任何内容。
所以我相信我必须使用一些“原始”方法来发送命令。
四处搜索,我发现了未记录的 IOCTL,
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
当您分解 IOCTL 部分时,这意味着:
Custom: (0)
Device Type: (7) FILE_DEVICE_DISK
Required Access: (3) METHOD_NEITHER
Custom: (0)
Function Code: (34)
Transfer Type: (0)
但是没有任何文档说明 inputBuffer
必须包含什么、它的大小以及它的 outputBuffer
将包含,或其必需的。我也无法弄清楚 functionCode
34 (0x22) 是什么。
我的问题:如何将原始 ATA 命令(例如 0xEC)发送到 ATA 设备并读取其响应?
另请参阅
答案片段
使用读写访问权限打开驱动器:
handle = CreateFile(
"\\.\PhysicalDrive0",
GENERIC_READ or GENERIC_WRITE, // IOCTL_ATA_PASS_THROUGH requires read-write
FILE_SHARE_READ,
nil, // no security attributes
OPEN_EXISTING,
0, // flags and attributes
nil // no template file
);
设置 ATA_PASS_THROUGH_EX
结构作为我们的输入缓冲区,与 IOCTL_ATA_PASS_THROUGH
配合使用 IO 控制代码:
ATA_PASS_THROUGH_EX inputBuffer;
inputBuffer.Length = sizeof(ATA_PASS_THROUGH_EX);
inputBuffer.AtaFlags = ATA_FLAGS_DATA_IN;
inputBuffer.DataTransferLength = 0;
inputBuffer.DataBufferOffset = 0;
// todo: put the ATA command (e.g. 0xEC) somewhere
uint inputBufferSize = sizeof(ATA_PASS_THROUGH_EX);
设置一个输出缓冲区以保存来自驱动器的预期 512 字节响应:
Byte[] outputBuffer = new Byte[512];
uint outputBufferSize = 512;
调用 DeviceIoControl
:
int ioControlCode = IOCTL_ATA_PASS_THROUGH; // or maybe IOCTL_ATA_PASS_THROUGH_DIRECT
uint bytesReturned = 0;
DeviceIoControl(handle, ioControlCode,
inputBuffer, inputBufferSize,
outputBuffer, outputBufferSize,
out bytesReturned,
nil // not an overlapped operation
);
关闭文件句柄:
handle.Close();
I’m trying to send ATA commands to a physical disk in Windows, and get the response from the device.
Note: In this case I want to send the
IDENTIFY DEVICE
(0xEC)
command. The device will respond with
a 512-byte block of data. (In
particular I’m interested in bit 0 of
word 119 - the device’s support for
theTRIM
command).
I know that I need to use CreateFile
to open the device:
handle = CreateFile(
"\\.\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ,
nil, // no security attributes
OPEN_EXISTING,
0, // flags and attributes
nil // no template file
);
But after this I’m stymied about what to do.
I thought about sending 0xEC
using [DeviceIoControl][4]
:
// const ATACommand_IdentifyDevice = 0xEC;
uint bytesReturned = 0;
DeviceIoControl(handle,
0xEC, // IO Control Code
nil, // input buffer not needed
0, // input buffer is zero bytes
@buffer, // output buffer to store the returned 512-bytes
512, // output buffer is 512 bytes long
out bytesReturned,
nil // not an overlapped operation
);
But this is completely wrong. An IoControlCode sent to DeviceIoControl must be a valid IO_CTL, which are built using the macro:
#define CTL_CODE(DeviceType, Function, Method, Access) (
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
)
Looking at the SDK, there are a number of valid Disk Management Control Codes, e.g.:
- IOCTL_DISK_CREATE_DISK
- IOCTL_DISK_GET_DRIVE_GEOMETRY
- IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
- IOCTL_DISK_GET_PARTITION_INFO
- IOCTL_STORAGE_QUERY_PROPERTY
But none of them are IDENTIFY DEVICE
command, or return anything it returns.
So I believe I have to use some “raw” method of sending commands.
Searching around, I came across and undocumented IOCTL
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
Which when you break down the IOCTL pieces, means:
Custom: (0)
Device Type: (7) FILE_DEVICE_DISK
Required Access: (3) METHOD_NEITHER
Custom: (0)
Function Code: (34)
Transfer Type: (0)
But there is no documentation anywhere on what the inputBuffer
must contain, its size, and what its outputBuffer
will contain, or its required. Nor can I figure out what functionCode
34 (0x22) is.
My question: How do I send raw ATA commands (e.g. 0xEC) to an ATA device, and read its response?
See also
- IOCTL_ATA_PASS_THROUGH Control Code
- IOCTL_ATA_PASS_THROUGH_DIRECT Control Code
- ATA_PASS_THROUGH_EX Structure
Answer pieces
Open the drive with ReadWrite access:
handle = CreateFile(
"\\.\PhysicalDrive0",
GENERIC_READ or GENERIC_WRITE, // IOCTL_ATA_PASS_THROUGH requires read-write
FILE_SHARE_READ,
nil, // no security attributes
OPEN_EXISTING,
0, // flags and attributes
nil // no template file
);
Setup an ATA_PASS_THROUGH_EX
structure as our input buffer to use with IOCTL_ATA_PASS_THROUGH
IO control code:
ATA_PASS_THROUGH_EX inputBuffer;
inputBuffer.Length = sizeof(ATA_PASS_THROUGH_EX);
inputBuffer.AtaFlags = ATA_FLAGS_DATA_IN;
inputBuffer.DataTransferLength = 0;
inputBuffer.DataBufferOffset = 0;
// todo: put the ATA command (e.g. 0xEC) somewhere
uint inputBufferSize = sizeof(ATA_PASS_THROUGH_EX);
Setup an output buffer to hold our expected 512-byte response from the drive:
Byte[] outputBuffer = new Byte[512];
uint outputBufferSize = 512;
Call DeviceIoControl
:
int ioControlCode = IOCTL_ATA_PASS_THROUGH; // or maybe IOCTL_ATA_PASS_THROUGH_DIRECT
uint bytesReturned = 0;
DeviceIoControl(handle, ioControlCode,
inputBuffer, inputBufferSize,
outputBuffer, outputBufferSize,
out bytesReturned,
nil // not an overlapped operation
);
Close the file handle:
handle.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 IOCTL_ATA_PASS_THROUGH/IOCTL_ATA_PASS_THROUGH_DIRECT,这些都有很好的文档记录。此外,您还需要 CreateFile 的 GENERIC_READ|GENERIC_WRITE 访问权限。
请注意,XP SP2 之前的版本不能正确支持这些。另外,如果您有基于 nForce 且带有 nvidia 驱动程序的 MB,您的 SATA 驱动器将显示为 SCSI,并且您无法使用此直通。
在某些情况下,SMART IOCTL(例如SMART_RCV_DRIVE_DATA)将在nForce 驱动程序上工作。您可以使用它们来获取 IDENTIFY 和 SMART 数据,但仅此而已。
开源 smartmontools 是开始寻找示例代码的好地方。
编辑:来自与 ATA 设备通信的应用程序的示例。
编辑:没有外部依赖项的示例。
IDENTIFY 需要 512 字节的数据缓冲区:
按照 ATA 规范中的规定设置 IDE 寄存器。
IDENTIFY 既不是 48 位也不是 DMA,它从设备读取:
执行 ioctl:
在这里您应该插入错误检查,既可以从 DeviceIOControl 中插入错误检查,也可以通过查看 IDEREGS 来查找设备报告的错误。
获取 IDENTIFY 数据,假设您已经定义了一个 struct IdentityData
You need to use IOCTL_ATA_PASS_THROUGH/IOCTL_ATA_PASS_THROUGH_DIRECT, these are quite well documented. Also, you need GENERIC_READ|GENERIC_WRITE access for CreateFile.
Be aware that pre XP SP2 does not support these properly. Also, if you have a nForce based MB with nvidia drivers, your SATA drives will appear as SCSI and you can't use this passthrough.
In some cases, the SMART IOCTL's (e.g. SMART_RCV_DRIVE_DATA) will work on nForce drivers. You can use these to get IDENTIFY and SMART data, but not much else.
The open source smartmontools is a good place to start looking for sample code.
EDIT: Sample from an app talking to ATA devices.
EDIT: Sample without external dependencies.
IDENTIFY requires a 512 byte buffer for data:
Set up the IDE registers as specified in ATA spec.
IDENTIFY is neither 48-bit nor DMA, it reads from the device:
Do the ioctl:
Here you should insert error checking, both from DeviceIOControl and by looking at IDEREGS for device reported errors.
Get the IDENTIFY data, assuming you have defined a struct IdentifyData
基于https://stackoverflow.com/a/5071027/15485的答案Erik 我编写了以下独立代码。我在配备 SSD 磁盘并运行 Windows 7 的 DELL 笔记本电脑上对其进行了测试。
Based on the answer https://stackoverflow.com/a/5071027/15485 by Erik I wrote the following self-contained code. I tested it on a DELL laptop with an SSD disk and running Windows 7.
您需要IOCTL_ATA_PASS_THROUGH控制代码
You need IOCTL_ATA_PASS_THROUGH Control Code