ReactOS  0.4.15-dev-1206-g731eddf
driver.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for driver.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define TAG_RTLREGISTRY   'vrqR'
 

Functions

NTSTATUS NTAPI IopInvalidDeviceRequest (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 
VOID NTAPI IopDeleteDriver (IN PVOID ObjectBody)
 
NTSTATUS FASTCALL IopGetDriverObject (PDRIVER_OBJECT *DriverObject, PUNICODE_STRING ServiceName, BOOLEAN FileSystem)
 
BOOLEAN NTAPI IopSuffixUnicodeString (IN PCUNICODE_STRING String1, IN PCUNICODE_STRING String2)
 
VOID FASTCALL IopDisplayLoadingMessage (PUNICODE_STRING ServiceName)
 
NTSTATUS FASTCALL IopNormalizeImagePath (_Inout_ _When_(return >=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem))) PUNICODE_STRING ImagePath, _In_ PUNICODE_STRING ServiceName)
 
NTSTATUS FASTCALL IopLoadServiceModule (IN PUNICODE_STRING ServiceName, OUT PLDR_DATA_TABLE_ENTRY *ModuleObject)
 
VOID NTAPI MmFreeDriverInitialization (IN PLDR_DATA_TABLE_ENTRY LdrEntry)
 
NTSTATUS FASTCALL IopInitializeDriverModule (IN PDEVICE_NODE DeviceNode, IN PLDR_DATA_TABLE_ENTRY ModuleObject, IN PUNICODE_STRING ServiceName, IN BOOLEAN FileSystemDriver, OUT PDRIVER_OBJECT *DriverObject)
 
NTSTATUS NTAPI IopAttachFilterDriversCallback (PWSTR ValueName, ULONG ValueType, PVOID ValueData, ULONG ValueLength, PVOID Context, PVOID EntryContext)
 
NTSTATUS FASTCALL IopAttachFilterDrivers (PDEVICE_NODE DeviceNode, HANDLE EnumSubKey, HANDLE ClassKey, BOOLEAN Lower)
 
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)
 
INIT_FUNCTION NTSTATUS NTAPI LdrProcessDriverModule (PLDR_DATA_TABLE_ENTRY LdrEntry, PUNICODE_STRING FileName, PLDR_DATA_TABLE_ENTRY *ModuleObject)
 
