VirtualBox

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

Last change on this file since 106061 was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: USBInstall.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBox host drivers - USB drivers - Filter & driver installation, Installation code.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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_T 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 RTMsgInfo("%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 RTMsgInfo("USB installation");
109
110 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
111 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
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 RTMsgInfo("Installation successful!");
133 else
134 {
135 RTMsgError("Installation failed: %Rhrc", hr);
136 rc = VERR_GENERAL_FAILURE;
137 }
138
139 RTUtf16Free(pwszInfFile);
140 }
141 else
142 RTMsgError("Failed to construct INF path: %Rrc", rc);
143 }
144 else
145 RTMsgError("Service creation failed: %Rrc", rc);
146
147 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
148}
149
150
151/**
152 * Changes the USB driver service to specified driver path.
153 *
154 * @returns 0 on success.
155 * @returns < 0 on failure.
156 */
157int usblibOsChangeService(const char *pszDriverPath)
158{
159 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
160 DWORD dwLastError = GetLastError();
161 int rc = RTErrConvertFromWin32(dwLastError);
162 AssertPtr(pszDriverPath);
163 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
164 if (hSMgrCreate)
165 {
166 SC_HANDLE hService = OpenService(hSMgrCreate,
167 SERVICE_NAME,
168 GENERIC_ALL);
169 dwLastError = GetLastError();
170 if (hService == NULL)
171 {
172 AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
173 rc = RTErrConvertFromWin32(dwLastError);
174 }
175 else
176 {
177 /* We only gonna change the driver image path, the rest remains like it already is */
178 if (ChangeServiceConfig(hService,
179 SERVICE_NO_CHANGE,
180 SERVICE_NO_CHANGE,
181 SERVICE_NO_CHANGE,
182 pszDriverPath,
183 NULL,
184 NULL,
185 NULL,
186 NULL,
187 NULL,
188 NULL))
189 {
190 RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath);
191 }
192 else
193 {
194 AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
195 rc = RTErrConvertFromWin32(dwLastError);
196 }
197 if (hService != NULL)
198 CloseServiceHandle(hService);
199 }
200
201 CloseServiceHandle(hSMgrCreate);
202 }
203 return rc;
204}
205
206
207/**
208 * Creates the service.
209 *
210 * @returns 0 on success.
211 * @returns < 0 on failure.
212 */
213int usblibOsCreateService(void)
214{
215 /*
216 * Assume it didn't exist, so we'll create the service.
217 */
218 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
219 DWORD dwLastError = GetLastError();
220 int rc = RTErrConvertFromWin32(dwLastError);
221 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
222 if (hSMgrCreate)
223 {
224 char szDriver[RTPATH_MAX];
225 rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxUSBMon.sys"));
226 if (RT_SUCCESS(rc))
227 {
228 strcat(szDriver, "\\VBoxUSBMon.sys");
229 RTPrintf("Creating USB monitor driver service with path %s ...\n", szDriver);
230 SC_HANDLE hService = CreateService(hSMgrCreate,
231 SERVICE_NAME,
232 "VBox USB Monitor Driver",
233 SERVICE_QUERY_STATUS,
234 SERVICE_KERNEL_DRIVER,
235 SERVICE_DEMAND_START,
236 SERVICE_ERROR_NORMAL,
237 szDriver,
238 NULL, NULL, NULL, NULL, NULL);
239 dwLastError = GetLastError();
240 if (dwLastError == ERROR_SERVICE_EXISTS)
241 {
242 RTPrintf("USB monitor driver service already exists, skipping creation.\n");
243 rc = usblibOsChangeService(szDriver);
244 }
245 else
246 {
247 AssertMsg(hService, ("CreateService failed! LastError=%Rwa, szDriver=%s\n", dwLastError, szDriver));
248 rc = RTErrConvertFromWin32(dwLastError);
249 if (hService != NULL)
250 CloseServiceHandle(hService);
251 }
252 }
253 CloseServiceHandle(hSMgrCreate);
254 }
255 return rc;
256}
Note: See TracBrowser for help on using the repository browser.

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