home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 August / CICA.cdr / unzipped / nt / mapmem / win32 / memtest.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-24  |  3.9 KB  |  136 lines

  1. //
  2. // MEMTEST.c
  3. //
  4. // Test proggie for the MAPMEM device driver. Asks the MAPMEM driver
  5. //   to map the specified physical memory into this process's
  6. //   address space so we can party on it.
  7. //
  8. // To test this (on x86 ISA platforms) try: "memtest 1 0 655360 10000"
  9. //   and watch it draw on video memory.
  10. //
  11.  
  12. #include "windows.h"
  13. #include "stdio.h"
  14. #include "stdlib.h"
  15.  
  16.  
  17.  
  18. //
  19. // A couple of typedefs mapmem.h depends on from  MINIPORT.H & NTDDK.H.
  20. //
  21.  
  22. typedef enum _INTERFACE_TYPE {
  23.     Internal,
  24.     Isa,
  25.     Eisa,
  26.     MicroChannel,
  27.     TurboChannel,
  28.     MaximumInterfaceType
  29. }INTERFACE_TYPE, *PINTERFACE_TYPE;
  30.  
  31. typedef LARGE_INTEGER PHYSICAL_ADDRESS;
  32.  
  33. #include "..\mapmem.h"
  34.  
  35.  
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39.     HANDLE               hDriver;
  40.     PHYSICAL_MEMORY_INFO pmi;
  41.     PVOID                pPartyMem;
  42.     DWORD                cbReturned;
  43.     ULONG                length;
  44.     char                 *aInterfaceType[] =  {"Internal",
  45.                                                "Isa",
  46.                                                "Eisa",
  47.                                                "MicroChannel",
  48.                                                "TurboChannel" };
  49.  
  50.     if (argc < 4)
  51.     {
  52.         printf ("\nUsage: memtest <interfaceType> <bus#> <physicalAddr> <length>\n\n");
  53.         printf ("\t<interfaceType>: 1 = Isa, 2 = Eisa, 3 = Microchannel, 4 = TurboChannel\n");
  54.         printf ("\t<bus#>         : bus number, i.e. 0 for standard x86 ISA systems\n");
  55.         printf ("\t<physicalAddr> : physical address to map (decimal)\n");
  56.         printf ("\t<length>       : length of section to map (decimal)\n\n");
  57.  
  58.         printf ("\tTry 'memtest 1 0 655360 8192' to retrieve a pointer to\n");
  59.         printf ("\tphysical address 0xA0000 (x86 ISA VGA video memory), and\n");
  60.         printf ("\tthen write out to the memory.\n");
  61.         return 0;
  62.     }
  63.  
  64.     if ((hDriver = CreateFile("\\\\.\\MAPMEM",
  65.                               GENERIC_READ | GENERIC_WRITE,
  66.                               0,
  67.                               NULL,
  68.                               OPEN_EXISTING,
  69.                               FILE_ATTRIBUTE_NORMAL,
  70.                               NULL
  71.                               )) != ((HANDLE)-1))
  72.  
  73.         printf("\nRetrieved valid handle for MAPMEM driver\n");
  74.  
  75.  
  76.     else
  77.     {
  78.         printf("Can't get a handle to MAPMEM driver\n");
  79.         return 0;
  80.     }
  81.  
  82.     pmi.interfaceType            = (INTERFACE_TYPE) atoi (argv[1]);
  83.     pmi.busNumber                = (ULONG)          atoi (argv[2]);
  84.     pmi.physicalAddress.LowPart  = (ULONG)          atoi (argv[3]);
  85.     pmi.physicalAddress.HighPart = (LONG)           0x00000000;
  86.     length = pmi.length          = (ULONG)          atoi (argv[4]);
  87.  
  88.     printf ("\tinterfaceType = %s\n", aInterfaceType[pmi.interfaceType]);
  89.     printf ("\tbus number    = %d\n", pmi.busNumber);
  90.     printf ("\tphysicalAddr  = %d (0x%x)\n",
  91.             pmi.physicalAddress.LowPart, pmi.physicalAddress.LowPart);
  92.     printf ("\tlength        = %d (0x%x)\n", pmi.length, pmi.length);
  93.  
  94.     if (DeviceIoControl (hDriver,
  95.                          IOCTL_MAPMEM_MAP_USER_PHYSICAL_MEMORY,
  96.                          &pmi,
  97.                          sizeof(PHYSICAL_MEMORY_INFO),
  98.                          &pPartyMem,
  99.                          sizeof(PVOID),
  100.                          &cbReturned,
  101.                          0
  102.                          ) )
  103.     {
  104.         ULONG j;
  105.  
  106.         //
  107.         // party on memory...
  108.         //
  109.  
  110.         if (pPartyMem)
  111.         {
  112.             UCHAR uc;
  113.  
  114.             for (j = 0; j < length; j++)
  115.             {
  116.                 uc = *(((PUCHAR) pPartyMem) + j);
  117.  
  118.                 *(((PUCHAR) pPartyMem) + j) = 0x47;
  119.             }
  120.         }
  121.  
  122.         else
  123.  
  124.             printf ("pPartyMem = NULL\n");
  125.     }
  126.  
  127.     else
  128.  
  129.         printf ("DeviceIoControl failed\n");
  130.  
  131.  
  132.     CloseHandle(hDriver);
  133.  
  134.     return 1;
  135. }
  136.