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

Go to the source code of this file.

Classes

struct  _LOAD_UNLOAD_PARAMS
 

Macros

#define NDEBUG
 
#define TAG_RTLREGISTRY   'vrqR'
 

Typedefs

typedef struct _LOAD_UNLOAD_PARAMS LOAD_UNLOAD_PARAMS
 
typedef struct _LOAD_UNLOAD_PARAMSPLOAD_UNLOAD_PARAMS
 

Functions

NTSTATUS IopDoLoadUnloadDriver (_In_opt_ PUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
 Process load and unload driver operations. This is mostly for NtLoadDriver and NtUnloadDriver, because their code should run inside PsInitialSystemProcess. More...
 
NTSTATUS NTAPI IopInvalidDeviceRequest (PDEVICE_OBJECT DeviceObject, PIRP Irp)
 
VOID NTAPI IopDeleteDriver (IN PVOID ObjectBody)
 
NTSTATUS IopGetDriverNames (_In_ HANDLE ServiceHandle, _Out_ PUNICODE_STRING DriverName, _Out_opt_ PUNICODE_STRING ServiceName)
 
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 IopInitializeDriverModule (_In_ PLDR_DATA_TABLE_ENTRY ModuleObject, _In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *OutDriverObject, _Out_ NTSTATUS *DriverEntryStatus)
 Initialize a loaded driver. More...
 
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)
 
NTSTATUS NTAPI LdrProcessDriverModule (PLDR_DATA_TABLE_ENTRY LdrEntry, PUNICODE_STRING FileName, PLDR_DATA_TABLE_ENTRY *ModuleObject)
 
