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