INIT_FUNCTION NTSTATUS NTAPI IopInitializeBuiltinDriver (IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
 
INIT_FUNCTION VOID FASTCALL IopInitializeBootDrivers (VOID)
 
INIT_FUNCTION VOID FASTCALL IopInitializeSystemDrivers (VOID)
 
NTSTATUS NTAPI IopUnloadDriver (PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
 
VOID NTAPI IopReinitializeDrivers (VOID)
 
VOID NTAPI IopReinitializeBootDrivers (VOID)
 
NTSTATUS NTAPI IopCreateDriver (IN PUNICODE_STRING DriverName OPTIONAL, IN PDRIVER_INITIALIZE InitializationFunction, IN PUNICODE_STRING RegistryPath OPTIONAL, IN PCUNICODE_STRING ServiceName, IN PLDR_DATA_TABLE_ENTRY ModuleObject OPTIONAL, OUT PDRIVER_OBJECT *pDriverObject)
 
NTSTATUS NTAPI IoCreateDriver (IN PUNICODE_STRING DriverName OPTIONAL, IN PDRIVER_INITIALIZE InitializationFunction)
 
VOID NTAPI IoDeleteDriver (IN PDRIVER_OBJECT DriverObject)
 
VOID NTAPI IoRegisterBootDriverReinitialization (IN PDRIVER_OBJECT DriverObject, IN PDRIVER_REINITIALIZE ReinitRoutine, IN PVOID Context)
 
VOID NTAPI IoRegisterDriverReinitialization (IN PDRIVER_OBJECT DriverObject, IN PDRIVER_REINITIALIZE ReinitRoutine, IN PVOID Context)
 
NTSTATUS NTAPI IoAllocateDriverObjectExtension (IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress, IN ULONG DriverObjectExtensionSize, OUT PVOID *DriverObjectExtension)
 
PVOID NTAPI IoGetDriverObjectExtension (IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress)
 
VOID NTAPI IopLoadUnloadDriverWorker (_Inout_ PVOID Parameter)
 
NTSTATUS NTAPI IopLoadUnloadDriver (_In_opt_ PCUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
 
NTSTATUS NTAPI NtLoadDriver (IN PUNICODE_STRING DriverServiceName)
 
NTSTATUS NTAPI NtUnloadDriver (IN PUNICODE_STRING DriverServiceName)
 

Variables

ERESOURCE IopDriverLoadResource
 
LIST_ENTRY DriverReinitListHead
 
KSPIN_LOCK DriverReinitListLock
 
PLIST_ENTRY DriverReinitTailEntry
 
PLIST_ENTRY DriverBootReinitTailEntry
 
LIST_ENTRY DriverBootReinitListHead
 
KSPIN_LOCK DriverBootReinitListLock
 
UNICODE_STRING IopHardwareDatabaseKey
 
POBJECT_TYPE IoDriverObjectType = NULL
 
BOOLEAN ExpInTextModeSetup
 
BOOLEAN PnpSystemInit
 
BOOLEAN PnPBootDriversLoaded
 
KEVENT PiEnumerationFinished
 
USHORT IopGroupIndex
 
PLIST_ENTRY IopGroupTable
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file driver.c.

◆ TAG_RTLREGISTRY

#define TAG_RTLREGISTRY   'vrqR'

Definition at line 34 of file driver.c.

Function Documentation

◆ IoAllocateDriverObjectExtension()

NTSTATUS NTAPI IoAllocateDriverObjectExtension ( IN PDRIVER_OBJECT  DriverObject,
IN PVOID  ClientIdentificationAddress,
IN ULONG  DriverObjectExtensionSize,
OUT PVOID DriverObjectExtension 
)

Definition at line 1788 of file driver.c.

1792 {
1793  KIRQL OldIrql;
1794  PIO_CLIENT_EXTENSION DriverExtensions, NewDriverExtension;
1795  BOOLEAN Inserted = FALSE;
1796 
1797  /* Assume failure */
1798  *DriverObjectExtension = NULL;
1799 
1800  /* Allocate the extension */
1801  NewDriverExtension = ExAllocatePoolWithTag(NonPagedPool,
1802  sizeof(IO_CLIENT_EXTENSION) +
1803  DriverObjectExtensionSize,
1805  if (!NewDriverExtension) return STATUS_INSUFFICIENT_RESOURCES;
1806 
1807  /* Clear the extension for teh caller */
1808  RtlZeroMemory(NewDriverExtension,
1809  sizeof(IO_CLIENT_EXTENSION) + DriverObjectExtensionSize);
1810 
1811  /* Acqure lock */
1813 
1814  /* Fill out the extension */
1816 
1817  /* Loop the current extensions */
1818  DriverExtensions = IoGetDrvObjExtension(DriverObject)->
1819  ClientDriverExtension;
1820  while (DriverExtensions)
1821  {
1822  /* Check if the identifier matches */
1823  if (DriverExtensions->ClientIdentificationAddress ==
1825  {
1826  /* We have a collision, break out */
1827  break;
1828  }
1829 
1830  /* Go to the next one */
1831  DriverExtensions = DriverExtensions->NextExtension;
1832  }
1833 
1834  /* Check if we didn't collide */
1835  if (!DriverExtensions)
1836  {
1837  /* Link this one in */
1838  NewDriverExtension->NextExtension =
1839  IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1840  IoGetDrvObjExtension(DriverObject)->ClientDriverExtension =
1841  NewDriverExtension;
1842  Inserted = TRUE;
1843  }
1844 
1845  /* Release the lock */
1847 
1848  /* Check if insertion failed */
1849  if (!Inserted)
1850  {
1851  /* Free the entry and fail */
1852  ExFreePoolWithTag(NewDriverExtension, TAG_DRIVER_EXTENSION);
1854  }
1855 
1856  /* Otherwise, return the pointer */
1857  *DriverObjectExtension = NewDriverExtension + 1;
1858  return STATUS_SUCCESS;
1859 }
#define STATUS_OBJECT_NAME_COLLISION
Definition: udferr_usr.h:150
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment KeRaiseIrqlToDpcLevel
Definition: CrNtStubs.h:67
struct _IO_CLIENT_EXTENSION * NextExtension
Definition: iotypes.h:814
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
#define TRUE
Definition: types.h:120
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
__drv_aliasesMem _In_ PVOID ClientIdentificationAddress
Definition: iofuncs.h:1026
#define TAG_DRIVER_EXTENSION
Definition: tag.h:42
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
PVOID ClientIdentificationAddress
Definition: iotypes.h:815
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define IoGetDrvObjExtension(DriverObject)
Definition: io.h:132

Referenced by _IRQL_requires_max_(), DriverEntry(), HidRegisterMinidriver(), KoDriverInitialize(), KsInitializeDriver(), NdisMRegisterMiniport(), PciIdeXInitialize(), StorPortInitialize(), StreamClassRegisterAdapter(), and VideoPortInitialize().

◆ IoCreateDriver()

NTSTATUS NTAPI IoCreateDriver ( IN PUNICODE_STRING DriverName  OPTIONAL,
IN PDRIVER_INITIALIZE  InitializationFunction 
)

Definition at line 1707 of file driver.c.

1709 {
1711  return IopCreateDriver(DriverName, InitializationFunction, NULL, DriverName, NULL, &DriverObject);
1712 }
NTSTATUS NTAPI IopCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL, IN PDRIVER_INITIALIZE InitializationFunction, IN PUNICODE_STRING RegistryPath OPTIONAL, IN PCUNICODE_STRING ServiceName, IN PLDR_DATA_TABLE_ENTRY ModuleObject OPTIONAL, OUT PDRIVER_OBJECT *pDriverObject)
Definition: driver.c:1494
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42

Referenced by HaliInitPnpDriver(), IopInitializePlugPlayServices(), and WmiInitialize().

◆ IoDeleteDriver()

VOID NTAPI IoDeleteDriver ( IN PDRIVER_OBJECT  DriverObject)

Definition at line 1719 of file driver.c.

1720 {
1721  /* Simply dereference the Object */
1723 }
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
static PDRIVER_OBJECT DriverObject
Definition: template.c:42

◆ IoGetDriverObjectExtension()

PVOID NTAPI IoGetDriverObjectExtension ( IN PDRIVER_OBJECT  DriverObject,
IN PVOID  ClientIdentificationAddress 
)

Definition at line 1866 of file driver.c.

1868 {
1869  KIRQL OldIrql;
1870  PIO_CLIENT_EXTENSION DriverExtensions;
1871 
1872  /* Acquire lock */
1874 
1875  /* Loop the list until we find the right one */
1876  DriverExtensions = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1877  while (DriverExtensions)
1878  {
1879  /* Check for a match */
1880  if (DriverExtensions->ClientIdentificationAddress ==
1882  {
1883  /* Break out */
1884  break;
1885  }
1886 
1887  /* Keep looping */
1888  DriverExtensions = DriverExtensions->NextExtension;
1889  }
1890 
1891  /* Release lock */
1893 
1894  /* Return nothing or the extension */
1895  if (!DriverExtensions) return NULL;
1896  return DriverExtensions + 1;
1897 }
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment KeRaiseIrqlToDpcLevel
Definition: CrNtStubs.h:67
struct _IO_CLIENT_EXTENSION * NextExtension
Definition: iotypes.h:814
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
UCHAR KIRQL
Definition: env_spec_w32.h:591
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
__drv_aliasesMem _In_ PVOID ClientIdentificationAddress
Definition: iofuncs.h:1026
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
PVOID ClientIdentificationAddress
Definition: iotypes.h:815
#define IoGetDrvObjExtension(DriverObject)
Definition: io.h:132

Referenced by _Dispatch_type_(), _IRQL_requires_max_(), ClassAddDevice(), ClassDispatchPnp(), ClassGetPdoId(), ClassPnpQueryFdoRelations(), ClassPnpStartDevice(), ClassUnload(), ConDrvCreateController(), ConDrvDeleteController(), CreateClassDeviceObject(), CreateGreenFdo(), DestroyPortDriver(), GreenAddDevice(), GreenDispatch(), HidClassAddDevice(), i8042RemoveDevice(), IntVideoPortAddDevice(), IntVideoPortFilterResourceRequirements(), IntVideoPortPnPStartDevice(), KeyboardAddDevice(), KopDispatchCreate(), KsAddDevice(), NdisIAddDevice(), PciIdeXAddDevice(), PciIdeXFdoStartDevice(), PortAddDevice(), PortUnload(), ScreenAddDevice(), SermouseAddDevice(), StorPortInitialize(), StreamClassAddDevice(), StreamClassStartDevice(), VfdCreateDevice(), VfdDeleteDevice(), VfdStoreLink(), VfdUnloadDriver(), VideoPortGetAccessRanges(), and VideoPortInitialize().

◆ IopAttachFilterDrivers()

NTSTATUS FASTCALL IopAttachFilterDrivers ( PDEVICE_NODE  DeviceNode,
HANDLE  EnumSubKey,
HANDLE  ClassKey,
BOOLEAN  Lower 
)

Definition at line 651 of file driver.c.

656 {
657  RTL_QUERY_REGISTRY_TABLE QueryTable[2] = { { NULL, 0, NULL, NULL, 0, NULL, 0 }, };
659 
660  /*
661  * First load the device filters
662  */
664  if (Lower)
665  QueryTable[0].Name = L"LowerFilters";
666  else
667  QueryTable[0].Name = L"UpperFilters";
668  QueryTable[0].Flags = 0;
670 
672  (PWSTR)EnumSubKey,
673  QueryTable,
674  DeviceNode,
675  NULL);
676  if (!NT_SUCCESS(Status))
677  {
678  DPRINT1("Failed to load device %s filters: %08X\n",
679  Lower ? "lower" : "upper", Status);
680  return Status;
681  }
682 
684  if (Lower)
685  QueryTable[0].Name = L"LowerFilters";
686  else
687  QueryTable[0].Name = L"UpperFilters";
689  QueryTable[0].Flags = 0;
691 
692  if (ClassKey == NULL)
693  {
694  return STATUS_SUCCESS;
695  }
696 
698  (PWSTR)ClassKey,
699  QueryTable,
700  DeviceNode,
701  NULL);
702 
703  if (!NT_SUCCESS(Status))
704  {
705  DPRINT1("Failed to load class %s filters: %08X\n",
706  Lower ? "lower" : "upper", Status);
707  return Status;
708  }
709 
710  return STATUS_SUCCESS;
711 }
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4142
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
uint16_t * PWSTR
Definition: typedefs.h:56
LONG NTSTATUS
Definition: precomp.h:26
PRTL_QUERY_REGISTRY_ROUTINE QueryRoutine
Definition: nt_native.h:109
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS NTAPI IopAttachFilterDriversCallback(PWSTR ValueName, ULONG ValueType, PVOID ValueData, ULONG ValueLength, PVOID Context, PVOID EntryContext)
Definition: driver.c:565
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Definition: Node.h:9
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
#define RTL_REGISTRY_HANDLE
Definition: nt_native.h:168
#define DPRINT1
Definition: precomp.h:8
#define REG_NONE
Definition: nt_native.h:1492
return STATUS_SUCCESS
Definition: btrfs.c:3014

◆ IopAttachFilterDriversCallback()

NTSTATUS NTAPI IopAttachFilterDriversCallback ( PWSTR  ValueName,
ULONG  ValueType,
PVOID  ValueData,
ULONG  ValueLength,
PVOID  Context,
PVOID  EntryContext 
)

Definition at line 565 of file driver.c.

572 {
575  PWCHAR Filters;
576  PLDR_DATA_TABLE_ENTRY ModuleObject;
579 
580  /* No filter value present */
581  if (ValueType == REG_NONE)
582  return STATUS_SUCCESS;
583 
584  for (Filters = ValueData;
585  ((ULONG_PTR)Filters - (ULONG_PTR)ValueData) < ValueLength &&
586  *Filters != 0;
587  Filters += (ServiceName.Length / sizeof(WCHAR)) + 1)
588  {
589  DPRINT("Filter Driver: %S (%wZ)\n", Filters, &DeviceNode->InstancePath);
590 
591  ServiceName.Buffer = Filters;
592  ServiceName.MaximumLength =
593  ServiceName.Length = (USHORT)wcslen(Filters) * sizeof(WCHAR);
594 
598  &ServiceName,
599  FALSE);
600  if (!NT_SUCCESS(Status))
601  {
602  /* Load and initialize the filter driver */
603  Status = IopLoadServiceModule(&ServiceName, &ModuleObject);
604  if (!NT_SUCCESS(Status))
605  {
608  return Status;
609  }
610 
612  ModuleObject,
613  &ServiceName,
614  FALSE,
615  &DriverObject);
616  if (!NT_SUCCESS(Status))
617  {
620  return Status;
621  }
622  }
623 
626 
628 
629  /* Remove extra reference */
631 
632  if (!NT_SUCCESS(Status))
633  return Status;
634  }
635 
636  return STATUS_SUCCESS;
637 }
#define TRUE
Definition: types.h:120
_In_ PCWSTR _In_z_ PCWSTR _In_ ULONG ValueType
Definition: rtlfuncs.h:4154
LONG NTSTATUS
Definition: precomp.h:26
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
uint16_t * PWCHAR
Definition: typedefs.h:56
NTSTATUS FASTCALL IopLoadServiceModule(IN PUNICODE_STRING ServiceName, OUT PLDR_DATA_TABLE_ENTRY *ModuleObject)
Definition: driver.c:316
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
#define FALSE
Definition: types.h:117
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
NTSTATUS FASTCALL IopInitializeDevice(IN PDEVICE_NODE DeviceNode, IN PDRIVER_OBJECT DriverObject)
NTSTATUS FASTCALL IopGetDriverObject(PDRIVER_OBJECT *DriverObject, PUNICODE_STRING ServiceName, BOOLEAN FileSystem)
Definition: driver.c:107
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ GUID _In_ PVOID ValueData
Definition: hubbusif.h:311
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
Definition: Node.h:9
Status
Definition: gdiplustypes.h:24
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
Definition: btrfs_drv.h:1922
ERESOURCE IopDriverLoadResource
Definition: driver.c:19
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
NTSTATUS FASTCALL IopInitializeDriverModule(IN PDEVICE_NODE DeviceNode, IN PLDR_DATA_TABLE_ENTRY ModuleObject, IN PUNICODE_STRING ServiceName, IN BOOLEAN FileSystemDriver, OUT PDRIVER_OBJECT *DriverObject)
Definition: driver.c:469
unsigned short USHORT
Definition: pedump.c:61
struct tagContext Context
Definition: acpixf.h:1034
#define ULONG_PTR
Definition: config.h:101
#define REG_NONE
Definition: nt_native.h:1492
return STATUS_SUCCESS
Definition: btrfs.c:3014
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by IopAttachFilterDrivers().

◆ IopCreateDriver()

NTSTATUS NTAPI IopCreateDriver ( IN PUNICODE_STRING DriverName  OPTIONAL,
IN PDRIVER_INITIALIZE  InitializationFunction,
IN PUNICODE_STRING RegistryPath  OPTIONAL,
IN PCUNICODE_STRING  ServiceName,
IN PLDR_DATA_TABLE_ENTRY ModuleObject  OPTIONAL,
OUT PDRIVER_OBJECT pDriverObject 
)

Definition at line 1494 of file driver.c.

1500 {
1501  WCHAR NameBuffer[100];
1502  USHORT NameLength;
1503  UNICODE_STRING LocalDriverName;
1504  NTSTATUS Status;
1506  ULONG ObjectSize;
1508  UNICODE_STRING ServiceKeyName;
1509  HANDLE hDriver;
1510  ULONG i, RetryCount = 0;
1511 
1512 try_again:
1513  /* First, create a unique name for the driver if we don't have one */
1514  if (!DriverName)
1515  {
1516  /* Create a random name and set up the string */
1517  NameLength = (USHORT)swprintf(NameBuffer,
1518  DRIVER_ROOT_NAME L"%08u",
1520  LocalDriverName.Length = NameLength * sizeof(WCHAR);
1521  LocalDriverName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1522  LocalDriverName.Buffer = NameBuffer;
1523  }
1524  else
1525  {
1526  /* So we can avoid another code path, use a local var */
1527  LocalDriverName = *DriverName;
1528  }
1529 
1530  /* Initialize the Attributes */
1531  ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
1533  &LocalDriverName,
1535  NULL,
1536  NULL);
1537 
1538  /* Create the Object */
1542  KernelMode,
1543  NULL,
1544  ObjectSize,
1545  0,
1546  0,
1547  (PVOID*)&DriverObject);
1548  if (!NT_SUCCESS(Status)) return Status;
1549 
1550  DPRINT("IopCreateDriver(): created DO %p\n", DriverObject);
1551 
1552  /* Set up the Object */
1553  RtlZeroMemory(DriverObject, ObjectSize);
1555  DriverObject->Size = sizeof(DRIVER_OBJECT);
1556 
1557  /*
1558  * Check whether RegistryPath and ModuleObject are both NULL because
1559  * IoCreateDriver() was called to initialize a built-in driver.
1560  */
1561  if ((RegistryPath != NULL) || (ModuleObject != NULL))
1563  else
1565 
1568  DriverObject->DriverInit = InitializationFunction;
1569  DriverObject->DriverSection = ModuleObject;
1570  /* Loop all Major Functions */
1571  for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1572  {
1573  /* Invalidate each function */
1575  }
1576 
1577  /* Set up the service key name buffer */
1578  ServiceKeyName.MaximumLength = ServiceName->Length + sizeof(UNICODE_NULL);
1579  ServiceKeyName.Buffer = ExAllocatePoolWithTag(NonPagedPool,
1580  ServiceKeyName.MaximumLength,
1581  TAG_IO);
1582  if (!ServiceKeyName.Buffer)
1583  {
1584  /* Fail */
1588  }
1589 
1590  /* Copy the name and set it in the driver extension */
1591  RtlCopyUnicodeString(&ServiceKeyName,
1592  ServiceName);
1593  DriverObject->DriverExtension->ServiceKeyName = ServiceKeyName;
1594 
1595  /* Make a copy of the driver name to store in the driver object */
1596  DriverObject->DriverName.MaximumLength = LocalDriverName.Length;
1599  TAG_IO);
1601  {
1602  /* Fail */
1606  }
1607 
1609  &LocalDriverName);
1610 
1611  /* Add the Object and get its handle */
1613  NULL,
1615  0,
1616  NULL,
1617  &hDriver);
1618 
1619  /* Eliminate small possibility when this function is called more than
1620  once in a row, and KeTickCount doesn't get enough time to change */
1621  if (!DriverName && (Status == STATUS_OBJECT_NAME_COLLISION) && (RetryCount < 100))
1622  {
1623  RetryCount++;
1624  goto try_again;
1625  }
1626 
1627  if (!NT_SUCCESS(Status)) return Status;
1628 
1629  /* Now reference it */
1631  0,
1633  KernelMode,
1634  (PVOID*)&DriverObject,
1635  NULL);
1636 
1637  /* Close the extra handle */
1638  ZwClose(hDriver);
1639 
1640  if (!NT_SUCCESS(Status))
1641  {
1642  /* Fail */
1645  return Status;
1646  }
1647 
1649  DriverObject->DriverStart = ModuleObject ? ModuleObject->DllBase : 0;
1650  DriverObject->DriverSize = ModuleObject ? ModuleObject->SizeOfImage : 0;
1651 
1652  /* Finally, call its init function */
1653  DPRINT("RegistryKey: %wZ\n", RegistryPath);
1654  DPRINT("Calling driver entrypoint at %p\n", InitializationFunction);
1655  Status = (*InitializationFunction)(DriverObject, RegistryPath);
1656  if (!NT_SUCCESS(Status))
1657  {
1658  /* If it didn't work, then kill the object */
1659  DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", DriverName, Status);
1663  return Status;
1664  }
1665  else
1666  {
1667  /* Returns to caller the object */
1668  *pDriverObject = DriverObject;
1669  }
1670 
1671  /* We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
1672  * Other parts of the I/O manager depend on this behavior */
1674 
1675  /* Loop all Major Functions */
1676  for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1677  {
1678  /*
1679  * Make sure the driver didn't set any dispatch entry point to NULL!
1680  * Doing so is illegal; drivers shouldn't touch entry points they
1681  * do not implement.
1682  */
1683 
1684  /* Check if it did so anyway */
1685  if (!DriverObject->MajorFunction[i])
1686  {
1687  /* Print a warning in the debug log */
1688  DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1690 
1691  /* Fix it up */
1693  }
1694  }
1695 
1696  /* Return the Status */
1697  return Status;
1698 }
#define STATUS_OBJECT_NAME_COLLISION
Definition: udferr_usr.h:150
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
ULONG LowPart
Definition: ketypes.h:916
#define DRVO_BUILTIN_DRIVER
Definition: iotypes.h:2207
#define DRVO_LEGACY_DRIVER
Definition: iotypes.h:2206
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
USHORT MaximumLength
Definition: env_spec_w32.h:370
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define IRP_MJ_MAXIMUM_FUNCTION
LONG NTSTATUS
Definition: precomp.h:26
PVOID DriverStart
Definition: iotypes.h:2259
_In_ LPWSTR _In_ ULONG _In_ ULONG _In_ ULONG _Out_ DEVINFO _In_ HDEV _In_ LPWSTR _In_ HANDLE hDriver
Definition: winddi.h:3553
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:32
NTSTATUS NTAPI IopInvalidDeviceRequest(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: driver.c:48
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
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
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:496
#define UNICODE_NULL
struct _DRIVER_OBJECT * DriverObject
Definition: iotypes.h:2199
#define FILE_READ_DATA
Definition: nt_native.h:628
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2262
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
PUNICODE_STRING HardwareDatabase
Definition: iotypes.h:2264
void DPRINT(...)
Definition: polytest.cpp:61
CSHORT Size
Definition: iotypes.h:2256
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:952
_In_z_ PWSTR RegistryPath
Definition: classp.h:1930
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define swprintf(buf, format,...)
Definition: sprintf.c:56
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
#define OBJ_PERMANENT
Definition: winternl.h:226
struct _DRIVER_OBJECT DRIVER_OBJECT
UNICODE_STRING ServiceKeyName
Definition: iotypes.h:2202
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:2932
unsigned short USHORT
Definition: pedump.c:61
struct _DRIVER_EXTENSION * PDRIVER_EXTENSION
UNICODE_STRING IopHardwareDatabaseKey
Definition: driver.c:29
#define IO_TYPE_DRIVER
#define DPRINT1
Definition: precomp.h:8
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1]
Definition: iotypes.h:2269
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
ULONG DriverSize
Definition: iotypes.h:2260
CSHORT Type
Definition: iotypes.h:2255
PVOID DriverSection
Definition: iotypes.h:2261
PDRIVER_INITIALIZE DriverInit
Definition: iotypes.h:2266
UNICODE_STRING DriverName
Definition: iotypes.h:2263
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2257
#define DRIVER_ROOT_NAME
Definition: ldr.h:5
VOID NTAPI ObMakeTemporaryObject(IN PVOID ObjectBody)
Definition: oblife.c:1361

Referenced by IoCreateDriver(), and IopInitializeDriverModule().

◆ IopDeleteDriver()

VOID NTAPI IopDeleteDriver ( IN PVOID  ObjectBody)

Definition at line 60 of file driver.c.

61 {
62  PDRIVER_OBJECT DriverObject = ObjectBody;
63  PIO_CLIENT_EXTENSION DriverExtension, NextDriverExtension;
64  PAGED_CODE();
65 
66  DPRINT1("Deleting driver object '%wZ'\n", &DriverObject->DriverName);
67 
68  /* There must be no device objects remaining at this point */
70 
71  /* Get the extension and loop them */
72  DriverExtension = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
73  while (DriverExtension)
74  {
75  /* Get the next one */
76  NextDriverExtension = DriverExtension->NextExtension;
78 
79  /* Move on */
80  DriverExtension = NextDriverExtension;
81  }
82 
83  /* Check if the driver image is still loaded */
85  {
86  /* Unload it */
88  }
89 
90  /* Check if it has a name */
92  {
93  /* Free it */
95  }
96 
97  /* Check if it has a service key name */
99  {
100  /* Free it */
102  }
103 }
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2262
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:912
#define TAG_DRIVER_EXTENSION
Definition: tag.h:42
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:41
UNICODE_STRING ServiceKeyName
Definition: iotypes.h:2202
#define DPRINT1
Definition: precomp.h:8
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define IoGetDrvObjExtension(DriverObject)
Definition: io.h:132
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
PVOID DriverSection
Definition: iotypes.h:2261
UNICODE_STRING DriverName
Definition: iotypes.h:2263
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2257
#define PAGED_CODE()

