1 | /* $Id: VBoxGuestDrvInst.cpp 69496 2017-10-28 14:55:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * instdrvmain - Install guest drivers on NT4
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @todo
|
---|
20 | * The spacing in this file is horrible! Here are some simple rules:
|
---|
21 | * - tabs are forbidden
|
---|
22 | * - indentation is 4 spaces
|
---|
23 | */
|
---|
24 |
|
---|
25 |
|
---|
26 | #include <iprt/win/windows.h>
|
---|
27 | #include <iprt/win/setupapi.h>
|
---|
28 | #include <regstr.h>
|
---|
29 | #include <DEVGUID.h>
|
---|
30 | #include <stdio.h>
|
---|
31 |
|
---|
32 | #include "tchar.h"
|
---|
33 | #include "string.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | /*********************************************************************************************************************************
|
---|
37 | * Defined Constants And Macros *
|
---|
38 | *********************************************************************************************************************************/
|
---|
39 |
|
---|
40 | /** The video service name. */
|
---|
41 | #define VBOXGUEST_VIDEO_NAME "VBoxVideo"
|
---|
42 |
|
---|
43 | /** The video inf file name */
|
---|
44 | #define VBOXGUEST_VIDEO_INF_NAME "VBoxVideo.inf"
|
---|
45 |
|
---|
46 | /////////////////////////////////////////////////////////////////////////////
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Do some cleanup of data we used. Called by installVideoDriver()
|
---|
52 | */
|
---|
53 | void closeAndDestroy(HDEVINFO hDevInfo, HINF hInf)
|
---|
54 | {
|
---|
55 | SetupDiDestroyDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
|
---|
56 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
57 | SetupCloseInfFile(hInf);
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Install the VBox video driver.
|
---|
63 | *
|
---|
64 | * @returns TRUE on success.
|
---|
65 | * @returns FALSE on failure.
|
---|
66 | * @param szDriverDir The base directory where we find the INF.
|
---|
67 | */
|
---|
68 | BOOL installVideoDriver(TCHAR* szDriverDir)
|
---|
69 | {
|
---|
70 | HDEVINFO hDevInfo;
|
---|
71 | SP_DEVINSTALL_PARAMS DeviceInstallParams={0};
|
---|
72 | SP_DRVINFO_DATA drvInfoData={0};
|
---|
73 | SP_DRVINFO_DETAIL_DATA DriverInfoDetailData={0};
|
---|
74 |
|
---|
75 | DWORD cbReqSize;
|
---|
76 |
|
---|
77 | /* Vars used for reading the INF */
|
---|
78 | HINF hInf;
|
---|
79 | TCHAR szServiceSection[LINE_LEN];
|
---|
80 | INFCONTEXT serviceContext;
|
---|
81 | TCHAR szServiceData[LINE_LEN];
|
---|
82 | TCHAR deviceRegStr[1000];//I'm lazy here. 1000 ought to be enough for everybody...
|
---|
83 |
|
---|
84 | SP_DEVINFO_DATA deviceInfoData;
|
---|
85 | DWORD configFlags;
|
---|
86 |
|
---|
87 | HKEY hkey;
|
---|
88 | DWORD disp;
|
---|
89 | TCHAR regKeyName[LINE_LEN];
|
---|
90 |
|
---|
91 | BOOL rc;
|
---|
92 |
|
---|
93 | /* Create an empty list */
|
---|
94 | hDevInfo = SetupDiCreateDeviceInfoList((LPGUID) &GUID_DEVCLASS_DISPLAY,
|
---|
95 | NULL);
|
---|
96 |
|
---|
97 | if (hDevInfo == INVALID_HANDLE_VALUE)
|
---|
98 | return FALSE;
|
---|
99 |
|
---|
100 | memset(&DeviceInstallParams, 0, sizeof(SP_DEVINSTALL_PARAMS));
|
---|
101 | DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
|
---|
102 |
|
---|
103 | rc=SetupDiGetDeviceInstallParams(hDevInfo,
|
---|
104 | NULL,
|
---|
105 | &DeviceInstallParams);
|
---|
106 |
|
---|
107 | if(!rc)
|
---|
108 | return FALSE;
|
---|
109 |
|
---|
110 | DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
|
---|
111 | DeviceInstallParams.Flags |= DI_NOFILECOPY | /* We did our own file copying */
|
---|
112 | DI_DONOTCALLCONFIGMG |
|
---|
113 | DI_ENUMSINGLEINF; /* .DriverPath specifies an inf file */
|
---|
114 |
|
---|
115 |
|
---|
116 | /* Path to inf file */
|
---|
117 | wsprintf(DeviceInstallParams.DriverPath,
|
---|
118 | TEXT("%ws\\%ws"),
|
---|
119 | szDriverDir, TEXT(VBOXGUEST_VIDEO_INF_NAME));
|
---|
120 |
|
---|
121 | rc=SetupDiSetDeviceInstallParams(hDevInfo,
|
---|
122 | NULL,
|
---|
123 | &DeviceInstallParams);
|
---|
124 | if(!rc)
|
---|
125 | return FALSE;
|
---|
126 |
|
---|
127 | /* Read the drivers from the inf file */
|
---|
128 | if (!SetupDiBuildDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER))
|
---|
129 | {
|
---|
130 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
131 | return FALSE;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Get the first found driver.
|
---|
135 | Our Inf file only contains one so this is fine */
|
---|
136 | drvInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
|
---|
137 | if(FALSE==SetupDiEnumDriverInfo(hDevInfo, NULL, SPDIT_CLASSDRIVER,
|
---|
138 | 0, &drvInfoData)){
|
---|
139 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
140 | return FALSE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Get necessary driver details */
|
---|
144 | DriverInfoDetailData.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
|
---|
145 | if (!(!SetupDiGetDriverInfoDetail(hDevInfo,
|
---|
146 | NULL,
|
---|
147 | &drvInfoData,
|
---|
148 | &DriverInfoDetailData,
|
---|
149 | DriverInfoDetailData.cbSize,
|
---|
150 | &cbReqSize)
|
---|
151 | &&GetLastError()== ERROR_INSUFFICIENT_BUFFER) )
|
---|
152 | {
|
---|
153 | SetupDiDestroyDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
|
---|
154 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
155 | return FALSE;
|
---|
156 | }
|
---|
157 |
|
---|
158 | hInf = SetupOpenInfFile(DriverInfoDetailData.InfFileName,
|
---|
159 | NULL, INF_STYLE_WIN4, NULL);
|
---|
160 |
|
---|
161 | if (hInf == INVALID_HANDLE_VALUE)
|
---|
162 | {
|
---|
163 | SetupDiDestroyDriverInfoList(hDevInfo, NULL, SPDIT_CLASSDRIVER);
|
---|
164 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
165 | return FALSE;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* First install the service */
|
---|
169 | wsprintf(szServiceSection, TEXT("%ws.Services"),
|
---|
170 | DriverInfoDetailData.SectionName);
|
---|
171 |
|
---|
172 | if(!SetupFindFirstLine(hInf, szServiceSection, NULL, &serviceContext))
|
---|
173 | {
|
---|
174 | /* No service line?? Can't be... */
|
---|
175 | closeAndDestroy(hDevInfo, hInf);
|
---|
176 | return FALSE;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* Get the name */
|
---|
180 | SetupGetStringField(&serviceContext,
|
---|
181 | 1,
|
---|
182 | szServiceData,
|
---|
183 | sizeof(szServiceData),
|
---|
184 | NULL);
|
---|
185 |
|
---|
186 | wsprintf(deviceRegStr, TEXT("Root\\LEGACY_%ws\\0000"), szServiceData);
|
---|
187 |
|
---|
188 | memset(&deviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
|
---|
189 | deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
---|
190 |
|
---|
191 | if (SetupDiOpenDeviceInfo(hDevInfo, deviceRegStr, NULL, 0, &deviceInfoData) //Check for existing
|
---|
192 | ||(SetupDiCreateDeviceInfo(hDevInfo, deviceRegStr, //Create new
|
---|
193 | (LPGUID) &GUID_DEVCLASS_DISPLAY,
|
---|
194 | NULL, //Do we need a description here?
|
---|
195 | NULL, //No user interface
|
---|
196 | 0,
|
---|
197 | &deviceInfoData) &&
|
---|
198 | SetupDiRegisterDeviceInfo(hDevInfo,
|
---|
199 | &deviceInfoData,
|
---|
200 | 0,
|
---|
201 | NULL,
|
---|
202 | NULL,
|
---|
203 | NULL)) )
|
---|
204 | {
|
---|
205 | /* We created a new key in the registry */
|
---|
206 |
|
---|
207 | memset(&DeviceInstallParams, 0,sizeof(SP_DEVINSTALL_PARAMS));
|
---|
208 | DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
|
---|
209 |
|
---|
210 | SetupDiGetDeviceInstallParams(hDevInfo,
|
---|
211 | &deviceInfoData,
|
---|
212 | &DeviceInstallParams);
|
---|
213 |
|
---|
214 | DeviceInstallParams.Flags |= DI_NOFILECOPY | //We already copied the files
|
---|
215 | DI_DONOTCALLCONFIGMG |
|
---|
216 | DI_ENUMSINGLEINF; //Use our INF file only
|
---|
217 |
|
---|
218 | /* Path to inf file */
|
---|
219 | wsprintf(DeviceInstallParams.DriverPath,
|
---|
220 | TEXT("%ws\\%ws"),
|
---|
221 | szDriverDir, TEXT(VBOXGUEST_VIDEO_INF_NAME));
|
---|
222 |
|
---|
223 | SetupDiSetDeviceInstallParams(hDevInfo,
|
---|
224 | &deviceInfoData,
|
---|
225 | &DeviceInstallParams);
|
---|
226 |
|
---|
227 |
|
---|
228 | if(!SetupDiBuildDriverInfoList(hDevInfo,
|
---|
229 | &deviceInfoData,
|
---|
230 | SPDIT_CLASSDRIVER))
|
---|
231 | {
|
---|
232 | closeAndDestroy(hDevInfo, hInf);
|
---|
233 | return FALSE;
|
---|
234 | }
|
---|
235 |
|
---|
236 | drvInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
|
---|
237 | if (!SetupDiEnumDriverInfo(hDevInfo,
|
---|
238 | &deviceInfoData,
|
---|
239 | SPDIT_CLASSDRIVER,
|
---|
240 | 0,
|
---|
241 | &drvInfoData))
|
---|
242 | {
|
---|
243 | closeAndDestroy(hDevInfo, hInf);
|
---|
244 | return FALSE;
|
---|
245 | }
|
---|
246 |
|
---|
247 | if(!SetupDiSetSelectedDriver(hDevInfo,
|
---|
248 | &deviceInfoData,
|
---|
249 | &drvInfoData))
|
---|
250 | {
|
---|
251 | closeAndDestroy(hDevInfo, hInf);
|
---|
252 | return FALSE;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if(!SetupDiInstallDevice(hDevInfo,
|
---|
256 | &deviceInfoData))
|
---|
257 | {
|
---|
258 | closeAndDestroy(hDevInfo, hInf);
|
---|
259 | return FALSE;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* Make sure the device is enabled */
|
---|
264 | if (SetupDiGetDeviceRegistryProperty(hDevInfo,
|
---|
265 | &deviceInfoData, SPDRP_CONFIGFLAGS,
|
---|
266 | NULL, (LPBYTE) &configFlags,
|
---|
267 | sizeof(DWORD),
|
---|
268 | NULL)
|
---|
269 | && (configFlags & CONFIGFLAG_DISABLED))
|
---|
270 | {
|
---|
271 | configFlags &= ~CONFIGFLAG_DISABLED;
|
---|
272 |
|
---|
273 | SetupDiSetDeviceRegistryProperty(hDevInfo,
|
---|
274 | &deviceInfoData,
|
---|
275 | SPDRP_CONFIGFLAGS,
|
---|
276 | (LPBYTE) &configFlags,
|
---|
277 | sizeof(DWORD));
|
---|
278 | }
|
---|
279 |
|
---|
280 | wsprintf(regKeyName,
|
---|
281 | TEXT("System\\CurrentControlSet\\Services\\%ws\\Device%d"),
|
---|
282 | szServiceData, 0); //We only have one device
|
---|
283 |
|
---|
284 | if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
|
---|
285 | regKeyName,
|
---|
286 | 0,
|
---|
287 | NULL,
|
---|
288 | REG_OPTION_NON_VOLATILE,
|
---|
289 | KEY_READ | KEY_WRITE,
|
---|
290 | NULL,
|
---|
291 | &hkey,
|
---|
292 | &disp) == ERROR_SUCCESS)
|
---|
293 | {
|
---|
294 | /* Insert description */
|
---|
295 | RegSetValueEx(hkey,
|
---|
296 | TEXT("Device Description"),
|
---|
297 | 0,
|
---|
298 | REG_SZ,
|
---|
299 | (LPBYTE) DriverInfoDetailData.DrvDescription,
|
---|
300 | (lstrlen(DriverInfoDetailData.DrvDescription) + 1) *
|
---|
301 | sizeof(TCHAR) );
|
---|
302 |
|
---|
303 | TCHAR szSoftwareSection[LINE_LEN];
|
---|
304 |
|
---|
305 | wsprintf(szSoftwareSection,
|
---|
306 | TEXT("%ws.SoftwareSettings"),
|
---|
307 | szServiceData);
|
---|
308 |
|
---|
309 | if (!SetupInstallFromInfSection(NULL,
|
---|
310 | hInf,
|
---|
311 | szSoftwareSection,
|
---|
312 | SPINST_REGISTRY,
|
---|
313 | hkey,
|
---|
314 | NULL,
|
---|
315 | 0,
|
---|
316 | NULL,
|
---|
317 | NULL,
|
---|
318 | NULL,
|
---|
319 | NULL))
|
---|
320 | {
|
---|
321 | RegCloseKey(hkey);
|
---|
322 | closeAndDestroy(hDevInfo, hInf);
|
---|
323 | return FALSE;
|
---|
324 | }
|
---|
325 |
|
---|
326 | RegCloseKey(hkey);
|
---|
327 | }
|
---|
328 |
|
---|
329 | /* Install OpenGL stuff */
|
---|
330 | if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
|
---|
331 | TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\OpenGLDrivers"),
|
---|
332 | 0,
|
---|
333 | NULL,
|
---|
334 | REG_OPTION_NON_VOLATILE,
|
---|
335 | KEY_READ | KEY_WRITE,
|
---|
336 | NULL,
|
---|
337 | &hkey,
|
---|
338 | &disp) == ERROR_SUCCESS)
|
---|
339 | {
|
---|
340 | /* Do installation here if ever necessary. Currently there is no OpenGL stuff */
|
---|
341 |
|
---|
342 | RegCloseKey(hkey);
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /* Cleanup */
|
---|
347 | closeAndDestroy(hDevInfo, hInf);
|
---|
348 |
|
---|
349 | #if 0
|
---|
350 | /* If this key is inserted into the registry, windows will show the desktop
|
---|
351 | applet on next boot. We decide in the installer if we want that so the code
|
---|
352 | is disabled here. */
|
---|
353 | /* Set registry keys so windows picks up the changes */
|
---|
354 | if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
|
---|
355 | TEXT("SYSTEM\\CurrentControlSet\\Control\\GraphicsDrivers\\NewDisplay"),
|
---|
356 | 0,
|
---|
357 | NULL,
|
---|
358 | REG_OPTION_NON_VOLATILE,
|
---|
359 | KEY_READ | KEY_WRITE,
|
---|
360 | NULL,
|
---|
361 | &hkey,
|
---|
362 | &disp) == ERROR_SUCCESS)
|
---|
363 | {
|
---|
364 | RegCloseKey(hkey);
|
---|
365 | }
|
---|
366 | #endif
|
---|
367 |
|
---|
368 | /* We must reboot at some point */
|
---|
369 | if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
|
---|
370 | TEXT("SYSTEM\\CurrentControlSet\\Control\\GraphicsDrivers\\RebootNecessary"),
|
---|
371 | 0,
|
---|
372 | NULL,
|
---|
373 | REG_OPTION_NON_VOLATILE,
|
---|
374 | KEY_READ | KEY_WRITE,
|
---|
375 | NULL,
|
---|
376 | &hkey,
|
---|
377 | &disp) == ERROR_SUCCESS)
|
---|
378 | {
|
---|
379 | RegCloseKey(hkey);
|
---|
380 | }
|
---|
381 |
|
---|
382 | return TRUE;
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | void displayHelpAndExit(char *programName)
|
---|
387 | {
|
---|
388 | printf("Installs VirtualBox Guest Additions for Windows NT 4.0\n\n");
|
---|
389 | printf("Syntax: %s [/i|/u]\n", programName);
|
---|
390 | printf("\n\t/i - perform Guest Additions installation\n");
|
---|
391 | printf("\t/u: - perform Guest Additions uninstallation\n");
|
---|
392 | exit(1);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Check if we are running on NT4.
|
---|
397 | *
|
---|
398 | * @returns TRUE if NT 4.
|
---|
399 | * @returns FALSE otherwise.
|
---|
400 | */
|
---|
401 | BOOL isNT4(void)
|
---|
402 | {
|
---|
403 | OSVERSIONINFO osVersion;
|
---|
404 |
|
---|
405 | osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
406 | GetVersionEx(&osVersion);
|
---|
407 |
|
---|
408 | switch (osVersion.dwPlatformId)
|
---|
409 | {
|
---|
410 | case VER_PLATFORM_WIN32s:
|
---|
411 | case VER_PLATFORM_WIN32_WINDOWS:
|
---|
412 | return FALSE;
|
---|
413 | case VER_PLATFORM_WIN32_NT:
|
---|
414 | if (osVersion.dwMajorVersion == 4)
|
---|
415 | return TRUE;
|
---|
416 | else
|
---|
417 | return FALSE;
|
---|
418 | default:
|
---|
419 | break;
|
---|
420 | }
|
---|
421 | return FALSE;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * Video driver uninstallation will be added later if necessary.
|
---|
426 | */
|
---|
427 | int uninstallDrivers(void)
|
---|
428 | {
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | int main(int argc, char **argv)
|
---|
434 | {
|
---|
435 | BOOL rc=FALSE;
|
---|
436 | TCHAR szInstallDir[MAX_PATH];
|
---|
437 | HMODULE hExe = GetModuleHandle(NULL);
|
---|
438 |
|
---|
439 | /** @todo r=bird:
|
---|
440 | * And by the way, we're only missing the coding style dmik uses now
|
---|
441 | * and this file would contain the complete set. */
|
---|
442 | if(2!=argc || (stricmp(argv[1], "/i") && stricmp(argv[1], "/u")))
|
---|
443 | displayHelpAndExit(argv[0]);
|
---|
444 |
|
---|
445 | /* This program is only for installing drivers on NT4 */
|
---|
446 | if(!isNT4()){
|
---|
447 | printf("This program only runs on NT4\n");
|
---|
448 | return -1;
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (!GetModuleFileName(hExe, &szInstallDir[0], sizeof(szInstallDir)))
|
---|
452 | {
|
---|
453 | printf("GetModuleFileName failed! rc = %d\n", GetLastError());
|
---|
454 | return -1;
|
---|
455 | }
|
---|
456 |
|
---|
457 | TCHAR *lastBackslash = wcsrchr(szInstallDir, L'\\');
|
---|
458 |
|
---|
459 | if (!lastBackslash)
|
---|
460 | {
|
---|
461 | printf("Invalid path name!\n");
|
---|
462 | return -1;
|
---|
463 | }
|
---|
464 |
|
---|
465 | *lastBackslash=L'\0';
|
---|
466 |
|
---|
467 | /* Install video driver. Mouse driver installation is done by overwriting
|
---|
468 | the image path in the setup script. */
|
---|
469 | if(!stricmp(argv[1], "/i")){
|
---|
470 | rc=installVideoDriver(szInstallDir);
|
---|
471 | }
|
---|
472 | else{
|
---|
473 | /* No video driver uninstallation yet. Do we need it? */
|
---|
474 | }
|
---|
475 |
|
---|
476 | if(FALSE==rc)
|
---|
477 | printf("Some failure occurred during driver installation.\n");
|
---|
478 | return !rc; /* Return != 0 on failure. */
|
---|
479 | }
|
---|