1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox host drivers - USB drivers - Filter & driver installation
|
---|
4 | *
|
---|
5 | * Installation code
|
---|
6 | *
|
---|
7 | * Copyright (C) 2006-2009 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 <windows.h>
|
---|
23 | #include <setupapi.h>
|
---|
24 | #include <newdev.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/err.h>
|
---|
27 | #include <iprt/initterm.h>
|
---|
28 | #include <iprt/param.h>
|
---|
29 | #include <iprt/path.h>
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <VBox/err.h>
|
---|
33 | #include <stdio.h>
|
---|
34 |
|
---|
35 | int usblibOsCreateService(void);
|
---|
36 |
|
---|
37 | int __cdecl main(int argc, char **argv)
|
---|
38 | {
|
---|
39 | if (RTR3Init() != VINF_SUCCESS)
|
---|
40 | {
|
---|
41 | printf("Could not init IPRT!\n");
|
---|
42 | return 1;
|
---|
43 | }
|
---|
44 |
|
---|
45 | RTPrintf("USB installation\n");
|
---|
46 |
|
---|
47 | int rc = usblibOsCreateService();
|
---|
48 |
|
---|
49 | if (RT_SUCCESS(rc))
|
---|
50 | {
|
---|
51 | LPSTR lpszFilePart;
|
---|
52 | TCHAR szFullPath[MAX_PATH];
|
---|
53 | TCHAR szCurDir[MAX_PATH];
|
---|
54 | DWORD len;
|
---|
55 |
|
---|
56 | len = GetFullPathName(".\\VBoxUSB.inf", sizeof(szFullPath), szFullPath, &lpszFilePart);
|
---|
57 | Assert(len);
|
---|
58 |
|
---|
59 | if (GetCurrentDirectory(sizeof(szCurDir), szCurDir) == 0)
|
---|
60 | {
|
---|
61 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
62 | RTPrintf("GetCurrentDirectory failed with rc=%Rrc\n", rc);
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | /* Copy INF file to Windows\INF, so Windows will automatically install it when our USB device is detected */
|
---|
67 | BOOL b = SetupCopyOEMInf(szFullPath, NULL, SPOST_PATH, 0, NULL, 0, NULL, NULL);
|
---|
68 | if (b == FALSE)
|
---|
69 | {
|
---|
70 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
71 | RTPrintf("SetupCopyOEMInf failed with rc=%Rrc\n", rc);
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | RTPrintf("Installation successful.\n");
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** @todo RTR3Term(); */
|
---|
81 | return rc;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** The support service name. */
|
---|
85 | #define SERVICE_NAME "VBoxUSBMon"
|
---|
86 | /** Win32 Device name. */
|
---|
87 | #define DEVICE_NAME "\\\\.\\VBoxUSBMon"
|
---|
88 | /** NT Device name. */
|
---|
89 | #define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
|
---|
90 | /** Win32 Symlink name. */
|
---|
91 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Changes the USB driver service to specified driver path.
|
---|
96 | *
|
---|
97 | * @returns 0 on success.
|
---|
98 | * @returns < 0 on failure.
|
---|
99 | */
|
---|
100 | int usblibOsChangeService(const char *pszDriverPath)
|
---|
101 | {
|
---|
102 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
103 | DWORD dwLastError = GetLastError();
|
---|
104 | int rc = RTErrConvertFromWin32(dwLastError);
|
---|
105 | AssertPtr(pszDriverPath);
|
---|
106 | AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
|
---|
107 | if (hSMgrCreate)
|
---|
108 | {
|
---|
109 | SC_HANDLE hService = OpenService(hSMgrCreate,
|
---|
110 | SERVICE_NAME,
|
---|
111 | GENERIC_ALL);
|
---|
112 | DWORD dwLastError = GetLastError();
|
---|
113 | if (hService == NULL)
|
---|
114 | {
|
---|
115 | AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
|
---|
116 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | /* We only gonna change the driver image path, the rest remains like it already is */
|
---|
121 | if (ChangeServiceConfig(hService,
|
---|
122 | SERVICE_NO_CHANGE,
|
---|
123 | SERVICE_NO_CHANGE,
|
---|
124 | SERVICE_NO_CHANGE,
|
---|
125 | pszDriverPath,
|
---|
126 | NULL,
|
---|
127 | NULL,
|
---|
128 | NULL,
|
---|
129 | NULL,
|
---|
130 | NULL,
|
---|
131 | NULL))
|
---|
132 | {
|
---|
133 | RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
|
---|
138 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
139 | }
|
---|
140 | if (hService != NULL)
|
---|
141 | CloseServiceHandle(hService);
|
---|
142 | }
|
---|
143 |
|
---|
144 | CloseServiceHandle(hSMgrCreate);
|
---|
145 | }
|
---|
146 | return rc;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Creates the service.
|
---|
152 | *
|
---|
153 | * @returns 0 on success.
|
---|
154 | * @returns < 0 on failure.
|
---|
155 | */
|
---|
156 | int usblibOsCreateService(void)
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * Assume it didn't exist, so we'll create the service.
|
---|
160 | */
|
---|
161 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
162 | DWORD dwLastError = GetLastError();
|
---|
163 | int rc = RTErrConvertFromWin32(dwLastError);
|
---|
164 | AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
|
---|
165 | if (hSMgrCreate)
|
---|
166 | {
|
---|
167 | char szDriver[RTPATH_MAX];
|
---|
168 | int rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxUSBMon.sys"));
|
---|
169 | if (RT_SUCCESS(rc))
|
---|
170 | {
|
---|
171 | strcat(szDriver, "\\VBoxUSBMon.sys");
|
---|
172 | RTPrintf("Creating USB monitor driver service with path %s ...\n", szDriver);
|
---|
173 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
174 | SERVICE_NAME,
|
---|
175 | "VBox USB Monitor Driver",
|
---|
176 | SERVICE_QUERY_STATUS,
|
---|
177 | SERVICE_KERNEL_DRIVER,
|
---|
178 | SERVICE_DEMAND_START,
|
---|
179 | SERVICE_ERROR_NORMAL,
|
---|
180 | szDriver,
|
---|
181 | NULL, NULL, NULL, NULL, NULL);
|
---|
182 | DWORD dwLastError = GetLastError();
|
---|
183 | if (dwLastError == ERROR_SERVICE_EXISTS)
|
---|
184 | {
|
---|
185 | RTPrintf("USB monitor driver service already exists, skipping creation.\n");
|
---|
186 | rc = usblibOsChangeService(szDriver);
|
---|
187 | }
|
---|
188 | else
|
---|
189 | {
|
---|
190 | AssertMsg(hService, ("CreateService failed! LastError=%Rwa, szDriver=%s\n", dwLastError, szDriver));
|
---|
191 | rc = RTErrConvertFromWin32(dwLastError);
|
---|
192 | if (hService != NULL)
|
---|
193 | CloseServiceHandle(hService);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | CloseServiceHandle(hSMgrCreate);
|
---|
197 | }
|
---|
198 | return rc;
|
---|
199 | }
|
---|