1 | /* $Id: USBUninstall.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver uninstallation.
|
---|
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 |
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/path.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/errcore.h>
|
---|
51 | #include <VBox/VBoxDrvCfg-win.h>
|
---|
52 | #include <stdio.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | int usblibOsStopService(void);
|
---|
56 | int usblibOsDeleteService(void);
|
---|
57 |
|
---|
58 | static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char *pszMsg, void *pvContext)
|
---|
59 | {
|
---|
60 | RT_NOREF1(pvContext);
|
---|
61 | switch (enmSeverity)
|
---|
62 | {
|
---|
63 | case VBOXDRVCFG_LOG_SEVERITY_FLOW:
|
---|
64 | case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
|
---|
65 | break;
|
---|
66 | case VBOXDRVCFG_LOG_SEVERITY_REL:
|
---|
67 | printf("%s", pszMsg);
|
---|
68 | break;
|
---|
69 | default:
|
---|
70 | break;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | static DECLCALLBACK(void) vboxUsbPanic(void *pvPanic)
|
---|
75 | {
|
---|
76 | RT_NOREF1(pvPanic);
|
---|
77 | #ifndef DEBUG_bird
|
---|
78 | AssertFailed();
|
---|
79 | #endif
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | int __cdecl main(int argc, char **argv)
|
---|
84 | {
|
---|
85 | RT_NOREF2(argc, argv);
|
---|
86 | printf("USB uninstallation\n");
|
---|
87 |
|
---|
88 | VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
|
---|
89 | VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
|
---|
90 |
|
---|
91 | usblibOsStopService();
|
---|
92 | usblibOsDeleteService();
|
---|
93 |
|
---|
94 | HRESULT hr = VBoxDrvCfgInfUninstallAllF(L"USB", L"USB\\VID_80EE&PID_CAFE", SUOI_FORCEDELETE);
|
---|
95 | if (hr != S_OK)
|
---|
96 | {
|
---|
97 | printf("SetupUninstallOEMInf failed with hr=0x%lx\n", hr);
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | printf("USB uninstallation succeeded!\n");
|
---|
102 |
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** The support service name. */
|
---|
107 | #define SERVICE_NAME "VBoxUSBMon"
|
---|
108 | /** Win32 Device name. */
|
---|
109 | #define DEVICE_NAME "\\\\.\\VBoxUSBMon"
|
---|
110 | /** NT Device name. */
|
---|
111 | #define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
|
---|
112 | /** Win32 Symlink name. */
|
---|
113 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Stops a possibly running service.
|
---|
117 | *
|
---|
118 | * @returns 0 on success.
|
---|
119 | * @returns -1 on failure.
|
---|
120 | */
|
---|
121 | int usblibOsStopService(void)
|
---|
122 | {
|
---|
123 | /*
|
---|
124 | * Assume it didn't exist, so we'll create the service.
|
---|
125 | */
|
---|
126 | int rc = -1;
|
---|
127 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
128 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", GetLastError()));
|
---|
129 | if (hSMgr)
|
---|
130 | {
|
---|
131 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
|
---|
132 | if (hService)
|
---|
133 | {
|
---|
134 | /*
|
---|
135 | * Stop the service.
|
---|
136 | */
|
---|
137 | SERVICE_STATUS Status;
|
---|
138 | QueryServiceStatus(hService, &Status);
|
---|
139 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
140 | rc = 0;
|
---|
141 | else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | * Wait for finish about 1 minute.
|
---|
145 | * It should be enough for work with driver verifier
|
---|
146 | */
|
---|
147 | int iWait = 600;
|
---|
148 | while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
|
---|
149 | {
|
---|
150 | Sleep(100);
|
---|
151 | QueryServiceStatus(hService, &Status);
|
---|
152 | }
|
---|
153 | if (Status.dwCurrentState == SERVICE_STOPPED)
|
---|
154 | rc = 0;
|
---|
155 | else
|
---|
156 | AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
|
---|
157 | }
|
---|
158 | else
|
---|
159 | AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", GetLastError(), Status.dwCurrentState));
|
---|
160 | CloseServiceHandle(hService);
|
---|
161 | }
|
---|
162 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
163 | rc = 0;
|
---|
164 | else
|
---|
165 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", GetLastError()));
|
---|
166 | CloseServiceHandle(hSMgr);
|
---|
167 | }
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Deletes the service.
|
---|
174 | *
|
---|
175 | * @returns 0 on success.
|
---|
176 | * @returns -1 on failure.
|
---|
177 | */
|
---|
178 | int usblibOsDeleteService(void)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * Assume it didn't exist, so we'll create the service.
|
---|
182 | */
|
---|
183 | int rc = -1;
|
---|
184 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
185 | AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", GetLastError()));
|
---|
186 | if (hSMgr)
|
---|
187 | {
|
---|
188 | SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
|
---|
189 | if (hService)
|
---|
190 | {
|
---|
191 | /*
|
---|
192 | * Delete the service.
|
---|
193 | */
|
---|
194 | if (DeleteService(hService))
|
---|
195 | rc = 0;
|
---|
196 | else
|
---|
197 | AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", GetLastError()));
|
---|
198 | CloseServiceHandle(hService);
|
---|
199 | }
|
---|
200 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
201 | rc = 0;
|
---|
202 | else
|
---|
203 | AssertMsgFailed(("OpenService failed LastError=%Rwa\n", GetLastError()));
|
---|
204 | CloseServiceHandle(hSMgr);
|
---|
205 | }
|
---|
206 | return rc;
|
---|
207 | }
|
---|