ReactOS 0.4.15-dev-8116-gf69e256
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 || kvInfo->DataLength == 0)
140 {
141 ExFreePool(kvInfo);
143 }
144
145 driverName.Length = kvInfo->DataLength - sizeof(UNICODE_NULL);
146 driverName.MaximumLength = kvInfo->DataLength;
148 if (!driverName.Buffer)
149 {
150 ExFreePool(kvInfo);
152 }
153
154 RtlMoveMemory(driverName.Buffer,
155 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
156 driverName.Length);
157 driverName.Buffer[driverName.Length / sizeof(WCHAR)] = UNICODE_NULL;
158 ExFreePool(kvInfo);
159 }
160
161 /* Check whether we need to get ServiceName as well, either to construct
162 * the driver name (because we could not use "ObjectName"), or because
163 * it is requested by the caller. */
164 PKEY_BASIC_INFORMATION basicInfo = NULL;
165 if (!NT_SUCCESS(status) || ServiceName != NULL)
166 {
167 /* Retrieve the necessary buffer size */
168 ULONG infoLength;
169 status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
171 {
173 goto Cleanup;
174 }
175
176 /* Allocate the buffer and retrieve the data */
177 basicInfo = ExAllocatePoolWithTag(PagedPool, infoLength, TAG_IO);
178 if (!basicInfo)
179 {
181 goto Cleanup;
182 }
183
184 status = ZwQueryKey(ServiceHandle, KeyBasicInformation, basicInfo, infoLength, &infoLength);
185 if (!NT_SUCCESS(status))
186 {
187 goto Cleanup;
188 }
189
190 serviceName.Length = basicInfo->NameLength;
191 serviceName.MaximumLength = basicInfo->NameLength;
192 serviceName.Buffer = basicInfo->Name;
193 }
194
195 /* 2. There is no "ObjectName" - construct it ourselves. Depending on the driver type,
196 * it will be either "\Driver<ServiceName>" or "\FileSystem<ServiceName>" */
197 if (driverName.Buffer == NULL)
198 {
199 ASSERT(basicInfo); // Container for serviceName
200
201 /* Retrieve the driver type */
202 ULONG driverType;
203 status = IopGetRegistryValue(ServiceHandle, L"Type", &kvInfo);
204 if (!NT_SUCCESS(status))
205 {
206 goto Cleanup;
207 }
208 if (kvInfo->Type != REG_DWORD || kvInfo->DataLength != sizeof(ULONG))
209 {
210 ExFreePool(kvInfo);
212 goto Cleanup;
213 }
214
215 RtlMoveMemory(&driverType,
216 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
217 sizeof(ULONG));
218 ExFreePool(kvInfo);
219
220 /* Compute the necessary driver name string size */
221 if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
222 driverName.MaximumLength = sizeof(FILESYSTEM_ROOT_NAME);
223 else
224 driverName.MaximumLength = sizeof(DRIVER_ROOT_NAME);
225
226 driverName.MaximumLength += serviceName.Length;
227 driverName.Length = 0;
228
229 /* Allocate and build it */
231 if (!driverName.Buffer)
232 {
234 goto Cleanup;
235 }
236
237 if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
239 else
241
243 }
244
245 if (ServiceName != NULL)
246 {
247 ASSERT(basicInfo); // Container for serviceName
248
249 /* Allocate a copy for the caller */
251 if (!buf)
252 {
254 goto Cleanup;
255 }
256 RtlMoveMemory(buf, serviceName.Buffer, serviceName.Length);
257 ServiceName->MaximumLength = serviceName.Length;
258 ServiceName->Length = serviceName.Length;
259 ServiceName->Buffer = buf;
260 }
261
262 *DriverName = driverName;
264
265Cleanup:
266 if (basicInfo)
267 ExFreePoolWithTag(basicInfo, TAG_IO);
268
269 if (!NT_SUCCESS(status) && driverName.Buffer)
270 ExFreePoolWithTag(driverName.Buffer, TAG_IO);
271
272 return status;
273}
274
279static BOOLEAN
281 _In_ PCUNICODE_STRING String1,
284{
285 PWCHAR pc1, pc2;
286 ULONG NumChars;
287
288 if (String2->Length < String1->Length)
289 return FALSE;
290
291 NumChars = String1->Length / sizeof(WCHAR);
292 pc1 = String1->Buffer;
293 pc2 = &String2->Buffer[String2->Length / sizeof(WCHAR) - NumChars];
294
295 if (pc1 && pc2)
296 {
297 if (CaseInSensitive)
298 {
299 while (NumChars--)
300 {
301 if (RtlUpcaseUnicodeChar(*pc1++) !=
303 {
304 return FALSE;
305 }
306 }
307 }
308 else
309 {
310 while (NumChars--)
311 {
312 if (*pc1++ != *pc2++)
313 return FALSE;
314 }
315 }
316
317 return TRUE;
318 }
319
320 return FALSE;
321}
322
326static VOID
330{
331 extern BOOLEAN SosEnabled; // See ex/init.c
332 static const UNICODE_STRING DotSys = RTL_CONSTANT_STRING(L".SYS");
333 CHAR TextBuffer[256];
334
335 if (!SosEnabled) return;
336 if (!KeLoaderBlock) return;
338 "%s%sSystem32\\Drivers\\%wZ%s\r\n",
343 ? "" : ".SYS");
345}
346
347/*
348 * IopNormalizeImagePath
349 *
350 * Normalize an image path to contain complete path.
351 *
352 * Parameters
353 * ImagePath
354 * The input path and on exit the result path. ImagePath.Buffer
355 * must be allocated by ExAllocatePool on input. Caller is responsible
356 * for freeing the buffer when it's no longer needed.
357 *
358 * ServiceName
359 * Name of the service that ImagePath belongs to.
360 *
361 * Return Value
362 * Status
363 *
364 * Remarks
365 * The input image path isn't freed on error.
366 */
370 _Inout_ _When_(return>=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem)))
371 PUNICODE_STRING ImagePath,
373{
374 UNICODE_STRING SystemRootString = RTL_CONSTANT_STRING(L"\\SystemRoot\\");
375 UNICODE_STRING DriversPathString = RTL_CONSTANT_STRING(L"\\SystemRoot\\System32\\drivers\\");
376 UNICODE_STRING DotSysString = RTL_CONSTANT_STRING(L".sys");
377 UNICODE_STRING InputImagePath;
378
379 DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
380
381 InputImagePath = *ImagePath;
382 if (InputImagePath.Length == 0)
383 {
384 ImagePath->Length = 0;
385 ImagePath->MaximumLength = DriversPathString.Length +
386 ServiceName->Length +
387 DotSysString.Length +
388 sizeof(UNICODE_NULL);
389 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
390 ImagePath->MaximumLength,
391 TAG_IO);
392 if (ImagePath->Buffer == NULL)
393 return STATUS_NO_MEMORY;
394
395 RtlCopyUnicodeString(ImagePath, &DriversPathString);
397 RtlAppendUnicodeStringToString(ImagePath, &DotSysString);
398 }
399 else if (InputImagePath.Buffer[0] != L'\\')
400 {
401 ImagePath->Length = 0;
402 ImagePath->MaximumLength = SystemRootString.Length +
403 InputImagePath.Length +
404 sizeof(UNICODE_NULL);
405 ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
406 ImagePath->MaximumLength,
407 TAG_IO);
408 if (ImagePath->Buffer == NULL)
409 return STATUS_NO_MEMORY;
410
411 RtlCopyUnicodeString(ImagePath, &SystemRootString);
412 RtlAppendUnicodeStringToString(ImagePath, &InputImagePath);
413
414 /* Free caller's string */
416 }
417
418 DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
419
420 return STATUS_SUCCESS;
421}
422
444 _In_ PLDR_DATA_TABLE_ENTRY ModuleObject,
445 _In_ HANDLE ServiceHandle,
446 _Out_ PDRIVER_OBJECT *OutDriverObject,
447 _Out_ NTSTATUS *DriverEntryStatus)
448{
451
452 PAGED_CODE();
453
454 Status = IopGetDriverNames(ServiceHandle, &DriverName, &ServiceName);
455 if (!NT_SUCCESS(Status))
456 {
457 MmUnloadSystemImage(ModuleObject);
458 return Status;
459 }
460
461 DPRINT("Driver name: '%wZ'\n", &DriverName);
462
463 /*
464 * Retrieve the driver's PE image NT header and perform some sanity checks.
465 * NOTE: We suppose that since the driver has been successfully loaded,
466 * its NT and optional headers are all valid and have expected sizes.
467 */
468 PIMAGE_NT_HEADERS NtHeaders = RtlImageNtHeader(ModuleObject->DllBase);
469 ASSERT(NtHeaders);
470 // NOTE: ModuleObject->SizeOfImage is actually (number of PTEs)*PAGE_SIZE.
471 ASSERT(ModuleObject->SizeOfImage == ROUND_TO_PAGES(NtHeaders->OptionalHeader.SizeOfImage));
472 ASSERT(ModuleObject->EntryPoint == RVA(ModuleObject->DllBase, NtHeaders->OptionalHeader.AddressOfEntryPoint));
473
474 /* Obtain the registry path for the DriverInit routine */
475 PKEY_NAME_INFORMATION nameInfo;
476 ULONG infoLength;
477 Status = ZwQueryKey(ServiceHandle, KeyNameInformation, NULL, 0, &infoLength);
479 {
480 nameInfo = ExAllocatePoolWithTag(NonPagedPool, infoLength, TAG_IO);
481 if (nameInfo)
482 {
483 Status = ZwQueryKey(ServiceHandle,
485 nameInfo,
486 infoLength,
487 &infoLength);
488 if (NT_SUCCESS(Status))
489 {
490 RegistryPath.Length = nameInfo->NameLength;
491 RegistryPath.MaximumLength = nameInfo->NameLength;
492 RegistryPath.Buffer = nameInfo->Name;
493 }
494 else
495 {
496 ExFreePoolWithTag(nameInfo, TAG_IO);
497 }
498 }
499 else
500 {
502 }
503 }
504 else
505 {
507 }
508
509 if (!NT_SUCCESS(Status))
510 {
512 RtlFreeUnicodeString(&DriverName);
513 MmUnloadSystemImage(ModuleObject);
514 return Status;
515 }
516
517 /* Create the driver object */
518 ULONG ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
519 OBJECT_ATTRIBUTES objAttrs;
520 PDRIVER_OBJECT driverObject;
522 &DriverName,
524 NULL,
525 NULL);
526
529 &objAttrs,
531 NULL,
532 ObjectSize,
533 0,
534 0,
535 (PVOID*)&driverObject);
536 if (!NT_SUCCESS(Status))
537 {
538 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
540 RtlFreeUnicodeString(&DriverName);
541 MmUnloadSystemImage(ModuleObject);
542 DPRINT1("Error while creating driver object \"%wZ\" status %x\n", &DriverName, Status);
543 return Status;
544 }
545
546 DPRINT("Created driver object 0x%p for \"%wZ\"\n", driverObject, &DriverName);
547
548 RtlZeroMemory(driverObject, ObjectSize);
549 driverObject->Type = IO_TYPE_DRIVER;
550 driverObject->Size = sizeof(DRIVER_OBJECT);
551
552 /* Set the legacy flag if this is not a WDM driver */
554 driverObject->Flags |= DRVO_LEGACY_DRIVER;
555
556 driverObject->DriverSection = ModuleObject;
557 driverObject->DriverStart = ModuleObject->DllBase;
558 driverObject->DriverSize = ModuleObject->SizeOfImage;
559 driverObject->DriverInit = ModuleObject->EntryPoint;
561 driverObject->DriverExtension = (PDRIVER_EXTENSION)(driverObject + 1);
562 driverObject->DriverExtension->DriverObject = driverObject;
563
564 /* Loop all Major Functions */
565 for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
566 {
567 /* Invalidate each function */
568 driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
569 }
570
571 /* Add the Object and get its handle */
573 Status = ObInsertObject(driverObject, NULL, FILE_READ_DATA, 0, NULL, &hDriver);
574 if (!NT_SUCCESS(Status))
575 {
576 ExFreePoolWithTag(nameInfo, TAG_IO);
578 RtlFreeUnicodeString(&DriverName);
579 return Status;
580 }
581
582 /* Now reference it */
584 0,
587 (PVOID*)&driverObject,
588 NULL);
589
590 /* Close the extra handle */
592
593 if (!NT_SUCCESS(Status))
594 {
595 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
597 RtlFreeUnicodeString(&DriverName);
598 return Status;
599 }
600
601 /* Set up the service key name buffer */
602 UNICODE_STRING serviceKeyName;
603 serviceKeyName.Length = 0;
604 // NULL-terminate for Windows compatibility
605 serviceKeyName.MaximumLength = ServiceName.MaximumLength + sizeof(UNICODE_NULL);
607 serviceKeyName.MaximumLength,
608 TAG_IO);
609 if (!serviceKeyName.Buffer)
610 {
611 ObMakeTemporaryObject(driverObject);
612 ObDereferenceObject(driverObject);
613 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
615 RtlFreeUnicodeString(&DriverName);
617 }
618
619 /* Copy the name and set it in the driver extension */
620 RtlCopyUnicodeString(&serviceKeyName, &ServiceName);
622 driverObject->DriverExtension->ServiceKeyName = serviceKeyName;
623
624 /* Make a copy of the driver name to store in the driver object */
625 UNICODE_STRING driverNamePaged;
626 driverNamePaged.Length = 0;
627 // NULL-terminate for Windows compatibility
628 driverNamePaged.MaximumLength = DriverName.MaximumLength + sizeof(UNICODE_NULL);
629 driverNamePaged.Buffer = ExAllocatePoolWithTag(PagedPool,
630 driverNamePaged.MaximumLength,
631 TAG_IO);
632 if (!driverNamePaged.Buffer)
633 {
634 ObMakeTemporaryObject(driverObject);
635 ObDereferenceObject(driverObject);
636 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
637 RtlFreeUnicodeString(&DriverName);
639 }
640
641 RtlCopyUnicodeString(&driverNamePaged, &DriverName);
642 driverObject->DriverName = driverNamePaged;
643
644 /* Finally, call its init function */
645 Status = driverObject->DriverInit(driverObject, &RegistryPath);
646 *DriverEntryStatus = Status;
647 if (!NT_SUCCESS(Status))
648 {
649 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", &DriverName, Status);
650 // return a special status value in case of failure
652 }
653
654 /* HACK: We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
655 * Other parts of the I/O manager depend on this behavior */
656 if (!driverObject->DeviceObject)
657 {
658 driverObject->Flags &= ~DRVO_LEGACY_DRIVER;
659 }
660
661 /* Windows does this fixup, keep it for compatibility */
662 for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
663 {
664 /*
665 * Make sure the driver didn't set any dispatch entry point to NULL!
666 * Doing so is illegal; drivers shouldn't touch entry points they
667 * do not implement.
668 */
669
670 /* Check if it did so anyway */
671 if (!driverObject->MajorFunction[i])
672 {
673 /* Print a warning in the debug log */
674 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
675 &driverObject->DriverName, i);
676
677 /* Fix it up */
678 driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
679 }
680 }
681
682 // TODO: for legacy drivers, unload the driver if it didn't create any DO
683
684 ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
685 RtlFreeUnicodeString(&DriverName);
686
687 if (!NT_SUCCESS(Status))
688 {
689 // if the driver entry has been failed, clear the object
690 ObMakeTemporaryObject(driverObject);
691 ObDereferenceObject(driverObject);
692 return Status;
693 }
694
695 *OutDriverObject = driverObject;
696
698
699 /* Set the driver as initialized */
700 IopReadyDeviceObjects(driverObject);
701
703
704 return STATUS_SUCCESS;
705}
706
708NTAPI
710 IN PUNICODE_STRING ImageFileDirectory,
711 IN PUNICODE_STRING NamePrefix OPTIONAL,
712 OUT PCHAR *MissingApi,
713 OUT PWCHAR *MissingDriver,
714 OUT PLOAD_IMPORTS *LoadImports);
715
716//
717// Used for images already loaded (boot drivers)
718//
719CODE_SEG("INIT")
721NTAPI
724 PLDR_DATA_TABLE_ENTRY *ModuleObject)
725{
727 UNICODE_STRING BaseName, BaseDirectory;
728 PLOAD_IMPORTS LoadedImports = MM_SYSLDR_NO_IMPORTS;
729 PCHAR MissingApiName, Buffer;
730 PWCHAR MissingDriverName;
731 PVOID DriverBase = LdrEntry->DllBase;
732
733 /* Allocate a buffer we'll use for names */
737 if (!Buffer)
738 {
739 /* Fail */
741 }
742
743 /* Check for a separator */
744 if (FileName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
745 {
746 PWCHAR p;
747 ULONG BaseLength;
748
749 /* Loop the path until we get to the base name */
750 p = &FileName->Buffer[FileName->Length / sizeof(WCHAR)];
751 while (*(p - 1) != OBJ_NAME_PATH_SEPARATOR) p--;
752
753 /* Get the length */
754 BaseLength = (ULONG)(&FileName->Buffer[FileName->Length / sizeof(WCHAR)] - p);
755 BaseLength *= sizeof(WCHAR);
756
757 /* Setup the string */
758 BaseName.Length = (USHORT)BaseLength;
759 BaseName.Buffer = p;
760 }
761 else
762 {
763 /* Otherwise, we already have a base name */
764 BaseName.Length = FileName->Length;
765 BaseName.Buffer = FileName->Buffer;
766 }
767
768 /* Setup the maximum length */
769 BaseName.MaximumLength = BaseName.Length;
770
771 /* Now compute the base directory */
772 BaseDirectory = *FileName;
773 BaseDirectory.Length -= BaseName.Length;
774 BaseDirectory.MaximumLength = BaseDirectory.Length;
775
776 /* Resolve imports */
777 MissingApiName = Buffer;
778 Status = MiResolveImageReferences(DriverBase,
779 &BaseDirectory,
780 NULL,
781 &MissingApiName,
782 &MissingDriverName,
783 &LoadedImports);
784
785 /* Free the temporary buffer */
787
788 /* Check the result of the imports resolution */
789 if (!NT_SUCCESS(Status)) return Status;
790
791 /* Return */
792 *ModuleObject = LdrEntry;
793 return STATUS_SUCCESS;
794}
795
798
799/*
800 * IopInitializeBuiltinDriver
801 *
802 * Initialize a driver that is already loaded in memory.
803 */
804CODE_SEG("INIT")
805static
808{
811 PWCHAR Buffer, FileNameWithoutPath;
812 PWSTR FileExtension;
813 PUNICODE_STRING ModuleName = &BootLdrEntry->BaseDllName;
814 PLDR_DATA_TABLE_ENTRY LdrEntry;
815 PLIST_ENTRY NextEntry;
818
819 /*
820 * Display 'Loading XXX...' message
821 */
824
826 ModuleName->Length + sizeof(UNICODE_NULL),
827 TAG_IO);
828 if (Buffer == NULL)
829 {
830 return FALSE;
831 }
832
835
836 /*
837 * Generate filename without path (not needed by freeldr)
838 */
839 FileNameWithoutPath = wcsrchr(Buffer, L'\\');
840 if (FileNameWithoutPath == NULL)
841 {
842 FileNameWithoutPath = Buffer;
843 }
844 else
845 {
846 FileNameWithoutPath++;
847 }
848
849 /*
850 * Strip the file extension from ServiceName
851 */
852 Success = RtlCreateUnicodeString(&ServiceName, FileNameWithoutPath);
854 if (!Success)
855 {
856 return FALSE;
857 }
858
859 FileExtension = wcsrchr(ServiceName.Buffer, L'.');
860 if (FileExtension != NULL)
861 {
862 ServiceName.Length -= (USHORT)wcslen(FileExtension) * sizeof(WCHAR);
863 FileExtension[0] = UNICODE_NULL;
864 }
865
867
868 // Make the registry path for the driver
869 RegistryPath.Length = 0;
870 RegistryPath.MaximumLength = sizeof(ServicesKeyName) + ServiceName.Length;
872 if (RegistryPath.Buffer == NULL)
873 {
874 return FALSE;
875 }
879
880 HANDLE serviceHandle;
883 if (!NT_SUCCESS(Status))
884 {
885 return FALSE;
886 }
887
888 /* Lookup the new Ldr entry in PsLoadedModuleList */
889 for (NextEntry = PsLoadedModuleList.Flink;
890 NextEntry != &PsLoadedModuleList;
891 NextEntry = NextEntry->Flink)
892 {
893 LdrEntry = CONTAINING_RECORD(NextEntry,
895 InLoadOrderLinks);
897 {
898 break;
899 }
900 }
901 ASSERT(NextEntry != &PsLoadedModuleList);
902
903 /*
904 * Initialize the driver
905 */
906 NTSTATUS driverEntryStatus;
908 serviceHandle,
910 &driverEntryStatus);
911
912 if (!NT_SUCCESS(Status))
913 {
914 DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName, Status);
915 return FALSE;
916 }
917
918 // The driver has been loaded, now check if where are any PDOs
919 // for that driver, and queue AddDevice call for them.
920 // The check is possible because HKLM/SYSTEM/CCS/Services/<ServiceName>/Enum directory
921 // is populated upon a new device arrival based on a (critical) device database
922
923 // Legacy drivers may add devices inside DriverEntry.
924 // We're lazy and always assume that they are doing so
925 BOOLEAN deviceAdded = !!(DriverObject->Flags & DRVO_LEGACY_DRIVER);
926
927 HANDLE enumServiceHandle;
928 UNICODE_STRING enumName = RTL_CONSTANT_STRING(L"Enum");
929
930 Status = IopOpenRegistryKeyEx(&enumServiceHandle, serviceHandle, &enumName, KEY_READ);
931 ZwClose(serviceHandle);
932
933 if (NT_SUCCESS(Status))
934 {
935 ULONG instanceCount = 0;
937 Status = IopGetRegistryValue(enumServiceHandle, L"Count", &kvInfo);
938 if (!NT_SUCCESS(Status))
939 {
940 goto Cleanup;
941 }
942 if (kvInfo->Type != REG_DWORD || kvInfo->DataLength != sizeof(ULONG))
943 {
944 ExFreePool(kvInfo);
945 goto Cleanup;
946 }
947
948 RtlMoveMemory(&instanceCount,
949 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
950 sizeof(ULONG));
951 ExFreePool(kvInfo);
952
953 DPRINT("Processing %u instances for %wZ module\n", instanceCount, ModuleName);
954
955 for (ULONG i = 0; i < instanceCount; i++)
956 {
957 WCHAR num[11];
958 UNICODE_STRING instancePath;
959 RtlStringCbPrintfW(num, sizeof(num), L"%u", i);
960
961 Status = IopGetRegistryValue(enumServiceHandle, num, &kvInfo);
962 if (!NT_SUCCESS(Status))
963 {
964 continue;
965 }
966 if (kvInfo->Type != REG_SZ || kvInfo->DataLength == 0)
967 {
968 ExFreePool(kvInfo);
969 continue;
970 }
971
972 instancePath.Length = kvInfo->DataLength - sizeof(UNICODE_NULL);
973 instancePath.MaximumLength = kvInfo->DataLength;
975 instancePath.MaximumLength,
976 TAG_IO);
977 if (instancePath.Buffer)
978 {
979 RtlMoveMemory(instancePath.Buffer,
980 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
981 instancePath.Length);
982 instancePath.Buffer[instancePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
983
985 if (pdo != NULL)
986 {
989 deviceAdded = TRUE;
990 }
991 else
992 {
993 DPRINT1("No device node found matching instance path '%wZ'\n", &instancePath);
994 }
995 }
996
997 ExFreePool(kvInfo);
998 }
999
1000 ZwClose(enumServiceHandle);
1001 }
1002Cleanup:
1003 /* Remove extra reference from IopInitializeDriverModule */
1005
1006 return deviceAdded;
1007}
1008
1009/*
1010 * IopInitializeBootDrivers
1011 *
1012 * Initialize boot drivers and free memory for boot files.
1013 *
1014 * Parameters
1015 * None
1016 *
1017 * Return Value
1018 * None
1019 */
1020CODE_SEG("INIT")
1021VOID
1024{
1025 PLIST_ENTRY ListHead, NextEntry, NextEntry2;
1026 PLDR_DATA_TABLE_ENTRY LdrEntry;
1028 UNICODE_STRING DriverName;
1029 ULONG i, Index;
1030 PDRIVER_INFORMATION DriverInfo, DriverInfoTag;
1032 PBOOT_DRIVER_LIST_ENTRY BootEntry;
1033 DPRINT("IopInitializeBootDrivers()\n");
1034
1035 /* Create the RAW FS built-in driver */
1036 RtlInitUnicodeString(&DriverName, L"\\FileSystem\\RAW");
1037
1038 Status = IoCreateDriver(&DriverName, RawFsDriverEntry);
1039 if (!NT_SUCCESS(Status))
1040 {
1041 /* Fail */
1042 return;
1043 }
1044
1045 /* Get highest group order index */
1047 if (IopGroupIndex == 0xFFFF)
1048 {
1050 }
1051
1052 /* Allocate the group table */
1054 IopGroupIndex * sizeof(LIST_ENTRY),
1055 TAG_IO);
1056 if (IopGroupTable == NULL)
1057 {
1059 }
1060
1061 /* Initialize the group table lists */
1062 for (i = 0; i < IopGroupIndex; i++) InitializeListHead(&IopGroupTable[i]);
1063
1064 /* Loop the boot modules */
1065 ListHead = &KeLoaderBlock->LoadOrderListHead;
1066 for (NextEntry = ListHead->Flink;
1067 NextEntry != ListHead;
1068 NextEntry = NextEntry->Flink)
1069 {
1070 /* Get the entry */
1071 LdrEntry = CONTAINING_RECORD(NextEntry,
1073 InLoadOrderLinks);
1074
1075 /* Check if the DLL needs to be initialized */
1076 if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
1077 {
1078 /* Call its entrypoint */
1079 MmCallDllInitialize(LdrEntry, NULL);
1080 }
1081 }
1082
1083 /* Loop the boot drivers */
1084 ListHead = &KeLoaderBlock->BootDriverListHead;
1085 for (NextEntry = ListHead->Flink;
1086 NextEntry != ListHead;
1087 NextEntry = NextEntry->Flink)
1088 {
1089 /* Get the entry */
1090 BootEntry = CONTAINING_RECORD(NextEntry,
1092 Link);
1093
1094 // FIXME: TODO: This LdrEntry is to be used in a special handling
1095 // for SETUPLDR (a similar procedure is done on Windows), where
1096 // the loader would, under certain conditions, be loaded in the
1097 // SETUPLDR-specific code block below...
1098#if 0
1099 /* Get the driver loader entry */
1100 LdrEntry = BootEntry->LdrEntry;
1101#endif
1102
1103 /* Allocate our internal accounting structure */
1105 sizeof(DRIVER_INFORMATION),
1106 TAG_IO);
1107 if (DriverInfo)
1108 {
1109 /* Zero it and initialize it */
1112 DriverInfo->DataTableEntry = BootEntry;
1113
1114 /* Open the registry key */
1116 NULL,
1117 &BootEntry->RegistryPath,
1118 KEY_READ);
1119 DPRINT("IopOpenRegistryKeyEx(%wZ) returned 0x%08lx\n", &BootEntry->RegistryPath, Status);
1120#if 0
1121 if (NT_SUCCESS(Status))
1122#else // Hack still needed...
1123 if ((NT_SUCCESS(Status)) || /* ReactOS HACK for SETUPLDR */
1124 ((KeLoaderBlock->SetupLdrBlock) && ((KeyHandle = (PVOID)1)))) // yes, it's an assignment!
1125#endif
1126 {
1127 /* Save the handle */
1129
1130 /* Get the group oder index */
1132
1133 /* Get the tag position */
1135
1136 /* Insert it into the list, at the right place */
1138 NextEntry2 = IopGroupTable[Index].Flink;
1139 while (NextEntry2 != &IopGroupTable[Index])
1140 {
1141 /* Get the driver info */
1142 DriverInfoTag = CONTAINING_RECORD(NextEntry2,
1144 Link);
1145
1146 /* Check if we found the right tag position */
1147 if (DriverInfoTag->TagPosition > DriverInfo->TagPosition)
1148 {
1149 /* We're done */
1150 break;
1151 }
1152
1153 /* Next entry */
1154 NextEntry2 = NextEntry2->Flink;
1155 }
1156
1157 /* Insert us right before the next entry */
1158 NextEntry2 = NextEntry2->Blink;
1159 InsertHeadList(NextEntry2, &DriverInfo->Link);
1160 }
1161 }
1162 }
1163
1164 /* Loop each group index */
1165 for (i = 0; i < IopGroupIndex; i++)
1166 {
1167 /* Loop each group table */
1168 for (NextEntry = IopGroupTable[i].Flink;
1169 NextEntry != &IopGroupTable[i];
1170 NextEntry = NextEntry->Flink)
1171 {
1172 /* Get the entry */
1173 DriverInfo = CONTAINING_RECORD(NextEntry,
1175 Link);
1176
1177 /* Get the driver loader entry */
1178 LdrEntry = DriverInfo->DataTableEntry->LdrEntry;
1179
1180 /* Initialize it */
1181 if (IopInitializeBuiltinDriver(LdrEntry))
1182 {
1183 // it does not make sense to enumerate the tree if there are no new devices added
1186 NULL,
1187 NULL);
1188 }
1189 }
1190 }
1191
1192 /* HAL Root Bus is being initialized before loading the boot drivers so this may cause issues
1193 * when some devices are not being initialized with their drivers. This flag is used to delay
1194 * all actions with devices (except PnP root device) until boot drivers are loaded.
1195 * See PiQueueDeviceAction function
1196 */
1198
1199 DbgPrint("BOOT DRIVERS LOADED\n");
1200
1203 NULL,
1204 NULL);
1205}
1206
1207CODE_SEG("INIT")
1208VOID
1211{
1212 PUNICODE_STRING *DriverList, *SavedList;
1213
1215
1216 /* HACK: No system drivers on the BootCD */
1217 if (KeLoaderBlock->SetupLdrBlock) return;
1218
1219 /* Get the driver list */
1220 SavedList = DriverList = CmGetSystemDriverList();
1221 ASSERT(DriverList);
1222
1223 /* Loop it */
1224 while (*DriverList)
1225 {
1226 /* Load the driver */
1227 ZwLoadDriver(*DriverList);
1228
1229 /* Free the entry */
1230 RtlFreeUnicodeString(*DriverList);
1231 ExFreePool(*DriverList);
1232
1233 /* Next entry */
1235 DriverList++;
1236 }
1237
1238 /* Free the list */
1239 ExFreePool(SavedList);
1240
1243 NULL,
1244 NULL);
1245}
1246
1247/*
1248 * IopUnloadDriver
1249 *
1250 * Unloads a device driver.
1251 *
1252 * Parameters
1253 * DriverServiceName
1254 * Name of the service to unload (registry key).
1255 *
1256 * UnloadPnpDrivers
1257 * Whether to unload Plug & Plug or only legacy drivers. If this
1258 * parameter is set to FALSE, the routine will unload only legacy
1259 * drivers.
1260 *
1261 * Return Value
1262 * Status
1263 *
1264 * To do
1265 * Guard the whole function by SEH.
1266 */
1267
1269IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
1270{
1271 UNICODE_STRING Backslash = RTL_CONSTANT_STRING(L"\\");
1273 UNICODE_STRING ImagePath;
1278 PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
1280 USHORT LastBackslash;
1281 BOOLEAN SafeToUnload = TRUE;
1283 UNICODE_STRING CapturedServiceName;
1284
1285 PAGED_CODE();
1286
1288
1289 /* Need the appropriate priviliege */
1291 {
1292 DPRINT1("No unload privilege!\n");
1294 }
1295
1296 /* Capture the service name */
1297 Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
1299 DriverServiceName);
1300 if (!NT_SUCCESS(Status))
1301 {
1302 return Status;
1303 }
1304
1305 DPRINT("IopUnloadDriver('%wZ', %u)\n", &CapturedServiceName, UnloadPnpDrivers);
1306
1307 /* We need a service name */
1308 if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
1309 {
1310 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1312 }
1313
1314 /*
1315 * Get the service name from the registry key name
1316 */
1318 &CapturedServiceName,
1319 &Backslash,
1320 &LastBackslash);
1321 if (NT_SUCCESS(Status))
1322 {
1323 NT_ASSERT(CapturedServiceName.Length >= LastBackslash + sizeof(WCHAR));
1324 ServiceName.Buffer = &CapturedServiceName.Buffer[LastBackslash / sizeof(WCHAR) + 1];
1325 ServiceName.Length = CapturedServiceName.Length - LastBackslash - sizeof(WCHAR);
1326 ServiceName.MaximumLength = CapturedServiceName.MaximumLength - LastBackslash - sizeof(WCHAR);
1327 }
1328 else
1329 {
1330 ServiceName = CapturedServiceName;
1331 }
1332
1333 /*
1334 * Construct the driver object name
1335 */
1336 Status = RtlUShortAdd(sizeof(DRIVER_ROOT_NAME),
1337 ServiceName.Length,
1338 &ObjectName.MaximumLength);
1339 if (!NT_SUCCESS(Status))
1340 {
1341 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1342 return Status;
1343 }
1344 ObjectName.Length = 0;
1346 ObjectName.MaximumLength,
1347 TAG_IO);
1348 if (!ObjectName.Buffer)
1349 {
1350 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1352 }
1355
1356 /*
1357 * Find the driver object
1358 */
1360 0,
1361 0,
1362 0,
1364 KernelMode,
1365 0,
1366 (PVOID*)&DriverObject);
1367
1368 if (!NT_SUCCESS(Status))
1369 {
1370 DPRINT1("Can't locate driver object for %wZ\n", &ObjectName);
1372 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1373 return Status;
1374 }
1375
1376 /* Free the buffer for driver object name */
1378
1379 /* Check that driver is not already unloading */
1380 if (DriverObject->Flags & DRVO_UNLOAD_INVOKED)
1381 {
1382 DPRINT1("Driver deletion pending\n");
1384 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1385 return STATUS_DELETE_PENDING;
1386 }
1387
1388 /*
1389 * Get path of service...
1390 */
1392
1393 RtlInitUnicodeString(&ImagePath, NULL);
1394
1395 QueryTable[0].Name = L"ImagePath";
1397 QueryTable[0].EntryContext = &ImagePath;
1398
1400 CapturedServiceName.Buffer,
1401 QueryTable,
1402 NULL,
1403 NULL);
1404
1405 /* We no longer need service name */
1406 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1407
1408 if (!NT_SUCCESS(Status))
1409 {
1410 DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1412 return Status;
1413 }
1414
1415 /*
1416 * Normalize the image path for all later processing.
1417 */
1419
1420 if (!NT_SUCCESS(Status))
1421 {
1422 DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status);
1424 return Status;
1425 }
1426
1427 /* Free the service path */
1428 ExFreePool(ImagePath.Buffer);
1429
1430 /*
1431 * Unload the module and release the references to the device object
1432 */
1433
1434 /* Call the load/unload routine, depending on current process */
1435 if (DriverObject->DriverUnload && DriverObject->DriverSection &&
1436 (UnloadPnpDrivers || (DriverObject->Flags & DRVO_LEGACY_DRIVER)))
1437 {
1438 /* Loop through each device object of the driver
1439 and set DOE_UNLOAD_PENDING flag */
1440 DeviceObject = DriverObject->DeviceObject;
1441 while (DeviceObject)
1442 {
1443 /* Set the unload pending flag for the device */
1444 DeviceExtension = IoGetDevObjExtension(DeviceObject);
1445 DeviceExtension->ExtensionFlags |= DOE_UNLOAD_PENDING;
1446
1447 /* Make sure there are no attached devices or no reference counts */
1448 if ((DeviceObject->ReferenceCount) || (DeviceObject->AttachedDevice))
1449 {
1450 /* Not safe to unload */
1451 DPRINT1("Drivers device object is referenced or has attached devices\n");
1452
1453 SafeToUnload = FALSE;
1454 }
1455
1456 DeviceObject = DeviceObject->NextDevice;
1457 }
1458
1459 /* If not safe to unload, then return success */
1460 if (!SafeToUnload)
1461 {
1463 return STATUS_SUCCESS;
1464 }
1465
1466 DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject->DriverName);
1467
1468 /* Set the unload invoked flag and call the unload routine */
1472
1473 /* Mark the driver object temporary, so it could be deleted later */
1475
1476 /* Dereference it 2 times */
1479
1480 return Status;
1481 }
1482 else
1483 {
1484 DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject->DriverName);
1485
1486 /* Dereference one time (refd inside this function) */
1488
1489 /* Return unloading failure */
1491 }
1492}
1493
1494VOID
1495NTAPI
1497{
1498 PDRIVER_REINIT_ITEM ReinitItem;
1500
1501 /* Get the first entry and start looping */
1504 while (Entry)
1505 {
1506 /* Get the item */
1507 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1508
1509 /* Increment reinitialization counter */
1510 ReinitItem->DriverObject->DriverExtension->Count++;
1511
1512 /* Remove the device object flag */
1513 ReinitItem->DriverObject->Flags &= ~DRVO_REINIT_REGISTERED;
1514
1515 /* Call the routine */
1516 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1517 ReinitItem->Context,
1518 ReinitItem->DriverObject->
1519 DriverExtension->Count);
1520
1521 /* Free the entry */
1523
1524 /* Move to the next one */
1527 }
1528}
1529
1530VOID
1531NTAPI
1533{
1534 PDRIVER_REINIT_ITEM ReinitItem;
1536
1537 /* Get the first entry and start looping */
1540 while (Entry)
1541 {
1542 /* Get the item */
1543 ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1544
1545 /* Increment reinitialization counter */
1546 ReinitItem->DriverObject->DriverExtension->Count++;
1547
1548 /* Remove the device object flag */
1549 ReinitItem->DriverObject->Flags &= ~DRVO_BOOTREINIT_REGISTERED;
1550
1551 /* Call the routine */
1552 ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1553 ReinitItem->Context,
1554 ReinitItem->DriverObject->
1555 DriverExtension->Count);
1556
1557 /* Free the entry */
1559
1560 /* Move to the next one */
1563 }
1564
1565 /* Wait for all device actions being finished*/
1567}
1568
1569/* PUBLIC FUNCTIONS ***********************************************************/
1570
1571/*
1572 * @implemented
1573 */
1575NTAPI
1577 _In_opt_ PUNICODE_STRING DriverName,
1578 _In_ PDRIVER_INITIALIZE InitializationFunction)
1579{
1580 WCHAR NameBuffer[100];
1581 USHORT NameLength;
1582 UNICODE_STRING LocalDriverName;
1585 ULONG ObjectSize;
1587 UNICODE_STRING ServiceKeyName;
1589 ULONG i, RetryCount = 0;
1590
1591try_again:
1592 /* First, create a unique name for the driver if we don't have one */
1593 if (!DriverName)
1594 {
1595 /* Create a random name and set up the string */
1596 NameLength = (USHORT)swprintf(NameBuffer,
1597 DRIVER_ROOT_NAME L"%08u",
1599 LocalDriverName.Length = NameLength * sizeof(WCHAR);
1600 LocalDriverName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1601 LocalDriverName.Buffer = NameBuffer;
1602 }
1603 else
1604 {
1605 /* So we can avoid another code path, use a local var */
1606 LocalDriverName = *DriverName;
1607 }
1608
1609 /* Initialize the Attributes */
1610 ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
1612 &LocalDriverName,
1614 NULL,
1615 NULL);
1616
1617 /* Create the Object */
1621 KernelMode,
1622 NULL,
1623 ObjectSize,
1624 0,
1625 0,
1626 (PVOID*)&DriverObject);
1627 if (!NT_SUCCESS(Status)) return Status;
1628
1629 DPRINT("IopCreateDriver(): created DO %p\n", DriverObject);
1630
1631 /* Set up the Object */
1632 RtlZeroMemory(DriverObject, ObjectSize);
1634 DriverObject->Size = sizeof(DRIVER_OBJECT);
1636 DriverObject->DriverExtension = (PDRIVER_EXTENSION)(DriverObject + 1);
1637 DriverObject->DriverExtension->DriverObject = DriverObject;
1638 DriverObject->DriverInit = InitializationFunction;
1639 /* Loop all Major Functions */
1640 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1641 {
1642 /* Invalidate each function */
1643 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1644 }
1645
1646 /* Set up the service key name buffer */
1647 ServiceKeyName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1648 ServiceKeyName.Buffer = ExAllocatePoolWithTag(PagedPool, LocalDriverName.MaximumLength, TAG_IO);
1649 if (!ServiceKeyName.Buffer)
1650 {
1651 /* Fail */
1655 }
1656
1657 /* For builtin drivers, the ServiceKeyName is equal to DriverName */
1658 RtlCopyUnicodeString(&ServiceKeyName, &LocalDriverName);
1659 ServiceKeyName.Buffer[ServiceKeyName.Length / sizeof(WCHAR)] = UNICODE_NULL;
1660 DriverObject->DriverExtension->ServiceKeyName = ServiceKeyName;
1661
1662 /* Make a copy of the driver name to store in the driver object */
1663 DriverObject->DriverName.MaximumLength = LocalDriverName.Length;
1664 DriverObject->DriverName.Buffer = ExAllocatePoolWithTag(PagedPool,
1665 DriverObject->DriverName.MaximumLength,
1666 TAG_IO);
1667 if (!DriverObject->DriverName.Buffer)
1668 {
1669 /* Fail */
1673 }
1674
1675 RtlCopyUnicodeString(&DriverObject->DriverName, &LocalDriverName);
1676
1677 /* Add the Object and get its handle */
1679 NULL,
1681 0,
1682 NULL,
1683 &hDriver);
1684
1685 /* Eliminate small possibility when this function is called more than
1686 once in a row, and KeTickCount doesn't get enough time to change */
1687 if (!DriverName && (Status == STATUS_OBJECT_NAME_COLLISION) && (RetryCount < 100))
1688 {
1689 RetryCount++;
1690 goto try_again;
1691 }
1692
1693 if (!NT_SUCCESS(Status)) return Status;
1694
1695 /* Now reference it */
1697 0,
1699 KernelMode,
1701 NULL);
1702
1703 /* Close the extra handle */
1705
1706 if (!NT_SUCCESS(Status))
1707 {
1708 /* Fail */
1711 return Status;
1712 }
1713
1714 /* Finally, call its init function */
1715 DPRINT("Calling driver entrypoint at %p\n", InitializationFunction);
1716 Status = InitializationFunction(DriverObject, NULL);
1717 if (!NT_SUCCESS(Status))
1718 {
1719 /* If it didn't work, then kill the object */
1720 DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", &LocalDriverName, Status);
1723 return Status;
1724 }
1725
1726 /* Windows does this fixup, keep it for compatibility */
1727 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1728 {
1729 /*
1730 * Make sure the driver didn't set any dispatch entry point to NULL!
1731 * Doing so is illegal; drivers shouldn't touch entry points they
1732 * do not implement.
1733 */
1734
1735 /* Check if it did so anyway */
1736 if (!DriverObject->MajorFunction[i])
1737 {
1738 /* Print a warning in the debug log */
1739 DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1740 &DriverObject->DriverName, i);
1741
1742 /* Fix it up */
1743 DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1744 }
1745 }
1746
1747 /* Return the Status */
1748 return Status;
1749}
1750
1751/*
1752 * @implemented
1753 */
1754VOID
1755NTAPI
1758{
1759 /* Simply dereference the Object */
1761}
1762
1763/*
1764 * @implemented
1765 */
1766VOID
1767NTAPI
1769 IN PDRIVER_REINITIALIZE ReinitRoutine,
1771{
1772 PDRIVER_REINIT_ITEM ReinitItem;
1773
1774 /* Allocate the entry */
1776 sizeof(DRIVER_REINIT_ITEM),
1777 TAG_REINIT);
1778 if (!ReinitItem) return;
1779
1780 /* Fill it out */
1781 ReinitItem->DriverObject = DriverObject;
1782 ReinitItem->ReinitRoutine = ReinitRoutine;
1783 ReinitItem->Context = Context;
1784
1785 /* Set the Driver Object flag and insert the entry into the list */
1788 &ReinitItem->ItemEntry,
1790}
1791
1792/*
1793 * @implemented
1794 */
1795VOID
1796NTAPI
1798 IN PDRIVER_REINITIALIZE ReinitRoutine,
1800{
1801 PDRIVER_REINIT_ITEM ReinitItem;
1802
1803 /* Allocate the entry */
1805 sizeof(DRIVER_REINIT_ITEM),
1806 TAG_REINIT);
1807 if (!ReinitItem) return;
1808
1809 /* Fill it out */
1810 ReinitItem->DriverObject = DriverObject;
1811 ReinitItem->ReinitRoutine = ReinitRoutine;
1812 ReinitItem->Context = Context;
1813
1814 /* Set the Driver Object flag and insert the entry into the list */
1817 &ReinitItem->ItemEntry,
1819}
1820
1821/*
1822 * @implemented
1823 */
1825NTAPI
1828 IN ULONG DriverObjectExtensionSize,
1829 OUT PVOID *DriverObjectExtension)
1830{
1831 KIRQL OldIrql;
1832 PIO_CLIENT_EXTENSION DriverExtensions, NewDriverExtension;
1833 BOOLEAN Inserted = FALSE;
1834
1835 /* Assume failure */
1836 *DriverObjectExtension = NULL;
1837
1838 /* Allocate the extension */
1839 NewDriverExtension = ExAllocatePoolWithTag(NonPagedPool,
1840 sizeof(IO_CLIENT_EXTENSION) +
1841 DriverObjectExtensionSize,
1843 if (!NewDriverExtension) return STATUS_INSUFFICIENT_RESOURCES;
1844
1845 /* Clear the extension for teh caller */
1846 RtlZeroMemory(NewDriverExtension,
1847 sizeof(IO_CLIENT_EXTENSION) + DriverObjectExtensionSize);
1848
1849 /* Acqure lock */
1851
1852 /* Fill out the extension */
1854
1855 /* Loop the current extensions */
1856 DriverExtensions = IoGetDrvObjExtension(DriverObject)->
1857 ClientDriverExtension;
1858 while (DriverExtensions)
1859 {
1860 /* Check if the identifier matches */
1861 if (DriverExtensions->ClientIdentificationAddress ==
1863 {
1864 /* We have a collision, break out */
1865 break;
1866 }
1867
1868 /* Go to the next one */
1869 DriverExtensions = DriverExtensions->NextExtension;
1870 }
1871
1872 /* Check if we didn't collide */
1873 if (!DriverExtensions)
1874 {
1875 /* Link this one in */
1876 NewDriverExtension->NextExtension =
1877 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1878 IoGetDrvObjExtension(DriverObject)->ClientDriverExtension =
1879 NewDriverExtension;
1880 Inserted = TRUE;
1881 }
1882
1883 /* Release the lock */
1885
1886 /* Check if insertion failed */
1887 if (!Inserted)
1888 {
1889 /* Free the entry and fail */
1890 ExFreePoolWithTag(NewDriverExtension, TAG_DRIVER_EXTENSION);
1892 }
1893
1894 /* Otherwise, return the pointer */
1895 *DriverObjectExtension = NewDriverExtension + 1;
1896 return STATUS_SUCCESS;
1897}
1898
1899/*
1900 * @implemented
1901 */
1902PVOID
1903NTAPI
1906{
1907 KIRQL OldIrql;
1908 PIO_CLIENT_EXTENSION DriverExtensions;
1909
1910 /* Acquire lock */
1912
1913 /* Loop the list until we find the right one */
1914 DriverExtensions = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1915 while (DriverExtensions)
1916 {
1917 /* Check for a match */
1918 if (DriverExtensions->ClientIdentificationAddress ==
1920 {
1921 /* Break out */
1922 break;
1923 }
1924
1925 /* Keep looping */
1926 DriverExtensions = DriverExtensions->NextExtension;
1927 }
1928
1929 /* Release lock */
1931
1932 /* Return nothing or the extension */
1933 if (!DriverExtensions) return NULL;
1934 return DriverExtensions + 1;
1935}
1936
1939 _In_ HANDLE ServiceHandle,
1941{
1942 UNICODE_STRING ImagePath;
1944 PLDR_DATA_TABLE_ENTRY ModuleObject;
1946
1948 Status = IopGetRegistryValue(ServiceHandle, L"ImagePath", &kvInfo);
1949 if (NT_SUCCESS(Status))
1950 {
1951 if ((kvInfo->Type != REG_EXPAND_SZ && kvInfo->Type != REG_SZ) || kvInfo->DataLength == 0)
1952 {
1953 ExFreePool(kvInfo);
1955 }
1956
1957 ImagePath.Length = kvInfo->DataLength - sizeof(UNICODE_NULL);
1958 ImagePath.MaximumLength = kvInfo->DataLength;
1960 if (!ImagePath.Buffer)
1961 {
1962 ExFreePool(kvInfo);
1964 }
1965
1966 RtlMoveMemory(ImagePath.Buffer,
1967 (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
1968 ImagePath.Length);
1969 ImagePath.Buffer[ImagePath.Length / sizeof(WCHAR)] = UNICODE_NULL;
1970 ExFreePool(kvInfo);
1971 }
1972 else
1973 {
1974 return Status;
1975 }
1976
1977 /*
1978 * Normalize the image path for all later processing.
1979 */
1980 Status = IopNormalizeImagePath(&ImagePath, NULL);
1981 if (!NT_SUCCESS(Status))
1982 {
1983 DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
1984 return Status;
1985 }
1986
1987 DPRINT("FullImagePath: '%wZ'\n", &ImagePath);
1988
1991
1992 /*
1993 * Load the driver module
1994 */
1995 DPRINT("Loading module from %wZ\n", &ImagePath);
1996 Status = MmLoadSystemImage(&ImagePath, NULL, NULL, 0, (PVOID)&ModuleObject, &BaseAddress);
1997 RtlFreeUnicodeString(&ImagePath);
1998
1999 if (!NT_SUCCESS(Status))
2000 {
2001 DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status);
2004 return Status;
2005 }
2006
2007 // Display the loading message
2008 ULONG infoLength;
2009 Status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
2011 {
2013 if (servName)
2014 {
2015 Status = ZwQueryKey(ServiceHandle,
2017 servName,
2018 infoLength,
2019 &infoLength);
2020 if (NT_SUCCESS(Status))
2021 {
2023 .Length = servName->NameLength,
2024 .MaximumLength = servName->NameLength,
2025 .Buffer = servName->Name
2026 };
2027
2029 }
2030 ExFreePoolWithTag(servName, TAG_IO);
2031 }
2032 }
2033
2034 NTSTATUS driverEntryStatus;
2035 Status = IopInitializeDriverModule(ModuleObject,
2036 ServiceHandle,
2038 &driverEntryStatus);
2039 if (!NT_SUCCESS(Status))
2040 {
2041 DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status);
2042 }
2043
2046
2047 return Status;
2048}
2049
2050static
2051VOID
2052NTAPI
2055{
2056 PLOAD_UNLOAD_PARAMS LoadParams = Parameter;
2057
2059
2060 if (LoadParams->DriverObject)
2061 {
2062 // unload request
2063 LoadParams->DriverObject->DriverUnload(LoadParams->DriverObject);
2064 LoadParams->Status = STATUS_SUCCESS;
2065 }
2066 else
2067 {
2068 // load request
2069 HANDLE serviceHandle;
2071 status = IopOpenRegistryKeyEx(&serviceHandle, NULL, LoadParams->RegistryPath, KEY_READ);
2072 if (!NT_SUCCESS(status))
2073 {
2074 LoadParams->Status = status;
2075 }
2076 else
2077 {
2078 LoadParams->Status = IopLoadDriver(serviceHandle, &LoadParams->DriverObject);
2079 ZwClose(serviceHandle);
2080 }
2081 }
2082
2083 if (LoadParams->SetEvent)
2084 {
2085 KeSetEvent(&LoadParams->Event, 0, FALSE);
2086 }
2087}
2088
2102{
2103 LOAD_UNLOAD_PARAMS LoadParams;
2104
2105 /* Prepare parameters block */
2106 LoadParams.RegistryPath = RegistryPath;
2107 LoadParams.DriverObject = *DriverObject;
2108
2110 {
2111 LoadParams.SetEvent = TRUE;
2113
2114 /* Initialize and queue a work item */
2115 ExInitializeWorkItem(&LoadParams.WorkItem, IopLoadUnloadDriverWorker, &LoadParams);
2117
2118 /* And wait till it completes */
2120 }
2121 else
2122 {
2123 /* If we're already in a system process, call it right here */
2124 LoadParams.SetEvent = FALSE;
2125 IopLoadUnloadDriverWorker(&LoadParams);
2126 }
2127
2128 return LoadParams.Status;
2129}
2130
2131/*
2132 * NtLoadDriver
2133 *
2134 * Loads a device driver.
2135 *
2136 * Parameters
2137 * DriverServiceName
2138 * Name of the service to load (registry key).
2139 *
2140 * Return Value
2141 * Status
2142 *
2143 * Status
2144 * implemented
2145 */
2148{
2149 UNICODE_STRING CapturedServiceName = { 0, 0, NULL };
2153
2154 PAGED_CODE();
2155
2157
2158 /* Need the appropriate priviliege */
2160 {
2161 DPRINT1("No load privilege!\n");
2163 }
2164
2165 /* Capture the service name */
2166 Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
2168 DriverServiceName);
2169 if (!NT_SUCCESS(Status))
2170 {
2171 return Status;
2172 }
2173
2174 DPRINT("NtLoadDriver('%wZ')\n", &CapturedServiceName);
2175
2176 /* We need a service name */
2177 if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
2178 {
2179 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2181 }
2182
2183 /* Load driver and call its entry point */
2185 Status = IopDoLoadUnloadDriver(&CapturedServiceName, &DriverObject);
2186
2187 ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2188 return Status;
2189}
2190
2191/*
2192 * NtUnloadDriver
2193 *
2194 * Unloads a legacy device driver.
2195 *
2196 * Parameters
2197 * DriverServiceName
2198 * Name of the service to unload (registry key).
2199 *
2200 * Return Value
2201 * Status
2202 *
2203 * Status
2204 * implemented
2205 */
2206
2209{
2210 return IopUnloadDriver(DriverServiceName, FALSE);
2211}
2212
2213/* EOF */
#define PAGED_CODE()
#define CODE_SEG(...)
NTSTATUS NtUnloadDriver(IN PUNICODE_STRING DriverServiceName)
Definition: driver.c:2208
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
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:1839
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 NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#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:140
struct _FileName FileName
Definition: fatprocs.h:896
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:626
_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:29
#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:56
#define MM_SYSLDR_NO_IMPORTS
Definition: miarm.h:200
#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 _Out_opt_
Definition: ms_sal.h:346
#define _Inout_
Definition: ms_sal.h:378
#define _Post_notnull_
Definition: ms_sal.h:701
#define _At_(target, annos)
Definition: ms_sal.h:244
#define _Out_
Definition: ms_sal.h:345
#define _When_(expr, annos)
Definition: ms_sal.h:254
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4715
#define KernelMode
Definition: asm.h:34
#define KeGetPreviousMode()
Definition: ketypes.h:1115
#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:2357
_In_ const STRING _In_ BOOLEAN CaseInSensitive
Definition: rtlfuncs.h:2390
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4208
DRIVER_INFORMATION DriverInfo
Definition: main.c:59
WCHAR NTAPI RtlUpcaseUnicodeChar(_In_ WCHAR Source)
Definition: nlsboot.c:176
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
@ 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:2659
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:2718
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:2938
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:1532
static VOID FASTCALL IopDisplayLoadingMessage(_In_ PCUNICODE_STRING ServiceName)
Displays a driver-loading message in SOS mode.
Definition: driver.c:328
PDEVICE_OBJECT IopGetDeviceObjectFromDeviceInstance(PUNICODE_STRING DeviceInstance)
Definition: plugplay.c:206
NTSTATUS IopLoadDriver(_In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1938
PVOID NTAPI IoGetDriverObjectExtension(IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress)
Definition: driver.c:1904
static BOOLEAN IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
Definition: driver.c:807
KEVENT PiEnumerationFinished
Definition: devaction.c:50
VOID NTAPI IoRegisterDriverReinitialization(IN PDRIVER_OBJECT DriverObject, IN PDRIVER_REINITIALIZE ReinitRoutine, IN PVOID Context)
Definition: driver.c:1797
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:1826
static const WCHAR ServicesKeyName[]
Definition: driver.c:32
NTSTATUS NTAPI IoCreateDriver(_In_opt_ PUNICODE_STRING DriverName, _In_ PDRIVER_INITIALIZE InitializationFunction)
Definition: driver.c:1576
BOOLEAN PnpSystemInit
Definition: iomgr.c:17
VOID FASTCALL IopInitializeSystemDrivers(VOID)
Definition: driver.c:1210
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:1269
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:1756
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:1768
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:443
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:1023
USHORT IopGroupIndex
Definition: driver.c:40
VOID NTAPI IopReinitializeDrivers(VOID)
Definition: driver.c:1496
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:2099
NTSTATUS NTAPI NtLoadDriver(IN PUNICODE_STRING DriverServiceName)
Definition: driver.c:2147
static VOID NTAPI IopLoadUnloadDriverWorker(_Inout_ PVOID Parameter)
Definition: driver.c:2053
NTSTATUS NTAPI LdrProcessDriverModule(PLDR_DATA_TABLE_ENTRY LdrEntry, PUNICODE_STRING FileName, PLDR_DATA_TABLE_ENTRY *ModuleObject)
Definition: driver.c:722
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:369
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:280
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
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
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:892
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:866
struct _IO_CLIENT_EXTENSION * NextExtension
Definition: iotypes.h:865
ULONG LowPart
Definition: ketypes.h:929
Definition: btrfs_drv.h:1876
ULONG Flags
Definition: ntddk_ex.h:207
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
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:84
#define TAG_LDR_WSTR
Definition: tag.h:102
#define TAG_RTLREGISTRY
Definition: tag.h:97
#define TAG_IO
Definition: tag.h:80
#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:33
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:3310
#define NT_VERIFY(exp)
Definition: rtlfuncs.h:3287
_Inout_opt_ PVOID Parameter
Definition: rtltypes.h:323
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175