499e42f8d183b5b988e46d5bfeff2b9a36970042
[reactos.git] / reactos / ntoskrnl / io / iomgr / driver.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/driver.c
5 * PURPOSE: Driver Object Management
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Filip Navara (navaraf@reactos.org)
8 * Hervé Poussineau (hpoussin@reactos.org)
9 */
10
11 /* INCLUDES *******************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <debug.h>
16
17 /* GLOBALS ********************************************************************/
18
19 ERESOURCE IopDriverLoadResource;
20
21 LIST_ENTRY DriverReinitListHead;
22 KSPIN_LOCK DriverReinitListLock;
23 PLIST_ENTRY DriverReinitTailEntry;
24
25 PLIST_ENTRY DriverBootReinitTailEntry;
26 LIST_ENTRY DriverBootReinitListHead;
27 KSPIN_LOCK DriverBootReinitListLock;
28
29 UNICODE_STRING IopHardwareDatabaseKey =
30 RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DESCRIPTION\\SYSTEM");
31
32 POBJECT_TYPE IoDriverObjectType = NULL;
33
34 #define TAG_RTLREGISTRY 'vrqR'
35
36 extern BOOLEAN ExpInTextModeSetup;
37 extern BOOLEAN PnpSystemInit;
38
39 USHORT IopGroupIndex;
40 PLIST_ENTRY IopGroupTable;
41
42 /* PRIVATE FUNCTIONS **********************************************************/
43
44 NTSTATUS
45 NTAPI
46 IopInvalidDeviceRequest(
47 PDEVICE_OBJECT DeviceObject,
48 PIRP Irp)
49 {
50 Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
51 Irp->IoStatus.Information = 0;
52 IoCompleteRequest(Irp, IO_NO_INCREMENT);
53 return STATUS_INVALID_DEVICE_REQUEST;
54 }
55
56 VOID
57 NTAPI
58 IopDeleteDriver(IN PVOID ObjectBody)
59 {
60 PDRIVER_OBJECT DriverObject = ObjectBody;
61 PIO_CLIENT_EXTENSION DriverExtension, NextDriverExtension;
62 PAGED_CODE();
63
64 DPRINT1("Deleting driver object '%wZ'\n", &DriverObject->DriverName);
65
66 /* There must be no device objects remaining at this point */
67 ASSERT(!DriverObject->DeviceObject);
68
69 /* Get the extension and loop them */
70 DriverExtension = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
71 while (DriverExtension)
72 {
73 /* Get the next one */
74 NextDriverExtension = DriverExtension->NextExtension;
75 ExFreePoolWithTag(DriverExtension, TAG_DRIVER_EXTENSION);
76
77 /* Move on */
78 DriverExtension = NextDriverExtension;
79 }
80
81 /* Check if the driver image is still loaded */
82 if (DriverObject->DriverSection)
83 {
84 /* Unload it */
85 MmUnloadSystemImage(DriverObject->DriverSection);
86 }
87
88 /* Check if it has a name */
89 if (DriverObject->DriverName.Buffer)
90 {
91 /* Free it */
92 ExFreePool(DriverObject->DriverName.Buffer);
93 }
94
95 /* Check if it has a service key name */
96 if (DriverObject->DriverExtension->ServiceKeyName.Buffer)
97 {
98 /* Free it */
99 ExFreePool(DriverObject->DriverExtension->ServiceKeyName.Buffer);
100 }
101 }
102
103 NTSTATUS
104 FASTCALL
105 IopGetDriverObject(
106 PDRIVER_OBJECT *DriverObject,
107 PUNICODE_STRING ServiceName,
108 BOOLEAN FileSystem)
109 {
110 PDRIVER_OBJECT Object;
111 WCHAR NameBuffer[MAX_PATH];
112 UNICODE_STRING DriverName;
113 NTSTATUS Status;
114
115 DPRINT("IopGetDriverObject(%p '%wZ' %x)\n",
116 DriverObject, ServiceName, FileSystem);
117
118 ASSERT(ExIsResourceAcquiredExclusiveLite(&IopDriverLoadResource));
119 *DriverObject = NULL;
120
121 /* Create ModuleName string */
122 if (ServiceName == NULL || ServiceName->Buffer == NULL)
123 /* We don't know which DriverObject we have to open */
124 return STATUS_INVALID_PARAMETER_2;
125
126 DriverName.Buffer = NameBuffer;
127 DriverName.Length = 0;
128 DriverName.MaximumLength = sizeof(NameBuffer);
129
130 if (FileSystem != FALSE)
131 RtlAppendUnicodeToString(&DriverName, FILESYSTEM_ROOT_NAME);
132 else
133 RtlAppendUnicodeToString(&DriverName, DRIVER_ROOT_NAME);
134 RtlAppendUnicodeStringToString(&DriverName, ServiceName);
135
136 DPRINT("Driver name: '%wZ'\n", &DriverName);
137
138 /* Open driver object */
139 Status = ObReferenceObjectByName(&DriverName,
140 OBJ_OPENIF | OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, /* Attributes */
141 NULL, /* PassedAccessState */
142 0, /* DesiredAccess */
143 IoDriverObjectType,
144 KernelMode,
145 NULL, /* ParseContext */
146 (PVOID*)&Object);
147 if (!NT_SUCCESS(Status))
148 {
149 DPRINT("Failed to reference driver object, status=0x%08x\n", Status);
150 return Status;
151 }
152
153 *DriverObject = Object;
154
155 DPRINT("Driver Object: %p\n", Object);
156
157 return STATUS_SUCCESS;
158 }
159
160 /*
161 * RETURNS
162 * TRUE if String2 contains String1 as a suffix.
163 */
164 BOOLEAN
165 NTAPI
166 IopSuffixUnicodeString(
167 IN PCUNICODE_STRING String1,
168 IN PCUNICODE_STRING String2)
169 {
170 PWCHAR pc1;
171 PWCHAR pc2;
172 ULONG Length;
173
174 if (String2->Length < String1->Length)
175 return FALSE;
176
177 Length = String1->Length / 2;
178 pc1 = String1->Buffer;
179 pc2 = &String2->Buffer[String2->Length / sizeof(WCHAR) - Length];
180
181 if (pc1 && pc2)
182 {
183 while (Length--)
184 {
185 if( *pc1++ != *pc2++ )
186 return FALSE;
187 }
188 return TRUE;
189 }
190 return FALSE;
191 }
192
193 /*
194 * IopDisplayLoadingMessage
195 *
196 * Display 'Loading XXX...' message.
197 */
198 VOID
199 FASTCALL
200 IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
201 {
202 CHAR TextBuffer[256];
203 UNICODE_STRING DotSys = RTL_CONSTANT_STRING(L".SYS");
204
205 if (ExpInTextModeSetup) return;
206 if (!KeLoaderBlock) return;
207 RtlUpcaseUnicodeString(ServiceName, ServiceName, FALSE);
208 snprintf(TextBuffer, sizeof(TextBuffer),
209 "%s%sSystem32\\Drivers\\%wZ%s\r\n",
210 KeLoaderBlock->ArcBootDeviceName,
211 KeLoaderBlock->NtBootPathName,
212 ServiceName,
213 IopSuffixUnicodeString(&DotSys, ServiceName) ? "" : ".SYS");
214 HalDisplayString(TextBuffer);
215 }
216
217 /*
218 * IopNormalizeImagePath
219 *
220 * Normalize an image path to contain complete path.
221 *
222 * Parameters
223 * ImagePath
224 * The input path and on exit the result path. ImagePath.Buffer
225 * must be allocated by ExAllocatePool on input. Caller is responsible
226 * for freeing the buffer when it's no longer needed.
227 *
228 * ServiceName
229 * Name of the service that ImagePath belongs to.
230 *
231 * Return Value
232 * Status
233 *
234 * Remarks
235 * The input image path isn't freed on error.
236 */
237 NTSTATUS
238 FASTCALL
239 IopNormalizeImagePath(
240 _Inout_ _When_(return>=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem)))
241 PUNICODE_STRING ImagePath,
242 _In_ PUNICODE_STRING ServiceName)
243 {
244 UNICODE_STRING SystemRootString = RTL_CONSTANT_STRING(L"\\SystemRoot\\");
245 UNICODE_STRING DriversPathString = RTL_CONSTANT_STRING(L"\\SystemRoot\\System32\\drivers\\");
246 UNICODE_STRING DotSysString = RTL_CONSTANT_STRING(L".sys");
247 UNICODE_STRING InputImagePath;
248
249 DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
250
251 InputImagePath = *ImagePath;
252 if (InputImagePath.Length == 0)
253 {
254 ImagePath->Length = 0;
255 ImagePath->MaximumLength = DriversPathString.Length +
256 ServiceName->Length +
257 DotSysString.Length +
258 sizeof(UNICODE_NULL);
259 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
260 ImagePath->MaximumLength,
261 TAG_IO);
262 if (ImagePath->Buffer == NULL)
263 return STATUS_NO_MEMORY;
264
265 RtlCopyUnicodeString(ImagePath, &DriversPathString);
266 RtlAppendUnicodeStringToString(ImagePath, ServiceName);
267 RtlAppendUnicodeStringToString(ImagePath, &DotSysString);
268 }
269 else if (InputImagePath.Buffer[0] != L'\\')
270 {
271 ImagePath->Length = 0;
272 ImagePath->MaximumLength = SystemRootString.Length +
273 InputImagePath.Length +
274 sizeof(UNICODE_NULL);
275 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
276 ImagePath->MaximumLength,
277 TAG_IO);
278 if (ImagePath->Buffer == NULL)
279 return STATUS_NO_MEMORY;
280
281 RtlCopyUnicodeString(ImagePath, &SystemRootString);
282 RtlAppendUnicodeStringToString(ImagePath, &InputImagePath);
283
284 /* Free caller's string */
285 ExFreePoolWithTag(InputImagePath.Buffer, TAG_RTLREGISTRY);
286 }
287
288 DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
289
290 return STATUS_SUCCESS;
291 }
292
293 /*
294 * IopLoadServiceModule
295 *
296 * Load a module specified by registry settings for service.
297 *
298 * Parameters
299 * ServiceName
300 * Name of the service to load.
301 *
302 * Return Value
303 * Status
304 */
305 NTSTATUS
306 FASTCALL
307 IopLoadServiceModule(
308 IN PUNICODE_STRING ServiceName,
309 OUT PLDR_DATA_TABLE_ENTRY *ModuleObject)
310 {
311 RTL_QUERY_REGISTRY_TABLE QueryTable[3];
312 ULONG ServiceStart;
313 UNICODE_STRING ServiceImagePath, CCSName;
314 NTSTATUS Status;
315 HANDLE CCSKey, ServiceKey;
316 PVOID BaseAddress;
317
318 ASSERT(ExIsResourceAcquiredExclusiveLite(&IopDriverLoadResource));
319 ASSERT(ServiceName->Length);
320 DPRINT("IopLoadServiceModule(%wZ, 0x%p)\n", ServiceName, ModuleObject);
321
322 if (ExpInTextModeSetup)
323 {
324 /* We have no registry, but luckily we know where all the drivers are */
325
326 /* ServiceStart < 4 is all that matters */
327 ServiceStart = 0;
328
329 /* IopNormalizeImagePath will do all of the work for us if we give it an empty string */
330 RtlInitEmptyUnicodeString(&ServiceImagePath, NULL, 0);
331 }
332 else
333 {
334 /* Open CurrentControlSet */
335 RtlInitUnicodeString(&CCSName,
336 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
337 Status = IopOpenRegistryKeyEx(&CCSKey, NULL, &CCSName, KEY_READ);
338 if (!NT_SUCCESS(Status))
339 {
340 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with Status %08X\n",
341 &CCSName, Status);
342 return Status;
343 }
344
345 /* Open service key */
346 Status = IopOpenRegistryKeyEx(&ServiceKey, CCSKey, ServiceName, KEY_READ);
347 if (!NT_SUCCESS(Status))
348 {
349 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with Status %08X\n",
350 ServiceName, Status);
351 ZwClose(CCSKey);
352 return Status;
353 }
354
355 /*
356 * Get information about the service.
357 */
358 RtlZeroMemory(QueryTable, sizeof(QueryTable));
359
360 RtlInitUnicodeString(&ServiceImagePath, NULL);
361
362 QueryTable[0].Name = L"Start";
363 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
364 QueryTable[0].EntryContext = &ServiceStart;
365
366 QueryTable[1].Name = L"ImagePath";
367 QueryTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
368 QueryTable[1].EntryContext = &ServiceImagePath;
369
370 Status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
371 (PWSTR)ServiceKey,
372 QueryTable,
373 NULL,
374 NULL);
375
376 ZwClose(ServiceKey);
377 ZwClose(CCSKey);
378
379 if (!NT_SUCCESS(Status))
380 {
381 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
382 return Status;
383 }
384 }
385
386 /*
387 * Normalize the image path for all later processing.
388 */
389 Status = IopNormalizeImagePath(&ServiceImagePath, ServiceName);
390
391 if (!NT_SUCCESS(Status))
392 {
393 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
394 return Status;
395 }
396
397 /*
398 * Case for disabled drivers
399 */
400 if (ServiceStart >= 4)
401 {
402 /* We can't load this */
403 Status = STATUS_DRIVER_UNABLE_TO_LOAD;
404 }
405 else
406 {
407 DPRINT("Loading module from %wZ\n", &ServiceImagePath);
408 Status = MmLoadSystemImage(&ServiceImagePath, NULL, NULL, 0, (PVOID)ModuleObject, &BaseAddress);
409 if (NT_SUCCESS(Status))
410 {
411 IopDisplayLoadingMessage(ServiceName);
412 }
413 }
414
415 ExFreePool(ServiceImagePath.Buffer);
416
417 /*
418 * Now check if the module was loaded successfully.
419 */
420 if (!NT_SUCCESS(Status))
421 {
422 DPRINT("Module loading failed (Status %x)\n", Status);
423 }
424
425 DPRINT("Module loading (Status %x)\n", Status);
426
427 return Status;
428 }
429
430 VOID
431 NTAPI
432 MmFreeDriverInitialization(IN PLDR_DATA_TABLE_ENTRY LdrEntry);
433
434 /*
435 * IopInitializeDriverModule
436 *
437 * Initialize a loaded driver.
438 *
439 * Parameters
440 * DeviceNode
441 * Pointer to device node.
442 *
443 * ModuleObject
444 * Module object representing the driver. It can be retrieve by
445 * IopLoadServiceModule.
446 *
447 * ServiceName
448 * Name of the service (as in registry).
449 *
450 * FileSystemDriver
451 * Set to TRUE for file system drivers.
452 *
453 * DriverObject
454 * On successful return this contains the driver object representing
455 * the loaded driver.
456 */
457 NTSTATUS
458 FASTCALL
459 IopInitializeDriverModule(
460 IN PDEVICE_NODE DeviceNode,
461 IN PLDR_DATA_TABLE_ENTRY ModuleObject,
462 IN PUNICODE_STRING ServiceName,
463 IN BOOLEAN FileSystemDriver,
464 OUT PDRIVER_OBJECT *DriverObject)
465 {
466 const WCHAR ServicesKeyName[] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\";
467 WCHAR NameBuffer[MAX_PATH];
468 UNICODE_STRING DriverName;
469 UNICODE_STRING RegistryKey;
470 PDRIVER_INITIALIZE DriverEntry;
471 PDRIVER_OBJECT Driver;
472 NTSTATUS Status;
473
474 DriverEntry = ModuleObject->EntryPoint;
475
476 if (ServiceName != NULL && ServiceName->Length != 0)
477 {
478 RegistryKey.Length = 0;
479 RegistryKey.MaximumLength = sizeof(ServicesKeyName) + ServiceName->Length;
480 RegistryKey.Buffer = ExAllocatePool(PagedPool, RegistryKey.MaximumLength);
481 if (RegistryKey.Buffer == NULL)
482 {
483 return STATUS_INSUFFICIENT_RESOURCES;
484 }
485 RtlAppendUnicodeToString(&RegistryKey, ServicesKeyName);
486 RtlAppendUnicodeStringToString(&RegistryKey, ServiceName);
487 }
488 else
489 {
490 RtlInitUnicodeString(&RegistryKey, NULL);
491 }
492
493 /* Create ModuleName string */
494 if (ServiceName && ServiceName->Length > 0)
495 {
496 if (FileSystemDriver != FALSE)
497 wcscpy(NameBuffer, FILESYSTEM_ROOT_NAME);
498 else
499 wcscpy(NameBuffer, DRIVER_ROOT_NAME);
500
501 RtlInitUnicodeString(&DriverName, NameBuffer);
502 DriverName.MaximumLength = sizeof(NameBuffer);
503
504 RtlAppendUnicodeStringToString(&DriverName, ServiceName);
505
506 DPRINT("Driver name: '%wZ'\n", &DriverName);
507 }
508 else
509 DriverName.Length = 0;
510
511 Status = IopCreateDriver(DriverName.Length > 0 ? &DriverName : NULL,
512 DriverEntry,
513 &RegistryKey,
514 ServiceName,
515 ModuleObject,
516 &Driver);
517 RtlFreeUnicodeString(&RegistryKey);
518
519 *DriverObject = Driver;
520 if (!NT_SUCCESS(Status))
521 {
522 DPRINT("IopCreateDriver() failed (Status 0x%08lx)\n", Status);
523 return Status;
524 }
525
526 MmFreeDriverInitialization((PLDR_DATA_TABLE_ENTRY)Driver->DriverSection);
527
528 /* Set the driver as initialized */
529 IopReadyDeviceObjects(Driver);
530
531 if (PnpSystemInit) IopReinitializeDrivers();
532
533 return STATUS_SUCCESS;
534 }
535
536 /*
537 * IopAttachFilterDriversCallback
538 *
539 * Internal routine used by IopAttachFilterDrivers.
540 */
541 NTSTATUS
542 NTAPI
543 IopAttachFilterDriversCallback(
544 PWSTR ValueName,
545 ULONG ValueType,
546 PVOID ValueData,
547 ULONG ValueLength,
548 PVOID Context,
549 PVOID EntryContext)
550 {
551 PDEVICE_NODE DeviceNode = Context;
552 UNICODE_STRING ServiceName;
553 PWCHAR Filters;
554 PLDR_DATA_TABLE_ENTRY ModuleObject;
555 PDRIVER_OBJECT DriverObject;
556 NTSTATUS Status;
557
558 /* No filter value present */
559 if (ValueType == REG_NONE)
560 return STATUS_SUCCESS;
561
562 for (Filters = ValueData;
563 ((ULONG_PTR)Filters - (ULONG_PTR)ValueData) < ValueLength &&
564 *Filters != 0;
565 Filters += (ServiceName.Length / sizeof(WCHAR)) + 1)
566 {
567 DPRINT("Filter Driver: %S (%wZ)\n", Filters, &DeviceNode->InstancePath);
568
569 ServiceName.Buffer = Filters;
570 ServiceName.MaximumLength =
571 ServiceName.Length = (USHORT)wcslen(Filters) * sizeof(WCHAR);
572
573 KeEnterCriticalRegion();
574 ExAcquireResourceExclusiveLite(&IopDriverLoadResource, TRUE);
575 Status = IopGetDriverObject(&DriverObject,
576 &ServiceName,
577 FALSE);
578 if (!NT_SUCCESS(Status))
579 {
580 /* Load and initialize the filter driver */
581 Status = IopLoadServiceModule(&ServiceName, &ModuleObject);
582 if (!NT_SUCCESS(Status))
583 {
584 ExReleaseResourceLite(&IopDriverLoadResource);
585 KeLeaveCriticalRegion();
586 return Status;
587 }
588
589 Status = IopInitializeDriverModule(DeviceNode,
590 ModuleObject,
591 &ServiceName,
592 FALSE,
593 &DriverObject);
594 if (!NT_SUCCESS(Status))
595 {
596 ExReleaseResourceLite(&IopDriverLoadResource);
597 KeLeaveCriticalRegion();
598 return Status;
599 }
600 }
601
602 ExReleaseResourceLite(&IopDriverLoadResource);
603 KeLeaveCriticalRegion();
604
605 Status = IopInitializeDevice(DeviceNode, DriverObject);
606
607 /* Remove extra reference */
608 ObDereferenceObject(DriverObject);
609
610 if (!NT_SUCCESS(Status))
611 return Status;
612 }
613
614 return STATUS_SUCCESS;
615 }
616
617 /*
618 * IopAttachFilterDrivers
619 *
620 * Load filter drivers for specified device node.
621 *
622 * Parameters
623 * Lower
624 * Set to TRUE for loading lower level filters or FALSE for upper
625 * level filters.
626 */
627 NTSTATUS
628 FASTCALL
629 IopAttachFilterDrivers(
630 PDEVICE_NODE DeviceNode,
631 BOOLEAN Lower)
632 {
633 RTL_QUERY_REGISTRY_TABLE QueryTable[2] = { { NULL, 0, NULL, NULL, 0, NULL, 0 }, };
634 UNICODE_STRING Class;
635 WCHAR ClassBuffer[40];
636 UNICODE_STRING EnumRoot = RTL_CONSTANT_STRING(ENUM_ROOT);
637 HANDLE EnumRootKey, SubKey;
638 NTSTATUS Status;
639
640 /* Open enumeration root key */
641 Status = IopOpenRegistryKeyEx(&EnumRootKey,
642 NULL,
643 &EnumRoot,
644 KEY_READ);
645 if (!NT_SUCCESS(Status))
646 {
647 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status);
648 return Status;
649 }
650
651 /* Open subkey */
652 Status = IopOpenRegistryKeyEx(&SubKey,
653 EnumRootKey,
654 &DeviceNode->InstancePath,
655 KEY_READ);
656 if (!NT_SUCCESS(Status))
657 {
658 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status);
659 ZwClose(EnumRootKey);
660 return Status;
661 }
662
663 /*
664 * First load the device filters
665 */
666 QueryTable[0].QueryRoutine = IopAttachFilterDriversCallback;
667 if (Lower)
668 QueryTable[0].Name = L"LowerFilters";
669 else
670 QueryTable[0].Name = L"UpperFilters";
671 QueryTable[0].Flags = 0;
672 QueryTable[0].DefaultType = REG_NONE;
673
674 Status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
675 (PWSTR)SubKey,
676 QueryTable,
677 DeviceNode,
678 NULL);
679 if (!NT_SUCCESS(Status))
680 {
681 DPRINT1("Failed to load device %s filters: %08X\n",
682 Lower ? "lower" : "upper", Status);
683 ZwClose(SubKey);
684 ZwClose(EnumRootKey);
685 return Status;
686 }
687
688 /*
689 * Now get the class GUID
690 */
691 Class.Length = 0;
692 Class.MaximumLength = 40 * sizeof(WCHAR);
693 Class.Buffer = ClassBuffer;
694 QueryTable[0].QueryRoutine = NULL;
695 QueryTable[0].Name = L"ClassGUID";
696 QueryTable[0].EntryContext = &Class;
697 QueryTable[0].Flags = RTL_QUERY_REGISTRY_REQUIRED | RTL_QUERY_REGISTRY_DIRECT;
698
699 Status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
700 (PWSTR)SubKey,
701 QueryTable,
702 DeviceNode,
703 NULL);
704
705 /* Close handles */
706 ZwClose(SubKey);
707 ZwClose(EnumRootKey);
708
709 /*
710 * Load the class filter driver
711 */
712 if (NT_SUCCESS(Status))
713 {
714 UNICODE_STRING ControlClass = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Class");
715
716 Status = IopOpenRegistryKeyEx(&EnumRootKey,
717 NULL,
718 &ControlClass,
719 KEY_READ);
720 if (!NT_SUCCESS(Status))
721 {
722 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status);
723 return Status;
724 }
725
726 /* Open subkey */
727 Status = IopOpenRegistryKeyEx(&SubKey,
728 EnumRootKey,
729 &Class,
730 KEY_READ);
731 if (!NT_SUCCESS(Status))
732 {
733 /* It's okay if there's no class key */
734 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status);
735 ZwClose(EnumRootKey);
736 return STATUS_SUCCESS;
737 }
738
739 QueryTable[0].QueryRoutine = IopAttachFilterDriversCallback;
740 if (Lower)
741 QueryTable[0].Name = L"LowerFilters";
742 else
743 QueryTable[0].Name = L"UpperFilters";
744 QueryTable[0].EntryContext = NULL;
745 QueryTable[0].Flags = 0;
746 QueryTable[0].DefaultType = REG_NONE;
747
748 Status = RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
749 (PWSTR)SubKey,
750 QueryTable,
751 DeviceNode,
752 NULL);
753
754 /* Clean up */
755 ZwClose(SubKey);
756 ZwClose(EnumRootKey);
757
758 if (!NT_SUCCESS(Status))
759 {
760 DPRINT1("Failed to load class %s filters: %08X\n",
761 Lower ? "lower" : "upper", Status);
762 ZwClose(SubKey);
763 ZwClose(EnumRootKey);
764 return Status;
765 }
766 }
767
768 return STATUS_SUCCESS;
769 }
770
771 NTSTATUS
772 NTAPI
773 MiResolveImageReferences(IN PVOID ImageBase,
774 IN PUNICODE_STRING ImageFileDirectory,
775 IN PUNICODE_STRING NamePrefix OPTIONAL,
776 OUT PCHAR *MissingApi,
777 OUT PWCHAR *MissingDriver,
778 OUT PLOAD_IMPORTS *LoadImports);
779
780 //
781 // Used for images already loaded (boot drivers)
782 //
783 NTSTATUS
784 NTAPI
785 INIT_FUNCTION
786 LdrProcessDriverModule(PLDR_DATA_TABLE_ENTRY LdrEntry,
787 PUNICODE_STRING FileName,
788 PLDR_DATA_TABLE_ENTRY *ModuleObject)
789 {
790 NTSTATUS Status;
791 UNICODE_STRING BaseName, BaseDirectory;
792 PLOAD_IMPORTS LoadedImports = (PVOID)-2;
793 PCHAR MissingApiName, Buffer;
794 PWCHAR MissingDriverName;
795 PVOID DriverBase = LdrEntry->DllBase;
796
797 /* Allocate a buffer we'll use for names */
798 Buffer = ExAllocatePoolWithTag(NonPagedPool, MAX_PATH, TAG_LDR_WSTR);
799 if (!Buffer)
800 {
801 /* Fail */
802 return STATUS_INSUFFICIENT_RESOURCES;
803 }
804
805 /* Check for a separator */
806 if (FileName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
807 {
808 PWCHAR p;
809 ULONG BaseLength;
810
811 /* Loop the path until we get to the base name */
812 p = &FileName->Buffer[FileName->Length / sizeof(WCHAR)];
813 while (*(p - 1) != OBJ_NAME_PATH_SEPARATOR) p--;
814
815 /* Get the length */
816 BaseLength = (ULONG)(&FileName->Buffer[FileName->Length / sizeof(WCHAR)] - p);
817 BaseLength *= sizeof(WCHAR);
818
819 /* Setup the string */
820 BaseName.Length = (USHORT)BaseLength;
821 BaseName.Buffer = p;
822 }
823 else
824 {
825 /* Otherwise, we already have a base name */
826 BaseName.Length = FileName->Length;
827 BaseName.Buffer = FileName->Buffer;
828 }
829
830 /* Setup the maximum length */
831 BaseName.MaximumLength = BaseName.Length;
832
833 /* Now compute the base directory */
834 BaseDirectory = *FileName;
835 BaseDirectory.Length -= BaseName.Length;
836 BaseDirectory.MaximumLength = BaseDirectory.Length;
837
838 /* Resolve imports */
839 MissingApiName = Buffer;
840 Status = MiResolveImageReferences(DriverBase,
841 &BaseDirectory,
842 NULL,
843 &MissingApiName,
844 &MissingDriverName,
845 &LoadedImports);
846
847 /* Free the temporary buffer */
848 ExFreePoolWithTag(Buffer, TAG_LDR_WSTR);
849
850 /* Check the result of the imports resolution */
851 if (!NT_SUCCESS(Status)) return Status;
852
853 /* Return */
854 *ModuleObject = LdrEntry;
855 return STATUS_SUCCESS;
856 }
857
858 /*
859 * IopInitializeBuiltinDriver
860 *
861 * Initialize a driver that is already loaded in memory.
862 */
863 NTSTATUS
864 NTAPI
865 INIT_FUNCTION
866 IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
867 {
868 PDEVICE_NODE DeviceNode;
869 PDRIVER_OBJECT DriverObject;
870 NTSTATUS Status;
871 PWCHAR FileNameWithoutPath;
872 LPWSTR FileExtension;
873 PUNICODE_STRING ModuleName = &BootLdrEntry->BaseDllName;
874 PLDR_DATA_TABLE_ENTRY LdrEntry;
875 PLIST_ENTRY NextEntry;
876 UNICODE_STRING ServiceName;
877 BOOLEAN Success;
878
879 /*
880 * Display 'Loading XXX...' message
881 */
882 IopDisplayLoadingMessage(ModuleName);
883 InbvIndicateProgress();
884
885 /*
886 * Generate filename without path (not needed by freeldr)
887 */
888 FileNameWithoutPath = wcsrchr(ModuleName->Buffer, L'\\');
889 if (FileNameWithoutPath == NULL)
890 {
891 FileNameWithoutPath = ModuleName->Buffer;
892 }
893 else
894 {
895 FileNameWithoutPath++;
896 }
897
898 /*
899 * Strip the file extension from ServiceName
900 */
901 Success = RtlCreateUnicodeString(&ServiceName, FileNameWithoutPath);
902 if (!Success)
903 {
904 return STATUS_INSUFFICIENT_RESOURCES;
905 }
906
907 FileExtension = wcsrchr(ServiceName.Buffer, '.');
908 if (FileExtension != NULL)
909 {
910 ServiceName.Length -= (USHORT)wcslen(FileExtension) * sizeof(WCHAR);
911 FileExtension[0] = 0;
912 }
913
914 /*
915 * Determine the right device object
916 */
917 /* Use IopRootDeviceNode for now */
918 Status = IopCreateDeviceNode(IopRootDeviceNode,
919 NULL,
920 &ServiceName,
921 &DeviceNode);
922 if (!NT_SUCCESS(Status))
923 {
924 DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName, Status);
925 return(Status);
926 }
927
928 /* Lookup the new Ldr entry in PsLoadedModuleList */
929 NextEntry = PsLoadedModuleList.Flink;
930 while (NextEntry != &PsLoadedModuleList)
931 {
932 LdrEntry = CONTAINING_RECORD(NextEntry,
933 LDR_DATA_TABLE_ENTRY,
934 InLoadOrderLinks);
935 if (RtlEqualUnicodeString(ModuleName, &LdrEntry->BaseDllName, TRUE))
936 {
937 break;
938 }
939
940 NextEntry = NextEntry->Flink;
941 }
942 ASSERT(NextEntry != &PsLoadedModuleList);
943
944 /*
945 * Initialize the driver
946 */
947 Status = IopInitializeDriverModule(DeviceNode,
948 LdrEntry,
949 &DeviceNode->ServiceName,
950 FALSE,
951 &DriverObject);
952
953 if (!NT_SUCCESS(Status))
954 {
955 return Status;
956 }
957
958 Status = IopInitializeDevice(DeviceNode, DriverObject);
959 if (NT_SUCCESS(Status))
960 {
961 Status = IopStartDevice(DeviceNode);
962 }
963
964 /* Remove extra reference from IopInitializeDriverModule */
965 ObDereferenceObject(DriverObject);
966
967 return Status;
968 }
969
970 /*
971 * IopInitializeBootDrivers
972 *
973 * Initialize boot drivers and free memory for boot files.
974 *
975 * Parameters
976 * None
977 *
978 * Return Value
979 * None
980 */
981 VOID
982 FASTCALL
983 INIT_FUNCTION
984 IopInitializeBootDrivers(VOID)
985 {
986 PLIST_ENTRY ListHead, NextEntry, NextEntry2;
987 PLDR_DATA_TABLE_ENTRY LdrEntry;
988 PDEVICE_NODE DeviceNode;
989 PDRIVER_OBJECT DriverObject;
990 LDR_DATA_TABLE_ENTRY ModuleObject;
991 NTSTATUS Status;
992 UNICODE_STRING DriverName;
993 ULONG i, Index;
994 PDRIVER_INFORMATION DriverInfo, DriverInfoTag;
995 HANDLE KeyHandle;
996 PBOOT_DRIVER_LIST_ENTRY BootEntry;
997 DPRINT("IopInitializeBootDrivers()\n");
998
999 /* Use IopRootDeviceNode for now */
1000 Status = IopCreateDeviceNode(IopRootDeviceNode, NULL, NULL, &DeviceNode);
1001 if (!NT_SUCCESS(Status)) return;
1002
1003 /* Setup the module object for the RAW FS Driver */
1004 ModuleObject.DllBase = NULL;
1005 ModuleObject.SizeOfImage = 0;
1006 ModuleObject.EntryPoint = RawFsDriverEntry;
1007 RtlInitUnicodeString(&DriverName, L"RAW");
1008
1009 /* Initialize it */
1010 Status = IopInitializeDriverModule(DeviceNode,
1011 &ModuleObject,
1012 &DriverName,
1013 TRUE,
1014 &DriverObject);
1015 if (!NT_SUCCESS(Status))
1016 {
1017 /* Fail */
1018 return;
1019 }
1020
1021 /* Now initialize the associated device */
1022 Status = IopInitializeDevice(DeviceNode, DriverObject);
1023 if (!NT_SUCCESS(Status))
1024 {
1025 /* Fail */
1026 ObDereferenceObject(DriverObject);
1027 return;
1028 }
1029
1030 /* Start it up */
1031 Status = IopStartDevice(DeviceNode);
1032 if (!NT_SUCCESS(Status))
1033 {
1034 /* Fail */
1035 ObDereferenceObject(DriverObject);
1036 return;
1037 }
1038
1039 /* Get highest group order index */
1040 IopGroupIndex = PpInitGetGroupOrderIndex(NULL);
1041 if (IopGroupIndex == 0xFFFF) ASSERT(FALSE);
1042
1043 /* Allocate the group table */
1044 IopGroupTable = ExAllocatePoolWithTag(PagedPool,
1045 IopGroupIndex * sizeof(LIST_ENTRY),
1046 TAG_IO);
1047 if (IopGroupTable == NULL) ASSERT(FALSE);
1048
1049 /* Initialize the group table lists */
1050 for (i = 0; i < IopGroupIndex; i++) InitializeListHead(&IopGroupTable[i]);
1051
1052 /* Loop the boot modules */
1053 ListHead = &KeLoaderBlock->LoadOrderListHead;
1054 NextEntry = ListHead->Flink;
1055 while (ListHead != NextEntry)
1056 {
1057 /* Get the entry */
1058 LdrEntry = CONTAINING_RECORD(NextEntry,
1059 LDR_DATA_TABLE_ENTRY,
1060 InLoadOrderLinks);
1061
1062 /* Check if the DLL needs to be initialized */
1063 if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
1064 {
1065 /* Call its entrypoint */
1066 MmCallDllInitialize(LdrEntry, NULL);
1067 }
1068
1069 /* Go to the next driver */
1070 NextEntry = NextEntry->Flink;
1071 }
1072
1073 /* Loop the boot drivers */
1074 ListHead = &KeLoaderBlock->BootDriverListHead;
1075 NextEntry = ListHead->Flink;
1076 while (ListHead != NextEntry)
1077 {
1078 /* Get the entry */
1079 BootEntry = CONTAINING_RECORD(NextEntry,
1080 BOOT_DRIVER_LIST_ENTRY,
1081 Link);
1082
1083 /* Get the driver loader entry */
1084 LdrEntry = BootEntry->LdrEntry;
1085
1086 /* Allocate our internal accounting structure */
1087 DriverInfo = ExAllocatePoolWithTag(PagedPool,
1088 sizeof(DRIVER_INFORMATION),
1089 TAG_IO);
1090 if (DriverInfo)
1091 {
1092 /* Zero it and initialize it */
1093 RtlZeroMemory(DriverInfo, sizeof(DRIVER_INFORMATION));
1094 InitializeListHead(&DriverInfo->Link);
1095 DriverInfo->DataTableEntry = BootEntry;
1096
1097 /* Open the registry key */
1098 Status = IopOpenRegistryKeyEx(&KeyHandle,
1099 NULL,
1100 &BootEntry->RegistryPath,
1101 KEY_READ);
1102 if ((NT_SUCCESS(Status)) || /* ReactOS HACK for SETUPLDR */
1103 ((KeLoaderBlock->SetupLdrBlock) && ((KeyHandle = (PVOID)1)))) // yes, it's an assignment!
1104 {
1105 /* Save the handle */
1106 DriverInfo->ServiceHandle = KeyHandle;
1107
1108 /* Get the group oder index */
1109 Index = PpInitGetGroupOrderIndex(KeyHandle);
1110
1111 /* Get the tag position */
1112 DriverInfo->TagPosition = PipGetDriverTagPriority(KeyHandle);
1113
1114 /* Insert it into the list, at the right place */
1115 ASSERT(Index < IopGroupIndex);
1116 NextEntry2 = IopGroupTable[Index].Flink;
1117 while (NextEntry2 != &IopGroupTable[Index])
1118 {
1119 /* Get the driver info */
1120 DriverInfoTag = CONTAINING_RECORD(NextEntry2,
1121 DRIVER_INFORMATION,
1122 Link);
1123
1124 /* Check if we found the right tag position */
1125 if (DriverInfoTag->TagPosition > DriverInfo->TagPosition)
1126 {
1127 /* We're done */
1128 break;
1129 }
1130
1131 /* Next entry */
1132 NextEntry2 = NextEntry2->Flink;
1133 }
1134
1135 /* Insert us right before the next entry */
1136 NextEntry2 = NextEntry2->Blink;
1137 InsertHeadList(NextEntry2, &DriverInfo->Link);
1138 }
1139 }
1140
1141 /* Go to the next driver */
1142 NextEntry = NextEntry->Flink;
1143 }
1144
1145 /* Loop each group index */
1146 for (i = 0; i < IopGroupIndex; i++)
1147 {
1148 /* Loop each group table */
1149 NextEntry = IopGroupTable[i].Flink;
1150 while (NextEntry != &IopGroupTable[i])
1151 {
1152 /* Get the entry */
1153 DriverInfo = CONTAINING_RECORD(NextEntry,
1154 DRIVER_INFORMATION,
1155 Link);
1156
1157 /* Get the driver loader entry */
1158 LdrEntry = DriverInfo->DataTableEntry->LdrEntry;
1159
1160 /* Initialize it */
1161 IopInitializeBuiltinDriver(LdrEntry);
1162
1163 /* Next entry */
1164 NextEntry = NextEntry->Flink;
1165 }
1166 }
1167
1168 /* In old ROS, the loader list became empty after this point. Simulate. */
1169 InitializeListHead(&KeLoaderBlock->LoadOrderListHead);
1170 }
1171
1172 VOID
1173 FASTCALL
1174 INIT_FUNCTION
1175 IopInitializeSystemDrivers(VOID)
1176 {
1177 PUNICODE_STRING *DriverList, *SavedList;
1178
1179 /* No system drivers on the boot cd */
1180 if (KeLoaderBlock->SetupLdrBlock) return;
1181
1182 /* Get the driver list */
1183 SavedList = DriverList = CmGetSystemDriverList();
1184 ASSERT(DriverList);
1185
1186 /* Loop it */
1187 while (*DriverList)
1188 {
1189 /* Load the driver */
1190 ZwLoadDriver(*DriverList);
1191
1192 /* Free the entry */
1193 RtlFreeUnicodeString(*DriverList);
1194 ExFreePool(*DriverList);
1195
1196 /* Next entry */
1197 InbvIndicateProgress();
1198 DriverList++;
1199 }
1200
1201 /* Free the list */
1202 ExFreePool(SavedList);
1203 }
1204
1205 /*
1206 * IopUnloadDriver
1207 *
1208 * Unloads a device driver.
1209 *
1210 * Parameters
1211 * DriverServiceName
1212 * Name of the service to unload (registry key).
1213 *
1214 * UnloadPnpDrivers
1215 * Whether to unload Plug & Plug or only legacy drivers. If this
1216 * parameter is set to FALSE, the routine will unload only legacy
1217 * drivers.
1218 *
1219 * Return Value
1220 * Status
1221 *
1222 * To do
1223 * Guard the whole function by SEH.
1224 */
1225
1226 NTSTATUS NTAPI
1227 IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
1228 {
1229 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1230 UNICODE_STRING ImagePath;
1231 UNICODE_STRING ServiceName;
1232 UNICODE_STRING ObjectName;
1233 PDRIVER_OBJECT DriverObject;
1234 PDEVICE_OBJECT DeviceObject;
1235 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
1236 NTSTATUS Status;
1237 LPWSTR Start;
1238 BOOLEAN SafeToUnload = TRUE;
1239
1240 DPRINT("IopUnloadDriver('%wZ', %u)\n", DriverServiceName, UnloadPnpDrivers);
1241
1242 PAGED_CODE();
1243
1244 /*
1245 * Get the service name from the registry key name
1246 */
1247
1248 Start = wcsrchr(DriverServiceName->Buffer, L'\\');
1249 if (Start == NULL)
1250 Start = DriverServiceName->Buffer;
1251 else
1252 Start++;
1253
1254 RtlInitUnicodeString(&ServiceName, Start);
1255
1256 /*
1257 * Construct the driver object name
1258 */
1259
1260 ObjectName.Length = ((USHORT)wcslen(Start) + 8) * sizeof(WCHAR);
1261 ObjectName.MaximumLength = ObjectName.Length + sizeof(WCHAR);
1262 ObjectName.Buffer = ExAllocatePool(PagedPool, ObjectName.MaximumLength);
1263 if (!ObjectName.Buffer) return STATUS_INSUFFICIENT_RESOURCES;
1264 wcscpy(ObjectName.Buffer, DRIVER_ROOT_NAME);
1265 memcpy(ObjectName.Buffer + 8, Start, ObjectName.Length - 8 * sizeof(WCHAR));
1266 ObjectName.Buffer[ObjectName.Length/sizeof(WCHAR)] = 0;
1267
1268 /*
1269 * Find the driver object
1270 */
1271 Status = ObReferenceObjectByName(&ObjectName,
1272 0,
1273 0,
1274 0,
1275 IoDriverObjectType,
1276 KernelMode,
1277 0,
1278 (PVOID*)&DriverObject);
1279
1280 if (!NT_SUCCESS(Status))
1281 {
1282 DPRINT1("Can't locate driver object for %wZ\n", &ObjectName);
1283 ExFreePool(ObjectName.Buffer);
1284 return Status;
1285 }
1286
1287 /*
1288 * Free the buffer for driver object name
1289 */
1290 ExFreePool(ObjectName.Buffer);
1291
1292 /* Check that driver is not already unloading */
1293 if (DriverObject->Flags & DRVO_UNLOAD_INVOKED)
1294 {
1295 DPRINT1("Driver deletion pending\n");
1296 ObDereferenceObject(DriverObject);
1297 return STATUS_DELETE_PENDING;
1298 }
1299
1300 /*
1301 * Get path of service...
1302 */
1303 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1304
1305 RtlInitUnicodeString(&ImagePath, NULL);
1306
1307 QueryTable[0].Name = L"ImagePath";
1308 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1309 QueryTable[0].EntryContext = &ImagePath;
1310
1311 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1312 DriverServiceName->Buffer,
1313 QueryTable,
1314 NULL,
1315 NULL);
1316
1317 if (!NT_SUCCESS(Status))
1318 {
1319 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1320 ObDereferenceObject(DriverObject);
1321 return Status;
1322 }
1323
1324 /*
1325 * Normalize the image path for all later processing.
1326 */
1327 Status = IopNormalizeImagePath(&ImagePath, &ServiceName);
1328
1329 if (!NT_SUCCESS(Status))
1330 {
1331 DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status);
1332 ObDereferenceObject(DriverObject);
1333 return Status;
1334 }
1335
1336 /*
1337 * Free the service path
1338 */
1339 ExFreePool(ImagePath.Buffer);
1340
1341 /*
1342 * Unload the module and release the references to the device object
1343 */
1344
1345 /* Call the load/unload routine, depending on current process */
1346 if (DriverObject->DriverUnload && DriverObject->DriverSection &&
1347 (UnloadPnpDrivers || (DriverObject->Flags & DRVO_LEGACY_DRIVER)))
1348 {
1349 /* Loop through each device object of the driver
1350 and set DOE_UNLOAD_PENDING flag */
1351 DeviceObject = DriverObject->DeviceObject;
1352 while (DeviceObject)
1353 {
1354 /* Set the unload pending flag for the device */
1355 DeviceExtension = IoGetDevObjExtension(DeviceObject);
1356 DeviceExtension->ExtensionFlags |= DOE_UNLOAD_PENDING;
1357
1358 /* Make sure there are no attached devices or no reference counts */
1359 if ((DeviceObject->ReferenceCount) || (DeviceObject->AttachedDevice))
1360 {
1361 /* Not safe to unload */
1362 DPRINT1("Drivers device object is referenced or has attached devices\n");
1363
1364 SafeToUnload = FALSE;
1365 }
1366
1367 DeviceObject = DeviceObject->NextDevice;
1368 }
1369
1370 /* If not safe to unload, then return success */
1371 if (!SafeToUnload)
1372 {
1373 ObDereferenceObject(DriverObject);
1374 return STATUS_SUCCESS;
1375 }
1376
1377 DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject->DriverName);
1378
1379 /* Set the unload invoked flag and call the unload routine */
1380 DriverObject->Flags |= DRVO_UNLOAD_INVOKED;
1381 Status = IopLoadUnloadDriver(NULL, &DriverObject);
1382 ASSERT(Status == STATUS_SUCCESS);
1383
1384 /* Mark the driver object temporary, so it could be deleted later */
1385 ObMakeTemporaryObject(DriverObject);
1386
1387 /* Dereference it 2 times */
1388 ObDereferenceObject(DriverObject);
1389 ObDereferenceObject(DriverObject);
1390
1391 return Status;
1392 }
1393 else
1394 {
1395 DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject->DriverName);
1396
1397 /* Dereference one time (refd inside this function) */
1398 ObDereferenceObject(DriverObject);
1399
1400 /* Return unloading failure */
1401 return STATUS_INVALID_DEVICE_REQUEST;
1402 }
1403 }
1404
1405 VOID
1406 NTAPI
1407 IopReinitializeDrivers(VOID)
1408 {
1409 PDRIVER_REINIT_ITEM ReinitItem;
1410 PLIST_ENTRY Entry;
1411
1412 /* Get the first entry and start looping */
1413 Entry = ExInterlockedRemoveHeadList(&DriverReinitListHead,
1414 &DriverReinitListLock);
1415 while (Entry)
1416 {
1417 /* Get the item*/
1418 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1419
1420 /* Increment reinitialization counter */
1421 ReinitItem->DriverObject->DriverExtension->Count++;
1422
1423 /* Remove the device object flag */
1424 ReinitItem->DriverObject->Flags &= ~DRVO_REINIT_REGISTERED;
1425
1426 /* Call the routine */
1427 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1428 ReinitItem->Context,
1429 ReinitItem->DriverObject->
1430 DriverExtension->Count);
1431
1432 /* Free the entry */
1433 ExFreePool(Entry);
1434
1435 /* Move to the next one */
1436 Entry = ExInterlockedRemoveHeadList(&DriverReinitListHead,
1437 &DriverReinitListLock);
1438 }
1439 }
1440
1441 VOID
1442 NTAPI
1443 IopReinitializeBootDrivers(VOID)
1444 {
1445 PDRIVER_REINIT_ITEM ReinitItem;
1446 PLIST_ENTRY Entry;
1447
1448 /* Get the first entry and start looping */
1449 Entry = ExInterlockedRemoveHeadList(&DriverBootReinitListHead,
1450 &DriverBootReinitListLock);
1451 while (Entry)
1452 {
1453 /* Get the item*/
1454 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1455
1456 /* Increment reinitialization counter */
1457 ReinitItem->DriverObject->DriverExtension->Count++;
1458
1459 /* Remove the device object flag */
1460 ReinitItem->DriverObject->Flags &= ~DRVO_BOOTREINIT_REGISTERED;
1461
1462 /* Call the routine */
1463 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1464 ReinitItem->Context,
1465 ReinitItem->DriverObject->
1466 DriverExtension->Count);
1467
1468 /* Free the entry */
1469 ExFreePool(Entry);
1470
1471 /* Move to the next one */
1472 Entry = ExInterlockedRemoveHeadList(&DriverBootReinitListHead,
1473 &DriverBootReinitListLock);
1474 }
1475 }
1476
1477 NTSTATUS
1478 NTAPI
1479 IopCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL,
1480 IN PDRIVER_INITIALIZE InitializationFunction,
1481 IN PUNICODE_STRING RegistryPath,
1482 IN PCUNICODE_STRING ServiceName,
1483 PLDR_DATA_TABLE_ENTRY ModuleObject,
1484 OUT PDRIVER_OBJECT *pDriverObject)
1485 {
1486 WCHAR NameBuffer[100];
1487 USHORT NameLength;
1488 UNICODE_STRING LocalDriverName;
1489 NTSTATUS Status;
1490 OBJECT_ATTRIBUTES ObjectAttributes;
1491 ULONG ObjectSize;
1492 PDRIVER_OBJECT DriverObject;
1493 UNICODE_STRING ServiceKeyName;
1494 HANDLE hDriver;
1495 ULONG i, RetryCount = 0;
1496
1497 try_again:
1498 /* First, create a unique name for the driver if we don't have one */
1499 if (!DriverName)
1500 {
1501 /* Create a random name and set up the string*/
1502 NameLength = (USHORT)swprintf(NameBuffer,
1503 DRIVER_ROOT_NAME L"%08u",
1504 KeTickCount.LowPart);
1505 LocalDriverName.Length = NameLength * sizeof(WCHAR);
1506 LocalDriverName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1507 LocalDriverName.Buffer = NameBuffer;
1508 }
1509 else
1510 {
1511 /* So we can avoid another code path, use a local var */
1512 LocalDriverName = *DriverName;
1513 }
1514
1515 /* Initialize the Attributes */
1516 ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
1517 InitializeObjectAttributes(&ObjectAttributes,
1518 &LocalDriverName,
1519 OBJ_PERMANENT | OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
1520 NULL,
1521 NULL);
1522
1523 /* Create the Object */
1524 Status = ObCreateObject(KernelMode,
1525 IoDriverObjectType,
1526 &ObjectAttributes,
1527 KernelMode,
1528 NULL,
1529 ObjectSize,
1530 0,
1531 0,
1532 (PVOID*)&DriverObject);
1533 if (!NT_SUCCESS(Status)) return Status;
1534
1535 DPRINT("IopCreateDriver(): created DO %p\n", DriverObject);
1536
1537 /* Set up the Object */
1538 RtlZeroMemory(DriverObject, ObjectSize);
1539 DriverObject->Type = IO_TYPE_DRIVER;
1540 DriverObject->Size = sizeof(DRIVER_OBJECT);
1541 DriverObject->Flags = DRVO_LEGACY_DRIVER;
1542 DriverObject->DriverExtension = (PDRIVER_EXTENSION)(DriverObject + 1);
1543 DriverObject->DriverExtension->DriverObject = DriverObject;
1544 DriverObject->DriverInit = InitializationFunction;
1545 DriverObject->DriverSection = ModuleObject;
1546 /* Loop all Major Functions */
1547 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1548 {
1549 /* Invalidate each function */
1550 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1551 }
1552
1553 /* Set up the service key name buffer */
1554 ServiceKeyName.MaximumLength = ServiceName->Length + sizeof(UNICODE_NULL);
1555 ServiceKeyName.Buffer = ExAllocatePoolWithTag(NonPagedPool,
1556 ServiceKeyName.MaximumLength,
1557 TAG_IO);
1558 if (!ServiceKeyName.Buffer)
1559 {
1560 /* Fail */
1561 ObMakeTemporaryObject(DriverObject);
1562 ObDereferenceObject(DriverObject);
1563 return STATUS_INSUFFICIENT_RESOURCES;
1564 }
1565
1566 /* Copy the name and set it in the driver extension */
1567 RtlCopyUnicodeString(&ServiceKeyName,
1568 ServiceName);
1569 DriverObject->DriverExtension->ServiceKeyName = ServiceKeyName;
1570
1571 /* Make a copy of the driver name to store in the driver object */
1572 DriverObject->DriverName.MaximumLength = LocalDriverName.Length;
1573 DriverObject->DriverName.Buffer = ExAllocatePoolWithTag(PagedPool,
1574 DriverObject->DriverName.MaximumLength,
1575 TAG_IO);
1576 if (!DriverObject->DriverName.Buffer)
1577 {
1578 /* Fail */
1579 ObMakeTemporaryObject(DriverObject);
1580 ObDereferenceObject(DriverObject);
1581 return STATUS_INSUFFICIENT_RESOURCES;
1582 }
1583
1584 RtlCopyUnicodeString(&DriverObject->DriverName,
1585 &LocalDriverName);
1586
1587 /* Add the Object and get its handle */
1588 Status = ObInsertObject(DriverObject,
1589 NULL,
1590 FILE_READ_DATA,
1591 0,
1592 NULL,
1593 &hDriver);
1594
1595 /* Eliminate small possibility when this function is called more than
1596 once in a row, and KeTickCount doesn't get enough time to change */
1597 if (!DriverName && (Status == STATUS_OBJECT_NAME_COLLISION) && (RetryCount < 100))
1598 {
1599 RetryCount++;
1600 goto try_again;
1601 }
1602
1603 if (!NT_SUCCESS(Status)) return Status;
1604
1605 /* Now reference it */
1606 Status = ObReferenceObjectByHandle(hDriver,
1607 0,
1608 IoDriverObjectType,
1609 KernelMode,
1610 (PVOID*)&DriverObject,
1611 NULL);
1612
1613 /* Close the extra handle */
1614 ZwClose(hDriver);
1615
1616 if (!NT_SUCCESS(Status))
1617 {
1618 /* Fail */
1619 ObMakeTemporaryObject(DriverObject);
1620 ObDereferenceObject(DriverObject);
1621 return Status;
1622 }
1623
1624 DriverObject->HardwareDatabase = &IopHardwareDatabaseKey;
1625 DriverObject->DriverStart = ModuleObject ? ModuleObject->DllBase : 0;
1626 DriverObject->DriverSize = ModuleObject ? ModuleObject->SizeOfImage : 0;
1627
1628 /* Finally, call its init function */
1629 DPRINT("RegistryKey: %wZ\n", RegistryPath);
1630 DPRINT("Calling driver entrypoint at %p\n", InitializationFunction);
1631 Status = (*InitializationFunction)(DriverObject, RegistryPath);
1632 if (!NT_SUCCESS(Status))
1633 {
1634 /* If it didn't work, then kill the object */
1635 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", DriverName, Status);
1636 DriverObject->DriverSection = NULL;
1637 ObMakeTemporaryObject(DriverObject);
1638 ObDereferenceObject(DriverObject);
1639 return Status;
1640 }
1641 else
1642 {
1643 /* Returns to caller the object */
1644 *pDriverObject = DriverObject;
1645 }
1646
1647 /* We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
1648 * Other parts of the I/O manager depend on this behavior */
1649 if (!DriverObject->DeviceObject) DriverObject->Flags &= ~DRVO_LEGACY_DRIVER;
1650
1651 /* Loop all Major Functions */
1652 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1653 {
1654 /*
1655 * Make sure the driver didn't set any dispatch entry point to NULL!
1656 * Doing so is illegal; drivers shouldn't touch entry points they
1657 * do not implement.
1658 */
1659
1660 /* Check if it did so anyway */
1661 if (!DriverObject->MajorFunction[i])
1662 {
1663 /* Print a warning in the debug log */
1664 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1665 &DriverObject->DriverName, i);
1666
1667 /* Fix it up */
1668 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1669 }
1670 }
1671
1672 /* Return the Status */
1673 return Status;
1674 }
1675
1676 /* PUBLIC FUNCTIONS ***********************************************************/
1677
1678 /*
1679 * @implemented
1680 */
1681 NTSTATUS
1682 NTAPI
1683 IoCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL,
1684 IN PDRIVER_INITIALIZE InitializationFunction)
1685 {
1686 PDRIVER_OBJECT DriverObject;
1687 return IopCreateDriver(DriverName, InitializationFunction, NULL, DriverName, NULL, &DriverObject);
1688 }
1689
1690 /*
1691 * @implemented
1692 */
1693 VOID
1694 NTAPI
1695 IoDeleteDriver(IN PDRIVER_OBJECT DriverObject)
1696 {
1697 /* Simply dereference the Object */
1698 ObDereferenceObject(DriverObject);
1699 }
1700
1701 /*
1702 * @implemented
1703 */
1704 VOID
1705 NTAPI
1706 IoRegisterBootDriverReinitialization(IN PDRIVER_OBJECT DriverObject,
1707 IN PDRIVER_REINITIALIZE ReinitRoutine,
1708 IN PVOID Context)
1709 {
1710 PDRIVER_REINIT_ITEM ReinitItem;
1711
1712 /* Allocate the entry */
1713 ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1714 sizeof(DRIVER_REINIT_ITEM),
1715 TAG_REINIT);
1716 if (!ReinitItem) return;
1717
1718 /* Fill it out */
1719 ReinitItem->DriverObject = DriverObject;
1720 ReinitItem->ReinitRoutine = ReinitRoutine;
1721 ReinitItem->Context = Context;
1722
1723 /* Set the Driver Object flag and insert the entry into the list */
1724 DriverObject->Flags |= DRVO_BOOTREINIT_REGISTERED;
1725 ExInterlockedInsertTailList(&DriverBootReinitListHead,
1726 &ReinitItem->ItemEntry,
1727 &DriverBootReinitListLock);
1728 }
1729
1730 /*
1731 * @implemented
1732 */
1733 VOID
1734 NTAPI
1735 IoRegisterDriverReinitialization(IN PDRIVER_OBJECT DriverObject,
1736 IN PDRIVER_REINITIALIZE ReinitRoutine,
1737 IN PVOID Context)
1738 {
1739 PDRIVER_REINIT_ITEM ReinitItem;
1740
1741 /* Allocate the entry */
1742 ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1743 sizeof(DRIVER_REINIT_ITEM),
1744 TAG_REINIT);
1745 if (!ReinitItem) return;
1746
1747 /* Fill it out */
1748 ReinitItem->DriverObject = DriverObject;
1749 ReinitItem->ReinitRoutine = ReinitRoutine;
1750 ReinitItem->Context = Context;
1751
1752 /* Set the Driver Object flag and insert the entry into the list */
1753 DriverObject->Flags |= DRVO_REINIT_REGISTERED;
1754 ExInterlockedInsertTailList(&DriverReinitListHead,
1755 &ReinitItem->ItemEntry,
1756 &DriverReinitListLock);
1757 }
1758
1759 /*
1760 * @implemented
1761 */
1762 NTSTATUS
1763 NTAPI
1764 IoAllocateDriverObjectExtension(IN PDRIVER_OBJECT DriverObject,
1765 IN PVOID ClientIdentificationAddress,
1766 IN ULONG DriverObjectExtensionSize,
1767 OUT PVOID *DriverObjectExtension)
1768 {
1769 KIRQL OldIrql;
1770 PIO_CLIENT_EXTENSION DriverExtensions, NewDriverExtension;
1771 BOOLEAN Inserted = FALSE;
1772
1773 /* Assume failure */
1774 *DriverObjectExtension = NULL;
1775
1776 /* Allocate the extension */
1777 NewDriverExtension = ExAllocatePoolWithTag(NonPagedPool,
1778 sizeof(IO_CLIENT_EXTENSION) +
1779 DriverObjectExtensionSize,
1780 TAG_DRIVER_EXTENSION);
1781 if (!NewDriverExtension) return STATUS_INSUFFICIENT_RESOURCES;
1782
1783 /* Clear the extension for teh caller */
1784 RtlZeroMemory(NewDriverExtension,
1785 sizeof(IO_CLIENT_EXTENSION) + DriverObjectExtensionSize);
1786
1787 /* Acqure lock */
1788 OldIrql = KeRaiseIrqlToDpcLevel();
1789
1790 /* Fill out the extension */
1791 NewDriverExtension->ClientIdentificationAddress = ClientIdentificationAddress;
1792
1793 /* Loop the current extensions */
1794 DriverExtensions = IoGetDrvObjExtension(DriverObject)->
1795 ClientDriverExtension;
1796 while (DriverExtensions)
1797 {
1798 /* Check if the identifier matches */
1799 if (DriverExtensions->ClientIdentificationAddress ==
1800 ClientIdentificationAddress)
1801 {
1802 /* We have a collision, break out */
1803 break;
1804 }
1805
1806 /* Go to the next one */
1807 DriverExtensions = DriverExtensions->NextExtension;
1808 }
1809
1810 /* Check if we didn't collide */
1811 if (!DriverExtensions)
1812 {
1813 /* Link this one in */
1814 NewDriverExtension->NextExtension =
1815 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1816 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension =
1817 NewDriverExtension;
1818 Inserted = TRUE;
1819 }
1820
1821 /* Release the lock */
1822 KeLowerIrql(OldIrql);
1823
1824 /* Check if insertion failed */
1825 if (!Inserted)
1826 {
1827 /* Free the entry and fail */
1828 ExFreePoolWithTag(NewDriverExtension, TAG_DRIVER_EXTENSION);
1829 return STATUS_OBJECT_NAME_COLLISION;
1830 }
1831
1832 /* Otherwise, return the pointer */
1833 *DriverObjectExtension = NewDriverExtension + 1;
1834 return STATUS_SUCCESS;
1835 }
1836
1837 /*
1838 * @implemented
1839 */
1840 PVOID
1841 NTAPI
1842 IoGetDriverObjectExtension(IN PDRIVER_OBJECT DriverObject,
1843 IN PVOID ClientIdentificationAddress)
1844 {
1845 KIRQL OldIrql;
1846 PIO_CLIENT_EXTENSION DriverExtensions;
1847
1848 /* Acquire lock */
1849 OldIrql = KeRaiseIrqlToDpcLevel();
1850
1851 /* Loop the list until we find the right one */
1852 DriverExtensions = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1853 while (DriverExtensions)
1854 {
1855 /* Check for a match */
1856 if (DriverExtensions->ClientIdentificationAddress ==
1857 ClientIdentificationAddress)
1858 {
1859 /* Break out */
1860 break;
1861 }
1862
1863 /* Keep looping */
1864 DriverExtensions = DriverExtensions->NextExtension;
1865 }
1866
1867 /* Release lock */
1868 KeLowerIrql(OldIrql);
1869
1870 /* Return nothing or the extension */
1871 if (!DriverExtensions) return NULL;
1872 return DriverExtensions + 1;
1873 }
1874
1875 VOID
1876 NTAPI
1877 IopLoadUnloadDriverWorker(
1878 _Inout_ PVOID Parameter)
1879 {
1880 PLOAD_UNLOAD_PARAMS LoadParams = Parameter;
1881
1882 ASSERT(PsGetCurrentProcess() == PsInitialSystemProcess);
1883 LoadParams->Status = IopLoadUnloadDriver(LoadParams->RegistryPath,
1884 &LoadParams->DriverObject);
1885 KeSetEvent(&LoadParams->Event, 0, FALSE);
1886 }
1887
1888 NTSTATUS
1889 NTAPI
1890 IopLoadUnloadDriver(
1891 _In_opt_ PCUNICODE_STRING RegistryPath,
1892 _Inout_ PDRIVER_OBJECT *DriverObject)
1893 {
1894 RTL_QUERY_REGISTRY_TABLE QueryTable[3];
1895 UNICODE_STRING ImagePath;
1896 UNICODE_STRING ServiceName;
1897 NTSTATUS Status;
1898 ULONG Type;
1899 PDEVICE_NODE DeviceNode;
1900 PLDR_DATA_TABLE_ENTRY ModuleObject;
1901 PVOID BaseAddress;
1902 WCHAR *cur;
1903
1904 /* Load/Unload must be called from system process */
1905 if (PsGetCurrentProcess() != PsInitialSystemProcess)
1906 {
1907 LOAD_UNLOAD_PARAMS LoadParams;
1908
1909 /* Prepare parameters block */
1910 LoadParams.RegistryPath = RegistryPath;
1911 LoadParams.DriverObject = *DriverObject;
1912 KeInitializeEvent(&LoadParams.Event, NotificationEvent, FALSE);
1913
1914 /* Initialize and queue a work item */
1915 ExInitializeWorkItem(&LoadParams.WorkItem,
1916 IopLoadUnloadDriverWorker,
1917 &LoadParams);
1918 ExQueueWorkItem(&LoadParams.WorkItem, DelayedWorkQueue);
1919
1920 /* And wait till it completes */
1921 KeWaitForSingleObject(&LoadParams.Event,
1922 UserRequest,
1923 KernelMode,
1924 FALSE,
1925 NULL);
1926 return LoadParams.Status;
1927 }
1928
1929 /* Check if it's an unload request */
1930 if (*DriverObject)
1931 {
1932 (*DriverObject)->DriverUnload(*DriverObject);
1933 return STATUS_SUCCESS;
1934 }
1935
1936 RtlInitUnicodeString(&ImagePath, NULL);
1937
1938 /*
1939 * Get the service name from the registry key name.
1940 */
1941 ASSERT(RegistryPath->Length >= sizeof(WCHAR));
1942
1943 ServiceName = *RegistryPath;
1944 cur = RegistryPath->Buffer + RegistryPath->Length / sizeof(WCHAR) - 1;
1945 while (RegistryPath->Buffer != cur)
1946 {
1947 if (*cur == L'\\')
1948 {
1949 ServiceName.Buffer = cur + 1;
1950 ServiceName.Length = RegistryPath->Length -
1951 (USHORT)((ULONG_PTR)ServiceName.Buffer -
1952 (ULONG_PTR)RegistryPath->Buffer);
1953 break;
1954 }
1955 cur--;
1956 }
1957
1958 /*
1959 * Get service type.
1960 */
1961 RtlZeroMemory(&QueryTable, sizeof(QueryTable));
1962
1963 RtlInitUnicodeString(&ImagePath, NULL);
1964
1965 QueryTable[0].Name = L"Type";
1966 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_QUERY_REGISTRY_REQUIRED;
1967 QueryTable[0].EntryContext = &Type;
1968
1969 QueryTable[1].Name = L"ImagePath";
1970 QueryTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
1971 QueryTable[1].EntryContext = &ImagePath;
1972
1973 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1974 RegistryPath->Buffer,
1975 QueryTable, NULL, NULL);
1976 if (!NT_SUCCESS(Status))
1977 {
1978 DPRINT("RtlQueryRegistryValues() failed (Status %lx)\n", Status);
1979 if (ImagePath.Buffer) ExFreePool(ImagePath.Buffer);
1980 return Status;
1981 }
1982
1983 /*
1984 * Normalize the image path for all later processing.
1985 */
1986 Status = IopNormalizeImagePath(&ImagePath, &ServiceName);
1987 if (!NT_SUCCESS(Status))
1988 {
1989 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
1990 return Status;
1991 }
1992
1993 DPRINT("FullImagePath: '%wZ'\n", &ImagePath);
1994 DPRINT("Type: %lx\n", Type);
1995
1996 KeEnterCriticalRegion();
1997 ExAcquireResourceExclusiveLite(&IopDriverLoadResource, TRUE);
1998 /*
1999 * Get existing DriverObject pointer (in case the driver
2000 * has already been loaded and initialized).
2001 */
2002 Status = IopGetDriverObject(DriverObject,
2003 &ServiceName,
2004 (Type == 2 /* SERVICE_FILE_SYSTEM_DRIVER */ ||
2005 Type == 8 /* SERVICE_RECOGNIZER_DRIVER */));
2006
2007 if (!NT_SUCCESS(Status))
2008 {
2009 /*
2010 * Load the driver module
2011 */
2012 DPRINT("Loading module from %wZ\n", &ImagePath);
2013 Status = MmLoadSystemImage(&ImagePath, NULL, NULL, 0, (PVOID)&ModuleObject, &BaseAddress);
2014 if (!NT_SUCCESS(Status))
2015 {
2016 DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status);
2017 ExReleaseResourceLite(&IopDriverLoadResource);
2018 KeLeaveCriticalRegion();
2019 return Status;
2020 }
2021
2022 /*
2023 * Initialize the driver module if it's loaded for the first time
2024 */
2025 Status = IopCreateDeviceNode(IopRootDeviceNode, NULL, &ServiceName, &DeviceNode);
2026 if (!NT_SUCCESS(Status))
2027 {
2028 DPRINT1("IopCreateDeviceNode() failed (Status %lx)\n", Status);
2029 ExReleaseResourceLite(&IopDriverLoadResource);
2030 KeLeaveCriticalRegion();
2031 MmUnloadSystemImage(ModuleObject);
2032 return Status;
2033 }
2034
2035 IopDisplayLoadingMessage(&DeviceNode->ServiceName);
2036
2037 Status = IopInitializeDriverModule(DeviceNode,
2038 ModuleObject,
2039 &DeviceNode->ServiceName,
2040 (Type == 2 /* SERVICE_FILE_SYSTEM_DRIVER */ ||
2041 Type == 8 /* SERVICE_RECOGNIZER_DRIVER */),
2042 DriverObject);
2043 if (!NT_SUCCESS(Status))
2044 {
2045 DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status);
2046 ExReleaseResourceLite(&IopDriverLoadResource);
2047 KeLeaveCriticalRegion();
2048 MmUnloadSystemImage(ModuleObject);
2049 return Status;
2050 }
2051
2052 ExReleaseResourceLite(&IopDriverLoadResource);
2053 KeLeaveCriticalRegion();
2054
2055 /* Initialize and start device */
2056 IopInitializeDevice(DeviceNode, *DriverObject);
2057 Status = IopStartDevice(DeviceNode);
2058 }
2059 else
2060 {
2061 ExReleaseResourceLite(&IopDriverLoadResource);
2062 KeLeaveCriticalRegion();
2063
2064 DPRINT("DriverObject already exist in ObjectManager\n");
2065 Status = STATUS_IMAGE_ALREADY_LOADED;
2066
2067 /* IopGetDriverObject references the DriverObject, so dereference it */
2068 ObDereferenceObject(*DriverObject);
2069 }
2070
2071 return Status;
2072 }
2073
2074 /*
2075 * NtLoadDriver
2076 *
2077 * Loads a device driver.
2078 *
2079 * Parameters
2080 * DriverServiceName
2081 * Name of the service to load (registry key).
2082 *
2083 * Return Value
2084 * Status
2085 *
2086 * Status
2087 * implemented
2088 */
2089 NTSTATUS NTAPI
2090 NtLoadDriver(IN PUNICODE_STRING DriverServiceName)
2091 {
2092 UNICODE_STRING CapturedDriverServiceName = { 0, 0, NULL };
2093 KPROCESSOR_MODE PreviousMode;
2094 PDRIVER_OBJECT DriverObject;
2095 NTSTATUS Status;
2096
2097 PAGED_CODE();
2098
2099 PreviousMode = KeGetPreviousMode();
2100
2101 /*
2102 * Check security privileges
2103 */
2104
2105 /* FIXME: Uncomment when privileges will be correctly implemented. */
2106 #if 0
2107 if (!SeSinglePrivilegeCheck(SeLoadDriverPrivilege, PreviousMode))
2108 {
2109 DPRINT("Privilege not held\n");
2110 return STATUS_PRIVILEGE_NOT_HELD;
2111 }
2112 #endif
2113
2114 Status = ProbeAndCaptureUnicodeString(&CapturedDriverServiceName,
2115 PreviousMode,
2116 DriverServiceName);
2117 if (!NT_SUCCESS(Status))
2118 {
2119 return Status;
2120 }
2121
2122 DPRINT("NtLoadDriver('%wZ')\n", &CapturedDriverServiceName);
2123
2124 /* Load driver and call its entry point */
2125 DriverObject = NULL;
2126 Status = IopLoadUnloadDriver(&CapturedDriverServiceName, &DriverObject);
2127
2128 ReleaseCapturedUnicodeString(&CapturedDriverServiceName,
2129 PreviousMode);
2130
2131 return Status;
2132 }
2133
2134 /*
2135 * NtUnloadDriver
2136 *
2137 * Unloads a legacy device driver.
2138 *
2139 * Parameters
2140 * DriverServiceName
2141 * Name of the service to unload (registry key).
2142 *
2143 * Return Value
2144 * Status
2145 *
2146 * Status
2147 * implemented
2148 */
2149
2150 NTSTATUS NTAPI
2151 NtUnloadDriver(IN PUNICODE_STRING DriverServiceName)
2152 {
2153 return IopUnloadDriver(DriverServiceName, FALSE);
2154 }
2155
2156 /* EOF */