Referenced by IopCreateObjectTypes().

◆ IopDisplayLoadingMessage()

VOID FASTCALL IopDisplayLoadingMessage ( PUNICODE_STRING  ServiceName)

Definition at line 209 of file driver.c.

210 {
211  CHAR TextBuffer[256];
212  UNICODE_STRING DotSys = RTL_CONSTANT_STRING(L".SYS");
213 
214  if (ExpInTextModeSetup) return;
215  if (!KeLoaderBlock) return;
217  snprintf(TextBuffer, sizeof(TextBuffer),
218  "%s%sSystem32\\Drivers\\%wZ%s\r\n",
221  ServiceName,
222  IopSuffixUnicodeString(&DotSys, ServiceName) ? "" : ".SYS");
224 }
BOOLEAN ExpInTextModeSetup
Definition: init.c:67
PSTR ArcBootDeviceName
Definition: arc.h:503
NTSTATUS RtlUpcaseUnicodeString(PUNICODE_STRING dst, PUNICODE_STRING src, BOOLEAN Alloc)
Definition: string_lib.cpp:46
char CHAR
Definition: xmlstorage.h:175
#define snprintf
Definition: wintirpc.h:48
#define FALSE
Definition: types.h:117
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
LPTSTR ServiceName
Definition: ServiceMain.c:15
static const WCHAR L[]
Definition: oid.c:1250
NTHALAPI VOID NTAPI HalDisplayString(PUCHAR String)
char TextBuffer[BUFFERLEN]
Definition: combotst.c:45
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
BOOLEAN NTAPI IopSuffixUnicodeString(IN PCUNICODE_STRING String1, IN PCUNICODE_STRING String2)
Definition: driver.c:175

Referenced by IopInitializeBuiltinDriver(), IopLoadServiceModule(), and IopLoadUnloadDriver().

◆ IopGetDriverObject()

NTSTATUS FASTCALL IopGetDriverObject ( PDRIVER_OBJECT DriverObject,
PUNICODE_STRING  ServiceName,
BOOLEAN  FileSystem 
)

Definition at line 107 of file driver.c.

111 {
114  UNICODE_STRING DriverName;
116 
117  DPRINT("IopGetDriverObject(%p '%wZ' %x)\n",
119 
121  *DriverObject = NULL;
122 
123  /* Create ModuleName string */
124  if (ServiceName == NULL || ServiceName->Buffer == NULL)
125  /* We don't know which DriverObject we have to open */
127 
128  if (FileSystem != FALSE)
130  else
132 
133  DriverName.Length = 0;
134  DriverName.MaximumLength = Prefix.Length + ServiceName->Length + sizeof(UNICODE_NULL);
135  ASSERT(DriverName.MaximumLength > ServiceName->Length);
136  DriverName.Buffer = ExAllocatePoolWithTag(PagedPool, DriverName.MaximumLength, TAG_IO);
137  if (DriverName.Buffer == NULL)
138  {
140  }
141  RtlAppendUnicodeStringToString(&DriverName, &Prefix);
143 
144  DPRINT("Driver name: '%wZ'\n", &DriverName);
145 
146  /* Open driver object */
147  Status = ObReferenceObjectByName(&DriverName,
149  NULL, /* PassedAccessState */
150  0, /* DesiredAccess */
152  KernelMode,
153  NULL, /* ParseContext */
154  (PVOID*)&Object);
155  ExFreePoolWithTag(DriverName.Buffer, TAG_IO);
156  if (!NT_SUCCESS(Status))
157  {
158  DPRINT("Failed to reference driver object, status=0x%08x\n", Status);
159  return Status;
160  }
161 
162  *DriverObject = Object;
163 
164  DPRINT("Driver Object: %p\n", Object);
165 
166  return STATUS_SUCCESS;
167 }
_In_ __drv_aliasesMem PSTRING Prefix
Definition: rtlfuncs.h:1631
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
PWCHAR FileSystem
Definition: format.c:72
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
unsigned short Length
Definition: sprintf.c:451
#define FILESYSTEM_ROOT_NAME
Definition: ldr.h:6
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:32
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_OPENIF
Definition: winternl.h:229
#define FALSE
Definition: types.h:117
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite(IN PERESOURCE Resource)
Definition: resource.c:1619
#define STATUS_INVALID_PARAMETER_2
Definition: ntstatus.h:476
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static IUnknown Object
Definition: main.c:512
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
ERESOURCE IopDriverLoadResource
Definition: driver.c:19
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:3014
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:411
#define DRIVER_ROOT_NAME
Definition: ldr.h:5

Referenced by IopAttachFilterDriversCallback(), and IopLoadUnloadDriver().

◆ IopInitializeBootDrivers()

INIT_FUNCTION VOID FASTCALL IopInitializeBootDrivers ( VOID  )

Definition at line 941 of file driver.c.

942 {
943  PLIST_ENTRY ListHead, NextEntry, NextEntry2;
944  PLDR_DATA_TABLE_ENTRY LdrEntry;
947  LDR_DATA_TABLE_ENTRY ModuleObject;
949  UNICODE_STRING DriverName;
950  ULONG i, Index;
951  PDRIVER_INFORMATION DriverInfo, DriverInfoTag;
953  PBOOT_DRIVER_LIST_ENTRY BootEntry;
954  DPRINT("IopInitializeBootDrivers()\n");
955 
956  /* Use IopRootDeviceNode for now */
958  if (!NT_SUCCESS(Status)) return;
959 
960  /* Setup the module object for the RAW FS Driver */
961  ModuleObject.DllBase = NULL;
962  ModuleObject.SizeOfImage = 0;
963  ModuleObject.EntryPoint = RawFsDriverEntry;
964  RtlInitUnicodeString(&DriverName, L"RAW");
965 
966  /* Initialize it */
968  &ModuleObject,
969  &DriverName,
970  TRUE,
971  &DriverObject);
972  if (!NT_SUCCESS(Status))
973  {
974  /* Fail */
975  return;
976  }
977 
978  /* Now initialize the associated device */
980  if (!NT_SUCCESS(Status))
981  {
982  /* Fail */
984  return;
985  }
986 
987  /* Start it up */
989  if (!NT_SUCCESS(Status))
990  {
991  /* Fail */
993  return;
994  }
995 
996  /* Get highest group order index */
998  if (IopGroupIndex == 0xFFFF) ASSERT(FALSE);
999 
1000  /* Allocate the group table */
1002  IopGroupIndex * sizeof(LIST_ENTRY),
1003  TAG_IO);
1004  if (IopGroupTable == NULL) ASSERT(FALSE);
1005 
1006  /* Initialize the group table lists */
1007  for (i = 0; i < IopGroupIndex; i++) InitializeListHead(&IopGroupTable[i]);
1008 
1009  /* Loop the boot modules */
1010  ListHead = &KeLoaderBlock->LoadOrderListHead;
1011  NextEntry = ListHead->Flink;
1012  while (ListHead != NextEntry)
1013  {
1014  /* Get the entry */
1015  LdrEntry = CONTAINING_RECORD(NextEntry,
1017  InLoadOrderLinks);
1018 
1019  /* Check if the DLL needs to be initialized */
1020  if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
1021  {
1022  /* Call its entrypoint */
1023  MmCallDllInitialize(LdrEntry, NULL);
1024  }
1025 
1026  /* Go to the next driver */
1027  NextEntry = NextEntry->Flink;
1028  }
1029 
1030  /* Loop the boot drivers */
1031  ListHead = &KeLoaderBlock->BootDriverListHead;
1032  NextEntry = ListHead->Flink;
1033  while (ListHead != NextEntry)
1034  {
1035  /* Get the entry */
1036  BootEntry = CONTAINING_RECORD(NextEntry,
1038  Link);
1039 
1040  /* Get the driver loader entry */
1041  LdrEntry = BootEntry->LdrEntry;
1042 
1043  /* Allocate our internal accounting structure */
1045  sizeof(DRIVER_INFORMATION),
1046  TAG_IO);
1047  if (DriverInfo)
1048  {
1049  /* Zero it and initialize it */
1052  DriverInfo->DataTableEntry = BootEntry;
1053 
1054  /* Open the registry key */
1056  NULL,
1057  &BootEntry->RegistryPath,
1058  KEY_READ);
1059  DPRINT("IopOpenRegistryKeyEx(%wZ) returned 0x%08lx\n", &BootEntry->RegistryPath, Status);
1060 #if 0
1061  if (NT_SUCCESS(Status))
1062 #else // Hack still needed...
1063  if ((NT_SUCCESS(Status)) || /* ReactOS HACK for SETUPLDR */
1064  ((KeLoaderBlock->SetupLdrBlock) && ((KeyHandle = (PVOID)1)))) // yes, it's an assignment!
1065 #endif
1066  {
1067  /* Save the handle */
1069 
1070  /* Get the group oder index */
1072 
1073  /* Get the tag position */
1075 
1076  /* Insert it into the list, at the right place */
1078  NextEntry2 = IopGroupTable[Index].Flink;
1079  while (NextEntry2 != &IopGroupTable[Index])
1080  {
1081  /* Get the driver info */
1082  DriverInfoTag = CONTAINING_RECORD(NextEntry2,
1084  Link);
1085 
1086  /* Check if we found the right tag position */
1087  if (DriverInfoTag->TagPosition > DriverInfo->TagPosition)
1088  {
1089  /* We're done */
1090  break;
1091  }
1092 
1093  /* Next entry */
1094  NextEntry2 = NextEntry2->Flink;
1095  }
1096 
1097  /* Insert us right before the next entry */
1098  NextEntry2 = NextEntry2->Blink;
1099  InsertHeadList(NextEntry2, &DriverInfo->Link);
1100  }
1101  }
1102 
1103  /* Go to the next driver */
1104  NextEntry = NextEntry->Flink;
1105  }
1106 
1107  /* Loop each group index */
1108  for (i = 0; i < IopGroupIndex; i++)
1109  {
1110  /* Loop each group table */
1111  NextEntry = IopGroupTable[i].Flink;
1112  while (NextEntry != &IopGroupTable[i])
1113  {
1114  /* Get the entry */
1115  DriverInfo = CONTAINING_RECORD(NextEntry,
1117  Link);
1118 
1119  /* Get the driver loader entry */
1120  LdrEntry = DriverInfo->DataTableEntry->LdrEntry;
1121 
1122  /* Initialize it */
1123  IopInitializeBuiltinDriver(LdrEntry);
1124 
1125  /* Next entry */
1126  NextEntry = NextEntry->Flink;
1127  }
1128  }
1129 
1130  /* HAL Root Bus is being initialized before loading the boot drivers so this may cause issues
1131  * when some devices are not being initialized with their drivers. This flag is used to delay
1132  * all actions with devices (except PnP root device) until boot drivers are loaded.
1133  * See PiQueueDeviceAction function
1134  */
1136 }
PDEVICE_NODE IopRootDeviceNode
Definition: devnode.c:18
INIT_FUNCTION NTSTATUS NTAPI RawFsDriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: rawfs.c:1193
#define TAG_IO
Definition: tag.h:69
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4711
#define KEY_READ
Definition: nt_native.h:1023
#define TRUE
Definition: types.h:120
struct _LIST_ENTRY * Blink
Definition: typedefs.h:122
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS IopCreateDeviceNode(IN PDEVICE_NODE ParentNode, IN PDEVICE_OBJECT PhysicalDeviceObject, IN PUNICODE_STRING ServiceName, OUT PDEVICE_NODE *DeviceNode)
ULONG SizeOfImage
Definition: ldrtypes.h:143
INIT_FUNCTION NTSTATUS NTAPI IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
Definition: driver.c:810
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define LDRP_DRIVER_DEPENDENT_DLL
Definition: ldrtypes.h:56
static int Link(const char **args)
Definition: vfdcmd.c:2414
USHORT NTAPI PpInitGetGroupOrderIndex(IN HANDLE ServiceHandle)
Definition: pnpinit.c:154
Definition: arc.h:198
PVOID DllBase
Definition: btrfs_drv.h:1926
NTSTATUS IopStartDevice(IN PDEVICE_NODE DeviceNode)
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
struct _SETUP_LOADER_BLOCK * SetupLdrBlock
Definition: arc.h:511
#define FALSE
Definition: types.h:117
NTSTATUS NTAPI MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry, IN PLIST_ENTRY ListHead)
Definition: sysldr.c:309
PVOID EntryPoint
Definition: ntddk_ex.h:203
LIST_ENTRY Link
Definition: io.h:411
smooth NULL
Definition: ftsmooth.c:416
HANDLE ServiceHandle
Definition: io.h:414
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
USHORT TagPosition
Definition: io.h:415
NTSTATUS FASTCALL IopInitializeDevice(IN PDEVICE_NODE DeviceNode, IN PDRIVER_OBJECT DriverObject)
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const UCHAR Index[8]
Definition: usbohci.c:18
PLIST_ENTRY IopGroupTable
Definition: driver.c:42
LIST_ENTRY BootDriverListHead
Definition: arc.h:495
Definition: Node.h:9
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
PBOOT_DRIVER_LIST_ENTRY DataTableEntry
Definition: io.h:413
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
Definition: btrfs_drv.h:1922
Definition: typedefs.h:119
LIST_ENTRY LoadOrderListHead
Definition: arc.h:493
UNICODE_STRING RegistryPath
Definition: arc.h:202
DRIVER_INFORMATION DriverInfo
Definition: main.c:59
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:1599
NTSTATUS FASTCALL IopInitializeDriverModule(IN PDEVICE_NODE DeviceNode, IN PLDR_DATA_TABLE_ENTRY ModuleObject, IN PUNICODE_STRING ServiceName, IN BOOLEAN FileSystemDriver, OUT PDRIVER_OBJECT *DriverObject)
Definition: driver.c:469
struct _LDR_DATA_TABLE_ENTRY * LdrEntry
Definition: arc.h:203
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
BOOLEAN PnPBootDriversLoaded
Definition: pnpinit.c:26
ULONG Flags
Definition: ntddk_ex.h:207
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
USHORT IopGroupIndex
Definition: driver.c:41
USHORT NTAPI PipGetDriverTagPriority(IN HANDLE ServiceHandle)
Definition: pnpinit.c:197