NTSTATUS NTAPI IopInitializeBuiltinDriver (IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
 
VOID FASTCALL IopInitializeBootDrivers (VOID)
 
VOID FASTCALL IopInitializeSystemDrivers (VOID)
 
NTSTATUS NTAPI IopUnloadDriver (PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
 
VOID NTAPI IopReinitializeDrivers (VOID)
 
VOID NTAPI IopReinitializeBootDrivers (VOID)
 
NTSTATUS NTAPI IoCreateDriver (_In_opt_ PUNICODE_STRING DriverName, _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)
 
NTSTATUS IopLoadDriver (_In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *DriverObject)
 
static VOID NTAPI IopLoadUnloadDriverWorker (_Inout_ PVOID Parameter)
 
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
 
static const WCHAR ServicesKeyName [] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"
 
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 35 of file driver.c.

Typedef Documentation

◆ LOAD_UNLOAD_PARAMS

◆ PLOAD_UNLOAD_PARAMS

Function Documentation

◆ IoAllocateDriverObjectExtension()

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

Definition at line 1663 of file driver.c.

1667 {
1668  KIRQL OldIrql;
1669  PIO_CLIENT_EXTENSION DriverExtensions, NewDriverExtension;
1670  BOOLEAN Inserted = FALSE;
1671 
1672  /* Assume failure */
1673  *DriverObjectExtension = NULL;
1674 
1675  /* Allocate the extension */
1676  NewDriverExtension = ExAllocatePoolWithTag(NonPagedPool,
1677  sizeof(IO_CLIENT_EXTENSION) +
1678  DriverObjectExtensionSize,
1680  if (!NewDriverExtension) return STATUS_INSUFFICIENT_RESOURCES;
1681 
1682  /* Clear the extension for teh caller */
1683  RtlZeroMemory(NewDriverExtension,
1684  sizeof(IO_CLIENT_EXTENSION) + DriverObjectExtensionSize);
1685 
1686  /* Acqure lock */
1688 
1689  /* Fill out the extension */
1691 
1692  /* Loop the current extensions */
1693  DriverExtensions = IoGetDrvObjExtension(DriverObject)->
1694  ClientDriverExtension;
1695  while (DriverExtensions)
1696  {
1697  /* Check if the identifier matches */
1698  if (DriverExtensions->ClientIdentificationAddress ==
1700  {
1701  /* We have a collision, break out */
1702  break;
1703  }
1704 
1705  /* Go to the next one */
1706  DriverExtensions = DriverExtensions->NextExtension;
1707  }
1708 
1709  /* Check if we didn't collide */
1710  if (!DriverExtensions)
1711  {
1712  /* Link this one in */
1713  NewDriverExtension->NextExtension =
1714  IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1715  IoGetDrvObjExtension(DriverObject)->ClientDriverExtension =
1716  NewDriverExtension;
1717  Inserted = TRUE;
1718  }
1719 
1720  /* Release the lock */
1722 
1723  /* Check if insertion failed */
1724  if (!Inserted)
1725  {
1726  /* Free the entry and fail */
1727  ExFreePoolWithTag(NewDriverExtension, TAG_DRIVER_EXTENSION);
1729  }
1730 
1731  /* Otherwise, return the pointer */
1732  *DriverObjectExtension = NewDriverExtension + 1;
1733  return STATUS_SUCCESS;
1734 }
#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
__drv_aliasesMem _In_ PVOID ClientIdentificationAddress
Definition: iofuncs.h:1026
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#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 NULL
Definition: types.h:112
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
PVOID ClientIdentificationAddress
Definition: iotypes.h:815
#define IoGetDrvObjExtension(DriverObject)
Definition: io.h:132

Referenced by _IRQL_requires_max_(), DriverEntry(), HidRegisterMinidriver(), KoDriverInitialize(), KsInitializeDriver(), Mx::MxAllocateDriverObjectExtension(), NdisMRegisterMiniport(), PciIdeXInitialize(), ScsiPortInitialize(), StorPortInitialize(), StreamClassRegisterAdapter(), VideoPortInitialize(), and WdfWriteKmdfVersionToRegistry().

◆ IoCreateDriver()

NTSTATUS NTAPI IoCreateDriver ( _In_opt_ PUNICODE_STRING  DriverName,
_In_ PDRIVER_INITIALIZE  InitializationFunction 
)

Definition at line 1414 of file driver.c.

1417 {
1418  WCHAR NameBuffer[100];
1419  USHORT NameLength;
1420  UNICODE_STRING LocalDriverName;
1421  NTSTATUS Status;
1423  ULONG ObjectSize;
1425  UNICODE_STRING ServiceKeyName;
1426  HANDLE hDriver;
1427  ULONG i, RetryCount = 0;
1428 
1429 try_again:
1430  /* First, create a unique name for the driver if we don't have one */
1431  if (!DriverName)
1432  {
1433  /* Create a random name and set up the string */
1434  NameLength = (USHORT)swprintf(NameBuffer,
1435  DRIVER_ROOT_NAME L"%08u",
1437  LocalDriverName.Length = NameLength * sizeof(WCHAR);
1438  LocalDriverName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1439  LocalDriverName.Buffer = NameBuffer;
1440  }
1441  else
1442  {
1443  /* So we can avoid another code path, use a local var */
1444  LocalDriverName = *DriverName;
1445  }
1446 
1447  /* Initialize the Attributes */
1448  ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
1450  &LocalDriverName,
1452  NULL,
1453  NULL);
1454 
1455  /* Create the Object */
1459  KernelMode,
1460  NULL,
1461  ObjectSize,
1462  0,
1463  0,
1464  (PVOID*)&DriverObject);
1465  if (!NT_SUCCESS(Status)) return Status;
1466 
1467  DPRINT("IopCreateDriver(): created DO %p\n", DriverObject);
1468 
1469  /* Set up the Object */
1470  RtlZeroMemory(DriverObject, ObjectSize);
1471  DriverObject->Type = IO_TYPE_DRIVER;
1472  DriverObject->Size = sizeof(DRIVER_OBJECT);
1474  DriverObject->DriverExtension = (PDRIVER_EXTENSION)(DriverObject + 1);
1475  DriverObject->DriverExtension->DriverObject = DriverObject;
1476  DriverObject->DriverInit = InitializationFunction;
1477  /* Loop all Major Functions */
1478  for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1479  {
1480  /* Invalidate each function */
1481  DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1482  }
1483 
1484  /* Set up the service key name buffer */
1485  ServiceKeyName.MaximumLength = LocalDriverName.Length + sizeof(UNICODE_NULL);
1486  ServiceKeyName.Buffer = ExAllocatePoolWithTag(PagedPool, LocalDriverName.MaximumLength, TAG_IO);
1487  if (!ServiceKeyName.Buffer)
1488  {
1489  /* Fail */
1493  }
1494 
1495  /* For builtin drivers, the ServiceKeyName is equal to DriverName */
1496  RtlCopyUnicodeString(&ServiceKeyName, &LocalDriverName);
1497  ServiceKeyName.Buffer[ServiceKeyName.Length / sizeof(WCHAR)] = UNICODE_NULL;
1498  DriverObject->DriverExtension->ServiceKeyName = ServiceKeyName;
1499 
1500  /* Make a copy of the driver name to store in the driver object */
1501  DriverObject->DriverName.MaximumLength = LocalDriverName.Length;
1502  DriverObject->DriverName.Buffer = ExAllocatePoolWithTag(PagedPool,
1503  DriverObject->DriverName.MaximumLength,
1504  TAG_IO);
1505  if (!DriverObject->DriverName.Buffer)
1506  {
1507  /* Fail */
1511  }
1512 
1513  RtlCopyUnicodeString(&DriverObject->DriverName, &LocalDriverName);
1514 
1515  /* Add the Object and get its handle */
1517  NULL,
1519  0,
1520  NULL,
1521  &hDriver);
1522 
1523  /* Eliminate small possibility when this function is called more than
1524  once in a row, and KeTickCount doesn't get enough time to change */
1525  if (!DriverName && (Status == STATUS_OBJECT_NAME_COLLISION) && (RetryCount < 100))
1526  {
1527  RetryCount++;
1528  goto try_again;
1529  }
1530 
1531  if (!NT_SUCCESS(Status)) return Status;
1532 
1533  /* Now reference it */
1535  0,
1537  KernelMode,
1538  (PVOID*)&DriverObject,
1539  NULL);
1540 
1541  /* Close the extra handle */
1542  ZwClose(hDriver);
1543 
1544  if (!NT_SUCCESS(Status))
1545  {
1546  /* Fail */
1549  return Status;
1550  }
1551 
1552  /* Finally, call its init function */
1553  DPRINT("Calling driver entrypoint at %p\n", InitializationFunction);
1554  Status = (*InitializationFunction)(DriverObject, NULL);
1555  if (!NT_SUCCESS(Status))
1556  {
1557  /* If it didn't work, then kill the object */
1558  DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", DriverName, Status);
1561  return Status;
1562  }
1563 
1564  // Windows does this fixup - keep it for compatibility
1565  for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
1566  {
1567  /*
1568  * Make sure the driver didn't set any dispatch entry point to NULL!
1569  * Doing so is illegal; drivers shouldn't touch entry points they
1570  * do not implement.
1571  */
1572 
1573  /* Check if it did so anyway */
1574  if (!DriverObject->MajorFunction[i])
1575  {
1576  /* Print a warning in the debug log */
1577  DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
1578  &DriverObject->DriverName, i);
1579 
1580  /* Fix it up */
1581  DriverObject->MajorFunction[i] = IopInvalidDeviceRequest;
1582  }
1583  }
1584 
1585  /* Return the Status */
1586  return Status;
1587 }
#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:917
#define DRVO_BUILTIN_DRIVER
Definition: iotypes.h:2224
#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
_In_ LPWSTR _In_ ULONG _In_ ULONG _In_ ULONG _Out_ DEVINFO _In_ HDEV _In_ LPWSTR _In_ HANDLE hDriver
Definition: winddi.h:3553
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:33
#define swprintf
Definition: precomp.h:40
NTSTATUS NTAPI IopInvalidDeviceRequest(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: driver.c:67
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
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
#define FILE_READ_DATA
Definition: nt_native.h:628
void DPRINT(...)
Definition: polytest.cpp:61
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
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
Status
Definition: gdiplustypes.h:24
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ObDereferenceObject
Definition: obfuncs.h:203
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
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
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
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
unsigned short USHORT
Definition: pedump.c:61
struct _DRIVER_EXTENSION * PDRIVER_EXTENSION
#define NULL
Definition: types.h:112
#define IO_TYPE_DRIVER
#define DPRINT1
Definition: precomp.h:8
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
PDRIVER_INITIALIZE DriverInit
Definition: iotypes.h:2283
#define DRIVER_ROOT_NAME
Definition: ldr.h:5
VOID NTAPI ObMakeTemporaryObject(IN PVOID ObjectBody)
Definition: oblife.c:1361

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

◆ IoDeleteDriver()

VOID NTAPI IoDeleteDriver ( IN PDRIVER_OBJECT  DriverObject)

Definition at line 1594 of file driver.c.

1595 {
1596  /* Simply dereference the Object */
1598 }
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define ObDereferenceObject
Definition: obfuncs.h:203

◆ IoGetDriverObjectExtension()

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

Definition at line 1741 of file driver.c.

1743 {
1744  KIRQL OldIrql;
1745  PIO_CLIENT_EXTENSION DriverExtensions;
1746 
1747  /* Acquire lock */
1749 
1750  /* Loop the list until we find the right one */
1751  DriverExtensions = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
1752  while (DriverExtensions)
1753  {
1754  /* Check for a match */
1755  if (DriverExtensions->ClientIdentificationAddress ==
1757  {
1758  /* Break out */
1759  break;
1760  }
1761 
1762  /* Keep looping */
1763  DriverExtensions = DriverExtensions->NextExtension;
1764  }
1765 
1766  /* Release lock */
1768 
1769  /* Return nothing or the extension */
1770  if (!DriverExtensions) return NULL;
1771  return DriverExtensions + 1;
1772 }
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
__drv_aliasesMem _In_ PVOID ClientIdentificationAddress
Definition: iofuncs.h:1026
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
#define NULL
Definition: types.h:112
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(), Mx::MxGetDriverObjectExtension(), NdisIAddDevice(), PciIdeXAddDevice(), PciIdeXFdoStartDevice(), PortAddDevice(), PortUnload(), ScreenAddDevice(), ScsiPortInitialize(), SermouseAddDevice(), StorPortInitialize(), StreamClassAddDevice(), StreamClassStartDevice(), VfdCreateDevice(), VfdDeleteDevice(), VfdStoreLink(), VfdUnloadDriver(), VideoPortGetAccessRanges(), VideoPortInitialize(), and WdfDeleteKmdfVersionFromRegistry().

◆ IopDeleteDriver()

VOID NTAPI IopDeleteDriver ( IN PVOID  ObjectBody)

Definition at line 79 of file driver.c.

80 {
81  PDRIVER_OBJECT DriverObject = ObjectBody;
82  PIO_CLIENT_EXTENSION DriverExtension, NextDriverExtension;
83  PAGED_CODE();
84 
85  DPRINT1("Deleting driver object '%wZ'\n", &DriverObject->DriverName);
86 
87  /* There must be no device objects remaining at this point */
88  ASSERT(!DriverObject->DeviceObject);
89 
90  /* Get the extension and loop them */
91  DriverExtension = IoGetDrvObjExtension(DriverObject)->ClientDriverExtension;
92  while (DriverExtension)
93  {
94  /* Get the next one */
95  NextDriverExtension = DriverExtension->NextExtension;
97 
98  /* Move on */
99  DriverExtension = NextDriverExtension;
100  }
101 
102  /* Check if the driver image is still loaded */
103  if (DriverObject->DriverSection)
104  {
105  /* Unload it */
106  MmUnloadSystemImage(DriverObject->DriverSection);
107  }
108 
109  /* Check if it has a name */
110  if (DriverObject->DriverName.Buffer)
111  {
112  /* Free it */
113  ExFreePool(DriverObject->DriverName.Buffer);
114  }
115 
116  /* Check if it has a service key name */
117  if (DriverObject->DriverExtension->ServiceKeyName.Buffer)
118  {
119  /* Free it */
120  ExFreePool(DriverObject->DriverExtension->ServiceKeyName.Buffer);
121  }
122 }
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:912
#define ASSERT(a)
Definition: mode.c:45
#define TAG_DRIVER_EXTENSION
Definition: tag.h:42
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:41
#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
#define PAGED_CODE()

Referenced by IopCreateObjectTypes().

◆ IopDisplayLoadingMessage()

VOID FASTCALL IopDisplayLoadingMessage ( PUNICODE_STRING  ServiceName)

Definition at line 290 of file driver.c.

291 {
292  CHAR TextBuffer[256];
293  UNICODE_STRING DotSys = RTL_CONSTANT_STRING(L".SYS");
294 
295  if (ExpInTextModeSetup) return;
296  if (!KeLoaderBlock) return;
298  snprintf(TextBuffer, sizeof(TextBuffer),
299  "%s%sSystem32\\Drivers\\%wZ%s\r\n",
302  ServiceName,
303  IopSuffixUnicodeString(&DotSys, ServiceName) ? "" : ".SYS");
305 }
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:256

Referenced by IopInitializeBuiltinDriver(), and IopLoadDriver().

◆ IopDoLoadUnloadDriver()

NTSTATUS IopDoLoadUnloadDriver ( _In_opt_ PUNICODE_STRING  RegistryPath,
_Inout_ PDRIVER_OBJECT DriverObject 
)

Process load and unload driver operations. This is mostly for NtLoadDriver and NtUnloadDriver, because their code should run inside PsInitialSystemProcess.

Parameters
[in]RegistryPathThe registry path
DriverObjectThe driver object
Returns
Status of the operation

Definition at line 1935 of file driver.c.

1938 {
1939  LOAD_UNLOAD_PARAMS LoadParams;
1940 
1941  /* Prepare parameters block */
1942  LoadParams.RegistryPath = RegistryPath;
1943  LoadParams.DriverObject = *DriverObject;
1944 
1946  {
1947  LoadParams.SetEvent = TRUE;
1949 
1950  /* Initialize and queue a work item */
1951  ExInitializeWorkItem(&LoadParams.WorkItem, IopLoadUnloadDriverWorker, &LoadParams);
1953 
1954  /* And wait till it completes */
1956  }
1957  else
1958  {
1959  /* If we're already in a system process, call it right here */
1960  LoadParams.SetEvent = FALSE;
1961  IopLoadUnloadDriverWorker(&LoadParams);
1962  }
1963 
1964  return LoadParams.Status;
1965 }
VOID NTAPI ExQueueWorkItem(IN PWORK_QUEUE_ITEM WorkItem, IN WORK_QUEUE_TYPE QueueType)
Definition: work.c:711
static VOID NTAPI IopLoadUnloadDriverWorker(_Inout_ PVOID Parameter)
Definition: driver.c:1889
#define TRUE
Definition: types.h:120
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:213
WORK_QUEUE_ITEM WorkItem
Definition: driver.c:52
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 FALSE
Definition: types.h:117
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
#define PsGetCurrentProcess
Definition: psfuncs.h:17
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define ExInitializeWorkItem(Item, Routine, Context)
Definition: exfuncs.h:265
NTSTATUS Status
Definition: driver.c:50
BOOLEAN SetEvent
Definition: driver.c:55
PDRIVER_OBJECT DriverObject
Definition: driver.c:54
PUNICODE_STRING RegistryPath
Definition: driver.c:51
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define NULL
Definition: types.h:112

Referenced by IopUnloadDriver(), and NtLoadDriver().

◆ IopGetDriverNames()

NTSTATUS IopGetDriverNames ( _In_ HANDLE  ServiceHandle,
_Out_ PUNICODE_STRING  DriverName,
_Out_opt_ PUNICODE_STRING  ServiceName 
)

Definition at line 125 of file driver.c.

129 {
130  UNICODE_STRING driverName = {.Buffer = NULL}, serviceName;
133 
134  PAGED_CODE();
135 
136  // 1. Check the "ObjectName" field in the driver's registry key (it has the priority)
137  status = IopGetRegistryValue(ServiceHandle, L"ObjectName", &kvInfo);
138  if (NT_SUCCESS(status))
139  {
140  // we're got the ObjectName. Use it to create the DRIVER_OBJECT
141  if (kvInfo->Type != REG_SZ || kvInfo->DataLength == 0)
142  {
143  ExFreePool(kvInfo);
145  }
146 
147  driverName.Length = kvInfo->DataLength - sizeof(WCHAR),
148  driverName.MaximumLength = kvInfo->DataLength,
149  driverName.Buffer = ExAllocatePoolWithTag(NonPagedPool, driverName.MaximumLength, TAG_IO);
150  if (!driverName.Buffer)
151  {
152  ExFreePool(kvInfo);
154  }
155 
156  RtlMoveMemory(driverName.Buffer,
157  (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
158  driverName.Length);
159  ExFreePool(kvInfo);
160  }
161 
162  // check if we need to get ServiceName as well
163  PKEY_BASIC_INFORMATION basicInfo;
164  if (!NT_SUCCESS(status) || ServiceName != NULL)
165  {
166  ULONG infoLength;
167  status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
169  {
170  basicInfo = ExAllocatePoolWithTag(PagedPool, infoLength, TAG_IO);
171  if (!basicInfo)
172  {
174  }
175 
176  status = ZwQueryKey(ServiceHandle, KeyBasicInformation, basicInfo, infoLength, &infoLength);
177  if (!NT_SUCCESS(status))
178  {
179  ExFreePoolWithTag(basicInfo, TAG_IO);
180  return status;
181  }
182 
183  serviceName.Length = basicInfo->NameLength;
184  serviceName.MaximumLength = basicInfo->NameLength;
185  serviceName.Buffer = basicInfo->Name;
186  }
187  else
188  {
190  }
191  }
192 
193  // 2. there is no "ObjectName" - construct it ourselves. Depending on a driver type,
194  // it will be either "\Driver<ServiceName>" or "\FileSystem<ServiceName>"
195  if (driverName.Buffer == NULL)
196  {
197  status = IopGetRegistryValue(ServiceHandle, L"Type", &kvInfo);
198  if (!NT_SUCCESS(status) || kvInfo->Type != REG_DWORD)
199  {
200  ExFreePool(kvInfo);
201  ExFreePoolWithTag(basicInfo, TAG_IO); // container for serviceName
203  }
204 
205  UINT32 driverType;
206  RtlMoveMemory(&driverType, (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset), sizeof(UINT32));
207  ExFreePool(kvInfo);
208 
209  driverName.Length = 0;
210  if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
211  driverName.MaximumLength = sizeof(FILESYSTEM_ROOT_NAME) + serviceName.Length;
212  else
213  driverName.MaximumLength = sizeof(DRIVER_ROOT_NAME) + serviceName.Length;
214  driverName.Buffer = ExAllocatePoolWithTag(NonPagedPool, driverName.MaximumLength, TAG_IO);
215  if (!driverName.Buffer)
216  {
217  ExFreePoolWithTag(basicInfo, TAG_IO); // container for serviceName
219  }
220 
221  if (driverType == SERVICE_RECOGNIZER_DRIVER || driverType == SERVICE_FILE_SYSTEM_DRIVER)
223  else
225 
227  }
228 
229  if (ServiceName)
230  {
232  if (!buf)
233  {
234  ExFreePoolWithTag(basicInfo, TAG_IO); // container for serviceName
235  ExFreePoolWithTag(driverName.Buffer, TAG_IO);
237  }
238  RtlMoveMemory(buf, serviceName.Buffer, serviceName.Length);
239  ServiceName->MaximumLength = serviceName.Length;
240  ServiceName->Length = serviceName.Length;
241  ServiceName->Buffer = buf;
242  }
243  ExFreePoolWithTag(basicInfo, TAG_IO); // container for ServiceName
244 
245  *DriverName = driverName;
246 
247  return STATUS_SUCCESS;
248 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
#define FILESYSTEM_ROOT_NAME
Definition: ldr.h:6
uint16_t * PWCHAR
Definition: typedefs.h:56
#define SERVICE_RECOGNIZER_DRIVER
Definition: cmtypes.h:956
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
uint32_t ULONG_PTR
Definition: typedefs.h:65
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
unsigned int UINT32
#define SERVICE_FILE_SYSTEM_DRIVER
Definition: cmtypes.h:954
#define STATUS_ILL_FORMED_SERVICE_ENTRY
Definition: ntstatus.h:588
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
char serviceName[]
Definition: tftpd.cpp:34
LPTSTR ServiceName
Definition: ServiceMain.c:15
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static const WCHAR L[]
Definition: oid.c:1250
NTSTATUS NTAPI IopGetRegistryValue(IN HANDLE Handle, IN PWSTR ValueName, OUT PKEY_VALUE_FULL_INFORMATION *Information)
Definition: pnpmgr.c:1683
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define REG_DWORD
Definition: sdbapi.c:596
static SERVICE_STATUS status
Definition: service.c:31
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define DRIVER_ROOT_NAME
Definition: ldr.h:5
#define PAGED_CODE()
#define REG_SZ
Definition: layer.c:22
Definition: ps.c:97

Referenced by IopInitializeDriverModule(), and PiAttachFilterDriversCallback().

◆ IopInitializeBootDrivers()

VOID FASTCALL IopInitializeBootDrivers ( VOID  )

Definition at line 882 of file driver.c.

883 {
884  PLIST_ENTRY ListHead, NextEntry, NextEntry2;
885  PLDR_DATA_TABLE_ENTRY LdrEntry;
887  UNICODE_STRING DriverName;
888  ULONG i, Index;
889  PDRIVER_INFORMATION DriverInfo, DriverInfoTag;
891  PBOOT_DRIVER_LIST_ENTRY BootEntry;
892  DPRINT("IopInitializeBootDrivers()\n");
893 
894  /* Create the RAW FS built-in driver */
895  RtlInitUnicodeString(&DriverName, L"\\FileSystem\\RAW");
896 
897  Status = IoCreateDriver(&DriverName, RawFsDriverEntry);
898  if (!NT_SUCCESS(Status))
899  {
900  /* Fail */
901  return;
902  }
903 
904  /* Get highest group order index */
906  if (IopGroupIndex == 0xFFFF) ASSERT(FALSE);
907 
908  /* Allocate the group table */
910  IopGroupIndex * sizeof(LIST_ENTRY),
911  TAG_IO);
912  if (IopGroupTable == NULL) ASSERT(FALSE);
913 
914  /* Initialize the group table lists */
915  for (i = 0; i < IopGroupIndex; i++) InitializeListHead(&IopGroupTable[i]);
916 
917  /* Loop the boot modules */
918  ListHead = &KeLoaderBlock->LoadOrderListHead;
919  NextEntry = ListHead->Flink;
920  while (ListHead != NextEntry)
921  {
922  /* Get the entry */
923  LdrEntry = CONTAINING_RECORD(NextEntry,
925  InLoadOrderLinks);
926 
927  /* Check if the DLL needs to be initialized */
928  if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
929  {
930  /* Call its entrypoint */
931  MmCallDllInitialize(LdrEntry, NULL);
932  }
933 
934  /* Go to the next driver */
935  NextEntry = NextEntry->Flink;
936  }
937 
938  /* Loop the boot drivers */
939  ListHead = &KeLoaderBlock->BootDriverListHead;
940  NextEntry = ListHead->Flink;
941  while (ListHead != NextEntry)
942  {
943  /* Get the entry */
944  BootEntry = CONTAINING_RECORD(NextEntry,
946  Link);
947 
948  /* Get the driver loader entry */
949  LdrEntry = BootEntry->LdrEntry;
950 
951  /* Allocate our internal accounting structure */
953  sizeof(DRIVER_INFORMATION),
954  TAG_IO);
955  if (DriverInfo)
956  {
957  /* Zero it and initialize it */
960  DriverInfo->DataTableEntry = BootEntry;
961 
962  /* Open the registry key */
964  NULL,
965  &BootEntry->RegistryPath,
966  KEY_READ);
967  DPRINT("IopOpenRegistryKeyEx(%wZ) returned 0x%08lx\n", &BootEntry->RegistryPath, Status);
968 #if 0
969  if (NT_SUCCESS(Status))
970 #else // Hack still needed...
971  if ((NT_SUCCESS(Status)) || /* ReactOS HACK for SETUPLDR */
972  ((KeLoaderBlock->SetupLdrBlock) && ((KeyHandle = (PVOID)1)))) // yes, it's an assignment!
973 #endif
974  {
975  /* Save the handle */
977 
978  /* Get the group oder index */
980 
981  /* Get the tag position */
983 
984  /* Insert it into the list, at the right place */
986  NextEntry2 = IopGroupTable[Index].Flink;
987  while (NextEntry2 != &IopGroupTable[Index])
988  {
989  /* Get the driver info */
990  DriverInfoTag = CONTAINING_RECORD(NextEntry2,
992  Link);
993 
994  /* Check if we found the right tag position */
995  if (DriverInfoTag->TagPosition > DriverInfo->TagPosition)
996  {
997  /* We're done */
998  break;
999  }
1000 
1001  /* Next entry */
1002  NextEntry2 = NextEntry2->Flink;
1003  }
1004 
1005  /* Insert us right before the next entry */
1006  NextEntry2 = NextEntry2->Blink;
1007  InsertHeadList(NextEntry2, &DriverInfo->Link);
1008  }
1009  }
1010 
1011  /* Go to the next driver */
1012  NextEntry = NextEntry->Flink;
1013  }
1014 
1015  /* Loop each group index */
1016  for (i = 0; i < IopGroupIndex; i++)
1017  {
1018  /* Loop each group table */
1019  NextEntry = IopGroupTable[i].Flink;
1020  while (NextEntry != &IopGroupTable[i])
1021  {
1022  /* Get the entry */
1023  DriverInfo = CONTAINING_RECORD(NextEntry,
1025  Link);
1026 
1027  /* Get the driver loader entry */
1028  LdrEntry = DriverInfo->DataTableEntry->LdrEntry;
1029 
1030  /* Initialize it */
1031  IopInitializeBuiltinDriver(LdrEntry);
1032 
1033  /* Start the devices found by a driver (if any) */
1036  NULL,
1037  NULL);
1038 
1039  /* Next entry */
1040  NextEntry = NextEntry->Flink;
1041  }
1042  }
1043 
1044  /* HAL Root Bus is being initialized before loading the boot drivers so this may cause issues
1045  * when some devices are not being initialized with their drivers. This flag is used to delay
1046  * all actions with devices (except PnP root device) until boot drivers are loaded.
1047  * See PiQueueDeviceAction function
1048  */
1050 }
NTSTATUS NTAPI RawFsDriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: rawfs.c:1193
PDEVICE_OBJECT PhysicalDeviceObject
Definition: iotypes.h:839
PDEVICE_NODE IopRootDeviceNode
Definition: devnode.c:18
#define TAG_IO
Definition: tag.h:69
VOID PiQueueDeviceAction(_In_ PDEVICE_OBJECT DeviceObject, _In_ DEVICE_ACTION Action, _In_opt_ PKEVENT CompletionEvent, _Out_opt_ NTSTATUS *CompletionStatus)
Queue a device operation to a worker thread.
Definition: devaction.c:2950
_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
#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:155
Definition: arc.h:198
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
LIST_ENTRY Link
Definition: io.h:399
HANDLE ServiceHandle
Definition: io.h:402
void DPRINT(...)
Definition: polytest.cpp:61
NTSTATUS NTAPI IoCreateDriver(_In_opt_ PUNICODE_STRING DriverName, _In_ PDRIVER_INITIALIZE InitializationFunction)
Definition: driver.c:1414
USHORT TagPosition
Definition: io.h:403
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
Status
Definition: gdiplustypes.h:24
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ WDFCOLLECTION _In_ ULONG Index
PLIST_ENTRY IopGroupTable
Definition: driver.c:43
LIST_ENTRY BootDriverListHead
Definition: arc.h:495
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
PBOOT_DRIVER_LIST_ENTRY DataTableEntry
Definition: io.h:401
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 IopInitializeBuiltinDriver(IN PLDR_DATA_TABLE_ENTRY BootLdrEntry)
Definition: driver.c:749
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:1532
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 _LDR_DATA_TABLE_ENTRY * LdrEntry
Definition: arc.h:203
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define NULL
Definition: types.h:112
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:42
USHORT NTAPI PipGetDriverTagPriority(IN HANDLE ServiceHandle)
Definition: pnpinit.c:198

