1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver installation
|
---|
4 | *
|
---|
5 | * Installation code
|
---|
6 | *
|
---|
7 | * Copyright (C) 2006-2016 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 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <iprt/win/windows.h>
|
---|
23 | #include <iprt/win/setupapi.h>
|
---|
24 | #include <newdev.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/initterm.h>
|
---|
28 | #include <iprt/param.h>
|
---|
29 | #include <iprt/path.h>
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <VBox/err.h>
|
---|
33 | #include <stdio.h>
|
---|
34 |
|
---|
35 | #include <VBox/VBoxDrvCfg-win.h>
|
---|
36 |
|
---|
37 | static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char *pszMsg, void *pvContext)
|
---|
38 | {
|
---|
39 | RT_NOREF1(pvContext);
|
---|
40 | switch (enmSeverity)
|
---|
41 | {
|
---|
42 | case VBOXDRVCFG_LOG_SEVERITY_FLOW:
|
---|
43 | case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
|
---|
44 | break;
|
---|
45 | case VBOXDRVCFG_LOG_SEVERITY_REL:
|
---|
46 | RTPrintf("%s", pszMsg);
|
---|
47 | break;
|
---|
48 | default:
|
---|
49 | break;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | static DECLCALLBACK(void) vboxUsbPanic(void *pvPanic)
|
---|
54 | {
|
---|
55 | RT_NOREF1(pvPanic);
|
---|
56 | #ifndef DEBUG_bird
|
---|
57 | AssertFailed();
|
---|
58 | #endif
|
---|
59 | }
|
---|
60 |
|
---|
61 | int usblibOsCreateService(void);
|
---|
62 |
|
---|
63 | int __cdecl main(int argc, char **argv)
|
---|
64 | {
|
---|
65 | if (RTR3InitExe(argc, &argv, 0) != VINF_SUCCESS)
|
---|
66 | {
|
---|
67 | printf("Could not init IPRT!\n");
|
---|
68 | return 1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
|
---|
72 | VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
|
---|
73 |
|
---|
74 | RTPrintf("USB installation\n");
|
---|
75 |
|
---|
76 | int rc = usblibOsCreateService();
|
---|
77 |
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | {
|
---|
80 | LPWSTR lpszFilePart;
|
---|
81 | WCHAR szFullPath[MAX_PATH];
|
---|
82 | DWORD len;
|
---|
83 |
|
---|
84 | len = GetFullPathNameW(L".\\VBoxUSB.inf", RT_ELEMENTS(szFullPath), szFullPath, &lpszFilePart);
|
---|
85 | Assert(len);
|
---|
86 |
|
---|
87 | HRESULT hr = VBoxDrvCfgInfInstall(szFullPath);
|
---|
88 | if (hr == S_OK)
|
---|
89 | {
|
---|
90 | RTPrintf("Installation successful.\n");
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | rc = -1;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | rc = 0;
|
---|
100 |
|
---|
101 | /** @todo RTR3Term(); */
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /** The support service name. */
|
---|
106 | #define SERVICE_NAME "VBoxUSBMon"
|
---|
107 | /** Win32 Device name. */
|
---|
108 | #define DEVICE_NAME "\\\\.\\VBoxUSBMon"
|
---|
109 | /** NT Device name. */
|
---|
110 | #define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
|
---|
111 | /** Win32 Symlink name. */
|
---|
112 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Changes the USB driver service to specified driver path.
|
---|
117 | *
|
---|
118 | * @returns 0 on success.
|
---|
119 | * @returns < 0 on failure.
|
---|
120 | */
|
---|
121 | int usblibOsChangeService(const char *pszDriverPath)
|
---|
122 | {
|
---|
123 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
124 | DWORD dwLastError = GetLastError();
|
---|
125 | int rc = RTErrConvertFromWin32(dwLastError);
|
---|
126 | AssertPtr(pszDriverPath);
|
---|
127 | AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
|
---|
128 | if (hSMgrCreate)
|
---|
129 | {
|
---|
130 | SC_HANDLE hService = OpenService(hSMgrCreate,
|
---|
131 | SERVICE_NAME,
|
---|
132 | GENERIC_ALL);
|
---|
133 | DWORD dwLastError = GetLastError();
|
---|
134 | if (hService == NULL)
|
---|
135 | {
|
---|
136 | AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
|
---|
137 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | /* We only gonna change the driver image path, the rest remains like it already is */
|
---|
142 | if (ChangeServiceConfig(hService,
|
---|
143 | SERVICE_NO_CHANGE,
|
---|
144 | SERVICE_NO_CHANGE,
|
---|
145 | SERVICE_NO_CHANGE,
|
---|
146 | pszDriverPath,
|
---|
147 | NULL,
|
---|
148 | NULL,
|
---|
149 | NULL,
|
---|
150 | NULL,
|
---|
151 | NULL,
|
---|
152 | NULL))
|
---|
153 | {
|
---|
154 | RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath);
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
|
---|
159 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
160 | }
|
---|
161 | if (hService != NULL)
|
---|
162 | CloseServiceHandle(hService);
|
---|
163 | }
|
---|
164 |
|
---|
165 | CloseServiceHandle(hSMgrCreate);
|
---|
166 | }
|
---|
167 | return rc;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Creates the service.
|
---|
173 | *
|
---|
174 | * @returns 0 on success.
|
---|
175 | * @returns < 0 on failure.
|
---|
176 | */
|
---|
177 | int usblibOsCreateService(void)
|
---|
178 | {
|
---|
179 | /*
|
---|
180 | * Assume it didn't exist, so we'll create the service.
|
---|
181 | */
|
---|
182 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
183 | DWORD dwLastError = GetLastError();
|
---|
184 | int rc = RTErrConvertFromWin32(dwLastError);
|
---|
185 | AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
|
---|
186 | if (hSMgrCreate)
|
---|
187 | {
|
---|
188 | char szDriver[RTPATH_MAX];
|
---|
189 | int rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxUSBMon.sys"));
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | strcat(szDriver, "\\VBoxUSBMon.sys");
|
---|
193 | RTPrintf("Creating USB monitor driver service with path %s ...\n", szDriver);
|
---|
194 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
195 | SERVICE_NAME,
|
---|
196 | "VBox USB Monitor Driver",
|
---|
197 | SERVICE_QUERY_STATUS,
|
---|
198 | SERVICE_KERNEL_DRIVER,
|
---|
199 | SERVICE_DEMAND_START,
|
---|
200 | SERVICE_ERROR_NORMAL,
|
---|
201 | szDriver,
|
---|
202 | NULL, NULL, NULL, NULL, NULL);
|
---|
203 | DWORD dwLastError = GetLastError();
|
---|
204 | if (dwLastError == ERROR_SERVICE_EXISTS)
|
---|
205 | {
|
---|
206 | RTPrintf("USB monitor driver service already exists, skipping creation.\n");
|
---|
207 | rc = usblibOsChangeService(szDriver);
|
---|
208 | }
|
---|
209 | else
|
---|
210 | {
|
---|
211 | AssertMsg(hService, ("CreateService failed! LastError=%Rwa, szDriver=%s\n", dwLastError, szDriver));
|
---|
212 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
213 | if (hService != NULL)
|
---|
214 | CloseServiceHandle(hService);
|
---|
215 | }
|
---|
216 | }
|
---|
217 | CloseServiceHandle(hSMgrCreate);
|
---|
218 | }
|
---|
219 | return rc;
|
---|
220 | }
|
---|