ReactOS 0.4.16-dev-1019-g2c2cdfd
driver.c
Go to the documentation of this file.
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#include <mm/ARM3/miarm.h>
17
18/* GLOBALS ********************************************************************/
19
21
25
29
31 RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DESCRIPTION\\SYSTEM");
32static const WCHAR ServicesKeyName[] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\";
33
35
39
42
43/* TYPES *********************************************************************/
44
45// Parameters packet for Load/Unload work item's context
46typedef struct _LOAD_UNLOAD_PARAMS
47{
55
60
61/* PRIVATE FUNCTIONS **********************************************************/
62
67 PIRP Irp)
68{
69 Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
70 Irp->IoStatus.Information = 0;
73}
74
75VOID
78{
79 PDRIVER_OBJECT DriverObject = ObjectBody;
80 PIO_CLIENT_EXTENSION DriverExtension, NextDriverExtension;
81 PAGED_CODE();
82
83 DPRINT1("Deleting driver object '%wZ'\n", &DriverObject->DriverName);
84
85 /* There must be no device objects remaining at this point */
86 ASSERT(!DriverObject->DeviceObject);
87
88 /* Get the extension and loop them */
89 DriverExtension = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
90 while (DriverExtension)
91 {
92 /* Get the next one */
93 NextDriverExtension = DriverExtension->NextExtension;
95
96 /* Move on */
97 DriverExtension = NextDriverExtension;
98 }
99
100 /* Check if the driver image is still loaded */
101 if (DriverObject->DriverSection)
102 {
103 /* Unload it */
104 MmUnloadSystemImage(DriverObject->DriverSection);
105 }
106
107 /* Check if it has a name */
108 if (DriverObject->DriverName.Buffer)
109 {
110 /* Free it */
111 ExFreePool(DriverObject->DriverName.Buffer);
112 }
113
114 /* Check if it has a service key name */
115 if (DriverObject->DriverExtension->ServiceKeyName.Buffer)
116 {
117 /* Free it */
118 ExFreePool(DriverObject->DriverExtension->ServiceKeyName.Buffer);
119 }
120}
121
124 _In_ HANDLE ServiceHandle,
125 _Out_ PUNICODE_STRING DriverName,
127{
128 UNICODE_STRING driverName = {.Buffer = NULL}, serviceName;
131
132 PAGED_CODE();
133
134 /* 1. Check the "ObjectName" field in the driver's registry key (it has priority) */
135 status = IopGetRegistryValue(ServiceHandle, L"ObjectName", &kvInfo);
136 if (NT_SUCCESS(status))
137 {
138 /* We've got the ObjectName, use it as the driver name */
139 if ((kvInfo->Type != REG_SZ) ||
140 (kvInfo->DataLength < sizeof(UNICODE_NULL)) ||
142 ((kvInfo->DataLength % sizeof(WCHAR)) != 0))
143 {
144 DPRINT1("ObjectName invalid (Type = %lu, DataLength = %lu)\n",
145 kvInfo->Type,
146 kvInfo->DataLength);
147 ExFreePool(kvInfo);
149 }
150
151 driverName.Length = (USHORT)(kvInfo->DataLength - sizeof(UNICODE_NULL));
152 driverName.MaximumLength = kvInfo->DataLength;
154 if (!driverName.Buffer)
155 {
156 ExFreePool(kvInfo);
158 }
159
160 RtlMoveMemory(driverName.Buffer,
161 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
162 driverName.Length);
163 driverName.Buffer[driverName.Length / sizeof(WCHAR)] = UNICODE_NULL;
164 ExFreePool(kvInfo);
165 }
166
167 /* Check whether we need to get ServiceName as well, either to construct
168 * the driver name (because we could not use "ObjectName"), or because
169 * it is requested by the caller. */
170 PKEY_BASIC_INFORMATION basicInfo = NULL;
171 if (!NT_SUCCESS(status) || ServiceName != NULL)
172 {
173 /* Retrieve the necessary buffer size */
174 ULONG infoLength;
175 status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
177 {
179 goto Cleanup;
180 }
181
182 /* Allocate the buffer and retrieve the data */
183 basicInfo = ExAllocatePoolWithTag(PagedPool, infoLength, TAG_IO);
184 if (!basicInfo)
185 {
187 goto Cleanup;
188 }
189
190 status = ZwQueryKey(ServiceHandle, KeyBasicInformation, basicInfo, infoLength, &infoLength);
191 if (!NT_SUCCESS(status))
192 {
193 goto Cleanup;
194 }
195
196 serviceName.Length = basicInfo->NameLength;
197 serviceName.MaximumLength = basicInfo->NameLength;
198 serviceName.Buffer = basicInfo->Name;
199 }
200
201 /* 2. There is no "ObjectName" - construct it ourselves. Depending on the driver type,
202 * it will be either "\Driver<ServiceName>" or "\FileSystem<ServiceName>" */
203 if (driverName.Buffer == NULL)
204 {
205 ASSERT(basicInfo); // Container for serviceName
206
207 /* Retrieve the driver type */
208 ULONG driverType;
209 status = IopGetRegistryValue(ServiceHandle, L"Type", &kvInfo);
210 if (!NT_SUCCESS(status))
211 {
212 goto Cleanup;
213 }
214 if (kvInfo->Type != REG_DWORD || kvInfo->DataLength != sizeof(ULONG))
215 {
216 ExFreePool(kvInfo);
218 goto Cleanup;
219 }
220
221 RtlMoveMemory(&driverType,
222 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
223 sizeof(ULONG));
224 ExFreePool(kvInfo);
225
226 /* Compute the necessary driver name string size */
227 if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
228 driverName.MaximumLength = sizeof(FILESYSTEM_ROOT_NAME);
229 else
230 driverName.MaximumLength = sizeof(DRIVER_ROOT_NAME);
231
232 driverName.MaximumLength += serviceName.Length;
233 driverName.Length = 0;
234
235 /* Allocate and build it */
237 if (!driverName.Buffer)
238 {
240 goto Cleanup;
241 }
242
243 if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
245 else
247
249 }
250
251 if (ServiceName != NULL)
252 {
253 ASSERT(basicInfo); // Container for serviceName
254
255 /* Allocate a copy for the caller */
257 if (!buf)
258 {
260 goto Cleanup;
261 }
262 RtlMoveMemory(buf, serviceName.Buffer, serviceName.Length);
263 ServiceName->MaximumLength = serviceName.Length;
264 ServiceName->Length = serviceName.Length;
265 ServiceName->Buffer = buf;
266 }
267
268 *DriverName = driverName;
270
271Cleanup:
272 if (basicInfo)
273 ExFreePoolWithTag(basicInfo, TAG_IO);
274
275 if (!NT_SUCCESS(status) && driverName.Buffer)
276 ExFreePoolWithTag(driverName.Buffer, TAG_IO);
277
278 return status;
279}
280
285static BOOLEAN
287 _In_ PCUNICODE_STRING String1,
290{
291 PWCHAR pc1, pc2;
292 ULONG NumChars;
293
294 if (String2->Length < String1->Length)
295 return FALSE;
296
297 NumChars = String1->Length / sizeof(WCHAR);
298 pc1 = String1->Buffer;
299 pc2 = &String2->Buffer[String2->Length / sizeof(WCHAR) - NumChars];
300
301 if (pc1 && pc2)
302 {
303 if (CaseInSensitive)
304 {
305 while (NumChars--)
306 {
307 if (RtlUpcaseUnicodeChar(*pc1++) !=
309 {
310 return FALSE;
311 }
312 }
313 }
314 else
315 {
316 while (NumChars--)
317 {
318 if (*pc1++ != *pc2++)
319 return FALSE;
320 }
321 }
322
323 return TRUE;
324 }
325
326 return FALSE;
327}
328
332static VOID
336{
337 extern BOOLEAN SosEnabled; // See ex/init.c
338 static const UNICODE_STRING DotSys = RTL_CONSTANT_STRING(L".SYS");
339 CHAR TextBuffer[256];
340
341 if (!SosEnabled) return;
342 if (!KeLoaderBlock) return;
344 "%s%sSystem32\\Drivers\\%wZ%s\r\n",
349 ? "" : ".SYS");
351}
352
353/*
354 * IopNormalizeImagePath
355 *
356 * Normalize an image path to contain complete path.
357 *
358 * Parameters
359 * ImagePath
360 * The input path and on exit the result path. ImagePath.Buffer
361 * must be allocated by ExAllocatePool on input. Caller is responsible
362 * for freeing the buffer when it's no longer needed.
363 *
364 * ServiceName
365 * Name of the service that ImagePath belongs to.
366 *
367 * Return Value
368 * Status
369 *
370 * Remarks
371 * The input image path isn't freed on error.
372 */
376 _Inout_ _When_(return>=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem)))
377 PUNICODE_STRING ImagePath,
379{
380 UNICODE_STRING SystemRootString = RTL_CONSTANT_STRING(L"\\SystemRoot\\");
381 UNICODE_STRING DriversPathString = RTL_CONSTANT_STRING(L"\\SystemRoot\\System32\\drivers\\");
382 UNICODE_STRING DotSysString = RTL_CONSTANT_STRING(L".sys");
383 UNICODE_STRING InputImagePath;
384
385 DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
386
387 InputImagePath = *ImagePath;
388 if (InputImagePath.Length == 0)
389 {
390 ImagePath->Length = 0;
391 ImagePath->MaximumLength = DriversPathString.Length +
392 ServiceName->Length +
393 DotSysString.Length +
394 sizeof(UNICODE_NULL);
395 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
396 ImagePath->MaximumLength,
397 TAG_IO);
398 if (ImagePath->Buffer == NULL)
399 return STATUS_NO_MEMORY;
400
401 RtlCopyUnicodeString(ImagePath, &DriversPathString);
403 RtlAppendUnicodeStringToString(ImagePath, &DotSysString);
404 }
405 else if (InputImagePath.Buffer[0] != L'\\')
406 {
407 ImagePath->Length = 0;
408 ImagePath->MaximumLength = SystemRootString.Length +
409 InputImagePath.Length +
410 sizeof(UNICODE_NULL);
411 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
412 ImagePath->MaximumLength,
413 TAG_IO);
414 if (ImagePath->Buffer == NULL)
415 return STATUS_NO_MEMORY;
416
417 RtlCopyUnicodeString(ImagePath, &SystemRootString);
418 RtlAppendUnicodeStringToString(ImagePath, &InputImagePath);
419
420 /* Free caller's string */
422 }
423
424 DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
425
426 return STATUS_SUCCESS;
427}
428
450 _In_ PLDR_DATA_TABLE_ENTRY ModuleObject,
451 _In_ HANDLE ServiceHandle,
452 _Out_ PDRIVER_OBJECT *OutDriverObject,
453 _Out_ NTSTATUS *DriverEntryStatus)
454{
457
458 PAGED_CODE();
459
460 Status = IopGetDriverNames(ServiceHandle, &DriverName, &ServiceName);
461 if (!NT_SUCCESS(Status))
462 {
463 MmUnloadSystemImage(ModuleObject);
464 return Status;
465 }
466
467 DPRINT("Driver name: '%wZ'\n", &DriverName);
468
469 /*
470 * Retrieve the driver's PE image NT header and perform some sanity checks.
471 * NOTE: We suppose that since the driver has been successfully loaded,
472 * its NT and optional headers are all valid and have expected sizes.
473 */
474 PIMAGE_NT_HEADERS NtHeaders = RtlImageNtHeader(ModuleObject->DllBase);
475 ASSERT(NtHeaders);
476 // NOTE: ModuleObject->SizeOfImage is actually (number of PTEs)*PAGE_SIZE.
477 ASSERT(ModuleObject->SizeOfImage == ROUND_TO_PAGES(NtHeaders->OptionalHeader.SizeOfImage));
478 ASSERT(ModuleObject->EntryPoint == RVA(ModuleObject->DllBase, NtHeaders->OptionalHeader.AddressOfEntryPoint));
479
480 /* Obtain the registry path for the DriverInit routine */
481 PKEY_NAME_INFORMATION nameInfo;
482 ULONG infoLength;
483 Status = ZwQueryKey(ServiceHandle, KeyNameInformation, NULL, 0, &infoLength);
485 {
486 nameInfo = ExAllocatePoolWithTag(NonPagedPool, infoLength, TAG_IO);
487 if (nameInfo)
488 {
489 Status = ZwQueryKey(ServiceHandle,
491 nameInfo,
492 infoLength,
493 &infoLength);
494 if (NT_SUCCESS(Status))
495 {
496 RegistryPath.Length = nameInfo->NameLength;
497 RegistryPath.MaximumLength = nameInfo->NameLength;
498 RegistryPath.Buffer = nameInfo->Name;
499 }
500 else
501 {
502 ExFreePoolWithTag(nameInfo, TAG_IO);
503 }
504 }
505 else
506 {
508 }
509 }
510 else
511 {
513 }
514
515 if (!NT_SUCCESS(Status))
516 {
518 RtlFreeUnicodeString(&DriverName);
519 MmUnloadSystemImage(ModuleObject);
520 return Status;
521 }
522
523 /* Create the driver object */
524 ULONG ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
525 OBJECT_ATTRIBUTES objAttrs;
526 PDRIVER_OBJECT driverObject;
528 &DriverName,
530 NULL,
531 NULL);
532
535 &objAttrs,
537 NULL,
538 ObjectSize,
539 0,
540 0,
541 (PVOID*)&driverObject);
542 if (!NT_SUCCESS(Status))
543 {
544 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
546 RtlFreeUnicodeString(&DriverName);
547 MmUnloadSystemImage(ModuleObject);
548 DPRINT1("Error while creating driver object \"%wZ\" status %x\n", &DriverName, Status);
549 return Status;
550 }
551
552 DPRINT("Created driver object 0x%p for \"%wZ\"\n", driverObject, &DriverName);
553
554 RtlZeroMemory(driverObject, ObjectSize);
555 driverObject->Type = IO_TYPE_DRIVER;
556 driverObject->Size = sizeof(DRIVER_OBJECT);
557
558 /* Set the legacy flag if this is not a WDM driver */
560 driverObject->Flags |= DRVO_LEGACY_DRIVER;
561
562 driverObject->DriverSection = ModuleObject;
563 driverObject->DriverStart = ModuleObject->DllBase;
564 driverObject->DriverSize = ModuleObject->SizeOfImage;
565 driverObject->DriverInit = ModuleObject->EntryPoint;
567 driverObject->DriverExtension = (PDRIVER_EXTENSION)(driverObject + 1);
568 driverObject->DriverExtension->DriverObject = driverObject;
569
570 /* Loop all Major Functions */
571 for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
572 {
573 /* Invalidate each function */
574 driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
575 }
576
577 /* Add the Object and get its handle */
579 Status = ObInsertObject(driverObject, NULL, FILE_READ_DATA, 0, NULL, &hDriver);
580 if (!NT_SUCCESS(Status))
581 {
582 ExFreePoolWithTag(nameInfo, TAG_IO);
584 RtlFreeUnicodeString(&DriverName);
585 return Status;
586 }
587
588 /* Now reference it */
590 0,
593 (PVOID*)&driverObject,
594 NULL);
595
596 /* Close the extra handle */
598
599 if (!NT_SUCCESS(Status))
600 {
601 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
603 RtlFreeUnicodeString(&DriverName);
604 return Status;
605 }
606
607 /* Set up the service key name buffer */
608 UNICODE_STRING serviceKeyName;
609 serviceKeyName.Length = 0;
610 // NULL-terminate for Windows compatibility
611 serviceKeyName.MaximumLength = ServiceName.MaximumLength + sizeof(UNICODE_NULL);
613 serviceKeyName.MaximumLength,
614 TAG_IO);
615 if (!serviceKeyName.Buffer)
616 {
617 ObMakeTemporaryObject(driverObject);
618 ObDereferenceObject(driverObject);
619 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
621 RtlFreeUnicodeString(&DriverName);
623 }
624
625 /* Copy the name and set it in the driver extension */
626 RtlCopyUnicodeString(&serviceKeyName, &ServiceName);
628 driverObject->DriverExtension->ServiceKeyName = serviceKeyName;
629
630 /* Make a copy of the driver name to store in the driver object */
631 UNICODE_STRING driverNamePaged;
632 driverNamePaged.Length = 0;
633 // NULL-terminate for Windows compatibility
634 driverNamePaged.MaximumLength = DriverName.MaximumLength + sizeof(UNICODE_NULL);
635 driverNamePaged.Buffer = ExAllocatePoolWithTag(PagedPool,
636 driverNamePaged.MaximumLength,
637 TAG_IO);
638 if (!driverNamePaged.Buffer)
639 {
640 ObMakeTemporaryObject(driverObject);
641 ObDereferenceObject(driverObject);
642 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
643 RtlFreeUnicodeString(&DriverName);
645 }
646
647 RtlCopyUnicodeString(&driverNamePaged, &DriverName);
648 driverObject->DriverName = driverNamePaged;
649
650 /* Finally, call its init function */
651 Status = driverObject->DriverInit(driverObject, &RegistryPath);
652 *DriverEntryStatus = Status;
653 if (!NT_SUCCESS(Status))
654 {
655 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", &DriverName, Status);
656 // return a special status value in case of failure
658 }
659
660 /* HACK: We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
661 * Other parts of the I/O manager depend on this behavior */
662 if (!driverObject->DeviceObject)
663 {
664 driverObject->Flags &= ~DRVO_LEGACY_DRIVER;
665 }
666
667 /* Windows does this fixup, keep it for compatibility */
668 for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
669 {
670 /*
671 * Make sure the driver didn't set any dispatch entry point to NULL!
672 * Doing so is illegal; drivers shouldn't touch entry points they
673 * do not implement.
674 */
675
676 /* Check if it did so anyway */
677 if (!driverObject->MajorFunction[i])
678 {
679 /* Print a warning in the debug log */
680 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
681 &driverObject->DriverName, i);
682
683 /* Fix it up */
684 driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
685 }
686 }
687
688 // TODO: for legacy drivers, unload the driver if it didn't create any DO
689
690 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
691 RtlFreeUnicodeString(&DriverName);
692
693 if (!NT_SUCCESS(Status))
694 {
695 // if the driver entry has been failed, clear the object
696 ObMakeTemporaryObject(driverObject);
697 ObDereferenceObject(driverObject);
698 return Status;
699 }
700
701 *OutDriverObject = driverObject;
702
704
705 /* Set the driver as initialized */
706 IopReadyDeviceObjects(driverObject);
707
709
710 return STATUS_SUCCESS;
711}
712
714NTAPI
716 IN PUNICODE_STRING ImageFileDirectory,
717 IN PUNICODE_STRING NamePrefix OPTIONAL,
718 OUT PCHAR *MissingApi,
719 OUT PWCHAR *MissingDriver,
720 OUT PLOAD_IMPORTS *LoadImports);
721
722//
723// Used for images already loaded (boot drivers)
724//
725CODE_SEG("INIT")
727NTAPI
730 PLDR_DATA_TABLE_ENTRY *ModuleObject)
731{
733 UNICODE_STRING BaseName, BaseDirectory;
734 PLOAD_IMPORTS LoadedImports = MM_SYSLDR_NO_IMPORTS;
735 PCHAR MissingApiName, Buffer;
736 PWCHAR MissingDriverName;
737 PVOID DriverBase = LdrEntry->DllBase;
738
739 /* Allocate a buffer we'll use for names */
743 if (!Buffer)
744 {
745 /* Fail */
747 }
748
749 /* Check for a separator */
750 if (FileName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
751 {
752 PWCHAR p;
753 ULONG BaseLength;
754
755 /* Loop the path until we get to the base name */
756 p = &FileName->Buffer[FileName->Length / sizeof(WCHAR)];
757 while (*(p - 1) != OBJ_NAME_PATH_SEPARATOR) p--;
758
759 /* Get the length */
760 BaseLength = (ULONG)(&FileName->Buffer[FileName->Length / sizeof(WCHAR)] - p);
761 BaseLength *= sizeof(WCHAR);
762
763 /* Setup the string */
764 BaseName.Length = (USHORT)BaseLength;
765 BaseName.Buffer = p;
766 }
767 else
768 {
769 /* Otherwise, we already have a base name */
770 BaseName.Length = FileName->Length;
771 BaseName.Buffer = FileName->Buffer;
772 }
773
774 /* Setup the maximum length */
775 BaseName.MaximumLength = BaseName.Length;
776
777 /* Now compute the base directory */
778 BaseDirectory = *FileName;
779 BaseDirectory.Length -= BaseName.Length;
780 BaseDirectory.MaximumLength = BaseDirectory.Length;
781
782 /* Resolve imports */
783 MissingApiName = Buffer;
784 Status = MiResolveImageReferences(DriverBase,
785 &BaseDirectory,
786 NULL,
787 &MissingApiName,
788 &MissingDriverName,
789 &LoadedImports);
790
791 /* Free the temporary buffer */
793
794 /* Check the result of the imports resolution */
795 if (!NT_SUCCESS(Status)) return Status;
796
797 /* Return */
798 *ModuleObject = LdrEntry;
799 return STATUS_SUCCESS;
800}
801
804
805/*
806 * IopInitializeBuiltinDriver
807 *
808 * Initialize a driver that is already loaded in memory.
809 */
810CODE_SEG("INIT")
811static
814{
817 PWCHAR Buffer, FileNameWithoutPath;
818 PWSTR FileExtension;
819 PUNICODE_STRING ModuleName = &BootLdrEntry->BaseDllName;
820 PLDR_DATA_TABLE_ENTRY LdrEntry;
821 PLIST_ENTRY NextEntry;
824
825 /*
826 * Display 'Loading XXX...' message
827 */
830
832 ModuleName->Length + sizeof(UNICODE_NULL),
833 TAG_IO);
834 if (Buffer == NULL)
835 {
836 return FALSE;
837 }
838
841
842 /*
843 * Generate filename without path (not needed by freeldr)
844 */
845 FileNameWithoutPath = wcsrchr(Buffer, L'\\');
846 if (FileNameWithoutPath == NULL)
847 {
848 FileNameWithoutPath = Buffer;
849 }
850 else
851 {
852 FileNameWithoutPath++;
853 }
854
855 /*
856 * Strip the file extension from ServiceName
857 */
858 Success = RtlCreateUnicodeString(&ServiceName, FileNameWithoutPath);
860 if (!Success)
861 {
862 return FALSE;
863 }
864
865 FileExtension = wcsrchr(ServiceName.Buffer, L'.');
866 if (FileExtension != NULL)
867 {
868 ServiceName.Length -= (USHORT)wcslen(FileExtension) * sizeof(WCHAR);
869 FileExtension[0] = UNICODE_NULL;
870 }
871
873
874 // Make the registry path for the driver
875 RegistryPath.Length = 0;
876 RegistryPath.MaximumLength = sizeof(ServicesKeyName) + ServiceName.Length;
878 if (RegistryPath.Buffer == NULL)
879 {
880 return FALSE;
881 }
885
886 HANDLE serviceHandle;
889 if (!NT_SUCCESS(Status))
890 {
891 return FALSE;
892 }
893
894 /* Lookup the new Ldr entry in PsLoadedModuleList */
895 for (NextEntry = PsLoadedModuleList.Flink;
896 NextEntry != &PsLoadedModuleList;
897 NextEntry = NextEntry->Flink)
898 {
899 LdrEntry = CONTAINING_RECORD(NextEntry,
901 InLoadOrderLinks);
903 {
904 break;
905 }
906 }
907 ASSERT(NextEntry != &PsLoadedModuleList);
908
909 /*
910 * Initialize the driver
911 */
912 NTSTATUS driverEntryStatus;
914 serviceHandle,
916 &driverEntryStatus);
917
918 if (!NT_SUCCESS(Status))
919 {
920 DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName, Status);
921 return FALSE;
922 }
923
924 // The driver has been loaded, now check if where are any PDOs
925 // for that driver, and queue AddDevice call for them.
926 // The check is possible because HKLM/SYSTEM/CCS/Services/<ServiceName>/Enum directory
927 // is populated upon a new device arrival based on a (critical) device database
928
929 // Legacy drivers may add devices inside DriverEntry.
930 // We're lazy and always assume that they are doing so
931 BOOLEAN deviceAdded = !!(DriverObject->Flags & DRVO_LEGACY_DRIVER);
932
933 HANDLE enumServiceHandle;
934 UNICODE_STRING enumName = RTL_CONSTANT_STRING(L"Enum");
935
936 Status = IopOpenRegistryKeyEx(&enumServiceHandle, serviceHandle, &enumName, KEY_READ);
937 ZwClose(serviceHandle);
938
939 if (NT_SUCCESS(Status))
940 {
941 ULONG instanceCount = 0;
943 Status = IopGetRegistryValue(enumServiceHandle, L"Count", &kvInfo);
944 if (!NT_SUCCESS(Status))
945 {
946 goto Cleanup;
947 }
948 if (kvInfo->Type != REG_DWORD || kvInfo->DataLength != sizeof(ULONG))
949 {
950 ExFreePool(kvInfo);
951 goto Cleanup;
952 }
953
954 RtlMoveMemory(&instanceCount,
955 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
956 sizeof(ULONG));
957 ExFreePool(kvInfo);
958
959 DPRINT("Processing %u instances for %wZ module\n", instanceCount, ModuleName);
960
961 for (ULONG i = 0; i < instanceCount; i++)
962 {
963 WCHAR num[11];
964 UNICODE_STRING instancePath;
965 RtlStringCbPrintfW(num, sizeof(num), L"%u", i);
966
967 Status = IopGetRegistryValue(enumServiceHandle, num, &kvInfo);
968 if (!NT_SUCCESS(Status))
969 {
970 continue;
971 }
972 if ((kvInfo->Type != REG_SZ) ||
973 (kvInfo->DataLength < sizeof(UNICODE_NULL)) ||
975 ((kvInfo->DataLength % sizeof(WCHAR)) != 0))
976 {
977 DPRINT1("ObjectName invalid (Type = %lu, DataLength = %lu)\n",
978 kvInfo->Type,
979 kvInfo->DataLength);
980 ExFreePool(kvInfo);
981 continue;
982 }
983
984 instancePath.Length = (USHORT)(kvInfo->DataLength - sizeof(UNICODE_NULL));
985 instancePath.MaximumLength = kvInfo->DataLength;
987 instancePath.MaximumLength,
988 TAG_IO);
989 if (instancePath.Buffer)
990 {
991 RtlMoveMemory(instancePath.Buffer,
992 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
993 instancePath.Length);
994 instancePath.Buffer[instancePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
995
997 if (pdo != NULL)
998 {
1001 deviceAdded = TRUE;
1002 }
1003 else
1004 {
1005 DPRINT1("No device node found matching instance path '%wZ'\n", &instancePath);
1006 }
1007 }
1008
1009 ExFreePool(kvInfo);
1010 }
1011
1012 ZwClose(enumServiceHandle);
1013 }
1014Cleanup:
1015 /* Remove extra reference from IopInitializeDriverModule */
1017
1018 return deviceAdded;
1019}
1020
1021/*
1022 * IopInitializeBootDrivers
1023 *
1024 * Initialize boot drivers and free memory for boot files.
1025 *
1026 * Parameters
1027 * None
1028 *
1029 * Return Value
1030 * None
1031 */
1032CODE_SEG("INIT")
1033VOID
1036{
1037 PLIST_ENTRY ListHead, NextEntry, NextEntry2;
1038 PLDR_DATA_TABLE_ENTRY LdrEntry;
1040 UNICODE_STRING DriverName;
1041 ULONG i, Index;
1042 PDRIVER_INFORMATION DriverInfo, DriverInfoTag;
1044 PBOOT_DRIVER_LIST_ENTRY BootEntry;
1045 DPRINT("IopInitializeBootDrivers()\n");
1046
1047 /* Create the RAW FS built-in driver */
1048 RtlInitUnicodeString(&DriverName, L"\\FileSystem\\RAW");
1049
1050 Status = IoCreateDriver(&DriverName, RawFsDriverEntry);
1051 if (!NT_SUCCESS(Status))
1052 {
1053 /* Fail */
1054 return;
1055 }
1056
1057 /* Get highest group order index */
1059 if (IopGroupIndex == 0xFFFF)
1060 {
1062 }
1063
1064 /* Allocate the group table */
1066 IopGroupIndex * sizeof(LIST_ENTRY),
1067 TAG_IO);
1068 if (IopGroupTable == NULL)
1069 {
1071 }
1072
1073 /* Initialize the group table lists */
1074 for (i = 0; i < IopGroupIndex; i++) InitializeListHead(&IopGroupTable[i]);
1075
1076 /* Loop the boot modules */
1077 ListHead = &KeLoaderBlock->LoadOrderListHead;
1078 for (NextEntry = ListHead->Flink;
1079 NextEntry != ListHead;
1080 NextEntry = NextEntry->Flink)
1081 {
1082 /* Get the entry */
1083 LdrEntry = CONTAINING_RECORD(NextEntry,
1085 InLoadOrderLinks);
1086
1087 /* Check if the DLL needs to be initialized */
1088 if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
1089 {
1090 /* Call its entrypoint */
1091 MmCallDllInitialize(LdrEntry, NULL);
1092 }
1093 }
1094
1095 /* Loop the boot drivers */
1096 ListHead = &KeLoaderBlock->BootDriverListHead;
1097 for (NextEntry = ListHead->Flink;
1098 NextEntry != ListHead;
1099 NextEntry = NextEntry->Flink)
1100 {
1101 /* Get the entry */
1102 BootEntry = CONTAINING_RECORD(NextEntry,
1104 Link);
1105
1106 // FIXME: TODO: This LdrEntry is to be used in a special handling
1107 // for SETUPLDR (a similar procedure is done on Windows), where
1108 // the loader would, under certain conditions, be loaded in the
1109 // SETUPLDR-specific code block below...
1110#if 0
1111 /* Get the driver loader entry */
1112 LdrEntry = BootEntry->LdrEntry;
1113#endif
1114
1115 /* Allocate our internal accounting structure */
1117 sizeof(DRIVER_INFORMATION),
1118 TAG_IO);
1119 if (DriverInfo)
1120 {
1121 /* Zero it and initialize it */
1124 DriverInfo->DataTableEntry = BootEntry;
1125
1126 /* Open the registry key */
1128 NULL,
1129 &BootEntry->RegistryPath,
1130 KEY_READ);
1131 DPRINT("IopOpenRegistryKeyEx(%wZ) returned 0x%08lx\n", &BootEntry->RegistryPath, Status);
1132#if 0
1133 if (NT_SUCCESS(Status))
1134#else // Hack still needed...
1135 if ((NT_SUCCESS(Status)) || /* ReactOS HACK for SETUPLDR */
1136 ((KeLoaderBlock->SetupLdrBlock) && ((KeyHandle = (PVOID)1)))) // yes, it's an assignment!
1137#endif
1138 {
1139 /* Save the handle */
1141
1142 /* Get the group oder index */
1144
1145 /* Get the tag position */
1147
1148 /* Insert it into the list, at the right place */
1150 NextEntry2 = IopGroupTable[Index].Flink;
1151 while (NextEntry2 != &IopGroupTable[Index])
1152 {
1153 /* Get the driver info */
1154 DriverInfoTag = CONTAINING_RECORD(NextEntry2,
1156 Link);
1157
1158 /* Check if we found the right tag position */
1159 if (DriverInfoTag->TagPosition > DriverInfo->TagPosition)
1160 {
1161 /* We're done */
1162 break;
1163 }
1164
1165 /* Next entry */
1166 NextEntry2 = NextEntry2->Flink;
1167 }
1168
1169 /* Insert us right before the next entry */
1170 NextEntry2 = NextEntry2->Blink;
1171 InsertHeadList(NextEntry2, &DriverInfo->Link);
1172 }
1173 }
1174 }
1175
1176 /* Loop each group index */
1177 for (i = 0; i < IopGroupIndex; i++)
1178 {
1179 /* Loop each group table */
1180 for (NextEntry = IopGroupTable[i].Flink;
1181 NextEntry != &IopGroupTable[i];
1182 NextEntry = NextEntry->Flink)
1183 {
1184 /* Get the entry */
1185 DriverInfo = CONTAINING_RECORD(NextEntry,
1187 Link);
1188
1189 /* Get the driver loader entry */
1190 LdrEntry = DriverInfo->DataTableEntry->LdrEntry;
1191
1192 /* Initialize it */
1193 if (IopInitializeBuiltinDriver(LdrEntry))
1194 {
1195 // it does not make sense to enumerate the tree if there are no new devices added
1198 NULL,
1199 NULL);
1200 }
1201 }
1202 }
1203
1204 /* HAL Root Bus is being initialized before loading the boot drivers so this may cause issues
1205 * when some devices are not being initialized with their drivers. This flag is used to delay
1206 * all actions with devices (except PnP root device) until boot drivers are loaded.
1207 * See PiQueueDeviceAction function
1208 */
1210
1211 DbgPrint("BOOT DRIVERS LOADED\n");
1212
1215 NULL,
1216 NULL);
1217}
1218
1219CODE_SEG("INIT")
1220VOID
1223{
1224 PUNICODE_STRING *DriverList, *SavedList;
1225
1227
1228 /* HACK: No system drivers on the BootCD */
1229 if (KeLoaderBlock->SetupLdrBlock) return;
1230
1231 /* Get the driver list */
1232 SavedList = DriverList = CmGetSystemDriverList();
1233 ASSERT(DriverList);
1234
1235 /* Loop it */
1236 while (*DriverList)
1237 {
1238 /* Load the driver */
1239 ZwLoadDriver(*DriverList);
1240
1241 /* Free the entry */
1242 RtlFreeUnicodeString(*DriverList);
1243 ExFreePool(*DriverList);
1244
1245 /* Next entry */
1247 DriverList++;
1248 }
1249
1250 /* Free the list */
1251 ExFreePool(SavedList);
1252
1255 NULL,
1256 NULL);
1257}
1258
1259/*
1260 * IopUnloadDriver
1261 *
1262 * Unloads a device driver.
1263 *
1264 * Parameters
1265 * DriverServiceName
1266 * Name of the service to unload (registry key).
1267 *
1268 * UnloadPnpDrivers
1269 * Whether to unload Plug & Plug or only legacy drivers. If this
1270 * parameter is set to FALSE, the routine will unload only legacy
1271 * drivers.
1272 *
1273 * Return Value
1274 * Status
1275 *
1276 * To do
1277 * Guard the whole function by SEH.
1278 */
1279
1281IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
1282{
1283 UNICODE_STRING Backslash = RTL_CONSTANT_STRING(L"\\");
1285 UNICODE_STRING ImagePath;
1290 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
1292 USHORT LastBackslash;
1293 BOOLEAN SafeToUnload = TRUE;
1295 UNICODE_STRING CapturedServiceName;
1296
1297 PAGED_CODE();
1298
1300
1301 /* Need the appropriate priviliege */
1303 {
1304 DPRINT1("No unload privilege!\n");
1306 }
1307
1308 /* Capture the service name */
1309 Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
1311 DriverServiceName);
1312 if (!NT_SUCCESS(Status))
1313 {
1314 return Status;
1315 }
1316
1317 DPRINT("IopUnloadDriver('%wZ', %u)\n", &CapturedServiceName, UnloadPnpDrivers);
1318
1319 /* We need a service name */
1320 if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
1321 {
1322 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1324 }
1325
1326 /*
1327 * Get the service name from the registry key name
1328 */
1330 &CapturedServiceName,
1331 &Backslash,
1332 &LastBackslash);
1333 if (NT_SUCCESS(Status))
1334 {
1335 NT_ASSERT(CapturedServiceName.Length >= LastBackslash + sizeof(WCHAR));
1336 ServiceName.Buffer = &CapturedServiceName.Buffer[LastBackslash / sizeof(WCHAR) + 1];
1337 ServiceName.Length = CapturedServiceName.Length - LastBackslash - sizeof(WCHAR);
1338 ServiceName.MaximumLength = CapturedServiceName.MaximumLength - LastBackslash - sizeof(WCHAR);
1339 }
1340 else
1341 {
1342 ServiceName = CapturedServiceName;
1343 }
1344
1345 /*
1346 * Construct the driver object name
1347 */
1348 Status = RtlUShortAdd(sizeof(DRIVER_ROOT_NAME),
1349 ServiceName.Length,
1350 &ObjectName.MaximumLength);
1351 if (!NT_SUCCESS(Status))
1352 {
1353 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1354 return Status;
1355 }
1356 ObjectName.Length = 0;
1358 ObjectName.MaximumLength,
1359 TAG_IO);
1360 if (!ObjectName.Buffer)
1361 {
1362 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1364 }
1367
1368 /*
1369 * Find the driver object
1370 */
1372 0,
1373 0,
1374 0,
1376 KernelMode,
1377 0,
1378 (PVOID*)&DriverObject);
1379
1380 if (!NT_SUCCESS(Status))
1381 {
1382 DPRINT1("Can't locate driver object for %wZ\n", &ObjectName);
1384 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1385 return Status;
1386 }
1387
1388 /* Free the buffer for driver object name */
1390
1391 /* Check that driver is not already unloading */
1392 if (DriverObject->Flags & DRVO_UNLOAD_INVOKED)
1393 {
1394 DPRINT1("Driver deletion pending\n");
1396 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1397 return STATUS_DELETE_PENDING;
1398 }
1399
1400 /*
1401 * Get path of service...
1402 */
1404
1405 RtlInitUnicodeString(&ImagePath, NULL);
1406
1407 QueryTable[0].Name = L"ImagePath";
1409 QueryTable[0].EntryContext = &ImagePath;
1410
1412 CapturedServiceName.Buffer,
1413 QueryTable,
1414 NULL,
1415 NULL);
1416
1417 /* We no longer need service name */
1418 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1419
1420 if (!NT_SUCCESS(Status))
1421 {
1422 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1424 return Status;
1425 }
1426
1427 /*
1428 * Normalize the image path for all later processing.
1429 */
1431
1432 if (!NT_SUCCESS(Status))
1433 {
1434 DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status);
1436 return Status;
1437 }
1438
1439 /* Free the service path */
1440 ExFreePool(ImagePath.Buffer);
1441
1442 /*
1443 * Unload the module and release the references to the device object
1444 */
1445
1446 /* Call the load/unload routine, depending on current process */
1447 if (DriverObject->DriverUnload && DriverObject->DriverSection &&
1448 (UnloadPnpDrivers || (DriverObject->Flags & DRVO_LEGACY_DRIVER)))
1449 {
1450 /* Loop through each device object of the driver
1451 and set DOE_UNLOAD_PENDING flag */
1452 DeviceObject = DriverObject->DeviceObject;
1453 while (DeviceObject)
1454 {
1455 /* Set the unload pending flag for the device */
1456 DeviceExtension = IoGetDevObjExtension(DeviceObject);
1457 DeviceExtension->ExtensionFlags |= DOE_UNLOAD_PENDING;
1458
1459 /* Make sure there are no attached devices or no reference counts */
1460 if ((DeviceObject->ReferenceCount) || (DeviceObject->AttachedDevice))
1461 {
1462 /* Not safe to unload */
1463 DPRINT1("Drivers device object is referenced or has attached devices\n");
1464
1465 SafeToUnload = FALSE;
1466 }
1467
1468 DeviceObject = DeviceObject->NextDevice;
1469 }
1470
1471 /* If not safe to unload, then return success */
1472 if (!SafeToUnload)
1473 {
1475 return STATUS_SUCCESS;
1476 }
1477
1478 DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject->DriverName);
1479
1480 /* Set the unload invoked flag and call the unload routine */
1484
1485 /* Mark the driver object temporary, so it could be deleted later */
1487
1488 /* Dereference it 2 times */
1491
1492 return Status;
1493 }
1494 else
1495 {
1496 DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject->DriverName);
1497
1498 /* Dereference one time (refd inside this function) */
1500
1501 /* Return unloading failure */
1503 }
1504}
1505
1506VOID
1507NTAPI
1509{
1510 PDRIVER_REINIT_ITEM ReinitItem;
1512
1513 /* Get the first entry and start looping */
1516 while (Entry)
1517 {
1518 /* Get the item */
1519 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1520
1521 /* Increment reinitialization counter */
1522 ReinitItem->DriverObject->DriverExtension->Count++;
1523
1524 /* Remove the device object flag */
1525 ReinitItem->DriverObject->Flags &= ~DRVO_REINIT_REGISTERED;
1526
1527 /* Call the routine */
1528 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1529 ReinitItem->Context,
1530 ReinitItem->DriverObject->
1531 DriverExtension->Count);
1532
1533 /* Free the entry */
1535
1536 /* Move to the next one */
1539 }
1540}
1541
1542VOID
1543NTAPI
1545{
1546 PDRIVER_REINIT_ITEM ReinitItem;
1548
1549 /* Get the first entry and start looping */
1552 while (Entry)
1553 {
1554 /* Get the item */
1555 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1556
1557 /* Increment reinitialization counter */
1558 ReinitItem->DriverObject->DriverExtension->Count++;
1559
1560 /* Remove the device object flag */
1561 ReinitItem->DriverObject->Flags &= ~DRVO_BOOTREINIT_REGISTERED;
1562
1563 /* Call the routine */
1564 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1565 ReinitItem->Context,
1566 ReinitItem->DriverObject->
1567 DriverExtension->Count);
1568
1569 /* Free the entry */
1571
1572 /* Move to the next one */
1575 }
1576
1577 /* Wait for all device actions being finished*/
1579}
1580
1581/* PUBLIC FUNCTIONS ***********************************************************/
1582
1583/*
1584 * @implemented
1585 */
1587NTAPI
1589 _In_opt_ PUNICODE_STRING DriverName,
1590 _In_ PDRIVER_INITIALIZE InitializationFunction)
1591{
1592 WCHAR NameBuffer[100];
1593 USHORT NameLength;
1594 UNICODE_STRING LocalDriverName;
1597 ULONG ObjectSize;
1599 UNICODE_STRING ServiceKeyName;
1601 ULONG i, RetryCount = 0;
1602
1603try_again:
1604 /* First, create a unique name for the driver if we don't have one */
1605 if (!DriverName)
1606 {
1607 /* Create a random name and set up the string */
1608 NameLength = (USHORT)swprintf(NameBuffer,
1609 DRIVER_ROOT_NAME L"%08u",
1611 LocalDriverName.Length = NameLength * sizeof(WCHAR);
1612 LocalDriverName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1613 LocalDriverName.Buffer = NameBuffer;
1614 }
1615 else
1616 {
1617 /* So we can avoid another code path, use a local var */
1618 LocalDriverName = *DriverName;
1619 }
1620
1621 /* Initialize the Attributes */
1622 ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
1624 &LocalDriverName,
1626 NULL,
1627 NULL);
1628
1629 /* Create the Object */
1633 KernelMode,
1634 NULL,
1635 ObjectSize,
1636 0,
1637 0,
1638 (PVOID*)&DriverObject);
1639 if (!NT_SUCCESS(Status)) return Status;
1640
1641 DPRINT("IopCreateDriver(): created DO %p\n", DriverObject);
1642
1643 /* Set up the Object */
1644 RtlZeroMemory(DriverObject, ObjectSize);
1646 DriverObject->Size = sizeof(DRIVER_OBJECT);
1648 DriverObject->DriverExtension = (PDRIVER_EXTENSION)(DriverObject + 1);
1649 DriverObject->DriverExtension->DriverObject = DriverObject;
1650 DriverObject->DriverInit = InitializationFunction;
1651 /* Loop all Major Functions */
1652 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1653 {
1654 /* Invalidate each function */
1655 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1656 }
1657
1658 /* Set up the service key name buffer */
1659 ServiceKeyName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1660 ServiceKeyName.Buffer = ExAllocatePoolWithTag(PagedPool, LocalDriverName.MaximumLength, TAG_IO);
1661 if (!ServiceKeyName.Buffer)
1662 {
1663 /* Fail */
1667 }
1668
1669 /* For builtin drivers, the ServiceKeyName is equal to DriverName */
1670 RtlCopyUnicodeString(&ServiceKeyName, &LocalDriverName);
1671 ServiceKeyName.Buffer[ServiceKeyName.Length / sizeof(WCHAR)] = UNICODE_NULL;
1672 DriverObject->DriverExtension->ServiceKeyName = ServiceKeyName;
1673
1674 /* Make a copy of the driver name to store in the driver object */
1675 DriverObject->DriverName.MaximumLength = LocalDriverName.Length;
1676 DriverObject->DriverName.Buffer = ExAllocatePoolWithTag(PagedPool,
1677 DriverObject->DriverName.MaximumLength,
1678 TAG_IO);
1679 if (!DriverObject->DriverName.Buffer)
1680 {
1681 /* Fail */
1685 }
1686
1687 RtlCopyUnicodeString(&DriverObject->DriverName, &LocalDriverName);
1688
1689 /* Add the Object and get its handle */
1691 NULL,
1693 0,
1694 NULL,
1695 &hDriver);
1696
1697 /* Eliminate small possibility when this function is called more than
1698 once in a row, and KeTickCount doesn't get enough time to change */
1699 if (!DriverName && (Status == STATUS_OBJECT_NAME_COLLISION) && (RetryCount < 100))
1700 {
1701 RetryCount++;
1702 goto try_again;
1703 }
1704
1705 if (!NT_SUCCESS(Status)) return Status;
1706
1707 /* Now reference it */
1709 0,
1711 KernelMode,
1713 NULL);
1714
1715 /* Close the extra handle */
1717
1718 if (!NT_SUCCESS(Status))
1719 {
1720 /* Fail */
1723 return Status;
1724 }
1725
1726 /* Finally, call its init function */
1727 DPRINT("Calling driver entrypoint at %p\n", InitializationFunction);
1728 Status = InitializationFunction(DriverObject, NULL);
1729 if (!NT_SUCCESS(Status))
1730 {
1731 /* If it didn't work, then kill the object */
1732 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", &LocalDriverName, Status);
1735 return Status;
1736 }
1737
1738 /* Windows does this fixup, keep it for compatibility */
1739 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1740 {
1741 /*
1742 * Make sure the driver didn't set any dispatch entry point to NULL!
1743 * Doing so is illegal; drivers shouldn't touch entry points they
1744 * do not implement.
1745 */
1746
1747 /* Check if it did so anyway */
1748 if (!DriverObject->MajorFunction[i])
1749 {
1750 /* Print a warning in the debug log */
1751 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1752 &DriverObject->DriverName, i);
1753
1754 /* Fix it up */
1755 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1756 }
1757 }
1758
1759 /* Return the Status */
1760 return Status;
1761}
1762
1763/*
1764 * @implemented
1765 */
1766VOID
1767NTAPI
1770{
1771 /* Simply dereference the Object */
1773}
1774
1775/*
1776 * @implemented
1777 */
1778VOID
1779NTAPI
1781 IN PDRIVER_REINITIALIZE ReinitRoutine,
1783{
1784 PDRIVER_REINIT_ITEM ReinitItem;
1785
1786 /* Allocate the entry */
1788 sizeof(DRIVER_REINIT_ITEM),
1789 TAG_REINIT);
1790 if (!ReinitItem) return;
1791
1792 /* Fill it out */
1793 ReinitItem->DriverObject = DriverObject;
1794 ReinitItem->ReinitRoutine = ReinitRoutine;
1795 ReinitItem->Context = Context;
1796
1797 /* Set the Driver Object flag and insert the entry into the list */
1800 &ReinitItem->ItemEntry,
1802}
1803
1804/*
1805 * @implemented
1806 */
1807VOID
1808NTAPI
1810 IN PDRIVER_REINITIALIZE ReinitRoutine,
1812{
1813 PDRIVER_REINIT_ITEM ReinitItem;
1814
1815 /* Allocate the entry */
1817 sizeof(DRIVER_REINIT_ITEM),
1818 TAG_REINIT);
1819 if (!ReinitItem) return;
1820
1821 /* Fill it out */
1822 ReinitItem->DriverObject = DriverObject;
1823 ReinitItem->ReinitRoutine = ReinitRoutine;
1824 ReinitItem->Context = Context;
1825
1826 /* Set the Driver Object flag and insert the entry into the list */
1829 &ReinitItem->ItemEntry,
1831}
1832
1833/*
1834 * @implemented
1835 */
1837NTAPI
1840 IN ULONG DriverObjectExtensionSize,
1841 OUT PVOID *DriverObjectExtension)
1842{
1843 KIRQL OldIrql;
1844 PIO_CLIENT_EXTENSION DriverExtensions, NewDriverExtension;
1845 BOOLEAN Inserted = FALSE;
1846
1847 /* Assume failure */
1848 *DriverObjectExtension = NULL;
1849
1850 /* Allocate the extension */
1851 NewDriverExtension = ExAllocatePoolWithTag(NonPagedPool,
1852 sizeof(IO_CLIENT_EXTENSION) +
1853 DriverObjectExtensionSize,
1855 if (!NewDriverExtension) return STATUS_INSUFFICIENT_RESOURCES;
1856
1857 /* Clear the extension for teh caller */
1858 RtlZeroMemory(NewDriverExtension,
1859 sizeof(IO_CLIENT_EXTENSION) + DriverObjectExtensionSize);
1860
1861 /* Acqure lock */
1863
1864 /* Fill out the extension */
1866
1867 /* Loop the current extensions */
1868 DriverExtensions = IoGetDrvObjExtension(DriverObject)->
1869 ClientDriverExtension;
1870 while (DriverExtensions)
1871 {
1872 /* Check if the identifier matches */
1873 if (DriverExtensions->ClientIdentificationAddress ==
1875 {
1876 /* We have a collision, break out */
1877 break;
1878 }
1879
1880 /* Go to the next one */
1881 DriverExtensions = DriverExtensions->NextExtension;
1882 }
1883
1884 /* Check if we didn't collide */
1885 if (!DriverExtensions)
1886 {
1887 /* Link this one in */
1888 NewDriverExtension->NextExtension =
1889 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1890 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension =
1891 NewDriverExtension;
1892 Inserted = TRUE;
1893 }
1894
1895 /* Release the lock */
1897
1898 /* Check if insertion failed */
1899 if (!Inserted)
1900 {
1901 /* Free the entry and fail */
1902 ExFreePoolWithTag(NewDriverExtension, TAG_DRIVER_EXTENSION);
1904 }
1905
1906 /* Otherwise, return the pointer */
1907 *DriverObjectExtension = NewDriverExtension + 1;
1908 return STATUS_SUCCESS;
1909}
1910
1911/*
1912 * @implemented
1913 */
1914PVOID
1915NTAPI
1918{
1919 KIRQL OldIrql;
1920 PIO_CLIENT_EXTENSION DriverExtensions;
1921
1922 /* Acquire lock */
1924
1925 /* Loop the list until we find the right one */
1926 DriverExtensions = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1927 while (DriverExtensions)
1928 {
1929 /* Check for a match */
1930 if (DriverExtensions->ClientIdentificationAddress ==
1932 {
1933 /* Break out */
1934 break;
1935 }
1936
1937 /* Keep looping */
1938 DriverExtensions = DriverExtensions->NextExtension;
1939 }
1940
1941 /* Release lock */
1943
1944 /* Return nothing or the extension */
1945 if (!DriverExtensions) return NULL;
1946 return DriverExtensions + 1;
1947}
1948
1951 _In_ HANDLE ServiceHandle,
1953{
1954 UNICODE_STRING ImagePath;
1956 PLDR_DATA_TABLE_ENTRY ModuleObject;
1958
1960 Status = IopGetRegistryValue(ServiceHandle, L"ImagePath", &kvInfo);
1961 if (NT_SUCCESS(Status))
1962 {
1963 if ((kvInfo->Type != REG_EXPAND_SZ && kvInfo->Type != REG_SZ) ||
1964 (kvInfo->DataLength < sizeof(UNICODE_NULL)) ||
1966 ((kvInfo->DataLength % sizeof(WCHAR)) != 0))
1967 {
1968 DPRINT1("ObjectName invalid (Type = %lu, DataLength = %lu)\n",
1969 kvInfo->Type,
1970 kvInfo->DataLength);
1971 ExFreePool(kvInfo);
1973 }
1974
1975 ImagePath.Length = (USHORT)(kvInfo->DataLength - sizeof(UNICODE_NULL));
1976 ImagePath.MaximumLength = kvInfo->DataLength;
1978 if (!ImagePath.Buffer)
1979 {
1980 ExFreePool(kvInfo);
1982 }
1983
1984 RtlMoveMemory(ImagePath.Buffer,
1985 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
1986 ImagePath.Length);
1987 ImagePath.Buffer[ImagePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
1988 ExFreePool(kvInfo);
1989 }
1990 else
1991 {
1992 return Status;
1993 }
1994
1995 /*
1996 * Normalize the image path for all later processing.
1997 */
1998 Status = IopNormalizeImagePath(&ImagePath, NULL);
1999 if (!NT_SUCCESS(Status))
2000 {
2001 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
2002 return Status;
2003 }
2004
2005 DPRINT("FullImagePath: '%wZ'\n", &ImagePath);
2006
2009
2010 /*
2011 * Load the driver module
2012 */
2013 DPRINT("Loading module from %wZ\n", &ImagePath);
2014 Status = MmLoadSystemImage(&ImagePath, NULL, NULL, 0, (PVOID)&ModuleObject, &BaseAddress);
2015 RtlFreeUnicodeString(&ImagePath);
2016
2017 if (!NT_SUCCESS(Status))
2018 {
2019 DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status);
2022 return Status;
2023 }
2024
2025 // Display the loading message
2026 ULONG infoLength;
2027 Status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
2029 {
2031 if (servName)
2032 {
2033 Status = ZwQueryKey(ServiceHandle,
2035 servName,
2036 infoLength,
2037 &infoLength);
2038 if (NT_SUCCESS(Status))
2039 {
2041 .Length = servName->NameLength,
2042 .MaximumLength = servName->NameLength,
2043 .Buffer = servName->Name
2044 };
2045
2047 }
2048 ExFreePoolWithTag(servName, TAG_IO);
2049 }
2050 }
2051
2052 NTSTATUS driverEntryStatus;
2053 Status = IopInitializeDriverModule(ModuleObject,
2054 ServiceHandle,
2056 &driverEntryStatus);
2057 if (!NT_SUCCESS(Status))
2058 {
2059 DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status);
2060 }
2061
2064
2065 return Status;
2066}
2067
2068static
2069VOID
2070NTAPI
2073{
2074 PLOAD_UNLOAD_PARAMS LoadParams = Parameter;
2075
2077
2078 if (LoadParams->DriverObject)
2079 {
2080 // unload request
2081 LoadParams->DriverObject->DriverUnload(LoadParams->DriverObject);
2082 LoadParams->Status = STATUS_SUCCESS;
2083 }
2084 else
2085 {
2086 // load request
2087 HANDLE serviceHandle;
2089 status = IopOpenRegistryKeyEx(&serviceHandle, NULL, LoadParams->RegistryPath, KEY_READ);
2090 if (!NT_SUCCESS(status))
2091 {
2092 LoadParams->Status = status;
2093 }
2094 else
2095 {
2096 LoadParams->Status = IopLoadDriver(serviceHandle, &LoadParams->DriverObject);
2097 ZwClose(serviceHandle);
2098 }
2099 }
2100
2101 if (LoadParams->SetEvent)
2102 {
2103 KeSetEvent(&LoadParams->Event, 0, FALSE);
2104 }
2105}
2106
2120{
2121 LOAD_UNLOAD_PARAMS LoadParams;
2122
2123 /* Prepare parameters block */
2124 LoadParams.RegistryPath = RegistryPath;
2125 LoadParams.DriverObject = *DriverObject;
2126
2128 {
2129 LoadParams.SetEvent = TRUE;
2131
2132 /* Initialize and queue a work item */
2133 ExInitializeWorkItem(&LoadParams.WorkItem, IopLoadUnloadDriverWorker, &LoadParams);
2135
2136 /* And wait till it completes */
2138 }
2139 else
2140 {
2141 /* If we're already in a system process, call it right here */
2142 LoadParams.SetEvent = FALSE;
2143 IopLoadUnloadDriverWorker(&LoadParams);
2144 }
2145
2146 return LoadParams.Status;
2147}
2148
2149/*
2150 * NtLoadDriver
2151 *
2152 * Loads a device driver.
2153 *
2154 * Parameters
2155 * DriverServiceName
2156 * Name of the service to load (registry key).
2157 *
2158 * Return Value
2159 * Status
2160 *
2161 * Status
2162 * implemented
2163 */
2166{
2167 UNICODE_STRING CapturedServiceName = { 0, 0, NULL };
2171
2172 PAGED_CODE();
2173
2175
2176 /* Need the appropriate priviliege */
2178 {
2179 DPRINT1("No load privilege!\n");
2181 }
2182
2183 /* Capture the service name */
2184 Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
2186 DriverServiceName);
2187 if (!NT_SUCCESS(Status))
2188 {
2189 return Status;
2190 }
2191
2192 DPRINT("NtLoadDriver('%wZ')\n", &CapturedServiceName);
2193
2194 /* We need a service name */
2195 if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
2196 {
2197 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2199 }
2200
2201 /* Load driver and call its entry point */
2203 Status = IopDoLoadUnloadDriver(&CapturedServiceName, &DriverObject);
2204
2205 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2206 return Status;
2207}
2208
2209/*
2210 * NtUnloadDriver
2211 *
2212 * Unloads a legacy device driver.
2213 *
2214 * Parameters
2215 * DriverServiceName
2216 * Name of the service to unload (registry key).
2217 *
2218 * Return Value
2219 * Status
2220 *
2221 * Status
2222 * implemented
2223 */
2224
2227{
2228 return IopUnloadDriver(DriverServiceName, FALSE);
2229}
2230
2231/* EOF */
#define PAGED_CODE()
#define CODE_SEG(...)
NTSTATUS NtUnloadDriver(IN PUNICODE_STRING DriverServiceName)
Definition: driver.c:2226
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
_In_ PVOID _In_ ULONG _Out_ PVOID _In_ ULONG _Inout_ PULONG _In_ KPROCESSOR_MODE PreviousMode
unsigned char BOOLEAN
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char * ModuleName
Definition: acpixf.h:1280
#define OBJ_NAME_PATH_SEPARATOR
Definition: arcname_tests.c:25
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
static WCHAR ServiceName[]
Definition: browser.c:19
Definition: bufpool.h:45
PUNICODE_STRING *NTAPI CmGetSystemDriverList(VOID)
Definition: cmsysini.c:1843
char TextBuffer[BUFFERLEN]
Definition: combotst.c:45
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_NO_MEMORY
Definition: d3dkmdt.h:51
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define wcsrchr
Definition: compat.h:16
DWORD RVA
Definition: compat.h:1262
#define RtlImageNtHeader
Definition: compat.h:806
static const unsigned char pc1[56]
Definition: des.c:54
static const unsigned char pc2[48]
Definition: des.c:68
static const WCHAR DeviceInstance[]
Definition: interface.c:28
#define swprintf
Definition: precomp.h:40
static const WCHAR Cleanup[]
Definition: register.c:80
#define UNIMPLEMENTED_DBGBREAK(...)
Definition: debug.h:57
#define __drv_allocatesMem(kind)
Definition: driverspecs.h:257
#define InsertHeadList(ListHead, Entry)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
UCHAR KIRQL
Definition: env_spec_w32.h:591
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define MAXIMUM_FILENAME_LENGTH
Definition: env_spec_w32.h:41
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define ExAcquireResourceExclusiveLite(res, wait)
Definition: env_spec_w32.h:615
#define NonPagedPool
Definition: env_spec_w32.h:307
ULONG ERESOURCE
Definition: env_spec_w32.h:594
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define PagedPool
Definition: env_spec_w32.h:308
@ Success
Definition: eventcreate.c:712
#define ExGetPreviousMode
Definition: ex.h:143
struct _FileName FileName
Definition: fatprocs.h:897
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment KeRaiseIrqlToDpcLevel
Definition: CrNtStubs.h:68
Status
Definition: gdiplustypes.h:25
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLfloat GLfloat p
Definition: glext.h:8902
GLuint GLuint num
Definition: glext.h:9618
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define DbgPrint
Definition: hal.h:12
static LPWSTR ClientIdentificationAddress
Definition: hidclass.c:16
NTHALAPI VOID NTAPI HalDisplayString(PUCHAR String)
VOID NTAPI InbvIndicateProgress(VOID)
Gives some progress feedback, without specifying any explicit number of progress steps or percentage....
Definition: inbv.c:629
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define OBJ_PERMANENT
Definition: winternl.h:226
@ KeyNameInformation
Definition: winternl.h:831
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
#define KeLeaveCriticalRegion()
Definition: ke_x.h:119
#define KeEnterCriticalRegion()
Definition: ke_x.h:88
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:28
#define REG_SZ
Definition: layer.c:22
#define DRIVER_ROOT_NAME
Definition: ldr.h:5
#define FILESYSTEM_ROOT_NAME
Definition: ldr.h:6
#define LDRP_DRIVER_DEPENDENT_DLL
Definition: ldrtypes.h:60
#define MM_SYSLDR_NO_IMPORTS
Definition: miarm.h:214
#define ASSERT(a)
Definition: mode.c:44
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _Post_notnull_
Definition: ms_sal.h:701
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4715
#define KeGetPreviousMode()
Definition: ketypes.h:1115
#define KernelMode
Definition: asm.h:38
#define DOE_UNLOAD_PENDING
Definition: iotypes.h:149
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
_In_ const STRING * String2
Definition: rtlfuncs.h:2396
_In_ const STRING _In_ BOOLEAN CaseInSensitive
Definition: rtlfuncs.h:2429
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4203
DRIVER_INFORMATION DriverInfo
Definition: main.c:59
WCHAR NTAPI RtlUpcaseUnicodeChar(_In_ WCHAR Source)
Definition: nlsboot.c:176
#define _Out_opt_
Definition: no_sal2.h:214
#define _Inout_
Definition: no_sal2.h:162
#define _At_(t, a)
Definition: no_sal2.h:40
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define _In_opt_
Definition: no_sal2.h:212
#define _When_(c, a)
Definition: no_sal2.h:38
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
@ KeyBasicInformation
Definition: nt_native.h:1131
#define FILE_READ_DATA
Definition: nt_native.h:628
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
#define RTL_REGISTRY_ABSOLUTE
Definition: nt_native.h:161
#define FASTCALL
Definition: nt_native.h:50
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
#define UNICODE_NULL
#define UNICODE_STRING_MAX_BYTES
@ NotificationEvent
#define IMAGE_DLLCHARACTERISTICS_WDM_DRIVER
Definition: ntimage.h:462
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1822
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:885
NTSTATUS NTAPI RawFsDriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: rawfs.c:1193
@ PiActionAddBootDevices
Definition: io.h:529
@ PiActionEnumRootDevices
Definition: io.h:527
@ PiActionEnumDeviceTree
Definition: io.h:526
USHORT NTAPI PpInitGetGroupOrderIndex(IN HANDLE ServiceHandle)
Definition: pnpinit.c:149
USHORT NTAPI PipGetDriverTagPriority(IN HANDLE ServiceHandle)
Definition: pnpinit.c:192
NTSTATUS NTAPI IopGetRegistryValue(IN HANDLE Handle, IN PWSTR ValueName, OUT PKEY_VALUE_FULL_INFORMATION *Information)
Definition: pnpmgr.c:1036
#define IoGetDrvObjExtension(DriverObject)
Definition: io.h:136
VOID PiQueueDeviceAction(_In_ PDEVICE_OBJECT DeviceObject, _In_ DEVICE_ACTION Action, _In_opt_ PKEVENT CompletionEvent, _Out_opt_ NTSTATUS *CompletionStatus)
Queue a device operation to a worker thread.
Definition: devaction.c:2668
PDEVICE_NODE IopRootDeviceNode
Definition: devnode.c:18
NTSTATUS PiPerformSyncDeviceAction(_In_ PDEVICE_OBJECT DeviceObject, _In_ DEVICE_ACTION Action)
Perfom a device operation synchronously via PiQueueDeviceAction.
Definition: devaction.c:2727
VOID NTAPI IopReadyDeviceObjects(IN PDRIVER_OBJECT Driver)
Definition: device.c:34
#define IoGetDevObjExtension(DeviceObject)
Definition: io.h:129
NTSTATUS NTAPI MmLoadSystemImage(IN PUNICODE_STRING FileName, IN PUNICODE_STRING NamePrefix OPTIONAL, IN PUNICODE_STRING LoadedName OPTIONAL, IN ULONG Flags, OUT PVOID *ModuleObject, OUT PVOID *ImageBaseAddress)
Definition: sysldr.c:2949
VOID NTAPI MmFreeDriverInitialization(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
Definition: sysldr.c:1673
NTSTATUS NTAPI MmCallDllInitialize(_In_ PLDR_DATA_TABLE_ENTRY LdrEntry, _In_ PLIST_ENTRY ModuleListHead)
Definition: sysldr.c:433
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:945
#define RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END
Definition: rtl.h:25
NTSTATUS NTAPI RtlFindCharInUnicodeString(_In_ ULONG Flags, _In_ PCUNICODE_STRING SearchString, _In_ PCUNICODE_STRING MatchString, _Out_ PUSHORT Position)
const LUID SeLoadDriverPrivilege
Definition: priv.c:29
LIST_ENTRY DriverReinitListHead
Definition: driver.c:22
struct _LOAD_UNLOAD_PARAMS LOAD_UNLOAD_PARAMS
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:34
VOID NTAPI IopReinitializeBootDrivers(VOID)
Definition: driver.c:1544
static VOID FASTCALL IopDisplayLoadingMessage(_In_ PCUNICODE_STRING ServiceName)
Displays a driver-loading message in SOS mode.
Definition: driver.c:334
PDEVICE_OBJECT IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance)
Definition: plugplay.c:206
NTSTATUS IopLoadDriver(_In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1950
PVOID NTAPI IoGetDriverObjectExtension(IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress)
Definition: driver.c:1916
static BOOLEAN IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
Definition: driver.c:813
KEVENT PiEnumerationFinished
Definition: devaction.c:50
VOID NTAPI IoRegisterDriverReinitialization(IN PDRIVER_OBJECT DriverObject, IN PDRIVER_REINITIALIZE ReinitRoutine, IN PVOID Context)
Definition: driver.c:1809
ERESOURCE IopDriverLoadResource
Definition: driver.c:20
struct _LOAD_UNLOAD_PARAMS * PLOAD_UNLOAD_PARAMS
NTSTATUS NTAPI IoAllocateDriverObjectExtension(IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress, IN ULONG DriverObjectExtensionSize, OUT PVOID *DriverObjectExtension)
Definition: driver.c:1838
static const WCHAR ServicesKeyName[]
Definition: driver.c:32
NTSTATUS NTAPI IoCreateDriver(_In_opt_ PUNICODE_STRING DriverName, _In_ PDRIVER_INITIALIZE InitializationFunction)
Definition: driver.c:1588
BOOLEAN PnpSystemInit
Definition: iomgr.c:17
VOID FASTCALL IopInitializeSystemDrivers(VOID)
Definition: driver.c:1222
PLIST_ENTRY IopGroupTable
Definition: driver.c:41
PLIST_ENTRY DriverBootReinitTailEntry
Definition: driver.c:26
KSPIN_LOCK DriverBootReinitListLock
Definition: driver.c:28
NTSTATUS NTAPI IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
Definition: driver.c:1281
NTSTATUS NTAPI MiResolveImageReferences(IN PVOID ImageBase, IN PUNICODE_STRING ImageFileDirectory, IN PUNICODE_STRING NamePrefix OPTIONAL, OUT PCHAR *MissingApi, OUT PWCHAR *MissingDriver, OUT PLOAD_IMPORTS *LoadImports)
Definition: sysldr.c:1034
VOID NTAPI IoDeleteDriver(_In_ PDRIVER_OBJECT DriverObject)
Definition: driver.c:1768
NTSTATUS NTAPI IopInvalidDeviceRequest(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: driver.c:65
VOID NTAPI IoRegisterBootDriverReinitialization(IN PDRIVER_OBJECT DriverObject, IN PDRIVER_REINITIALIZE ReinitRoutine, IN PVOID Context)
Definition: driver.c:1780
NTSTATUS IopInitializeDriverModule(_In_ PLDR_DATA_TABLE_ENTRY ModuleObject, _In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *OutDriverObject, _Out_ NTSTATUS *DriverEntryStatus)
Initialize a loaded driver.
Definition: driver.c:449
LIST_ENTRY DriverBootReinitListHead
Definition: driver.c:27
KSPIN_LOCK DriverReinitListLock
Definition: driver.c:23
VOID NTAPI IopDeleteDriver(IN PVOID ObjectBody)
Definition: driver.c:77
NTSTATUS IopGetDriverNames(_In_ HANDLE ServiceHandle, _Out_ PUNICODE_STRING DriverName, _Out_opt_ PUNICODE_STRING ServiceName)
Definition: driver.c:123
VOID FASTCALL IopInitializeBootDrivers(VOID)
Definition: driver.c:1035
USHORT IopGroupIndex
Definition: driver.c:40
VOID NTAPI IopReinitializeDrivers(VOID)
Definition: driver.c:1508
NTSTATUS IopDoLoadUnloadDriver(_In_opt_ PUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Process load and unload driver operations. This is mostly for NtLoadDriver and NtUnloadDriver,...
Definition: driver.c:2117
NTSTATUS NTAPI NtLoadDriver(IN PUNICODE_STRING DriverServiceName)
Definition: driver.c:2165
static VOID NTAPI IopLoadUnloadDriverWorker(_Inout_ PVOID Parameter)
Definition: driver.c:2071
NTSTATUS NTAPI LdrProcessDriverModule(PLDR_DATA_TABLE_ENTRY LdrEntry, PUNICODE_STRING FileName, PLDR_DATA_TABLE_ENTRY *ModuleObject)
Definition: driver.c:728
NTSTATUS FASTCALL IopNormalizeImagePath(_Inout_ _When_(return >=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem))) PUNICODE_STRING ImagePath, _In_ PUNICODE_STRING ServiceName)
Definition: driver.c:375
PLIST_ENTRY DriverReinitTailEntry
Definition: driver.c:24
UNICODE_STRING IopHardwareDatabaseKey
Definition: driver.c:30
static BOOLEAN IopSuffixUnicodeString(_In_ PCUNICODE_STRING String1, _In_ PCUNICODE_STRING String2, _In_ BOOLEAN CaseInSensitive)
Determines whether String1 may be a suffix of String2.
Definition: driver.c:286
BOOLEAN PnPBootDriversLoaded
Definition: pnpinit.c:20
#define IoCompleteRequest
Definition: irp.c:1240
BOOLEAN NTAPI SeSinglePrivilegeCheck(_In_ LUID PrivilegeValue, _In_ KPROCESSOR_MODE PreviousMode)
Checks if a single privilege is present in the context of the calling thread.
Definition: priv.c:744
#define STATUS_DELETE_PENDING
Definition: ntstatus.h:322
#define STATUS_FAILED_DRIVER_ENTRY
Definition: ntstatus.h:911
#define STATUS_ILL_FORMED_SERVICE_ENTRY
Definition: ntstatus.h:588
NTSTRSAFEVAPI RtlStringCbPrintfA(_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PSTR pszDest, _In_ size_t cbDest, _In_ _Printf_format_string_ NTSTRSAFE_PCSTR pszFormat,...)
Definition: ntstrsafe.h:1148
NTSTRSAFEVAPI RtlStringCbPrintfW(_Out_writes_bytes_(cbDest) _Always_(_Post_z_) NTSTRSAFE_PWSTR pszDest, _In_ size_t cbDest, _In_ _Printf_format_string_ NTSTRSAFE_PCWSTR pszFormat,...)
Definition: ntstrsafe.h:1173
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI ObInsertObject(IN PVOID Object, IN PACCESS_STATE AccessState OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG ObjectPointerBias, OUT PVOID *NewObject OPTIONAL, OUT PHANDLE Handle)
Definition: obhandle.c:2935
NTSTATUS NTAPI ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL, IN POBJECT_TYPE Type, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, IN ULONG ObjectSize, IN ULONG PagedPoolCharge OPTIONAL, IN ULONG NonPagedPoolCharge OPTIONAL, OUT PVOID *Object)
Definition: oblife.c:1039
VOID NTAPI ObMakeTemporaryObject(IN PVOID ObjectBody)
Definition: oblife.c:1449
NTSTATUS NTAPI ObReferenceObjectByName(IN PUNICODE_STRING ObjectPath, IN ULONG Attributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext, OUT PVOID *ObjectPtr)
Definition: obref.c:409
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:31
unsigned short USHORT
Definition: pedump.c:61
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:21
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
#define REG_DWORD
Definition: sdbapi.c:596
static __inline NTSTATUS ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest, IN KPROCESSOR_MODE CurrentMode, IN const UNICODE_STRING *UnsafeSrc)
Definition: probe.h:142
static __inline VOID ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString, IN KPROCESSOR_MODE CurrentMode)
Definition: probe.h:239
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
#define DPRINT
Definition: sndvol32.h:73
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
base of all file and directory entries
Definition: entries.h:83
Definition: arc.h:246
UNICODE_STRING RegistryPath
Definition: arc.h:249
struct _LDR_DATA_TABLE_ENTRY * LdrEntry
Definition: arc.h:250
PDEVICE_OBJECT PhysicalDeviceObject
Definition: iotypes.h:1010
struct _DRIVER_OBJECT * DriverObject
Definition: iotypes.h:2219
UNICODE_STRING ServiceKeyName
Definition: iotypes.h:2222
HANDLE ServiceHandle
Definition: io.h:406
PBOOT_DRIVER_LIST_ENTRY DataTableEntry
Definition: io.h:405
LIST_ENTRY Link
Definition: io.h:403
USHORT TagPosition
Definition: io.h:407
PUNICODE_STRING HardwareDatabase
Definition: iotypes.h:2284
PVOID DriverStart
Definition: iotypes.h:2279
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1]
Definition: iotypes.h:2289
PVOID DriverSection
Definition: iotypes.h:2281
CSHORT Size
Definition: iotypes.h:2276
ULONG DriverSize
Definition: iotypes.h:2280
PDRIVER_INITIALIZE DriverInit
Definition: iotypes.h:2286
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2282
CSHORT Type
Definition: iotypes.h:2275
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2277
PDRIVER_UNLOAD DriverUnload
Definition: iotypes.h:2288
UNICODE_STRING DriverName
Definition: iotypes.h:2283
PDRIVER_OBJECT DriverObject
Definition: io.h:449
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:450
LIST_ENTRY ItemEntry
Definition: io.h:448
PVOID Context
Definition: io.h:451
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
PVOID ClientIdentificationAddress
Definition: iotypes.h:984
struct _IO_CLIENT_EXTENSION * NextExtension
Definition: iotypes.h:983
ULONG LowPart
Definition: ketypes.h:929
Definition: btrfs_drv.h:1876
ULONG Flags
Definition: ntddk_ex.h:207
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:149
Definition: typedefs.h:120
struct _LIST_ENTRY * Blink
Definition: typedefs.h:122
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
LIST_ENTRY BootDriverListHead
Definition: arc.h:542
LIST_ENTRY LoadOrderListHead
Definition: arc.h:540
PSTR ArcBootDeviceName
Definition: arc.h:550
struct _SETUP_LOADER_BLOCK * SetupLdrBlock
Definition: arc.h:558
WORK_QUEUE_ITEM WorkItem
Definition: driver.c:50
NTSTATUS Status
Definition: driver.c:48
BOOLEAN SetEvent
Definition: driver.c:53
PUNICODE_STRING RegistryPath
Definition: driver.c:49
PDRIVER_OBJECT DriverObject
Definition: driver.c:52
USHORT MaximumLength
Definition: env_spec_w32.h:370
ACPI_SIZE Length
Definition: actypes.h:1053
Definition: ps.c:97
#define TAG_REINIT
Definition: tag.h:83
#define TAG_LDR_WSTR
Definition: tag.h:101
#define TAG_RTLREGISTRY
Definition: tag.h:95
#define TAG_IO
Definition: tag.h:79
#define TAG_DRIVER_EXTENSION
Definition: tag.h:61
char serviceName[]
Definition: tftpd.cpp:34
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
uint16_t * PWSTR
Definition: typedefs.h:56
#define NTAPI
Definition: typedefs.h:36
int32_t INT
Definition: typedefs.h:58
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
uint16_t * PWCHAR
Definition: typedefs.h:56
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define STATUS_OBJECT_NAME_COLLISION
Definition: udferr_usr.h:150
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
static int Link(const char **args)
Definition: vfdcmd.c:2414
_In_ WDFCOLLECTION _In_ ULONG Index
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
_In_ LPWSTR _In_ ULONG _In_ ULONG _In_ ULONG _Out_ DEVINFO _In_ HDEV _In_ LPWSTR _In_ HANDLE hDriver
Definition: winddi.h:3557
BOOLEAN SosEnabled
Definition: winldr.c:31
VOID NTAPI ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem, IN WORK_QUEUE_TYPE QueueType)
Definition: work.c:723
_In_ PVOID _Out_opt_ PULONG_PTR _Outptr_opt_ PCUNICODE_STRING * ObjectName
Definition: cmfuncs.h:64
#define SERVICE_RECOGNIZER_DRIVER
Definition: cmtypes.h:956
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
@ DelayedWorkQueue
Definition: extypes.h:190
#define DRVO_BUILTIN_DRIVER
Definition: iotypes.h:2227
#define IO_NO_INCREMENT
Definition: iotypes.h:598
struct _DRIVER_OBJECT DRIVER_OBJECT
VOID(NTAPI * PDRIVER_REINITIALIZE)(_In_ struct _DRIVER_OBJECT *DriverObject, _In_opt_ PVOID Context, _In_ ULONG Count)
Definition: iotypes.h:4455
struct _DRIVER_EXTENSION * PDRIVER_EXTENSION
#define DRVO_BOOTREINIT_REGISTERED
Definition: iotypes.h:4471
#define DRVO_UNLOAD_INVOKED
Definition: iotypes.h:2225
#define DRVO_REINIT_REGISTERED
Definition: iotypes.h:4469
#define IO_TYPE_DRIVER
#define DRVO_LEGACY_DRIVER
Definition: iotypes.h:2226
#define IRP_MJ_MAXIMUM_FUNCTION
DRIVER_INITIALIZE * PDRIVER_INITIALIZE
Definition: iotypes.h:2235
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778
@ UserRequest
Definition: ketypes.h:421
@ Executive
Definition: ketypes.h:415
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define ROUND_TO_PAGES(Size)
#define ObDereferenceObject
Definition: obfuncs.h:203
#define PsGetCurrentProcess
Definition: psfuncs.h:17
#define NT_ASSERT
Definition: rtlfuncs.h:3327
#define NT_VERIFY(exp)
Definition: rtlfuncs.h:3304
_Inout_opt_ PVOID Parameter
Definition: rtltypes.h:336
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175