Referenced by IoInitSystem().

◆ IopInitializeBuiltinDriver()

NTSTATUS NTAPI IopInitializeBuiltinDriver ( IN PLDR_DATA_TABLE_ENTRY  BootLdrEntry)

Definition at line 749 of file driver.c.

750 {
753  PWCHAR Buffer, FileNameWithoutPath;
754  PWSTR FileExtension;
755  PUNICODE_STRING ModuleName = &BootLdrEntry->BaseDllName;
756  PLDR_DATA_TABLE_ENTRY LdrEntry;
757  PLIST_ENTRY NextEntry;
760 
761  /*
762  * Display 'Loading XXX...' message
763  */
766 
768  ModuleName->Length + sizeof(UNICODE_NULL),
769  TAG_IO);
770  if (Buffer == NULL)
771  {
773  }
774 
776  Buffer[ModuleName->Length / sizeof(WCHAR)] = UNICODE_NULL;
777 
778  /*
779  * Generate filename without path (not needed by freeldr)
780  */
781  FileNameWithoutPath = wcsrchr(Buffer, L'\\');
782  if (FileNameWithoutPath == NULL)
783  {
784  FileNameWithoutPath = Buffer;
785  }
786  else
787  {
788  FileNameWithoutPath++;
789  }
790 
791  /*
792  * Strip the file extension from ServiceName
793  */
794  Success = RtlCreateUnicodeString(&ServiceName, FileNameWithoutPath);
796  if (!Success)
797  {
799  }
800 
801  FileExtension = wcsrchr(ServiceName.Buffer, L'.');
802  if (FileExtension != NULL)
803  {
804  ServiceName.Length -= (USHORT)wcslen(FileExtension) * sizeof(WCHAR);
805  FileExtension[0] = UNICODE_NULL;
806  }
807 
809 
810  // Make the registry path for the driver
811  RegistryPath.Length = 0;
812  RegistryPath.MaximumLength = sizeof(ServicesKeyName) + ServiceName.Length;
814  if (RegistryPath.Buffer == NULL)
815  {
817  }
821 
822  HANDLE serviceHandle;
823  Status = IopOpenRegistryKeyEx(&serviceHandle, NULL, &RegistryPath, KEY_READ);
825  if (!NT_SUCCESS(Status))
826  {
827  return Status;
828  }
829 
830  /* Lookup the new Ldr entry in PsLoadedModuleList */
831  NextEntry = PsLoadedModuleList.Flink;
832  while (NextEntry != &PsLoadedModuleList)
833  {
834  LdrEntry = CONTAINING_RECORD(NextEntry,
836  InLoadOrderLinks);
838  {
839  break;
840  }
841 
842  NextEntry = NextEntry->Flink;
843  }
844  ASSERT(NextEntry != &PsLoadedModuleList);
845 
846  /*
847  * Initialize the driver
848  */
849  NTSTATUS driverEntryStatus;
851  serviceHandle,
852  &DriverObject,
853  &driverEntryStatus);
854  ZwClose(serviceHandle);
855 
856  if (!NT_SUCCESS(Status))
857  {
858  DPRINT1("Driver '%wZ' load failed, status (%x)\n", ModuleName, Status);
859  return Status;
860  }
861 
862  /* Remove extra reference from IopInitializeDriverModule */
864 
865  return Status;
866 }
VOID NTAPI InbvIndicateProgress(VOID)
Definition: inbv.c:884
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define KEY_READ
Definition: nt_native.h:1023
#define TRUE
Definition: types.h:120
VOID FASTCALL IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
Definition: driver.c:290
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
uint16_t * PWSTR
Definition: typedefs.h:56
ACPI_SIZE Length
Definition: actypes.h:1044
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:213
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
#define UNICODE_NULL
unsigned char BOOLEAN
Definition: bufpool.h:45
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
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
Status
Definition: gdiplustypes.h:24
NTSTATUS RtlAppendUnicodeToString(IN PUNICODE_STRING Str1, IN PWSTR Str2)
Definition: string_lib.cpp:62
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define ASSERT(a)
Definition: mode.c:45
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
LIST_ENTRY PsLoadedModuleList
Definition: sysldr.c:34
#define ObDereferenceObject
Definition: obfuncs.h:203
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
NTSYSAPI BOOLEAN NTAPI RtlCreateUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
static const WCHAR L[]
Definition: oid.c:1250
Definition: btrfs_drv.h:1922
Definition: typedefs.h:119
#define wcsrchr
Definition: compat.h:16
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:1532
unsigned short USHORT
Definition: pedump.c:61
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
UNICODE_STRING BaseDllName
Definition: ldrtypes.h:145
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
NTSTATUS IopInitializeDriverModule(_In_ PLDR_DATA_TABLE_ENTRY ModuleObject, _In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *OutDriverObject, _Out_ NTSTATUS *DriverEntryStatus)
Initialize a loaded driver.
Definition: driver.c:403
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
static const WCHAR ServicesKeyName[]
Definition: driver.c:31
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
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 IopInitializeDriverModule ( _In_ PLDR_DATA_TABLE_ENTRY  ModuleObject,
_In_ HANDLE  ServiceHandle,
_Out_ PDRIVER_OBJECT OutDriverObject,
_Out_ NTSTATUS DriverEntryStatus 
)

