2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS text-mode setup
4 * FILE: subsys/system/usetup/interface/devinst.c
5 * PURPOSE: Device installation
6 * PROGRAMMER: Hervé Poussineau (hpoussin@reactos.org)
18 PLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
;
21 RtlInitUnicodeString(&ResetDeviceData
.DeviceInstance
, DeviceId
);
22 Status
= NtPlugPlayControl(PlugPlayControlResetDevice
, &ResetDeviceData
, sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
));
23 if (!NT_SUCCESS(Status
))
25 DPRINT1("NtPlugPlayControl() failed with status 0x%08x\n", Status
);
37 IN LPCWSTR HardwareId
)
39 UNICODE_STRING PathPrefix
= RTL_CONSTANT_STRING(L
"System32\\DRIVERS\\");
40 UNICODE_STRING ServiceU
= RTL_CONSTANT_STRING(L
"Service");
41 UNICODE_STRING ErrorControlU
= RTL_CONSTANT_STRING(L
"ErrorControl");
42 UNICODE_STRING ImagePathU
= RTL_CONSTANT_STRING(L
"ImagePath");
43 UNICODE_STRING StartU
= RTL_CONSTANT_STRING(L
"Start");
44 UNICODE_STRING TypeU
= RTL_CONSTANT_STRING(L
"Type");
45 UNICODE_STRING StringU
;
46 OBJECT_ATTRIBUTES ObjectAttributes
;
49 LPWSTR Driver
, ImagePath
, FullImagePath
;
53 BOOLEAN deviceInstalled
= FALSE
;
55 /* Check if we know the hardware */
56 if (!SetupFindFirstLineW(hInf
, L
"HardwareIdsDatabase", HardwareId
, &Context
))
58 if (!INF_GetDataField(&Context
, 1, &Driver
))
61 /* Find associated driver name */
62 /* FIXME: check in other sections too! */
63 if (!SetupFindFirstLineW(hInf
, L
"BootBusExtenders.Load", Driver
, &Context
)
64 && !SetupFindFirstLineW(hInf
, L
"BusExtenders.Load", Driver
, &Context
)
65 && !SetupFindFirstLineW(hInf
, L
"SCSI.Load", Driver
, &Context
)
66 && !SetupFindFirstLineW(hInf
, L
"Keyboard.Load", Driver
, &Context
))
68 if (!INF_GetDataField(&Context
, 1, &ImagePath
))
71 /* Prepare full driver path */
72 dwValue
= PathPrefix
.MaximumLength
+ wcslen(ImagePath
) * sizeof(WCHAR
);
73 FullImagePath
= (LPWSTR
)RtlAllocateHeap(ProcessHeap
, 0, dwValue
);
76 DPRINT1("RtlAllocateHeap() failed\n");
79 RtlCopyMemory(FullImagePath
, PathPrefix
.Buffer
, PathPrefix
.MaximumLength
);
80 wcscat(FullImagePath
, ImagePath
);
82 DPRINT1("Using driver '%S' for device '%S'\n", ImagePath
, DeviceId
);
84 /* Create service key */
85 RtlInitUnicodeString(&StringU
, Driver
);
86 InitializeObjectAttributes(&ObjectAttributes
, &StringU
, 0, hServices
, NULL
);
87 Status
= NtCreateKey(&hService
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, 0, &Disposition
);
88 if (!NT_SUCCESS(Status
))
90 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08x\n", &StringU
, Status
);
91 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
95 /* Fill service key */
96 if (Disposition
== REG_CREATED_NEW_KEY
)
114 dwValue
= SERVICE_KERNEL_DRIVER
;
123 /* HACK: don't put any path in registry */
130 (wcslen(ImagePath
) + 1) * sizeof(WCHAR
));
132 /* Associate device with the service we just filled */
133 Status
= NtSetValueKey(
139 (wcslen(Driver
) + 1) * sizeof(WCHAR
));
140 if (NT_SUCCESS(Status
))
142 /* Restart the device, so it will use the driver we registred */
143 deviceInstalled
= ResetDevice(DeviceId
);
146 /* HACK: Update driver path */
153 (wcslen(FullImagePath
) + 1) * sizeof(WCHAR
));
154 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
157 return deviceInstalled
;
167 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
168 UNICODE_STRING CompatibleIDsU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
169 UNICODE_STRING DeviceIdU
;
170 OBJECT_ATTRIBUTES ObjectAttributes
;
172 PKEY_VALUE_PARTIAL_INFORMATION pPartialInformation
= NULL
;
175 BOOLEAN bDriverInstalled
= FALSE
;
178 RtlInitUnicodeString(&DeviceIdU
, DeviceId
);
179 InitializeObjectAttributes(&ObjectAttributes
, &DeviceIdU
, 0, hEnum
, NULL
);
180 Status
= NtOpenKey(&hDeviceKey
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, &ObjectAttributes
);
181 if (!NT_SUCCESS(Status
))
183 DPRINT("Unable to open subkey '%S'\n", DeviceId
);
187 Status
= NtQueryValueKey(
190 KeyValuePartialInformation
,
194 if (Status
== STATUS_BUFFER_TOO_SMALL
)
196 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
197 if (!pPartialInformation
)
199 DPRINT1("RtlAllocateHeap() failed\n");
203 Status
= NtQueryValueKey(
206 KeyValuePartialInformation
,
211 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
215 else if (!NT_SUCCESS(Status
))
217 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
218 if (pPartialInformation
)
219 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
223 else if (pPartialInformation
)
225 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
226 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
228 && !bDriverInstalled
;
229 HardwareID
+= wcslen(HardwareID
) + 1)
231 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
235 if (!bDriverInstalled
)
237 if (pPartialInformation
)
239 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
240 pPartialInformation
= NULL
;
242 Status
= NtQueryValueKey(
245 KeyValuePartialInformation
,
249 if (Status
== STATUS_BUFFER_TOO_SMALL
)
251 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
252 if (!pPartialInformation
)
254 DPRINT("RtlAllocateHeap() failed\n");
258 Status
= NtQueryValueKey(
261 KeyValuePartialInformation
,
266 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
270 else if (!NT_SUCCESS(Status
))
272 if (pPartialInformation
)
273 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
275 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
278 else if (pPartialInformation
)
280 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
281 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
283 && !bDriverInstalled
;
284 HardwareID
+= wcslen(HardwareID
) + 1)
286 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
290 if (!bDriverInstalled
)
291 DPRINT("No driver available for %S\n", DeviceId
);
293 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
298 EventThread(IN LPVOID lpParameter
)
300 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
301 UNICODE_STRING ServicesU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
302 PPLUGPLAY_EVENT_BLOCK PnpEvent
;
303 OBJECT_ATTRIBUTES ObjectAttributes
;
306 HANDLE hEnum
, hServices
;
309 hInf
= *(HINF
*)lpParameter
;
311 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
312 Status
= NtOpenKey(&hEnum
, KEY_QUERY_VALUE
, &ObjectAttributes
);
313 if (!NT_SUCCESS(Status
))
315 DPRINT1("NtOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
319 InitializeObjectAttributes(&ObjectAttributes
, &ServicesU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
320 Status
= NtCreateKey(&hServices
, 0, &ObjectAttributes
, 0, NULL
, 0, NULL
);
321 if (!NT_SUCCESS(Status
))
323 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08lx\n", &ServicesU
, Status
);
328 PnpEventSize
= 0x1000;
329 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
330 if (PnpEvent
== NULL
)
334 return STATUS_NO_MEMORY
;
339 DPRINT("Calling NtGetPlugPlayEvent()\n");
341 /* Wait for the next pnp event */
342 Status
= NtGetPlugPlayEvent(0, 0, PnpEvent
, PnpEventSize
);
344 /* Resize the buffer for the PnP event if it's too small. */
345 if (Status
== STATUS_BUFFER_TOO_SMALL
)
347 PnpEventSize
+= 0x400;
348 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
349 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
350 if (PnpEvent
== NULL
)
354 return STATUS_NO_MEMORY
;
359 if (!NT_SUCCESS(Status
))
361 DPRINT("NtPlugPlayEvent() failed (Status %lx)\n", Status
);
365 /* Process the pnp event */
366 DPRINT("Received PnP Event\n");
367 if (IsEqualIID(&PnpEvent
->EventGuid
, (REFGUID
)&GUID_DEVICE_ENUMERATED
))
369 DPRINT("Device arrival event: %S\n", PnpEvent
->TargetDevice
.DeviceIds
);
370 InstallDevice(hInf
, hEnum
, hServices
, PnpEvent
->TargetDevice
.DeviceIds
);
374 DPRINT("Unknown event\n");
377 /* Dequeue the current pnp event and signal the next one */
378 NtPlugPlayControl(PlugPlayControlUserResponse
, NULL
, 0);
381 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
385 return STATUS_SUCCESS
;
389 PnpEventThread(IN LPVOID lpParameter
)
392 Status
= EventThread(lpParameter
);
393 NtTerminateThread(NtCurrentThread(), Status
);