下面的函数主要初始化PnP发现的设备,并为它安装服务,代码如下:
#001 NTSTATUS
#002 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode)
#003 {
#004 DEVICETREE_TRAVERSE_CONTEXT Context;
#005
#006 DPRINT("IopInitializePnpServices(%p)/n",DeviceNode);
#007
构造一个响应子服务发现的操作。
#008 IopInitDeviceTreeTraverseContext(
#009 &Context,
#010 DeviceNode,
#011 IopActionInitChildServices,
#012 DeviceNode);
#013
开始遍历整个设备树。
#014 return IopTraverseDeviceTree(&Context);
#015 }
#016
初始化每个设备节点,如下:
#001 NTSTATUS
#002 IopActionInitChildServices(PDEVICE_NODE DeviceNode,
#003 PVOID Context)
#004 {
#005 PDEVICE_NODE ParentDeviceNode;
#006 NTSTATUS Status;
#007 BOOLEAN BootDrivers = !PnpSystemInit;
#008
#009 DPRINT("IopActionInitChildServices(%p,%p)/n",DeviceNode,Context);
#010
取父节点设备。
#011 ParentDeviceNode = (PDEVICE_NODE)Context;
#012
#013 /*
#014 * We are called for the parent too,but we don't need to do special
#015 * handling for this node
#016 */
如果下一个节点等于父节点,没有子节点,就直接返回。
#017 if (DeviceNode == ParentDeviceNode)
#018 {
#019 DPRINT("Success/n");
#020 return STATUS_SUCCESS;
#021 }
#022
#023 /*
#024 * Make sure this device node is a direct child of the parent device node
#025 * that is given as an argument
#026 */
#027 #if 0
#028 if (DeviceNode->Parent != ParentDeviceNode)
#029 {
#030 /*
#031 * Stop the traversal immediately and indicate unsuccessful operation
#032 */
#033 DPRINT("Stop/n");
#034 return STATUS_UNSUCCESSFUL;
#035 }
#036 #endif
#037
判断这个节点是否可以安装服务。
#038 if (!IopDeviceNodeHasFlag(DeviceNode,DNF_DISABLED) &&
#039 !IopDeviceNodeHasFlag(DeviceNode,DNF_ADDED) &&
#040 !IopDeviceNodeHasFlag(DeviceNode,DNF_STARTED))
#041 {
#042 PLDR_DATA_TABLE_ENTRY ModuleObject;
#043 PDRIVER_OBJECT DriverObject;
#044
获取已经存在驱动程序指针。
#045 /* Get existing DriverObject pointer (in case the driver has
#046 already been loaded and initialized) */
#047 Status = IopGetDriverObject(
#048 &DriverObject,
#049 &DeviceNode->ServiceName,
#050 FALSE);
#051
#052 if (!NT_SUCCESS(Status))
#053 {
#054 /* Driver is not initialized,try to load it */
#055 Status = IopLoadServiceModule(&DeviceNode->ServiceName,&ModuleObject);
#056
#057 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
#058 {
#059 /* STATUS_IMAGE_ALREADY_LOADED means this driver
#060 was loaded by the bootloader */
#061 if ((Status != STATUS_IMAGE_ALREADY_LOADED) ||
#062 (Status == STATUS_IMAGE_ALREADY_LOADED && !DriverObject))
#063 {
初始化这个设备的驱动程序。
#064 /* Initialize the driver */
#065 Status = IopInitializeDriverModule(DeviceNode,ModuleObject,
#066 &DeviceNode->ServiceName,FALSE,&DriverObject);
#067 }
#068 else
#069 {
#070 Status = STATUS_SUCCESS;
#071 }
#072 }
#073 else
#074 {
#075 DPRINT1("IopLoadServiceModule(%wZ) Failed with status 0x%08x/n",
#076 &DeviceNode->ServiceName,Status);
#077 }
#078 }
#079
设备驱动程序已经加载,开始初始化入口点。
#080 /* Driver is loaded and initialized at this point */
#081 if (NT_SUCCESS(Status))
#082 {
添加低层次的过滤驱动程序。
#083 /* Attach lower level filter drivers. */
#084 IopAttachFilterDrivers(DeviceNode,TRUE);
初始化设备。
#085 /* Initialize the function driver for the device node */
#086 Status = IopInitializeDevice(DeviceNode,DriverObject);
#087
#088 if (NT_SUCCESS(Status))
#089 {
#090 /* Attach upper level filter drivers. */
#091 IopAttachFilterDrivers(DeviceNode,FALSE);
#092 IopDeviceNodeSetFlag(DeviceNode,DNF_STARTED);
#093
启动这个设备。
#094 Status = IopStartDevice(DeviceNode);
#095 }
#096 else
#097 {
#098 DPRINT1("IopInitializeDevice(%wZ) Failed with status 0x%08x/n",
#099 &DeviceNode->InstancePath,Status);
#100 }
#101 }
#102 else
#103 {
设置引导驱动程序不可以再加载。
#104 /*
#105 * Don't disable when trying to load only boot drivers
#106 */
#107 if (!BootDrivers)
#108 {
#109 IopDeviceNodeSetFlag(DeviceNode,DNF_DISABLED);
#110 IopDeviceNodeSetFlag(DeviceNode,DNF_START_Failed);
#111 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
#112 DPRINT1("Initialization of service %S Failed (Status %x)/n",
#113 DeviceNode->ServiceName.Buffer,Status);
#114 }
#115 }
#116 }
#117 else
#118 {
#119 DPRINT("Device %wZ is disabled or already initialized/n",
#120 &DeviceNode->InstancePath);
#121 }
#122
#123 return STATUS_SUCCESS;
#124}
原文链接:https://www.f2er.com/react/308450.html