Initialize a loaded driver.

Parameters
[in]ModuleObjectModule object representing the driver. It can be retrieved by IopLoadServiceModule. Freed on failure, so in a such case this should not be accessed anymore
[in]ServiceHandleHandle to a driver's CCS/Services/<ServiceName> key
[out]DriverObjectThis contains the driver object if it was created (even with unsuccessfull result)
[out]DriverEntryStatusThis contains the status value returned by the driver's DriverEntry routine (will not be valid of the return value is not STATUS_SUCCESS or STATUS_FAILED_DRIVER_ENTRY)
Returns
Status of the operation

Definition at line 403 of file driver.c.

408 {
411 
412  PAGED_CODE();
413 
414  Status = IopGetDriverNames(ServiceHandle, &DriverName, &ServiceName);
415  if (!NT_SUCCESS(Status))
416  {
417  MmUnloadSystemImage(ModuleObject);
418  return Status;
419  }
420 
421  DPRINT("Driver name: '%wZ'\n", &DriverName);
422 
423  // obtain the registry path for the DriverInit routine
424  PKEY_NAME_INFORMATION nameInfo;
425  ULONG infoLength;
426  Status = ZwQueryKey(ServiceHandle, KeyNameInformation, NULL, 0, &infoLength);
428  {
429  nameInfo = ExAllocatePoolWithTag(NonPagedPool, infoLength, TAG_IO);
430  if (nameInfo)
431  {
432  Status = ZwQueryKey(ServiceHandle,
434  nameInfo,
435  infoLength,
436  &infoLength);
437  if (NT_SUCCESS(Status))
438  {
439  RegistryPath.Length = nameInfo->NameLength;
440  RegistryPath.MaximumLength = nameInfo->NameLength;
441  RegistryPath.Buffer = nameInfo->Name;
442  }
443  else
444  {
445  ExFreePoolWithTag(nameInfo, TAG_IO);
446  }
447  }
448  else
449  {
451  }
452  }
453  else
454  {
456  }
457 
458  if (!NT_SUCCESS(Status))
459  {
461  RtlFreeUnicodeString(&DriverName);
462  MmUnloadSystemImage(ModuleObject);
463  return Status;
464  }
465 
466  // create the driver object
467  UINT32 ObjectSize = sizeof(DRIVER_OBJECT) + sizeof(EXTENDED_DRIVER_EXTENSION);
468  OBJECT_ATTRIBUTES objAttrs;
469  PDRIVER_OBJECT driverObject;
470  InitializeObjectAttributes(&objAttrs,
471  &DriverName,
473  NULL,
474  NULL);
475 
478  &objAttrs,
479  KernelMode,
480  NULL,
481  ObjectSize,
482  0,
483  0,
484  (PVOID*)&driverObject);
485  if (!NT_SUCCESS(Status))
486  {
487  ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
489  RtlFreeUnicodeString(&DriverName);
490  MmUnloadSystemImage(ModuleObject);
491  DPRINT1("Error while creating driver object \"%wZ\" status %x\n", &DriverName, Status);
492  return Status;
493  }
494 
495  DPRINT("Created driver object 0x%p for \"%wZ\"\n", driverObject, &DriverName);
496 
497  RtlZeroMemory(driverObject, ObjectSize);
498  driverObject->Type = IO_TYPE_DRIVER;
499  driverObject->Size = sizeof(DRIVER_OBJECT);
500  driverObject->Flags = DRVO_LEGACY_DRIVER; // TODO: check the WDM_DRIVER flag on the module
501  driverObject->DriverSection = ModuleObject;
502  driverObject->DriverStart = ModuleObject->DllBase;
503  driverObject->DriverSize = ModuleObject->SizeOfImage;
504  driverObject->DriverInit = ModuleObject->EntryPoint;
505  driverObject->HardwareDatabase = &IopHardwareDatabaseKey;
506  driverObject->DriverExtension = (PDRIVER_EXTENSION)(driverObject + 1);
507  driverObject->DriverExtension->DriverObject = driverObject;
508 
509  /* Loop all Major Functions */
510  for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
511  {
512  /* Invalidate each function */
513  driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
514  }
515 
516  /* Add the Object and get its handle */
517  HANDLE hDriver;
518  Status = ObInsertObject(driverObject, NULL, FILE_READ_DATA, 0, NULL, &hDriver);
519  if (!NT_SUCCESS(Status))
520  {
521  ExFreePoolWithTag(nameInfo, TAG_IO);
523  RtlFreeUnicodeString(&DriverName);
524  return Status;
525  }
526 
527  /* Now reference it */
529  0,
531  KernelMode,
532  (PVOID*)&driverObject,
533  NULL);
534 
535  /* Close the extra handle */
536  ZwClose(hDriver);
537 
538  if (!NT_SUCCESS(Status))
539  {
540  ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
542  RtlFreeUnicodeString(&DriverName);
543  return Status;
544  }
545 
546  /* Set up the service key name buffer */
547  UNICODE_STRING serviceKeyName;
548  serviceKeyName.Length = 0;
549  // put a NULL character at the end for Windows compatibility
550  serviceKeyName.MaximumLength = ServiceName.MaximumLength + sizeof(UNICODE_NULL);
551  serviceKeyName.Buffer = ExAllocatePoolWithTag(NonPagedPool,
552  serviceKeyName.MaximumLength,
553  TAG_IO);
554  if (!serviceKeyName.Buffer)
555  {
556  ObMakeTemporaryObject(driverObject);
557  ObDereferenceObject(driverObject);
558  ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
560  RtlFreeUnicodeString(&DriverName);
562  }
563 
564  /* Copy the name and set it in the driver extension */
565  RtlCopyUnicodeString(&serviceKeyName, &ServiceName);
567  driverObject->DriverExtension->ServiceKeyName = serviceKeyName;
568 
569  /* Make a copy of the driver name to store in the driver object */
570  UNICODE_STRING driverNamePaged;
571  driverNamePaged.Length = 0;
572  // put a NULL character at the end for Windows compatibility
573  driverNamePaged.MaximumLength = DriverName.MaximumLength + sizeof(UNICODE_NULL);
574  driverNamePaged.Buffer = ExAllocatePoolWithTag(PagedPool,
575  driverNamePaged.MaximumLength,
576  TAG_IO);
577  if (!driverNamePaged.Buffer)
578  {
579  ObMakeTemporaryObject(driverObject);
580  ObDereferenceObject(driverObject);
581  ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
582  RtlFreeUnicodeString(&DriverName);
584  }
585 
586  RtlCopyUnicodeString(&driverNamePaged, &DriverName);
587  driverObject->DriverName = driverNamePaged;
588 
589  /* Finally, call its init function */
590  Status = driverObject->DriverInit(driverObject, &RegistryPath);
591  *DriverEntryStatus = Status;
592  if (!NT_SUCCESS(Status))
593  {
594  DPRINT1("'%wZ' initialization failed, status (0x%08lx)\n", &DriverName, Status);
595  // return a special status value in case of failure
597  }
598 
599  /* HACK: We're going to say if we don't have any DOs from DriverEntry, then we're not legacy.
600  * Other parts of the I/O manager depend on this behavior */
601  if (!driverObject->DeviceObject)
602  {
603  driverObject->Flags &= ~DRVO_LEGACY_DRIVER;
604  }
605 
606  // Windows does this fixup - keep it for compatibility
607  for (INT i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
608  {
609  /*
610  * Make sure the driver didn't set any dispatch entry point to NULL!
611  * Doing so is illegal; drivers shouldn't touch entry points they
612  * do not implement.
613  */
614 
615  /* Check if it did so anyway */
616  if (!driverObject->MajorFunction[i])
617  {
618  /* Print a warning in the debug log */
619  DPRINT1("Driver <%wZ> set DriverObject->MajorFunction[%lu] to NULL!\n",
620  &driverObject->DriverName, i);
621 
622  /* Fix it up */
623  driverObject->MajorFunction[i] = IopInvalidDeviceRequest;
624  }
625  }
626 
627  // TODO: for legacy drivers, unload the driver if it didn't create any DO
628 
629  ExFreePoolWithTag(nameInfo, TAG_IO); // container for RegistryPath
630  RtlFreeUnicodeString(&DriverName);
631 
632  if (!NT_SUCCESS(Status))
633  {
634  // if the driver entry has been failed, clear the object
635  ObMakeTemporaryObject(driverObject);
636  ObDereferenceObject(driverObject);
637  return Status;
638  }
639 
640  *OutDriverObject = driverObject;
641 
643 
644  /* Set the driver as initialized */
645  IopReadyDeviceObjects(driverObject);
646 
648 
649  return STATUS_SUCCESS;
650 }
#define DRVO_LEGACY_DRIVER
Definition: iotypes.h:2223
VOID NTAPI MmFreeDriverInitialization(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
Definition: sysldr.c:1636
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
VOID NTAPI IopReinitializeDrivers(VOID)
Definition: driver.c:1334
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
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:213
#define STATUS_FAILED_DRIVER_ENTRY
Definition: ntstatus.h:911
PVOID DriverStart
Definition: iotypes.h:2276
_In_ LPWSTR _In_ ULONG _In_ ULONG _In_ ULONG _Out_ DEVINFO _In_ HDEV _In_ LPWSTR _In_ HANDLE hDriver
Definition: winddi.h:3553
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:33
BOOLEAN PnpSystemInit
Definition: iomgr.c:17
NTSTATUS IopGetDriverNames(_In_ HANDLE ServiceHandle, _Out_ PUNICODE_STRING DriverName, _Out_opt_ PUNICODE_STRING ServiceName)
Definition: driver.c:125
NTSTATUS NTAPI IopInvalidDeviceRequest(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: driver.c:67
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
int32_t INT
Definition: typedefs.h:58
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
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
unsigned int UINT32
struct _DRIVER_OBJECT * DriverObject
Definition: iotypes.h:2216
#define FILE_READ_DATA
Definition: nt_native.h:628
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2279
PUNICODE_STRING HardwareDatabase
Definition: iotypes.h:2281
void DPRINT(...)
Definition: polytest.cpp:61
CSHORT Size
Definition: iotypes.h:2273
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
NTSTATUS NTAPI MmUnloadSystemImage(IN PVOID ImageHandle)
Definition: sysldr.c:912
Status
Definition: gdiplustypes.h:24
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ObDereferenceObject
Definition: obfuncs.h:203
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
VOID NTAPI IopReadyDeviceObjects(IN PDRIVER_OBJECT Driver)
Definition: device.c:34
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define OBJ_PERMANENT
Definition: winternl.h:226
Definition: btrfs_drv.h:1922
struct _DRIVER_OBJECT DRIVER_OBJECT
UNICODE_STRING ServiceKeyName
Definition: iotypes.h:2219
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
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 _DRIVER_EXTENSION * PDRIVER_EXTENSION
UNICODE_STRING IopHardwareDatabaseKey
Definition: driver.c:29
#define NULL
Definition: types.h:112
#define IO_TYPE_DRIVER
#define DPRINT1
Definition: precomp.h:8
PDRIVER_DISPATCH MajorFunction[IRP_MJ_MAXIMUM_FUNCTION+1]
Definition: iotypes.h:2286
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
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
ULONG DriverSize
Definition: iotypes.h:2277
CSHORT Type
Definition: iotypes.h:2272
PVOID DriverSection
Definition: iotypes.h:2278
PDRIVER_INITIALIZE DriverInit
Definition: iotypes.h:2283
UNICODE_STRING DriverName
Definition: iotypes.h:2280
PDEVICE_OBJECT DeviceObject
Definition: iotypes.h:2274
#define PAGED_CODE()
VOID NTAPI ObMakeTemporaryObject(IN PVOID ObjectBody)
Definition: oblife.c:1361

Referenced by IopInitializeBuiltinDriver(), and IopLoadDriver().

◆ IopInitializeSystemDrivers()

VOID FASTCALL IopInitializeSystemDrivers ( VOID  )

Definition at line 1055 of file driver.c.

1056 {
1057  PUNICODE_STRING *DriverList, *SavedList;
1058 
1059  /* No system drivers on the boot cd */
1060  if (KeLoaderBlock->SetupLdrBlock) return; // ExpInTextModeSetup
1061 
1062  /* Get the driver list */
1063  SavedList = DriverList = CmGetSystemDriverList();
1064  ASSERT(DriverList);
1065 
1066  /* Loop it */
1067  while (*DriverList)
1068  {
1069  /* Load the driver */
1070  ZwLoadDriver(*DriverList);
1071 
1072  /* Free the entry */
1073  RtlFreeUnicodeString(*DriverList);
1074  ExFreePool(*DriverList);
1075 
1076  /* Next entry */
1078  DriverList++;
1079  }
1080 
1081  /* Free the list */
1082  ExFreePool(SavedList);
1083 }
VOID NTAPI InbvIndicateProgress(VOID)
Definition: inbv.c:884
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
#define ASSERT(a)
Definition: mode.c:45
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352

Referenced by IoInitSystem().

◆ IopInvalidDeviceRequest()

NTSTATUS NTAPI IopInvalidDeviceRequest ( PDEVICE_OBJECT  DeviceObject,
PIRP  Irp 
)

Definition at line 67 of file driver.c.

70 {
71  Irp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
72  Irp->IoStatus.Information = 0;
75 }
#define STATUS_INVALID_DEVICE_REQUEST
Definition: udferr_usr.h:138
_In_ PIRP Irp
Definition: csq.h:116
#define IoCompleteRequest
Definition: irp.c:1240
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by IoCreateDriver(), and IopInitializeDriverModule().

◆ IopLoadDriver()

NTSTATUS IopLoadDriver ( _In_ HANDLE  ServiceHandle,
_Out_ PDRIVER_OBJECT DriverObject 
)

Definition at line 1775 of file driver.c.

1778 {
1779  UNICODE_STRING ImagePath;
1780  NTSTATUS Status;
1781  PLDR_DATA_TABLE_ENTRY ModuleObject;
1783 
1785  Status = IopGetRegistryValue(ServiceHandle, L"ImagePath", &kvInfo);
1786  if (NT_SUCCESS(Status))
1787  {
1788  if (kvInfo->Type != REG_EXPAND_SZ || kvInfo->DataLength == 0)
1789  {
1790  ExFreePool(kvInfo);
1792  }
1793 
1794  ImagePath.Length = kvInfo->DataLength - sizeof(UNICODE_NULL),
1795  ImagePath.MaximumLength = kvInfo->DataLength,
1797  if (!ImagePath.Buffer)
1798  {
1799  ExFreePool(kvInfo);
1801  }
1802 
1803  RtlMoveMemory(ImagePath.Buffer,
1804  (PVOID)((ULONG_PTR)kvInfo + kvInfo->DataOffset),
1805  ImagePath.Length);
1806  ExFreePool(kvInfo);
1807  }
1808  else
1809  {
1810  return Status;
1811  }
1812 
1813  /*
1814  * Normalize the image path for all later processing.
1815  */
1816  Status = IopNormalizeImagePath(&ImagePath, NULL);
1817  if (!NT_SUCCESS(Status))
1818  {
1819  DPRINT("IopNormalizeImagePath() failed (Status %x)\n", Status);
1820  return Status;
1821  }
1822 
1823  DPRINT("FullImagePath: '%wZ'\n", &ImagePath);
1824 
1827 
1828  /*
1829  * Load the driver module
1830  */
1831  DPRINT("Loading module from %wZ\n", &ImagePath);
1832  Status = MmLoadSystemImage(&ImagePath, NULL, NULL, 0, (PVOID)&ModuleObject, &BaseAddress);
1833  RtlFreeUnicodeString(&ImagePath);
1834 
1835  if (!NT_SUCCESS(Status))
1836  {
1837  DPRINT("MmLoadSystemImage() failed (Status %lx)\n", Status);
1840  return Status;
1841  }
1842 
1843  // Display the loading message
1844  ULONG infoLength;
1845  Status = ZwQueryKey(ServiceHandle, KeyBasicInformation, NULL, 0, &infoLength);
1847  {
1849  if (servName)
1850  {
1851  Status = ZwQueryKey(ServiceHandle,
1853  servName,
1854  infoLength,
1855  &infoLength);
1856  if (NT_SUCCESS(Status))
1857  {
1859  .Length = servName->NameLength,
1860  .MaximumLength = servName->NameLength,
1861  .Buffer = servName->Name
1862  };
1863 
1865  }
1866  ExFreePoolWithTag(servName, TAG_IO);
1867  }
1868  }
1869 
1870  NTSTATUS driverEntryStatus;
1871  Status = IopInitializeDriverModule(ModuleObject,
1872  ServiceHandle,
1873  DriverObject,
1874  &driverEntryStatus);
1875  if (!NT_SUCCESS(Status))
1876  {
1877  DPRINT1("IopInitializeDriverModule() failed (Status %lx)\n", Status);
1878  }
1879 
1882 
1883  return Status;
1884 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define TAG_IO
Definition: tag.h:69
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define TAG_RTLREGISTRY
Definition: driver.c:35
#define TRUE
Definition: types.h:120
VOID FASTCALL IopDisplayLoadingMessage(PUNICODE_STRING ServiceName)
Definition: driver.c:290
LONG NTSTATUS
Definition: precomp.h:26
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:264
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
#define UNICODE_NULL
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:329
void DPRINT(...)
Definition: polytest.cpp:61
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define STATUS_ILL_FORMED_SERVICE_ENTRY
Definition: ntstatus.h:588
Status
Definition: gdiplustypes.h:24
char serviceName[]
Definition: tftpd.cpp:34
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static const WCHAR L[]
Definition: oid.c:1250
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
Definition: btrfs_drv.h:1922
NTSTATUS NTAPI IopGetRegistryValue(IN HANDLE Handle, IN PWSTR ValueName, OUT PKEY_VALUE_FULL_INFORMATION *Information)
Definition: pnpmgr.c:1683
ERESOURCE IopDriverLoadResource
Definition: driver.c:19
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
#define REG_EXPAND_SZ
Definition: nt_native.h:1494
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
NTSTATUS IopInitializeDriverModule(_In_ PLDR_DATA_TABLE_ENTRY ModuleObject, _In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *OutDriverObject, _Out_ NTSTATUS *DriverEntryStatus)
Initialize a loaded driver.
Definition: driver.c:403
unsigned int ULONG
Definition: retypes.h:1
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
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 IopLoadUnloadDriverWorker(), and PiAttachFilterDriversCallback().

◆ IopLoadUnloadDriverWorker()

static VOID NTAPI IopLoadUnloadDriverWorker ( _Inout_ PVOID  Parameter)
static

Definition at line 1889 of file driver.c.

1891 {
1892  PLOAD_UNLOAD_PARAMS LoadParams = Parameter;
1893 
1895 
1896  if (LoadParams->DriverObject)
1897  {
1898  // unload request
1899  LoadParams->DriverObject->DriverUnload(LoadParams->DriverObject);
1900  LoadParams->Status = STATUS_SUCCESS;
1901  }
1902  else
1903  {
1904  // load request
1905  HANDLE serviceHandle;
1906  NTSTATUS status;
1907  status = IopOpenRegistryKeyEx(&serviceHandle, NULL, LoadParams->RegistryPath, KEY_READ);
1908  if (!NT_SUCCESS(status))
1909  {
1910  LoadParams->Status = status;
1911  }
1912  else
1913  {
1914  LoadParams->Status = IopLoadDriver(serviceHandle, &LoadParams->DriverObject);
1915  ZwClose(serviceHandle);
1916  }
1917  }
1918 
1919  if (LoadParams->SetEvent)
1920  {
1921  KeSetEvent(&LoadParams->Event, 0, FALSE);
1922  }
1923 }
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS IopLoadDriver(_In_ HANDLE ServiceHandle, _Out_ PDRIVER_OBJECT *DriverObject)
Definition: driver.c:1775
_In_ PVOID Parameter
Definition: ldrtypes.h:241
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
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS Status
Definition: driver.c:50
PDRIVER_UNLOAD DriverUnload
Definition: iotypes.h:2285
BOOLEAN SetEvent
Definition: driver.c:55
PDRIVER_OBJECT DriverObject
Definition: driver.c:54
PUNICODE_STRING RegistryPath
Definition: driver.c:51
NTSTATUS NTAPI IopOpenRegistryKeyEx(PHANDLE KeyHandle, HANDLE ParentKey, PUNICODE_STRING Name, ACCESS_MASK DesiredAccess)
Definition: pnpmgr.c:1532
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65
static SERVICE_STATUS status
Definition: service.c:31
Definition: ps.c:97

Referenced by IopDoLoadUnloadDriver().

◆ 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 329 of file driver.c.

333 {
334  UNICODE_STRING SystemRootString = RTL_CONSTANT_STRING(L"\\SystemRoot\\");
335  UNICODE_STRING DriversPathString = RTL_CONSTANT_STRING(L"\\SystemRoot\\System32\\drivers\\");
336  UNICODE_STRING DotSysString = RTL_CONSTANT_STRING(L".sys");
337  UNICODE_STRING InputImagePath;
338 
339  DPRINT("Normalizing image path '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
340 
341  InputImagePath = *ImagePath;
342  if (InputImagePath.Length == 0)
343  {
344  ImagePath->Length = 0;
345  ImagePath->MaximumLength = DriversPathString.Length +
346  ServiceName->Length +
347  DotSysString.Length +
348  sizeof(UNICODE_NULL);
349  ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
350  ImagePath->MaximumLength,
351  TAG_IO);
352  if (ImagePath->Buffer == NULL)
353  return STATUS_NO_MEMORY;
354 
355  RtlCopyUnicodeString(ImagePath, &DriversPathString);
357  RtlAppendUnicodeStringToString(ImagePath, &DotSysString);
358  }
359  else if (InputImagePath.Buffer[0] != L'\\')
360  {
361  ImagePath->Length = 0;
362  ImagePath->MaximumLength = SystemRootString.Length +
363  InputImagePath.Length +
364  sizeof(UNICODE_NULL);
365  ImagePath->Buffer = ExAllocatePoolWithTag(NonPagedPool,
366  ImagePath->MaximumLength,
367  TAG_IO);
368  if (ImagePath->Buffer == NULL)
369  return STATUS_NO_MEMORY;
370 
371  RtlCopyUnicodeString(ImagePath, &SystemRootString);
372  RtlAppendUnicodeStringToString(ImagePath, &InputImagePath);
373 
374  /* Free caller's string */
375  ExFreePoolWithTag(InputImagePath.Buffer, TAG_RTLREGISTRY);
376  }
377 
378  DPRINT("Normalized image path is '%wZ' for service '%wZ'\n", ImagePath, ServiceName);
379 
380  return STATUS_SUCCESS;
381 }
#define TAG_IO
Definition: tag.h:69
#define TAG_RTLREGISTRY
Definition: driver.c:35
NTSYSAPI VOID NTAPI RtlCopyUnicodeString(PUNICODE_STRING DestinationString, PUNICODE_STRING SourceString)
#define UNICODE_NULL
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 NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Referenced by IopLoadDriver(), and IopUnloadDriver().

◆ IopReinitializeBootDrivers()

VOID NTAPI IopReinitializeBootDrivers ( VOID  )

Definition at line 1370 of file driver.c.

1371 {
1372  PDRIVER_REINIT_ITEM ReinitItem;
1374 
1375  /* Get the first entry and start looping */
1378  while (Entry)
1379  {
1380  /* Get the item */
1381  ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1382 
1383  /* Increment reinitialization counter */
1384  ReinitItem->DriverObject->DriverExtension->Count++;
1385 
1386  /* Remove the device object flag */
1388 
1389  /* Call the routine */
1390  ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1391  ReinitItem->Context,
1392  ReinitItem->DriverObject->
1393  DriverExtension->Count);
1394 
1395  /* Free the entry */
1396  ExFreePool(Entry);
1397 
1398  /* Move to the next one */
1401  }
1402 
1403  /* Wait for all device actions being finished*/
1405 }
struct _Entry Entry
Definition: kefuncs.h:627
PVOID Context
Definition: io.h:459
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:2279
PLIST_ENTRY NTAPI ExInterlockedRemoveHeadList(IN OUT PLIST_ENTRY ListHead, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:166
PDRIVER_OBJECT DriverObject
Definition: io.h:457
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:50
Definition: typedefs.h:119
PPCI_DRIVER_EXTENSION DriverExtension
Definition: pci.c:41
#define NULL
Definition: types.h:112
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:458
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:4467

Referenced by IoInitSystem().

◆ IopReinitializeDrivers()

VOID NTAPI IopReinitializeDrivers ( VOID  )

Definition at line 1334 of file driver.c.

1335 {
1336  PDRIVER_REINIT_ITEM ReinitItem;
1338 
1339  /* Get the first entry and start looping */
1342  while (Entry)
1343  {
1344  /* Get the item */
1345  ReinitItem = CONTAINING_RECORD(Entry, DRIVER_REINIT_ITEM, ItemEntry);
1346 
1347  /* Increment reinitialization counter */
1348  ReinitItem->DriverObject->DriverExtension->Count++;
1349 
1350  /* Remove the device object flag */
1351  ReinitItem->DriverObject->Flags &= ~DRVO_REINIT_REGISTERED;
1352 
1353  /* Call the routine */
1354  ReinitItem->ReinitRoutine(ReinitItem->DriverObject,
1355  ReinitItem->Context,
1356  ReinitItem->DriverObject->
1357  DriverExtension->Count);
1358 
1359  /* Free the entry */
1360  ExFreePool(Entry);
1361 
1362  /* Move to the next one */
1365  }
1366 }
struct _Entry Entry
Definition: kefuncs.h:627
PVOID Context
Definition: io.h:459
#define DRVO_REINIT_REGISTERED
Definition: iotypes.h:4465
KSPIN_LOCK DriverReinitListLock
Definition: driver.c:22
PDRIVER_EXTENSION DriverExtension
Definition: iotypes.h:2279
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:457
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:458
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 256 of file driver.c.

259 {
260  PWCHAR pc1;
261  PWCHAR pc2;
262  ULONG Length;
263 
264  if (String2->Length < String1->Length)
265  return FALSE;
266 
267  Length = String1->Length / 2;
268  pc1 = String1->Buffer;
269  pc2 = &String2->Buffer[String2->Length / sizeof(WCHAR) - Length];
270 
271  if (pc1 && pc2)
272  {
273  while (Length--)
274  {
275  if( *pc1++ != *pc2++ )
276  return FALSE;
277  }
278  return TRUE;
279  }
280  return FALSE;
281 }
static const unsigned char pc2[48]
Definition: des.c:68
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
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_ 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 1107 of file driver.c.

1108 {
1111  UNICODE_STRING ImagePath;
1116  PEXTENDED_DEVOBJ_EXTENSION DeviceExtension;
1117  NTSTATUS Status;
1118  USHORT LastBackslash;
1119  BOOLEAN SafeToUnload = TRUE;
1121  UNICODE_STRING CapturedServiceName;
1122 
1123  PAGED_CODE();
1124 
1126 
1127  /* Need the appropriate priviliege */
1129  {
1130  DPRINT1("No unload privilege!\n");
1132  }
1133 
1134  /* Capture the service name */
1135  Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
1136  PreviousMode,
1137  DriverServiceName);
1138  if (!NT_SUCCESS(Status))
1139  {
1140  return Status;
1141  }
1142 
1143  DPRINT("IopUnloadDriver('%wZ', %u)\n", &CapturedServiceName, UnloadPnpDrivers);
1144 
1145  /* We need a service name */
1146  if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
1147  {
1148  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1149  return STATUS_INVALID_PARAMETER;
1150  }
1151 
1152  /*
1153  * Get the service name from the registry key name
1154  */
1156  &CapturedServiceName,
1157  &Backslash,
1158  &LastBackslash);
1159  if (NT_SUCCESS(Status))
1160  {
1161  NT_ASSERT(CapturedServiceName.Length >= LastBackslash + sizeof(WCHAR));
1162  ServiceName.Buffer = &CapturedServiceName.Buffer[LastBackslash / sizeof(WCHAR) + 1];
1163  ServiceName.Length = CapturedServiceName.Length - LastBackslash - sizeof(WCHAR);
1164  ServiceName.MaximumLength = CapturedServiceName.MaximumLength - LastBackslash - sizeof(WCHAR);
1165  }
1166  else
1167  {
1168  ServiceName = CapturedServiceName;
1169  }
1170 
1171  /*
1172  * Construct the driver object name
1173  */
1174  Status = RtlUShortAdd(sizeof(DRIVER_ROOT_NAME),
1175  ServiceName.Length,
1176  &ObjectName.MaximumLength);
1177  if (!NT_SUCCESS(Status))
1178  {
1179  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1180  return Status;
1181  }
1182  ObjectName.Length = 0;
1184  ObjectName.MaximumLength,
1185  TAG_IO);
1186  if (!ObjectName.Buffer)
1187  {
1188  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1190  }
1193 
1194  /*
1195  * Find the driver object
1196  */
1198  0,
1199  0,
1200  0,
1202  KernelMode,
1203  0,
1204  (PVOID*)&DriverObject);
1205 
1206  if (!NT_SUCCESS(Status))
1207  {
1208  DPRINT1("Can't locate driver object for %wZ\n", &ObjectName);
1210  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1211  return Status;
1212  }
1213 
1214  /* Free the buffer for driver object name */
1216 
1217  /* Check that driver is not already unloading */
1218  if (DriverObject->Flags & DRVO_UNLOAD_INVOKED)
1219  {
1220  DPRINT1("Driver deletion pending\n");
1222  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1223  return STATUS_DELETE_PENDING;
1224  }
1225 
1226  /*
1227  * Get path of service...
1228  */
1230 
1231  RtlInitUnicodeString(&ImagePath, NULL);
1232 
1233  QueryTable[0].Name = L"ImagePath";
1235  QueryTable[0].EntryContext = &ImagePath;
1236 
1238  CapturedServiceName.Buffer,
1239  QueryTable,
1240  NULL,
1241  NULL);
1242 
1243  /* We no longer need service name */
1244  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
1245 
1246  if (!NT_SUCCESS(Status))
1247  {
1248  DPRINT1("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1250  return Status;
1251  }
1252 
1253  /*
1254  * Normalize the image path for all later processing.
1255  */
1256  Status = IopNormalizeImagePath(&ImagePath, &ServiceName);
1257 
1258  if (!NT_SUCCESS(Status))
1259  {
1260  DPRINT1("IopNormalizeImagePath() failed (Status %x)\n", Status);
1262  return Status;
1263  }
1264 
1265  /* Free the service path */
1266  ExFreePool(ImagePath.Buffer);
1267 
1268  /*
1269  * Unload the module and release the references to the device object
1270  */
1271 
1272  /* Call the load/unload routine, depending on current process */
1273  if (DriverObject->DriverUnload && DriverObject->DriverSection &&
1274  (UnloadPnpDrivers || (DriverObject->Flags & DRVO_LEGACY_DRIVER)))
1275  {
1276  /* Loop through each device object of the driver
1277  and set DOE_UNLOAD_PENDING flag */
1278  DeviceObject = DriverObject->DeviceObject;
1279  while (DeviceObject)
1280  {
1281  /* Set the unload pending flag for the device */
1282  DeviceExtension = IoGetDevObjExtension(DeviceObject);
1283  DeviceExtension->ExtensionFlags |= DOE_UNLOAD_PENDING;
1284 
1285  /* Make sure there are no attached devices or no reference counts */
1286  if ((DeviceObject->ReferenceCount) || (DeviceObject->AttachedDevice))
1287  {
1288  /* Not safe to unload */
1289  DPRINT1("Drivers device object is referenced or has attached devices\n");
1290 
1291  SafeToUnload = FALSE;
1292  }
1293 
1294  DeviceObject = DeviceObject->NextDevice;
1295  }
1296 
1297  /* If not safe to unload, then return success */
1298  if (!SafeToUnload)
1299  {
1301  return STATUS_SUCCESS;
1302  }
1303 
1304  DPRINT1("Unloading driver '%wZ' (manual)\n", &DriverObject->DriverName);
1305 
1306  /* Set the unload invoked flag and call the unload routine */
1310 
1311  /* Mark the driver object temporary, so it could be deleted later */
1313 
1314  /* Dereference it 2 times */
1317 
1318  return Status;
1319  }
1320  else
1321  {
1322  DPRINT1("No DriverUnload function! '%wZ' will not be unloaded!\n", &DriverObject->DriverName);
1323 
1324  /* Dereference one time (refd inside this function) */
1326 
1327  /* Return unloading failure */
1329  }
1330 }
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:2223
#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
NTSTATUS IopDoLoadUnloadDriver(_In_opt_ PUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Process load and unload driver operations. This is mostly for NtLoadDriver and NtUnloadDriver,...
Definition: driver.c:1935
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:520
KPROCESSOR_MODE NTAPI ExGetPreviousMode(VOID)
Definition: sysinfo.c:3070
POBJECT_TYPE IoDriverObjectType
Definition: driver.c:33
#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)
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
#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:329
const LUID SeLoadDriverPrivilege
Definition: priv.c:27
void DPRINT(...)
Definition: polytest.cpp:61
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define DRVO_UNLOAD_INVOKED
Definition: iotypes.h:2222
Status
Definition: gdiplustypes.h:24
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 ASSERT(a)
Definition: mode.c:45
#define DOE_UNLOAD_PENDING
Definition: iotypes.h:149
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ObDereferenceObject
Definition: obfuncs.h:203
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static const WCHAR L[]
Definition: oid.c:1250
#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)
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#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
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
#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 1605 of file driver.c.

1608 {
1609  PDRIVER_REINIT_ITEM ReinitItem;
1610 
1611  /* Allocate the entry */
1612  ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1613  sizeof(DRIVER_REINIT_ITEM),
1614  TAG_REINIT);
1615  if (!ReinitItem) return;
1616 
1617  /* Fill it out */
1618  ReinitItem->DriverObject = DriverObject;
1619  ReinitItem->ReinitRoutine = ReinitRoutine;
1620  ReinitItem->Context = Context;
1621 
1622  /* Set the Driver Object flag and insert the entry into the list */
1625  &ReinitItem->ItemEntry,
1627 }
PVOID Context
Definition: io.h:459
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:456
KSPIN_LOCK DriverBootReinitListLock
Definition: driver.c:27
PDRIVER_OBJECT DriverObject
Definition: io.h:457
#define TAG_REINIT
Definition: tag.h:73
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
struct tagContext Context
Definition: acpixf.h:1034
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:458
#define DRVO_BOOTREINIT_REGISTERED
Definition: iotypes.h:4467