Referenced by IoInitSystem().

◆ IopInitializeBuiltinDriver()

INIT_FUNCTION NTSTATUS NTAPI IopInitializeBuiltinDriver ( IN PLDR_DATA_TABLE_ENTRY  BootLdrEntry)

Definition at line 810 of file driver.c.

811 {
815  PWCHAR Buffer, FileNameWithoutPath;
816  PWSTR FileExtension;
817  PUNICODE_STRING ModuleName = &BootLdrEntry->BaseDllName;
818  PLDR_DATA_TABLE_ENTRY LdrEntry;
819  PLIST_ENTRY NextEntry;
822 
823  /*
824  * Display 'Loading XXX...' message
825  */
828 
830  ModuleName->Length + sizeof(UNICODE_NULL),
831  TAG_IO);
832  if (Buffer == NULL)
833  {
835  }
836 
838  Buffer[ModuleName->Length / sizeof(WCHAR)] = UNICODE_NULL;
839 
840  /*
841  * Generate filename without path (not needed by freeldr)
842  */
843  FileNameWithoutPath = wcsrchr(Buffer, L'\\');
844  if (FileNameWithoutPath == NULL)
845  {
846  FileNameWithoutPath = Buffer;
847  }
848  else
849  {
850  FileNameWithoutPath++;
851  }
852 
853  /*
854  * Strip the file extension from ServiceName
855  */
856  Success = RtlCreateUnicodeString(&ServiceName, FileNameWithoutPath);
858  if (!Success)
859  {
861  }
862 
863  FileExtension = wcsrchr(ServiceName.Buffer, L'.');
864  if (FileExtension != NULL)
865  {
866  ServiceName.Length -= (USHORT)wcslen(FileExtension) * sizeof(WCHAR);
867  FileExtension[0] = UNICODE_NULL;
868  }
869 
870  /*
871  * Determine the right device object
872  */
873  /* Use IopRootDeviceNode for now */
875  NULL,
876  &ServiceName,
877  &DeviceNode);
879  if (!NT_SUCCESS(Status))
880  {
881  DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName, Status);
882  return Status;
883  }
884 
885  /* Lookup the new Ldr entry in PsLoadedModuleList */
886  NextEntry = PsLoadedModuleList.Flink;
887  while (NextEntry != &PsLoadedModuleList)
888  {
889  LdrEntry = CONTAINING_RECORD(NextEntry,
891  InLoadOrderLinks);
893  {
894  break;
895  }
896 
897  NextEntry = NextEntry->Flink;
898  }
899  ASSERT(NextEntry != &PsLoadedModuleList);
900 
901  /*
902  * Initialize the driver
903  */
905  LdrEntry,
906  &DeviceNode->ServiceName,
907  FALSE,
908  &DriverObject);
909 
910  if (!NT_SUCCESS(Status))
911  {
912  return Status;
913  }
914 
916  if (NT_SUCCESS(Status))
917  {
919  }
920 
921  /* Remove extra reference from IopInitializeDriverModule */
923 
924  return Status;
925 }
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
PDEVICE_NODE IopRootDeviceNode
Definition: devnode.c:18
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
#define TRUE
Definition: types.h:120
VOID FASTCALL IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
Definition: driver.c:209
uint16_t * PWSTR
Definition: typedefs.h:56
INIT_FUNCTION VOID NTAPI InbvIndicateProgress(VOID)
Definition: inbv.c:884
ACPI_SIZE Length
Definition: actypes.h:1044
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS IopCreateDeviceNode(IN PDEVICE_NODE ParentNode, IN PDEVICE_OBJECT PhysicalDeviceObject, IN PUNICODE_STRING ServiceName, OUT PDEVICE_NODE *DeviceNode)
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
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:1274
uint16_t * PWCHAR
Definition: typedefs.h:56
NTSTATUS IopStartDevice(IN PDEVICE_NODE DeviceNode)
#define FALSE
Definition: types.h:117
#define UNICODE_NULL
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
Definition: bufpool.h:45
NTSTATUS FASTCALL IopInitializeDevice(IN PDEVICE_NODE DeviceNode, IN PDRIVER_OBJECT DriverObject)
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
Definition: Node.h:9
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
Definition: btrfs_drv.h:1922
Definition: typedefs.h:119
#define wcsrchr
Definition: compat.h:16
NTSTATUS FASTCALL IopInitializeDriverModule(IN PDEVICE_NODE DeviceNode, IN PLDR_DATA_TABLE_ENTRY ModuleObject, IN PUNICODE_STRING ServiceName, IN BOOLEAN FileSystemDriver, OUT PDRIVER_OBJECT *DriverObject)
Definition: driver.c:469
unsigned short USHORT
Definition: pedump.c:61
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
#define DPRINT1
Definition: precomp.h:8
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)

Referenced by IopInitializeBootDrivers().

◆ IopInitializeDriverModule()

NTSTATUS FASTCALL IopInitializeDriverModule ( IN PDEVICE_NODE  DeviceNode,
IN PLDR_DATA_TABLE_ENTRY  ModuleObject,
IN PUNICODE_STRING  ServiceName,
IN BOOLEAN  FileSystemDriver,
OUT PDRIVER_OBJECT DriverObject 
)

Definition at line 469 of file driver.c.

