The NT Insider

Try This -- Interactive Driver Testing
(By: The NT Insider, Vol 11, Issue 3&4, May-August 2004 | Published: 18-Aug-04| Modified: 18-Aug-04)

Drivers can be tough to test, especially early in their development cycle. Often, you need to get a ton of code written and running just so you can get to the point where you can start testing to see if some of the basic assumptions you?ve made about how your hardware works are valid.

Well, maybe not. One of the most unique ideas I?ve seen in driver testing was recently proposed by NTDEV member (and DDK MVP) Don Burn.

Let?s say you want to do some basic tests to see if you can actually get your hardware initialized. Why not stick your code into the world?s most minimal driver skeleton using a DriverEntry routine like that shown in Figure 1?

DriverEntry(PDRIVER_OBJECT DriverObj, PUNICODE_STRING RegistryPath)
    ULONG function;
    NTSTATUS status;
    BOOLEAN done = FALSE;

    DbgPrint("\n MyDriver -- Compiled %s %s\n",__DATE__, __TIME__);

    while(done == FALSE)  {

        DbgPrint("\nMyDriver TEST -- Functions:\n\n");
        DbgPrint("\t1. Input Port Address\n");
        DbgPrint("\t2. Input Shared Memory Address\n");
        DbgPrint("\t3. Call HwInit\n");
        DbgPrint("\t4. Test InitStatus\n");
        DbgPrint("\n\t0. Exit\n");
 DbgPrompt("\n\tSelection: %x", &function);

        switch(function)  {

            case 0:
                done = TRUE;

            case 1:
                DbgPrompt("\nPort to use: %x", &Port)
            case 2:   {
                ULONG mem;
                PHYSICAL_ADDRESS pa;

                DbgPrompt("Shared Memory low-part: %x", &mem);
                pa.LowPart = pa;
                DbgPrompt("Shared Memory high-part: %x", &mem);
                pa.HighPart = pa;

                MappedMem = MmMapIoSpace(pa, SHARED_SIZE, MmNonCached);

            case 3:
                DbgPrint("Calling HwInit");
                status = HwInit();
                DbgPrint("HwInit returned 0x%0x\n", status);

            case 4:
                DbgPrompt("How many MS to wait?? %d", &mem);

                DbgPrint("Calling TestInitStatus");   
                status = TestInitStatus(mem);
                DbgPrint("TestInitStatus returned 0x%0x\n", status);




    DbgPrint(DRIVER_NAME "DriverEntry: Leaving\n");

    if(MammpedMem)  {
        MmUnmapIoSpace(MappedMem, SHARED_SIZE);


Figure 1

The trick here is that you write a DriverEntry routine that uses DbgPrint and DbgPrompt in a loop to interact with you via the debugger. When you?re done playing (er, testing); your code breaks out of the loop, reverses any changes it made in the system, and returns an error. That?ll cause your driver to unload.

Given that you?ll be performing the majority of this testing on one or two test machines at the most, with a limited set of hardware, you will almost certainly know the port and/or shared memory address assigned to your device. If you don?t know these from your hardware specification, you can always use one of the many PCI bus examination utilities and read the information from your device?s BARs. After all, these registers were assigned to your device during system initialization. You can very happily use them on a limited basis and in a test environment only without doing the usual StartDevice song-and-dance.

Before you go all frothy at the mouth, understand that this whole idea is limited to early test scenarios. We?re talking about a technique that?ll help you get something going fast with the least possible annoyance and smallest-possible investment in test code. Once you get some solid code in your driver, you?ll want to change over to properly allocating the ports and shared memory address like you?re supposed to, and using a user-mode test app to send testing IOCTLs to you driver.

And, be warned, this idea might not work with SoftICE.

But for early stage testing, where you need to continuously vary timer values and other test information, this might be just the trick you need. Have fun!


This article was printed from OSR Online

Copyright 2017 OSR Open Systems Resources, Inc.