Referenced by _Function_class_(), and DriverEntry().

◆ IoRegisterDriverReinitialization()

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

Definition at line 1634 of file driver.c.

1637 {
1638  PDRIVER_REINIT_ITEM ReinitItem;
1639 
1640  /* Allocate the entry */
1641  ReinitItem = ExAllocatePoolWithTag(NonPagedPool,
1642  sizeof(DRIVER_REINIT_ITEM),
1643  TAG_REINIT);
1644  if (!ReinitItem) return;
1645 
1646  /* Fill it out */
1647  ReinitItem->DriverObject = DriverObject;
1648  ReinitItem->ReinitRoutine = ReinitRoutine;
1649  ReinitItem->Context = Context;
1650 
1651  /* Set the Driver Object flag and insert the entry into the list */
1654  &ReinitItem->ItemEntry,
1656 }
PVOID Context
Definition: io.h:459
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:4465
LIST_ENTRY ItemEntry
Definition: io.h:456
KSPIN_LOCK DriverReinitListLock
Definition: driver.c:22
LIST_ENTRY DriverReinitListHead
Definition: driver.c:21
PDRIVER_OBJECT DriverObject
Definition: io.h:457
#define TAG_REINIT
Definition: tag.h:73
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
struct tagContext Context
Definition: acpixf.h:1034
PDRIVER_REINITIALIZE ReinitRoutine
Definition: io.h:458