475 {
476  static const WCHAR ServicesKeyName[] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\";
477  UNICODE_STRING DriverName;
478  UNICODE_STRING RegistryKey;
482 
483  DriverEntry = ModuleObject->EntryPoint;
484 
485  if (ServiceName != NULL && ServiceName->Length != 0)
486  {
487  RegistryKey.Length = 0;
488  RegistryKey.MaximumLength = sizeof(ServicesKeyName) + ServiceName->Length;
489  RegistryKey.Buffer = ExAllocatePoolWithTag(PagedPool,
490  RegistryKey.MaximumLength,
491  TAG_IO);
492  if (RegistryKey.Buffer == NULL)
493  {
495  }
496  RtlAppendUnicodeToString(&RegistryKey, ServicesKeyName);
498  }
499  else
500  {
501  RtlInitEmptyUnicodeString(&RegistryKey, NULL, 0);
502  }
503 
504  /* Create ModuleName string */
505  if (ServiceName && ServiceName->Length > 0)
506  {
507  DriverName.Length = 0;
508  DriverName.MaximumLength = sizeof(FILESYSTEM_ROOT_NAME) + ServiceName->Length;
510  DriverName.MaximumLength,
511  TAG_IO);
512  if (DriverName.Buffer == NULL)
513  {
514  RtlFreeUnicodeString(&RegistryKey);
516  }
517 
518  if (FileSystemDriver != FALSE)
520  else
523 
524  DPRINT("Driver name: '%wZ'\n", &DriverName);
525  }
526  else
527  {
528  RtlInitEmptyUnicodeString(&DriverName, NULL, 0);
529  }
530 
531  Status = IopCreateDriver(DriverName.Length > 0 ? &DriverName : NULL,
532  DriverEntry,
533  &RegistryKey,
534  ServiceName,
535  ModuleObject,
536  &Driver);
537  RtlFreeUnicodeString(&RegistryKey);
538  RtlFreeUnicodeString(&DriverName);
539 
540  if (!NT_SUCCESS(Status))
541  {
542  DPRINT("IopCreateDriver() failed (Status 0x%08lx)\n", Status);
543  return Status;
544  }
545 
546  *DriverObject = Driver;
547 
549 
550  /* Set the driver as initialized */
552 
554 
555  return STATUS_SUCCESS;
556 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
VOID NTAPI IopReinitializeDrivers(VOID)
Definition: driver.c:1419
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS NTAPI IopCreateDriver(IN PUNICODE_STRING DriverName OPTIONAL, IN PDRIVER_INITIALIZE InitializationFunction, IN PUNICODE_STRING RegistryPath OPTIONAL, IN PCUNICODE_STRING ServiceName, IN PLDR_DATA_TABLE_ENTRY ModuleObject OPTIONAL, OUT PDRIVER_OBJECT *pDriverObject)
Definition: driver.c:1494
#define FILESYSTEM_ROOT_NAME
Definition: ldr.h:6
BOOLEAN PnpSystemInit
Definition: iomgr.c:17
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
Definition: driver.c:10
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
_In_ PCUNICODE_STRING _In_ PVOID Driver
Definition: cmfuncs.h:32
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
VOID NTAPI MmFreeDriverInitialization(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
Definition: sysldr.c:1636
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
VOID NTAPI IopReadyDeviceObjects(IN PDRIVER_OBJECT Driver)
Definition: device.c:34
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
DRIVER_INITIALIZE * PDRIVER_INITIALIZE
Definition: iotypes.h:2215
static const WCHAR L[]
Definition: oid.c:1250
Definition: btrfs_drv.h:1922
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define DRIVER_ROOT_NAME
Definition: ldr.h:5

Referenced by IopActionInitChildServices(), IopAttachFilterDriversCallback(), IopInitializeBootDrivers(), IopInitializeBuiltinDriver(), and IopLoadUnloadDriver().

◆ IopInitializeSystemDrivers()

INIT_FUNCTION VOID FASTCALL IopInitializeSystemDrivers ( VOID  )

Definition at line 1141 of file driver.c.

1142 {
1143  PUNICODE_STRING *DriverList, *SavedList;
1144 
1145  /* No system drivers on the boot cd */
1146  if (KeLoaderBlock->SetupLdrBlock) return; // ExpInTextModeSetup
1147 
1148  /* Get the driver list */
1149  SavedList = DriverList = CmGetSystemDriverList();
1150  ASSERT(DriverList);
1151 
1152  /* Loop it */
1153  while (*DriverList)
1154  {
1155  /* Load the driver */
1156  ZwLoadDriver(*DriverList);
1157 
1158  /* Free the entry */
1159  RtlFreeUnicodeString(*DriverList);
1160  ExFreePool(*DriverList);
1161 
1162  /* Next entry */
1164  DriverList++;
1165  }
1166 
1167  /* Free the list */
1168  ExFreePool(SavedList);
1169 }
INIT_FUNCTION VOID NTAPI InbvIndicateProgress(VOID)
Definition: inbv.c:884
INIT_FUNCTION PUNICODE_STRING *NTAPI CmGetSystemDriverList(VOID)
Definition: cmsysini.c:1776
struct _SETUP_LOADER_BLOCK * SetupLdrBlock
Definition: arc.h:511
PLOADER_PARAMETER_BLOCK KeLoaderBlock
Definition: krnlinit.c:29
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

Referenced by IoInitSystem().

◆ IopInvalidDeviceRequest()

NTSTATUS NTAPI IopInvalidDeviceRequest ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 48 of file driver.c.

51 {
52  Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
53  Irp->IoStatus.Information = 0;
56 }
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
#define IoCompleteRequest
Definition: irp.c:1240
#define IO_NO_INCREMENT
Definition: iotypes.h:581

Referenced by IopCreateDriver().

◆ IopLoadServiceModule()

NTSTATUS FASTCALL IopLoadServiceModule ( IN PUNICODE_STRING  ServiceName,
OUT PLDR_DATA_TABLE_ENTRY ModuleObject 
)

Definition at line 316 of file driver.c.

319 {
322  UNICODE_STRING ServiceImagePath, CCSName;
324  HANDLE CCSKey, ServiceKey;
326 
328  ASSERT(ServiceName->Length);
329  DPRINT("IopLoadServiceModule(%wZ, 0x%p)\n", ServiceName, ModuleObject);
330 
331  if (ExpInTextModeSetup)
332  {
333  /* We have no registry, but luckily we know where all the drivers are */
334  DPRINT1("IopLoadServiceModule(%wZ, 0x%p) called in ExpInTextModeSetup mode...\n", ServiceName, ModuleObject);
335 
336  /* ServiceStart < 4 is all that matters */
337  ServiceStart = 0;
338 
339  /* IopNormalizeImagePath will do all of the work for us if we give it an empty string */
340  RtlInitEmptyUnicodeString(&ServiceImagePath, NULL, 0);
341  }
342  else
343  {
344  /* Open CurrentControlSet */
345  RtlInitUnicodeString(&CCSName,
346  L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
347  Status = IopOpenRegistryKeyEx(&CCSKey, NULL, &CCSName, KEY_READ);
348  if (!NT_SUCCESS(Status))
349  {
350  DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
351  &CCSName, Status);
352  return Status;
353  }
354 
355  /* Open service key */
356  Status = IopOpenRegistryKeyEx(&ServiceKey, CCSKey, ServiceName, KEY_READ);
357  if (!NT_SUCCESS(Status))
358  {
359  DPRINT1("IopOpenRegistryKeyEx() failed for '%wZ' with status 0x%lx\n",
361  ZwClose(CCSKey);
362  return Status;
363  }
364 
365  /*
366  * Get information about the service.
367  */
369 
370  RtlInitUnicodeString(&ServiceImagePath, NULL);
371 
372  QueryTable[0].Name = L"Start";
375 
376  QueryTable[1].Name = L"ImagePath";
378  QueryTable[1].EntryContext = &ServiceImagePath;
379 
381  (PWSTR)ServiceKey,
382  QueryTable,
383  NULL,
384  NULL);
385 
386  ZwClose(ServiceKey);
387  ZwClose(CCSKey);
388 
389  if (!NT_SUCCESS(Status))
390  {
391  DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
392  return Status;
393  }
394  }
395 
396  /*
397  * Normalize the image path for all later processing.
398  */
399  Status = IopNormalizeImagePath(&ServiceImagePath, ServiceName);
400 
401  if (!NT_SUCCESS(Status))
402  {
403  DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
404  return Status;
405  }
406 
407  /*
408  * Case for disabled drivers
409  */
410  if (ServiceStart >= 4)
411  {
412  /* We can't load this */
414  }
415  else
416  {
417  DPRINT("Loading module from %wZ\n", &ServiceImagePath);
418  Status = MmLoadSystemImage(&ServiceImagePath, NULL, NULL, 0, (PVOID)ModuleObject, &BaseAddress);
419  if (NT_SUCCESS(Status))
420  {
422  }
423  }
424 
425  ExFreePool(ServiceImagePath.Buffer);
426 
427  /*
428  * Now check if the module was loaded successfully.
429  */
430  if (!NT_SUCCESS(Status))
431  {
432  DPRINT("Module loading failed (Status %x)\n", Status);
433  }
434 
435  DPRINT("Module loading (Status %x)\n", Status);
436 
437  return Status;
438 }
BOOLEAN ExpInTextModeSetup
Definition: init.c:67
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4142
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
#define KEY_READ
Definition: nt_native.h:1023
VOID FASTCALL IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
Definition: driver.c:209
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
uint16_t * PWSTR
Definition: typedefs.h:56
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_DRIVER_UNABLE_TO_LOAD
Definition: ntstatus.h:745
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:248
smooth NULL
Definition: ftsmooth.c:416
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite(IN PERESOURCE Resource)
Definition: resource.c:1619
void DPRINT(...)
Definition: polytest.cpp:61
VOID ServiceStart(DWORD argc, LPTSTR *argv)
Definition: nfs41_daemon.c:381
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
#define RTL_REGISTRY_HANDLE
Definition: nt_native.h:168
ERESOURCE IopDriverLoadResource
Definition: driver.c:19
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:1599
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
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:2819

Referenced by IopActionInitChildServices(), and IopAttachFilterDriversCallback().

◆ IopLoadUnloadDriver()

NTSTATUS NTAPI IopLoadUnloadDriver ( _In_opt_ PCUNICODE_STRING  RegistryPath,
_Inout_ PDRIVER_OBJECT DriverObject 
)

Definition at line 1914 of file driver.c.

1917 {
1919  UNICODE_STRING ImagePath;
1921  NTSTATUS Status;
1922  ULONG Type;
1924  PLDR_DATA_TABLE_ENTRY ModuleObject;
1926  WCHAR *cur;
1927 
1928  /* Load/Unload must be called from system process */
1930  {
1931  LOAD_UNLOAD_PARAMS LoadParams;
1932 
1933  /* Prepare parameters block */
1934  LoadParams.RegistryPath = RegistryPath;
1935  LoadParams.DriverObject = *DriverObject;
1937 
1938  /* Initialize and queue a work item */
1939  ExInitializeWorkItem(&LoadParams.WorkItem,
1941  &LoadParams);
1943 
1944  /* And wait till it completes */
1945  KeWaitForSingleObject(&LoadParams.Event,
1946  UserRequest,
1947  KernelMode,
1948  FALSE,
1949  NULL);
1950  return LoadParams.Status;
1951  }
1952 
1953  /* Check if it's an unload request */
1954  if (*DriverObject)
1955  {
1956  (*DriverObject)->DriverUnload(*DriverObject);
1957  return STATUS_SUCCESS;
1958  }
1959 
1960  RtlInitUnicodeString(&ImagePath, NULL);
1961 
1962  /*
1963  * Get the service name from the registry key name.
1964  */
1965  ASSERT(RegistryPath->Length >= sizeof(WCHAR));
1966 
1968  cur = RegistryPath->Buffer + RegistryPath->Length / sizeof(WCHAR) - 1;
1969  while (RegistryPath->Buffer != cur)
1970  {
1971  if (*cur == L'\\')
1972  {
1973  ServiceName.Buffer = cur + 1;
1974  ServiceName.Length = RegistryPath->Length -
1975  (USHORT)((ULONG_PTR)ServiceName.Buffer -
1976  (ULONG_PTR)RegistryPath->Buffer);
1977  break;
1978  }
1979  cur--;
1980  }
1981 
1982  /*
1983  * Get service type.
1984  */
1985  RtlZeroMemory(&QueryTable, sizeof(QueryTable));
1986 
1987  RtlInitUnicodeString(&ImagePath, NULL);
1988 
1989  QueryTable[0].Name = L"Type";
1992 
1993  QueryTable[1].Name = L"ImagePath";
1995  QueryTable[1].EntryContext = &ImagePath;
1996 
1998  RegistryPath->Buffer,
1999  QueryTable, NULL, NULL);
2000  if (!NT_SUCCESS(Status))
2001  {
2002  DPRINT("RtlQueryRegistryValues() failed (Status %lx)\n", Status);
2003  if (ImagePath.Buffer) ExFreePool(ImagePath.Buffer);
2004  return Status;
2005  }
2006 
2007  /*
2008  * Normalize the image path for all later processing.
2009  */
2010  Status = IopNormalizeImagePath(&ImagePath, &ServiceName);
2011  if (!NT_SUCCESS(Status))
2012  {
2013  DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
2014  return Status;
2015  }
2016 
2017  DPRINT("FullImagePath: '%wZ'\n", &ImagePath);
2018  DPRINT("Type: %lx\n", Type);
2019 
2022  /*
2023  * Get existing DriverObject pointer (in case the driver
2024  * has already been loaded and initialized).
2025  */
2027  &ServiceName,
2030 
2031  if (!NT_SUCCESS(Status))
2032  {
2033  /*
2034  * Load the driver module
2035  */
2036  DPRINT("Loading module from %wZ\n", &ImagePath);
2037  Status = MmLoadSystemImage(&ImagePath, NULL, NULL, 0, (PVOID)&ModuleObject, &BaseAddress);
2038  if (!NT_SUCCESS(Status))
2039  {
2040  DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status);
2043  return Status;
2044  }
2045 
2046  /*
2047  * Initialize the driver module if it's loaded for the first time
2048  */
2050  if (!NT_SUCCESS(Status))
2051  {
2052  DPRINT1("IopCreateDeviceNode() failed (Status %lx)\n", Status);
2055  MmUnloadSystemImage(ModuleObject);
2056  return Status;
2057  }
2058 
2059  IopDisplayLoadingMessage(&DeviceNode->ServiceName);
2060 
2062  ModuleObject,
2063  &DeviceNode->ServiceName,
2066  DriverObject);
2067  if (!NT_SUCCESS(Status))
2068  {
2069  DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status);
2072  MmUnloadSystemImage(ModuleObject);
2073  return Status;
2074  }
2075 
2078 
2079  /* Initialize and start device */
2082  }
2083  else
2084  {
2087 
2088  DPRINT("DriverObject already exist in ObjectManager\n");
2090 
2091  /* IopGetDriverObject references the DriverObject, so dereference it */
2093  }
2094 
2095  return Status;
2096 }
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4142
PDEVICE_NODE IopRootDeviceNode
Definition: devnode.c:18
VOID NTAPI ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem, IN WORK_QUEUE_TYPE QueueType)
Definition: work.c:717
Type
Definition: Type.h:6
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
#define TRUE
Definition: types.h:120
VOID FASTCALL IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
Definition: driver.c:209
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS IopCreateDeviceNode(IN PDEVICE_NODE ParentNode, IN PDEVICE_OBJECT PhysicalDeviceObject, IN PUNICODE_STRING ServiceName, OUT PDEVICE_NODE *DeviceNode)
WORK_QUEUE_ITEM WorkItem
Definition: io.h:401
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
KEVENT Event
Definition: io.h:402
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define SERVICE_RECOGNIZER_DRIVER
Definition: cmtypes.h:956
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
NTSTATUS IopStartDevice(IN PDEVICE_NODE DeviceNode)
#define FALSE
Definition: types.h:117
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
#define PsGetCurrentProcess
Definition: psfuncs.h:17
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:248
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
NTSTATUS FASTCALL IopInitializeDevice(IN PDEVICE_NODE DeviceNode, IN PDRIVER_OBJECT DriverObject)
NTSTATUS FASTCALL IopGetDriverObject(PDRIVER_OBJECT *DriverObject, PUNICODE_STRING ServiceName, BOOLEAN FileSystem)
Definition: driver.c:107
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:912
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_In_z_ PWSTR RegistryPath
Definition: classp.h:1930
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
Definition: Node.h:9
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
NTSTATUS Status
Definition: io.h:399
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
Definition: btrfs_drv.h:1922
#define RTL_REGISTRY_ABSOLUTE
Definition: nt_native.h:161
PDRIVER_OBJECT DriverObject
Definition: io.h:403
ERESOURCE IopDriverLoadResource
Definition: driver.c:19
PCUNICODE_STRING RegistryPath
Definition: io.h:400
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS FASTCALL IopInitializeDriverModule(IN PDEVICE_NODE DeviceNode, IN PLDR_DATA_TABLE_ENTRY ModuleObject, IN PUNICODE_STRING ServiceName, IN BOOLEAN FileSystemDriver, OUT PDRIVER_OBJECT *DriverObject)
Definition: driver.c:469
unsigned short USHORT
Definition: pedump.c:61
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ULONG_PTR
Definition: config.h:101
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define RTL_QUERY_REGISTRY_REQUIRED
Definition: nt_native.h:132
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
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:2819
#define STATUS_IMAGE_ALREADY_LOADED
Definition: ntstatus.h:506
VOID NTAPI IopLoadUnloadDriverWorker(_Inout_ PVOID Parameter)
Definition: driver.c:1901

Referenced by IopLoadUnloadDriverWorker(), IopUnloadDriver(), and NtLoadDriver().

◆ IopLoadUnloadDriverWorker()

VOID NTAPI IopLoadUnloadDriverWorker ( _Inout_ PVOID  Parameter)

Definition at line 1901 of file driver.c.

1903 {
1904  PLOAD_UNLOAD_PARAMS LoadParams = Parameter;
1905 
1907  LoadParams->Status = IopLoadUnloadDriver(LoadParams->RegistryPath,
1908  &LoadParams->DriverObject);
1909  KeSetEvent(&LoadParams->Event, 0, FALSE);
1910 }
_In_ PVOID Parameter
Definition: ldrtypes.h:241
KEVENT Event
Definition: io.h:402
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
#define PsGetCurrentProcess
Definition: psfuncs.h:17
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
NTSTATUS Status
Definition: io.h:399
PDRIVER_OBJECT DriverObject
Definition: io.h:403
PCUNICODE_STRING RegistryPath
Definition: io.h:400
NTSTATUS NTAPI IopLoadUnloadDriver(_In_opt_ PCUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1914

Referenced by IopLoadUnloadDriver().

◆ IopNormalizeImagePath()

NTSTATUS FASTCALL IopNormalizeImagePath ( _Inout_ _When_(return >=0, _At_(ImagePath->Buffer, _Post_notnull_ __drv_allocatesMem(Mem))) PUNICODE_STRING  ImagePath,
_In_ PUNICODE_STRING  ServiceName 
)

Definition at line 248 of file driver.c.

252 {
253  UNICODE_STRING SystemRootString = RTL_CONSTANT_STRING(L"\\SystemRoot\\");
254  UNICODE_STRING DriversPathString = RTL_CONSTANT_STRING(L"\\SystemRoot\\System32\\drivers\\");
255  UNICODE_STRING DotSysString = RTL_CONSTANT_STRING(L".sys");
256  UNICODE_STRING InputImagePath;
257 
258  DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
259 
260  InputImagePath = *ImagePath;
261  if (InputImagePath.Length == 0)
262  {
263  ImagePath->Length = 0;
264  ImagePath->MaximumLength = DriversPathString.Length +
265  ServiceName->Length +
266  DotSysString.Length +
267  sizeof(UNICODE_NULL);
268  ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
269  ImagePath->MaximumLength,
270  TAG_IO);
271  if (ImagePath->Buffer == NULL)
272  return STATUS_NO_MEMORY;
273 
274  RtlCopyUnicodeString(ImagePath, &DriversPathString);
276  RtlAppendUnicodeStringToString(ImagePath, &DotSysString);
277  }
278  else if (InputImagePath.Buffer[0] != L'\\')
279  {
280  ImagePath->Length = 0;
281  ImagePath->MaximumLength = SystemRootString.Length +
282  InputImagePath.Length +
283  sizeof(UNICODE_NULL);
284  ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
285  ImagePath->MaximumLength,
286  TAG_IO);
287  if (ImagePath->Buffer == NULL)
288  return STATUS_NO_MEMORY;
289 
290  RtlCopyUnicodeString(ImagePath, &SystemRootString);
291  RtlAppendUnicodeStringToString(ImagePath, &InputImagePath);
292 
293  /* Free caller's string */
294  ExFreePoolWithTag(InputImagePath.Buffer, TAG_RTLREGISTRY);
295  }
296 
297  DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
298 
299  return STATUS_SUCCESS;
300 }
#define TAG_IO
Definition: tag.h:69
#define TAG_RTLREGISTRY
Definition: driver.c:34
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
#define UNICODE_NULL
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static const WCHAR L[]
Definition: oid.c:1250
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Referenced by IopLoadServiceModule(), IopLoadUnloadDriver(), and IopUnloadDriver().

◆ IopReinitializeBootDrivers()

VOID NTAPI IopReinitializeBootDrivers ( VOID  )

Definition at line 1455 of file driver.c.

1456 {
1457  PDRIVER_REINIT_ITEM ReinitItem;
1459 
1460  /* Get the first entry and start looping */
1463  while (Entry)
1464  {
1465  /* Get the item */
1466  ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1467 
1468  /* Increment reinitialization counter */
1469  ReinitItem->DriverObject->DriverExtension->Count++;
1470 
1471  /* Remove the device object flag */
1473 
1474  /* Call the routine */
1475  ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1476  ReinitItem->Context,
1477  ReinitItem->DriverObject->
1478  DriverExtension->Count);
1479 
1480  /* Free the entry */
1481  ExFreePool(Entry);
1482 
1483  /* Move to the next one */
1486  }
1487 
1488  /* Wait for all device actions being finished*/
1490 }
struct _Entry Entry
Definition: kefuncs.h:627
PVOID Context
Definition: io.h:471
LIST_ENTRY DriverBootReinitListHead
Definition: driver.c:26
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
KSPIN_LOCK DriverBootReinitListLock
Definition: driver.c:27
#define FALSE
Definition: types.h:117
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2262
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
smooth NULL
Definition: ftsmooth.c:416
PDRIVER_OBJECT DriverObject
Definition: io.h:469
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KEVENT PiEnumerationFinished
Definition: devaction.c:49
Definition: typedefs.h:119
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:41
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:470
base of all file and directory entries
Definition: entries.h:82
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define DRVO_BOOTREINIT_REGISTERED
Definition: iotypes.h:4450

