Source code of Windows XP (NT5)
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.

386 lines
8.4 KiB

  1. //
  2. // Buggy - Template Test Driver
  3. // Copyright (c) Microsoft Corporation, 1999, 2000.
  4. //
  5. // Module: tdriver.c
  6. // Author: Silviu Calinoiu (SilviuC)
  7. // Created: 4/20/1999 2:39pm
  8. //
  9. // This module contains a template driver.
  10. //
  11. // --- History ---
  12. //
  13. // 4/20/1999 (SilviuC): initial version.
  14. //
  15. // 1/19/2000 (SilviuC): make it really extensible.
  16. //
  17. //
  18. // PLEASE READ IF YOU MODIFY THIS FILE !
  19. //
  20. // The only modification needed in this module is an include
  21. // statement for the header of the module implementing the new
  22. // test in the section `Test specific headers'. That's all.
  23. //
  24. #include <ntddk.h>
  25. //
  26. // Test specific headers.
  27. //
  28. #include "active.h"
  29. #include "bugcheck.h"
  30. #include "ContMem.h"
  31. #include "SectMap.h"
  32. #include "tracedb.h"
  33. #include "physmem.h"
  34. #include "mmtests.h"
  35. #include "MapView.h"
  36. #include "locktest.h"
  37. #include "ResrvMap.h"
  38. #include "newstuff.h"
  39. //
  40. // Standard tdriver headers.
  41. //
  42. #define FUNS_DEFINITION_MODULE
  43. #include "tdriver.h"
  44. #include "funs.h"
  45. //
  46. // Driver implementation
  47. //
  48. NTSTATUS
  49. DriverEntry (
  50. IN PDRIVER_OBJECT DriverObject,
  51. IN PUNICODE_STRING RegistryPath
  52. )
  53. {
  54. NTSTATUS Status;
  55. UNICODE_STRING NtName;
  56. UNICODE_STRING Win32Name;
  57. ULONG Index;
  58. PDEVICE_OBJECT Device;
  59. DbgPrint ("Buggy: DriverEntry() \n");
  60. //
  61. // Create Unicode NT name for the device.
  62. RtlInitUnicodeString (
  63. &NtName,
  64. TD_NT_DEVICE_NAME);
  65. //
  66. // Create NT device
  67. //
  68. Status = IoCreateDevice (
  69. DriverObject, // pointer to driver object
  70. sizeof (TD_DRIVER_INFO), // device extension
  71. &NtName, // device name
  72. FILE_DEVICE_UNKNOWN, // device type
  73. 0, // device characteristics
  74. FALSE, // not exclusive
  75. &Device); // returned device object pointer
  76. if (! NT_SUCCESS(Status)) {
  77. return Status;
  78. }
  79. //
  80. // Create dispatch points
  81. //
  82. for (Index = 0; Index < IRP_MJ_MAXIMUM_FUNCTION; Index++) {
  83. DriverObject->MajorFunction[Index] = TdInvalidDeviceRequest;
  84. }
  85. DriverObject->MajorFunction[IRP_MJ_CREATE] = TdDeviceCreate;
  86. DriverObject->MajorFunction[IRP_MJ_CLOSE] = TdDeviceClose;
  87. DriverObject->MajorFunction[IRP_MJ_CLEANUP] = TdDeviceCleanup;
  88. DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = TdDeviceControl;
  89. DriverObject->DriverUnload = TdDeviceUnload;
  90. //
  91. // Create counted string version of our Win32 device name.
  92. //
  93. RtlInitUnicodeString (
  94. &Win32Name,
  95. TD_DOS_DEVICE_NAME);
  96. //
  97. // Create a link from our device name to a name in the Win32 namespace.
  98. //
  99. Status = IoCreateSymbolicLink (
  100. &Win32Name,
  101. &NtName);
  102. if (! NT_SUCCESS(Status)) {
  103. IoDeleteDevice (DriverObject->DeviceObject);
  104. return Status;
  105. }
  106. return Status;
  107. }
  108. NTSTATUS
  109. TdDeviceCreate (
  110. PDEVICE_OBJECT DeviceObject,
  111. PIRP Irp
  112. )
  113. //
  114. // Handles create IRP.
  115. //
  116. {
  117. Irp->IoStatus.Status = STATUS_SUCCESS;
  118. Irp->IoStatus.Information = 0;
  119. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  120. return STATUS_SUCCESS;
  121. }
  122. NTSTATUS
  123. TdDeviceClose (
  124. PDEVICE_OBJECT DeviceObject,
  125. PIRP Irp
  126. )
  127. //
  128. // Handles close IRP.
  129. //
  130. {
  131. Irp->IoStatus.Status = STATUS_SUCCESS;
  132. Irp->IoStatus.Information = 0;
  133. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  134. return STATUS_SUCCESS;
  135. }
  136. NTSTATUS
  137. TdDeviceCleanup (
  138. PDEVICE_OBJECT DeviceObject,
  139. PIRP Irp
  140. )
  141. //
  142. // Handles cleanup IRP.
  143. //
  144. {
  145. Irp->IoStatus.Status = STATUS_SUCCESS;
  146. Irp->IoStatus.Information = 0;
  147. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  148. return STATUS_SUCCESS;
  149. }
  150. NTSTATUS
  151. TdDeviceControl (
  152. IN PDEVICE_OBJECT DeviceObject,
  153. IN PIRP Irp
  154. )
  155. //
  156. // Handles control IRP.
  157. //
  158. {
  159. PIO_STACK_LOCATION IrpStack;
  160. ULONG InputBufferLength;
  161. ULONG OutputBufferLength;
  162. ULONG Ioctl;
  163. NTSTATUS Status;
  164. ULONG BufferSize;
  165. ULONG ReturnedSize;
  166. KIRQL irql;
  167. ULONG Index;
  168. LOGICAL IoctlFound = FALSE;
  169. Status = STATUS_SUCCESS;
  170. IrpStack = IoGetCurrentIrpStackLocation (Irp);
  171. InputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
  172. OutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
  173. Ioctl = IrpStack->Parameters.DeviceIoControl.IoControlCode;
  174. if( Ioctl == IOCTL_TD_BUGCHECK )
  175. {
  176. if( InputBufferLength == sizeof( BUGCHECK_PARAMS ) )
  177. {
  178. PBUGCHECK_PARAMS pBugcheckParams = (PBUGCHECK_PARAMS)(Irp->AssociatedIrp.SystemBuffer);
  179. DbgPrint( "Buggy: calling KeBugCheckEx( %X, %p, %p, %p, %p )\n",
  180. pBugcheckParams->BugCheckCode,
  181. pBugcheckParams->BugCheckParameters[ 0 ],
  182. pBugcheckParams->BugCheckParameters[ 1 ],
  183. pBugcheckParams->BugCheckParameters[ 2 ],
  184. pBugcheckParams->BugCheckParameters[ 3 ] );
  185. KeBugCheckEx(
  186. pBugcheckParams->BugCheckCode,
  187. pBugcheckParams->BugCheckParameters[ 0 ],
  188. pBugcheckParams->BugCheckParameters[ 1 ],
  189. pBugcheckParams->BugCheckParameters[ 2 ],
  190. pBugcheckParams->BugCheckParameters[ 3 ] );
  191. }
  192. else
  193. {
  194. DbgPrint( "Buggy: cannot read bugcheck data, expected data length %u, IrpStack->Parameters.DeviceIoControl.InputBufferLength = %u\n",
  195. sizeof( BUGCHECK_PARAMS ),
  196. InputBufferLength );
  197. }
  198. goto Done;
  199. }
  200. //
  201. // (SilviuC): maybe we should do parameter checking on the info buffer.
  202. // Not really important since this is not a production driver.
  203. //
  204. for (Index = 0; BuggyFuns[Index].Ioctl != 0; Index++) {
  205. if (Ioctl == BuggyFuns[Index].Ioctl) {
  206. DbgPrint ("Buggy: %s ioctl \n", BuggyFuns[Index].Message);
  207. (BuggyFuns[Index].Function)((PVOID)Irp);
  208. DbgPrint ("Buggy: done with %s. \n", BuggyFuns[Index].Message);
  209. IoctlFound = TRUE;
  210. break;
  211. }
  212. }
  213. //
  214. // Complain if Ioctl code not found.
  215. //
  216. if (! IoctlFound) {
  217. DbgPrint ("Buggy: unrecognized ioctl code %u \n", Ioctl);
  218. }
  219. //
  220. // Complete the irp and return.
  221. //
  222. Done:
  223. Irp->IoStatus.Status = Status;
  224. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  225. return Status;
  226. }
  227. VOID
  228. TdDeviceUnload (
  229. IN PDRIVER_OBJECT DriverObject
  230. )
  231. //
  232. // This function handles driver unloading. All this driver needs to do
  233. // is to delete the device object and the symbolic link between our
  234. // device name and the Win32 visible name.
  235. //
  236. {
  237. UNICODE_STRING Win32Name;
  238. DbgPrint ("Buggy: unload \n");
  239. #if RESRVMAP_ACTIVE
  240. //
  241. // Clean-up a possible currently reserved buffer
  242. //
  243. TdReservedMappingCleanup();
  244. #endif //#if RESRVMAP_ACTIVE
  245. //
  246. //
  247. //
  248. // Create counted string version of our Win32 device name.
  249. //
  250. RtlInitUnicodeString (
  251. &Win32Name,
  252. TD_DOS_DEVICE_NAME );
  253. //
  254. // Delete the link from our device name to a name in the Win32 namespace.
  255. //
  256. IoDeleteSymbolicLink (&Win32Name);
  257. //
  258. // Finally delete our device object
  259. //
  260. IoDeleteDevice (DriverObject->DeviceObject);
  261. }
  262. NTSTATUS
  263. TdInvalidDeviceRequest(
  264. IN PDEVICE_OBJECT DeviceObject,
  265. IN PIRP Irp
  266. )
  267. /*++
  268. Routine Description:
  269. This function is the default dispatch routine for all driver entries
  270. not implemented by drivers that have been loaded into the system. Its
  271. responsibility is simply to set the status in the packet to indicate
  272. that the operation requested is invalid for this device type, and then
  273. complete the packet.
  274. Arguments:
  275. DeviceObject - Specifies the device object for which this request is
  276. bound. Ignored by this routine.
  277. Irp - Specifies the address of the I/O Request Packet (IRP) for this
  278. request.
  279. Return Value:
  280. The final status is always STATUS_INVALID_DEVICE_REQUEST.
  281. --*/
  282. {
  283. UNREFERENCED_PARAMETER( DeviceObject );
  284. //
  285. // Simply store the appropriate status, complete the request, and return
  286. // the same status stored in the packet.
  287. //
  288. if ((IoGetCurrentIrpStackLocation(Irp))->MajorFunction == IRP_MJ_POWER) {
  289. PoStartNextPowerIrp(Irp);
  290. }
  291. Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
  292. IoCompleteRequest( Irp, IO_NO_INCREMENT );
  293. return STATUS_INVALID_DEVICE_REQUEST;
  294. }
  295. //
  296. // End of module: tdriver.c
  297. //