Referenced by DiskBootDriverReinit(), and DriverEntry().

◆ LdrProcessDriverModule()

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

Definition at line 667 of file driver.c.

670 {
672  UNICODE_STRING BaseName, BaseDirectory;
673  PLOAD_IMPORTS LoadedImports = (PVOID)-2;
674  PCHAR MissingApiName, Buffer;
675  PWCHAR MissingDriverName;
676  PVOID DriverBase = LdrEntry->DllBase;
677 
678  /* Allocate a buffer we'll use for names */
681  TAG_LDR_WSTR);
682  if (!Buffer)
683  {
684  /* Fail */
686  }
687 
688  /* Check for a separator */
689  if (FileName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
690  {
691  PWCHAR p;
692  ULONG BaseLength;
693 
694  /* Loop the path until we get to the base name */
695  p = &FileName->Buffer[FileName->Length / sizeof(WCHAR)];
696  while (*(p - 1) != OBJ_NAME_PATH_SEPARATOR) p--;
697 
698  /* Get the length */
699  BaseLength = (ULONG)(&FileName->Buffer[FileName->Length / sizeof(WCHAR)] - p);
700  BaseLength *= sizeof(WCHAR);
701 
702  /* Setup the string */
703  BaseName.Length = (USHORT)BaseLength;
704  BaseName.Buffer = p;
705  }
706  else
707  {
708  /* Otherwise, we already have a base name */
709  BaseName.Length = FileName->Length;
710  BaseName.Buffer = FileName->Buffer;
711  }
712 
713  /* Setup the maximum length */
714  BaseName.MaximumLength = BaseName.Length;
715 
716  /* Now compute the base directory */
717  BaseDirectory = *FileName;
718  BaseDirectory.Length -= BaseName.Length;
719  BaseDirectory.MaximumLength = BaseDirectory.Length;
720 
721  /* Resolve imports */
722  MissingApiName = Buffer;
723  Status = MiResolveImageReferences(DriverBase,
724  &BaseDirectory,
725  NULL,
726  &MissingApiName,
727  &MissingDriverName,
728  &LoadedImports);
729 
730  /* Free the temporary buffer */
732 
733  /* Check the result of the imports resolution */
734  if (!NT_SUCCESS(Status)) return Status;
735 
736  /* Return */
737  *ModuleObject = LdrEntry;
738  return STATUS_SUCCESS;
739 }
signed char * PCHAR
Definition: retypes.h:7
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
USHORT MaximumLength
Definition: env_spec_w32.h:370
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
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
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
Status
Definition: gdiplustypes.h:24
__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
struct _FileName FileName
Definition: fatprocs.h:893
unsigned short USHORT
Definition: pedump.c:61
#define NULL
Definition: types.h:112
unsigned int ULONG
Definition: retypes.h:1
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
GLfloat GLfloat p
Definition: glext.h:8902

