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