In my previous post, I tried to build a command line app to show Physical Hard drives and volume(s) mapping for each HDD present/attached to a system. Now I've extended the program to read and display the properties of the storage adapter for each physical hard drive. To achieve this I've used the very popular Win32 API, CreateFile(), and IOCTL_STORAGE_QUERY_PROPERTY control code.
The previous program has been extended and now I'm showing only the part I've added on top of my previous program released under the title "PhysicalDisk and Volume Mapping information".
Here goes the rest of the code to get the storage adapter's information:
void PrintBusTypeName(BYTE iBusType)
{
switch(iBusType)
{
case BusTypeUnknown:
wprintf(L"BusType: Unknown\n");
break;
case BusTypeScsi:
wprintf(L"BusType: SCSI\n");
break;
case BusTypeAtapi:
wprintf(L"BusType: ATAPI\n");
break;
case BusTypeAta:
wprintf(L"BusType: ATA\n");
break;
case BusType1394:
wprintf(L"BusType: 1394\n");
break;
case BusTypeSsa:
wprintf(L"BusType: SSA\n");
break;
case BusTypeFibre:
wprintf(L"BusType: Fibre\n");
break;
case BusTypeUsb:
wprintf(L"BusType: USB\n");
break;
case BusTypeRAID:
wprintf(L"BusType: RAID\n");
break;
case BusTypeiScsi:
wprintf(L"BusType: iSCSI\n");
break;
case BusTypeSas:
wprintf(L"BusType: SAS\n");
break;
case BusTypeSata:
wprintf(L"BusType: SATA\n");
break;
case BusTypeSd:
wprintf(L"BusType: SD\n");
break;
case BusTypeMmc:
wprintf(L"BusType: MMC\n");
break;
case BusTypeVirtual:
wprintf(L"BusType: Virtual\n");
break;
case BusTypeFileBackedVirtual:
wprintf(L"BusType: FileBackedVirtual\n");
break;
case BusTypeMax:
wprintf(L"BusType: Max\n");
break;
case BusTypeMaxReserved:
wprintf(L"BusType: Max Reserved\n");
break;
default:
break;
}
}
void printAdapterPropertiesHDD(DWORD iDrvNumber)
{
TCHAR hddNum[5] = {0};
swprintf_s(hddNum, 5, _T("%ld"), iDrvNumber);
TCHAR szPhysicalDrv[STR_SIZE];
memset(szPhysicalDrv, 0, STR_SIZE);
HANDLE hDevice = INVALID_HANDLE_VALUE;
_tcscpy_s(szPhysicalDrv, STR_SIZE, _T("\\\\.\\PhysicalDrive"));
_tcscat_s(szPhysicalDrv, STR_SIZE, hddNum);
hDevice = CreateFile(
szPhysicalDrv, // device name
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if(INVALID_HANDLE_VALUE == hDevice)
{
wprintf(L"CreateFile failed with error: %d\n", GetLastError());
return;
}
STORAGE_PROPERTY_QUERY query;
PSTORAGE_ADAPTER_DESCRIPTOR adpDesc;
UCHAR outBuf[512];
DWORD returnedLength;
BOOL status;
query.PropertyId = StorageAdapterProperty;
query.QueryType = PropertyStandardQuery;
status = DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof( STORAGE_PROPERTY_QUERY ),
&outBuf,
512,
&returnedLength,
NULL
);
if ( !status )
{
wprintf(L"IOCTL failed with error code%d.\n\n", GetLastError() );
}
else
{
adpDesc = (PSTORAGE_ADAPTER_DESCRIPTOR) outBuf;
wprintf( L"\nAdapter Properties\n");
wprintf( L"------------------\n");
PrintBusTypeName(adpDesc->BusType);
wprintf( L"Max. Tr. Length: 0x%x\n", adpDesc->MaximumTransferLength );
wprintf( L"Max. Phy. Pages: 0x%x\n", adpDesc->MaximumPhysicalPages );
// Specifies the storage adapter's alignment requirements for transfers.
// The alignment mask indicates alignment restrictions for buffers required by the storage adapter for transfer operations.
switch(adpDesc->AlignmentMask)
{
case 0:
wprintf(L"Storage adapter's alignment requirements for transfers: BYTE boundaries.\n");
break;
case 1:
wprintf(L"Storage adapter's alignment requirements for transfers: WORD boundaries.\n");
break;
case 3:
wprintf(L"Storage adapter's alignment requirements for transfers: DWORD32 boundaries.\n");
break;
case 7:
wprintf(L"Storage adapter's alignment requirements for transfers: DWORD64 boundaries.\n");
break;
default:
break;
}
// AdapterUsesPio
if(adpDesc->AdapterUsesPio)
{
wprintf(L"The storage adapter uses programmed I/O (PIO).\n");
}
else
{
wprintf(L"The storage adapter doesn't use programmed I/O (PIO).\n");
}
// AdapterScansDown
if(adpDesc->AdapterScansDown)
{
wprintf(L"The storage adapter begins scanning with the highest device number, ");
wprintf(L"that is, the storage adapter scans down for BIOS devices.\n");
}
else
{
wprintf(L"The storage adapter begins scanning with the lowest device number.\n");
}
// AcceleratedTransfer
if(adpDesc->AcceleratedTransfer)
{
wprintf(L"The storage adapter supports synchronous transfers ");
wprintf(L"as a way of speeding up I/O.\n");
}
else
{
wprintf(L"The storage adapter does not support synchronous ");
wprintf(L"transfers as a way of speeding up I/O.\n");
}
// CommandQueueing
if(adpDesc->CommandQueueing)
{
wprintf(L"The storage adapter supports SCSI tagged queuing and");
wprintf(L"/or per-logical-unit internal queues, or the non-SCSI equivalent.\n");
}
else
{
wprintf(L"The storage adapter neither supports SCSI-tagged queuing ");
wprintf(L"nor per-logical-unit internal queues.\n");
}
// BusMajorVersion
wprintf(L"The storage adapter major Version: %d\n", adpDesc->BusMajorVersion);
// BusMinorVersion
wprintf(L"The storage adapter minor Version: %d\n", adpDesc->BusMinorVersion);
}
if( !CloseHandle(hDevice) )
{
wprintf(L"Failed to close drive %s.\n\n", szPhysicalDrv);
}
wprintf(L"\n");
}
In the _tmain....., called printAdapterPropertiesHDD from the following location.
:::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::
for( std::map < DWORD, DWORD >::iterator ii = mapPhysicaDriveCnt.begin(); ii != mapPhysicaDriveCnt.end(); ++ii)
{
wprintf(L"No. of volume(s) on physical drive %ld is/are: %ld\n", (*ii).first, mapDriveVolume.count((*ii).first));
std::pair < std::multimap < DWORD, TCHAR * >::iterator, std::multimap < DWORD, TCHAR * >::iterator > ret;
ret = mapDriveVolume.equal_range((*ii).first);
for (std::multimap< DWORD, TCHAR * >::iterator it=ret.first; it!=ret.second; ++it)
{
wprintf(L"Volume on physical drive: %ld is: %s\n", it->first, it->second);
}
wprintf(L"\n");
printAdapterPropertiesHDD((*ii).first);
}
The output looks like the below:
The previous program has been extended and now I'm showing only the part I've added on top of my previous program released under the title "PhysicalDisk and Volume Mapping information".
Here goes the rest of the code to get the storage adapter's information:
void PrintBusTypeName(BYTE iBusType)
{
switch(iBusType)
{
case BusTypeUnknown:
wprintf(L"BusType: Unknown\n");
break;
case BusTypeScsi:
wprintf(L"BusType: SCSI\n");
break;
case BusTypeAtapi:
wprintf(L"BusType: ATAPI\n");
break;
case BusTypeAta:
wprintf(L"BusType: ATA\n");
break;
case BusType1394:
wprintf(L"BusType: 1394\n");
break;
case BusTypeSsa:
wprintf(L"BusType: SSA\n");
break;
case BusTypeFibre:
wprintf(L"BusType: Fibre\n");
break;
case BusTypeUsb:
wprintf(L"BusType: USB\n");
break;
case BusTypeRAID:
wprintf(L"BusType: RAID\n");
break;
case BusTypeiScsi:
wprintf(L"BusType: iSCSI\n");
break;
case BusTypeSas:
wprintf(L"BusType: SAS\n");
break;
case BusTypeSata:
wprintf(L"BusType: SATA\n");
break;
case BusTypeSd:
wprintf(L"BusType: SD\n");
break;
case BusTypeMmc:
wprintf(L"BusType: MMC\n");
break;
case BusTypeVirtual:
wprintf(L"BusType: Virtual\n");
break;
case BusTypeFileBackedVirtual:
wprintf(L"BusType: FileBackedVirtual\n");
break;
case BusTypeMax:
wprintf(L"BusType: Max\n");
break;
case BusTypeMaxReserved:
wprintf(L"BusType: Max Reserved\n");
break;
default:
break;
}
}
void printAdapterPropertiesHDD(DWORD iDrvNumber)
{
TCHAR hddNum[5] = {0};
swprintf_s(hddNum, 5, _T("%ld"), iDrvNumber);
TCHAR szPhysicalDrv[STR_SIZE];
memset(szPhysicalDrv, 0, STR_SIZE);
HANDLE hDevice = INVALID_HANDLE_VALUE;
_tcscpy_s(szPhysicalDrv, STR_SIZE, _T("\\\\.\\PhysicalDrive"));
_tcscat_s(szPhysicalDrv, STR_SIZE, hddNum);
hDevice = CreateFile(
szPhysicalDrv, // device name
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if(INVALID_HANDLE_VALUE == hDevice)
{
wprintf(L"CreateFile failed with error: %d\n", GetLastError());
return;
}
STORAGE_PROPERTY_QUERY query;
PSTORAGE_ADAPTER_DESCRIPTOR adpDesc;
UCHAR outBuf[512];
DWORD returnedLength;
BOOL status;
query.PropertyId = StorageAdapterProperty;
query.QueryType = PropertyStandardQuery;
status = DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof( STORAGE_PROPERTY_QUERY ),
&outBuf,
512,
&returnedLength,
NULL
);
if ( !status )
{
wprintf(L"IOCTL failed with error code%d.\n\n", GetLastError() );
}
else
{
adpDesc = (PSTORAGE_ADAPTER_DESCRIPTOR) outBuf;
wprintf( L"\nAdapter Properties\n");
wprintf( L"------------------\n");
PrintBusTypeName(adpDesc->BusType);
wprintf( L"Max. Tr. Length: 0x%x\n", adpDesc->MaximumTransferLength );
wprintf( L"Max. Phy. Pages: 0x%x\n", adpDesc->MaximumPhysicalPages );
// Specifies the storage adapter's alignment requirements for transfers.
// The alignment mask indicates alignment restrictions for buffers required by the storage adapter for transfer operations.
switch(adpDesc->AlignmentMask)
{
case 0:
wprintf(L"Storage adapter's alignment requirements for transfers: BYTE boundaries.\n");
break;
case 1:
wprintf(L"Storage adapter's alignment requirements for transfers: WORD boundaries.\n");
break;
case 3:
wprintf(L"Storage adapter's alignment requirements for transfers: DWORD32 boundaries.\n");
break;
case 7:
wprintf(L"Storage adapter's alignment requirements for transfers: DWORD64 boundaries.\n");
break;
default:
break;
}
// AdapterUsesPio
if(adpDesc->AdapterUsesPio)
{
wprintf(L"The storage adapter uses programmed I/O (PIO).\n");
}
else
{
wprintf(L"The storage adapter doesn't use programmed I/O (PIO).\n");
}
// AdapterScansDown
if(adpDesc->AdapterScansDown)
{
wprintf(L"The storage adapter begins scanning with the highest device number, ");
wprintf(L"that is, the storage adapter scans down for BIOS devices.\n");
}
else
{
wprintf(L"The storage adapter begins scanning with the lowest device number.\n");
}
// AcceleratedTransfer
if(adpDesc->AcceleratedTransfer)
{
wprintf(L"The storage adapter supports synchronous transfers ");
wprintf(L"as a way of speeding up I/O.\n");
}
else
{
wprintf(L"The storage adapter does not support synchronous ");
wprintf(L"transfers as a way of speeding up I/O.\n");
}
// CommandQueueing
if(adpDesc->CommandQueueing)
{
wprintf(L"The storage adapter supports SCSI tagged queuing and");
wprintf(L"/or per-logical-unit internal queues, or the non-SCSI equivalent.\n");
}
else
{
wprintf(L"The storage adapter neither supports SCSI-tagged queuing ");
wprintf(L"nor per-logical-unit internal queues.\n");
}
// BusMajorVersion
wprintf(L"The storage adapter major Version: %d\n", adpDesc->BusMajorVersion);
// BusMinorVersion
wprintf(L"The storage adapter minor Version: %d\n", adpDesc->BusMinorVersion);
}
if( !CloseHandle(hDevice) )
{
wprintf(L"Failed to close drive %s.\n\n", szPhysicalDrv);
}
wprintf(L"\n");
}
In the _tmain....., called printAdapterPropertiesHDD from the following location.
:::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::
for( std::map < DWORD, DWORD >::iterator ii = mapPhysicaDriveCnt.begin(); ii != mapPhysicaDriveCnt.end(); ++ii)
{
wprintf(L"No. of volume(s) on physical drive %ld is/are: %ld\n", (*ii).first, mapDriveVolume.count((*ii).first));
std::pair < std::multimap < DWORD, TCHAR * >::iterator, std::multimap < DWORD, TCHAR * >::iterator > ret;
ret = mapDriveVolume.equal_range((*ii).first);
for (std::multimap< DWORD, TCHAR * >::iterator it=ret.first; it!=ret.second; ++it)
{
wprintf(L"Volume on physical drive: %ld is: %s\n", it->first, it->second);
}
wprintf(L"\n");
printAdapterPropertiesHDD((*ii).first);
}
The output looks like the below:
Comments