4164
|
1 /*
|
|
2 MAPDEV.h - include file for VxD MAPDEV
|
|
3 Copyright (c) 1996 Vireo Software, Inc.
|
|
4 Modified for libdha by Nick Kurshev.
|
|
5 */
|
|
6
|
|
7 #include <windows.h>
|
12056
|
8 #include <ddk/ntddk.h>
|
|
9 #include "../dhahelperwin/dhahelper.h"
|
|
10
|
4164
|
11 /*
|
|
12 This is the request structure that applications use
|
|
13 to request services from the MAPDEV VxD.
|
|
14 */
|
|
15
|
|
16 typedef struct _MapDevRequest
|
|
17 {
|
|
18 DWORD mdr_ServiceID; /* supplied by caller */
|
|
19 LPVOID mdr_PhysicalAddress; /* supplied by caller */
|
|
20 DWORD mdr_SizeInBytes; /* supplied by caller */
|
|
21 LPVOID mdr_LinearAddress; /* returned by VxD */
|
|
22 WORD mdr_Selector; /* returned if 16-bit caller */
|
|
23 WORD mdr_Status; /* MDR_xxxx code below */
|
|
24 } MAPDEVREQUEST, *PMAPDEVREQUEST;
|
|
25
|
|
26 #define MDR_SERVICE_MAP CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_NEITHER, FILE_ANY_ACCESS)
|
|
27 #define MDR_SERVICE_UNMAP CTL_CODE(FILE_DEVICE_UNKNOWN, 2, METHOD_NEITHER, FILE_ANY_ACCESS)
|
|
28
|
|
29 #define MDR_STATUS_SUCCESS 1
|
|
30 #define MDR_STATUS_ERROR 0
|
|
31 /*#include "winioctl.h"*/
|
|
32 #define FILE_DEVICE_UNKNOWN 0x00000022
|
|
33 #define METHOD_NEITHER 3
|
|
34 #define FILE_ANY_ACCESS 0
|
|
35 #define CTL_CODE( DeviceType, Function, Method, Access ) ( \
|
|
36 ((DeviceType)<<16) | ((Access)<<14) | ((Function)<<2) | (Method) )
|
|
37
|
12056
|
38
|
|
39 int IsWinNT(){
|
|
40 OSVERSIONINFO OSVersionInfo;
|
|
41 OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
42 GetVersionEx(&OSVersionInfo);
|
|
43 return OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT;
|
|
44 }
|
|
45
|
|
46 static HANDLE hDriver = INVALID_HANDLE_VALUE;
|
|
47
|
|
48
|
4164
|
49 /* Memory Map a piece of Real Memory */
|
10980
|
50 void *map_phys_mem(unsigned long base, unsigned long size) {
|
12056
|
51 if(!IsWinNT()){
|
4164
|
52 HANDLE hDevice ;
|
|
53 PVOID inBuf[1] ; /* buffer for struct pointer to VxD */
|
|
54 DWORD RetInfo[2] ; /* buffer to receive data from VxD */
|
|
55 DWORD cbBytesReturned ; /* count of bytes returned from VxD */
|
|
56 MAPDEVREQUEST req ; /* map device request structure */
|
|
57 DWORD *pNicstar, Status, Time ; int i ; char *endptr ;
|
|
58 const PCHAR VxDName = "\\\\.\\MAPDEV.VXD" ;
|
|
59 const PCHAR VxDNameAlreadyLoaded = "\\\\.\\MAPDEV" ;
|
|
60
|
|
61 hDevice = CreateFile(VxDName, 0,0,0,
|
|
62 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ;
|
|
63 if (hDevice == INVALID_HANDLE_VALUE)
|
|
64 hDevice = CreateFile(VxDNameAlreadyLoaded, 0,0,0,
|
|
65 CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, 0) ;
|
|
66 if (hDevice == INVALID_HANDLE_VALUE) {
|
|
67 fprintf(stderr, "Cannot open driver, error=%08lx\n", GetLastError()) ;
|
|
68 exit(1) ; }
|
|
69
|
|
70 req.mdr_ServiceID = MDR_SERVICE_MAP ;
|
|
71 req.mdr_PhysicalAddress = (PVOID)base ;
|
|
72 req.mdr_SizeInBytes = size ;
|
|
73 inBuf[0] = &req ;
|
|
74
|
|
75 if ( ! DeviceIoControl(hDevice, MDR_SERVICE_MAP, inBuf, sizeof(PVOID),
|
|
76 NULL, 0, &cbBytesReturned, NULL) ) {
|
|
77 fprintf(stderr, "Failed to map device\n") ; exit(1) ; }
|
|
78
|
|
79 return (void*)req.mdr_LinearAddress ;
|
12056
|
80 }
|
|
81 else{
|
|
82 dhahelper_t dhahelper_priv;
|
|
83 DWORD dwBytesReturned;
|
|
84 dhahelper_priv.size = size;
|
|
85 dhahelper_priv.base = base;
|
|
86 if(hDriver==INVALID_HANDLE_VALUE)hDriver = CreateFile("\\\\.\\DHAHELPER",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
|
|
87 if (!DeviceIoControl(hDriver, IOCTL_DHAHELPER_MAPPHYSTOLIN, &dhahelper_priv,sizeof(dhahelper_t), &dhahelper_priv, sizeof(dhahelper_t),&dwBytesReturned, NULL)){
|
|
88 printf("unable to map thre requested memory region\n");
|
|
89 return NULL;
|
|
90 }
|
|
91 else return dhahelper_priv.ptr;
|
|
92 }
|
4164
|
93 }
|
|
94
|
12056
|
95 void unmap_phys_mem(void *ptr, unsigned long size) {
|
|
96 if(IsWinNT()){
|
|
97 dhahelper_t dhahelper_priv;
|
|
98 DWORD dwBytesReturned;
|
|
99 dhahelper_priv.ptr = ptr;
|
|
100 DeviceIoControl(hDriver, IOCTL_DHAHELPER_UNMAPPHYSADDR, &dhahelper_priv,sizeof(dhahelper_t), NULL, 0, &dwBytesReturned, NULL);
|
|
101 }
|
|
102 }
|