Skip to main content

Physical Disk (HDD) Adapter Information.

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:


Comments

Popular posts from this blog

Reversing char array without splitting the array to tokens

 I was reading about strdup, a C++ function and suddenly an idea came to my mind if this can be leveraged to aid in reversing a character array without splitting the array into words and reconstructing it again by placing spaces and removing trailing spaces. Again, I wanted an array to be passed as a function argument and an array size to be passed implicitly with the array to the function. Assumed, a well-formed char array has been passed into the function. No malformed array checking is done inside the function. So, the function signature and definition are like below: Below is the call from the client code to reverse the array without splitting tokens and reconstructing it. Finally, copy the reversed array to the destination.  For GNU C++, we should use strdup instead _strdup . On run, we get the following output: Demo code

A simple approach to generate Fibonacci series via multi-threading

T his is a very simple approach taken to generate the Fibonacci series through multithreading. Here instead of a function, used a function object. The code is very simple and self-explanatory.  #include <iostream> #include <mutex> #include <thread> class Fib { public:     Fib() : _num0(1), _num1(1) {}     unsigned long operator()(); private:     unsigned long _num0, _num1;     std::mutex mu; }; unsigned long Fib::operator()() {     mu.lock(); // critical section, exclusive access to the below code by locking the mutex     unsigned long  temp = _num0;     _num0 = _num1;     _num1 = temp + _num0;     mu.unlock();     return temp; } int main() {     Fib f;          int i = 0;     unsigned long res = 0, res2= 0, res3 = 0;     std::cout << "Fibonacci series: ";     while (i <= 15) {         std::thread t1([&] { res = f(); }); // Capturing result to respective variable via lambda         std::thread t2([&] { res2 = f(); });         std::thread t3(

Variadic template class to add numbers recursively during compilation

 The idea of having a class to add numbers (variable parameters) during compilation time recursively. Also wanted to restrict types to a single type while sending parameters to class member function. That said, if we mix int, float and double types to add function shall result in compilation error. How do we achieve this. The below is the code which actually helps to achieve this: <code> #include < fmt/format.h > template < typename T> class MyVarSumClass{     private :         T _sum = 0 ;     public :         template < typename ... TRest>         T add(T num, TRest... nums){             static_assert (std::conjunction<std::is_same<TRest, T>...>{}); /* Assert fails                if types are different */             _sum += num;             return add(nums...); // Next parameter packs gets picked recursively         }         // Base case         T add(T num){             _sum += num;             return _sum;         } }; int main() {     My