Referenced by IoInitSystem().

◆ IopReinitializeDrivers()

VOID NTAPI IopReinitializeDrivers ( VOID  )

Definition at line 1419 of file driver.c.

1420 {
1421  PDRIVER_REINIT_ITEM ReinitItem;
1423 
1424  /* Get the first entry and start looping */
1427  while (Entry)
1428  {
1429  /* Get the item */
1430  ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1431 
1432  /* Increment reinitialization counter */
1433  ReinitItem->DriverObject->DriverExtension->Count++;
1434 
1435  /* Remove the device object flag */
1436  ReinitItem->DriverObject->Flags &= ~DRVO_REINIT_REGISTERED;
1437 
1438  /* Call the routine */
1439  ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1440  ReinitItem->Context,
1441  ReinitItem->DriverObject->
1442  DriverExtension->Count);
1443 
1444  /* Free the entry */
1445  ExFreePool(Entry);
1446 
1447  /* Move to the next one */
1450  }
1451 }
struct _Entry Entry
Definition: kefuncs.h:627
PVOID Context
Definition: io.h:471
#define DRVO_REINIT_REGISTERED
Definition: iotypes.h:4448
KSPIN_LOCK DriverReinitListLock
Definition: driver.c:22
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2262
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
LIST_ENTRY DriverReinitListHead
Definition: driver.c:21
PDRIVER_OBJECT DriverObject
Definition: io.h:469
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
Definition: typedefs.h:119
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:41
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:470
base of all file and directory entries
Definition: entries.h:82
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

Referenced by IoInitSystem(), and IopInitializeDriverModule().

◆ IopSuffixUnicodeString()

BOOLEAN NTAPI IopSuffixUnicodeString ( IN PCUNICODE_STRING  String1,
IN PCUNICODE_STRING  String2 
)

Definition at line 175 of file driver.c.

178 {
179  PWCHAR pc1;
180  PWCHAR pc2;
181  ULONG Length;
182 
183  if (String2->Length < String1->Length)
184  return FALSE;
185 
186  Length = String1->Length / 2;
187  pc1 = String1->Buffer;
188  pc2 = &String2->Buffer[String2->Length / sizeof(WCHAR) - Length];
189 
190  if (pc1 && pc2)
191  {
192  while (Length--)
193  {
194  if( *pc1++ != *pc2++ )
195  return FALSE;
196  }
197  return TRUE;
198  }
199  return FALSE;
200 }
static const unsigned char pc2[48]
Definition: des.c:68
static const unsigned char pc1[56]
Definition: des.c:54
#define TRUE
Definition: types.h:120
uint16_t * PWCHAR
Definition: typedefs.h:56
#define FALSE
Definition: types.h:117
__wchar_t WCHAR
Definition: xmlstorage.h:180
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
_In_ const STRING * String2
Definition: rtlfuncs.h:2291
unsigned int ULONG
Definition: retypes.h:1

Referenced by IopDisplayLoadingMessage().

◆ IopUnloadDriver()

NTSTATUS NTAPI IopUnloadDriver ( PUNICODE_STRING  DriverServiceName,
BOOLEAN  UnloadPnpDrivers 
)

Definition at line 1193 of file driver.c.

1194 {
1197  UNICODE_STRING ImagePath;
1202  PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
1203  NTSTATUS Status;
1204  USHORT LastBackslash;
1205  BOOLEAN SafeToUnload = TRUE;
1207  UNICODE_STRING CapturedServiceName;
1208 
1209  PAGED_CODE();
1210 
1212 
1213  /* Need the appropriate priviliege */
1215  {
1216  DPRINT1("No unload privilege!\n");
1218  }
1219 
1220  /* Capture the service name */
1221  Status = ProbeAndCaptureUnicodeString(&CapturedServiceName, PreviousMode, DriverServiceName);
1222  if (!NT_SUCCESS(Status))
1223  {
1224  return Status;
1225  }
1226 
1227  DPRINT("IopUnloadDriver('%wZ', %u)\n", &CapturedServiceName, UnloadPnpDrivers);
1228 
1229 
1230  /* We need a service name */
1231  if (CapturedServiceName.Length == 0)
1232  {
1233  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1234  return STATUS_INVALID_PARAMETER;
1235  }
1236 
1237  /*
1238  * Get the service name from the registry key name
1239  */
1241  &CapturedServiceName,
1242  &Backslash,
1243  &LastBackslash);
1244  if (NT_SUCCESS(Status))
1245  {
1246  NT_ASSERT(CapturedServiceName.Length >= LastBackslash + sizeof(WCHAR));
1247  ServiceName.Buffer = &CapturedServiceName.Buffer[LastBackslash / sizeof(WCHAR) + 1];
1248  ServiceName.Length = CapturedServiceName.Length - LastBackslash - sizeof(WCHAR);
1249  ServiceName.MaximumLength = CapturedServiceName.MaximumLength - LastBackslash - sizeof(WCHAR);
1250  }
1251  else
1252  {
1253  ServiceName = CapturedServiceName;
1254  }
1255 
1256  /*
1257  * Construct the driver object name
1258  */
1259  Status = RtlUShortAdd(sizeof(DRIVER_ROOT_NAME),
1260  ServiceName.Length,
1261  &ObjectName.MaximumLength);
1262  if (!NT_SUCCESS(Status))
1263  {
1264  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1265  return Status;
1266  }
1267  ObjectName.Length = 0;
1269  ObjectName.MaximumLength,
1270  TAG_IO);
1271  if (!ObjectName.Buffer)
1272  {
1273  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1275  }
1278 
1279  /*
1280  * Find the driver object
1281  */
1283  0,
1284  0,
1285  0,
1287  KernelMode,
1288  0,
1289  (PVOID*)&DriverObject);
1290 
1291  if (!NT_SUCCESS(Status))
1292  {
1293  DPRINT1("Can't locate driver object for %wZ\n", &ObjectName);
1295  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1296  return Status;
1297  }
1298 
1299  /* Free the buffer for driver object name */
1301 
1302  /* Check that driver is not already unloading */
1304  {
1305  DPRINT1("Driver deletion pending\n");
1307  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1308  return STATUS_DELETE_PENDING;
1309  }
1310 
1311  /*
1312  * Get path of service...
1313  */
1315 
1316  RtlInitUnicodeString(&ImagePath, NULL);
1317 
1318  QueryTable[0].Name = L"ImagePath";
1320  QueryTable[0].EntryContext = &ImagePath;
1321 
1323  CapturedServiceName.Buffer,
1324  QueryTable,
1325  NULL,
1326  NULL);
1327 
1328  /* We no longer need service name */
1329  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1330 
1331  if (!NT_SUCCESS(Status))
1332  {
1333  DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1335  return Status;
1336  }
1337 
1338  /*
1339  * Normalize the image path for all later processing.
1340  */
1341  Status = IopNormalizeImagePath(&ImagePath, &ServiceName);
1342 
1343  if (!NT_SUCCESS(Status))
1344  {
1345  DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status);
1347  return Status;
1348  }
1349 
1350  /* Free the service path */
1351  ExFreePool(ImagePath.Buffer);
1352 
1353  /*
1354  * Unload the module and release the references to the device object
1355  */
1356 
1357  /* Call the load/unload routine, depending on current process */
1359  (UnloadPnpDrivers || (DriverObject->Flags & DRVO_LEGACY_DRIVER)))
1360  {
1361  /* Loop through each device object of the driver
1362  and set DOE_UNLOAD_PENDING flag */
1364  while (DeviceObject)
1365  {
1366  /* Set the unload pending flag for the device */
1367  DeviceExtension = IoGetDevObjExtension(DeviceObject);
1368  DeviceExtension->ExtensionFlags |= DOE_UNLOAD_PENDING;
1369 
1370  /* Make sure there are no attached devices or no reference counts */
1371  if ((DeviceObject->ReferenceCount) || (DeviceObject->AttachedDevice))
1372  {
1373  /* Not safe to unload */
1374  DPRINT1("Drivers device object is referenced or has attached devices\n");
1375 
1376  SafeToUnload = FALSE;
1377  }
1378 
1379  DeviceObject = DeviceObject->NextDevice;
1380  }
1381 
1382  /* If not safe to unload, then return success */
1383  if (!SafeToUnload)
1384  {
1386  return STATUS_SUCCESS;
1387  }
1388 
1389  DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject->DriverName);
1390 
1391  /* Set the unload invoked flag and call the unload routine */
1395 
1396  /* Mark the driver object temporary, so it could be deleted later */
1398 
1399  /* Dereference it 2 times */
1402 
1403  return Status;
1404  }
1405  else
1406  {
1407  DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject->DriverName);
1408 
1409  /* Dereference one time (refd inside this function) */
1411 
1412  /* Return unloading failure */
1414  }
1415 }
static __inline NTSTATUS ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest, IN KPROCESSOR_MODE CurrentMode, IN const UNICODE_STRING *UnsafeSrc)
Definition: probe.h:142
#define RTL_FIND_CHAR_IN_UNICODE_STRING_START_AT_END
Definition: rtl.h:25
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:4142
#define STATUS_DELETE_PENDING
Definition: ntstatus.h:322
#define DRVO_LEGACY_DRIVER
Definition: iotypes.h:2206
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define TRUE
Definition: types.h:120
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
BOOLEAN NTAPI SeSinglePrivilegeCheck(IN LUID PrivilegeValue, IN KPROCESSOR_MODE PreviousMode)
Definition: priv.c:524
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3066
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:32
#define NT_VERIFY(exp)
Definition: rtlfuncs.h:3289
_In_ PVOID _Out_opt_ PULONG_PTR _Outptr_opt_ PCUNICODE_STRING * ObjectName
Definition: cmfuncs.h:62
NTSTATUS NTAPI RtlFindCharInUnicodeString(_In_ ULONG Flags, _In_ PCUNICODE_STRING SearchString, _In_ PCUNICODE_STRING MatchString, _Out_ PUSHORT Position)
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
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:248
smooth NULL
Definition: ftsmooth.c:416
const LUID SeLoadDriverPrivilege
Definition: priv.c:31
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
#define DRVO_UNLOAD_INVOKED
Definition: iotypes.h:2205
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
static __inline VOID ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString, IN KPROCESSOR_MODE CurrentMode)
Definition: probe.h:239
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define DOE_UNLOAD_PENDING
Definition: iotypes.h:149
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
PDRIVER_UNLOAD DriverUnload
Definition: iotypes.h:2268
#define RTL_REGISTRY_ABSOLUTE
Definition: nt_native.h:161
static const WCHAR Backslash[]
Definition: cfgmgr.c:38
unsigned short USHORT
Definition: pedump.c:61
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
_In_ PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2464
#define DPRINT1
Definition: precomp.h:8
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define IoGetDevObjExtension(DeviceObject)
Definition: io.h:125
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:411
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
PVOID DriverSection
Definition: iotypes.h:2261
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
UNICODE_STRING DriverName
Definition: iotypes.h:2263
NTSTATUS NTAPI IopLoadUnloadDriver(_In_opt_ PCUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1914
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2257
#define DRIVER_ROOT_NAME
Definition: ldr.h:5
#define PAGED_CODE()
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
#define NT_ASSERT
Definition: rtlfuncs.h:3312
VOID NTAPI ObMakeTemporaryObject(IN PVOID ObjectBody)
Definition: oblife.c:1361

Referenced by NtUnloadDriver().

◆ IoRegisterBootDriverReinitialization()

VOID NTAPI IoRegisterBootDriverReinitialization ( IN PDRIVER_OBJECT  DriverObject,
IN PDRIVER_REINITIALIZE  ReinitRoutine,
IN PVOID  Context 
)

Definition at line 1730 of file driver.c.

1733 {
1734  PDRIVER_REINIT_ITEM ReinitItem;
1735 
1736  /* Allocate the entry */
1737  ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1738  sizeof(DRIVER_REINIT_ITEM),
1739  TAG_REINIT);
1740  if (!ReinitItem) return;
1741 
1742  /* Fill it out */
1743  ReinitItem->DriverObject = DriverObject;
1744  ReinitItem->ReinitRoutine = ReinitRoutine;
1745  ReinitItem->Context = Context;
1746 
1747  /* Set the Driver Object flag and insert the entry into the list */
1750  &ReinitItem->ItemEntry,
1752 }
PVOID Context
Definition: io.h:471
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
LIST_ENTRY DriverBootReinitListHead
Definition: driver.c:26
LIST_ENTRY ItemEntry
Definition: io.h:468
KSPIN_LOCK DriverBootReinitListLock
Definition: driver.c:27
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
PDRIVER_OBJECT DriverObject
Definition: io.h:469
#define TAG_REINIT
Definition: tag.h:73
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
struct tagContext Context
Definition: acpixf.h:1034
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:470
#define DRVO_BOOTREINIT_REGISTERED
Definition: iotypes.h:4450

Referenced by _Function_class_(), DriverEntry(), and HalpAddDevice().

◆ IoRegisterDriverReinitialization()

VOID NTAPI IoRegisterDriverReinitialization ( IN PDRIVER_OBJECT  DriverObject,
IN PDRIVER_REINITIALIZE  ReinitRoutine,
IN PVOID  Context 
)

Definition at line 1759 of file driver.c.

1762 {
1763  PDRIVER_REINIT_ITEM ReinitItem;
1764 
1765  /* Allocate the entry */
1766  ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1767  sizeof(DRIVER_REINIT_ITEM),
1768  TAG_REINIT);
1769  if (!ReinitItem) return;
1770 
1771  /* Fill it out */
1772  ReinitItem->DriverObject = DriverObject;
1773  ReinitItem->ReinitRoutine = ReinitRoutine;
1774  ReinitItem->Context = Context;
1775 
1776  /* Set the Driver Object flag and insert the entry into the list */
1779  &ReinitItem->ItemEntry,
1781 }
PVOID Context
Definition: io.h:471
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
#define DRVO_REINIT_REGISTERED
Definition: iotypes.h:4448
LIST_ENTRY ItemEntry
Definition: io.h:468
KSPIN_LOCK DriverReinitListLock
Definition: driver.c:22
LIST_ENTRY DriverReinitListHead
Definition: driver.c:21
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
PDRIVER_OBJECT DriverObject
Definition: io.h:469
#define TAG_REINIT
Definition: tag.h:73
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
struct tagContext Context
Definition: acpixf.h:1034
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:470

