2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS text-mode setup
4 * FILE: base/setup/usetup/devinst.c
5 * PURPOSE: Device installation
6 * PROGRAMMER: Hervé Poussineau (hpoussin@reactos.org)
16 #include <libs/umpnpmgr/sysguid.h>
22 PLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
;
25 RtlInitUnicodeString(&ResetDeviceData
.DeviceInstance
, DeviceId
);
26 Status
= NtPlugPlayControl(PlugPlayControlResetDevice
, &ResetDeviceData
, sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
));
27 if (!NT_SUCCESS(Status
))
29 DPRINT1("NtPlugPlayControl() failed with status 0x%08x\n", Status
);
41 IN LPCWSTR HardwareId
)
43 UNICODE_STRING PathPrefix
= RTL_CONSTANT_STRING(L
"System32\\DRIVERS\\");
44 UNICODE_STRING ServiceU
= RTL_CONSTANT_STRING(L
"Service");
45 UNICODE_STRING ErrorControlU
= RTL_CONSTANT_STRING(L
"ErrorControl");
46 UNICODE_STRING ImagePathU
= RTL_CONSTANT_STRING(L
"ImagePath");
47 UNICODE_STRING StartU
= RTL_CONSTANT_STRING(L
"Start");
48 UNICODE_STRING TypeU
= RTL_CONSTANT_STRING(L
"Type");
49 UNICODE_STRING UpperFiltersU
= RTL_CONSTANT_STRING(L
"UpperFilters");
50 PWSTR keyboardClass
= L
"kbdclass\0";
52 UNICODE_STRING StringU
;
53 OBJECT_ATTRIBUTES ObjectAttributes
;
56 PWSTR Driver
, ClassGuid
, ImagePath
, FullImagePath
;
60 BOOLEAN deviceInstalled
= FALSE
;
62 /* Check if we know the hardware */
63 if (!SetupFindFirstLineW(hInf
, L
"HardwareIdsDatabase", HardwareId
, &Context
))
65 if (!INF_GetDataField(&Context
, 1, &Driver
))
68 /* Get associated class GUID (if any) */
69 if (!INF_GetDataField(&Context
, 2, &ClassGuid
))
72 /* Find associated driver name */
73 /* FIXME: check in other sections too! */
74 if (!SetupFindFirstLineW(hInf
, L
"BootBusExtenders.Load", Driver
, &Context
)
75 && !SetupFindFirstLineW(hInf
, L
"BusExtenders.Load", Driver
, &Context
)
76 && !SetupFindFirstLineW(hInf
, L
"SCSI.Load", Driver
, &Context
)
77 && !SetupFindFirstLineW(hInf
, L
"InputDevicesSupport.Load", Driver
, &Context
)
78 && !SetupFindFirstLineW(hInf
, L
"Keyboard.Load", Driver
, &Context
))
80 INF_FreeData(ClassGuid
);
85 if (!INF_GetDataField(&Context
, 1, &ImagePath
))
87 INF_FreeData(ClassGuid
);
92 /* Prepare full driver path */
93 dwValue
= PathPrefix
.MaximumLength
+ wcslen(ImagePath
) * sizeof(WCHAR
);
94 FullImagePath
= (PWSTR
)RtlAllocateHeap(ProcessHeap
, 0, dwValue
);
97 DPRINT1("RtlAllocateHeap() failed\n");
98 INF_FreeData(ImagePath
);
99 INF_FreeData(ClassGuid
);
100 INF_FreeData(Driver
);
103 RtlCopyMemory(FullImagePath
, PathPrefix
.Buffer
, PathPrefix
.MaximumLength
);
104 ConcatPaths(FullImagePath
, dwValue
/ sizeof(WCHAR
), 1, ImagePath
);
106 DPRINT1("Using driver '%S' for device '%S'\n", ImagePath
, DeviceId
);
108 /* Create service key */
109 RtlInitUnicodeString(&StringU
, Driver
);
110 InitializeObjectAttributes(&ObjectAttributes
, &StringU
, 0, hServices
, NULL
);
111 Status
= NtCreateKey(&hService
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, REG_OPTION_NON_VOLATILE
, &Disposition
);
112 if (!NT_SUCCESS(Status
))
114 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08x\n", &StringU
, Status
);
115 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
116 INF_FreeData(ImagePath
);
117 INF_FreeData(ClassGuid
);
118 INF_FreeData(Driver
);
122 /* Fill service key */
123 if (Disposition
== REG_CREATED_NEW_KEY
)
126 NtSetValueKey(hService
,
134 NtSetValueKey(hService
,
141 dwValue
= SERVICE_KERNEL_DRIVER
;
142 NtSetValueKey(hService
,
149 /* HACK: don't put any path in registry */
150 NtSetValueKey(hService
,
155 (wcslen(ImagePath
) + 1) * sizeof(WCHAR
));
157 INF_FreeData(ImagePath
);
159 if (ClassGuid
&&_wcsicmp(ClassGuid
, L
"{4D36E96B-E325-11CE-BFC1-08002BE10318}") == 0)
161 DPRINT1("Installing keyboard class driver for '%S'\n", DeviceId
);
162 NtSetValueKey(hDeviceKey
,
167 (wcslen(keyboardClass
) + 2) * sizeof(WCHAR
));
170 INF_FreeData(ClassGuid
);
172 /* Associate device with the service we just filled */
173 Status
= NtSetValueKey(hDeviceKey
,
178 (wcslen(Driver
) + 1) * sizeof(WCHAR
));
179 if (NT_SUCCESS(Status
))
181 /* Restart the device, so it will use the driver we registered */
182 deviceInstalled
= ResetDevice(DeviceId
);
185 INF_FreeData(Driver
);
187 /* HACK: Update driver path */
188 NtSetValueKey(hService
,
193 (wcslen(FullImagePath
) + 1) * sizeof(WCHAR
));
194 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
198 return deviceInstalled
;
208 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
209 UNICODE_STRING CompatibleIDsU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
211 UNICODE_STRING DeviceIdU
;
212 OBJECT_ATTRIBUTES ObjectAttributes
;
214 PKEY_VALUE_PARTIAL_INFORMATION pPartialInformation
= NULL
;
217 BOOLEAN bDriverInstalled
= FALSE
;
220 RtlInitUnicodeString(&DeviceIdU
, DeviceId
);
221 InitializeObjectAttributes(&ObjectAttributes
, &DeviceIdU
, 0, hEnum
, NULL
);
222 Status
= NtOpenKey(&hDeviceKey
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, &ObjectAttributes
);
223 if (!NT_SUCCESS(Status
))
225 DPRINT("Unable to open subkey '%S'\n", DeviceId
);
229 Status
= NtQueryValueKey(
232 KeyValuePartialInformation
,
236 if (Status
== STATUS_BUFFER_TOO_SMALL
)
238 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
239 if (!pPartialInformation
)
241 DPRINT1("RtlAllocateHeap() failed\n");
245 Status
= NtQueryValueKey(
248 KeyValuePartialInformation
,
253 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
257 else if (!NT_SUCCESS(Status
))
259 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
260 if (pPartialInformation
)
261 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
265 else if (pPartialInformation
)
267 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
268 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
270 && !bDriverInstalled
;
271 HardwareID
+= wcslen(HardwareID
) + 1)
273 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
277 if (!bDriverInstalled
)
279 if (pPartialInformation
)
281 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
282 pPartialInformation
= NULL
;
284 Status
= NtQueryValueKey(
287 KeyValuePartialInformation
,
291 if (Status
== STATUS_BUFFER_TOO_SMALL
)
293 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
294 if (!pPartialInformation
)
296 DPRINT("RtlAllocateHeap() failed\n");
300 Status
= NtQueryValueKey(
303 KeyValuePartialInformation
,
308 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
312 else if (!NT_SUCCESS(Status
))
314 if (pPartialInformation
)
315 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
317 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
320 else if (pPartialInformation
)
322 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
323 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
325 && !bDriverInstalled
;
326 HardwareID
+= wcslen(HardwareID
) + 1)
328 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
332 if (!bDriverInstalled
)
333 DPRINT("No driver available for %S\n", DeviceId
);
335 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
340 EventThread(IN LPVOID lpParameter
)
342 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
343 UNICODE_STRING ServicesU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
345 PPLUGPLAY_EVENT_BLOCK PnpEvent
, NewPnpEvent
;
346 OBJECT_ATTRIBUTES ObjectAttributes
;
349 HANDLE hEnum
, hServices
;
352 hInf
= *(HINF
*)lpParameter
;
354 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
355 Status
= NtOpenKey(&hEnum
, KEY_QUERY_VALUE
, &ObjectAttributes
);
356 if (!NT_SUCCESS(Status
))
358 DPRINT1("NtOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
362 InitializeObjectAttributes(&ObjectAttributes
, &ServicesU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
363 Status
= NtCreateKey(&hServices
, KEY_ALL_ACCESS
, &ObjectAttributes
, 0, NULL
, REG_OPTION_NON_VOLATILE
, NULL
);
364 if (!NT_SUCCESS(Status
))
366 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08lx\n", &ServicesU
, Status
);
371 PnpEventSize
= 0x1000;
372 PnpEvent
= RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
373 if (PnpEvent
== NULL
)
375 Status
= STATUS_NO_MEMORY
;
381 DPRINT("Calling NtGetPlugPlayEvent()\n");
383 /* Wait for the next PnP event */
384 Status
= NtGetPlugPlayEvent(0, 0, PnpEvent
, PnpEventSize
);
386 /* Resize the buffer for the PnP event if it's too small */
387 if (Status
== STATUS_BUFFER_TOO_SMALL
)
389 PnpEventSize
+= 0x400;
390 NewPnpEvent
= RtlReAllocateHeap(ProcessHeap
, 0, PnpEvent
, PnpEventSize
);
391 if (NewPnpEvent
== NULL
)
393 Status
= STATUS_NO_MEMORY
;
396 PnpEvent
= NewPnpEvent
;
400 if (!NT_SUCCESS(Status
))
402 DPRINT("NtPlugPlayEvent() failed (Status %lx)\n", Status
);
406 /* Process the PnP event */
407 DPRINT("Received PnP Event\n");
408 if (IsEqualIID(&PnpEvent
->EventGuid
, (REFGUID
)&GUID_DEVICE_ENUMERATED
))
410 DPRINT("Device arrival event: %S\n", PnpEvent
->TargetDevice
.DeviceIds
);
411 InstallDevice(hInf
, hEnum
, hServices
, PnpEvent
->TargetDevice
.DeviceIds
);
415 DPRINT("Unknown event\n");
418 /* Dequeue the current PnP event and signal the next one */
419 NtPlugPlayControl(PlugPlayControlUserResponse
, NULL
, 0);
422 Status
= STATUS_SUCCESS
;
426 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
435 PnpEventThread(IN LPVOID lpParameter
)
438 Status
= EventThread(lpParameter
);
439 NtTerminateThread(NtCurrentThread(), Status
);