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

Close a Window Application from another application.

 This is just a demo application code to show how the WM_CLOSE message can be sent to the target process which has a titled window to close the application. To achieve this, either we can use SendMessage or PostMessage APIs to send required Windows messages to the target application. Though both the APIs are dispatching WM_XXXXX message to target application two APIs has some differences, these are as below: 1. SendMessage () call is a blocking call but PostMessage is a non-blocking call(Asynchronous) 2. SendMessage() APIs return type is LRESULT (LONG_PTR) but PostMessage() APIs return type is BOOL(typedef int). In Short, SendMessage () APIs return type depends on what message has been sent to the Windowed target process. For the other one, it's always a non-zero value, which indicates the message has been successfully placed on the target process message queue. Now let's see how can I close a target windowed application "Solitaire & Casual Games" from my custom-

XOR (Exclusive OR) for branchless coding

The following example shows the array reversing using the  XOR operator . No need to take any additional variable to reverse the array.   int main(int argc, _TCHAR* argv[]) { char str[] = "I AM STUDENT"; int length = strlen(str); for(int i = 0; i < ((length/2)); i++) { str[i] ^= str[length - (1+i)]; str[length - (1+i)] ^= str[i]; str[i] ^= str[length - (1+i)]; } cout << str << endl; return 0; } The above example is one of the uses of XOR but XOR comes in handy when we can do branchless coding  methods like butterfly switch etc. Sometimes this is very effective in speeding up the execution.  Let's see one of the uses of XOR in branchless coding. I am taking a simple example of Y = | X |.  Yes, I am generating abs of a supplied number. So, my function signature/definition in C++ looks like below: int absoluteBranch( int x) {     if (x < 0 ) {         return -x;     }     else {         retur