1 | /* $Id: iokit.cpp 59374 2016-01-18 11:49:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Main - Darwin IOKit Routines.
|
---|
4 | *
|
---|
5 | * Because IOKit makes use of COM like interfaces, it does not mix very
|
---|
6 | * well with COM/XPCOM and must therefore be isolated from it using a
|
---|
7 | * simpler C interface.
|
---|
8 | */
|
---|
9 |
|
---|
10 | /*
|
---|
11 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
12 | *
|
---|
13 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | * available from http://www.virtualbox.org. This file is free software;
|
---|
15 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | * General Public License (GPL) as published by the Free Software
|
---|
17 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*********************************************************************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *********************************************************************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
27 | #ifdef STANDALONE_TESTCASE
|
---|
28 | # define VBOX_WITH_USB
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #include <mach/mach.h>
|
---|
32 | #include <Carbon/Carbon.h>
|
---|
33 | #include <IOKit/IOKitLib.h>
|
---|
34 | #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
|
---|
35 | #include <IOKit/scsi/SCSITaskLib.h>
|
---|
36 | #include <SystemConfiguration/SystemConfiguration.h>
|
---|
37 | #include <mach/mach_error.h>
|
---|
38 | #ifdef VBOX_WITH_USB
|
---|
39 | # include <IOKit/usb/IOUSBLib.h>
|
---|
40 | # include <IOKit/IOCFPlugIn.h>
|
---|
41 | # include <IOKit/storage/IOMedia.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <VBox/log.h>
|
---|
45 | #include <VBox/err.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/process.h>
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/thread.h>
|
---|
51 | #include <iprt/uuid.h>
|
---|
52 | #include <iprt/system.h>
|
---|
53 | #ifdef STANDALONE_TESTCASE
|
---|
54 | # include <iprt/initterm.h>
|
---|
55 | # include <iprt/stream.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #include "iokit.h"
|
---|
59 |
|
---|
60 | /* A small hack... */
|
---|
61 | #ifdef STANDALONE_TESTCASE
|
---|
62 | # define DarwinFreeUSBDeviceFromIOKit(a) do { } while (0)
|
---|
63 | #endif
|
---|
64 |
|
---|
65 |
|
---|
66 | /*********************************************************************************************************************************
|
---|
67 | * Defined Constants And Macros *
|
---|
68 | *********************************************************************************************************************************/
|
---|
69 | /** An attempt at catching reference leaks. */
|
---|
70 | #define MY_CHECK_CREFS(cRefs) do { AssertMsg(cRefs < 25, ("%ld\n", cRefs)); NOREF(cRefs); } while (0)
|
---|
71 |
|
---|
72 | /** Contains the pid of the current client. If 0, the kernel is the current client. */
|
---|
73 | #define VBOXUSB_CLIENT_KEY "VBoxUSB-Client"
|
---|
74 | /** Contains the pid of the filter owner (i.e. the VBoxSVC pid). */
|
---|
75 | #define VBOXUSB_OWNER_KEY "VBoxUSB-Owner"
|
---|
76 | /** The VBoxUSBDevice class name. */
|
---|
77 | #define VBOXUSBDEVICE_CLASS_NAME "org_virtualbox_VBoxUSBDevice"
|
---|
78 |
|
---|
79 | /** Define the constant for the IOUSBHostDevice class name added in El Capitan. */
|
---|
80 | #ifndef kIOUSBHostDeviceClassName
|
---|
81 | # define kIOUSBHostDeviceClassName "IOUSBHostDevice"
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | /** The major darwin version indicating OS X El Captian, used to take care of the USB changes. */
|
---|
85 | #define VBOX_OSX_EL_CAPTIAN_VER 15
|
---|
86 |
|
---|
87 | /*********************************************************************************************************************************
|
---|
88 | * Global Variables *
|
---|
89 | *********************************************************************************************************************************/
|
---|
90 | /** The IO Master Port. */
|
---|
91 | static mach_port_t g_MasterPort = NULL;
|
---|
92 | /** Major darwin version as returned by uname -r. */
|
---|
93 | uint32_t g_uMajorDarwin = 0;
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Lazily opens the master port.
|
---|
98 | *
|
---|
99 | * @returns true if the port is open, false on failure (very unlikely).
|
---|
100 | */
|
---|
101 | static bool darwinOpenMasterPort(void)
|
---|
102 | {
|
---|
103 | if (!g_MasterPort)
|
---|
104 | {
|
---|
105 | kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &g_MasterPort);
|
---|
106 | AssertReturn(krc == KERN_SUCCESS, false);
|
---|
107 |
|
---|
108 | /* Get the darwin version we are running on. */
|
---|
109 | char aszVersion[16] = { 0 };
|
---|
110 | int rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, &aszVersion[0], sizeof(aszVersion));
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | /* Make sure it is zero terminated (paranoia). */
|
---|
114 | aszVersion[15] = '\0';
|
---|
115 | rc = RTStrToUInt32Ex(&aszVersion[0], NULL, 10, &g_uMajorDarwin);
|
---|
116 | if ( rc != VINF_SUCCESS
|
---|
117 | && rc != VWRN_TRAILING_CHARS)
|
---|
118 | LogRel(("IOKit: Failed to convert the major part of the version string \"%s\" into an integer\n", &aszVersion[0]));
|
---|
119 | }
|
---|
120 | else
|
---|
121 | LogRel(("IOKit: Failed to query the OS release version with %Rrc\n", rc));
|
---|
122 | }
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Checks whether the value exists.
|
---|
129 | *
|
---|
130 | * @returns true / false accordingly.
|
---|
131 | * @param DictRef The dictionary.
|
---|
132 | * @param KeyStrRef The key name.
|
---|
133 | */
|
---|
134 | static bool darwinDictIsPresent(CFDictionaryRef DictRef, CFStringRef KeyStrRef)
|
---|
135 | {
|
---|
136 | return !!CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Gets a boolean value.
|
---|
142 | *
|
---|
143 | * @returns Success indicator (true/false).
|
---|
144 | * @param DictRef The dictionary.
|
---|
145 | * @param KeyStrRef The key name.
|
---|
146 | * @param pf Where to store the key value.
|
---|
147 | */
|
---|
148 | static bool darwinDictGetBool(CFDictionaryRef DictRef, CFStringRef KeyStrRef, bool *pf)
|
---|
149 | {
|
---|
150 | CFTypeRef BoolRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
151 | if ( BoolRef
|
---|
152 | && CFGetTypeID(BoolRef) == CFBooleanGetTypeID())
|
---|
153 | {
|
---|
154 | *pf = CFBooleanGetValue((CFBooleanRef)BoolRef);
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 | *pf = false;
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Gets an unsigned 8-bit integer value.
|
---|
164 | *
|
---|
165 | * @returns Success indicator (true/false).
|
---|
166 | * @param DictRef The dictionary.
|
---|
167 | * @param KeyStrRef The key name.
|
---|
168 | * @param pu8 Where to store the key value.
|
---|
169 | */
|
---|
170 | static bool darwinDictGetU8(CFDictionaryRef DictRef, CFStringRef KeyStrRef, uint8_t *pu8)
|
---|
171 | {
|
---|
172 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
173 | if (ValRef)
|
---|
174 | {
|
---|
175 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt8Type, pu8))
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 | *pu8 = 0;
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Gets an unsigned 16-bit integer value.
|
---|
185 | *
|
---|
186 | * @returns Success indicator (true/false).
|
---|
187 | * @param DictRef The dictionary.
|
---|
188 | * @param KeyStrRef The key name.
|
---|
189 | * @param pu16 Where to store the key value.
|
---|
190 | */
|
---|
191 | static bool darwinDictGetU16(CFDictionaryRef DictRef, CFStringRef KeyStrRef, uint16_t *pu16)
|
---|
192 | {
|
---|
193 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
194 | if (ValRef)
|
---|
195 | {
|
---|
196 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt16Type, pu16))
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 | *pu16 = 0;
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Gets an unsigned 32-bit integer value.
|
---|
206 | *
|
---|
207 | * @returns Success indicator (true/false).
|
---|
208 | * @param DictRef The dictionary.
|
---|
209 | * @param KeyStrRef The key name.
|
---|
210 | * @param pu32 Where to store the key value.
|
---|
211 | */
|
---|
212 | static bool darwinDictGetU32(CFDictionaryRef DictRef, CFStringRef KeyStrRef, uint32_t *pu32)
|
---|
213 | {
|
---|
214 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
215 | if (ValRef)
|
---|
216 | {
|
---|
217 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt32Type, pu32))
|
---|
218 | return true;
|
---|
219 | }
|
---|
220 | *pu32 = 0;
|
---|
221 | return false;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Gets an unsigned 64-bit integer value.
|
---|
227 | *
|
---|
228 | * @returns Success indicator (true/false).
|
---|
229 | * @param DictRef The dictionary.
|
---|
230 | * @param KeyStrRef The key name.
|
---|
231 | * @param pu64 Where to store the key value.
|
---|
232 | */
|
---|
233 | static bool darwinDictGetU64(CFDictionaryRef DictRef, CFStringRef KeyStrRef, uint64_t *pu64)
|
---|
234 | {
|
---|
235 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
236 | if (ValRef)
|
---|
237 | {
|
---|
238 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt64Type, pu64))
|
---|
239 | return true;
|
---|
240 | }
|
---|
241 | *pu64 = 0;
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Gets a RTPROCESS value.
|
---|
248 | *
|
---|
249 | * @returns Success indicator (true/false).
|
---|
250 | * @param DictRef The dictionary.
|
---|
251 | * @param KeyStrRef The key name.
|
---|
252 | * @param pProcess Where to store the key value.
|
---|
253 | */
|
---|
254 | static bool darwinDictGetProcess(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, PRTPROCESS pProcess)
|
---|
255 | {
|
---|
256 | switch (sizeof(*pProcess))
|
---|
257 | {
|
---|
258 | case sizeof(uint16_t): return darwinDictGetU16(DictRef, KeyStrRef, (uint16_t *)pProcess);
|
---|
259 | case sizeof(uint32_t): return darwinDictGetU32(DictRef, KeyStrRef, (uint32_t *)pProcess);
|
---|
260 | case sizeof(uint64_t): return darwinDictGetU64(DictRef, KeyStrRef, (uint64_t *)pProcess);
|
---|
261 | default:
|
---|
262 | AssertMsgFailedReturn(("%d\n", sizeof(*pProcess)), false);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Gets string value, converted to UTF-8 and put in user buffer.
|
---|
269 | *
|
---|
270 | * @returns Success indicator (true/false).
|
---|
271 | * @param DictRef The dictionary.
|
---|
272 | * @param KeyStrRef The key name.
|
---|
273 | * @param psz The string buffer. On failure this will be an empty string ("").
|
---|
274 | * @param cch The size of the buffer.
|
---|
275 | */
|
---|
276 | static bool darwinDictGetString(CFDictionaryRef DictRef, CFStringRef KeyStrRef, char *psz, size_t cch)
|
---|
277 | {
|
---|
278 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
279 | if (ValRef)
|
---|
280 | {
|
---|
281 | if (CFStringGetCString((CFStringRef)ValRef, psz, cch, kCFStringEncodingUTF8))
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 | Assert(cch > 0);
|
---|
285 | *psz = '\0';
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Gets string value, converted to UTF-8 and put in a IPRT string buffer.
|
---|
292 | *
|
---|
293 | * @returns Success indicator (true/false).
|
---|
294 | * @param DictRef The dictionary.
|
---|
295 | * @param KeyStrRef The key name.
|
---|
296 | * @param ppsz Where to store the key value. Free with RTStrFree. Set to NULL on failure.
|
---|
297 | */
|
---|
298 | static bool darwinDictDupString(CFDictionaryRef DictRef, CFStringRef KeyStrRef, char **ppsz)
|
---|
299 | {
|
---|
300 | char szBuf[512];
|
---|
301 | if (darwinDictGetString(DictRef, KeyStrRef, szBuf, sizeof(szBuf)))
|
---|
302 | {
|
---|
303 | *ppsz = RTStrDup(szBuf);
|
---|
304 | if (*ppsz)
|
---|
305 | return true;
|
---|
306 | }
|
---|
307 | *ppsz = NULL;
|
---|
308 | return false;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Gets a byte string (data) of a specific size.
|
---|
314 | *
|
---|
315 | * @returns Success indicator (true/false).
|
---|
316 | * @param DictRef The dictionary.
|
---|
317 | * @param KeyStrRef The key name.
|
---|
318 | * @param pvBuf The buffer to store the bytes in.
|
---|
319 | * @param cbBuf The size of the buffer. This must exactly match the data size.
|
---|
320 | */
|
---|
321 | static bool darwinDictGetData(CFDictionaryRef DictRef, CFStringRef KeyStrRef, void *pvBuf, size_t cbBuf)
|
---|
322 | {
|
---|
323 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
324 | if (ValRef)
|
---|
325 | {
|
---|
326 | CFIndex cbActual = CFDataGetLength((CFDataRef)ValRef);
|
---|
327 | if (cbActual >= 0 && cbBuf == (size_t)cbActual)
|
---|
328 | {
|
---|
329 | CFDataGetBytes((CFDataRef)ValRef, CFRangeMake(0, cbBuf), (uint8_t *)pvBuf);
|
---|
330 | return true;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | memset(pvBuf, '\0', cbBuf);
|
---|
334 | return false;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | #if 1 && !defined(STANDALONE_TESTCASE) /* dumping disabled */
|
---|
339 | # define DARWIN_IOKIT_LOG(a) Log(a)
|
---|
340 | # define DARWIN_IOKIT_LOG_FLUSH() do {} while (0)
|
---|
341 | # define DARWIN_IOKIT_DUMP_OBJ(o) do {} while (0)
|
---|
342 | #else
|
---|
343 | # if defined(STANDALONE_TESTCASE)
|
---|
344 | # include <iprt/stream.h>
|
---|
345 | # define DARWIN_IOKIT_LOG(a) RTPrintf a
|
---|
346 | # define DARWIN_IOKIT_LOG_FLUSH() RTStrmFlush(g_pStdOut)
|
---|
347 | # else
|
---|
348 | # define DARWIN_IOKIT_LOG(a) RTLogPrintf a
|
---|
349 | # define DARWIN_IOKIT_LOG_FLUSH() RTLogFlush(NULL)
|
---|
350 | # endif
|
---|
351 | # define DARWIN_IOKIT_DUMP_OBJ(o) darwinDumpObj(o)
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Callback for dumping a dictionary key.
|
---|
355 | *
|
---|
356 | * @param pvKey The key name.
|
---|
357 | * @param pvValue The key value
|
---|
358 | * @param pvUser The recursion depth.
|
---|
359 | */
|
---|
360 | static void darwinDumpDictCallback(const void *pvKey, const void *pvValue, void *pvUser)
|
---|
361 | {
|
---|
362 | /* display the key name. */
|
---|
363 | char *pszKey = (char *)RTMemTmpAlloc(1024);
|
---|
364 | if (!CFStringGetCString((CFStringRef)pvKey, pszKey, 1024, kCFStringEncodingUTF8))
|
---|
365 | strcpy(pszKey, "CFStringGetCString failure");
|
---|
366 | DARWIN_IOKIT_LOG(("%+*s%s", (int)(uintptr_t)pvUser, "", pszKey));
|
---|
367 | RTMemTmpFree(pszKey);
|
---|
368 |
|
---|
369 | /* display the value type */
|
---|
370 | CFTypeID Type = CFGetTypeID(pvValue);
|
---|
371 | DARWIN_IOKIT_LOG((" [%d-", Type));
|
---|
372 |
|
---|
373 | /* display the value */
|
---|
374 | if (Type == CFDictionaryGetTypeID())
|
---|
375 | {
|
---|
376 | DARWIN_IOKIT_LOG(("dictionary] =\n"
|
---|
377 | "%-*s{\n", (int)(uintptr_t)pvUser, ""));
|
---|
378 | CFDictionaryApplyFunction((CFDictionaryRef)pvValue, darwinDumpDictCallback, (void *)((uintptr_t)pvUser + 4));
|
---|
379 | DARWIN_IOKIT_LOG(("%-*s}\n", (int)(uintptr_t)pvUser, ""));
|
---|
380 | }
|
---|
381 | else if (Type == CFBooleanGetTypeID())
|
---|
382 | DARWIN_IOKIT_LOG(("bool] = %s\n", CFBooleanGetValue((CFBooleanRef)pvValue) ? "true" : "false"));
|
---|
383 | else if (Type == CFNumberGetTypeID())
|
---|
384 | {
|
---|
385 | union
|
---|
386 | {
|
---|
387 | SInt8 s8;
|
---|
388 | SInt16 s16;
|
---|
389 | SInt32 s32;
|
---|
390 | SInt64 s64;
|
---|
391 | Float32 rf32;
|
---|
392 | Float64 rd64;
|
---|
393 | char ch;
|
---|
394 | short s;
|
---|
395 | int i;
|
---|
396 | long l;
|
---|
397 | long long ll;
|
---|
398 | float rf;
|
---|
399 | double rd;
|
---|
400 | CFIndex iCF;
|
---|
401 | } u;
|
---|
402 | RT_ZERO(u);
|
---|
403 | CFNumberType NumType = CFNumberGetType((CFNumberRef)pvValue);
|
---|
404 | if (CFNumberGetValue((CFNumberRef)pvValue, NumType, &u))
|
---|
405 | {
|
---|
406 | switch (CFNumberGetType((CFNumberRef)pvValue))
|
---|
407 | {
|
---|
408 | case kCFNumberSInt8Type: DARWIN_IOKIT_LOG(("SInt8] = %RI8 (%#RX8)\n", NumType, u.s8, u.s8)); break;
|
---|
409 | case kCFNumberSInt16Type: DARWIN_IOKIT_LOG(("SInt16] = %RI16 (%#RX16)\n", NumType, u.s16, u.s16)); break;
|
---|
410 | case kCFNumberSInt32Type: DARWIN_IOKIT_LOG(("SInt32] = %RI32 (%#RX32)\n", NumType, u.s32, u.s32)); break;
|
---|
411 | case kCFNumberSInt64Type: DARWIN_IOKIT_LOG(("SInt64] = %RI64 (%#RX64)\n", NumType, u.s64, u.s64)); break;
|
---|
412 | case kCFNumberFloat32Type: DARWIN_IOKIT_LOG(("float32] = %#lx\n", NumType, u.l)); break;
|
---|
413 | case kCFNumberFloat64Type: DARWIN_IOKIT_LOG(("float64] = %#llx\n", NumType, u.ll)); break;
|
---|
414 | case kCFNumberFloatType: DARWIN_IOKIT_LOG(("float] = %#lx\n", NumType, u.l)); break;
|
---|
415 | case kCFNumberDoubleType: DARWIN_IOKIT_LOG(("double] = %#llx\n", NumType, u.ll)); break;
|
---|
416 | case kCFNumberCharType: DARWIN_IOKIT_LOG(("char] = %hhd (%hhx)\n", NumType, u.ch, u.ch)); break;
|
---|
417 | case kCFNumberShortType: DARWIN_IOKIT_LOG(("short] = %hd (%hx)\n", NumType, u.s, u.s)); break;
|
---|
418 | case kCFNumberIntType: DARWIN_IOKIT_LOG(("int] = %d (%#x)\n", NumType, u.i, u.i)); break;
|
---|
419 | case kCFNumberLongType: DARWIN_IOKIT_LOG(("long] = %ld (%#lx)\n", NumType, u.l, u.l)); break;
|
---|
420 | case kCFNumberLongLongType: DARWIN_IOKIT_LOG(("long long] = %lld (%#llx)\n", NumType, u.ll, u.ll)); break;
|
---|
421 | case kCFNumberCFIndexType: DARWIN_IOKIT_LOG(("CFIndex] = %lld (%#llx)\n", NumType, (long long)u.iCF, (long long)u.iCF)); break;
|
---|
422 | break;
|
---|
423 | default: DARWIN_IOKIT_LOG(("%d?] = %lld (%llx)\n", NumType, u.ll, u.ll)); break;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | else
|
---|
427 | DARWIN_IOKIT_LOG(("number] = CFNumberGetValue failed\n"));
|
---|
428 | }
|
---|
429 | else if (Type == CFBooleanGetTypeID())
|
---|
430 | DARWIN_IOKIT_LOG(("boolean] = %RTbool\n", CFBooleanGetValue((CFBooleanRef)pvValue)));
|
---|
431 | else if (Type == CFStringGetTypeID())
|
---|
432 | {
|
---|
433 | DARWIN_IOKIT_LOG(("string] = "));
|
---|
434 | char *pszValue = (char *)RTMemTmpAlloc(16*_1K);
|
---|
435 | if (!CFStringGetCString((CFStringRef)pvValue, pszValue, 16*_1K, kCFStringEncodingUTF8))
|
---|
436 | strcpy(pszValue, "CFStringGetCString failure");
|
---|
437 | DARWIN_IOKIT_LOG(("\"%s\"\n", pszValue));
|
---|
438 | RTMemTmpFree(pszValue);
|
---|
439 | }
|
---|
440 | else if (Type == CFDataGetTypeID())
|
---|
441 | {
|
---|
442 | CFIndex cb = CFDataGetLength((CFDataRef)pvValue);
|
---|
443 | DARWIN_IOKIT_LOG(("%zu bytes] =", (size_t)cb));
|
---|
444 | void *pvData = RTMemTmpAlloc(cb + 8);
|
---|
445 | CFDataGetBytes((CFDataRef)pvValue, CFRangeMake(0, cb), (uint8_t *)pvData);
|
---|
446 | if (!cb)
|
---|
447 | DARWIN_IOKIT_LOG((" \n"));
|
---|
448 | else if (cb <= 32)
|
---|
449 | DARWIN_IOKIT_LOG((" %.*Rhxs\n", cb, pvData));
|
---|
450 | else
|
---|
451 | DARWIN_IOKIT_LOG(("\n%.*Rhxd\n", cb, pvData));
|
---|
452 | RTMemTmpFree(pvData);
|
---|
453 | }
|
---|
454 | else
|
---|
455 | DARWIN_IOKIT_LOG(("??] = %p\n", pvValue));
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Dumps a dictionary to the log.
|
---|
461 | *
|
---|
462 | * @param DictRef The dictionary to dump.
|
---|
463 | */
|
---|
464 | static void darwinDumpDict(CFDictionaryRef DictRef, unsigned cIndents)
|
---|
465 | {
|
---|
466 | CFDictionaryApplyFunction(DictRef, darwinDumpDictCallback, (void *)(uintptr_t)cIndents);
|
---|
467 | DARWIN_IOKIT_LOG_FLUSH();
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Dumps an I/O kit registry object and all it children.
|
---|
473 | * @param Object The object to dump.
|
---|
474 | * @param cIndents The number of indents to use.
|
---|
475 | */
|
---|
476 | static void darwinDumpObjInt(io_object_t Object, unsigned cIndents)
|
---|
477 | {
|
---|
478 | static io_string_t s_szPath;
|
---|
479 | kern_return_t krc = IORegistryEntryGetPath(Object, kIOServicePlane, s_szPath);
|
---|
480 | if (krc != KERN_SUCCESS)
|
---|
481 | strcpy(s_szPath, "IORegistryEntryGetPath failed");
|
---|
482 | DARWIN_IOKIT_LOG(("Dumping %p - %s:\n", (const void *)Object, s_szPath));
|
---|
483 |
|
---|
484 | CFMutableDictionaryRef PropsRef = 0;
|
---|
485 | krc = IORegistryEntryCreateCFProperties(Object, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
486 | if (krc == KERN_SUCCESS)
|
---|
487 | {
|
---|
488 | darwinDumpDict(PropsRef, cIndents + 4);
|
---|
489 | CFRelease(PropsRef);
|
---|
490 | }
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * Children.
|
---|
494 | */
|
---|
495 | io_iterator_t Children;
|
---|
496 | krc = IORegistryEntryGetChildIterator(Object, kIOServicePlane, &Children);
|
---|
497 | if (krc == KERN_SUCCESS)
|
---|
498 | {
|
---|
499 | io_object_t Child;
|
---|
500 | while ((Child = IOIteratorNext(Children)))
|
---|
501 | {
|
---|
502 | darwinDumpObjInt(Child, cIndents + 4);
|
---|
503 | IOObjectRelease(Child);
|
---|
504 | }
|
---|
505 | IOObjectRelease(Children);
|
---|
506 | }
|
---|
507 | else
|
---|
508 | DARWIN_IOKIT_LOG(("IORegistryEntryGetChildIterator -> %#x\n", krc));
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Dumps an I/O kit registry object and all it children.
|
---|
513 | * @param Object The object to dump.
|
---|
514 | */
|
---|
515 | static void darwinDumpObj(io_object_t Object)
|
---|
516 | {
|
---|
517 | darwinDumpObjInt(Object, 0);
|
---|
518 | }
|
---|
519 |
|
---|
520 | #endif /* helpers for dumping registry dictionaries */
|
---|
521 |
|
---|
522 |
|
---|
523 | #ifdef VBOX_WITH_USB
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Notification data created by DarwinSubscribeUSBNotifications, used by
|
---|
527 | * the callbacks and finally freed by DarwinUnsubscribeUSBNotifications.
|
---|
528 | */
|
---|
529 | typedef struct DARWINUSBNOTIFY
|
---|
530 | {
|
---|
531 | /** The notification port.
|
---|
532 | * It's shared between the notification callbacks. */
|
---|
533 | IONotificationPortRef NotifyPort;
|
---|
534 | /** The run loop source for NotifyPort. */
|
---|
535 | CFRunLoopSourceRef NotifyRLSrc;
|
---|
536 | /** The attach notification iterator. */
|
---|
537 | io_iterator_t AttachIterator;
|
---|
538 | /** The 2nd attach notification iterator. */
|
---|
539 | io_iterator_t AttachIterator2;
|
---|
540 | /** The detach notification iterator. */
|
---|
541 | io_iterator_t DetachIterator;
|
---|
542 | } DARWINUSBNOTIFY, *PDARWINUSBNOTIFY;
|
---|
543 |
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Run thru an iterator.
|
---|
547 | *
|
---|
548 | * The docs says this is necessary to start getting notifications,
|
---|
549 | * so this function is called in the callbacks and right after
|
---|
550 | * registering the notification.
|
---|
551 | *
|
---|
552 | * @param pIterator The iterator reference.
|
---|
553 | */
|
---|
554 | static void darwinDrainIterator(io_iterator_t pIterator)
|
---|
555 | {
|
---|
556 | io_object_t Object;
|
---|
557 | while ((Object = IOIteratorNext(pIterator)))
|
---|
558 | {
|
---|
559 | DARWIN_IOKIT_DUMP_OBJ(Object);
|
---|
560 | IOObjectRelease(Object);
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 |
|
---|
565 | /**
|
---|
566 | * Callback for the 1st attach notification.
|
---|
567 | *
|
---|
568 | * @param pvNotify Our data.
|
---|
569 | * @param NotifyIterator The notification iterator.
|
---|
570 | */
|
---|
571 | static void darwinUSBAttachNotification1(void *pvNotify, io_iterator_t NotifyIterator)
|
---|
572 | {
|
---|
573 | DARWIN_IOKIT_LOG(("USB Attach Notification1\n"));
|
---|
574 | NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
|
---|
575 | darwinDrainIterator(NotifyIterator);
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Callback for the 2nd attach notification.
|
---|
581 | *
|
---|
582 | * @param pvNotify Our data.
|
---|
583 | * @param NotifyIterator The notification iterator.
|
---|
584 | */
|
---|
585 | static void darwinUSBAttachNotification2(void *pvNotify, io_iterator_t NotifyIterator)
|
---|
586 | {
|
---|
587 | DARWIN_IOKIT_LOG(("USB Attach Notification2\n"));
|
---|
588 | NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
|
---|
589 | darwinDrainIterator(NotifyIterator);
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Callback for the detach notifications.
|
---|
595 | *
|
---|
596 | * @param pvNotify Our data.
|
---|
597 | * @param NotifyIterator The notification iterator.
|
---|
598 | */
|
---|
599 | static void darwinUSBDetachNotification(void *pvNotify, io_iterator_t NotifyIterator)
|
---|
600 | {
|
---|
601 | DARWIN_IOKIT_LOG(("USB Detach Notification\n"));
|
---|
602 | NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
|
---|
603 | darwinDrainIterator(NotifyIterator);
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * Subscribes the run loop to USB notification events relevant to
|
---|
609 | * device attach/detach.
|
---|
610 | *
|
---|
611 | * The source mode for these events is defined as VBOX_IOKIT_MODE_STRING
|
---|
612 | * so that the caller can listen to events from this mode only and
|
---|
613 | * re-evalutate the list of attached devices whenever an event arrives.
|
---|
614 | *
|
---|
615 | * @returns opaque for passing to the unsubscribe function. If NULL
|
---|
616 | * something unexpectedly failed during subscription.
|
---|
617 | */
|
---|
618 | void *DarwinSubscribeUSBNotifications(void)
|
---|
619 | {
|
---|
620 | AssertReturn(darwinOpenMasterPort(), NULL);
|
---|
621 |
|
---|
622 | PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)RTMemAllocZ(sizeof(*pNotify));
|
---|
623 | AssertReturn(pNotify, NULL);
|
---|
624 |
|
---|
625 | /*
|
---|
626 | * Create the notification port, bake it into a runloop source which we
|
---|
627 | * then add to our run loop.
|
---|
628 | */
|
---|
629 | pNotify->NotifyPort = IONotificationPortCreate(g_MasterPort);
|
---|
630 | Assert(pNotify->NotifyPort);
|
---|
631 | if (pNotify->NotifyPort)
|
---|
632 | {
|
---|
633 | pNotify->NotifyRLSrc = IONotificationPortGetRunLoopSource(pNotify->NotifyPort);
|
---|
634 | Assert(pNotify->NotifyRLSrc);
|
---|
635 | if (pNotify->NotifyRLSrc)
|
---|
636 | {
|
---|
637 | CFRunLoopRef RunLoopRef = CFRunLoopGetCurrent();
|
---|
638 | CFRetain(RunLoopRef); /* Workaround for crash when cleaning up the TLS / runloop((sub)mode). See @bugref{2807}. */
|
---|
639 | CFRunLoopAddSource(RunLoopRef, pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
|
---|
640 |
|
---|
641 | /*
|
---|
642 | * Create the notification callbacks.
|
---|
643 | */
|
---|
644 | kern_return_t rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
|
---|
645 | kIOPublishNotification,
|
---|
646 | IOServiceMatching(kIOUSBDeviceClassName),
|
---|
647 | darwinUSBAttachNotification1,
|
---|
648 | pNotify,
|
---|
649 | &pNotify->AttachIterator);
|
---|
650 | if (rc == KERN_SUCCESS)
|
---|
651 | {
|
---|
652 | darwinDrainIterator(pNotify->AttachIterator);
|
---|
653 | rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
|
---|
654 | kIOMatchedNotification,
|
---|
655 | IOServiceMatching(kIOUSBDeviceClassName),
|
---|
656 | darwinUSBAttachNotification2,
|
---|
657 | pNotify,
|
---|
658 | &pNotify->AttachIterator2);
|
---|
659 | if (rc == KERN_SUCCESS)
|
---|
660 | {
|
---|
661 | darwinDrainIterator(pNotify->AttachIterator2);
|
---|
662 | rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
|
---|
663 | kIOTerminatedNotification,
|
---|
664 | IOServiceMatching(kIOUSBDeviceClassName),
|
---|
665 | darwinUSBDetachNotification,
|
---|
666 | pNotify,
|
---|
667 | &pNotify->DetachIterator);
|
---|
668 | {
|
---|
669 | darwinDrainIterator(pNotify->DetachIterator);
|
---|
670 | return pNotify;
|
---|
671 | }
|
---|
672 | IOObjectRelease(pNotify->AttachIterator2);
|
---|
673 | }
|
---|
674 | IOObjectRelease(pNotify->AttachIterator);
|
---|
675 | }
|
---|
676 | CFRunLoopRemoveSource(RunLoopRef, pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
|
---|
677 | }
|
---|
678 | IONotificationPortDestroy(pNotify->NotifyPort);
|
---|
679 | }
|
---|
680 |
|
---|
681 | RTMemFree(pNotify);
|
---|
682 | return NULL;
|
---|
683 | }
|
---|
684 |
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Unsubscribe the run loop from USB notification subscribed to
|
---|
688 | * by DarwinSubscribeUSBNotifications.
|
---|
689 | *
|
---|
690 | * @param pvOpaque The return value from DarwinSubscribeUSBNotifications.
|
---|
691 | */
|
---|
692 | void DarwinUnsubscribeUSBNotifications(void *pvOpaque)
|
---|
693 | {
|
---|
694 | PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvOpaque;
|
---|
695 | if (!pNotify)
|
---|
696 | return;
|
---|
697 |
|
---|
698 | IOObjectRelease(pNotify->AttachIterator);
|
---|
699 | pNotify->AttachIterator = NULL;
|
---|
700 | IOObjectRelease(pNotify->AttachIterator2);
|
---|
701 | pNotify->AttachIterator2 = NULL;
|
---|
702 | IOObjectRelease(pNotify->DetachIterator);
|
---|
703 | pNotify->DetachIterator = NULL;
|
---|
704 |
|
---|
705 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
|
---|
706 | IONotificationPortDestroy(pNotify->NotifyPort);
|
---|
707 | pNotify->NotifyRLSrc = NULL;
|
---|
708 | pNotify->NotifyPort = NULL;
|
---|
709 |
|
---|
710 | RTMemFree(pNotify);
|
---|
711 | }
|
---|
712 |
|
---|
713 |
|
---|
714 | /**
|
---|
715 | * Descends recursively into a IORegistry tree locating the first object of a given class.
|
---|
716 | *
|
---|
717 | * The search is performed depth first.
|
---|
718 | *
|
---|
719 | * @returns Object reference if found, NULL if not.
|
---|
720 | * @param Object The current tree root.
|
---|
721 | * @param pszClass The name of the class we're looking for.
|
---|
722 | * @param pszNameBuf A scratch buffer for query the class name in to avoid
|
---|
723 | * wasting 128 bytes on an io_name_t object for every recursion.
|
---|
724 | */
|
---|
725 | static io_object_t darwinFindObjectByClass(io_object_t Object, const char *pszClass, io_name_t pszNameBuf)
|
---|
726 | {
|
---|
727 | io_iterator_t Children;
|
---|
728 | kern_return_t krc = IORegistryEntryGetChildIterator(Object, kIOServicePlane, &Children);
|
---|
729 | if (krc != KERN_SUCCESS)
|
---|
730 | return NULL;
|
---|
731 | io_object_t Child;
|
---|
732 | while ((Child = IOIteratorNext(Children)))
|
---|
733 | {
|
---|
734 | krc = IOObjectGetClass(Child, pszNameBuf);
|
---|
735 | if ( krc == KERN_SUCCESS
|
---|
736 | && !strcmp(pszNameBuf, pszClass))
|
---|
737 | break;
|
---|
738 |
|
---|
739 | io_object_t GrandChild = darwinFindObjectByClass(Child, pszClass, pszNameBuf);
|
---|
740 | IOObjectRelease(Child);
|
---|
741 | if (GrandChild)
|
---|
742 | {
|
---|
743 | Child = GrandChild;
|
---|
744 | break;
|
---|
745 | }
|
---|
746 | }
|
---|
747 | IOObjectRelease(Children);
|
---|
748 | return Child;
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Descends recursively into IOUSBMassStorageClass tree to check whether
|
---|
754 | * the MSD is mounted or not.
|
---|
755 | *
|
---|
756 | * The current heuristic is to look for the IOMedia class.
|
---|
757 | *
|
---|
758 | * @returns true if mounted, false if not.
|
---|
759 | * @param MSDObj The IOUSBMassStorageClass object.
|
---|
760 | * @param pszNameBuf A scratch buffer for query the class name in to avoid
|
---|
761 | * wasting 128 bytes on an io_name_t object for every recursion.
|
---|
762 | */
|
---|
763 | static bool darwinIsMassStorageInterfaceInUse(io_object_t MSDObj, io_name_t pszNameBuf)
|
---|
764 | {
|
---|
765 | io_object_t MediaObj = darwinFindObjectByClass(MSDObj, kIOMediaClass, pszNameBuf);
|
---|
766 | if (MediaObj)
|
---|
767 | {
|
---|
768 | CFMutableDictionaryRef pProperties;
|
---|
769 | kern_return_t krc;
|
---|
770 | bool fInUse = true;
|
---|
771 |
|
---|
772 | krc = IORegistryEntryCreateCFProperties(MediaObj, &pProperties, kCFAllocatorDefault, kNilOptions);
|
---|
773 | if (krc == KERN_SUCCESS)
|
---|
774 | {
|
---|
775 | CFBooleanRef pBoolValue = (CFBooleanRef)CFDictionaryGetValue(pProperties, CFSTR(kIOMediaOpenKey));
|
---|
776 | if (pBoolValue)
|
---|
777 | fInUse = CFBooleanGetValue(pBoolValue);
|
---|
778 |
|
---|
779 | CFRelease(pProperties);
|
---|
780 | }
|
---|
781 |
|
---|
782 | /* more checks? */
|
---|
783 | IOObjectRelease(MediaObj);
|
---|
784 | return fInUse;
|
---|
785 | }
|
---|
786 |
|
---|
787 | return false;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Finds the matching IOUSBHostDevice registry entry for the given legacy USB device interface (IOUSBDevice).
|
---|
792 | *
|
---|
793 | * @returns kern_return_t error code.
|
---|
794 | * @param USBDeviceLegacy The legacy device I/O Kit object.
|
---|
795 | * @param pUSBDevice Where to store the IOUSBHostDevice object on success.
|
---|
796 | */
|
---|
797 | static kern_return_t darwinGetUSBHostDeviceFromLegacyDevice(io_object_t USBDeviceLegacy, io_object_t *pUSBDevice)
|
---|
798 | {
|
---|
799 | kern_return_t krc = KERN_SUCCESS;
|
---|
800 | uint64_t uIoRegEntryId = 0;
|
---|
801 |
|
---|
802 | *pUSBDevice = 0;
|
---|
803 |
|
---|
804 | /* Get the registry entry ID to match against. */
|
---|
805 | krc = IORegistryEntryGetRegistryEntryID(USBDeviceLegacy, &uIoRegEntryId);
|
---|
806 | if (krc != KERN_SUCCESS)
|
---|
807 | return krc;
|
---|
808 |
|
---|
809 | /*
|
---|
810 | * Create a matching dictionary for searching for USB Devices in the IOKit.
|
---|
811 | */
|
---|
812 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBHostDeviceClassName);
|
---|
813 | AssertReturn(RefMatchingDict, KERN_FAILURE);
|
---|
814 |
|
---|
815 | /*
|
---|
816 | * Perform the search and get a collection of USB Device back.
|
---|
817 | */
|
---|
818 | io_iterator_t USBDevices = NULL;
|
---|
819 | IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
|
---|
820 | AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), KERN_FAILURE);
|
---|
821 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
822 |
|
---|
823 | /*
|
---|
824 | * Walk the devices and check for the matching alternate registry entry ID.
|
---|
825 | */
|
---|
826 | io_object_t USBDevice;
|
---|
827 | while ((USBDevice = IOIteratorNext(USBDevices)) != 0)
|
---|
828 | {
|
---|
829 | DARWIN_IOKIT_DUMP_OBJ(USBDevice);
|
---|
830 |
|
---|
831 | CFMutableDictionaryRef PropsRef = 0;
|
---|
832 | krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
833 | if (krc == KERN_SUCCESS)
|
---|
834 | {
|
---|
835 | uint64_t uAltRegId = 0;
|
---|
836 | if ( darwinDictGetU64(PropsRef, CFSTR("AppleUSBAlternateServiceRegistryID"), &uAltRegId)
|
---|
837 | && uAltRegId == uIoRegEntryId)
|
---|
838 | {
|
---|
839 | *pUSBDevice = USBDevice;
|
---|
840 | CFRelease(PropsRef);
|
---|
841 | break;
|
---|
842 | }
|
---|
843 |
|
---|
844 | CFRelease(PropsRef);
|
---|
845 | }
|
---|
846 | IOObjectRelease(USBDevice);
|
---|
847 | }
|
---|
848 | IOObjectRelease(USBDevices);
|
---|
849 |
|
---|
850 | return krc;
|
---|
851 | }
|
---|
852 |
|
---|
853 | static bool darwinUSBDeviceIsGrabbedDetermineState(PUSBDEVICE pCur, io_object_t USBDevice)
|
---|
854 | {
|
---|
855 | /*
|
---|
856 | * Iterate the interfaces (among the children of the IOUSBDevice object).
|
---|
857 | */
|
---|
858 | io_iterator_t Interfaces;
|
---|
859 | kern_return_t krc = IORegistryEntryGetChildIterator(USBDevice, kIOServicePlane, &Interfaces);
|
---|
860 | if (krc != KERN_SUCCESS)
|
---|
861 | return false;
|
---|
862 |
|
---|
863 | bool fHaveOwner = false;
|
---|
864 | RTPROCESS Owner = NIL_RTPROCESS;
|
---|
865 | bool fHaveClient = false;
|
---|
866 | RTPROCESS Client = NIL_RTPROCESS;
|
---|
867 | io_object_t Interface;
|
---|
868 | while ((Interface = IOIteratorNext(Interfaces)) != 0)
|
---|
869 | {
|
---|
870 | io_name_t szName;
|
---|
871 | krc = IOObjectGetClass(Interface, szName);
|
---|
872 | if ( krc == KERN_SUCCESS
|
---|
873 | && !strcmp(szName, VBOXUSBDEVICE_CLASS_NAME))
|
---|
874 | {
|
---|
875 | CFMutableDictionaryRef PropsRef = 0;
|
---|
876 | krc = IORegistryEntryCreateCFProperties(Interface, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
877 | if (krc == KERN_SUCCESS)
|
---|
878 | {
|
---|
879 | fHaveOwner = darwinDictGetProcess(PropsRef, CFSTR(VBOXUSB_OWNER_KEY), &Owner);
|
---|
880 | fHaveClient = darwinDictGetProcess(PropsRef, CFSTR(VBOXUSB_CLIENT_KEY), &Client);
|
---|
881 | CFRelease(PropsRef);
|
---|
882 | }
|
---|
883 | }
|
---|
884 |
|
---|
885 | IOObjectRelease(Interface);
|
---|
886 | }
|
---|
887 | IOObjectRelease(Interfaces);
|
---|
888 |
|
---|
889 | /*
|
---|
890 | * Calc the status.
|
---|
891 | */
|
---|
892 | if (fHaveOwner)
|
---|
893 | {
|
---|
894 | if (Owner == RTProcSelf())
|
---|
895 | pCur->enmState = !fHaveClient || Client == NIL_RTPROCESS || !Client
|
---|
896 | ? USBDEVICESTATE_HELD_BY_PROXY
|
---|
897 | : USBDEVICESTATE_USED_BY_GUEST;
|
---|
898 | else
|
---|
899 | pCur->enmState = USBDEVICESTATE_USED_BY_HOST;
|
---|
900 | }
|
---|
901 |
|
---|
902 | return fHaveOwner;
|
---|
903 | }
|
---|
904 |
|
---|
905 | /**
|
---|
906 | * Worker for determining the USB device state for devices which are not captured by the VBoxUSB driver
|
---|
907 | * Works for both, IOUSBDevice (legacy on release >= El Capitan) and IOUSBHostDevice (available on >= El Capitan).
|
---|
908 | *
|
---|
909 | * @returns nothing.
|
---|
910 | * @param pCur The USB device data.
|
---|
911 | * @param USBDevice I/O Kit USB device object (either IOUSBDevice or IOUSBHostDevice).
|
---|
912 | */
|
---|
913 | static void darwinDetermineUSBDeviceStateWorker(PUSBDEVICE pCur, io_object_t USBDevice)
|
---|
914 | {
|
---|
915 | /*
|
---|
916 | * Iterate the interfaces (among the children of the IOUSBDevice object).
|
---|
917 | */
|
---|
918 | io_iterator_t Interfaces;
|
---|
919 | kern_return_t krc = IORegistryEntryGetChildIterator(USBDevice, kIOServicePlane, &Interfaces);
|
---|
920 | if (krc != KERN_SUCCESS)
|
---|
921 | return;
|
---|
922 |
|
---|
923 | bool fUserClientOnly = true;
|
---|
924 | bool fConfigured = false;
|
---|
925 | bool fInUse = false;
|
---|
926 | bool fSeizable = true;
|
---|
927 | io_object_t Interface;
|
---|
928 | while ((Interface = IOIteratorNext(Interfaces)) != 0)
|
---|
929 | {
|
---|
930 | io_name_t szName;
|
---|
931 | krc = IOObjectGetClass(Interface, szName);
|
---|
932 | if ( krc == KERN_SUCCESS
|
---|
933 | && ( !strcmp(szName, "IOUSBInterface")
|
---|
934 | || !strcmp(szName, "IOUSBHostInterface")))
|
---|
935 | {
|
---|
936 | fConfigured = true;
|
---|
937 |
|
---|
938 | /*
|
---|
939 | * Iterate the interface children looking for stuff other than
|
---|
940 | * IOUSBUserClientInit objects.
|
---|
941 | */
|
---|
942 | io_iterator_t Children1;
|
---|
943 | krc = IORegistryEntryGetChildIterator(Interface, kIOServicePlane, &Children1);
|
---|
944 | if (krc == KERN_SUCCESS)
|
---|
945 | {
|
---|
946 | io_object_t Child1;
|
---|
947 | while ((Child1 = IOIteratorNext(Children1)) != 0)
|
---|
948 | {
|
---|
949 | krc = IOObjectGetClass(Child1, szName);
|
---|
950 | if ( krc == KERN_SUCCESS
|
---|
951 | && strcmp(szName, "IOUSBUserClientInit"))
|
---|
952 | {
|
---|
953 | fUserClientOnly = false;
|
---|
954 |
|
---|
955 | if ( !strcmp(szName, "IOUSBMassStorageClass")
|
---|
956 | || !strcmp(szName, "IOUSBMassStorageInterfaceNub"))
|
---|
957 | {
|
---|
958 | /* Only permit capturing MSDs that aren't mounted, at least
|
---|
959 | until the GUI starts poping up warnings about data loss
|
---|
960 | and such when capturing a busy device. */
|
---|
961 | fSeizable = false;
|
---|
962 | fInUse |= darwinIsMassStorageInterfaceInUse(Child1, szName);
|
---|
963 | }
|
---|
964 | else if (!strcmp(szName, "IOUSBHIDDriver")
|
---|
965 | || !strcmp(szName, "AppleHIDMouse")
|
---|
966 | /** @todo more? */)
|
---|
967 | {
|
---|
968 | /* For now, just assume that all HID devices are inaccessible
|
---|
969 | because of the greedy HID service. */
|
---|
970 | fSeizable = false;
|
---|
971 | fInUse = true;
|
---|
972 | }
|
---|
973 | else
|
---|
974 | fInUse = true;
|
---|
975 | }
|
---|
976 | IOObjectRelease(Child1);
|
---|
977 | }
|
---|
978 | IOObjectRelease(Children1);
|
---|
979 | }
|
---|
980 | }
|
---|
981 |
|
---|
982 | IOObjectRelease(Interface);
|
---|
983 | }
|
---|
984 | IOObjectRelease(Interfaces);
|
---|
985 |
|
---|
986 | /*
|
---|
987 | * Calc the status.
|
---|
988 | */
|
---|
989 | if (!fInUse)
|
---|
990 | pCur->enmState = USBDEVICESTATE_UNUSED;
|
---|
991 | else
|
---|
992 | pCur->enmState = fSeizable
|
---|
993 | ? USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
|
---|
994 | : USBDEVICESTATE_USED_BY_HOST;
|
---|
995 | }
|
---|
996 |
|
---|
997 | /**
|
---|
998 | * Worker function for DarwinGetUSBDevices() that tries to figure out
|
---|
999 | * what state the device is in and set enmState.
|
---|
1000 | *
|
---|
1001 | * This is mostly a matter of distinguishing between devices that nobody
|
---|
1002 | * uses, devices that can be seized and devices that cannot be grabbed.
|
---|
1003 | *
|
---|
1004 | * @param pCur The USB device data.
|
---|
1005 | * @param USBDevice The USB device object.
|
---|
1006 | * @param PropsRef The USB device properties.
|
---|
1007 | */
|
---|
1008 | static void darwinDeterminUSBDeviceState(PUSBDEVICE pCur, io_object_t USBDevice, CFMutableDictionaryRef /* PropsRef */)
|
---|
1009 | {
|
---|
1010 |
|
---|
1011 | if (!darwinUSBDeviceIsGrabbedDetermineState(pCur, USBDevice))
|
---|
1012 | {
|
---|
1013 | /*
|
---|
1014 | * The USB stack was completely reworked on El Capitan and the IOUSBDevice and IOUSBInterface
|
---|
1015 | * are deprecated and don't return the information required for the additional checks below.
|
---|
1016 | * We also can't directly make use of the new classes (IOUSBHostDevice and IOUSBHostInterface)
|
---|
1017 | * because VBoxUSB only exposes the legacy interfaces. Trying to use the new classes results in errors
|
---|
1018 | * because the I/O Kit USB library wants to use the new interfaces. The result is us losing the device
|
---|
1019 | * form the list when VBoxUSB has attached to the USB device.
|
---|
1020 | *
|
---|
1021 | * To make the checks below work we have to get hold of the IOUSBHostDevice and IOUSBHostInterface
|
---|
1022 | * instances for the current device. Fortunately the IOUSBHostDevice instance contains a
|
---|
1023 | * "AppleUSBAlternateServiceRegistryID" which points to the legacy class instance for the same device.
|
---|
1024 | * So just iterate over the list of IOUSBHostDevice instances and check whether the
|
---|
1025 | * AppleUSBAlternateServiceRegistryID property matches with the legacy instance.
|
---|
1026 | *
|
---|
1027 | * The upside is that we can keep VBoxUSB untouched and still compatible with older OS X releases.
|
---|
1028 | */
|
---|
1029 | if (g_uMajorDarwin >= VBOX_OSX_EL_CAPTIAN_VER)
|
---|
1030 | {
|
---|
1031 | io_object_t IOUSBDeviceNew = 0;
|
---|
1032 |
|
---|
1033 | io_object_t krc = darwinGetUSBHostDeviceFromLegacyDevice(USBDevice, &IOUSBDeviceNew);
|
---|
1034 | if ( krc == KERN_SUCCESS
|
---|
1035 | && IOUSBDeviceNew != 0)
|
---|
1036 | {
|
---|
1037 | darwinDetermineUSBDeviceStateWorker(pCur, IOUSBDeviceNew);
|
---|
1038 | IOObjectRelease(IOUSBDeviceNew);
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 | else
|
---|
1042 | darwinDetermineUSBDeviceStateWorker(pCur, USBDevice);
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 |
|
---|
1047 | /**
|
---|
1048 | * Enumerate the USB devices returning a FIFO of them.
|
---|
1049 | *
|
---|
1050 | * @returns Pointer to the head.
|
---|
1051 | * USBProxyService::freeDevice is expected to free each of the list elements.
|
---|
1052 | */
|
---|
1053 | PUSBDEVICE DarwinGetUSBDevices(void)
|
---|
1054 | {
|
---|
1055 | AssertReturn(darwinOpenMasterPort(), NULL);
|
---|
1056 | //DARWIN_IOKIT_LOG(("DarwinGetUSBDevices\n"));
|
---|
1057 |
|
---|
1058 | /*
|
---|
1059 | * Create a matching dictionary for searching for USB Devices in the IOKit.
|
---|
1060 | */
|
---|
1061 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
|
---|
1062 | AssertReturn(RefMatchingDict, NULL);
|
---|
1063 |
|
---|
1064 | /*
|
---|
1065 | * Perform the search and get a collection of USB Device back.
|
---|
1066 | */
|
---|
1067 | io_iterator_t USBDevices = NULL;
|
---|
1068 | IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
|
---|
1069 | AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
|
---|
1070 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * Enumerate the USB Devices.
|
---|
1074 | */
|
---|
1075 | PUSBDEVICE pHead = NULL;
|
---|
1076 | PUSBDEVICE pTail = NULL;
|
---|
1077 | unsigned i = 0;
|
---|
1078 | io_object_t USBDevice;
|
---|
1079 | while ((USBDevice = IOIteratorNext(USBDevices)) != 0)
|
---|
1080 | {
|
---|
1081 | DARWIN_IOKIT_DUMP_OBJ(USBDevice);
|
---|
1082 |
|
---|
1083 | /*
|
---|
1084 | * Query the device properties from the registry.
|
---|
1085 | *
|
---|
1086 | * We could alternatively use the device and such, but that will be
|
---|
1087 | * slower and we would have to resort to the registry for the three
|
---|
1088 | * string anyway.
|
---|
1089 | */
|
---|
1090 | CFMutableDictionaryRef PropsRef = 0;
|
---|
1091 | kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1092 | if (krc == KERN_SUCCESS)
|
---|
1093 | {
|
---|
1094 | bool fOk = false;
|
---|
1095 | PUSBDEVICE pCur = (PUSBDEVICE)RTMemAllocZ(sizeof(*pCur));
|
---|
1096 | do /* loop for breaking out of on failure. */
|
---|
1097 | {
|
---|
1098 | AssertBreak(pCur);
|
---|
1099 |
|
---|
1100 | /*
|
---|
1101 | * Mandatory
|
---|
1102 | */
|
---|
1103 | pCur->bcdUSB = 0; /* we've no idea. */
|
---|
1104 | pCur->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; /* just a default, we'll try harder in a bit. */
|
---|
1105 |
|
---|
1106 | /* Skip hubs. On 10.11 beta 3, the root hub simulations does not have a USBDeviceClass property, so
|
---|
1107 | simply ignore failures to retrieve it. */
|
---|
1108 | if (!darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceClass), &pCur->bDeviceClass))
|
---|
1109 | {
|
---|
1110 | #ifdef VBOX_STRICT
|
---|
1111 | char szTmp[80];
|
---|
1112 | Assert( darwinDictGetString(PropsRef, CFSTR("IOClassNameOverride"), szTmp, sizeof(szTmp))
|
---|
1113 | && strcmp(szTmp, "IOUSBRootHubDevice") == 0);
|
---|
1114 | #endif
|
---|
1115 | break;
|
---|
1116 | }
|
---|
1117 | if (pCur->bDeviceClass == 0x09 /* hub, find a define! */)
|
---|
1118 | break;
|
---|
1119 | AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceSubClass), &pCur->bDeviceSubClass));
|
---|
1120 | AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceProtocol), &pCur->bDeviceProtocol));
|
---|
1121 | AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBVendorID), &pCur->idVendor));
|
---|
1122 | AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBProductID), &pCur->idProduct));
|
---|
1123 | AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBDeviceReleaseNumber), &pCur->bcdDevice));
|
---|
1124 | uint32_t u32LocationId;
|
---|
1125 | AssertBreak(darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32LocationId));
|
---|
1126 | uint64_t u64SessionId;
|
---|
1127 | AssertBreak(darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64SessionId));
|
---|
1128 | char szAddress[64];
|
---|
1129 | RTStrPrintf(szAddress, sizeof(szAddress), "p=0x%04RX16;v=0x%04RX16;s=0x%016RX64;l=0x%08RX32",
|
---|
1130 | pCur->idProduct, pCur->idVendor, u64SessionId, u32LocationId);
|
---|
1131 | pCur->pszAddress = RTStrDup(szAddress);
|
---|
1132 | AssertBreak(pCur->pszAddress);
|
---|
1133 | pCur->bBus = u32LocationId >> 24;
|
---|
1134 | darwinDictGetU8(PropsRef, CFSTR("PortNum"), &pCur->bPort); /* Not present in 10.11 beta 3, so ignore failure. (Is set to zero.) */
|
---|
1135 | uint8_t bSpeed;
|
---|
1136 | AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDevicePropertySpeed), &bSpeed));
|
---|
1137 | Assert(bSpeed <= 3);
|
---|
1138 | pCur->enmSpeed = bSpeed == 3 ? USBDEVICESPEED_SUPER
|
---|
1139 | : bSpeed == 2 ? USBDEVICESPEED_HIGH
|
---|
1140 | : bSpeed == 1 ? USBDEVICESPEED_FULL
|
---|
1141 | : bSpeed == 0 ? USBDEVICESPEED_LOW
|
---|
1142 | : USBDEVICESPEED_UNKNOWN;
|
---|
1143 |
|
---|
1144 | /*
|
---|
1145 | * Optional.
|
---|
1146 | * There are some nameless device in the iMac, apply names to them.
|
---|
1147 | */
|
---|
1148 | darwinDictDupString(PropsRef, CFSTR("USB Vendor Name"), (char **)&pCur->pszManufacturer);
|
---|
1149 | if ( !pCur->pszManufacturer
|
---|
1150 | && pCur->idVendor == kIOUSBVendorIDAppleComputer)
|
---|
1151 | pCur->pszManufacturer = RTStrDup("Apple Computer, Inc.");
|
---|
1152 | darwinDictDupString(PropsRef, CFSTR("USB Product Name"), (char **)&pCur->pszProduct);
|
---|
1153 | if ( !pCur->pszProduct
|
---|
1154 | && pCur->bDeviceClass == 224 /* Wireless */
|
---|
1155 | && pCur->bDeviceSubClass == 1 /* Radio Frequency */
|
---|
1156 | && pCur->bDeviceProtocol == 1 /* Bluetooth */)
|
---|
1157 | pCur->pszProduct = RTStrDup("Bluetooth");
|
---|
1158 | darwinDictDupString(PropsRef, CFSTR("USB Serial Number"), (char **)&pCur->pszSerialNumber);
|
---|
1159 |
|
---|
1160 | pCur->pszBackend = RTStrDup("host");
|
---|
1161 | AssertBreak(pCur->pszBackend);
|
---|
1162 |
|
---|
1163 | #if 0 /* leave the remainder as zero for now. */
|
---|
1164 | /*
|
---|
1165 | * Create a plugin interface for the service and query its USB Device interface.
|
---|
1166 | */
|
---|
1167 | SInt32 Score = 0;
|
---|
1168 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
1169 | rc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
|
---|
1170 | kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
|
---|
1171 | if (rc == kIOReturnSuccess)
|
---|
1172 | {
|
---|
1173 | IOUSBDeviceInterface245 **ppUSBDevI = NULL;
|
---|
1174 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
1175 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
|
---|
1176 | (LPVOID *)&ppUSBDevI);
|
---|
1177 | rc = IODestroyPlugInInterface(ppPlugInInterface); Assert(rc == kIOReturnSuccess);
|
---|
1178 | ppPlugInInterface = NULL;
|
---|
1179 | if (hrc == S_OK)
|
---|
1180 | {
|
---|
1181 | /** @todo enumerate configurations and interfaces if we actually need them. */
|
---|
1182 | //IOReturn (*GetNumberOfConfigurations)(void *self, UInt8 *numConfig);
|
---|
1183 | //IOReturn (*GetConfigurationDescriptorPtr)(void *self, UInt8 configIndex, IOUSBConfigurationDescriptorPtr *desc);
|
---|
1184 | //IOReturn (*CreateInterfaceIterator)(void *self, IOUSBFindInterfaceRequest *req, io_iterator_t *iter);
|
---|
1185 | }
|
---|
1186 | long cReft = (*ppUSBDeviceInterface)->Release(ppUSBDeviceInterface); MY_CHECK_CREFS(cRefs);
|
---|
1187 | }
|
---|
1188 | #endif
|
---|
1189 | /*
|
---|
1190 | * Try determine the state.
|
---|
1191 | */
|
---|
1192 | darwinDeterminUSBDeviceState(pCur, USBDevice, PropsRef);
|
---|
1193 |
|
---|
1194 | /*
|
---|
1195 | * We're good. Link the device.
|
---|
1196 | */
|
---|
1197 | pCur->pPrev = pTail;
|
---|
1198 | if (pTail)
|
---|
1199 | pTail = pTail->pNext = pCur;
|
---|
1200 | else
|
---|
1201 | pTail = pHead = pCur;
|
---|
1202 | fOk = true;
|
---|
1203 | } while (0);
|
---|
1204 |
|
---|
1205 | /* cleanup on failure / skipped device. */
|
---|
1206 | if (!fOk && pCur)
|
---|
1207 | DarwinFreeUSBDeviceFromIOKit(pCur);
|
---|
1208 |
|
---|
1209 | CFRelease(PropsRef);
|
---|
1210 | }
|
---|
1211 | else
|
---|
1212 | AssertMsgFailed(("krc=%#x\n", krc));
|
---|
1213 |
|
---|
1214 | IOObjectRelease(USBDevice);
|
---|
1215 | i++;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | IOObjectRelease(USBDevices);
|
---|
1219 | //DARWIN_IOKIT_LOG_FLUSH();
|
---|
1220 |
|
---|
1221 | /*
|
---|
1222 | * Some post processing. There are a couple of things we have to
|
---|
1223 | * make 100% sure about, and that is that the (Apple) keyboard
|
---|
1224 | * and mouse most likely to be in use by the user aren't available
|
---|
1225 | * for capturing. If there is no Apple mouse or keyboard we'll
|
---|
1226 | * take the first one from another vendor.
|
---|
1227 | */
|
---|
1228 | /* As it turns out, the HID service will take all keyboards and mice
|
---|
1229 | and we're not currently able to seize them. */
|
---|
1230 | PUSBDEVICE pMouse = NULL;
|
---|
1231 | PUSBDEVICE pKeyboard = NULL;
|
---|
1232 | for (PUSBDEVICE pCur = pHead; pCur; pCur = pCur->pNext)
|
---|
1233 | if (pCur->idVendor == kIOUSBVendorIDAppleComputer)
|
---|
1234 | {
|
---|
1235 | /*
|
---|
1236 | * This test is a bit rough, should check device class/protocol but
|
---|
1237 | * we don't have interface info yet so that might be a bit tricky.
|
---|
1238 | */
|
---|
1239 | if ( ( !pKeyboard
|
---|
1240 | || pKeyboard->idVendor != kIOUSBVendorIDAppleComputer)
|
---|
1241 | && pCur->pszProduct
|
---|
1242 | && strstr(pCur->pszProduct, " Keyboard"))
|
---|
1243 | pKeyboard = pCur;
|
---|
1244 | else if ( ( !pMouse
|
---|
1245 | || pMouse->idVendor != kIOUSBVendorIDAppleComputer)
|
---|
1246 | && pCur->pszProduct
|
---|
1247 | && strstr(pCur->pszProduct, " Mouse")
|
---|
1248 | )
|
---|
1249 | pMouse = pCur;
|
---|
1250 | }
|
---|
1251 | else if (!pKeyboard || !pMouse)
|
---|
1252 | {
|
---|
1253 | if ( pCur->bDeviceClass == 3 /* HID */
|
---|
1254 | && pCur->bDeviceProtocol == 1 /* Keyboard */)
|
---|
1255 | pKeyboard = pCur;
|
---|
1256 | else if ( pCur->bDeviceClass == 3 /* HID */
|
---|
1257 | && pCur->bDeviceProtocol == 2 /* Mouse */)
|
---|
1258 | pMouse = pCur;
|
---|
1259 | /** @todo examin interfaces */
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | if (pKeyboard)
|
---|
1263 | pKeyboard->enmState = USBDEVICESTATE_USED_BY_HOST;
|
---|
1264 | if (pMouse)
|
---|
1265 | pMouse->enmState = USBDEVICESTATE_USED_BY_HOST;
|
---|
1266 |
|
---|
1267 | return pHead;
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Triggers re-enumeration of a device.
|
---|
1273 | *
|
---|
1274 | * @returns VBox status code.
|
---|
1275 | * @param pCur The USBDEVICE structure for the device.
|
---|
1276 | */
|
---|
1277 | int DarwinReEnumerateUSBDevice(PCUSBDEVICE pCur)
|
---|
1278 | {
|
---|
1279 | int vrc;
|
---|
1280 | const char *pszAddress = pCur->pszAddress;
|
---|
1281 | AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
|
---|
1282 | AssertReturn(darwinOpenMasterPort(), VERR_GENERAL_FAILURE);
|
---|
1283 |
|
---|
1284 | /*
|
---|
1285 | * This code is a short version of the Open method in USBProxyDevice-darwin.cpp stuff.
|
---|
1286 | * Fixes made to this code probably applies there too!
|
---|
1287 | */
|
---|
1288 |
|
---|
1289 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
|
---|
1290 | AssertReturn(RefMatchingDict, NULL);
|
---|
1291 |
|
---|
1292 | uint64_t u64SessionId = 0;
|
---|
1293 | uint32_t u32LocationId = 0;
|
---|
1294 | const char *psz = pszAddress;
|
---|
1295 | do
|
---|
1296 | {
|
---|
1297 | const char chValue = *psz;
|
---|
1298 | AssertReleaseReturn(psz[1] == '=', VERR_INTERNAL_ERROR);
|
---|
1299 | uint64_t u64Value;
|
---|
1300 | int rc = RTStrToUInt64Ex(psz + 2, (char **)&psz, 0, &u64Value);
|
---|
1301 | AssertReleaseRCReturn(rc, rc);
|
---|
1302 | AssertReleaseReturn(!*psz || *psz == ';', rc);
|
---|
1303 | switch (chValue)
|
---|
1304 | {
|
---|
1305 | case 'l':
|
---|
1306 | u32LocationId = (uint32_t)u64Value;
|
---|
1307 | break;
|
---|
1308 | case 's':
|
---|
1309 | u64SessionId = u64Value;
|
---|
1310 | break;
|
---|
1311 | case 'p':
|
---|
1312 | case 'v':
|
---|
1313 | {
|
---|
1314 | #if 0 /* Guess what, this doesn't 'ing work either! */
|
---|
1315 | SInt32 i32 = (int16_t)u64Value;
|
---|
1316 | CFNumberRef Num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i32);
|
---|
1317 | AssertBreak(Num);
|
---|
1318 | CFDictionarySetValue(RefMatchingDict, chValue == 'p' ? CFSTR(kUSBProductID) : CFSTR(kUSBVendorID), Num);
|
---|
1319 | CFRelease(Num);
|
---|
1320 | #endif
|
---|
1321 | break;
|
---|
1322 | }
|
---|
1323 | default:
|
---|
1324 | AssertReleaseMsgFailedReturn(("chValue=%#x\n", chValue), VERR_INTERNAL_ERROR);
|
---|
1325 | }
|
---|
1326 | if (*psz == ';')
|
---|
1327 | psz++;
|
---|
1328 | } while (*psz);
|
---|
1329 |
|
---|
1330 | io_iterator_t USBDevices = NULL;
|
---|
1331 | IOReturn irc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
|
---|
1332 | AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%#x\n", irc), NULL);
|
---|
1333 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
1334 |
|
---|
1335 | unsigned cMatches = 0;
|
---|
1336 | io_object_t USBDevice;
|
---|
1337 | while ((USBDevice = IOIteratorNext(USBDevices)))
|
---|
1338 | {
|
---|
1339 | cMatches++;
|
---|
1340 | CFMutableDictionaryRef PropsRef = 0;
|
---|
1341 | kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1342 | if (krc == KERN_SUCCESS)
|
---|
1343 | {
|
---|
1344 | uint64_t u64CurSessionId;
|
---|
1345 | uint32_t u32CurLocationId;
|
---|
1346 | if ( ( !u64SessionId
|
---|
1347 | || ( darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64CurSessionId)
|
---|
1348 | && u64CurSessionId == u64SessionId))
|
---|
1349 | && ( !u32LocationId
|
---|
1350 | || ( darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32CurLocationId)
|
---|
1351 | && u32CurLocationId == u32LocationId))
|
---|
1352 | )
|
---|
1353 | {
|
---|
1354 | CFRelease(PropsRef);
|
---|
1355 | break;
|
---|
1356 | }
|
---|
1357 | CFRelease(PropsRef);
|
---|
1358 | }
|
---|
1359 | IOObjectRelease(USBDevice);
|
---|
1360 | }
|
---|
1361 | IOObjectRelease(USBDevices);
|
---|
1362 | USBDevices = NULL;
|
---|
1363 | if (!USBDevice)
|
---|
1364 | {
|
---|
1365 | LogRel(("USB: Device '%s' not found (%d pid+vid matches)\n", pszAddress, cMatches));
|
---|
1366 | IOObjectRelease(USBDevices);
|
---|
1367 | return VERR_VUSB_DEVICE_NAME_NOT_FOUND;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | /*
|
---|
1371 | * Create a plugin interface for the device and query its IOUSBDeviceInterface.
|
---|
1372 | */
|
---|
1373 | SInt32 Score = 0;
|
---|
1374 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
1375 | irc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
|
---|
1376 | kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
|
---|
1377 | if (irc == kIOReturnSuccess)
|
---|
1378 | {
|
---|
1379 | IOUSBDeviceInterface245 **ppDevI = NULL;
|
---|
1380 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
1381 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
|
---|
1382 | (LPVOID *)&ppDevI);
|
---|
1383 | irc = IODestroyPlugInInterface(ppPlugInInterface); Assert(irc == kIOReturnSuccess);
|
---|
1384 | ppPlugInInterface = NULL;
|
---|
1385 | if (hrc == S_OK)
|
---|
1386 | {
|
---|
1387 | /*
|
---|
1388 | * Try open the device for exclusive access.
|
---|
1389 | */
|
---|
1390 | irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
|
---|
1391 | if (irc == kIOReturnExclusiveAccess)
|
---|
1392 | {
|
---|
1393 | RTThreadSleep(20);
|
---|
1394 | irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
|
---|
1395 | }
|
---|
1396 | if (irc == kIOReturnSuccess)
|
---|
1397 | {
|
---|
1398 | /*
|
---|
1399 | * Re-enumerate the device and bail out.
|
---|
1400 | */
|
---|
1401 | irc = (*ppDevI)->USBDeviceReEnumerate(ppDevI, 0);
|
---|
1402 | if (irc == kIOReturnSuccess)
|
---|
1403 | vrc = VINF_SUCCESS;
|
---|
1404 | else
|
---|
1405 | {
|
---|
1406 | LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
|
---|
1407 | vrc = RTErrConvertFromDarwinIO(irc);
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | (*ppDevI)->USBDeviceClose(ppDevI);
|
---|
1411 | }
|
---|
1412 | else if (irc == kIOReturnExclusiveAccess)
|
---|
1413 | {
|
---|
1414 | LogRel(("USB: Device '%s' is being used by another process\n", pszAddress));
|
---|
1415 | vrc = VERR_SHARING_VIOLATION;
|
---|
1416 | }
|
---|
1417 | else
|
---|
1418 | {
|
---|
1419 | LogRel(("USB: Failed to open device '%s', irc=%#x.\n", pszAddress, irc));
|
---|
1420 | vrc = VERR_OPEN_FAILED;
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 | else
|
---|
1424 | {
|
---|
1425 | LogRel(("USB: Failed to create plugin interface for device '%s', hrc=%#x.\n", pszAddress, hrc));
|
---|
1426 | vrc = VERR_OPEN_FAILED;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | (*ppDevI)->Release(ppDevI);
|
---|
1430 | }
|
---|
1431 | else
|
---|
1432 | {
|
---|
1433 | LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
|
---|
1434 | vrc = RTErrConvertFromDarwinIO(irc);
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | return vrc;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | #endif /* VBOX_WITH_USB */
|
---|
1441 |
|
---|
1442 |
|
---|
1443 | /**
|
---|
1444 | * Enumerate the CD, DVD and BlueRay drives returning a FIFO of device name strings.
|
---|
1445 | *
|
---|
1446 | * @returns Pointer to the head.
|
---|
1447 | * The caller is responsible for calling RTMemFree() on each of the nodes.
|
---|
1448 | */
|
---|
1449 | PDARWINDVD DarwinGetDVDDrives(void)
|
---|
1450 | {
|
---|
1451 | AssertReturn(darwinOpenMasterPort(), NULL);
|
---|
1452 |
|
---|
1453 | /*
|
---|
1454 | * Create a matching dictionary for searching for CD, DVD and BlueRay services in the IOKit.
|
---|
1455 | *
|
---|
1456 | * The idea is to find all the devices which are of class IOCDBlockStorageDevice.
|
---|
1457 | * CD devices are represented by IOCDBlockStorageDevice class itself, while DVD and BlueRay ones
|
---|
1458 | * have it as a parent class.
|
---|
1459 | */
|
---|
1460 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IOCDBlockStorageDevice");
|
---|
1461 | AssertReturn(RefMatchingDict, NULL);
|
---|
1462 |
|
---|
1463 | /*
|
---|
1464 | * Perform the search and get a collection of DVD services.
|
---|
1465 | */
|
---|
1466 | io_iterator_t DVDServices = NULL;
|
---|
1467 | IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &DVDServices);
|
---|
1468 | AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
|
---|
1469 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
1470 |
|
---|
1471 | /*
|
---|
1472 | * Enumerate the matching services.
|
---|
1473 | * (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
|
---|
1474 | */
|
---|
1475 | PDARWINDVD pHead = NULL;
|
---|
1476 | PDARWINDVD pTail = NULL;
|
---|
1477 | unsigned i = 0;
|
---|
1478 | io_object_t DVDService;
|
---|
1479 | while ((DVDService = IOIteratorNext(DVDServices)) != 0)
|
---|
1480 | {
|
---|
1481 | DARWIN_IOKIT_DUMP_OBJ(DVDService);
|
---|
1482 |
|
---|
1483 | /*
|
---|
1484 | * Get the properties we use to identify the DVD drive.
|
---|
1485 | *
|
---|
1486 | * While there is a (weird 12 byte) GUID, it isn't persistent
|
---|
1487 | * across boots. So, we have to use a combination of the
|
---|
1488 | * vendor name and product name properties with an optional
|
---|
1489 | * sequence number for identification.
|
---|
1490 | */
|
---|
1491 | CFMutableDictionaryRef PropsRef = 0;
|
---|
1492 | kern_return_t krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1493 | if (krc == KERN_SUCCESS)
|
---|
1494 | {
|
---|
1495 | /* Get the Device Characteristics dictionary. */
|
---|
1496 | CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
|
---|
1497 | if (DevCharRef)
|
---|
1498 | {
|
---|
1499 | /* The vendor name. */
|
---|
1500 | char szVendor[128];
|
---|
1501 | char *pszVendor = &szVendor[0];
|
---|
1502 | CFTypeRef ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyVendorNameKey));
|
---|
1503 | if ( ValueRef
|
---|
1504 | && CFGetTypeID(ValueRef) == CFStringGetTypeID()
|
---|
1505 | && CFStringGetCString((CFStringRef)ValueRef, szVendor, sizeof(szVendor), kCFStringEncodingUTF8))
|
---|
1506 | pszVendor = RTStrStrip(szVendor);
|
---|
1507 | else
|
---|
1508 | *pszVendor = '\0';
|
---|
1509 |
|
---|
1510 | /* The product name. */
|
---|
1511 | char szProduct[128];
|
---|
1512 | char *pszProduct = &szProduct[0];
|
---|
1513 | ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyProductNameKey));
|
---|
1514 | if ( ValueRef
|
---|
1515 | && CFGetTypeID(ValueRef) == CFStringGetTypeID()
|
---|
1516 | && CFStringGetCString((CFStringRef)ValueRef, szProduct, sizeof(szProduct), kCFStringEncodingUTF8))
|
---|
1517 | pszProduct = RTStrStrip(szProduct);
|
---|
1518 | else
|
---|
1519 | *pszProduct = '\0';
|
---|
1520 |
|
---|
1521 | /* Construct the name and check for duplicates. */
|
---|
1522 | char szName[256 + 32];
|
---|
1523 | if (*pszVendor || *pszProduct)
|
---|
1524 | {
|
---|
1525 | if (*pszVendor && *pszProduct)
|
---|
1526 | RTStrPrintf(szName, sizeof(szName), "%s %s", pszVendor, pszProduct);
|
---|
1527 | else
|
---|
1528 | strcpy(szName, *pszVendor ? pszVendor : pszProduct);
|
---|
1529 |
|
---|
1530 | for (PDARWINDVD pCur = pHead; pCur; pCur = pCur->pNext)
|
---|
1531 | {
|
---|
1532 | if (!strcmp(szName, pCur->szName))
|
---|
1533 | {
|
---|
1534 | if (*pszVendor && *pszProduct)
|
---|
1535 | RTStrPrintf(szName, sizeof(szName), "%s %s (#%u)", pszVendor, pszProduct, i);
|
---|
1536 | else
|
---|
1537 | RTStrPrintf(szName, sizeof(szName), "%s (#%u)", *pszVendor ? pszVendor : pszProduct, i);
|
---|
1538 | break;
|
---|
1539 | }
|
---|
1540 | }
|
---|
1541 | }
|
---|
1542 | else
|
---|
1543 | RTStrPrintf(szName, sizeof(szName), "(#%u)", i);
|
---|
1544 |
|
---|
1545 | /* Create the device. */
|
---|
1546 | size_t cbName = strlen(szName) + 1;
|
---|
1547 | PDARWINDVD pNew = (PDARWINDVD)RTMemAlloc(RT_OFFSETOF(DARWINDVD, szName[cbName]));
|
---|
1548 | if (pNew)
|
---|
1549 | {
|
---|
1550 | pNew->pNext = NULL;
|
---|
1551 | memcpy(pNew->szName, szName, cbName);
|
---|
1552 | if (pTail)
|
---|
1553 | pTail = pTail->pNext = pNew;
|
---|
1554 | else
|
---|
1555 | pTail = pHead = pNew;
|
---|
1556 | }
|
---|
1557 | }
|
---|
1558 | CFRelease(PropsRef);
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | AssertMsgFailed(("krc=%#x\n", krc));
|
---|
1562 |
|
---|
1563 | IOObjectRelease(DVDService);
|
---|
1564 | i++;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | IOObjectRelease(DVDServices);
|
---|
1568 |
|
---|
1569 | return pHead;
|
---|
1570 | }
|
---|
1571 |
|
---|
1572 |
|
---|
1573 | /**
|
---|
1574 | * Enumerate the ethernet capable network devices returning a FIFO of them.
|
---|
1575 | *
|
---|
1576 | * @returns Pointer to the head.
|
---|
1577 | */
|
---|
1578 | PDARWINETHERNIC DarwinGetEthernetControllers(void)
|
---|
1579 | {
|
---|
1580 | AssertReturn(darwinOpenMasterPort(), NULL);
|
---|
1581 |
|
---|
1582 | /*
|
---|
1583 | * Create a matching dictionary for searching for ethernet controller
|
---|
1584 | * services in the IOKit.
|
---|
1585 | *
|
---|
1586 | * For some really stupid reason I don't get all the controllers if I look for
|
---|
1587 | * objects that are instances of IOEthernetController or its descendants (only
|
---|
1588 | * get the AirPort on my mac pro). But fortunately using IOEthernetInterface
|
---|
1589 | * seems to work. Weird s**t!
|
---|
1590 | */
|
---|
1591 | //CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IOEthernetController"); - this doesn't work :-(
|
---|
1592 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IOEthernetInterface");
|
---|
1593 | AssertReturn(RefMatchingDict, NULL);
|
---|
1594 |
|
---|
1595 | /*
|
---|
1596 | * Perform the search and get a collection of ethernet controller services.
|
---|
1597 | */
|
---|
1598 | io_iterator_t EtherIfServices = NULL;
|
---|
1599 | IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &EtherIfServices);
|
---|
1600 | AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
|
---|
1601 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
1602 |
|
---|
1603 | /*
|
---|
1604 | * Get a copy of the current network interfaces from the system configuration service.
|
---|
1605 | * We'll use this for looking up the proper interface names.
|
---|
1606 | */
|
---|
1607 | CFArrayRef IfsRef = SCNetworkInterfaceCopyAll();
|
---|
1608 | CFIndex cIfs = IfsRef ? CFArrayGetCount(IfsRef) : 0;
|
---|
1609 |
|
---|
1610 | /*
|
---|
1611 | * Get the current preferences and make a copy of the network services so we
|
---|
1612 | * can look up the right interface names. The IfsRef is just for fallback.
|
---|
1613 | */
|
---|
1614 | CFArrayRef ServicesRef = NULL;
|
---|
1615 | CFIndex cServices = 0;
|
---|
1616 | SCPreferencesRef PrefsRef = SCPreferencesCreate(kCFAllocatorDefault, CFSTR("org.virtualbox.VBoxSVC"), NULL);
|
---|
1617 | if (PrefsRef)
|
---|
1618 | {
|
---|
1619 | SCNetworkSetRef SetRef = SCNetworkSetCopyCurrent(PrefsRef);
|
---|
1620 | CFRelease(PrefsRef);
|
---|
1621 | if (SetRef)
|
---|
1622 | {
|
---|
1623 | ServicesRef = SCNetworkSetCopyServices(SetRef);
|
---|
1624 | CFRelease(SetRef);
|
---|
1625 | cServices = ServicesRef ? CFArrayGetCount(ServicesRef) : 0;
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | /*
|
---|
1630 | * Enumerate the ethernet controller services.
|
---|
1631 | */
|
---|
1632 | PDARWINETHERNIC pHead = NULL;
|
---|
1633 | PDARWINETHERNIC pTail = NULL;
|
---|
1634 | io_object_t EtherIfService;
|
---|
1635 | while ((EtherIfService = IOIteratorNext(EtherIfServices)) != 0)
|
---|
1636 | {
|
---|
1637 | /*
|
---|
1638 | * Dig up the parent, meaning the IOEthernetController.
|
---|
1639 | */
|
---|
1640 | io_object_t EtherNICService;
|
---|
1641 | kern_return_t krc = IORegistryEntryGetParentEntry(EtherIfService, kIOServicePlane, &EtherNICService);
|
---|
1642 | /*krc = IORegistryEntryGetChildEntry(EtherNICService, kIOServicePlane, &EtherIfService); */
|
---|
1643 | if (krc == KERN_SUCCESS)
|
---|
1644 | {
|
---|
1645 | DARWIN_IOKIT_DUMP_OBJ(EtherNICService);
|
---|
1646 | /*
|
---|
1647 | * Get the properties we use to identify and name the Ethernet NIC.
|
---|
1648 | * We need the both the IOEthernetController and it's IONetworkInterface child.
|
---|
1649 | */
|
---|
1650 | CFMutableDictionaryRef PropsRef = 0;
|
---|
1651 | krc = IORegistryEntryCreateCFProperties(EtherNICService, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1652 | if (krc == KERN_SUCCESS)
|
---|
1653 | {
|
---|
1654 | CFMutableDictionaryRef IfPropsRef = 0;
|
---|
1655 | krc = IORegistryEntryCreateCFProperties(EtherIfService, &IfPropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1656 | if (krc == KERN_SUCCESS)
|
---|
1657 | {
|
---|
1658 | /*
|
---|
1659 | * Gather the required data.
|
---|
1660 | * We'll create a UUID from the MAC address and the BSD name.
|
---|
1661 | */
|
---|
1662 | char szTmp[256];
|
---|
1663 | do
|
---|
1664 | {
|
---|
1665 | /* Check if airport (a bit heuristical - it's com.apple.driver.AirPortBrcm43xx here). */
|
---|
1666 | darwinDictGetString(PropsRef, CFSTR("CFBundleIdentifier"), szTmp, sizeof(szTmp));
|
---|
1667 | bool fWireless;
|
---|
1668 | bool fAirPort = fWireless = strstr(szTmp, ".AirPort") != NULL;
|
---|
1669 |
|
---|
1670 | /* Check if it's USB. */
|
---|
1671 | darwinDictGetString(PropsRef, CFSTR("IOProviderClass"), szTmp, sizeof(szTmp));
|
---|
1672 | bool fUSB = strstr(szTmp, "USB") != NULL;
|
---|
1673 |
|
---|
1674 |
|
---|
1675 | /* Is it builtin? */
|
---|
1676 | bool fBuiltin;
|
---|
1677 | darwinDictGetBool(IfPropsRef, CFSTR("IOBuiltin"), &fBuiltin);
|
---|
1678 |
|
---|
1679 | /* Is it the primary interface */
|
---|
1680 | bool fPrimaryIf;
|
---|
1681 | darwinDictGetBool(IfPropsRef, CFSTR("IOPrimaryInterface"), &fPrimaryIf);
|
---|
1682 |
|
---|
1683 | /* Get the MAC address. */
|
---|
1684 | RTMAC Mac;
|
---|
1685 | AssertBreak(darwinDictGetData(PropsRef, CFSTR("IOMACAddress"), &Mac, sizeof(Mac)));
|
---|
1686 |
|
---|
1687 | /* The BSD Name from the interface dictionary. */
|
---|
1688 | char szBSDName[RT_SIZEOFMEMB(DARWINETHERNIC, szBSDName)];
|
---|
1689 | AssertBreak(darwinDictGetString(IfPropsRef, CFSTR("BSD Name"), szBSDName, sizeof(szBSDName)));
|
---|
1690 |
|
---|
1691 | /* Check if it's really wireless. */
|
---|
1692 | if ( darwinDictIsPresent(IfPropsRef, CFSTR("IO80211CountryCode"))
|
---|
1693 | || darwinDictIsPresent(IfPropsRef, CFSTR("IO80211DriverVersion"))
|
---|
1694 | || darwinDictIsPresent(IfPropsRef, CFSTR("IO80211HardwareVersion"))
|
---|
1695 | || darwinDictIsPresent(IfPropsRef, CFSTR("IO80211Locale")))
|
---|
1696 | fWireless = true;
|
---|
1697 | else
|
---|
1698 | fAirPort = fWireless = false;
|
---|
1699 |
|
---|
1700 | /** @todo IOPacketFilters / IONetworkFilterGroup? */
|
---|
1701 | /*
|
---|
1702 | * Create the interface name.
|
---|
1703 | *
|
---|
1704 | * Note! The ConsoleImpl2.cpp code ASSUMES things about the name. It is also
|
---|
1705 | * stored in the VM config files. (really bright idea)
|
---|
1706 | */
|
---|
1707 | strcpy(szTmp, szBSDName);
|
---|
1708 | char *psz = strchr(szTmp, '\0');
|
---|
1709 | *psz++ = ':';
|
---|
1710 | *psz++ = ' ';
|
---|
1711 | size_t cchLeft = sizeof(szTmp) - (psz - &szTmp[0]) - (sizeof(" (Wireless)") - 1);
|
---|
1712 | bool fFound = false;
|
---|
1713 | CFIndex i;
|
---|
1714 |
|
---|
1715 | /* look it up among the current services */
|
---|
1716 | for (i = 0; i < cServices; i++)
|
---|
1717 | {
|
---|
1718 | SCNetworkServiceRef ServiceRef = (SCNetworkServiceRef)CFArrayGetValueAtIndex(ServicesRef, i);
|
---|
1719 | SCNetworkInterfaceRef IfRef = SCNetworkServiceGetInterface(ServiceRef);
|
---|
1720 | if (IfRef)
|
---|
1721 | {
|
---|
1722 | CFStringRef BSDNameRef = SCNetworkInterfaceGetBSDName(IfRef);
|
---|
1723 | if ( BSDNameRef
|
---|
1724 | && CFStringGetCString(BSDNameRef, psz, cchLeft, kCFStringEncodingUTF8)
|
---|
1725 | && !strcmp(psz, szBSDName))
|
---|
1726 | {
|
---|
1727 | CFStringRef ServiceNameRef = SCNetworkServiceGetName(ServiceRef);
|
---|
1728 | if ( ServiceNameRef
|
---|
1729 | && CFStringGetCString(ServiceNameRef, psz, cchLeft, kCFStringEncodingUTF8))
|
---|
1730 | {
|
---|
1731 | fFound = true;
|
---|
1732 | break;
|
---|
1733 | }
|
---|
1734 | }
|
---|
1735 | }
|
---|
1736 | }
|
---|
1737 | /* Look it up in the interface list. */
|
---|
1738 | if (!fFound)
|
---|
1739 | for (i = 0; i < cIfs; i++)
|
---|
1740 | {
|
---|
1741 | SCNetworkInterfaceRef IfRef = (SCNetworkInterfaceRef)CFArrayGetValueAtIndex(IfsRef, i);
|
---|
1742 | CFStringRef BSDNameRef = SCNetworkInterfaceGetBSDName(IfRef);
|
---|
1743 | if ( BSDNameRef
|
---|
1744 | && CFStringGetCString(BSDNameRef, psz, cchLeft, kCFStringEncodingUTF8)
|
---|
1745 | && !strcmp(psz, szBSDName))
|
---|
1746 | {
|
---|
1747 | CFStringRef DisplayNameRef = SCNetworkInterfaceGetLocalizedDisplayName(IfRef);
|
---|
1748 | if ( DisplayNameRef
|
---|
1749 | && CFStringGetCString(DisplayNameRef, psz, cchLeft, kCFStringEncodingUTF8))
|
---|
1750 | {
|
---|
1751 | fFound = true;
|
---|
1752 | break;
|
---|
1753 | }
|
---|
1754 | }
|
---|
1755 | }
|
---|
1756 | /* Generate a half plausible name if we for some silly reason didn't find the interface. */
|
---|
1757 | if (!fFound)
|
---|
1758 | RTStrPrintf(szTmp, sizeof(szTmp), "%s: %s%s(?)",
|
---|
1759 | szBSDName,
|
---|
1760 | fUSB ? "USB " : "",
|
---|
1761 | fWireless ? fAirPort ? "AirPort " : "Wireless" : "Ethernet");
|
---|
1762 | /* If we did find it and it's wireless but without "AirPort" or "Wireless", fix it */
|
---|
1763 | else if ( fWireless
|
---|
1764 | && !strstr(psz, "AirPort")
|
---|
1765 | && !strstr(psz, "Wireless"))
|
---|
1766 | strcat(szTmp, fAirPort ? " (AirPort)" : " (Wireless)");
|
---|
1767 |
|
---|
1768 | /*
|
---|
1769 | * Create the list entry.
|
---|
1770 | */
|
---|
1771 | DARWIN_IOKIT_LOG(("Found: if=%s mac=%.6Rhxs fWireless=%RTbool fAirPort=%RTbool fBuiltin=%RTbool fPrimaryIf=%RTbool fUSB=%RTbool\n",
|
---|
1772 | szBSDName, &Mac, fWireless, fAirPort, fBuiltin, fPrimaryIf, fUSB));
|
---|
1773 |
|
---|
1774 | size_t cchName = strlen(szTmp);
|
---|
1775 | PDARWINETHERNIC pNew = (PDARWINETHERNIC)RTMemAlloc(RT_OFFSETOF(DARWINETHERNIC, szName[cchName + 1]));
|
---|
1776 | if (pNew)
|
---|
1777 | {
|
---|
1778 | strncpy(pNew->szBSDName, szBSDName, sizeof(pNew->szBSDName)); /* the '\0' padding is intentional! */
|
---|
1779 |
|
---|
1780 | RTUuidClear(&pNew->Uuid);
|
---|
1781 | memcpy(&pNew->Uuid, pNew->szBSDName, RT_MIN(sizeof(pNew->szBSDName), sizeof(pNew->Uuid)));
|
---|
1782 | pNew->Uuid.Gen.u8ClockSeqHiAndReserved = (pNew->Uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
|
---|
1783 | pNew->Uuid.Gen.u16TimeHiAndVersion = (pNew->Uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
|
---|
1784 | pNew->Uuid.Gen.au8Node[0] = Mac.au8[0];
|
---|
1785 | pNew->Uuid.Gen.au8Node[1] = Mac.au8[1];
|
---|
1786 | pNew->Uuid.Gen.au8Node[2] = Mac.au8[2];
|
---|
1787 | pNew->Uuid.Gen.au8Node[3] = Mac.au8[3];
|
---|
1788 | pNew->Uuid.Gen.au8Node[4] = Mac.au8[4];
|
---|
1789 | pNew->Uuid.Gen.au8Node[5] = Mac.au8[5];
|
---|
1790 |
|
---|
1791 | pNew->Mac = Mac;
|
---|
1792 | pNew->fWireless = fWireless;
|
---|
1793 | pNew->fAirPort = fAirPort;
|
---|
1794 | pNew->fBuiltin = fBuiltin;
|
---|
1795 | pNew->fUSB = fUSB;
|
---|
1796 | pNew->fPrimaryIf = fPrimaryIf;
|
---|
1797 | memcpy(pNew->szName, szTmp, cchName + 1);
|
---|
1798 |
|
---|
1799 | /*
|
---|
1800 | * Link it into the list, keep the list sorted by fPrimaryIf and the BSD name.
|
---|
1801 | */
|
---|
1802 | if (pTail)
|
---|
1803 | {
|
---|
1804 | PDARWINETHERNIC pPrev = pTail;
|
---|
1805 | if (strcmp(pNew->szBSDName, pPrev->szBSDName) < 0)
|
---|
1806 | {
|
---|
1807 | pPrev = NULL;
|
---|
1808 | for (PDARWINETHERNIC pCur = pHead; pCur; pPrev = pCur, pCur = pCur->pNext)
|
---|
1809 | if ( (int)pNew->fPrimaryIf - (int)pCur->fPrimaryIf > 0
|
---|
1810 | || ( (int)pNew->fPrimaryIf - (int)pCur->fPrimaryIf == 0
|
---|
1811 | && strcmp(pNew->szBSDName, pCur->szBSDName) >= 0))
|
---|
1812 | break;
|
---|
1813 | }
|
---|
1814 | if (pPrev)
|
---|
1815 | {
|
---|
1816 | /* tail or in list. */
|
---|
1817 | pNew->pNext = pPrev->pNext;
|
---|
1818 | pPrev->pNext = pNew;
|
---|
1819 | if (pPrev == pTail)
|
---|
1820 | pTail = pNew;
|
---|
1821 | }
|
---|
1822 | else
|
---|
1823 | {
|
---|
1824 | /* head */
|
---|
1825 | pNew->pNext = pHead;
|
---|
1826 | pHead = pNew;
|
---|
1827 | }
|
---|
1828 | }
|
---|
1829 | else
|
---|
1830 | {
|
---|
1831 | /* empty list */
|
---|
1832 | pNew->pNext = NULL;
|
---|
1833 | pTail = pHead = pNew;
|
---|
1834 | }
|
---|
1835 | }
|
---|
1836 | } while (0);
|
---|
1837 |
|
---|
1838 | CFRelease(IfPropsRef);
|
---|
1839 | }
|
---|
1840 | CFRelease(PropsRef);
|
---|
1841 | }
|
---|
1842 | IOObjectRelease(EtherNICService);
|
---|
1843 | }
|
---|
1844 | else
|
---|
1845 | AssertMsgFailed(("krc=%#x\n", krc));
|
---|
1846 | IOObjectRelease(EtherIfService);
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | IOObjectRelease(EtherIfServices);
|
---|
1850 | if (ServicesRef)
|
---|
1851 | CFRelease(ServicesRef);
|
---|
1852 | if (IfsRef)
|
---|
1853 | CFRelease(IfsRef);
|
---|
1854 | return pHead;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | #ifdef STANDALONE_TESTCASE
|
---|
1858 | /**
|
---|
1859 | * This file can optionally be compiled into a testcase, this is the main function.
|
---|
1860 | * To build:
|
---|
1861 | * g++ -I ../../../../include -D IN_RING3 iokit.cpp ../../../../out/darwin.x86/debug/lib/RuntimeR3.a ../../../../out/darwin.x86/debug/lib/SUPR3.a ../../../../out/darwin.x86/debug/lib/RuntimeR3.a ../../../../out/darwin.x86/debug/lib/VBox-kStuff.a ../../../../out/darwin.x86/debug/lib/RuntimeR3.a -framework CoreFoundation -framework IOKit -framework SystemConfiguration -liconv -D STANDALONE_TESTCASE -o iokit -g && ./iokit
|
---|
1862 | */
|
---|
1863 | int main(int argc, char **argv)
|
---|
1864 | {
|
---|
1865 | RTR3InitExe(argc, &argv, 0);
|
---|
1866 |
|
---|
1867 | if (1)
|
---|
1868 | {
|
---|
1869 | /*
|
---|
1870 | * Network preferences.
|
---|
1871 | */
|
---|
1872 | RTPrintf("Preferences: Network Services\n");
|
---|
1873 | SCPreferencesRef PrefsRef = SCPreferencesCreate(kCFAllocatorDefault, CFSTR("org.virtualbox.VBoxSVC"), NULL);
|
---|
1874 | if (PrefsRef)
|
---|
1875 | {
|
---|
1876 | CFDictionaryRef NetworkServiceRef = (CFDictionaryRef)SCPreferencesGetValue(PrefsRef, kSCPrefNetworkServices);
|
---|
1877 | darwinDumpDict(NetworkServiceRef, 4);
|
---|
1878 | CFRelease(PrefsRef);
|
---|
1879 | }
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | if (1)
|
---|
1883 | {
|
---|
1884 | /*
|
---|
1885 | * Network services interfaces in the current config.
|
---|
1886 | */
|
---|
1887 | RTPrintf("Preferences: Network Service Interfaces\n");
|
---|
1888 | SCPreferencesRef PrefsRef = SCPreferencesCreate(kCFAllocatorDefault, CFSTR("org.virtualbox.VBoxSVC"), NULL);
|
---|
1889 | if (PrefsRef)
|
---|
1890 | {
|
---|
1891 | SCNetworkSetRef SetRef = SCNetworkSetCopyCurrent(PrefsRef);
|
---|
1892 | if (SetRef)
|
---|
1893 | {
|
---|
1894 | CFArrayRef ServicesRef = SCNetworkSetCopyServices(SetRef);
|
---|
1895 | CFIndex cServices = CFArrayGetCount(ServicesRef);
|
---|
1896 | for (CFIndex i = 0; i < cServices; i++)
|
---|
1897 | {
|
---|
1898 | SCNetworkServiceRef ServiceRef = (SCNetworkServiceRef)CFArrayGetValueAtIndex(ServicesRef, i);
|
---|
1899 | char szServiceName[128] = {0};
|
---|
1900 | CFStringGetCString(SCNetworkServiceGetName(ServiceRef), szServiceName, sizeof(szServiceName), kCFStringEncodingUTF8);
|
---|
1901 |
|
---|
1902 | SCNetworkInterfaceRef IfRef = SCNetworkServiceGetInterface(ServiceRef);
|
---|
1903 | char szBSDName[16] = {0};
|
---|
1904 | if (SCNetworkInterfaceGetBSDName(IfRef))
|
---|
1905 | CFStringGetCString(SCNetworkInterfaceGetBSDName(IfRef), szBSDName, sizeof(szBSDName), kCFStringEncodingUTF8);
|
---|
1906 | char szDisplayName[128] = {0};
|
---|
1907 | if (SCNetworkInterfaceGetLocalizedDisplayName(IfRef))
|
---|
1908 | CFStringGetCString(SCNetworkInterfaceGetLocalizedDisplayName(IfRef), szDisplayName, sizeof(szDisplayName), kCFStringEncodingUTF8);
|
---|
1909 |
|
---|
1910 | RTPrintf(" #%u ServiceName=\"%s\" IfBSDName=\"%s\" IfDisplayName=\"%s\"\n",
|
---|
1911 | i, szServiceName, szBSDName, szDisplayName);
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | CFRelease(ServicesRef);
|
---|
1915 | CFRelease(SetRef);
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | CFRelease(PrefsRef);
|
---|
1919 | }
|
---|
1920 | }
|
---|
1921 |
|
---|
1922 | if (1)
|
---|
1923 | {
|
---|
1924 | /*
|
---|
1925 | * Network interfaces.
|
---|
1926 | */
|
---|
1927 | RTPrintf("Preferences: Network Interfaces\n");
|
---|
1928 | CFArrayRef IfsRef = SCNetworkInterfaceCopyAll();
|
---|
1929 | if (IfsRef)
|
---|
1930 | {
|
---|
1931 | CFIndex cIfs = CFArrayGetCount(IfsRef);
|
---|
1932 | for (CFIndex i = 0; i < cIfs; i++)
|
---|
1933 | {
|
---|
1934 | SCNetworkInterfaceRef IfRef = (SCNetworkInterfaceRef)CFArrayGetValueAtIndex(IfsRef, i);
|
---|
1935 | char szBSDName[16] = {0};
|
---|
1936 | if (SCNetworkInterfaceGetBSDName(IfRef))
|
---|
1937 | CFStringGetCString(SCNetworkInterfaceGetBSDName(IfRef), szBSDName, sizeof(szBSDName), kCFStringEncodingUTF8);
|
---|
1938 | char szDisplayName[128] = {0};
|
---|
1939 | if (SCNetworkInterfaceGetLocalizedDisplayName(IfRef))
|
---|
1940 | CFStringGetCString(SCNetworkInterfaceGetLocalizedDisplayName(IfRef), szDisplayName, sizeof(szDisplayName), kCFStringEncodingUTF8);
|
---|
1941 | RTPrintf(" #%u BSDName=\"%s\" DisplayName=\"%s\"\n",
|
---|
1942 | i, szBSDName, szDisplayName);
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | CFRelease(IfsRef);
|
---|
1946 | }
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | if (1)
|
---|
1950 | {
|
---|
1951 | /*
|
---|
1952 | * Get and display the ethernet controllers.
|
---|
1953 | */
|
---|
1954 | RTPrintf("Ethernet controllers:\n");
|
---|
1955 | PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
|
---|
1956 | for (PDARWINETHERNIC pCur = pEtherNICs; pCur; pCur = pCur->pNext)
|
---|
1957 | {
|
---|
1958 | RTPrintf("%s\n", pCur->szName);
|
---|
1959 | RTPrintf(" szBSDName=%s\n", pCur->szBSDName);
|
---|
1960 | RTPrintf(" UUID=%RTuuid\n", &pCur->Uuid);
|
---|
1961 | RTPrintf(" Mac=%.6Rhxs\n", &pCur->Mac);
|
---|
1962 | RTPrintf(" fWireless=%RTbool\n", pCur->fWireless);
|
---|
1963 | RTPrintf(" fAirPort=%RTbool\n", pCur->fAirPort);
|
---|
1964 | RTPrintf(" fBuiltin=%RTbool\n", pCur->fBuiltin);
|
---|
1965 | RTPrintf(" fUSB=%RTbool\n", pCur->fUSB);
|
---|
1966 | RTPrintf(" fPrimaryIf=%RTbool\n", pCur->fPrimaryIf);
|
---|
1967 | }
|
---|
1968 | }
|
---|
1969 |
|
---|
1970 |
|
---|
1971 | return 0;
|
---|
1972 | }
|
---|
1973 | #endif
|
---|