◆ 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
#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
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
union _IMAGE_THUNK_DATA32::@2123 u1
#define PCHAR
Definition: match.c:90
Status
Definition: gdiplustypes.h:24
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define ASSERT(a)
Definition: mode.c:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)
#define _strnicmp(_String1, _String2, _MaxCount)
Definition: compat.h:23
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
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
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
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 NULL
Definition: types.h:112
#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 STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#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().

◆ NtLoadDriver()

NTSTATUS NTAPI NtLoadDriver ( IN PUNICODE_STRING  DriverServiceName)

Definition at line 1983 of file driver.c.

1984 {
1985  UNICODE_STRING CapturedServiceName = { 0, 0, NULL };
1988  NTSTATUS Status;
1989 
1990  PAGED_CODE();
1991 
1993 
1994  /* Need the appropriate priviliege */
1996  {
1997  DPRINT1("No load privilege!\n");
1999  }
2000 
2001  /* Capture the service name */
2002  Status = ProbeAndCaptureUnicodeString(&CapturedServiceName,
2003  PreviousMode,
2004  DriverServiceName);
2005  if (!NT_SUCCESS(Status))
2006  {
2007  return Status;
2008  }
2009 
2010  DPRINT("NtLoadDriver('%wZ')\n", &CapturedServiceName);
2011 
2012  /* We need a service name */
2013  if (CapturedServiceName.Length == 0 || CapturedServiceName.Buffer == NULL)
2014  {
2015  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2016  return STATUS_INVALID_PARAMETER;
2017  }
2018 
2019  /* Load driver and call its entry point */
2020  DriverObject = NULL;
2021  Status = IopDoLoadUnloadDriver(&CapturedServiceName, &DriverObject);
2022 
2023  ReleaseCapturedUnicodeString(&CapturedServiceName, PreviousMode);
2024  return Status;
2025 }
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 STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define KeGetPreviousMode()
Definition: ketypes.h:1107
NTSTATUS IopDoLoadUnloadDriver(_In_opt_ PUNICODE_STRING RegistryPath, _Inout_ PDRIVER_OBJECT *DriverObject)
Process load and unload driver operations. This is mostly for NtLoadDriver and NtUnloadDriver,...
Definition: driver.c:1935
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI SeSinglePrivilegeCheck(IN LUID PrivilegeValue, IN KPROCESSOR_MODE PreviousMode)
Definition: priv.c:520
const LUID SeLoadDriverPrivilege
Definition: priv.c:27
void DPRINT(...)
Definition: polytest.cpp:61
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
Status
Definition: gdiplustypes.h:24
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
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
#define PAGED_CODE()

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

