下文转载自http://blog.51cto.com/cutebunny/704266
我们在第七节http://cutebunny.blog.51cto.com/301216/625577中探讨了如何获取系统中所有磁盘的磁盘号。但是在很多情况下,仅仅是磁盘号并不能满足我们的需求,我们希望在应用程序界面上显示更详细的磁盘信息。通过前面几节的方法,我们已经可以获得许多磁盘信息,例如磁盘大小,柱面,扇区,分区等等。但是如下图所示,我们在设备管理器中看到的“WDC WD1600AAJS-08B4A0”这种更为人性化的磁盘类型描述,至今为止我们还没有办法获得。本节就来解决这个问题。
提前声明,本节涉及一些ATA/APAPI
内容,我本人理解的不是十分透彻,在网上搜集了很多相关资料才调通了后面示例中的代码。如有不正确之处欢迎大家指教。
对于类似于“WDC WD1600AAJS-08B4A0
”的磁盘型号描述,下文中称为model number
,
微软在VC/MFC
环境中没有开放获取的方法。所以我们要用到ATA/APAPI
的IDENTIFY DEVICE
指令。ATA/APAPI
是国际组织T13
起草和发布的IDE/EIDE/UDMA
硬盘及其它可移动存储设备与主机接口的标准,至今已经到了ATA/APAPI-7
版本。该接口标准规定了ATA/ATAPI
设备的输入输出寄存器和指令集。
首先介绍几个新的DeviceIoControl
的控制码和相关数据结构。
1.
控制码SMART_GET_VERSION
控制码SMART_GET_VERSION
在MSDN
中的描述如下,
Operation
Returns version information, a capabilities mask, and a bitmask for the device. This IOCTL must be handled by drivers that support Self-Monitoring Analysis and Reporting Technology (SMART).
好,我们要的就是这个bitmask
,它是我们后面完成任务的关键参数。
而这个bitmask
是结构体GETVERSIONINPARAMS
的成员,此结构体是作为控制码为SMART_GET_VERSION
的DeviceIoControl()
函数的输出参数的。定义如下:
typedef struct _GETVERSIONINPARAMS {UCHAR bVersion ;UCHAR bRevision;UCHAR bReserved ;UCHAR bIDEDeviceMap ;ULONG fCapabilities;ULONG dwReserved [4];} GETVERSIONINPARAMS, *PGETVERSIONINPARAMS, *LPGETVERSIONINPARAMS;
其中
UCHAR bIDEDeviceMap
就是我们所要的
bitmask
,具体定义为
bIDEDeviceMap
Contains the bitmap. The following table explains the meaning of the bitmap:
Bitmap Flags
|
Meaning
|
Bit 0 is set to 1.
|
The device is an IDE drive, and it is the master device on the primary channel.
|
Bit 1 is set to 1.
|
The device is an IDE drive, and it is the slave device on the primary channel.
|
Bit 2 is set to 1.
|
The device is an IDE drive, and it is the master device on the secondary channel.
|
Bit 3 is set to 1.
|
The device is an IDE drive, and it is the slave device on the secondary channel.
|
Bit 4 is set to 1.
|
The device is an ATAPI drive, and it is the master device on the primary channel.
|
Bit 5 is set to 1.
|
The device is an ATAPI drive, and it is the slave device on the primary channel.
|
Bit 6 is set to 1.
|
The device is an ATAPI drive, and it is the master device on the secondary channel.
|
Bit 7 is set to 1.
|
The device is an ATAPI drive, and it is the slave device on the secondary channel.
|
从这里可以看出,这个关键的参数事实上表明了驱动器类型和所在的channel
。
2.
控制码SMART_RCV_DRIVE_DATA
使用控制码SMART_RCV_DRIVE_DATA
的DeviceIoControl()
函数的输入输出参数分别为SENDCMDINPARAMS
和SENDCMDOUTPARAMS
。我们在输入参数SENDCMDINPARAMS
中填入合适的信息,包括刚才那个关键的bitmask
,就能在输出参数SENDCMDOUTPARAMS
中拿到我们需要的model number
。SENDCMDOUTPARAMS
结构定义如下,
typedef struct _SENDCMDOUTPARAMS {ULONG cBufferSize ;DRIVERSTATUS DriverStatus;UCHAR bBuffer[1];} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS ;
model number
就存在于bBuffer
中,bBuffer
的格式需要依据ATA/ATAPI
中的定义解析:
typedef struct _IDINFO{USHORT wGenConfig; // WORD 0: 基本信息字USHORT wNumCyls; // WORD 1: 柱面数USHORT wReserved2; // WORD 2: 保留USHORT wNumHeads; // WORD 3: 磁头数USHORT wReserved4; // WORD 4: 保留USHORT wReserved5; // WORD 5: 保留USHORT wNumSectorsPerTrack; // WORD 6: 每磁道扇区数USHORT wVendorUnique[3]; // WORD 7-9: 厂家设定值CHAR sSerialNumber[20]; // WORD 10-19:序列号USHORT wBufferType; // WORD 20: 缓冲类型USHORT wBufferSize; // WORD 21: 缓冲大小USHORT wECCSize; // WORD 22: ECC校验大小CHAR sFirmwareRev[8]; // WORD 23-26: 固件版本CHAR sModelNumber[40]; // WORD 27-46: 内部型号USHORT wMoreVendorUnique; // WORD 47: 厂家设定值USHORT wReserved48; // WORD 48: 保留
struct {USHORT reserved1:8;USHORT DMA:1; // 1=支持DMAUSHORT LBA:1; // 1=支持LBAUSHORT DisIORDY:1; // 1=可不使用IORDYUSHORT IORDY:1; // 1=支持IORDYUSHORT SoftReset:1; // 1=需要ATA软启动USHORT Overlap:1; // 1=支持重叠操作USHORT Queue:1; // 1=支持命令队列USHORT InlDMA:1; // 1=支持交叉存取DMA} wCapabilities; // WORD 49: 一般能力
USHORT wReserved1; // WORD 50: 保留USHORT wPIOTiming; // WORD 51: PIO时序USHORT wDMATiming; // WORD 52: DMA时序struct {USHORT CHSNumber:1; // 1=WORD 54-58有效USHORT CycleNumber:1; // 1=WORD 64-70有效USHORT UnltraDMA:1; // 1=WORD 88有效USHORT reserved:13;} wFieldValidity; // WORD 53: 后续字段有效性标志
USHORT wNumCurCyls; // WORD 54: CHS可寻址的柱面数USHORT wNumCurHeads; // WORD 55: CHS可寻址的磁头数USHORT wNumCurSectorsPerTrack; // WORD 56: CHS可寻址每磁道扇区数USHORT wCurSectorsLow; // WORD 57: CHS可寻址的扇区数低位字USHORT wCurSectorsHigh; // WORD 58: CHS可寻址的扇区数高位字struct {USHORT CurNumber:8; // 当前一次性可读写扇区数USHORT Multi:1; // 1=已选择多扇区读写USHORT reserved1:7;} wMultSectorStuff; // WORD 59: 多扇区读写设定
ULONG dwTotalSectors; // WORD 60-61: LBA可寻址的扇区数USHORT wSingleWordDMA; // WORD 62: 单字节DMA支持能力struct {USHORT Mode0:1; // 1=支持模式0 (4.17Mb/s)USHORT Mode1:1; // 1=支持模式1 (13.3Mb/s)USHORT Mode2:1; // 1=支持模式2 (16.7Mb/s)USHORT Reserved1:5;USHORT Mode0Sel:1; // 1=已选择模式0USHORT Mode1Sel:1; // 1=已选择模式1USHORT Mode2Sel:1; // 1=已选择模式2USHORT Reserved2:5;} wMultiWordDMA; // WORD 63: 多字节DMA支持能力
struct {USHORT AdvPOIModes:8; // 支持高级POI模式数USHORT reserved:8;} wPIOCapacity; // WORD 64: 高级PIO支持能力
USHORT wMinMultiWordDMACycle; // WORD 65: 多字节DMA传输周期的最小值USHORT wRecMultiWordDMACycle; // WORD 66: 多字节DMA传输周期的建议值USHORT wMinPIONoFlowCycle; // WORD 67: 无流控制时PIO传输周期的最小值USHORT wMinPOIFlowCycle; // WORD 68: 有流控制时PIO传输周期的最小值USHORT wReserved69[11]; // WORD 69-79: 保留struct {USHORT Reserved1:1;USHORT ATA1:1; // 1=支持ATA-1USHORT ATA2:1; // 1=支持ATA-2USHORT ATA3:1; // 1=支持ATA-3USHORT ATA4:1; // 1=支持ATA/ATAPI-4USHORT ATA5:1; // 1=支持ATA/ATAPI-5USHORT ATA6:1; // 1=支持ATA/ATAPI-6USHORT ATA7:1; // 1=支持ATA/ATAPI-7USHORT ATA8:1; // 1=支持ATA/ATAPI-8USHORT ATA9:1; // 1=支持ATA/ATAPI-9USHORT ATA10:1; // 1=支持ATA/ATAPI-10USHORT ATA11:1; // 1=支持ATA/ATAPI-11USHORT ATA12:1; // 1=支持ATA/ATAPI-12USHORT ATA13:1; // 1=支持ATA/ATAPI-13USHORT ATA14:1; // 1=支持ATA/ATAPI-14USHORT Reserved2:1;} wMajorVersion; // WORD 80: 主版本
USHORT wMinorVersion; // WORD 81: 副版本USHORT wReserved82[6]; // WORD 82-87: 保留struct {USHORT Mode0:1; // 1=支持模式0 (16.7Mb/s)USHORT Mode1:1; // 1=支持模式1 (25Mb/s)USHORT Mode2:1; // 1=支持模式2 (33Mb/s)USHORT Mode3:1; // 1=支持模式3 (44Mb/s)USHORT Mode4:1; // 1=支持模式4 (66Mb/s)USHORT Mode5:1; // 1=支持模式5 (100Mb/s)USHORT Mode6:1; // 1=支持模式6 (133Mb/s)USHORT Mode7:1; // 1=支持模式7 (166Mb/s) ???USHORT Mode0Sel:1; // 1=已选择模式0USHORT Mode1Sel:1; // 1=已选择模式1USHORT Mode2Sel:1; // 1=已选择模式2USHORT Mode3Sel:1; // 1=已选择模式3USHORT Mode4Sel:1; // 1=已选择模式4USHORT Mode5Sel:1; // 1=已选择模式5USHORT Mode6Sel:1; // 1=已选择模式6USHORT Mode7Sel:1; // 1=已选择模式7} wUltraDMA; // WORD 88: Ultra DMA支持能力
USHORT wReserved89[167]; // WORD 89-255} IDINFO, *PIDINFO;
由此可知,512
字节的bBuffer
中,27~46
字节即为我们所需的model number
。经过一定的字节序转换后大功告成。
实现代码如下:
const WORD IDE_ATAPI_IDENTIFY = 0xA1; // 读取ATAPI设备的命令const WORD IDE_ATA_IDENTIFY = 0xEC; // 读取ATA设备的命令DWORD GetDiskModelNumber(DWORD driver, CHAR *modelNumber){CHAR sFilePath[DISK_PATH_LEN];BOOL result; // results flagDWORD readed; // discard resultsHANDLE hDevice;WORD i;sprintf(sFilePath, "\\\\.\\PHYSICALDRIVE%d", driver);hDevice = CreateFile(sFilePath, // drive to openGENERIC_READ | GENERIC_WRITE, // access to the driveFILE_SHARE_READ | FILE_SHARE_WRITE, //share modeNULL, // default security attributesOPEN_EXISTING, // disposition0, // file attributesNULL // do not copy file attribute);if (hDevice == INVALID_HANDLE_VALUE){fprintf(stderr, "CreateFile() Error: %ld\n", GetLastError());return (DWORD)-1;}GETVERSIONINPARAMS gvopVersionParams;result = DeviceIoControl(hDevice,SMART_GET_VERSION,NULL,0,&gvopVersionParams,sizeof(gvopVersionParams),&readed,NULL);if (!result) //fail{fprintf(stderr, "SMART_GET_VERSION Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return (DWORD)-1;}if(0 == gvopVersionParams.bIDEDeviceMap){return (DWORD)-1;}// IDE or ATAPI IDENTIFY cmdBYTE btIDCmd;SENDCMDINPARAMS inParams;BYTE nDrive =0;btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;// output structureBYTE outParams[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1]; // + 512 - 1//fill in the input bufferinParams.cBufferSize = 0; //or IDENTIFY_BUFFER_SIZE ?inParams.irDriveRegs.bFeaturesReg = READ_ATTRIBUTES;inParams.irDriveRegs.bSectorCountReg = 1;inParams.irDriveRegs.bSectorNumberReg = 1;inParams.irDriveRegs.bCylLowReg = 0;inParams.irDriveRegs.bCylHighReg = 0;inParams.irDriveRegs.bDriveHeadReg = (nDrive & 1) ? 0xB0 : 0xA0;inParams.irDriveRegs.bCommandReg = btIDCmd;//inParams.bDriveNumber = nDrive;//get the attributesresult = DeviceIoControl(hDevice,SMART_RCV_DRIVE_DATA,&inParams,sizeof(SENDCMDINPARAMS) - 1,outParams,sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,&readed,NULL);if (!result) //fail{fprintf(stderr, "SMART_RCV_DRIVE_DATA Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return (DWORD)-1;}(void)CloseHandle(hDevice);DWORD dwDiskData[IDENTIFY_BUFFER_SIZE / 2];WORD *pIDSector; // 对应结构IDSECTOR,见头文件pIDSector = (WORD *)(((SENDCMDOUTPARAMS*)outParams)->bBuffer); //lint !e826for(i = 0; i < IDENTIFY_BUFFER_SIZE / 2; i++){dwDiskData[i] = pIDSector[i]; //lint !e662 !e661}// get model numbermemset(modelNumber, 0, DISK_INFO_BUF_LEN);strcpy(modelNumber, ConvertSENDCMDOUTPARAMSBufferToString(dwDiskData, 27, 46));return 0;}
代码分析:
1.
老套路,通过CreateFile()
打开设备。
2.
调用控制码为SMART_GET_VERSION
的DeviceIoControl()
函数获得输出结构GETVERSIONINPARAMS gvopVersionParams
。
3.
通过检测gvopVersionParams.bIDEDeviceMap
来确定设备类型,并记录在btIDCmd
中。
4.
填充SENDCMDINPARAMS inParams
参数,注意将第3
步得到的btIDCmd
赋值给inParams.irDriveRegs.bCommandReg
。
5.
调用控制码为SMART_RCV_DRIVE_DATA
的DeviceIoControl()
函数,其输入参数为第4
步准备好的inParams
,得到输出参数outParams
。
6.
解析outParams->bBuffer
的27~46
字节,得到所需的model number
。
解析outParams->bBuffer
时,因为ATA/ATAPI
中的WORD
与Windows
采用的字节顺序相反,所以
需要将字符
串中的字符两两颠倒,函数如下,
CHAR *ConvertSENDCMDOUTPARAMSBufferToString(const DWORD *dwDiskData, DWORD nFirstIndex, DWORD nLastIndex){static CHAR szResBuf[IDENTIFY_BUFFER_SIZE]; //512DWORD nIndex = 0;DWORD nPosition = 0;for (nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex++){// get high byteszResBuf[nPosition] = (CHAR)(dwDiskData[nIndex] >> 8);nPosition++;// get low byteszResBuf[nPosition] = (CHAR)(dwDiskData[nIndex] & 0xff);nPosition++;}// End the stringszResBuf[nPosition] = '\0';return szResBuf;}