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 StringU
;
50 OBJECT_ATTRIBUTES ObjectAttributes
;
53 PWSTR Driver
, ClassGuid
, ImagePath
, FullImagePath
;
57 BOOLEAN deviceInstalled
= FALSE
;
58 UNICODE_STRING UpperFiltersU
= RTL_CONSTANT_STRING(L
"UpperFilters");
59 PWSTR keyboardClass
= L
"kbdclass\0";
61 /* Check if we know the hardware */
62 if (!SetupFindFirstLineW(hInf
, L
"HardwareIdsDatabase", HardwareId
, &Context
))
64 if (!INF_GetDataField(&Context
, 1, &Driver
))
67 /* Get associated class GUID (if any) */
68 if (!INF_GetDataField(&Context
, 2, &ClassGuid
))
71 /* Find associated driver name */
72 /* FIXME: check in other sections too! */
73 if (!SetupFindFirstLineW(hInf
, L
"BootBusExtenders.Load", Driver
, &Context
)
74 && !SetupFindFirstLineW(hInf
, L
"BusExtenders.Load", Driver
, &Context
)
75 && !SetupFindFirstLineW(hInf
, L
"SCSI.Load", Driver
, &Context
)
76 && !SetupFindFirstLineW(hInf
, L
"InputDevicesSupport.Load", Driver
, &Context
)
77 && !SetupFindFirstLineW(hInf
, L
"Keyboard.Load", Driver
, &Context
))
82 if (!INF_GetDataField(&Context
, 1, &ImagePath
))
85 /* Prepare full driver path */
86 dwValue
= PathPrefix
.MaximumLength
+ wcslen(ImagePath
) * sizeof(WCHAR
);
87 FullImagePath
= (PWSTR
)RtlAllocateHeap(ProcessHeap
, 0, dwValue
);
90 DPRINT1("RtlAllocateHeap() failed\n");
93 RtlCopyMemory(FullImagePath
, PathPrefix
.Buffer
, PathPrefix
.MaximumLength
);
94 ConcatPaths(FullImagePath
, dwValue
/ sizeof(WCHAR
), 1, ImagePath
);
96 DPRINT1("Using driver '%S' for device '%S'\n", ImagePath
, DeviceId
);
98 /* Create service key */
99 RtlInitUnicodeString(&StringU
, Driver
);
100 InitializeObjectAttributes(&ObjectAttributes
, &StringU
, 0, hServices
, NULL
);
101 Status
= NtCreateKey(&hService
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, 0, &Disposition
);
102 if (!NT_SUCCESS(Status
))
104 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08x\n", &StringU
, Status
);
105 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
109 /* Fill service key */
110 if (Disposition
== REG_CREATED_NEW_KEY
)
128 dwValue
= SERVICE_KERNEL_DRIVER
;
137 /* HACK: don't put any path in registry */
144 (wcslen(ImagePath
) + 1) * sizeof(WCHAR
));
146 if (ClassGuid
&&_wcsicmp(ClassGuid
, L
"{4D36E96B-E325-11CE-BFC1-08002BE10318}") == 0)
148 DPRINT1("Installing keyboard class driver for '%S'\n", DeviceId
);
149 NtSetValueKey(hDeviceKey
,
154 (wcslen(keyboardClass
) + 2) * sizeof(WCHAR
));
157 /* Associate device with the service we just filled */
158 Status
= NtSetValueKey(
164 (wcslen(Driver
) + 1) * sizeof(WCHAR
));
165 if (NT_SUCCESS(Status
))
167 /* Restart the device, so it will use the driver we registered */
168 deviceInstalled
= ResetDevice(DeviceId
);
171 /* HACK: Update driver path */
178 (wcslen(FullImagePath
) + 1) * sizeof(WCHAR
));
179 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
182 return deviceInstalled
;
192 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
193 UNICODE_STRING CompatibleIDsU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
194 UNICODE_STRING DeviceIdU
;
195 OBJECT_ATTRIBUTES ObjectAttributes
;
197 PKEY_VALUE_PARTIAL_INFORMATION pPartialInformation
= NULL
;
200 BOOLEAN bDriverInstalled
= FALSE
;
203 RtlInitUnicodeString(&DeviceIdU
, DeviceId
);
204 InitializeObjectAttributes(&ObjectAttributes
, &DeviceIdU
, 0, hEnum
, NULL
);
205 Status
= NtOpenKey(&hDeviceKey
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, &ObjectAttributes
);
206 if (!NT_SUCCESS(Status
))
208 DPRINT("Unable to open subkey '%S'\n", DeviceId
);
212 Status
= NtQueryValueKey(
215 KeyValuePartialInformation
,
219 if (Status
== STATUS_BUFFER_TOO_SMALL
)
221 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
222 if (!pPartialInformation
)
224 DPRINT1("RtlAllocateHeap() failed\n");
228 Status
= NtQueryValueKey(
231 KeyValuePartialInformation
,
236 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
240 else if (!NT_SUCCESS(Status
))
242 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
243 if (pPartialInformation
)
244 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
248 else if (pPartialInformation
)
250 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
251 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
253 && !bDriverInstalled
;
254 HardwareID
+= wcslen(HardwareID
) + 1)
256 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
260 if (!bDriverInstalled
)
262 if (pPartialInformation
)
264 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
265 pPartialInformation
= NULL
;
267 Status
= NtQueryValueKey(
270 KeyValuePartialInformation
,
274 if (Status
== STATUS_BUFFER_TOO_SMALL
)
276 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
277 if (!pPartialInformation
)
279 DPRINT("RtlAllocateHeap() failed\n");
283 Status
= NtQueryValueKey(
286 KeyValuePartialInformation
,
291 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
295 else if (!NT_SUCCESS(Status
))
297 if (pPartialInformation
)
298 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
300 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
303 else if (pPartialInformation
)
305 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
306 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
308 && !bDriverInstalled
;
309 HardwareID
+= wcslen(HardwareID
) + 1)
311 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
315 if (!bDriverInstalled
)
316 DPRINT("No driver available for %S\n", DeviceId
);
318 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
323 EventThread(IN LPVOID lpParameter
)
325 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
326 UNICODE_STRING ServicesU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
327 PPLUGPLAY_EVENT_BLOCK PnpEvent
;
328 OBJECT_ATTRIBUTES ObjectAttributes
;
331 HANDLE hEnum
, hServices
;
334 hInf
= *(HINF
*)lpParameter
;
336 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
337 Status
= NtOpenKey(&hEnum
, KEY_QUERY_VALUE
, &ObjectAttributes
);
338 if (!NT_SUCCESS(Status
))
340 DPRINT1("NtOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
344 InitializeObjectAttributes(&ObjectAttributes
, &ServicesU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
345 Status
= NtCreateKey(&hServices
, KEY_ALL_ACCESS
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
346 if (!NT_SUCCESS(Status
))
348 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08lx\n", &ServicesU
, Status
);
353 PnpEventSize
= 0x1000;
354 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
355 if (PnpEvent
== NULL
)
359 return STATUS_NO_MEMORY
;
364 DPRINT("Calling NtGetPlugPlayEvent()\n");
366 /* Wait for the next pnp event */
367 Status
= NtGetPlugPlayEvent(0, 0, PnpEvent
, PnpEventSize
);
369 /* Resize the buffer for the PnP event if it's too small. */
370 if (Status
== STATUS_BUFFER_TOO_SMALL
)
372 PnpEventSize
+= 0x400;
373 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
374 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
375 if (PnpEvent
== NULL
)
379 return STATUS_NO_MEMORY
;
384 if (!NT_SUCCESS(Status
))
386 DPRINT("NtPlugPlayEvent() failed (Status %lx)\n", Status
);
390 /* Process the pnp event */
391 DPRINT("Received PnP Event\n");
392 if (IsEqualIID(&PnpEvent
->EventGuid
, (REFGUID
)&GUID_DEVICE_ENUMERATED
))
394 DPRINT("Device arrival event: %S\n", PnpEvent
->TargetDevice
.DeviceIds
);
395 InstallDevice(hInf
, hEnum
, hServices
, PnpEvent
->TargetDevice
.DeviceIds
);
399 DPRINT("Unknown event\n");
402 /* Dequeue the current pnp event and signal the next one */
403 NtPlugPlayControl(PlugPlayControlUserResponse
, NULL
, 0);
406 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
410 return STATUS_SUCCESS
;
414 PnpEventThread(IN LPVOID lpParameter
)
417 Status
= EventThread(lpParameter
);
418 NtTerminateThread(NtCurrentThread(), Status
);