◆ NtUnloadDriver()

NTSTATUS NTAPI NtUnloadDriver ( IN PUNICODE_STRING  DriverServiceName)

Definition at line 2044 of file driver.c.

2045 {
2046  return IopUnloadDriver(DriverServiceName, FALSE);
2047 }
#define FALSE
Definition: types.h:117
NTSTATUS NTAPI IopUnloadDriver(PUNICODE_STRING DriverServiceName, BOOLEAN UnloadPnpDrivers)
Definition: driver.c:1107

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

BOOLEAN ExpInTextModeSetup

◆ IoDriverObjectType

◆ IopDriverLoadResource

ERESOURCE IopDriverLoadResource

Definition at line 19 of file driver.c.

Referenced by IoInitSystem(), and IopLoadDriver().

◆ IopGroupIndex

USHORT IopGroupIndex

Definition at line 42 of file driver.c.

Referenced by IopInitializeBootDrivers().

◆ IopGroupTable

PLIST_ENTRY IopGroupTable

Definition at line 43 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 IopInitializeDriverModule().

◆ PiEnumerationFinished

KEVENT PiEnumerationFinished

Definition at line 50 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().

◆ ServicesKeyName

const WCHAR ServicesKeyName[] = L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"
static

Definition at line 31 of file driver.c.

Referenced by IopInitializeBuiltinDriver(), and MmCallDllInitialize().