Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
13 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: WinIo.c
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // http://www.internals.com //
  13. //----------------------------------------------------------------------------
  14. // ---------------------------------------------------- //
  15. // WinIo v2.0 //
  16. // Direct Hardware Access Under Windows 9x/NT/2000/XP //
  17. // Copyright 1998-2002 Yariv Kaplan //
  18. // http://www.internals.com //
  19. // ---------------------------------------------------- //
  20. #include <ntddk.h>
  21. #include "winio_nt.h"
  22. #define OutputDebugString
  23. #define IOPM_SIZE 0x2000
  24. typedef char IOPM[IOPM_SIZE];
  25. IOPM *pIOPM = NULL;
  26. // Function definition section
  27. // -----------------------------------------------------------------
  28. NTSTATUS WinIoDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
  29. void WinIoUnload(IN PDRIVER_OBJECT DriverObject);
  30. NTSTATUS UnmapPhysicalMemory(HANDLE PhysicalMemoryHandle, PVOID pPhysMemLin);
  31. NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,
  32. ULONG PhysMemSizeInBytes,
  33. PVOID *ppPhysMemLin,
  34. HANDLE *pPhysicalMemoryHandle);
  35. void Ke386SetIoAccessMap(int, IOPM *);
  36. void Ke386QueryIoAccessMap(int, IOPM *);
  37. void Ke386IoSetAccessProcess(PEPROCESS, int);
  38. // -----------------------------------------------------------------
  39. // Installable driver initialization entry point.
  40. // This entry point is called directly by the I/O system.
  41. NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,
  42. IN PUNICODE_STRING RegistryPath)
  43. {
  44. UNICODE_STRING DeviceNameUnicodeString;
  45. UNICODE_STRING DeviceLinkUnicodeString;
  46. NTSTATUS ntStatus;
  47. PDEVICE_OBJECT DeviceObject = NULL;
  48. OutputDebugString ("Entering DriverEntry");
  49. RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\WinIo");
  50. // Create an EXCLUSIVE device object (only 1 thread at a time
  51. // can make requests to this device).
  52. ntStatus = IoCreateDevice (DriverObject,
  53. 0,
  54. &DeviceNameUnicodeString,
  55. FILE_DEVICE_WINIO,
  56. 0,
  57. TRUE,
  58. &DeviceObject);
  59. if (NT_SUCCESS(ntStatus))
  60. {
  61. // Create dispatch points for device control, create, close.
  62. DriverObject->MajorFunction[IRP_MJ_CREATE] =
  63. DriverObject->MajorFunction[IRP_MJ_CLOSE] =
  64. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = WinIoDispatch;
  65. DriverObject->DriverUnload = WinIoUnload;
  66. // Create a symbolic link, e.g. a name that a Win32 app can specify
  67. // to open the device.
  68. RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\WinIo");
  69. ntStatus = IoCreateSymbolicLink (&DeviceLinkUnicodeString,
  70. &DeviceNameUnicodeString);
  71. if (!NT_SUCCESS(ntStatus))
  72. {
  73. // Symbolic link creation failed- note this & then delete the
  74. // device object (it's useless if a Win32 app can't get at it).
  75. OutputDebugString ("ERROR: IoCreateSymbolicLink failed");
  76. IoDeleteDevice (DeviceObject);
  77. }
  78. }
  79. else
  80. {
  81. OutputDebugString ("ERROR: IoCreateDevice failed");
  82. }
  83. OutputDebugString ("Leaving DriverEntry");
  84. return ntStatus;
  85. }
  86. // Process the IRPs sent to this device
  87. NTSTATUS WinIoDispatch(IN PDEVICE_OBJECT DeviceObject,
  88. IN PIRP Irp)
  89. {
  90. PIO_STACK_LOCATION IrpStack;
  91. ULONG dwInputBufferLength;
  92. ULONG dwOutputBufferLength;
  93. ULONG dwIoControlCode;
  94. PVOID pvIOBuffer;
  95. NTSTATUS ntStatus;
  96. struct tagPhys32Struct Phys32Struct;
  97. OutputDebugString ("Entering WinIoDispatch");
  98. // Init to default settings
  99. Irp->IoStatus.Status = STATUS_SUCCESS;
  100. Irp->IoStatus.Information = 0;
  101. IrpStack = IoGetCurrentIrpStackLocation(Irp);
  102. // Get the pointer to the input/output buffer and it's length
  103. pvIOBuffer = Irp->AssociatedIrp.SystemBuffer;
  104. dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
  105. dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
  106. switch (IrpStack->MajorFunction)
  107. {
  108. case IRP_MJ_CREATE:
  109. OutputDebugString("IRP_MJ_CREATE");
  110. break;
  111. case IRP_MJ_CLOSE:
  112. OutputDebugString("IRP_MJ_CLOSE");
  113. break;
  114. case IRP_MJ_DEVICE_CONTROL:
  115. OutputDebugString("IRP_MJ_DEVICE_CONTROL");
  116. dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
  117. switch (dwIoControlCode)
  118. {
  119. case IOCTL_WINIO_ENABLEDIRECTIO:
  120. OutputDebugString("IOCTL_WINIO_ENABLEDIRECTIO");
  121. pIOPM = MmAllocateNonCachedMemory(sizeof(IOPM));
  122. if (pIOPM)
  123. {
  124. RtlZeroMemory(pIOPM, sizeof(IOPM));
  125. Ke386IoSetAccessProcess(PsGetCurrentProcess(), 1);
  126. Ke386SetIoAccessMap(1, pIOPM);
  127. }
  128. else
  129. Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
  130. break;
  131. case IOCTL_WINIO_DISABLEDIRECTIO:
  132. OutputDebugString("IOCTL_WINIO_DISABLEDIRECTIO");
  133. if (pIOPM)
  134. {
  135. Ke386IoSetAccessProcess(PsGetCurrentProcess(), 0);
  136. Ke386SetIoAccessMap(1, pIOPM);
  137. MmFreeNonCachedMemory(pIOPM, sizeof(IOPM));
  138. pIOPM = NULL;
  139. }
  140. break;
  141. case IOCTL_WINIO_MAPPHYSTOLIN:
  142. OutputDebugString("IOCTL_WINIO_MAPPHYSTOLIN");
  143. if (dwInputBufferLength)
  144. {
  145. memcpy (&Phys32Struct, pvIOBuffer, dwInputBufferLength);
  146. ntStatus = MapPhysicalMemoryToLinearSpace(Phys32Struct.pvPhysAddress,
  147. Phys32Struct.dwPhysMemSizeInBytes,
  148. &Phys32Struct.pvPhysMemLin,
  149. &Phys32Struct.PhysicalMemoryHandle);
  150. if (NT_SUCCESS(ntStatus))
  151. {
  152. memcpy (pvIOBuffer, &Phys32Struct, dwInputBufferLength);
  153. Irp->IoStatus.Information = dwInputBufferLength;
  154. }
  155. Irp->IoStatus.Status = ntStatus;
  156. }
  157. else
  158. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  159. break;
  160. case IOCTL_WINIO_UNMAPPHYSADDR:
  161. OutputDebugString("IOCTL_WINIO_UNMAPPHYSADDR");
  162. if (dwInputBufferLength)
  163. {
  164. memcpy (&Phys32Struct, pvIOBuffer, dwInputBufferLength);
  165. ntStatus = UnmapPhysicalMemory(Phys32Struct.PhysicalMemoryHandle, Phys32Struct.pvPhysMemLin);
  166. Irp->IoStatus.Status = ntStatus;
  167. }
  168. else
  169. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  170. break;
  171. default:
  172. OutputDebugString("ERROR: Unknown IRP_MJ_DEVICE_CONTROL");
  173. Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
  174. break;
  175. }
  176. break;
  177. }
  178. // DON'T get cute and try to use the status field of the irp in the
  179. // return status. That IRP IS GONE as soon as you call IoCompleteRequest.
  180. ntStatus = Irp->IoStatus.Status;
  181. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  182. // We never have pending operation so always return the status code.
  183. OutputDebugString("Leaving WinIoDispatch");
  184. return ntStatus;
  185. }
  186. // Delete the associated device and return
  187. void WinIoUnload(IN PDRIVER_OBJECT DriverObject)
  188. {
  189. UNICODE_STRING DeviceLinkUnicodeString;
  190. NTSTATUS ntStatus;
  191. OutputDebugString ("Entering WinIoUnload");
  192. RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\WinIo");
  193. ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString);
  194. if (NT_SUCCESS(ntStatus))
  195. {
  196. IoDeleteDevice (DriverObject->DeviceObject);
  197. }
  198. else
  199. {
  200. OutputDebugString ("ERROR: IoDeleteSymbolicLink");
  201. }
  202. OutputDebugString ("Leaving WinIoUnload");
  203. }
  204. NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,
  205. ULONG PhysMemSizeInBytes,
  206. PVOID *ppPhysMemLin,
  207. HANDLE *pPhysicalMemoryHandle)
  208. {
  209. UNICODE_STRING PhysicalMemoryUnicodeString;
  210. PVOID PhysicalMemorySection = NULL;
  211. OBJECT_ATTRIBUTES ObjectAttributes;
  212. PHYSICAL_ADDRESS ViewBase;
  213. NTSTATUS ntStatus;
  214. PHYSICAL_ADDRESS pStartPhysAddress;
  215. PHYSICAL_ADDRESS pEndPhysAddress;
  216. PHYSICAL_ADDRESS MappingLength;
  217. BOOLEAN Result1, Result2;
  218. ULONG IsIOSpace;
  219. unsigned char *pbPhysMemLin = NULL;
  220. OutputDebugString ("Entering MapPhysicalMemoryToLinearSpace");
  221. RtlInitUnicodeString (&PhysicalMemoryUnicodeString,
  222. L"\\Device\\PhysicalMemory");
  223. InitializeObjectAttributes (&ObjectAttributes,
  224. &PhysicalMemoryUnicodeString,
  225. OBJ_CASE_INSENSITIVE,
  226. (HANDLE) NULL,
  227. (PSECURITY_DESCRIPTOR) NULL);
  228. *pPhysicalMemoryHandle = NULL;
  229. ntStatus = ZwOpenSection (pPhysicalMemoryHandle,
  230. SECTION_ALL_ACCESS,
  231. &ObjectAttributes);
  232. if (NT_SUCCESS(ntStatus))
  233. {
  234. ntStatus = ObReferenceObjectByHandle (*pPhysicalMemoryHandle,
  235. SECTION_ALL_ACCESS,
  236. (POBJECT_TYPE) NULL,
  237. KernelMode,
  238. &PhysicalMemorySection,
  239. (POBJECT_HANDLE_INFORMATION) NULL);
  240. if (NT_SUCCESS(ntStatus))
  241. {
  242. pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress;
  243. pEndPhysAddress = RtlLargeIntegerAdd (pStartPhysAddress,
  244. RtlConvertUlongToLargeInteger(PhysMemSizeInBytes));
  245. IsIOSpace = 0;
  246. Result1 = HalTranslateBusAddress (1, 0, pStartPhysAddress, &IsIOSpace, &pStartPhysAddress);
  247. IsIOSpace = 0;
  248. Result2 = HalTranslateBusAddress (1, 0, pEndPhysAddress, &IsIOSpace, &pEndPhysAddress);
  249. if (Result1 && Result2)
  250. {
  251. MappingLength = RtlLargeIntegerSubtract (pEndPhysAddress, pStartPhysAddress);
  252. if (MappingLength.LowPart)
  253. {
  254. // Let ZwMapViewOfSection pick a linear address
  255. PhysMemSizeInBytes = MappingLength.LowPart;
  256. ViewBase = pStartPhysAddress;
  257. ntStatus = ZwMapViewOfSection (*pPhysicalMemoryHandle,
  258. (HANDLE) -1,
  259. &pbPhysMemLin,
  260. 0L,
  261. PhysMemSizeInBytes,
  262. &ViewBase,
  263. &PhysMemSizeInBytes,
  264. ViewShare,
  265. 0,
  266. PAGE_READWRITE | PAGE_NOCACHE);
  267. if (!NT_SUCCESS(ntStatus))
  268. OutputDebugString ("ERROR: ZwMapViewOfSection failed");
  269. else
  270. {
  271. pbPhysMemLin += (ULONG)pStartPhysAddress.LowPart - (ULONG)ViewBase.LowPart;
  272. *ppPhysMemLin = pbPhysMemLin;
  273. }
  274. }
  275. else
  276. OutputDebugString ("ERROR: RtlLargeIntegerSubtract failed");
  277. }
  278. else
  279. OutputDebugString ("ERROR: MappingLength = 0");
  280. }
  281. else
  282. OutputDebugString ("ERROR: ObReferenceObjectByHandle failed");
  283. }
  284. else
  285. OutputDebugString ("ERROR: ZwOpenSection failed");
  286. if (!NT_SUCCESS(ntStatus))
  287. ZwClose(*pPhysicalMemoryHandle);
  288. OutputDebugString ("Leaving MapPhysicalMemoryToLinearSpace");
  289. return ntStatus;
  290. }
  291. NTSTATUS UnmapPhysicalMemory(HANDLE PhysicalMemoryHandle, PVOID pPhysMemLin)
  292. {
  293. NTSTATUS ntStatus;
  294. OutputDebugString ("Entering UnmapPhysicalMemory");
  295. ntStatus = ZwUnmapViewOfSection((HANDLE)-1, pPhysMemLin);
  296. if (!NT_SUCCESS(ntStatus))
  297. OutputDebugString ("ERROR: UnmapViewOfSection failed");
  298. ZwClose(PhysicalMemoryHandle);
  299. OutputDebugString ("Leaving UnmapPhysicalMemory");
  300. return ntStatus;
  301. }