1 | /* $Id: VUSBSniffer.cpp 53163 2014-10-29 10:14:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual USB - Sniffer facility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_DRV_VUSB
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <iprt/file.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | #include <iprt/buildconfig.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/system.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include <iprt/time.h>
|
---|
30 |
|
---|
31 | #include "VUSBSniffer.h"
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Defined Constants And Macros *
|
---|
35 | *******************************************************************************/
|
---|
36 |
|
---|
37 | /** DumpFile Section Header Block type. */
|
---|
38 | #define DUMPFILE_SHB_BLOCK_TYPE UINT32_C(0x0a0d0d0a)
|
---|
39 | /** The byte order magic value. */
|
---|
40 | #define DUMPFILE_SHB_BYTE_ORDER_MAGIC UINT32_C(0x1a2b3c4d)
|
---|
41 | /** Current major version. */
|
---|
42 | #define DUMPFILE_SHB_VERSION_MAJOR UINT16_C(1)
|
---|
43 | /** Current minor version. */
|
---|
44 | #define DUMPFILE_SHB_VERSION_MINOR UINT16_C(0)
|
---|
45 |
|
---|
46 | /** Block type for the interface descriptor block. */
|
---|
47 | #define DUMPFILE_IDB_BLOCK_TYPE UINT32_C(0x00000001)
|
---|
48 | /** USB link type. */
|
---|
49 | #define DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED UINT16_C(220)
|
---|
50 |
|
---|
51 | /** Block type for an enhanced packet block. */
|
---|
52 | #define DUMPFILE_EPB_BLOCK_TYPE UINT32_C(0x00000006)
|
---|
53 |
|
---|
54 | /** USB packet event types. */
|
---|
55 | #define DUMPFILE_USB_EVENT_TYPE_SUBMIT ('S')
|
---|
56 | #define DUMPFILE_USB_EVENT_TYPE_COMPLETE ('C')
|
---|
57 | #define DUMPFILE_USB_EVENT_TYPE_ERROR ('E')
|
---|
58 |
|
---|
59 | #define DUMPFILE_OPTION_CODE_END UINT16_C(0)
|
---|
60 | #define DUMPFILE_OPTION_CODE_COMMENT UINT16_C(1)
|
---|
61 |
|
---|
62 | #define DUMPFILE_OPTION_CODE_HARDWARE UINT16_C(2)
|
---|
63 | #define DUMPFILE_OPTION_CODE_OS UINT16_C(3)
|
---|
64 | #define DUMPFILE_OPTION_CODE_USERAPP UINT16_C(4)
|
---|
65 |
|
---|
66 | #define DUMPFILE_IDB_OPTION_TS_RESOLUTION UINT16_C(9)
|
---|
67 |
|
---|
68 | /*******************************************************************************
|
---|
69 | * DumpFile format structures *
|
---|
70 | *******************************************************************************/
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * DumpFile Block header.
|
---|
74 | */
|
---|
75 | typedef struct DumpFileBlockHdr
|
---|
76 | {
|
---|
77 | /** Block type. */
|
---|
78 | uint32_t u32BlockType;
|
---|
79 | /** Block total length. */
|
---|
80 | uint32_t u32BlockTotalLength;
|
---|
81 | } DumpFileBlockHdr;
|
---|
82 | /** Pointer to a block header. */
|
---|
83 | typedef DumpFileBlockHdr *PDumpFileBlockHdr;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * DumpFile Option header.
|
---|
87 | */
|
---|
88 | typedef struct DumpFileOptionHdr
|
---|
89 | {
|
---|
90 | /** Option code. */
|
---|
91 | uint16_t u16OptionCode;
|
---|
92 | /** Block total length. */
|
---|
93 | uint16_t u16OptionLength;
|
---|
94 | } DumpFileOptionHdr;
|
---|
95 | /** Pointer to a option header. */
|
---|
96 | typedef DumpFileOptionHdr *PDumpFileOptionHdr;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * DumpFile Section Header Block.
|
---|
100 | */
|
---|
101 | typedef struct DumpFileShb
|
---|
102 | {
|
---|
103 | /** Block header. */
|
---|
104 | DumpFileBlockHdr Hdr;
|
---|
105 | /** Byte order magic. */
|
---|
106 | uint32_t u32ByteOrderMagic;
|
---|
107 | /** Major version. */
|
---|
108 | uint16_t u16VersionMajor;
|
---|
109 | /** Minor version. */
|
---|
110 | uint16_t u16VersionMinor;
|
---|
111 | /** Section length. */
|
---|
112 | uint64_t u64SectionLength;
|
---|
113 | } DumpFileShb;
|
---|
114 | /** Pointer to a Section Header Block. */
|
---|
115 | typedef DumpFileShb *PDumpFileShb;
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * DumpFile Interface description block.
|
---|
119 | */
|
---|
120 | typedef struct DumpFileIdb
|
---|
121 | {
|
---|
122 | /** Block header. */
|
---|
123 | DumpFileBlockHdr Hdr;
|
---|
124 | /** Link type. */
|
---|
125 | uint16_t u16LinkType;
|
---|
126 | /** Reserved. */
|
---|
127 | uint16_t u16Reserved;
|
---|
128 | /** Maximum number of bytes dumped from each packet. */
|
---|
129 | uint32_t u32SnapLen;
|
---|
130 | } DumpFileIdb;
|
---|
131 | /** Pointer to an Interface description block. */
|
---|
132 | typedef DumpFileIdb *PDumpFileIdb;
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * DumpFile Enhanced packet block.
|
---|
136 | */
|
---|
137 | typedef struct DumpFileEpb
|
---|
138 | {
|
---|
139 | /** Block header. */
|
---|
140 | DumpFileBlockHdr Hdr;
|
---|
141 | /** Interface ID. */
|
---|
142 | uint32_t u32InterfaceId;
|
---|
143 | /** Timestamp (high). */
|
---|
144 | uint32_t u32TimestampHigh;
|
---|
145 | /** Timestamp (low). */
|
---|
146 | uint32_t u32TimestampLow;
|
---|
147 | /** Captured packet length. */
|
---|
148 | uint32_t u32CapturedLen;
|
---|
149 | /** Original packet length. */
|
---|
150 | uint32_t u32PacketLen;
|
---|
151 | } DumpFileEpb;
|
---|
152 | /** Pointer to an Enhanced packet block. */
|
---|
153 | typedef DumpFileEpb *PDumpFileEpb;
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * USB setup URB data.
|
---|
157 | */
|
---|
158 | typedef struct DumpFileUsbSetup
|
---|
159 | {
|
---|
160 | uint8_t bmRequestType;
|
---|
161 | uint8_t bRequest;
|
---|
162 | uint16_t wValue;
|
---|
163 | uint16_t wIndex;
|
---|
164 | uint16_t wLength;
|
---|
165 | } DumpFileUsbSetup;
|
---|
166 | typedef DumpFileUsbSetup *PDumpFileUsbSetup;
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * USB Isochronous data.
|
---|
170 | */
|
---|
171 | typedef struct DumpFileIsoRec
|
---|
172 | {
|
---|
173 | int32_t i32ErrorCount;
|
---|
174 | int32_t i32NumDesc;
|
---|
175 | } DumpFileIsoRec;
|
---|
176 | typedef DumpFileIsoRec *PDumpFileIsoRec;
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * USB packet header (Linux mmapped variant).
|
---|
180 | */
|
---|
181 | typedef struct DumpFileUsbHeaderLnxMmapped
|
---|
182 | {
|
---|
183 | /** Packet Id. */
|
---|
184 | uint64_t u64Id;
|
---|
185 | /** Event type. */
|
---|
186 | uint8_t u8EventType;
|
---|
187 | /** Transfer type. */
|
---|
188 | uint8_t u8TransferType;
|
---|
189 | /** Endpoint number. */
|
---|
190 | uint8_t u8EndpointNumber;
|
---|
191 | /** Device address. */
|
---|
192 | uint8_t u8DeviceAddress;
|
---|
193 | /** Bus id. */
|
---|
194 | uint16_t u16BusId;
|
---|
195 | /** Setup flag != 0 if the URB setup header is not present. */
|
---|
196 | uint8_t u8SetupFlag;
|
---|
197 | /** Data present flag != 0 if the URB data is not present. */
|
---|
198 | uint8_t u8DataFlag;
|
---|
199 | /** Timestamp (second part). */
|
---|
200 | uint64_t u64TimestampSec;
|
---|
201 | /** Timestamp (us part). */
|
---|
202 | uint32_t u32TimestampUSec;
|
---|
203 | /** Status. */
|
---|
204 | int32_t i32Status;
|
---|
205 | /** URB length. */
|
---|
206 | uint32_t u32UrbLength;
|
---|
207 | /** Recorded data length. */
|
---|
208 | uint32_t u32DataLength;
|
---|
209 | /** Union of data for different URB types. */
|
---|
210 | union
|
---|
211 | {
|
---|
212 | DumpFileUsbSetup UsbSetup;
|
---|
213 | DumpFileIsoRec IsoRec;
|
---|
214 | } u;
|
---|
215 | int32_t i32Interval;
|
---|
216 | int32_t i32StartFrame;
|
---|
217 | /** Copy of transfer flags. */
|
---|
218 | uint32_t u32XferFlags;
|
---|
219 | /** Number of isochronous descriptors. */
|
---|
220 | uint32_t u32NumDesc;
|
---|
221 | } DumpFileUsbHeaderLnxMmapped;
|
---|
222 | /** Pointer to a USB packet header. */
|
---|
223 | typedef DumpFileUsbHeaderLnxMmapped *PDumpFileUsbHeaderLnxMmapped;
|
---|
224 |
|
---|
225 | AssertCompileSize(DumpFileUsbHeaderLnxMmapped, 64);
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * USB packet isochronous descriptor.
|
---|
229 | */
|
---|
230 | typedef struct DumpFileUsbIsoDesc
|
---|
231 | {
|
---|
232 | int32_t i32Status;
|
---|
233 | uint32_t u32Offset;
|
---|
234 | uint32_t u32Len;
|
---|
235 | uint8_t au8Padding[4];
|
---|
236 | } DumpFileUsbIsoDesc;
|
---|
237 | typedef DumpFileUsbIsoDesc *PDumpFileUsbIsoDesc;
|
---|
238 |
|
---|
239 | /*******************************************************************************
|
---|
240 | * Structures and Typedefs *
|
---|
241 | *******************************************************************************/
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * The internal VUSB sniffer state.
|
---|
245 | */
|
---|
246 | typedef struct VUSBSNIFFERINT
|
---|
247 | {
|
---|
248 | /** The file handle to dump to. */
|
---|
249 | RTFILE hFile;
|
---|
250 | /** Current size of the block being written. */
|
---|
251 | uint32_t cbBlockCur;
|
---|
252 | /** Maximum size allocated for the block. */
|
---|
253 | uint32_t cbBlockMax;
|
---|
254 | /** Current block header. */
|
---|
255 | PDumpFileBlockHdr pBlockHdr;
|
---|
256 | /** Pointer to the block data which will be written on commit. */
|
---|
257 | uint8_t *pbBlockData;
|
---|
258 | /** Fast Mutex protecting the state against concurrent access. */
|
---|
259 | RTSEMFASTMUTEX hMtx;
|
---|
260 | } VUSBSNIFFERINT;
|
---|
261 | /** Pointer to the internal VUSB sniffer state. */
|
---|
262 | typedef VUSBSNIFFERINT *PVUSBSNIFFERINT;
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Allocates additional space for the block.
|
---|
266 | *
|
---|
267 | * @returns Pointer to the new unused space or NULL if out of memory.
|
---|
268 | * @param pThis The VUSB sniffer instance.
|
---|
269 | * @param cbAdditional The additional memory requested.
|
---|
270 | */
|
---|
271 | static void *vusbSnifferBlockAllocSpace(PVUSBSNIFFERINT pThis, uint32_t cbAdditional)
|
---|
272 | {
|
---|
273 | /* Fast path where we have enough memory allocated. */
|
---|
274 | if (pThis->cbBlockCur + cbAdditional <= pThis->cbBlockMax)
|
---|
275 | {
|
---|
276 | void *pv = pThis->pbBlockData + pThis->cbBlockCur;
|
---|
277 | pThis->cbBlockCur += cbAdditional;
|
---|
278 | return pv;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* Allocate additional memory. */
|
---|
282 | uint32_t cbNew = pThis->cbBlockCur + cbAdditional;
|
---|
283 | uint8_t *pbDataNew = (uint8_t *)RTMemRealloc(pThis->pbBlockData, cbNew);
|
---|
284 | if (pbDataNew)
|
---|
285 | {
|
---|
286 | pThis->pbBlockData = pbDataNew;
|
---|
287 | pThis->pBlockHdr = (PDumpFileBlockHdr)pbDataNew;
|
---|
288 |
|
---|
289 | void *pv = pThis->pbBlockData + pThis->cbBlockCur;
|
---|
290 | pThis->cbBlockCur = cbNew;
|
---|
291 | pThis->cbBlockMax = cbNew;
|
---|
292 | return pv;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return NULL;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Adds new data to the current block.
|
---|
300 | *
|
---|
301 | * @returns VBox status code.
|
---|
302 | * @param pThis The VUSB sniffer instance.
|
---|
303 | * @param pvData The data to add.
|
---|
304 | * @param cbData Amount of data to add.
|
---|
305 | */
|
---|
306 | static int vusbSnifferBlockAddData(PVUSBSNIFFERINT pThis, const void *pvData, uint32_t cbData)
|
---|
307 | {
|
---|
308 | int rc = VINF_SUCCESS;
|
---|
309 |
|
---|
310 | Assert(pThis->cbBlockCur);
|
---|
311 | AssertPtr(pThis->pBlockHdr);
|
---|
312 |
|
---|
313 | void *pv = vusbSnifferBlockAllocSpace(pThis, cbData);
|
---|
314 | if (pv)
|
---|
315 | memcpy(pv, pvData, cbData);
|
---|
316 | else
|
---|
317 | rc = VERR_NO_MEMORY;
|
---|
318 |
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Aligns the current block data to a 32bit boundary.
|
---|
324 | *
|
---|
325 | * @returns VBox status code.
|
---|
326 | * @param pThis The VUSB sniffer instance.
|
---|
327 | */
|
---|
328 | static int vusbSnifferBlockAlign(PVUSBSNIFFERINT pThis)
|
---|
329 | {
|
---|
330 | int rc = VINF_SUCCESS;
|
---|
331 |
|
---|
332 | Assert(pThis->cbBlockCur);
|
---|
333 |
|
---|
334 | /* Pad to 32bits. */
|
---|
335 | uint8_t abPad[3] = { 0 };
|
---|
336 | uint32_t cbPad = RT_ALIGN_32(pThis->cbBlockCur, 4) - pThis->cbBlockCur;
|
---|
337 |
|
---|
338 | Assert(cbPad <= 3);
|
---|
339 | if (cbPad)
|
---|
340 | rc = vusbSnifferBlockAddData(pThis, abPad, cbPad);
|
---|
341 |
|
---|
342 | return rc;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Commits the current block to the capture file.
|
---|
347 | *
|
---|
348 | * @returns VBox status code.
|
---|
349 | * @param pThis The VUSB sniffer instance.
|
---|
350 | */
|
---|
351 | static int vusbSnifferBlockCommit(PVUSBSNIFFERINT pThis)
|
---|
352 | {
|
---|
353 | int rc = VINF_SUCCESS;
|
---|
354 |
|
---|
355 | AssertPtr(pThis->pBlockHdr);
|
---|
356 |
|
---|
357 | rc = vusbSnifferBlockAlign(pThis);
|
---|
358 | if (RT_SUCCESS(rc))
|
---|
359 | {
|
---|
360 | /* Update the block total length field. */
|
---|
361 | uint32_t *pcbTotalLength = (uint32_t *)vusbSnifferBlockAllocSpace(pThis, 4);
|
---|
362 | if (pcbTotalLength)
|
---|
363 | {
|
---|
364 | *pcbTotalLength = pThis->cbBlockCur;
|
---|
365 | pThis->pBlockHdr->u32BlockTotalLength = pThis->cbBlockCur;
|
---|
366 |
|
---|
367 | /* Write the data. */
|
---|
368 | rc = RTFileWrite(pThis->hFile, pThis->pbBlockData, pThis->cbBlockCur, NULL);
|
---|
369 | pThis->cbBlockCur = 0;
|
---|
370 | pThis->pBlockHdr = NULL;
|
---|
371 | }
|
---|
372 | else
|
---|
373 | rc = VERR_NO_MEMORY;
|
---|
374 | }
|
---|
375 |
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Starts a new block for capturing.
|
---|
381 | *
|
---|
382 | * @returns VBox status code.
|
---|
383 | * @param pThis The VUSB sniffer instance.
|
---|
384 | * @param pBlockHdr Pointer to the block header for the new block.
|
---|
385 | * @param cbData Amount of data added with this block.
|
---|
386 | */
|
---|
387 | static int vusbSnifferBlockNew(PVUSBSNIFFERINT pThis, PDumpFileBlockHdr pBlockHdr, uint32_t cbData)
|
---|
388 | {
|
---|
389 | int rc = VINF_SUCCESS;
|
---|
390 |
|
---|
391 | /* Validate we don't get called while another block is active. */
|
---|
392 | Assert(!pThis->cbBlockCur);
|
---|
393 | Assert(!pThis->pBlockHdr);
|
---|
394 | pThis->pBlockHdr = (PDumpFileBlockHdr)vusbSnifferBlockAllocSpace(pThis, cbData);
|
---|
395 | if (pThis->pBlockHdr)
|
---|
396 | memcpy(pThis->pBlockHdr, pBlockHdr, cbData);
|
---|
397 | else
|
---|
398 | rc = VERR_NO_MEMORY;
|
---|
399 |
|
---|
400 | return rc;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Add a new option to the current block.
|
---|
405 | *
|
---|
406 | * @returns VBox status code.
|
---|
407 | * @param pThis The VUSB sniffer instance.
|
---|
408 | * @param u16OptionCode The option code identifying the type of option.
|
---|
409 | * @param pvOption Raw data for the option.
|
---|
410 | * @param cbOption Size of the optiob data.
|
---|
411 | */
|
---|
412 | static int vusbSnifferAddOption(PVUSBSNIFFERINT pThis, uint16_t u16OptionCode, const void *pvOption, uint16_t cbOption)
|
---|
413 | {
|
---|
414 | int rc = VINF_SUCCESS;
|
---|
415 | DumpFileOptionHdr OptHdr;
|
---|
416 |
|
---|
417 | OptHdr.u16OptionCode = u16OptionCode;
|
---|
418 | OptHdr.u16OptionLength = cbOption;
|
---|
419 | rc = vusbSnifferBlockAddData(pThis, &OptHdr, sizeof(OptHdr));
|
---|
420 | if ( RT_SUCCESS(rc)
|
---|
421 | && u16OptionCode != DUMPFILE_OPTION_CODE_END
|
---|
422 | && cbOption != 0)
|
---|
423 | {
|
---|
424 | rc = vusbSnifferBlockAddData(pThis, pvOption, cbOption);
|
---|
425 | if (RT_SUCCESS(rc))
|
---|
426 | rc = vusbSnifferBlockAlign(pThis);
|
---|
427 | }
|
---|
428 |
|
---|
429 | return rc;
|
---|
430 | }
|
---|
431 |
|
---|
432 | DECLHIDDEN(int) VUSBSnifferCreate(PVUSBSNIFFER phSniffer, uint32_t fFlags,
|
---|
433 | const char *pszCaptureFilename, const char *pszDesc)
|
---|
434 | {
|
---|
435 | int rc = VINF_SUCCESS;
|
---|
436 | PVUSBSNIFFERINT pThis = NULL;
|
---|
437 |
|
---|
438 | pThis = (PVUSBSNIFFERINT)RTMemAllocZ(sizeof(VUSBSNIFFERINT));
|
---|
439 | if (pThis)
|
---|
440 | {
|
---|
441 | pThis->hFile = NIL_RTFILE;
|
---|
442 | pThis->cbBlockCur = 0;
|
---|
443 | pThis->cbBlockMax = 0;
|
---|
444 | pThis->pbBlockData = NULL;
|
---|
445 | pThis->hMtx = NIL_RTSEMFASTMUTEX;
|
---|
446 |
|
---|
447 | rc = RTSemFastMutexCreate(&pThis->hMtx);
|
---|
448 | if (RT_SUCCESS(rc))
|
---|
449 | {
|
---|
450 | rc = RTFileOpen(&pThis->hFile, pszCaptureFilename, RTFILE_O_DENY_NONE | RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_READ);
|
---|
451 | if (RT_SUCCESS(rc))
|
---|
452 | {
|
---|
453 | /* Write header and link type blocks. */
|
---|
454 | DumpFileShb Shb;
|
---|
455 |
|
---|
456 | Shb.Hdr.u32BlockType = DUMPFILE_SHB_BLOCK_TYPE;
|
---|
457 | Shb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
|
---|
458 | Shb.u32ByteOrderMagic = DUMPFILE_SHB_BYTE_ORDER_MAGIC;
|
---|
459 | Shb.u16VersionMajor = DUMPFILE_SHB_VERSION_MAJOR;
|
---|
460 | Shb.u16VersionMinor = DUMPFILE_SHB_VERSION_MINOR;
|
---|
461 | Shb.u64SectionLength = UINT64_C(0xffffffffffffffff); /* -1 */
|
---|
462 |
|
---|
463 | /* Write the blocks. */
|
---|
464 | rc = vusbSnifferBlockNew(pThis, &Shb.Hdr, sizeof(Shb));
|
---|
465 | if (RT_SUCCESS(rc))
|
---|
466 | {
|
---|
467 | const char *pszOpt = RTBldCfgTargetDotArch();
|
---|
468 | rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_HARDWARE, pszOpt, strlen(pszOpt) + 1);
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (RT_SUCCESS(rc))
|
---|
472 | {
|
---|
473 | char szTmp[512];
|
---|
474 | size_t cbTmp = sizeof(szTmp);
|
---|
475 |
|
---|
476 | RT_ZERO(szTmp);
|
---|
477 |
|
---|
478 | /* Build the OS code. */
|
---|
479 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, cbTmp);
|
---|
480 | if (RT_SUCCESS(rc))
|
---|
481 | {
|
---|
482 | size_t cb = strlen(szTmp);
|
---|
483 |
|
---|
484 | szTmp[cb] = ' ';
|
---|
485 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, &szTmp[cb + 1], cbTmp - (cb + 1));
|
---|
486 | if (RT_SUCCESS(rc))
|
---|
487 | {
|
---|
488 | cb = strlen(szTmp);
|
---|
489 | szTmp[cb] = ' ';
|
---|
490 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, &szTmp[cb + 1], cbTmp - (cb + 1));
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 | if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)
|
---|
495 | rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_OS, szTmp, strlen(szTmp) + 1);
|
---|
496 | else
|
---|
497 | rc = VINF_SUCCESS; /* Skip OS code if building the string failed. */
|
---|
498 | }
|
---|
499 |
|
---|
500 | if (RT_SUCCESS(rc))
|
---|
501 | {
|
---|
502 | /** @todo: Add product info. */
|
---|
503 | }
|
---|
504 |
|
---|
505 | if (RT_SUCCESS(rc))
|
---|
506 | rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
|
---|
507 | if (RT_SUCCESS(rc))
|
---|
508 | rc = vusbSnifferBlockCommit(pThis);
|
---|
509 |
|
---|
510 | /* Write Interface descriptor block. */
|
---|
511 | if (RT_SUCCESS(rc))
|
---|
512 | {
|
---|
513 | DumpFileIdb Idb;
|
---|
514 |
|
---|
515 | Idb.Hdr.u32BlockType = DUMPFILE_IDB_BLOCK_TYPE;
|
---|
516 | Idb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
|
---|
517 | Idb.u16LinkType = DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED;
|
---|
518 | Idb.u16Reserved = 0;
|
---|
519 | Idb.u32SnapLen = UINT32_C(0xffffffff);
|
---|
520 |
|
---|
521 | rc = vusbSnifferBlockNew(pThis, &Idb.Hdr, sizeof(Idb));
|
---|
522 | if (RT_SUCCESS(rc))
|
---|
523 | {
|
---|
524 | uint8_t u8TsResolution = 9; /* Nano second resolution. */
|
---|
525 | /* Add timestamp resolution option. */
|
---|
526 | rc = vusbSnifferAddOption(pThis, DUMPFILE_IDB_OPTION_TS_RESOLUTION,
|
---|
527 | &u8TsResolution, sizeof(u8TsResolution));
|
---|
528 | }
|
---|
529 | if (RT_SUCCESS(rc))
|
---|
530 | rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
|
---|
531 | if (RT_SUCCESS(rc))
|
---|
532 | rc = vusbSnifferBlockCommit(pThis);
|
---|
533 | }
|
---|
534 |
|
---|
535 | if (RT_SUCCESS(rc))
|
---|
536 | {
|
---|
537 | *phSniffer = pThis;
|
---|
538 | return VINF_SUCCESS;
|
---|
539 | }
|
---|
540 |
|
---|
541 | RTFileClose(pThis->hFile);
|
---|
542 | pThis->hFile = NIL_RTFILE;
|
---|
543 | RTFileDelete(pszCaptureFilename);
|
---|
544 | }
|
---|
545 | RTSemFastMutexDestroy(pThis->hMtx);
|
---|
546 | pThis->hMtx = NIL_RTSEMFASTMUTEX;
|
---|
547 | }
|
---|
548 | RTMemFree(pThis);
|
---|
549 | }
|
---|
550 | else
|
---|
551 | rc = VERR_NO_MEMORY;
|
---|
552 |
|
---|
553 | return rc;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /**
|
---|
557 | * Destroys the given VUSB sniffer instance.
|
---|
558 | *
|
---|
559 | * @returns nothing.
|
---|
560 | * @param hSniffer The sniffer instance to destroy.
|
---|
561 | */
|
---|
562 | DECLHIDDEN(void) VUSBSnifferDestroy(VUSBSNIFFER hSniffer)
|
---|
563 | {
|
---|
564 | PVUSBSNIFFERINT pThis = hSniffer;
|
---|
565 |
|
---|
566 | int rc = RTSemFastMutexRequest(pThis->hMtx);
|
---|
567 | AssertRC(rc);
|
---|
568 |
|
---|
569 | if (pThis->hFile != NIL_RTFILE)
|
---|
570 | RTFileClose(pThis->hFile);
|
---|
571 | if (pThis->pbBlockData)
|
---|
572 | RTMemFree(pThis->pbBlockData);
|
---|
573 |
|
---|
574 | RTSemFastMutexRelease(pThis->hMtx);
|
---|
575 | RTSemFastMutexDestroy(pThis->hMtx);
|
---|
576 | RTMemFree(pThis);
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Records an VUSB event.
|
---|
581 | *
|
---|
582 | * @returns VBox status code.
|
---|
583 | * @param hSniffer The sniffer instance.
|
---|
584 | * @param pUrb The URB triggering the event.
|
---|
585 | * @param enmEvent The type of event to record.
|
---|
586 | */
|
---|
587 | DECLHIDDEN(int) VUSBSnifferRecordEvent(VUSBSNIFFER hSniffer, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
|
---|
588 | {
|
---|
589 | int rc = VINF_SUCCESS;
|
---|
590 | PVUSBSNIFFERINT pThis = hSniffer;
|
---|
591 | DumpFileEpb Epb;
|
---|
592 | DumpFileUsbHeaderLnxMmapped UsbHdr;
|
---|
593 | DumpFileUsbSetup UsbSetup;
|
---|
594 | RTTIMESPEC TimeNow;
|
---|
595 | uint64_t u64TimestampEvent;
|
---|
596 | size_t cbUrbLength = 0;
|
---|
597 | uint32_t cbDataLength = 0;
|
---|
598 | uint32_t cbCapturedLength = sizeof(UsbHdr);
|
---|
599 | uint32_t cIsocPkts = 0;
|
---|
600 |
|
---|
601 | RTTimeNow(&TimeNow);
|
---|
602 | u64TimestampEvent = RTTimeSpecGetNano(&TimeNow);
|
---|
603 |
|
---|
604 | /* Start with the enhanced packet block. */
|
---|
605 | Epb.Hdr.u32BlockType = DUMPFILE_EPB_BLOCK_TYPE;
|
---|
606 | Epb.Hdr.u32BlockTotalLength = 0;
|
---|
607 | Epb.u32InterfaceId = 0;
|
---|
608 | Epb.u32TimestampHigh = (u64TimestampEvent >> 32) & UINT32_C(0xffffffff);
|
---|
609 | Epb.u32TimestampLow = u64TimestampEvent & UINT32_C(0xffffffff);
|
---|
610 |
|
---|
611 | UsbHdr.u64Id = (uint64_t)pUrb; /** @todo: check whether the pointer is a good ID. */
|
---|
612 | switch (enmEvent)
|
---|
613 | {
|
---|
614 | case VUSBSNIFFEREVENT_SUBMIT:
|
---|
615 | UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_SUBMIT;
|
---|
616 | cbUrbLength = pUrb->cbData;
|
---|
617 | break;
|
---|
618 | case VUSBSNIFFEREVENT_COMPLETE:
|
---|
619 | UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_COMPLETE;
|
---|
620 | cbUrbLength = pUrb->cbData;
|
---|
621 | break;
|
---|
622 | case VUSBSNIFFEREVENT_ERROR_SUBMIT:
|
---|
623 | case VUSBSNIFFEREVENT_ERROR_COMPLETE:
|
---|
624 | UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_ERROR;
|
---|
625 | break;
|
---|
626 | default:
|
---|
627 | AssertMsgFailed(("Invalid event type %d\n", enmEvent));
|
---|
628 | }
|
---|
629 | cbDataLength = cbUrbLength;
|
---|
630 |
|
---|
631 | switch (pUrb->enmType)
|
---|
632 | {
|
---|
633 | case VUSBXFERTYPE_ISOC:
|
---|
634 | {
|
---|
635 | int32_t i32ErrorCount = 0;
|
---|
636 |
|
---|
637 | UsbHdr.u8TransferType = 0;
|
---|
638 | cIsocPkts = pUrb->cIsocPkts;
|
---|
639 | for (unsigned i = 0; i < cIsocPkts; i++)
|
---|
640 | if ( pUrb->aIsocPkts[i].enmStatus != VUSBSTATUS_OK
|
---|
641 | && pUrb->aIsocPkts[i].enmStatus != VUSBSTATUS_NOT_ACCESSED)
|
---|
642 | i32ErrorCount++;
|
---|
643 |
|
---|
644 | UsbHdr.u.IsoRec.i32ErrorCount = i32ErrorCount;
|
---|
645 | UsbHdr.u.IsoRec.i32NumDesc = pUrb->cIsocPkts;
|
---|
646 | cbCapturedLength += cIsocPkts * sizeof(DumpFileUsbIsoDesc);
|
---|
647 | break;
|
---|
648 | }
|
---|
649 | case VUSBXFERTYPE_BULK:
|
---|
650 | UsbHdr.u8TransferType = 3;
|
---|
651 | break;
|
---|
652 | case VUSBXFERTYPE_INTR:
|
---|
653 | UsbHdr.u8TransferType = 1;
|
---|
654 | break;
|
---|
655 | case VUSBXFERTYPE_CTRL:
|
---|
656 | case VUSBXFERTYPE_MSG:
|
---|
657 | UsbHdr.u8TransferType = 2;
|
---|
658 | break;
|
---|
659 | default:
|
---|
660 | AssertMsgFailed(("invalid transfer type %d\n", pUrb->enmType));
|
---|
661 | }
|
---|
662 |
|
---|
663 | if (pUrb->enmDir == VUSBDIRECTION_IN)
|
---|
664 | {
|
---|
665 | if (enmEvent == VUSBSNIFFEREVENT_SUBMIT)
|
---|
666 | cbDataLength = 0;
|
---|
667 | }
|
---|
668 | else if (pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
669 | {
|
---|
670 | if ( enmEvent == VUSBSNIFFEREVENT_COMPLETE
|
---|
671 | || pUrb->enmType == VUSBXFERTYPE_CTRL
|
---|
672 | || pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
673 | cbDataLength = 0;
|
---|
674 | }
|
---|
675 |
|
---|
676 | Epb.u32CapturedLen = cbCapturedLength + cbDataLength;
|
---|
677 | Epb.u32PacketLen = cbCapturedLength + cbUrbLength;
|
---|
678 |
|
---|
679 | UsbHdr.u8EndpointNumber = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0x00);
|
---|
680 | UsbHdr.u8DeviceAddress = pUrb->DstAddress;
|
---|
681 | UsbHdr.u16BusId = 0;
|
---|
682 | UsbHdr.u8DataFlag = cbDataLength ? 0 : 1;
|
---|
683 | UsbHdr.u64TimestampSec = u64TimestampEvent / RT_NS_1SEC_64;
|
---|
684 | UsbHdr.u32TimestampUSec = u64TimestampEvent / RT_NS_1US_64 - UsbHdr.u64TimestampSec * RT_US_1SEC;
|
---|
685 | UsbHdr.i32Status = pUrb->enmStatus;
|
---|
686 | UsbHdr.u32UrbLength = cbUrbLength;
|
---|
687 | UsbHdr.u32DataLength = cbDataLength + cIsocPkts * sizeof(DumpFileUsbIsoDesc);
|
---|
688 | UsbHdr.i32Interval = 0;
|
---|
689 | UsbHdr.i32StartFrame = 0;
|
---|
690 | UsbHdr.u32XferFlags = 0;
|
---|
691 | UsbHdr.u32NumDesc = cIsocPkts;
|
---|
692 |
|
---|
693 | if ( (pUrb->enmType == VUSBXFERTYPE_MSG || pUrb->enmType == VUSBXFERTYPE_CTRL)
|
---|
694 | && enmEvent == VUSBSNIFFEREVENT_SUBMIT)
|
---|
695 | {
|
---|
696 | PVUSBSETUP pSetup = (PVUSBSETUP)pUrb->abData;
|
---|
697 |
|
---|
698 | UsbHdr.u.UsbSetup.bmRequestType = pSetup->bmRequestType;
|
---|
699 | UsbHdr.u.UsbSetup.bRequest = pSetup->bRequest;
|
---|
700 | UsbHdr.u.UsbSetup.wValue = pSetup->wValue;
|
---|
701 | UsbHdr.u.UsbSetup.wIndex = pSetup->wIndex;
|
---|
702 | UsbHdr.u.UsbSetup.wLength = pSetup->wLength;
|
---|
703 | UsbHdr.u8SetupFlag = 0;
|
---|
704 | }
|
---|
705 | else
|
---|
706 | UsbHdr.u8SetupFlag = '-'; /* Follow usbmon source here. */
|
---|
707 |
|
---|
708 | /* Write the packet to the capture file. */
|
---|
709 | rc = RTSemFastMutexRequest(pThis->hMtx);
|
---|
710 | if (RT_SUCCESS(rc))
|
---|
711 | {
|
---|
712 | rc = vusbSnifferBlockNew(pThis, &Epb.Hdr, sizeof(Epb));
|
---|
713 | if (RT_SUCCESS(rc))
|
---|
714 | rc = vusbSnifferBlockAddData(pThis, &UsbHdr, sizeof(UsbHdr));
|
---|
715 |
|
---|
716 | /* Add Isochronous descriptors now. */
|
---|
717 | for (unsigned i = 0; i < cIsocPkts && RT_SUCCESS(rc); i++)
|
---|
718 | {
|
---|
719 | DumpFileUsbIsoDesc IsoDesc;
|
---|
720 | IsoDesc.i32Status = pUrb->aIsocPkts[i].enmStatus;
|
---|
721 | IsoDesc.u32Offset = pUrb->aIsocPkts[i].off;
|
---|
722 | IsoDesc.u32Len = pUrb->aIsocPkts[i].cb;
|
---|
723 | rc = vusbSnifferBlockAddData(pThis, &IsoDesc, sizeof(IsoDesc));
|
---|
724 | }
|
---|
725 |
|
---|
726 | /* Record data. */
|
---|
727 | if ( RT_SUCCESS(rc)
|
---|
728 | && cbDataLength)
|
---|
729 | rc = vusbSnifferBlockAddData(pThis, pUrb->abData, pUrb->cbData);
|
---|
730 |
|
---|
731 | if (RT_SUCCESS(rc))
|
---|
732 | rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
|
---|
733 |
|
---|
734 | if (RT_SUCCESS(rc))
|
---|
735 | rc = vusbSnifferBlockCommit(pThis);
|
---|
736 |
|
---|
737 | RTSemFastMutexRelease(pThis->hMtx);
|
---|
738 | }
|
---|
739 |
|
---|
740 | return rc;
|
---|
741 | }
|
---|
742 |
|
---|