/*++
*******************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved          *
*                                                                             *
* This software contains proprietary and confidential information of Digi     *
* International Inc.  By accepting transfer of this copy, Recipient agrees    *
* to retain this software in confidence, to prevent disclosure to others,     *
* and to make no use of this software other than that for which it was        *
* delivered.  This is an unpublished copyrighted work of Digi International   *
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly   *
* prohibited.                                                                 *
*                                                                             *
* Restricted Rights Legend                                                    *
*                                                                             *
* Use, duplication, or disclosure by the Government is subject to             *
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in         *
* Technical Data and Computer Software clause at DFARS 252.227-7031 or        *
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -          *
* Restricted Rights at 48 CFR 52.227-19, as applicable.                       *
*                                                                             *
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343          *
*                                                                             *
*******************************************************************************

Module Name:

   dxbmemdump.cpp

Abstract:

   Dumps selected memory region from Dxb adapter to binary file.

Author:

   Chris Walls-Manning

--*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "DigiKme.h"
#include "dxbtypes.h"
#include "triglbl.h"
#include "hou2mmap.h"


int __cdecl main(int argc, char * argv[])
{
   HANDLE hAdapter = INVALID_HANDLE_VALUE;
	int i;

   {
      char szAdapterDeviceName[32];
      sprintf(szAdapterDeviceName, "\\\\.\\%s", argv[1]);

      hAdapter = CreateFile(szAdapterDeviceName,
         GENERIC_READ | GENERIC_WRITE,
         FILE_SHARE_READ | FILE_SHARE_WRITE,
         NULL,
         CREATE_ALWAYS,
         FILE_ATTRIBUTE_NORMAL,
         NULL);
      if (hAdapter == INVALID_HANDLE_VALUE)
      {
         printf("ERROR: Unable to open adapter %s!\n", argv[1]);
         goto Error;
      }
   }

	for (i = 0; i < 10; i++)
   {
      KME_MAP kmeMap = { 0 };
      BOOL fDevIocltSucceeded = FALSE;
      DWORD cbDevIoctlReturned = 0;

      kmeMap.Flags = KME_FLAG_ADAPTER_MEMORY;

      fDevIocltSucceeded = DeviceIoControl(hAdapter, (DWORD)IOCTL_KME_MAP,
                                           &kmeMap, sizeof kmeMap,
                                           &kmeMap, sizeof kmeMap,
                                           &cbDevIoctlReturned, NULL);

      if ((!fDevIocltSucceeded) || (cbDevIoctlReturned != sizeof kmeMap))
      {
         printf("ERROR: Unable to map memory!  fDevIocltSucceeded == %d cbDevIoctlReturned == %d GetLastError() == %d\n", fDevIocltSucceeded, cbDevIoctlReturned, GetLastError());
         goto Error;
      }

		printf("Mapped %d bytes of memory at 0x%08x\n", kmeMap.Length, kmeMap.pView);

#if 0
		{
			GLOBAL_DATA * pgd = (GLOBAL_DATA *)((ULONG)kmeMap.pView + GLOBAL_DATA_OFFSET);
			GLOBAL_COMMON * pgc = (GLOBAL_COMMON *)((ULONG)kmeMap.pView + pgd->global_data_offset);

			printf("global_data_offset == 0x%08x\n", pgd->global_data_offset);
			printf("global_host_paddr == 0x%08x\n", pgd->global_host_paddr);
			printf("global_endian == 0x%08x\n", pgd->global_endian);

			printf("magic == %c%c%c%c\n", ((pgc->magic >> 24) & 0xff), ((pgc->magic >> 16) & 0xff), ((pgc->magic >> 8) & 0xff), (pgc->magic & 0xff));
			printf("status == 0x%08x\n", pgc->status);
			printf("revision == 0x%08x\n", pgc->revision);
			printf("heartbeat == 0x%08x\n", pgc->heartbeat);
			printf("bigendian_board == 0x%08x\n", pgc->bigendian_board);
		}
#endif

#if 0
		cbDevIoctlReturned = 0;
      fDevIocltSucceeded = DeviceIoControl(hAdapter, (DWORD)IOCTL_KME_UNMAP,
                                           &kmeMap.pView, sizeof kmeMap.pView,
                                           NULL, 0,
                                           &cbDevIoctlReturned, NULL);
      if (!fDevIocltSucceeded)
      {
         printf("ERROR: Unable to unmap memory!  fDevIocltSucceeded == %d cbDevIoctlReturned == %d GetLastError() == %d\n", fDevIocltSucceeded, cbDevIoctlReturned, GetLastError());
         goto Error;
      }
#endif
   }


Error:
   printf("done\n");
   if (hAdapter != INVALID_HANDLE_VALUE) CloseHandle(hAdapter);

   return 0;
}