Referenced by DiskBootDriverReinit(), and DriverEntry().

◆ LdrProcessDriverModule()

INIT_FUNCTION NTSTATUS NTAPI LdrProcessDriverModule ( PLDR_DATA_TABLE_ENTRY  LdrEntry,
PUNICODE_STRING  FileName,
PLDR_DATA_TABLE_ENTRY ModuleObject 
)

Definition at line 728 of file driver.c.

731 {
733  UNICODE_STRING BaseName, BaseDirectory;
734  PLOAD_IMPORTS LoadedImports = (PVOID)-2;
735  PCHAR MissingApiName, Buffer;
736  PWCHAR MissingDriverName;
737  PVOID DriverBase = LdrEntry->DllBase;
738 
739  /* Allocate a buffer we'll use for names */
742  TAG_LDR_WSTR);
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 }
signed char * PCHAR
Definition: retypes.h:7
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
uint16_t * PWCHAR
Definition: typedefs.h:56
#define MAXIMUM_FILENAME_LENGTH
Definition: env_spec_w32.h:41
PVOID DllBase
Definition: btrfs_drv.h:1926
#define TAG_LDR_WSTR
Definition: tag.h:110
#define OBJ_NAME_PATH_SEPARATOR
Definition: arcname_tests.c:25
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
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:1001
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
struct _FileName FileName
Definition: fatprocs.h:893
unsigned short USHORT
Definition: pedump.c:61
unsigned int ULONG
Definition: retypes.h:1
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
GLfloat GLfloat p
Definition: glext.h:8902
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
return STATUS_SUCCESS
Definition: btrfs.c:3014

◆ MiResolveImageReferences()

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 at line 1001 of file sysldr.c.

1007 {
1008  static UNICODE_STRING DriversFolderName = RTL_CONSTANT_STRING(L"drivers\\");
1009  PCHAR MissingApiBuffer = *MissingApi, ImportName;
1010  PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor, CurrentImport;
1011  ULONG ImportSize, ImportCount = 0, LoadedImportsSize, ExportSize;
1012  PLOAD_IMPORTS LoadedImports, NewImports;
1013  ULONG GdiLink, NormalLink, i;
1014  BOOLEAN ReferenceNeeded, Loaded;
1015  ANSI_STRING TempString;
1016  UNICODE_STRING NameString, DllName;
1017  PLDR_DATA_TABLE_ENTRY LdrEntry = NULL, DllEntry, ImportEntry = NULL;
1018  PVOID ImportBase, DllBase;
1019  PLIST_ENTRY NextEntry;
1020  PIMAGE_EXPORT_DIRECTORY ExportDirectory;
1021  NTSTATUS Status;
1022  PIMAGE_THUNK_DATA OrigThunk, FirstThunk;
1023  PAGED_CODE();
1024  DPRINT("%s - ImageBase: %p. ImageFileDirectory: %wZ\n",
1025  __FUNCTION__, ImageBase, ImageFileDirectory);
1026 
1027  /* No name string buffer yet */
1028  NameString.Buffer = NULL;
1029 
1030  /* Assume no imports */
1031  *LoadImports = MM_SYSLDR_NO_IMPORTS;
1032 
1033  /* Get the import descriptor */
1034  ImportDescriptor = RtlImageDirectoryEntryToData(ImageBase,
1035  TRUE,
1037  &ImportSize);
1038  if (!ImportDescriptor) return STATUS_SUCCESS;
1039 
1040  /* Loop all imports to count them */
1041  for (CurrentImport = ImportDescriptor;
1042  (CurrentImport->Name) && (CurrentImport->OriginalFirstThunk);
1043  CurrentImport++)
1044  {
1045  /* One more */
1046  ImportCount++;
1047  }
1048 
1049  /* Make sure we have non-zero imports */
1050  if (ImportCount)
1051  {
1052  /* Calculate and allocate the list we'll need */
1053  LoadedImportsSize = ImportCount * sizeof(PVOID) + sizeof(SIZE_T);
1054  LoadedImports = ExAllocatePoolWithTag(PagedPool,
1055  LoadedImportsSize,
1056  TAG_LDR_IMPORTS);
1057  if (LoadedImports)
1058  {
1059  /* Zero it */
1060  RtlZeroMemory(LoadedImports, LoadedImportsSize);
1061  LoadedImports->Count = ImportCount;
1062  }
1063  }
1064  else
1065  {
1066  /* No table */
1067  LoadedImports = NULL;
1068  }
1069 
1070  /* Reset the import count and loop descriptors again */
1071  ImportCount = GdiLink = NormalLink = 0;
1072  while ((ImportDescriptor->Name) && (ImportDescriptor->OriginalFirstThunk))
1073  {
1074  /* Get the name */
1075  ImportName = (PCHAR)((ULONG_PTR)ImageBase + ImportDescriptor->Name);
1076 
1077  /* Check if this is a GDI driver */
1078  GdiLink = GdiLink |
1079  !(_strnicmp(ImportName, "win32k", sizeof("win32k") - 1));
1080 
1081  /* We can also allow dxapi (for Windows compat, allow IRT and coverage) */
1082  NormalLink = NormalLink |
1083  ((_strnicmp(ImportName, "win32k", sizeof("win32k") - 1)) &&
1084  (_strnicmp(ImportName, "dxapi", sizeof("dxapi") - 1)) &&
1085  (_strnicmp(ImportName, "coverage", sizeof("coverage") - 1)) &&
1086  (_strnicmp(ImportName, "irt", sizeof("irt") - 1)));
1087 
1088  /* Check if this is a valid GDI driver */
1089  if ((GdiLink) && (NormalLink))
1090  {
1091  /* It's not, it's importing stuff it shouldn't be! */
1093  goto Failure;
1094  }
1095 
1096  /* Check for user-mode printer or video card drivers, which don't belong */
1097  if (!(_strnicmp(ImportName, "ntdll", sizeof("ntdll") - 1)) ||
1098  !(_strnicmp(ImportName, "winsrv", sizeof("winsrv") - 1)) ||
1099  !(_strnicmp(ImportName, "advapi32", sizeof("advapi32") - 1)) ||
1100  !(_strnicmp(ImportName, "kernel32", sizeof("kernel32") - 1)) ||
1101  !(_strnicmp(ImportName, "user32", sizeof("user32") - 1)) ||
1102  !(_strnicmp(ImportName, "gdi32", sizeof("gdi32") - 1)))
1103  {
1104  /* This is not kernel code */
1106  goto Failure;
1107  }
1108 
1109  /* Check if this is a "core" import, which doesn't get referenced */
1110  if (!(_strnicmp(ImportName, "ntoskrnl", sizeof("ntoskrnl") - 1)) ||
1111  !(_strnicmp(ImportName, "win32k", sizeof("win32k") - 1)) ||
1112  !(_strnicmp(ImportName, "hal", sizeof("hal") - 1)))
1113  {
1114  /* Don't reference this */
1115  ReferenceNeeded = FALSE;
1116  }
1117  else
1118  {
1119  /* Reference these modules */
1120  ReferenceNeeded = TRUE;
1121  }
1122 
1123  /* Now setup a unicode string for the import */
1124  RtlInitAnsiString(&TempString, ImportName);
1125  Status = RtlAnsiStringToUnicodeString(&NameString, &TempString, TRUE);
1126  if (!NT_SUCCESS(Status))
1127  {
1128  /* Failed */
1129  goto Failure;
1130  }
1131 
1132  /* We don't support name prefixes yet */
1133  if (NamePrefix) DPRINT1("Name Prefix not yet supported!\n");
1134 
1135  /* Remember that we haven't loaded the import at this point */
1136 CheckDllState:
1137  Loaded = FALSE;
1138  ImportBase = NULL;
1139 
1140  /* Loop the driver list */
1141  NextEntry = PsLoadedModuleList.Flink;
1142  while (NextEntry != &PsLoadedModuleList)
1143  {
1144  /* Get the loader entry and compare the name */
1145  LdrEntry = CONTAINING_RECORD(NextEntry,
1147  InLoadOrderLinks);
1148  if (RtlEqualUnicodeString(&NameString,
1149  &LdrEntry->BaseDllName,
1150  TRUE))
1151  {
1152  /* Get the base address */
1153  ImportBase = LdrEntry->DllBase;
1154 
1155  /* Check if we haven't loaded yet, and we need references */
1156  if (!(Loaded) && (ReferenceNeeded))
1157  {
1158  /* Make sure we're not already loading */
1159  if (!(LdrEntry->Flags & LDRP_LOAD_IN_PROGRESS))
1160  {
1161  /* Increase the load count */
1162  LdrEntry->LoadCount++;
1163  }
1164  }
1165 
1166  /* Done, break out */
1167  break;
1168  }
1169 
1170  /* Go to the next entry */
1171  NextEntry = NextEntry->Flink;
1172  }
1173 
1174  /* Check if we haven't loaded the import yet */
1175  if (!ImportBase)
1176  {
1177  /* Setup the import DLL name */
1178  DllName.MaximumLength = NameString.Length +
1179  ImageFileDirectory->Length +
1180  sizeof(UNICODE_NULL);
1182  DllName.MaximumLength,
1183  TAG_LDR_WSTR);
1184  if (!DllName.Buffer)
1185  {
1186  /* We're out of resources */
1188  goto Failure;
1189  }
1190 
1191  /* Add the import name to the base directory */
1192  RtlCopyUnicodeString(&DllName, ImageFileDirectory);
1194  &NameString);
1195 
1196  /* Load the image */
1197  Status = MmLoadSystemImage(&DllName,
1198  NamePrefix,
1199  NULL,
1200  FALSE,
1201  (PVOID *)&DllEntry,
1202  &DllBase);
1203 
1204  /* win32k / GDI drivers can also import from system32 folder */
1206  (MI_IS_SESSION_ADDRESS(ImageBase) || 1)) // HACK
1207  {
1208  /* Free the old name buffer */
1210 
1211  /* Calculate size for a string the adds 'drivers\' */
1212  DllName.MaximumLength += DriversFolderName.Length;
1213 
1214  /* Allocate the new buffer */
1216  DllName.MaximumLength,
1217  TAG_LDR_WSTR);
1218  if (!DllName.Buffer)
1219  {
1220  /* We're out of resources */
1222  goto Failure;
1223  }
1224 
1225  /* Copy image directory and append 'drivers\' folder name */
1226  RtlCopyUnicodeString(&DllName, ImageFileDirectory);
1227  RtlAppendUnicodeStringToString(&DllName, &DriversFolderName);
1228 
1229  /* Now add the import name */
1230  RtlAppendUnicodeStringToString(&DllName, &NameString);
1231 
1232  /* Try once again to load the image */
1233  Status = MmLoadSystemImage(&DllName,
1234  NamePrefix,
1235  NULL,
1236  FALSE,
1237  (PVOID *)&DllEntry,
1238  &DllBase);
1239  }
1240 
1241  if (!NT_SUCCESS(Status))
1242  {
1243  /* Fill out the information for the error */
1244  *MissingDriver = DllName.Buffer;
1245  *(PULONG)MissingDriver |= 1;
1246  *MissingApi = NULL;
1247 
1248  DPRINT1("Failed to load dependency: %wZ\n", &DllName);
1249 
1250  /* Don't free the name */
1251  DllName.Buffer = NULL;
1252 
1253  /* Cleanup and return */
1254  goto Failure;
1255  }
1256 
1257  /* We can free the DLL Name */
1259  DllName.Buffer = NULL;
1260 
1261  /* We're now loaded */
1262  Loaded = TRUE;
1263 
1264  /* Sanity check */
1265  ASSERT(DllBase == DllEntry->DllBase);
1266 
1267  /* Call the initialization routines */
1269  if (!NT_SUCCESS(Status))
1270  {
1271  /* We failed, unload the image */
1272  MmUnloadSystemImage(DllEntry);
1273  ERROR_DBGBREAK("MmCallDllInitialize failed with status 0x%x\n", Status);
1274  Loaded = FALSE;
1275  }
1276 
1277  /* Loop again to make sure that everything is OK */
1278  goto CheckDllState;
1279  }
1280 
1281  /* Check if we're support to reference this import */
1282  if ((ReferenceNeeded) && (LoadedImports))
1283  {
1284  /* Make sure we're not already loading */
1285  if (!(LdrEntry->Flags & LDRP_LOAD_IN_PROGRESS))
1286  {
1287  /* Add the entry */
1288  LoadedImports->Entry[ImportCount] = LdrEntry;
1289  ImportCount++;
1290  }
1291  }
1292 
1293  /* Free the import name */
1294  RtlFreeUnicodeString(&NameString);
1295 
1296  /* Set the missing driver name and get the export directory */
1297  *MissingDriver = LdrEntry->BaseDllName.Buffer;
1298  ExportDirectory = RtlImageDirectoryEntryToData(ImportBase,
1299  TRUE,
1301  &ExportSize);
1302  if (!ExportDirectory)
1303  {
1304  /* Cleanup and return */
1305  DPRINT1("Warning: Driver failed to load, %S not found\n", *MissingDriver);
1307  goto Failure;
1308  }
1309 
1310  /* Make sure we have an IAT */
1311  if (ImportDescriptor->OriginalFirstThunk)
1312  {
1313  /* Get the first thunks */
1314  OrigThunk = (PVOID)((ULONG_PTR)ImageBase +
1315  ImportDescriptor->OriginalFirstThunk);
1316  FirstThunk = (PVOID)((ULONG_PTR)ImageBase +
1317  ImportDescriptor->FirstThunk);
1318 
1319  /* Loop the IAT */
1320  while (OrigThunk->u1.AddressOfData)
1321  {
1322  /* Snap thunk */
1323  Status = MiSnapThunk(ImportBase,
1324  ImageBase,
1325  OrigThunk++,
1326  FirstThunk++,
1327  ExportDirectory,
1328  ExportSize,
1329  FALSE,
1330  MissingApi);
1331  if (!NT_SUCCESS(Status))
1332  {
1333  /* Cleanup and return */
1334  goto Failure;
1335  }
1336 
1337  /* Reset the buffer */
1338  *MissingApi = MissingApiBuffer;
1339  }
1340  }
1341 
1342  /* Go to the next import */
1343  ImportDescriptor++;
1344  }
1345 
1346  /* Check if we have an import list */
1347  if (LoadedImports)
1348  {
1349  /* Reset the count again, and loop entries */
1350  ImportCount = 0;
1351  for (i = 0; i < LoadedImports->Count; i++)
1352  {
1353  if (LoadedImports->Entry[i])
1354  {
1355  /* Got an entry, OR it with 1 in case it's the single entry */
1356  ImportEntry = (PVOID)((ULONG_PTR)LoadedImports->Entry[i] |
1358  ImportCount++;
1359  }
1360  }
1361 
1362  /* Check if we had no imports */
1363  if (!ImportCount)
1364  {
1365  /* Free the list and set it to no imports */
1366  ExFreePoolWithTag(LoadedImports, TAG_LDR_IMPORTS);
1367  LoadedImports = MM_SYSLDR_NO_IMPORTS;
1368  }
1369  else if (ImportCount == 1)
1370  {
1371  /* Just one entry, we can free the table and only use our entry */
1372  ExFreePoolWithTag(LoadedImports, TAG_LDR_IMPORTS);
1373  LoadedImports = (PLOAD_IMPORTS)ImportEntry;
1374  }
1375  else if (ImportCount != LoadedImports->Count)
1376  {
1377  /* Allocate a new list */
1378  LoadedImportsSize = ImportCount * sizeof(PVOID) + sizeof(SIZE_T);
1379  NewImports = ExAllocatePoolWithTag(PagedPool,
1380  LoadedImportsSize,
1381  TAG_LDR_IMPORTS);
1382  if (NewImports)
1383  {
1384  /* Set count */
1385  NewImports->Count = 0;
1386 
1387  /* Loop all the imports */
1388  for (i = 0; i < LoadedImports->Count; i++)
1389  {
1390  /* Make sure it's valid */
1391  if (LoadedImports->Entry[i])
1392  {
1393  /* Copy it */
1394  NewImports->Entry[NewImports->Count] = LoadedImports->Entry[i];
1395  NewImports->Count++;
1396  }
1397  }
1398 
1399  /* Free the old copy */
1400  ExFreePoolWithTag(LoadedImports, TAG_LDR_IMPORTS);
1401  LoadedImports = NewImports;
1402  }
1403  }
1404 
1405  /* Return the list */
1406  *LoadImports = LoadedImports;
1407  }
1408 
1409  /* Return success */
1410  return STATUS_SUCCESS;
1411 
1412 Failure:
1413 
1414  /* Cleanup and return */
1415  RtlFreeUnicodeString(&NameString);
1416 
1417  if (LoadedImports)
1418  {
1419  MiDereferenceImports(LoadedImports);
1420  ExFreePoolWithTag(LoadedImports, TAG_LDR_IMPORTS);
1421  }
1422 
1423  return Status;
1424 }
signed char * PCHAR
Definition: retypes.h:7
#define LDRP_LOAD_IN_PROGRESS
Definition: ldrtypes.h:42
NTSTATUS NTAPI MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry, IN PLIST_ENTRY ListHead)
Definition: sysldr.c:309
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
PLDR_DATA_TABLE_ENTRY Entry[1]
Definition: ldrtypes.h:173
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:912
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define TRUE
Definition: types.h:120
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
#define MI_IS_SESSION_ADDRESS(Address)
Definition: miarm.h:167
LONG NTSTATUS
Definition: precomp.h:26
struct _LOAD_IMPORTS * PLOAD_IMPORTS
#define MM_SYSLDR_SINGLE_ENTRY
Definition: miarm.h:203
NTSTATUS NTAPI MiDereferenceImports(IN PLOAD_IMPORTS ImportList)
Definition: sysldr.c:396
#define TAG_LDR_IMPORTS
Definition: tag.h:111
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
Definition: fs_rec.h:186
#define STATUS_DRIVER_ENTRYPOINT_NOT_FOUND
Definition: ntstatus.h:736
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
uint32_t ULONG_PTR
Definition: typedefs.h:65
PVOID DllBase
Definition: btrfs_drv.h:1926
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 TAG_LDR_WSTR
Definition: tag.h:110
#define FALSE
Definition: types.h:117
#define UNICODE_NULL
#define ERROR_DBGBREAK(...)
Definition: debug.h:221
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
void * PVOID
Definition: retypes.h:9
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define PCHAR
Definition: match.c:90
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
union _IMAGE_THUNK_DATA32::@2106 u1
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define _strnicmp(_String1, _String2, _MaxCount)
Definition: compat.h:23
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
Status
Definition: gdiplustypes.h:24
static const WCHAR L[]
Definition: oid.c:1250
#define RtlImageDirectoryEntryToData
Definition: compat.h:668
Definition: btrfs_drv.h:1922
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:2819
Definition: typedefs.h:119
#define IMAGE_DIRECTORY_ENTRY_EXPORT
Definition: compat.h:151
SIZE_T Count
Definition: ldrtypes.h:172
#define MM_SYSLDR_NO_IMPORTS
Definition: miarm.h:200
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
unsigned int * PULONG
Definition: retypes.h:1
#define STATUS_PROCEDURE_NOT_FOUND
Definition: ntstatus.h:358
#define DPRINT1
Definition: precomp.h:8
#define IMAGE_DIRECTORY_ENTRY_IMPORT
Definition: pedump.c:260
ULONG Flags
Definition: ntddk_ex.h:207
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define __FUNCTION__
Definition: types.h:112
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
USHORT LoadCount
Definition: ntddk_ex.h:208
NTSTATUS NTAPI MiSnapThunk(IN PVOID DllBase, IN PVOID ImageBase, IN PIMAGE_THUNK_DATA Name, IN PIMAGE_THUNK_DATA Address, IN PIMAGE_EXPORT_DIRECTORY ExportDirectory, IN ULONG ExportSize, IN BOOLEAN SnapForwarder, OUT PCHAR *MissingApi)
Definition: sysldr.c:692
#define PAGED_CODE()
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Referenced by LdrProcessDriverModule(), and MmLoadSystemImage().

