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)
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 LPWSTR Driver
, ImagePath
, FullImagePath
;
57 BOOLEAN deviceInstalled
= FALSE
;
59 /* Check if we know the hardware */
60 if (!SetupFindFirstLineW(hInf
, L
"HardwareIdsDatabase", HardwareId
, &Context
))
62 if (!INF_GetDataField(&Context
, 1, &Driver
))
65 /* Find associated driver name */
66 /* FIXME: check in other sections too! */
67 if (!SetupFindFirstLineW(hInf
, L
"BootBusExtenders.Load", Driver
, &Context
)
68 && !SetupFindFirstLineW(hInf
, L
"Keyboard.Load", Driver
, &Context
))
70 if (!INF_GetDataField(&Context
, 1, &ImagePath
))
73 /* Prepare full driver path */
74 dwValue
= PathPrefix
.MaximumLength
+ wcslen(ImagePath
) * sizeof(WCHAR
);
75 FullImagePath
= (LPWSTR
)RtlAllocateHeap(ProcessHeap
, 0, dwValue
);
78 DPRINT1("RtlAllocateHeap() failed\n");
81 RtlCopyMemory(FullImagePath
, PathPrefix
.Buffer
, PathPrefix
.MaximumLength
);
82 wcscat(FullImagePath
, ImagePath
);
84 DPRINT("Using driver '%S' for device '%S'\n", ImagePath
, DeviceId
);
86 /* Create service key */
87 RtlInitUnicodeString(&StringU
, Driver
);
88 InitializeObjectAttributes(&ObjectAttributes
, &StringU
, 0, hServices
, NULL
);
89 Status
= NtCreateKey(&hService
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, 0, &Disposition
);
90 if (!NT_SUCCESS(Status
))
92 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08x\n", &StringU
, Status
);
93 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
97 /* Fill service key */
98 if (Disposition
== REG_CREATED_NEW_KEY
)
116 dwValue
= SERVICE_KERNEL_DRIVER
;
125 /* HACK: don't put any path in registry */
132 (wcslen(ImagePath
) + 1) * sizeof(WCHAR
));
134 /* Associate device with the service we just filled */
135 Status
= NtSetValueKey(
141 (wcslen(Driver
) + 1) * sizeof(WCHAR
));
142 if (NT_SUCCESS(Status
))
144 /* Restart the device, so it will use the driver we registred */
145 deviceInstalled
= ResetDevice(DeviceId
);
148 /* HACK: Update driver path */
155 (wcslen(FullImagePath
) + 1) * sizeof(WCHAR
));
156 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
159 return deviceInstalled
;
169 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
170 UNICODE_STRING CompatibleIDsU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
171 UNICODE_STRING DeviceIdU
;
172 OBJECT_ATTRIBUTES ObjectAttributes
;
174 PKEY_VALUE_PARTIAL_INFORMATION pPartialInformation
= NULL
;
177 BOOLEAN bDriverInstalled
= FALSE
;
180 RtlInitUnicodeString(&DeviceIdU
, DeviceId
);
181 InitializeObjectAttributes(&ObjectAttributes
, &DeviceIdU
, 0, hEnum
, NULL
);
182 Status
= NtOpenKey(&hDeviceKey
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, &ObjectAttributes
);
183 if (!NT_SUCCESS(Status
))
185 DPRINT("Unable to open subkey '%S'\n", DeviceId
);
189 Status
= NtQueryValueKey(
192 KeyValuePartialInformation
,
196 if (Status
== STATUS_BUFFER_TOO_SMALL
)
198 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
199 if (!pPartialInformation
)
201 DPRINT1("RtlAllocateHeap() failed\n");
205 Status
= NtQueryValueKey(
208 KeyValuePartialInformation
,
213 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
217 else if (!NT_SUCCESS(Status
))
219 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
220 if (pPartialInformation
)
221 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
225 else if (pPartialInformation
)
227 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
228 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
230 && !bDriverInstalled
;
231 HardwareID
+= wcslen(HardwareID
) + 1)
233 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
237 if (!bDriverInstalled
)
239 if (pPartialInformation
)
241 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
242 pPartialInformation
= NULL
;
244 Status
= NtQueryValueKey(
247 KeyValuePartialInformation
,
251 if (Status
== STATUS_BUFFER_TOO_SMALL
)
253 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
254 if (!pPartialInformation
)
256 DPRINT("RtlAllocateHeap() failed\n");
260 Status
= NtQueryValueKey(
263 KeyValuePartialInformation
,
268 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
272 else if (!NT_SUCCESS(Status
))
274 if (pPartialInformation
)
275 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
277 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
280 else if (pPartialInformation
)
282 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
283 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
285 && !bDriverInstalled
;
286 HardwareID
+= wcslen(HardwareID
) + 1)
288 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
292 if (!bDriverInstalled
)
293 DPRINT("No driver available for %S\n", DeviceId
);
295 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
300 EventThread(IN LPVOID lpParameter
)
302 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
303 UNICODE_STRING ServicesU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
304 PPLUGPLAY_EVENT_BLOCK PnpEvent
;
305 OBJECT_ATTRIBUTES ObjectAttributes
;
308 HANDLE hEnum
, hServices
;
311 hInf
= *(HINF
*)lpParameter
;
313 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
314 Status
= NtOpenKey(&hEnum
, KEY_QUERY_VALUE
, &ObjectAttributes
);
315 if (!NT_SUCCESS(Status
))
317 DPRINT1("NtOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
321 InitializeObjectAttributes(&ObjectAttributes
, &ServicesU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
322 Status
= NtCreateKey(&hServices
, 0, &ObjectAttributes
, 0, NULL
, 0, NULL
);
323 if (!NT_SUCCESS(Status
))
325 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08lx\n", &ServicesU
, Status
);
330 PnpEventSize
= 0x1000;
331 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
332 if (PnpEvent
== NULL
)
336 return STATUS_NO_MEMORY
;
341 DPRINT("Calling NtGetPlugPlayEvent()\n");
343 /* Wait for the next pnp event */
344 Status
= NtGetPlugPlayEvent(0, 0, PnpEvent
, PnpEventSize
);
346 /* Resize the buffer for the PnP event if it's too small. */
347 if (Status
== STATUS_BUFFER_TOO_SMALL
)
349 PnpEventSize
+= 0x400;
350 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
351 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
352 if (PnpEvent
== NULL
)
356 return STATUS_NO_MEMORY
;
361 if (!NT_SUCCESS(Status
))
363 DPRINT("NtPlugPlayEvent() failed (Status %lx)\n", Status
);
367 /* Process the pnp event */
368 DPRINT("Received PnP Event\n");
369 if (IsEqualIID(&PnpEvent
->EventGuid
, (REFGUID
)&GUID_DEVICE_ENUMERATED
))
371 DPRINT("Device arrival event: %S\n", PnpEvent
->TargetDevice
.DeviceIds
);
372 InstallDevice(hInf
, hEnum
, hServices
, PnpEvent
->TargetDevice
.DeviceIds
);
376 DPRINT("Unknown event\n");
379 /* Dequeue the current pnp event and signal the next one */
380 NtPlugPlayControl(PlugPlayControlUserResponse
, NULL
, 0);
383 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
387 return STATUS_SUCCESS
;
391 PnpEventThread(IN LPVOID lpParameter
)
394 Status
= EventThread(lpParameter
);
395 NtTerminateThread(NtCurrentThread(), Status
);