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