◆ MmFreeDriverInitialization()

VOID NTAPI MmFreeDriverInitialization ( IN PLDR_DATA_TABLE_ENTRY  LdrEntry)

Definition at line 1636 of file sysldr.c.

1637 {
1638  PMMPTE StartPte, EndPte;
1639  PFN_NUMBER PageCount;
1640  PVOID DllBase;
1641  ULONG i;
1642  PIMAGE_NT_HEADERS NtHeader;
1643  PIMAGE_SECTION_HEADER Section, DiscardSection;
1644 
1645  /* Get the base address and the page count */
1646  DllBase = LdrEntry->DllBase;
1647  PageCount = LdrEntry->SizeOfImage >> PAGE_SHIFT;
1648 
1649  /* Get the last PTE in this image */
1650  EndPte = MiAddressToPte(DllBase) + PageCount;
1651 
1652  /* Get the NT header */
1653  NtHeader = RtlImageNtHeader(DllBase);
1654  if (!NtHeader) return;
1655 
1656  /* Get the last section and loop each section backwards */
1657  Section = IMAGE_FIRST_SECTION(NtHeader) + NtHeader->FileHeader.NumberOfSections;
1658  DiscardSection = NULL;
1659  for (i = 0; i < NtHeader->FileHeader.NumberOfSections; i++)
1660  {
1661  /* Go back a section and check if it's discardable */
1662  Section--;
1664  {
1665  /* It is, select it for freeing */
1666  DiscardSection = Section;
1667  }
1668  else
1669  {
1670  /* No more discardable sections exist, bail out */
1671  break;
1672  }
1673  }
1674 
1675  /* Bail out if there's nothing to free */
1676  if (!DiscardSection) return;
1677 
1678  /* Push the DLL base to the first disacrable section, and get its PTE */
1679  DllBase = (PVOID)ROUND_TO_PAGES((ULONG_PTR)DllBase + DiscardSection->VirtualAddress);
1680  ASSERT(MI_IS_PHYSICAL_ADDRESS(DllBase) == FALSE);
1681  StartPte = MiAddressToPte(DllBase);
1682 
1683  /* Check how many pages to free total */
1684  PageCount = (PFN_NUMBER)(EndPte - StartPte);
1685  if (!PageCount) return;
1686 
1687  /* Delete this many PTEs */
1688  MiDeleteSystemPageableVm(StartPte, PageCount, 0, NULL);
1689 }
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
uint32_t ULONG_PTR
Definition: typedefs.h:65
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 MiAddressToPte(x)
Definition: mmx86.c:19
ULONG PFN_NUMBER
Definition: ke.h:8
#define FALSE
Definition: types.h:117
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PFN_COUNT NTAPI MiDeleteSystemPageableVm(IN PMMPTE PointerPte, IN PFN_NUMBER PageCount, IN ULONG Flags, OUT PPFN_NUMBER ValidPages)
Definition: virtual.c:297
#define ROUND_TO_PAGES(Size)
#define RtlImageNtHeader
Definition: compat.h:665
unsigned int ULONG
Definition: retypes.h:1
FORCEINLINE BOOLEAN MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
Definition: miarm.h:928
#define IMAGE_SCN_MEM_DISCARDABLE
Definition: ntimage.h:235

Referenced by IopInitializeDriverModule().

◆ NtLoadDriver()

NTSTATUS NTAPI NtLoadDriver ( IN PUNICODE_STRING  DriverServiceName)

Definition at line 2114 of file driver.c.

2115 {
2116  UNICODE_STRING CapturedDriverServiceName = { 0, 0, NULL };
2119  NTSTATUS Status;
2120 
2121  PAGED_CODE();
2122 
2124 
2125  /*
2126  * Check security privileges
2127  */
2129  {
2130  DPRINT("Privilege not held\n");
2132  }
2133 
2134  Status = ProbeAndCaptureUnicodeString(&CapturedDriverServiceName,
2135  PreviousMode,
2136  DriverServiceName);
2137  if (!NT_SUCCESS(Status))
2138  {
2139  return Status;
2140  }
2141 
2142  DPRINT("NtLoadDriver('%wZ')\n", &CapturedDriverServiceName);
2143 
2144  /* Load driver and call its entry point */
2145  DriverObject = NULL;
2146  Status = IopLoadUnloadDriver(&CapturedDriverServiceName, &DriverObject);
2147 
2148  ReleaseCapturedUnicodeString(&CapturedDriverServiceName,
2149  PreviousMode);
2150 
2151  return Status;
2152 }
static __inline NTSTATUS ProbeAndCaptureUnicodeString(OUT PUNICODE_STRING Dest, IN KPROCESSOR_MODE CurrentMode, IN const UNICODE_STRING *UnsafeSrc)
Definition: probe.h:142
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
#define KeGetPreviousMode()
Definition: ketypes.h:1107
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI SeSinglePrivilegeCheck(IN LUID PrivilegeValue, IN KPROCESSOR_MODE PreviousMode)
Definition: priv.c:524
smooth NULL
Definition: ftsmooth.c:416
const LUID SeLoadDriverPrivilege
Definition: priv.c:31
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
void DPRINT(...)
Definition: polytest.cpp:61
static __inline VOID ReleaseCapturedUnicodeString(IN PUNICODE_STRING CapturedString, IN KPROCESSOR_MODE CurrentMode)
Definition: probe.h:239
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI IopLoadUnloadDriver(_In_opt_ PCUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1914
#define PAGED_CODE()

Referenced by NtStartDriver(), ScmLoadDriver(), and wmain().

◆ NtUnloadDriver()

NTSTATUS NTAPI NtUnloadDriver ( IN PUNICODE_STRING  DriverServiceName)

Definition at line 2171 of file driver.c.

2172 {
2173  return IopUnloadDriver(DriverServiceName, FALSE);
2174 }
#define FALSE
Definition: types.h:117
NTSTATUS NTAPI IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
Definition: driver.c:1193

Referenced by LoadVia_SystemExtendServiceTableInformation(), LoadVia_SystemLoadGdiDriverInformation(), NtStopDriver(), ScmUnloadDriver(), START_TEST(), and wmain().

Variable Documentation

◆ DriverBootReinitListHead

LIST_ENTRY DriverBootReinitListHead

◆ DriverBootReinitListLock

KSPIN_LOCK DriverBootReinitListLock

◆ DriverBootReinitTailEntry

PLIST_ENTRY DriverBootReinitTailEntry

Definition at line 25 of file driver.c.

◆ DriverReinitListHead

LIST_ENTRY DriverReinitListHead

Definition at line 21 of file driver.c.

Referenced by IoInitSystem(), IopReinitializeDrivers(), and IoRegisterDriverReinitialization().

◆ DriverReinitListLock

KSPIN_LOCK DriverReinitListLock

Definition at line 22 of file driver.c.

Referenced by IoInitSystem(), IopReinitializeDrivers(), and IoRegisterDriverReinitialization().

◆ DriverReinitTailEntry

PLIST_ENTRY DriverReinitTailEntry

Definition at line 23 of file driver.c.

◆ ExpInTextModeSetup

◆ IoDriverObjectType

◆ IopDriverLoadResource

◆ IopGroupIndex

USHORT IopGroupIndex

Definition at line 41 of file driver.c.

Referenced by IopInitializeBootDrivers().

◆ IopGroupTable

PLIST_ENTRY IopGroupTable

Definition at line 42 of file driver.c.

Referenced by IopInitializeBootDrivers().

◆ IopHardwareDatabaseKey

UNICODE_STRING IopHardwareDatabaseKey
Initial value:
=
RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DESCRIPTION\\SYSTEM")
static const WCHAR L[]
Definition: oid.c:1250
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Definition at line 29 of file driver.c.

Referenced by IopCreateDriver().

◆ PiEnumerationFinished

KEVENT PiEnumerationFinished

Definition at line 49 of file devaction.c.

Referenced by IopReinitializeBootDrivers().

◆ PnPBootDriversLoaded

BOOLEAN PnPBootDriversLoaded

Definition at line 26 of file pnpinit.c.

Referenced by IopInitializeBootDrivers().

◆ PnpSystemInit

BOOLEAN PnpSystemInit

Definition at line 17 of file iomgr.c.

Referenced by IopInitializeDriverModule().