VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/Install/USBInstall.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 3 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: USBInstall.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VBox host drivers - USB drivers - Filter & driver installation, Installation code.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/win/windows.h>
42#include <iprt/win/setupapi.h>
43#include <newdev.h>
44#include <iprt/assert.h>
45#include <iprt/errcore.h>
46#include <iprt/initterm.h>
47#include <iprt/message.h>
48#include <iprt/param.h>
49#include <iprt/path.h>
50#include <iprt/process.h>
51#include <iprt/stream.h>
52#include <iprt/string.h>
53#include <iprt/utf16.h>
54
55#include <VBox/VBoxDrvCfg-win.h>
56
57
58/*********************************************************************************************************************************
59* Defined Constants And Macros *
60*********************************************************************************************************************************/
61/** The support service name. */
62#define SERVICE_NAME "VBoxUSBMon"
63/** Win32 Device name. */
64#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
65/** NT Device name. */
66#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
67/** Win32 Symlink name. */
68#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
69
70
71/*********************************************************************************************************************************
72* Internal Functions *
73*********************************************************************************************************************************/
74int usblibOsCreateService(void);
75
76
77static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char *pszMsg, void *pvContext)
78{
79 RT_NOREF1(pvContext);
80 switch (enmSeverity)
81 {
82 case VBOXDRVCFG_LOG_SEVERITY_FLOW:
83 case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
84 break;
85 case VBOXDRVCFG_LOG_SEVERITY_REL:
86 RTPrintf("%s", pszMsg);
87 break;
88 default:
89 break;
90 }
91}
92
93static DECLCALLBACK(void) vboxUsbPanic(void *pvPanic)
94{
95 RT_NOREF1(pvPanic);
96#ifndef DEBUG_bird
97 AssertFailed();
98#endif
99}
100
101
102int __cdecl main(int argc, char **argv)
103{
104 int rc = RTR3InitExe(argc, &argv, 0);
105 if (RT_FAILURE(rc))
106 return RTMsgInitFailure(rc);
107
108 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
109 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
110
111 RTPrintf("USB installation\n");
112
113 rc = usblibOsCreateService();
114 if (RT_SUCCESS(rc))
115 {
116 /* Build the path to the INF file: */
117 char szInfFile[RTPATH_MAX];
118 rc = RTProcGetExecutablePath(szInfFile, sizeof(szInfFile)) ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
119 if (RT_SUCCESS(rc))
120 {
121 RTPathStripFilename(szInfFile);
122 rc = RTPathAppend(szInfFile, sizeof(szInfFile), "VBoxUSB.inf");
123 }
124 PRTUTF16 pwszInfFile = NULL;
125 if (RT_SUCCESS(rc))
126 rc = RTStrToUtf16(szInfFile, &pwszInfFile);
127 if (RT_SUCCESS(rc))
128 {
129 /* Install the INF file: */
130 HRESULT hr = VBoxDrvCfgInfInstall(pwszInfFile);
131 if (hr == S_OK)
132 RTPrintf("Installation successful.\n");
133 else
134 rc = -1;
135
136 RTUtf16Free(pwszInfFile);
137 }
138 else
139 RTMsgError("Failed to construct INF path: %Rrc", rc);
140 }
141
142 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
143}
144
145
146/**
147 * Changes the USB driver service to specified driver path.
148 *
149 * @returns 0 on success.
150 * @returns < 0 on failure.
151 */
152int usblibOsChangeService(const char *pszDriverPath)
153{
154 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
155 DWORD dwLastError = GetLastError();
156 int rc = RTErrConvertFromWin32(dwLastError);
157 AssertPtr(pszDriverPath);
158 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
159 if (hSMgrCreate)
160 {
161 SC_HANDLE hService = OpenService(hSMgrCreate,
162 SERVICE_NAME,
163 GENERIC_ALL);
164 dwLastError = GetLastError();
165 if (hService == NULL)
166 {
167 AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
168 rc = RTErrConvertFromWin32(dwLastError);
169 }
170 else
171 {
172 /* We only gonna change the driver image path, the rest remains like it already is */
173 if (ChangeServiceConfig(hService,
174 SERVICE_NO_CHANGE,
175 SERVICE_NO_CHANGE,
176 SERVICE_NO_CHANGE,
177 pszDriverPath,
178 NULL,
179 NULL,
180 NULL,
181 NULL,
182 NULL,
183 NULL))
184 {
185 RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath);
186 }
187 else
188 {
189 AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
190 rc = RTErrConvertFromWin32(dwLastError);
191 }
192 if (hService != NULL)
193 CloseServiceHandle(hService);
194 }
195
196 CloseServiceHandle(hSMgrCreate);
197 }
198 return rc;
199}
200
201
202/**
203 * Creates the service.
204 *
205 * @returns 0 on success.
206 * @returns < 0 on failure.
207 */
208int usblibOsCreateService(void)
209{
210 /*
211 * Assume it didn't exist, so we'll create the service.
212 */
213 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
214 DWORD dwLastError = GetLastError();
215 int rc = RTErrConvertFromWin32(dwLastError);
216 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
217 if (hSMgrCreate)
218 {
219 char szDriver[RTPATH_MAX];
220 rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxUSBMon.sys"));
221 if (RT_SUCCESS(rc))
222 {
223 strcat(szDriver, "\\VBoxUSBMon.sys");
224 RTPrintf("Creating USB monitor driver service with path %s ...\n", szDriver);
225 SC_HANDLE hService = CreateService(hSMgrCreate,
226 SERVICE_NAME,
227 "VBox USB Monitor Driver",
228 SERVICE_QUERY_STATUS,
229 SERVICE_KERNEL_DRIVER,
230 SERVICE_DEMAND_START,
231 SERVICE_ERROR_NORMAL,
232 szDriver,
233 NULL, NULL, NULL, NULL, NULL);
234 dwLastError = GetLastError();
235 if (dwLastError == ERROR_SERVICE_EXISTS)
236 {
237 RTPrintf("USB monitor driver service already exists, skipping creation.\n");
238 rc = usblibOsChangeService(szDriver);
239 }
240 else
241 {
242 AssertMsg(hService, ("CreateService failed! LastError=%Rwa, szDriver=%s\n", dwLastError, szDriver));
243 rc = RTErrConvertFromWin32(dwLastError);
244 if (hService != NULL)
245 CloseServiceHandle(hService);
246 }
247 }
248 CloseServiceHandle(hSMgrCreate);
249 }
250 return rc;
251}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette