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)
11 /* INCLUDES *******************************************************************/
17 /* GLOBALS ********************************************************************/
19 LIST_ENTRY DriverReinitListHead
;
20 KSPIN_LOCK DriverReinitListLock
;
21 PLIST_ENTRY DriverReinitTailEntry
;
23 PLIST_ENTRY DriverBootReinitTailEntry
;
24 LIST_ENTRY DriverBootReinitListHead
;
25 KSPIN_LOCK DriverBootReinitListLock
;
27 UNICODE_STRING IopHardwareDatabaseKey
=
28 RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\HARDWARE\\DESCRIPTION\\SYSTEM");
30 POBJECT_TYPE IoDriverObjectType
= NULL
;
32 #define TAG_RTLREGISTRY 'vrqR'
34 extern BOOLEAN ExpInTextModeSetup
;
35 extern BOOLEAN PnpSystemInit
;
38 PLIST_ENTRY IopGroupTable
;
40 /* PRIVATE FUNCTIONS **********************************************************/
43 IopInvalidDeviceRequest(
44 PDEVICE_OBJECT DeviceObject
,
47 Irp
->IoStatus
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
48 Irp
->IoStatus
.Information
= 0;
49 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
50 return STATUS_INVALID_DEVICE_REQUEST
;
55 IopDeleteDriver(IN PVOID ObjectBody
)
57 PDRIVER_OBJECT DriverObject
= ObjectBody
;
58 PIO_CLIENT_EXTENSION DriverExtension
, NextDriverExtension
;
61 DPRINT1("Deleting driver object '%wZ'\n", &DriverObject
->DriverName
);
63 /* There must be no device objects remaining at this point */
64 ASSERT(!DriverObject
->DeviceObject
);
66 /* Get the extension and loop them */
67 DriverExtension
= IoGetDrvObjExtension(DriverObject
)->
68 ClientDriverExtension
;
69 while (DriverExtension
)
71 /* Get the next one */
72 NextDriverExtension
= DriverExtension
->NextExtension
;
73 ExFreePoolWithTag(DriverExtension
, TAG_DRIVER_EXTENSION
);
76 DriverExtension
= NextDriverExtension
;
79 /* Check if the driver image is still loaded */
80 if (DriverObject
->DriverSection
)
83 MmUnloadSystemImage(DriverObject
->DriverSection
);
86 /* Check if it has a name */
87 if (DriverObject
->DriverName
.Buffer
)
90 ExFreePool(DriverObject
->DriverName
.Buffer
);
93 #if 0 /* See a bit of hack in IopCreateDriver */
94 /* Check if it has a service key name */
95 if (DriverObject
->DriverExtension
->ServiceKeyName
.Buffer
)
98 ExFreePool(DriverObject
->DriverExtension
->ServiceKeyName
.Buffer
);
105 PDRIVER_OBJECT
*DriverObject
,
106 PUNICODE_STRING ServiceName
,
109 PDRIVER_OBJECT Object
;
110 WCHAR NameBuffer
[MAX_PATH
];
111 UNICODE_STRING DriverName
;
114 DPRINT("IopGetDriverObject(%p '%wZ' %x)\n",
115 DriverObject
, ServiceName
, FileSystem
);
117 *DriverObject
= NULL
;
119 /* Create ModuleName string */
120 if (ServiceName
== NULL
|| ServiceName
->Buffer
== NULL
)
121 /* We don't know which DriverObject we have to open */
122 return STATUS_INVALID_PARAMETER_2
;
124 DriverName
.Buffer
= NameBuffer
;
125 DriverName
.Length
= 0;
126 DriverName
.MaximumLength
= sizeof(NameBuffer
);
128 if (FileSystem
== TRUE
)
129 RtlAppendUnicodeToString(&DriverName
, FILESYSTEM_ROOT_NAME
);
131 RtlAppendUnicodeToString(&DriverName
, DRIVER_ROOT_NAME
);
132 RtlAppendUnicodeStringToString(&DriverName
, ServiceName
);
134 DPRINT("Driver name: '%wZ'\n", &DriverName
);
136 /* Open driver object */
137 Status
= ObReferenceObjectByName(
139 OBJ_OPENIF
| OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, /* Attributes */
140 NULL
, /* PassedAccessState */
141 0, /* DesiredAccess */
144 NULL
, /* ParseContext */
147 if (!NT_SUCCESS(Status
))
149 DPRINT("Failed to reference driver object, status=0x%08x\n", Status
);
153 *DriverObject
= Object
;
155 DPRINT("Driver Object: %p\n", Object
);
157 return STATUS_SUCCESS
;
162 * TRUE if String2 contains String1 as a suffix.
166 IopSuffixUnicodeString(
167 IN PCUNICODE_STRING String1
,
168 IN PCUNICODE_STRING String2
)
174 if (String2
->Length
< String1
->Length
)
177 Length
= String1
->Length
/ 2;
178 pc1
= String1
->Buffer
;
179 pc2
= &String2
->Buffer
[String2
->Length
/ sizeof(WCHAR
) - Length
];
185 if( *pc1
++ != *pc2
++ )
194 * IopDisplayLoadingMessage
196 * Display 'Loading XXX...' message.
202 IopDisplayLoadingMessage(PUNICODE_STRING ServiceName
)
204 CHAR TextBuffer
[256];
205 UNICODE_STRING DotSys
= RTL_CONSTANT_STRING(L
".SYS");
207 if (ExpInTextModeSetup
) return;
208 if (!KeLoaderBlock
) return;
209 RtlUpcaseUnicodeString(ServiceName
, ServiceName
, FALSE
);
210 snprintf(TextBuffer
, sizeof(TextBuffer
),
211 "%s%sSystem32\\Drivers\\%wZ%s\r\n",
212 KeLoaderBlock
->ArcBootDeviceName
,
213 KeLoaderBlock
->NtBootPathName
,
215 IopSuffixUnicodeString(&DotSys
, ServiceName
) ? "" : ".SYS");
216 HalDisplayString(TextBuffer
);
220 * IopNormalizeImagePath
222 * Normalize an image path to contain complete path.
226 * The input path and on exit the result path. ImagePath.Buffer
227 * must be allocated by ExAllocatePool on input. Caller is responsible
228 * for freeing the buffer when it's no longer needed.
231 * Name of the service that ImagePath belongs to.
237 * The input image path isn't freed on error.
242 IopNormalizeImagePath(
243 _Inout_
_When_(return>=0, _At_(ImagePath
->Buffer
, _Post_notnull_
__drv_allocatesMem(Mem
)))
244 PUNICODE_STRING ImagePath
,
245 _In_ PUNICODE_STRING ServiceName
)
247 UNICODE_STRING InputImagePath
;
249 DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath
, ServiceName
);
254 sizeof(UNICODE_STRING
));
256 if (InputImagePath
.Length
== 0)
258 ImagePath
->Length
= 0;
259 ImagePath
->MaximumLength
=
260 (33 * sizeof(WCHAR
)) + ServiceName
->Length
+ sizeof(UNICODE_NULL
);
261 ImagePath
->Buffer
= ExAllocatePool(NonPagedPool
, ImagePath
->MaximumLength
);
262 if (ImagePath
->Buffer
== NULL
)
263 return STATUS_NO_MEMORY
;
265 RtlAppendUnicodeToString(ImagePath
, L
"\\SystemRoot\\system32\\drivers\\");
266 RtlAppendUnicodeStringToString(ImagePath
, ServiceName
);
267 RtlAppendUnicodeToString(ImagePath
, L
".sys");
269 if (InputImagePath
.Buffer
[0] != L
'\\')
271 ImagePath
->Length
= 0;
272 ImagePath
->MaximumLength
=
273 12 * sizeof(WCHAR
) + InputImagePath
.Length
+ sizeof(UNICODE_NULL
);
274 ImagePath
->Buffer
= ExAllocatePool(NonPagedPool
, ImagePath
->MaximumLength
);
275 if (ImagePath
->Buffer
== NULL
)
276 return STATUS_NO_MEMORY
;
278 RtlAppendUnicodeToString(ImagePath
, L
"\\SystemRoot\\");
279 RtlAppendUnicodeStringToString(ImagePath
, &InputImagePath
);
281 /* Free caller's string */
282 ExFreePoolWithTag(InputImagePath
.Buffer
, TAG_RTLREGISTRY
);
285 DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath
, ServiceName
);
287 return STATUS_SUCCESS
;
291 * IopLoadServiceModule
293 * Load a module specified by registry settings for service.
297 * Name of the service to load.
304 IopLoadServiceModule(
305 IN PUNICODE_STRING ServiceName
,
306 OUT PLDR_DATA_TABLE_ENTRY
*ModuleObject
)
308 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
310 UNICODE_STRING ServiceImagePath
, CCSName
;
312 HANDLE CCSKey
, ServiceKey
;
315 DPRINT("IopLoadServiceModule(%wZ, 0x%p)\n", ServiceName
, ModuleObject
);
317 /* FIXME: This check may be removed once the bug is fixed */
318 if (ServiceName
->Buffer
== NULL
)
320 DPRINT1("If you see this, please report to Fireball or hpoussin!\n");
321 return STATUS_UNSUCCESSFUL
;
324 if (ExpInTextModeSetup
)
326 /* We have no registry, but luckily we know where all the drivers are */
328 /* ServiceStart < 4 is all that matters */
331 /* IopNormalizeImagePath will do all of the work for us if we give it an empty string */
332 ServiceImagePath
.Length
= ServiceImagePath
.MaximumLength
= 0;
333 ServiceImagePath
.Buffer
= NULL
;
337 /* Open CurrentControlSet */
338 RtlInitUnicodeString(&CCSName
,
339 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
340 Status
= IopOpenRegistryKeyEx(&CCSKey
, NULL
, &CCSName
, KEY_READ
);
341 if (!NT_SUCCESS(Status
))
343 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with Status %08X\n",
348 /* Open service key */
349 Status
= IopOpenRegistryKeyEx(&ServiceKey
, CCSKey
, ServiceName
, KEY_READ
);
350 if (!NT_SUCCESS(Status
))
352 DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with Status %08X\n",
353 ServiceName
, Status
);
359 * Get information about the service.
362 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
364 RtlInitUnicodeString(&ServiceImagePath
, NULL
);
366 QueryTable
[0].Name
= L
"Start";
367 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
368 QueryTable
[0].EntryContext
= &ServiceStart
;
370 QueryTable
[1].Name
= L
"ImagePath";
371 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
372 QueryTable
[1].EntryContext
= &ServiceImagePath
;
374 Status
= RtlQueryRegistryValues(RTL_REGISTRY_HANDLE
,
375 (PWSTR
)ServiceKey
, QueryTable
, NULL
, NULL
);
380 if (!NT_SUCCESS(Status
))
382 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status
);
388 * Normalize the image path for all later processing.
391 Status
= IopNormalizeImagePath(&ServiceImagePath
, ServiceName
);
393 if (!NT_SUCCESS(Status
))
395 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status
);
400 * Case for disabled drivers
403 if (ServiceStart
>= 4)
405 /* We can't load this */
406 Status
= STATUS_DRIVER_UNABLE_TO_LOAD
;
410 DPRINT("Loading module from %wZ\n", &ServiceImagePath
);
411 Status
= MmLoadSystemImage(&ServiceImagePath
, NULL
, NULL
, 0, (PVOID
)ModuleObject
, &BaseAddress
);
412 if (NT_SUCCESS(Status
))
414 IopDisplayLoadingMessage(ServiceName
);
418 ExFreePool(ServiceImagePath
.Buffer
);
421 * Now check if the module was loaded successfully.
424 if (!NT_SUCCESS(Status
))
426 DPRINT("Module loading failed (Status %x)\n", Status
);
429 DPRINT("Module loading (Status %x)\n", Status
);
436 MmFreeDriverInitialization(IN PLDR_DATA_TABLE_ENTRY LdrEntry
);
439 * IopInitializeDriverModule
441 * Initialize a loaded driver.
445 * Pointer to device node.
448 * Module object representing the driver. It can be retrieve by
449 * IopLoadServiceModule.
452 * Name of the service (as in registry).
455 * Set to TRUE for file system drivers.
458 * On successful return this contains the driver object representing
463 IopInitializeDriverModule(
464 IN PDEVICE_NODE DeviceNode
,
465 IN PLDR_DATA_TABLE_ENTRY ModuleObject
,
466 IN PUNICODE_STRING ServiceName
,
467 IN BOOLEAN FileSystemDriver
,
468 OUT PDRIVER_OBJECT
*DriverObject
)
470 const WCHAR ServicesKeyName
[] = L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\";
471 WCHAR NameBuffer
[MAX_PATH
];
472 UNICODE_STRING DriverName
;
473 UNICODE_STRING RegistryKey
;
474 PDRIVER_INITIALIZE DriverEntry
;
475 PDRIVER_OBJECT Driver
;
478 DriverEntry
= ModuleObject
->EntryPoint
;
480 if (ServiceName
!= NULL
&& ServiceName
->Length
!= 0)
482 RegistryKey
.Length
= 0;
483 RegistryKey
.MaximumLength
= sizeof(ServicesKeyName
) + ServiceName
->Length
;
484 RegistryKey
.Buffer
= ExAllocatePool(PagedPool
, RegistryKey
.MaximumLength
);
485 if (RegistryKey
.Buffer
== NULL
)
487 return STATUS_INSUFFICIENT_RESOURCES
;
489 RtlAppendUnicodeToString(&RegistryKey
, ServicesKeyName
);
490 RtlAppendUnicodeStringToString(&RegistryKey
, ServiceName
);
494 RtlInitUnicodeString(&RegistryKey
, NULL
);
497 /* Create ModuleName string */
498 if (ServiceName
&& ServiceName
->Length
> 0)
500 if (FileSystemDriver
== TRUE
)
501 wcscpy(NameBuffer
, FILESYSTEM_ROOT_NAME
);
503 wcscpy(NameBuffer
, DRIVER_ROOT_NAME
);
505 RtlInitUnicodeString(&DriverName
, NameBuffer
);
506 DriverName
.MaximumLength
= sizeof(NameBuffer
);
508 RtlAppendUnicodeStringToString(&DriverName
, ServiceName
);
510 DPRINT("Driver name: '%wZ'\n", &DriverName
);
513 DriverName
.Length
= 0;
515 Status
= IopCreateDriver(
516 DriverName
.Length
> 0 ? &DriverName
: NULL
,
521 RtlFreeUnicodeString(&RegistryKey
);
523 *DriverObject
= Driver
;
524 if (!NT_SUCCESS(Status
))
526 DPRINT("IopCreateDriver() failed (Status 0x%08lx)\n", Status
);
530 MmFreeDriverInitialization((PLDR_DATA_TABLE_ENTRY
)Driver
->DriverSection
);
532 /* Set the driver as initialized */
533 IopReadyDeviceObjects(Driver
);
535 if (PnpSystemInit
) IopReinitializeDrivers();
537 return STATUS_SUCCESS
;
541 * IopAttachFilterDriversCallback
543 * Internal routine used by IopAttachFilterDrivers.
547 IopAttachFilterDriversCallback(
555 PDEVICE_NODE DeviceNode
= Context
;
556 UNICODE_STRING ServiceName
;
558 PLDR_DATA_TABLE_ENTRY ModuleObject
;
559 PDRIVER_OBJECT DriverObject
;
562 /* No filter value present */
563 if (ValueType
== REG_NONE
)
564 return STATUS_SUCCESS
;
566 for (Filters
= ValueData
;
567 ((ULONG_PTR
)Filters
- (ULONG_PTR
)ValueData
) < ValueLength
&&
569 Filters
+= (ServiceName
.Length
/ sizeof(WCHAR
)) + 1)
571 DPRINT("Filter Driver: %S (%wZ)\n", Filters
, &DeviceNode
->InstancePath
);
573 ServiceName
.Buffer
= Filters
;
574 ServiceName
.MaximumLength
=
575 ServiceName
.Length
= (USHORT
)wcslen(Filters
) * sizeof(WCHAR
);
577 Status
= IopGetDriverObject(&DriverObject
,
580 if (!NT_SUCCESS(Status
))
582 /* Load and initialize the filter driver */
583 Status
= IopLoadServiceModule(&ServiceName
, &ModuleObject
);
584 if (!NT_SUCCESS(Status
))
587 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
, &ServiceName
,
588 FALSE
, &DriverObject
);
589 if (!NT_SUCCESS(Status
))
593 Status
= IopInitializeDevice(DeviceNode
, DriverObject
);
595 /* Remove extra reference */
596 ObDereferenceObject(DriverObject
);
598 if (!NT_SUCCESS(Status
))
602 return STATUS_SUCCESS
;
606 * IopAttachFilterDrivers
608 * Load filter drivers for specified device node.
612 * Set to TRUE for loading lower level filters or FALSE for upper
617 IopAttachFilterDrivers(
618 PDEVICE_NODE DeviceNode
,
621 RTL_QUERY_REGISTRY_TABLE QueryTable
[2] = { { NULL
, 0, NULL
, NULL
, 0, NULL
, 0 }, };
622 UNICODE_STRING Class
;
623 WCHAR ClassBuffer
[40];
624 UNICODE_STRING EnumRoot
= RTL_CONSTANT_STRING(ENUM_ROOT
);
625 HANDLE EnumRootKey
, SubKey
;
628 /* Open enumeration root key */
629 Status
= IopOpenRegistryKeyEx(&EnumRootKey
, NULL
,
630 &EnumRoot
, KEY_READ
);
631 if (!NT_SUCCESS(Status
))
633 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status
);
638 Status
= IopOpenRegistryKeyEx(&SubKey
, EnumRootKey
,
639 &DeviceNode
->InstancePath
, KEY_READ
);
640 if (!NT_SUCCESS(Status
))
642 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status
);
643 ZwClose(EnumRootKey
);
648 * First load the device filters
650 QueryTable
[0].QueryRoutine
= IopAttachFilterDriversCallback
;
652 QueryTable
[0].Name
= L
"LowerFilters";
654 QueryTable
[0].Name
= L
"UpperFilters";
655 QueryTable
[0].Flags
= 0;
656 QueryTable
[0].DefaultType
= REG_NONE
;
658 Status
= RtlQueryRegistryValues(
664 if (!NT_SUCCESS(Status
))
666 DPRINT1("Failed to load device %s filters: %08X\n",
667 Lower
? "lower" : "upper", Status
);
669 ZwClose(EnumRootKey
);
674 * Now get the class GUID
677 Class
.MaximumLength
= 40 * sizeof(WCHAR
);
678 Class
.Buffer
= ClassBuffer
;
679 QueryTable
[0].QueryRoutine
= NULL
;
680 QueryTable
[0].Name
= L
"ClassGUID";
681 QueryTable
[0].EntryContext
= &Class
;
682 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_REQUIRED
| RTL_QUERY_REGISTRY_DIRECT
;
684 Status
= RtlQueryRegistryValues(
693 ZwClose(EnumRootKey
);
696 * Load the class filter driver
698 if (NT_SUCCESS(Status
))
700 UNICODE_STRING ControlClass
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Class");
702 Status
= IopOpenRegistryKeyEx(&EnumRootKey
, NULL
,
703 &ControlClass
, KEY_READ
);
704 if (!NT_SUCCESS(Status
))
706 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status
);
711 Status
= IopOpenRegistryKeyEx(&SubKey
, EnumRootKey
,
713 if (!NT_SUCCESS(Status
))
715 /* It's okay if there's no class key */
716 DPRINT1("ZwOpenKey() failed with Status %08X\n", Status
);
717 ZwClose(EnumRootKey
);
718 return STATUS_SUCCESS
;
721 QueryTable
[0].QueryRoutine
= IopAttachFilterDriversCallback
;
723 QueryTable
[0].Name
= L
"LowerFilters";
725 QueryTable
[0].Name
= L
"UpperFilters";
726 QueryTable
[0].EntryContext
= NULL
;
727 QueryTable
[0].Flags
= 0;
728 QueryTable
[0].DefaultType
= REG_NONE
;
730 Status
= RtlQueryRegistryValues(
739 ZwClose(EnumRootKey
);
741 if (!NT_SUCCESS(Status
))
743 DPRINT1("Failed to load class %s filters: %08X\n",
744 Lower
? "lower" : "upper", Status
);
746 ZwClose(EnumRootKey
);
751 return STATUS_SUCCESS
;
756 MiResolveImageReferences(IN PVOID ImageBase
,
757 IN PUNICODE_STRING ImageFileDirectory
,
758 IN PUNICODE_STRING NamePrefix OPTIONAL
,
759 OUT PCHAR
*MissingApi
,
760 OUT PWCHAR
*MissingDriver
,
761 OUT PLOAD_IMPORTS
*LoadImports
);
764 // Used for images already loaded (boot drivers)
769 LdrProcessDriverModule(PLDR_DATA_TABLE_ENTRY LdrEntry
,
770 PUNICODE_STRING FileName
,
771 PLDR_DATA_TABLE_ENTRY
*ModuleObject
)
774 UNICODE_STRING BaseName
, BaseDirectory
;
775 PLOAD_IMPORTS LoadedImports
= (PVOID
)-2;
776 PCHAR MissingApiName
, Buffer
;
777 PWCHAR MissingDriverName
;
778 PVOID DriverBase
= LdrEntry
->DllBase
;
780 /* Allocate a buffer we'll use for names */
781 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
, TAG_LDR_WSTR
);
785 return STATUS_INSUFFICIENT_RESOURCES
;
788 /* Check for a separator */
789 if (FileName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
794 /* Loop the path until we get to the base name */
795 p
= &FileName
->Buffer
[FileName
->Length
/ sizeof(WCHAR
)];
796 while (*(p
- 1) != OBJ_NAME_PATH_SEPARATOR
) p
--;
799 BaseLength
= (ULONG
)(&FileName
->Buffer
[FileName
->Length
/ sizeof(WCHAR
)] - p
);
800 BaseLength
*= sizeof(WCHAR
);
802 /* Setup the string */
803 BaseName
.Length
= (USHORT
)BaseLength
;
808 /* Otherwise, we already have a base name */
809 BaseName
.Length
= FileName
->Length
;
810 BaseName
.Buffer
= FileName
->Buffer
;
813 /* Setup the maximum length */
814 BaseName
.MaximumLength
= BaseName
.Length
;
816 /* Now compute the base directory */
817 BaseDirectory
= *FileName
;
818 BaseDirectory
.Length
-= BaseName
.Length
;
819 BaseDirectory
.MaximumLength
= BaseDirectory
.Length
;
821 /* Resolve imports */
822 MissingApiName
= Buffer
;
823 Status
= MiResolveImageReferences(DriverBase
,
830 /* Free the temporary buffer */
831 ExFreePoolWithTag(Buffer
, TAG_LDR_WSTR
);
833 /* Check the result of the imports resolution */
834 if (!NT_SUCCESS(Status
)) return Status
;
837 *ModuleObject
= LdrEntry
;
838 return STATUS_SUCCESS
;
842 * IopInitializeBuiltinDriver
844 * Initialize a driver that is already loaded in memory.
850 IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry
)
852 PDEVICE_NODE DeviceNode
;
853 PDRIVER_OBJECT DriverObject
;
855 PWCHAR FileNameWithoutPath
;
856 LPWSTR FileExtension
;
857 PUNICODE_STRING ModuleName
= &BootLdrEntry
->BaseDllName
;
858 PLDR_DATA_TABLE_ENTRY LdrEntry
;
859 PLIST_ENTRY NextEntry
;
860 UNICODE_STRING ServiceName
;
863 * Display 'Loading XXX...' message
865 IopDisplayLoadingMessage(ModuleName
);
866 InbvIndicateProgress();
869 * Generate filename without path (not needed by freeldr)
871 FileNameWithoutPath
= wcsrchr(ModuleName
->Buffer
, L
'\\');
872 if (FileNameWithoutPath
== NULL
)
874 FileNameWithoutPath
= ModuleName
->Buffer
;
878 FileNameWithoutPath
++;
882 * Strip the file extension from ServiceName
884 RtlCreateUnicodeString(&ServiceName
, FileNameWithoutPath
);
885 FileExtension
= wcsrchr(ServiceName
.Buffer
, '.');
886 if (FileExtension
!= NULL
)
888 ServiceName
.Length
-= (USHORT
)wcslen(FileExtension
) * sizeof(WCHAR
);
889 FileExtension
[0] = 0;
893 * Determine the right device object
895 /* Use IopRootDeviceNode for now */
896 Status
= IopCreateDeviceNode(IopRootDeviceNode
, NULL
, &ServiceName
, &DeviceNode
);
897 if (!NT_SUCCESS(Status
))
899 DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName
, Status
);
903 /* Lookup the new Ldr entry in PsLoadedModuleList */
904 NextEntry
= PsLoadedModuleList
.Flink
;
905 while (NextEntry
!= &PsLoadedModuleList
)
907 LdrEntry
= CONTAINING_RECORD(NextEntry
,
908 LDR_DATA_TABLE_ENTRY
,
910 if (RtlEqualUnicodeString(ModuleName
, &LdrEntry
->BaseDllName
, TRUE
))
915 NextEntry
= NextEntry
->Flink
;
917 NT_ASSERT(NextEntry
!= &PsLoadedModuleList
);
920 * Initialize the driver
922 Status
= IopInitializeDriverModule(DeviceNode
, LdrEntry
,
923 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
925 if (!NT_SUCCESS(Status
))
927 IopFreeDeviceNode(DeviceNode
);
931 Status
= IopInitializeDevice(DeviceNode
, DriverObject
);
932 if (NT_SUCCESS(Status
))
934 Status
= IopStartDevice(DeviceNode
);
937 /* Remove extra reference from IopInitializeDriverModule */
938 ObDereferenceObject(DriverObject
);
944 * IopInitializeBootDrivers
946 * Initialize boot drivers and free memory for boot files.
957 IopInitializeBootDrivers(VOID
)
959 PLIST_ENTRY ListHead
, NextEntry
, NextEntry2
;
960 PLDR_DATA_TABLE_ENTRY LdrEntry
;
961 PDEVICE_NODE DeviceNode
;
962 PDRIVER_OBJECT DriverObject
;
963 LDR_DATA_TABLE_ENTRY ModuleObject
;
965 UNICODE_STRING DriverName
;
967 PDRIVER_INFORMATION DriverInfo
, DriverInfoTag
;
969 PBOOT_DRIVER_LIST_ENTRY BootEntry
;
970 DPRINT("IopInitializeBootDrivers()\n");
972 /* Use IopRootDeviceNode for now */
973 Status
= IopCreateDeviceNode(IopRootDeviceNode
, NULL
, NULL
, &DeviceNode
);
974 if (!NT_SUCCESS(Status
)) return;
976 /* Setup the module object for the RAW FS Driver */
977 ModuleObject
.DllBase
= NULL
;
978 ModuleObject
.SizeOfImage
= 0;
979 ModuleObject
.EntryPoint
= RawFsDriverEntry
;
980 RtlInitUnicodeString(&DriverName
, L
"RAW");
983 Status
= IopInitializeDriverModule(DeviceNode
,
988 if (!NT_SUCCESS(Status
))
991 IopFreeDeviceNode(DeviceNode
);
995 /* Now initialize the associated device */
996 Status
= IopInitializeDevice(DeviceNode
, DriverObject
);
997 if (!NT_SUCCESS(Status
))
1000 IopFreeDeviceNode(DeviceNode
);
1001 ObDereferenceObject(DriverObject
);
1006 Status
= IopStartDevice(DeviceNode
);
1007 if (!NT_SUCCESS(Status
))
1010 IopFreeDeviceNode(DeviceNode
);
1011 ObDereferenceObject(DriverObject
);
1015 /* Get highest group order index */
1016 IopGroupIndex
= PpInitGetGroupOrderIndex(NULL
);
1017 if (IopGroupIndex
== 0xFFFF) ASSERT(FALSE
);
1019 /* Allocate the group table */
1020 IopGroupTable
= ExAllocatePoolWithTag(PagedPool
,
1021 IopGroupIndex
* sizeof(LIST_ENTRY
),
1023 if (IopGroupTable
== NULL
) ASSERT(FALSE
);
1025 /* Initialize the group table lists */
1026 for (i
= 0; i
< IopGroupIndex
; i
++) InitializeListHead(&IopGroupTable
[i
]);
1028 /* Loop the boot modules */
1029 ListHead
= &KeLoaderBlock
->LoadOrderListHead
;
1030 NextEntry
= ListHead
->Flink
;
1031 while (ListHead
!= NextEntry
)
1034 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1035 LDR_DATA_TABLE_ENTRY
,
1038 /* Check if the DLL needs to be initialized */
1039 if (LdrEntry
->Flags
& LDRP_DRIVER_DEPENDENT_DLL
)
1041 /* Call its entrypoint */
1042 MmCallDllInitialize(LdrEntry
, NULL
);
1045 /* Go to the next driver */
1046 NextEntry
= NextEntry
->Flink
;
1049 /* Loop the boot drivers */
1050 ListHead
= &KeLoaderBlock
->BootDriverListHead
;
1051 NextEntry
= ListHead
->Flink
;
1052 while (ListHead
!= NextEntry
)
1055 BootEntry
= CONTAINING_RECORD(NextEntry
,
1056 BOOT_DRIVER_LIST_ENTRY
,
1059 /* Get the driver loader entry */
1060 LdrEntry
= BootEntry
->LdrEntry
;
1062 /* Allocate our internal accounting structure */
1063 DriverInfo
= ExAllocatePoolWithTag(PagedPool
,
1064 sizeof(DRIVER_INFORMATION
),
1068 /* Zero it and initialize it */
1069 RtlZeroMemory(DriverInfo
, sizeof(DRIVER_INFORMATION
));
1070 InitializeListHead(&DriverInfo
->Link
);
1071 DriverInfo
->DataTableEntry
= BootEntry
;
1073 /* Open the registry key */
1074 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
1076 &BootEntry
->RegistryPath
,
1078 if ((NT_SUCCESS(Status
)) || /* ReactOS HACK for SETUPLDR */
1079 ((KeLoaderBlock
->SetupLdrBlock
) && ((KeyHandle
= (PVOID
)1)))) // yes, it's an assignment!
1081 /* Save the handle */
1082 DriverInfo
->ServiceHandle
= KeyHandle
;
1084 /* Get the group oder index */
1085 Index
= PpInitGetGroupOrderIndex(KeyHandle
);
1087 /* Get the tag position */
1088 DriverInfo
->TagPosition
= PipGetDriverTagPriority(KeyHandle
);
1090 /* Insert it into the list, at the right place */
1091 ASSERT(Index
< IopGroupIndex
);
1092 NextEntry2
= IopGroupTable
[Index
].Flink
;
1093 while (NextEntry2
!= &IopGroupTable
[Index
])
1095 /* Get the driver info */
1096 DriverInfoTag
= CONTAINING_RECORD(NextEntry2
,
1100 /* Check if we found the right tag position */
1101 if (DriverInfoTag
->TagPosition
> DriverInfo
->TagPosition
)
1108 NextEntry2
= NextEntry2
->Flink
;
1111 /* Insert us right before the next entry */
1112 NextEntry2
= NextEntry2
->Blink
;
1113 InsertHeadList(NextEntry2
, &DriverInfo
->Link
);
1117 /* Go to the next driver */
1118 NextEntry
= NextEntry
->Flink
;
1121 /* Loop each group index */
1122 for (i
= 0; i
< IopGroupIndex
; i
++)
1124 /* Loop each group table */
1125 NextEntry
= IopGroupTable
[i
].Flink
;
1126 while (NextEntry
!= &IopGroupTable
[i
])
1129 DriverInfo
= CONTAINING_RECORD(NextEntry
,
1133 /* Get the driver loader entry */
1134 LdrEntry
= DriverInfo
->DataTableEntry
->LdrEntry
;
1137 IopInitializeBuiltinDriver(LdrEntry
);
1140 NextEntry
= NextEntry
->Flink
;
1144 /* In old ROS, the loader list became empty after this point. Simulate. */
1145 InitializeListHead(&KeLoaderBlock
->LoadOrderListHead
);
1151 IopInitializeSystemDrivers(VOID
)
1153 PUNICODE_STRING
*DriverList
, *SavedList
;
1155 /* No system drivers on the boot cd */
1156 if (KeLoaderBlock
->SetupLdrBlock
) return;
1158 /* Get the driver list */
1159 SavedList
= DriverList
= CmGetSystemDriverList();
1165 /* Load the driver */
1166 ZwLoadDriver(*DriverList
);
1168 /* Free the entry */
1169 RtlFreeUnicodeString(*DriverList
);
1170 ExFreePool(*DriverList
);
1173 InbvIndicateProgress();
1178 ExFreePool(SavedList
);
1184 * Unloads a device driver.
1188 * Name of the service to unload (registry key).
1191 * Whether to unload Plug & Plug or only legacy drivers. If this
1192 * parameter is set to FALSE, the routine will unload only legacy
1199 * Guard the whole function by SEH.
1203 IopUnloadDriver(PUNICODE_STRING DriverServiceName
, BOOLEAN UnloadPnpDrivers
)
1205 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
1206 UNICODE_STRING ImagePath
;
1207 UNICODE_STRING ServiceName
;
1208 UNICODE_STRING ObjectName
;
1209 PDRIVER_OBJECT DriverObject
;
1210 PDEVICE_OBJECT DeviceObject
;
1211 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension
;
1212 LOAD_UNLOAD_PARAMS LoadParams
;
1215 BOOLEAN SafeToUnload
= TRUE
;
1217 DPRINT("IopUnloadDriver('%wZ', %u)\n", DriverServiceName
, UnloadPnpDrivers
);
1222 * Get the service name from the registry key name
1225 Start
= wcsrchr(DriverServiceName
->Buffer
, L
'\\');
1227 Start
= DriverServiceName
->Buffer
;
1231 RtlInitUnicodeString(&ServiceName
, Start
);
1234 * Construct the driver object name
1237 ObjectName
.Length
= ((USHORT
)wcslen(Start
) + 8) * sizeof(WCHAR
);
1238 ObjectName
.MaximumLength
= ObjectName
.Length
+ sizeof(WCHAR
);
1239 ObjectName
.Buffer
= ExAllocatePool(PagedPool
, ObjectName
.MaximumLength
);
1240 if (!ObjectName
.Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
1241 wcscpy(ObjectName
.Buffer
, DRIVER_ROOT_NAME
);
1242 memcpy(ObjectName
.Buffer
+ 8, Start
, ObjectName
.Length
- 8 * sizeof(WCHAR
));
1243 ObjectName
.Buffer
[ObjectName
.Length
/sizeof(WCHAR
)] = 0;
1246 * Find the driver object
1248 Status
= ObReferenceObjectByName(&ObjectName
,
1255 (PVOID
*)&DriverObject
);
1257 if (!NT_SUCCESS(Status
))
1259 DPRINT1("Can't locate driver object for %wZ\n", &ObjectName
);
1260 ExFreePool(ObjectName
.Buffer
);
1265 * Free the buffer for driver object name
1267 ExFreePool(ObjectName
.Buffer
);
1269 /* Check that driver is not already unloading */
1270 if (DriverObject
->Flags
& DRVO_UNLOAD_INVOKED
)
1272 DPRINT1("Driver deletion pending\n");
1273 ObDereferenceObject(DriverObject
);
1274 return STATUS_DELETE_PENDING
;
1278 * Get path of service...
1281 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
1283 RtlInitUnicodeString(&ImagePath
, NULL
);
1285 QueryTable
[0].Name
= L
"ImagePath";
1286 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
1287 QueryTable
[0].EntryContext
= &ImagePath
;
1289 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
1290 DriverServiceName
->Buffer
, QueryTable
, NULL
, NULL
);
1292 if (!NT_SUCCESS(Status
))
1294 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status
);
1295 ObDereferenceObject(DriverObject
);
1300 * Normalize the image path for all later processing.
1303 Status
= IopNormalizeImagePath(&ImagePath
, &ServiceName
);
1305 if (!NT_SUCCESS(Status
))
1307 DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status
);
1308 ObDereferenceObject(DriverObject
);
1313 * Free the service path
1316 ExFreePool(ImagePath
.Buffer
);
1319 * Unload the module and release the references to the device object
1322 /* Call the load/unload routine, depending on current process */
1323 if (DriverObject
->DriverUnload
&& DriverObject
->DriverSection
&&
1324 (UnloadPnpDrivers
|| (DriverObject
->Flags
& DRVO_LEGACY_DRIVER
)))
1326 /* Loop through each device object of the driver
1327 and set DOE_UNLOAD_PENDING flag */
1328 DeviceObject
= DriverObject
->DeviceObject
;
1329 while (DeviceObject
)
1331 /* Set the unload pending flag for the device */
1332 DeviceExtension
= IoGetDevObjExtension(DeviceObject
);
1333 DeviceExtension
->ExtensionFlags
|= DOE_UNLOAD_PENDING
;
1335 /* Make sure there are no attached devices or no reference counts */
1336 if ((DeviceObject
->ReferenceCount
) || (DeviceObject
->AttachedDevice
))
1338 /* Not safe to unload */
1339 DPRINT1("Drivers device object is referenced or has attached devices\n");
1341 SafeToUnload
= FALSE
;
1344 DeviceObject
= DeviceObject
->NextDevice
;
1347 /* If not safe to unload, then return success */
1350 ObDereferenceObject(DriverObject
);
1351 return STATUS_SUCCESS
;
1354 DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject
->DriverName
);
1356 /* Set the unload invoked flag */
1357 DriverObject
->Flags
|= DRVO_UNLOAD_INVOKED
;
1359 if (PsGetCurrentProcess() == PsInitialSystemProcess
)
1361 /* Just call right away */
1362 (*DriverObject
->DriverUnload
)(DriverObject
);
1366 /* Load/Unload must be called from system process */
1368 /* Prepare parameters block */
1369 LoadParams
.DriverObject
= DriverObject
;
1370 KeInitializeEvent(&LoadParams
.Event
, NotificationEvent
, FALSE
);
1372 ExInitializeWorkItem(&LoadParams
.WorkItem
,
1373 (PWORKER_THREAD_ROUTINE
)IopLoadUnloadDriver
,
1374 (PVOID
)&LoadParams
);
1377 ExQueueWorkItem(&LoadParams
.WorkItem
, DelayedWorkQueue
);
1379 /* And wait when it completes */
1380 KeWaitForSingleObject(&LoadParams
.Event
, UserRequest
, KernelMode
,
1384 /* Mark the driver object temporary, so it could be deleted later */
1385 ObMakeTemporaryObject(DriverObject
);
1387 /* Dereference it 2 times */
1388 ObDereferenceObject(DriverObject
);
1389 ObDereferenceObject(DriverObject
);
1391 return STATUS_SUCCESS
;
1395 DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject
->DriverName
);
1397 /* Dereference one time (refd inside this function) */
1398 ObDereferenceObject(DriverObject
);
1400 /* Return unloading failure */
1401 return STATUS_INVALID_DEVICE_REQUEST
;
1407 IopReinitializeDrivers(VOID
)
1409 PDRIVER_REINIT_ITEM ReinitItem
;
1412 /* Get the first entry and start looping */
1413 Entry
= ExInterlockedRemoveHeadList(&DriverReinitListHead
,
1414 &DriverReinitListLock
);
1418 ReinitItem
= CONTAINING_RECORD(Entry
, DRIVER_REINIT_ITEM
, ItemEntry
);
1420 /* Increment reinitialization counter */
1421 ReinitItem
->DriverObject
->DriverExtension
->Count
++;
1423 /* Remove the device object flag */
1424 ReinitItem
->DriverObject
->Flags
&= ~DRVO_REINIT_REGISTERED
;
1426 /* Call the routine */
1427 ReinitItem
->ReinitRoutine(ReinitItem
->DriverObject
,
1428 ReinitItem
->Context
,
1429 ReinitItem
->DriverObject
->
1430 DriverExtension
->Count
);
1432 /* Free the entry */
1435 /* Move to the next one */
1436 Entry
= ExInterlockedRemoveHeadList(&DriverReinitListHead
,
1437 &DriverReinitListLock
);
1443 IopReinitializeBootDrivers(VOID
)
1445 PDRIVER_REINIT_ITEM ReinitItem
;
1448 /* Get the first entry and start looping */
1449 Entry
= ExInterlockedRemoveHeadList(&DriverBootReinitListHead
,
1450 &DriverBootReinitListLock
);
1454 ReinitItem
= CONTAINING_RECORD(Entry
, DRIVER_REINIT_ITEM
, ItemEntry
);
1456 /* Increment reinitialization counter */
1457 ReinitItem
->DriverObject
->DriverExtension
->Count
++;
1459 /* Remove the device object flag */
1460 ReinitItem
->DriverObject
->Flags
&= ~DRVO_BOOTREINIT_REGISTERED
;
1462 /* Call the routine */
1463 ReinitItem
->ReinitRoutine(ReinitItem
->DriverObject
,
1464 ReinitItem
->Context
,
1465 ReinitItem
->DriverObject
->
1466 DriverExtension
->Count
);
1468 /* Free the entry */
1471 /* Move to the next one */
1472 Entry
= ExInterlockedRemoveHeadList(&DriverBootReinitListHead
,
1473 &DriverBootReinitListLock
);
1479 IopCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL
,
1480 IN PDRIVER_INITIALIZE InitializationFunction
,
1481 IN PUNICODE_STRING RegistryPath
,
1482 PLDR_DATA_TABLE_ENTRY ModuleObject
,
1483 OUT PDRIVER_OBJECT
*pDriverObject
)
1485 WCHAR NameBuffer
[100];
1487 UNICODE_STRING LocalDriverName
;
1489 OBJECT_ATTRIBUTES ObjectAttributes
;
1491 PDRIVER_OBJECT DriverObject
;
1492 UNICODE_STRING ServiceKeyName
;
1494 ULONG i
, RetryCount
= 0;
1497 /* First, create a unique name for the driver if we don't have one */
1500 /* Create a random name and set up the string*/
1501 NameLength
= (USHORT
)swprintf(NameBuffer
,
1502 DRIVER_ROOT_NAME L
"%08u",
1504 LocalDriverName
.Length
= NameLength
* sizeof(WCHAR
);
1505 LocalDriverName
.MaximumLength
= LocalDriverName
.Length
+ sizeof(UNICODE_NULL
);
1506 LocalDriverName
.Buffer
= NameBuffer
;
1510 /* So we can avoid another code path, use a local var */
1511 LocalDriverName
= *DriverName
;
1514 /* Initialize the Attributes */
1515 ObjectSize
= sizeof(DRIVER_OBJECT
) + sizeof(EXTENDED_DRIVER_EXTENSION
);
1516 InitializeObjectAttributes(&ObjectAttributes
,
1518 OBJ_PERMANENT
| OBJ_CASE_INSENSITIVE
,
1522 /* Create the Object */
1523 Status
= ObCreateObject(KernelMode
,
1531 (PVOID
*)&DriverObject
);
1532 if (!NT_SUCCESS(Status
)) return Status
;
1534 DPRINT("IopCreateDriver(): created DO %p\n", DriverObject
);
1536 /* Set up the Object */
1537 RtlZeroMemory(DriverObject
, ObjectSize
);
1538 DriverObject
->Type
= IO_TYPE_DRIVER
;
1539 DriverObject
->Size
= sizeof(DRIVER_OBJECT
);
1540 DriverObject
->Flags
= DRVO_LEGACY_DRIVER
;
1541 DriverObject
->DriverExtension
= (PDRIVER_EXTENSION
)(DriverObject
+ 1);
1542 DriverObject
->DriverExtension
->DriverObject
= DriverObject
;
1543 DriverObject
->DriverInit
= InitializationFunction
;
1544 DriverObject
->DriverSection
= ModuleObject
;
1545 /* Loop all Major Functions */
1546 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
1548 /* Invalidate each function */
1549 DriverObject
->MajorFunction
[i
] = IopInvalidDeviceRequest
;
1552 /* Set up the service key name buffer */
1553 ServiceKeyName
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
1554 LocalDriverName
.Length
+
1557 if (!ServiceKeyName
.Buffer
)
1560 ObMakeTemporaryObject(DriverObject
);
1561 ObDereferenceObject(DriverObject
);
1562 return STATUS_INSUFFICIENT_RESOURCES
;
1565 /* Fill out the key data and copy the buffer */
1566 ServiceKeyName
.Length
= LocalDriverName
.Length
;
1567 ServiceKeyName
.MaximumLength
= LocalDriverName
.MaximumLength
;
1568 RtlCopyMemory(ServiceKeyName
.Buffer
,
1569 LocalDriverName
.Buffer
,
1570 LocalDriverName
.Length
);
1572 /* Null-terminate it and set it */
1573 ServiceKeyName
.Buffer
[ServiceKeyName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1574 DriverObject
->DriverExtension
->ServiceKeyName
= ServiceKeyName
;
1576 /* Also store it in the Driver Object. This is a bit of a hack. */
1577 RtlCopyMemory(&DriverObject
->DriverName
,
1579 sizeof(UNICODE_STRING
));
1581 /* Add the Object and get its handle */
1582 Status
= ObInsertObject(DriverObject
,
1589 /* Eliminate small possibility when this function is called more than
1590 once in a row, and KeTickCount doesn't get enough time to change */
1591 if (!DriverName
&& (Status
== STATUS_OBJECT_NAME_COLLISION
) && (RetryCount
< 100))
1597 if (!NT_SUCCESS(Status
)) return Status
;
1599 /* Now reference it */
1600 Status
= ObReferenceObjectByHandle(hDriver
,
1604 (PVOID
*)&DriverObject
,
1607 /* Close the extra handle */
1610 if (!NT_SUCCESS(Status
))
1613 ObMakeTemporaryObject(DriverObject
);
1614 ObDereferenceObject(DriverObject
);
1618 DriverObject
->HardwareDatabase
= &IopHardwareDatabaseKey
;
1619 DriverObject
->DriverStart
= ModuleObject
? ModuleObject
->DllBase
: 0;
1620 DriverObject
->DriverSize
= ModuleObject
? ModuleObject
->SizeOfImage
: 0;
1622 /* Finally, call its init function */
1623 DPRINT("RegistryKey: %wZ\n", RegistryPath
);
1624 DPRINT("Calling driver entrypoint at %p\n", InitializationFunction
);
1625 Status
= (*InitializationFunction
)(DriverObject
, RegistryPath
);
1626 if (!NT_SUCCESS(Status
))
1628 /* If it didn't work, then kill the object */
1629 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", DriverName
, Status
);
1630 DriverObject
->DriverSection
= NULL
;
1631 ObMakeTemporaryObject(DriverObject
);
1632 ObDereferenceObject(DriverObject
);
1637 /* Returns to caller the object */
1638 *pDriverObject
= DriverObject
;
1641 /* We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
1642 * Other parts of the I/O manager depend on this behavior */
1643 if (!DriverObject
->DeviceObject
) DriverObject
->Flags
&= ~DRVO_LEGACY_DRIVER
;
1645 /* Loop all Major Functions */
1646 for (i
= 0; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
1649 * Make sure the driver didn't set any dispatch entry point to NULL!
1650 * Doing so is illegal; drivers shouldn't touch entry points they
1654 /* Check if it did so anyway */
1655 if (!DriverObject
->MajorFunction
[i
])
1657 /* Print a warning in the debug log */
1658 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1659 &DriverObject
->DriverName
, i
);
1662 DriverObject
->MajorFunction
[i
] = IopInvalidDeviceRequest
;
1666 /* Return the Status */
1670 /* PUBLIC FUNCTIONS ***********************************************************/
1677 IoCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL
,
1678 IN PDRIVER_INITIALIZE InitializationFunction
)
1680 PDRIVER_OBJECT DriverObject
;
1681 return IopCreateDriver(DriverName
, InitializationFunction
, NULL
, NULL
, &DriverObject
);
1689 IoDeleteDriver(IN PDRIVER_OBJECT DriverObject
)
1691 /* Simply dereference the Object */
1692 ObDereferenceObject(DriverObject
);
1700 IoRegisterBootDriverReinitialization(IN PDRIVER_OBJECT DriverObject
,
1701 IN PDRIVER_REINITIALIZE ReinitRoutine
,
1704 PDRIVER_REINIT_ITEM ReinitItem
;
1706 /* Allocate the entry */
1707 ReinitItem
= ExAllocatePoolWithTag(NonPagedPool
,
1708 sizeof(DRIVER_REINIT_ITEM
),
1710 if (!ReinitItem
) return;
1713 ReinitItem
->DriverObject
= DriverObject
;
1714 ReinitItem
->ReinitRoutine
= ReinitRoutine
;
1715 ReinitItem
->Context
= Context
;
1717 /* Set the Driver Object flag and insert the entry into the list */
1718 DriverObject
->Flags
|= DRVO_BOOTREINIT_REGISTERED
;
1719 ExInterlockedInsertTailList(&DriverBootReinitListHead
,
1720 &ReinitItem
->ItemEntry
,
1721 &DriverBootReinitListLock
);
1729 IoRegisterDriverReinitialization(IN PDRIVER_OBJECT DriverObject
,
1730 IN PDRIVER_REINITIALIZE ReinitRoutine
,
1733 PDRIVER_REINIT_ITEM ReinitItem
;
1735 /* Allocate the entry */
1736 ReinitItem
= ExAllocatePoolWithTag(NonPagedPool
,
1737 sizeof(DRIVER_REINIT_ITEM
),
1739 if (!ReinitItem
) return;
1742 ReinitItem
->DriverObject
= DriverObject
;
1743 ReinitItem
->ReinitRoutine
= ReinitRoutine
;
1744 ReinitItem
->Context
= Context
;
1746 /* Set the Driver Object flag and insert the entry into the list */
1747 DriverObject
->Flags
|= DRVO_REINIT_REGISTERED
;
1748 ExInterlockedInsertTailList(&DriverReinitListHead
,
1749 &ReinitItem
->ItemEntry
,
1750 &DriverReinitListLock
);
1758 IoAllocateDriverObjectExtension(IN PDRIVER_OBJECT DriverObject
,
1759 IN PVOID ClientIdentificationAddress
,
1760 IN ULONG DriverObjectExtensionSize
,
1761 OUT PVOID
*DriverObjectExtension
)
1764 PIO_CLIENT_EXTENSION DriverExtensions
, NewDriverExtension
;
1765 BOOLEAN Inserted
= FALSE
;
1767 /* Assume failure */
1768 *DriverObjectExtension
= NULL
;
1770 /* Allocate the extension */
1771 NewDriverExtension
= ExAllocatePoolWithTag(NonPagedPool
,
1772 sizeof(IO_CLIENT_EXTENSION
) +
1773 DriverObjectExtensionSize
,
1774 TAG_DRIVER_EXTENSION
);
1775 if (!NewDriverExtension
) return STATUS_INSUFFICIENT_RESOURCES
;
1777 /* Clear the extension for teh caller */
1778 RtlZeroMemory(NewDriverExtension
,
1779 sizeof(IO_CLIENT_EXTENSION
) + DriverObjectExtensionSize
);
1782 OldIrql
= KeRaiseIrqlToDpcLevel();
1784 /* Fill out the extension */
1785 NewDriverExtension
->ClientIdentificationAddress
= ClientIdentificationAddress
;
1787 /* Loop the current extensions */
1788 DriverExtensions
= IoGetDrvObjExtension(DriverObject
)->
1789 ClientDriverExtension
;
1790 while (DriverExtensions
)
1792 /* Check if the identifier matches */
1793 if (DriverExtensions
->ClientIdentificationAddress
==
1794 ClientIdentificationAddress
)
1796 /* We have a collision, break out */
1800 /* Go to the next one */
1801 DriverExtensions
= DriverExtensions
->NextExtension
;
1804 /* Check if we didn't collide */
1805 if (!DriverExtensions
)
1807 /* Link this one in */
1808 NewDriverExtension
->NextExtension
=
1809 IoGetDrvObjExtension(DriverObject
)->ClientDriverExtension
;
1810 IoGetDrvObjExtension(DriverObject
)->ClientDriverExtension
=
1815 /* Release the lock */
1816 KeLowerIrql(OldIrql
);
1818 /* Check if insertion failed */
1821 /* Free the entry and fail */
1822 ExFreePoolWithTag(NewDriverExtension
, TAG_DRIVER_EXTENSION
);
1823 return STATUS_OBJECT_NAME_COLLISION
;
1826 /* Otherwise, return the pointer */
1827 *DriverObjectExtension
= NewDriverExtension
+ 1;
1828 return STATUS_SUCCESS
;
1836 IoGetDriverObjectExtension(IN PDRIVER_OBJECT DriverObject
,
1837 IN PVOID ClientIdentificationAddress
)
1840 PIO_CLIENT_EXTENSION DriverExtensions
;
1843 OldIrql
= KeRaiseIrqlToDpcLevel();
1845 /* Loop the list until we find the right one */
1846 DriverExtensions
= IoGetDrvObjExtension(DriverObject
)->ClientDriverExtension
;
1847 while (DriverExtensions
)
1849 /* Check for a match */
1850 if (DriverExtensions
->ClientIdentificationAddress
==
1851 ClientIdentificationAddress
)
1858 DriverExtensions
= DriverExtensions
->NextExtension
;
1862 KeLowerIrql(OldIrql
);
1864 /* Return nothing or the extension */
1865 if (!DriverExtensions
) return NULL
;
1866 return DriverExtensions
+ 1;
1870 IopLoadUnloadDriver(PLOAD_UNLOAD_PARAMS LoadParams
)
1872 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
1873 UNICODE_STRING ImagePath
;
1874 UNICODE_STRING ServiceName
;
1877 PDEVICE_NODE DeviceNode
;
1878 PDRIVER_OBJECT DriverObject
;
1879 PLDR_DATA_TABLE_ENTRY ModuleObject
;
1883 /* Check if it's an unload request */
1884 if (LoadParams
->DriverObject
)
1886 (*LoadParams
->DriverObject
->DriverUnload
)(LoadParams
->DriverObject
);
1888 /* Return success and signal the event */
1889 LoadParams
->Status
= STATUS_SUCCESS
;
1890 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
1894 RtlInitUnicodeString(&ImagePath
, NULL
);
1897 * Get the service name from the registry key name.
1899 ASSERT(LoadParams
->ServiceName
->Length
>= sizeof(WCHAR
));
1901 ServiceName
= *LoadParams
->ServiceName
;
1902 cur
= LoadParams
->ServiceName
->Buffer
+
1903 (LoadParams
->ServiceName
->Length
/ sizeof(WCHAR
)) - 1;
1904 while (LoadParams
->ServiceName
->Buffer
!= cur
)
1908 ServiceName
.Buffer
= cur
+ 1;
1909 ServiceName
.Length
= LoadParams
->ServiceName
->Length
-
1910 (USHORT
)((ULONG_PTR
)ServiceName
.Buffer
-
1911 (ULONG_PTR
)LoadParams
->ServiceName
->Buffer
);
1921 RtlZeroMemory(&QueryTable
, sizeof(QueryTable
));
1923 RtlInitUnicodeString(&ImagePath
, NULL
);
1925 QueryTable
[0].Name
= L
"Type";
1926 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
1927 QueryTable
[0].EntryContext
= &Type
;
1929 QueryTable
[1].Name
= L
"ImagePath";
1930 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
1931 QueryTable
[1].EntryContext
= &ImagePath
;
1933 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
1934 LoadParams
->ServiceName
->Buffer
,
1935 QueryTable
, NULL
, NULL
);
1936 if (!NT_SUCCESS(Status
))
1938 DPRINT("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
1939 if (ImagePath
.Buffer
) ExFreePool(ImagePath
.Buffer
);
1940 LoadParams
->Status
= Status
;
1941 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
1946 * Normalize the image path for all later processing.
1949 Status
= IopNormalizeImagePath(&ImagePath
, &ServiceName
);
1950 if (!NT_SUCCESS(Status
))
1952 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status
);
1953 LoadParams
->Status
= Status
;
1954 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
1958 DPRINT("FullImagePath: '%wZ'\n", &ImagePath
);
1959 DPRINT("Type: %lx\n", Type
);
1962 * Get existing DriverObject pointer (in case the driver
1963 * has already been loaded and initialized).
1965 Status
= IopGetDriverObject(&DriverObject
,
1967 (Type
== 2 /* SERVICE_FILE_SYSTEM_DRIVER */ ||
1968 Type
== 8 /* SERVICE_RECOGNIZER_DRIVER */));
1970 if (!NT_SUCCESS(Status
))
1973 * Load the driver module
1976 DPRINT("Loading module from %wZ\n", &ImagePath
);
1977 Status
= MmLoadSystemImage(&ImagePath
, NULL
, NULL
, 0, (PVOID
)&ModuleObject
, &BaseAddress
);
1978 if (!NT_SUCCESS(Status
))
1980 DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status
);
1981 LoadParams
->Status
= Status
;
1982 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
1987 * Initialize the driver module if it's loaded for the first time
1989 Status
= IopCreateDeviceNode(IopRootDeviceNode
, NULL
, &ServiceName
, &DeviceNode
);
1990 if (!NT_SUCCESS(Status
))
1992 DPRINT1("IopCreateDeviceNode() failed (Status %lx)\n", Status
);
1993 MmUnloadSystemImage(ModuleObject
);
1994 LoadParams
->Status
= Status
;
1995 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
1999 IopDisplayLoadingMessage(&DeviceNode
->ServiceName
);
2001 Status
= IopInitializeDriverModule(DeviceNode
,
2003 &DeviceNode
->ServiceName
,
2004 (Type
== 2 /* SERVICE_FILE_SYSTEM_DRIVER */ ||
2005 Type
== 8 /* SERVICE_RECOGNIZER_DRIVER */),
2007 if (!NT_SUCCESS(Status
))
2009 DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status
);
2010 MmUnloadSystemImage(ModuleObject
);
2011 IopFreeDeviceNode(DeviceNode
);
2012 LoadParams
->Status
= Status
;
2013 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
2017 /* Initialize and start device */
2018 IopInitializeDevice(DeviceNode
, DriverObject
);
2019 Status
= IopStartDevice(DeviceNode
);
2023 DPRINT("DriverObject already exist in ObjectManager\n");
2024 Status
= STATUS_IMAGE_ALREADY_LOADED
;
2026 /* IopGetDriverObject references the DriverObject, so dereference it */
2027 ObDereferenceObject(DriverObject
);
2030 /* Pass status to the caller and signal the event */
2031 LoadParams
->Status
= Status
;
2032 KeSetEvent(&LoadParams
->Event
, 0, FALSE
);
2038 * Loads a device driver.
2042 * Name of the service to load (registry key).
2051 NtLoadDriver(IN PUNICODE_STRING DriverServiceName
)
2053 UNICODE_STRING CapturedDriverServiceName
= { 0, 0, NULL
};
2054 KPROCESSOR_MODE PreviousMode
;
2055 LOAD_UNLOAD_PARAMS LoadParams
;
2060 PreviousMode
= KeGetPreviousMode();
2063 * Check security privileges
2066 /* FIXME: Uncomment when privileges will be correctly implemented. */
2068 if (!SeSinglePrivilegeCheck(SeLoadDriverPrivilege
, PreviousMode
))
2070 DPRINT("Privilege not held\n");
2071 return STATUS_PRIVILEGE_NOT_HELD
;
2075 Status
= ProbeAndCaptureUnicodeString(&CapturedDriverServiceName
,
2078 if (!NT_SUCCESS(Status
))
2083 DPRINT("NtLoadDriver('%wZ')\n", &CapturedDriverServiceName
);
2085 LoadParams
.ServiceName
= &CapturedDriverServiceName
;
2086 LoadParams
.DriverObject
= NULL
;
2087 KeInitializeEvent(&LoadParams
.Event
, NotificationEvent
, FALSE
);
2089 /* Call the load/unload routine, depending on current process */
2090 if (PsGetCurrentProcess() == PsInitialSystemProcess
)
2092 /* Just call right away */
2093 IopLoadUnloadDriver(&LoadParams
);
2097 /* Load/Unload must be called from system process */
2098 ExInitializeWorkItem(&LoadParams
.WorkItem
,
2099 (PWORKER_THREAD_ROUTINE
)IopLoadUnloadDriver
,
2100 (PVOID
)&LoadParams
);
2103 ExQueueWorkItem(&LoadParams
.WorkItem
, DelayedWorkQueue
);
2105 /* And wait when it completes */
2106 KeWaitForSingleObject(&LoadParams
.Event
, UserRequest
, KernelMode
,
2110 ReleaseCapturedUnicodeString(&CapturedDriverServiceName
,
2113 return LoadParams
.Status
;
2119 * Unloads a legacy device driver.
2123 * Name of the service to unload (registry key).
2133 NtUnloadDriver(IN PUNICODE_STRING DriverServiceName
)
2135 return IopUnloadDriver(DriverServiceName
, FALSE
);