📄 enumdisk.c
字号:
// Allocate memory to get the interface detail data
// This contains the devicepath we need to open the device
//
interfaceDetailDataSize = reqSize;
interfaceDetailData = malloc (interfaceDetailDataSize);
if ( interfaceDetailData == NULL ) {
printf("Unable to allocate memory to get the interface detail data.\n" );
return FALSE;
}
interfaceDetailData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);
status = SetupDiGetDeviceInterfaceDetail (
IntDevInfo, // Interface Device info handle
&interfaceData, // Interface data for the event class
interfaceDetailData, // Interface detail data
interfaceDetailDataSize, // Interface detail data size
&reqSize, // Buffer size required to get the detail data
NULL); // Interface device info
if ( status == FALSE ) {
printf("Error in SetupDiGetDeviceInterfaceDetail failed with error: %d\n", GetLastError() );
return FALSE;
}
//
// Now we have the device path. Open the device interface
// to send Pass Through command
printf("Interface: %s\n", interfaceDetailData->DevicePath);
hDevice = CreateFile(
interfaceDetailData->DevicePath, // device interface name
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
//
// We have the handle to talk to the device.
// So we can release the interfaceDetailData buffer
//
free (interfaceDetailData);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("CreateFile failed with error: %d\n", GetLastError() );
return TRUE;
}
query.PropertyId = StorageAdapterProperty;
query.QueryType = PropertyStandardQuery;
status = DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof( STORAGE_PROPERTY_QUERY ),
&outBuf,
512,
&returnedLength,
NULL
);
if ( !status ) {
printf("IOCTL failed with error code%d.\n\n", GetLastError() );
}
else {
adpDesc = (PSTORAGE_ADAPTER_DESCRIPTOR) outBuf;
printf("\nAdapter Properties\n");
printf("------------------\n");
printf("Bus Type : %s\n", BusType[adpDesc->BusType]);
printf("Max. Tr. Length: 0x%x\n", adpDesc->MaximumTransferLength );
printf("Max. Phy. Pages: 0x%x\n", adpDesc->MaximumPhysicalPages );
printf("Alignment Mask : 0x%x\n", adpDesc->AlignmentMask );
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
status = DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof( STORAGE_PROPERTY_QUERY ),
&outBuf,
512,
&returnedLength,
NULL
);
if ( !status ) {
printf("IOCTL failed with error code%d.\n\n", GetLastError() );
}
else {
printf("\nDevice Properties\n");
printf("-----------------\n");
devDesc = (PSTORAGE_DEVICE_DESCRIPTOR) outBuf;
//
// Our device table can handle only 16 devices.
//
printf("Device Type : %s (0x%X)\n",
DeviceType[devDesc->DeviceType > 0x0F? 0x0F: devDesc->DeviceType ], devDesc->DeviceType);
if ( devDesc->DeviceTypeModifier ) {
printf("Device Modifier : 0x%x\n", devDesc->DeviceTypeModifier);
}
printf("Removable Media : %s\n", devDesc->RemovableMedia ? "Yes" : "No" );
p = (PUCHAR) outBuf;
if ( devDesc->VendorIdOffset && p[devDesc->VendorIdOffset] ) {
printf("Vendor ID : " );
for ( i = devDesc->VendorIdOffset; p[i] != (UCHAR) NULL && i < returnedLength; i++ ) {
printf("%c", p[i] );
}
printf("\n");
}
if ( devDesc->ProductIdOffset && p[devDesc->ProductIdOffset] ) {
printf("Product ID : " );
for ( i = devDesc->ProductIdOffset; p[i] != (UCHAR) NULL && i < returnedLength; i++ ) {
printf("%c", p[i] );
}
printf("\n");
}
if ( devDesc->ProductRevisionOffset && p[devDesc->ProductRevisionOffset] ) {
printf("Product Revision: " );
for ( i = devDesc->ProductRevisionOffset; p[i] != (UCHAR) NULL && i < returnedLength; i++ ) {
printf("%c", p[i] );
}
printf("\n");
}
if ( devDesc->SerialNumberOffset && p[devDesc->SerialNumberOffset] ) {
printf("Serial Number : " );
for ( i = devDesc->SerialNumberOffset; p[i] != (UCHAR) NULL && i < returnedLength; i++ ) {
printf("%c", p[i] );
}
printf("\n");
}
}
}
ZeroMemory(&sptwb,sizeof(SCSI_PASS_THROUGH_WITH_BUFFERS));
sptwb.Spt.Length = sizeof(SCSI_PASS_THROUGH);
sptwb.Spt.PathId = 0;
sptwb.Spt.TargetId = 1;
sptwb.Spt.Lun = 0;
sptwb.Spt.CdbLength = CDB6GENERIC_LENGTH;
sptwb.Spt.SenseInfoLength = 24;
sptwb.Spt.DataIn = SCSI_IOCTL_DATA_IN;
sptwb.Spt.DataTransferLength = 192;
sptwb.Spt.TimeOutValue = 2;
sptwb.Spt.DataBufferOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS,DataBuf);
sptwb.Spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS,SenseBuf);
sptwb.Spt.Cdb[0] = SCSIOP_INQUIRY;
sptwb.Spt.Cdb[4] = 192;
length = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS,DataBuf) +
sptwb.Spt.DataTransferLength;
status = DeviceIoControl(hDevice,
IOCTL_SCSI_PASS_THROUGH,
&sptwb,
sizeof(SCSI_PASS_THROUGH),
&sptwb,
length,
&returned,
FALSE);
PrintStatusResults(status, returned, &sptwb);
//
// Close handle the driver
//
if ( !CloseHandle(hDevice) ) {
DebugPrint( 2, "Failed to close device.\n");
}
return TRUE;
}
VOID
PrintError(ULONG ErrorCode)
/*++
Routine Description:
Prints formated error message
Arguments:
ErrorCode - Error code to print
Return Value:
None
--*/
{
UCHAR errorBuffer[80];
ULONG count;
count = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
ErrorCode,
0,
errorBuffer,
sizeof(errorBuffer),
NULL
);
if (count != 0) {
printf("%s\n", errorBuffer);
} else {
printf("Format message failed. Error: %d\n", GetLastError());
}
}
VOID
PrintDataBuffer(PUCHAR DataBuffer, ULONG BufferLength)
/*++
Routine Description:
Prints the formated data buffer
Arguments:
DataBuffer - Buffer to be printed
BufferLength - Length of the buffer
Return Value:
None
--*/
{
ULONG cnt;
DebugPrint( 3, " 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
DebugPrint( 3, " ---------------------------------------------------------------\n");
for (cnt = 0; cnt < BufferLength; cnt++) {
if ((cnt) % 16 == 0) {
DebugPrint( 3, " %03X ",cnt);
}
DebugPrint( 3, "%02X ", DataBuffer[cnt]);
if ((cnt+1) % 8 == 0) {
DebugPrint( 3, " ");
}
if ((cnt+1) % 16 == 0) {
DebugPrint( 3, "\n");
}
}
DebugPrint( 3, "\n\n");
}
VOID
PrintStatusResults( BOOL Status, DWORD Returned, PSCSI_PASS_THROUGH_WITH_BUFFERS Psptwb )
/*++
Routine Description:
Prints the SCSI Inquiry data from the device
Arguments:
Status - Status of the DeviceIOControl
Returned - Number of bytes returned
Psptwb - SCSI pass through structure
Return Value:
None
--*/
{
ULONG errorCode;
USHORT i, devType;
printf("\nInquiry Data from Pass Through\n");
printf("------------------------------\n");
if (!Status ) {
printf("Error: %d ", errorCode = GetLastError() );
PrintError(errorCode);
return;
}
if (Psptwb->Spt.ScsiStatus) {
PrintSenseInfo(Psptwb);
return;
}
else {
devType = Psptwb->DataBuf[0] & 0x1f ;
//
// Our Device Table can handle only 16 devices.
//
printf("Device Type: %s (0x%X)\n", DeviceType[devType > 0x0F? 0x0F: devType], devType );
printf("Vendor ID : ");
for (i = 8; i <= 15; i++) {
printf("%c", Psptwb->DataBuf[i] );
}
printf("\nProduct ID : ");
for (i = 16; i <= 31; i++) {
printf("%c", Psptwb->DataBuf[i] );
}
printf("\nProduct Rev: ");
for (i = 32; i <= 35; i++) {
printf("%c", Psptwb->DataBuf[i] );
}
printf("\nVendor Str : ");
for (i = 36; i <= 55; i++) {
printf("%c", Psptwb->DataBuf[i] );
}
printf("\n\n");
DebugPrint( 3, "Scsi status: %02Xh, Bytes returned: %Xh, ",
Psptwb->Spt.ScsiStatus, Returned);
DebugPrint( 3, "Data buffer length: %Xh\n\n\n",
Psptwb->Spt.DataTransferLength);
PrintDataBuffer( Psptwb->DataBuf, 192);
printf("\n\n");
}
}
VOID
PrintSenseInfo(PSCSI_PASS_THROUGH_WITH_BUFFERS Psptwb)
/*++
Routine Description:
Prints the SCSI status and Sense Info from the device
Arguments:
Psptwb - Pass Through buffer that contains the Sense info
Return Value:
None
--*/
{
int i;
printf("Scsi status: %02Xh\n\n", Psptwb->Spt.ScsiStatus);
if (Psptwb->Spt.SenseInfoLength == 0) {
return;
}
DebugPrint( 3, "Sense Info -- consult SCSI spec for details\n");
DebugPrint( 3, "-------------------------------------------------------------\n");
for (i=0; i < Psptwb->Spt.SenseInfoLength; i++) {
DebugPrint( 3, "%02X ", Psptwb->SenseBuf[i]);
}
printf("\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -