1 // Copyright 2019 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 use crate::types::{self, Descriptor, DescriptorHeader, EndpointDescriptor};
6 use crate::{Error, Result};
7 use base::warn;
8 use data_model::DataInit;
9 use std::collections::BTreeMap;
10 use std::mem::size_of;
11 use std::ops::Deref;
12
13 #[derive(Clone)]
14 pub struct DeviceDescriptorTree {
15 // Full descriptor tree in the original format returned by the device.
16 raw: Vec<u8>,
17 inner: types::DeviceDescriptor,
18 // Map of bConfigurationValue to ConfigDescriptor
19 config_descriptors: BTreeMap<u8, ConfigDescriptorTree>,
20 // Map of config index to bConfigurationValue.
21 config_values: BTreeMap<u8, u8>,
22 }
23
24 #[derive(Clone)]
25 pub struct ConfigDescriptorTree {
26 offset: usize,
27 inner: types::ConfigDescriptor,
28 // Map of (bInterfaceNumber, bAlternateSetting) to InterfaceDescriptor
29 interface_descriptors: BTreeMap<(u8, u8), InterfaceDescriptorTree>,
30 }
31
32 #[derive(Clone)]
33 pub struct InterfaceDescriptorTree {
34 offset: usize,
35 inner: types::InterfaceDescriptor,
36 // Map of bEndpointAddress to EndpointDescriptor
37 endpoint_descriptors: BTreeMap<u8, EndpointDescriptor>,
38 }
39
40 impl DeviceDescriptorTree {
get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree>41 pub fn get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree> {
42 self.config_descriptors.get(&config_value)
43 }
44
45 /// Retrieve the Nth configuration descriptor in the device descriptor.
46 /// `config_index`: 0-based index into the list of configuration descriptors.
get_config_descriptor_by_index( &self, config_index: u8, ) -> Option<&ConfigDescriptorTree>47 pub fn get_config_descriptor_by_index(
48 &self,
49 config_index: u8,
50 ) -> Option<&ConfigDescriptorTree> {
51 self.config_descriptors
52 .get(self.config_values.get(&config_index)?)
53 }
54
55 /// Access the raw descriptor tree as a slice of bytes.
raw(&self) -> &[u8]56 pub fn raw(&self) -> &[u8] {
57 &self.raw
58 }
59 }
60
61 impl Deref for DeviceDescriptorTree {
62 type Target = types::DeviceDescriptor;
63
deref(&self) -> &types::DeviceDescriptor64 fn deref(&self) -> &types::DeviceDescriptor {
65 &self.inner
66 }
67 }
68
69 impl ConfigDescriptorTree {
70 /// Get interface by number and alt setting.
get_interface_descriptor( &self, interface_num: u8, alt_setting: u8, ) -> Option<&InterfaceDescriptorTree>71 pub fn get_interface_descriptor(
72 &self,
73 interface_num: u8,
74 alt_setting: u8,
75 ) -> Option<&InterfaceDescriptorTree> {
76 self.interface_descriptors
77 .get(&(interface_num, alt_setting))
78 }
79
80 /// Get the offset of this configuration descriptor within the raw descriptor tree.
offset(&self) -> usize81 pub fn offset(&self) -> usize {
82 self.offset
83 }
84 }
85
86 impl Deref for ConfigDescriptorTree {
87 type Target = types::ConfigDescriptor;
88
deref(&self) -> &types::ConfigDescriptor89 fn deref(&self) -> &types::ConfigDescriptor {
90 &self.inner
91 }
92 }
93
94 impl InterfaceDescriptorTree {
get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor>95 pub fn get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor> {
96 self.endpoint_descriptors.get(&ep_idx)
97 }
98
99 /// Get the offset of this interface descriptor within the raw descriptor tree.
offset(&self) -> usize100 pub fn offset(&self) -> usize {
101 self.offset
102 }
103 }
104
105 impl Deref for InterfaceDescriptorTree {
106 type Target = types::InterfaceDescriptor;
107
deref(&self) -> &types::InterfaceDescriptor108 fn deref(&self) -> &types::InterfaceDescriptor {
109 &self.inner
110 }
111 }
112
113 /// Given `data` containing a full set of descriptors as provided by the Linux kernel
114 /// usbdevfs `descriptors` file, parse the descriptors into a tree data structure.
parse_usbfs_descriptors(data: &[u8]) -> Result<DeviceDescriptorTree>115 pub fn parse_usbfs_descriptors(data: &[u8]) -> Result<DeviceDescriptorTree> {
116 let mut offset = 0;
117
118 // Find the next descriptor of type T and return it and its offset.
119 // Any other descriptors encountered while searching for the expected type are skipped.
120 // The `offset` parameter will be advanced to point to the next byte after the returned
121 // descriptor.
122 fn next_descriptor<T: Descriptor + DataInit>(
123 data: &[u8],
124 offset: &mut usize,
125 ) -> Result<(T, usize)> {
126 let desc_type = T::descriptor_type() as u8;
127 loop {
128 let hdr = DescriptorHeader::from_slice(
129 data.get(*offset..*offset + size_of::<DescriptorHeader>())
130 .ok_or(Error::DescriptorParse)?,
131 )
132 .ok_or(Error::DescriptorParse)?;
133 if hdr.bDescriptorType == desc_type {
134 if usize::from(hdr.bLength) < size_of::<DescriptorHeader>() + size_of::<T>() {
135 return Err(Error::DescriptorParse);
136 }
137
138 let desc_offset = *offset;
139
140 *offset += size_of::<DescriptorHeader>();
141 let desc = T::from_slice(
142 data.get(*offset..*offset + size_of::<T>())
143 .ok_or(Error::DescriptorParse)?,
144 )
145 .ok_or(Error::DescriptorParse)?;
146 *offset += hdr.bLength as usize - size_of::<DescriptorHeader>();
147 return Ok((*desc, desc_offset));
148 } else {
149 // Finding a ConfigDescriptor while looking for InterfaceDescriptor means
150 // that we should advance to the next device configuration.
151 if desc_type == types::InterfaceDescriptor::descriptor_type() as u8
152 && hdr.bDescriptorType == types::ConfigDescriptor::descriptor_type() as u8
153 {
154 return Err(Error::DescriptorParse);
155 }
156
157 // Make sure we make forward progress.
158 if hdr.bLength == 0 {
159 return Err(Error::DescriptorParse);
160 }
161
162 // Skip this entire descriptor, since it's not the right type.
163 *offset += hdr.bLength as usize;
164 }
165 }
166 }
167
168 let (raw_device_descriptor, _) = next_descriptor::<types::DeviceDescriptor>(data, &mut offset)?;
169 let mut device_descriptor = DeviceDescriptorTree {
170 raw: data.into(),
171 inner: raw_device_descriptor,
172 config_descriptors: BTreeMap::new(),
173 config_values: BTreeMap::new(),
174 };
175
176 for cfg_idx in 0..device_descriptor.bNumConfigurations {
177 if let Ok((raw_config_descriptor, config_offset)) =
178 next_descriptor::<types::ConfigDescriptor>(&device_descriptor.raw, &mut offset)
179 {
180 let mut config_descriptor = ConfigDescriptorTree {
181 offset: config_offset,
182 inner: raw_config_descriptor,
183 interface_descriptors: BTreeMap::new(),
184 };
185
186 while let Ok((raw_interface_descriptor, interface_offset)) =
187 next_descriptor::<types::InterfaceDescriptor>(&device_descriptor.raw, &mut offset)
188 {
189 let mut interface_descriptor = InterfaceDescriptorTree {
190 offset: interface_offset,
191 inner: raw_interface_descriptor,
192 endpoint_descriptors: BTreeMap::new(),
193 };
194
195 for ep_idx in 0..interface_descriptor.bNumEndpoints {
196 if let Ok((endpoint_descriptor, _)) =
197 next_descriptor::<EndpointDescriptor>(&device_descriptor.raw, &mut offset)
198 {
199 interface_descriptor
200 .endpoint_descriptors
201 .insert(ep_idx, endpoint_descriptor);
202 } else {
203 warn!("Could not read endpoint descriptor {}", ep_idx);
204 break;
205 }
206 }
207
208 config_descriptor.interface_descriptors.insert(
209 (
210 interface_descriptor.bInterfaceNumber,
211 interface_descriptor.bAlternateSetting,
212 ),
213 interface_descriptor,
214 );
215 }
216
217 for intf_idx in 0..config_descriptor.bNumInterfaces {
218 if config_descriptor
219 .interface_descriptors
220 .get(&(intf_idx, 0))
221 .is_none()
222 {
223 warn!("device interface {} has no interface descriptors", intf_idx);
224 }
225 }
226
227 device_descriptor
228 .config_values
229 .insert(cfg_idx, config_descriptor.bConfigurationValue);
230 device_descriptor
231 .config_descriptors
232 .insert(config_descriptor.bConfigurationValue, config_descriptor);
233 } else {
234 warn!("Could not read config descriptor {}", cfg_idx);
235 break;
236 }
237 }
238
239 Ok(device_descriptor)
240 }
241
242 #[cfg(test)]
243 #[allow(clippy::useless_conversion)]
244 mod tests {
245 use super::*;
246 #[test]
parse_descriptors_mass_storage()247 fn parse_descriptors_mass_storage() {
248 let data: &[u8] = &[
249 0x12, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x81, 0x07, 0x80, 0x55, 0x10, 0x00,
250 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x2C, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, 0x09,
251 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x04,
252 0x00, 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x05, 0x02, 0x02, 0x00, 0x04, 0x00,
253 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00,
254 ];
255
256 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
257
258 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
259
260 assert_eq!(u16::from(d.bcdUSB), 0x03_00);
261 assert_eq!(d.bDeviceClass, 0x00);
262 assert_eq!(d.bDeviceSubClass, 0x00);
263 assert_eq!(d.bDeviceProtocol, 0x00);
264 assert_eq!(d.bMaxPacketSize0, 9);
265 assert_eq!(u16::from(d.idVendor), 0x0781);
266 assert_eq!(u16::from(d.idProduct), 0x5580);
267 assert_eq!(u16::from(d.bcdDevice), 0x00_10);
268 assert_eq!(d.iManufacturer, 1);
269 assert_eq!(d.iProduct, 2);
270 assert_eq!(d.iSerialNumber, 3);
271 assert_eq!(d.bNumConfigurations, 1);
272
273 let c = d
274 .get_config_descriptor(1)
275 .expect("could not get config descriptor 1");
276 assert_eq!(u16::from(c.wTotalLength), 44);
277 assert_eq!(c.bNumInterfaces, 1);
278 assert_eq!(c.bConfigurationValue, 1);
279 assert_eq!(c.iConfiguration, 0);
280 assert_eq!(c.bmAttributes, 0x80);
281 assert_eq!(c.bMaxPower, 50);
282
283 let i = c
284 .get_interface_descriptor(0, 0)
285 .expect("could not get interface descriptor 0 alt setting 0");
286 assert_eq!(i.bInterfaceNumber, 0);
287 assert_eq!(i.bAlternateSetting, 0);
288 assert_eq!(i.bNumEndpoints, 2);
289 assert_eq!(i.bInterfaceClass, 0x08);
290 assert_eq!(i.bInterfaceSubClass, 0x06);
291 assert_eq!(i.bInterfaceProtocol, 0x50);
292 assert_eq!(i.iInterface, 0);
293
294 let e = i
295 .get_endpoint_descriptor(0)
296 .expect("could not get endpoint 0 descriptor");
297 assert_eq!(e.bEndpointAddress, 0x81);
298 assert_eq!(e.bmAttributes, 0x02);
299 assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
300 assert_eq!(e.bInterval, 0);
301
302 let e = i
303 .get_endpoint_descriptor(1)
304 .expect("could not get endpoint 1 descriptor");
305 assert_eq!(e.bEndpointAddress, 0x02);
306 assert_eq!(e.bmAttributes, 0x02);
307 assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
308 assert_eq!(e.bInterval, 0);
309 }
310
311 #[test]
parse_descriptors_servo()312 fn parse_descriptors_servo() {
313 let data: &[u8] = &[
314 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0x1b, 0x50, 0x00, 0x01,
315 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x7c, 0x00, 0x06, 0x01, 0x04, 0xc0, 0xfa, 0x09,
316 0x04, 0x00, 0x00, 0x02, 0xff, 0x50, 0x01, 0x06, 0x07, 0x05, 0x81, 0x02, 0x40, 0x00,
317 0x0a, 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00, 0x09, 0x04, 0x02, 0x00, 0x02, 0xff,
318 0x52, 0x01, 0x05, 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x03, 0x02,
319 0x40, 0x00, 0x00, 0x09, 0x04, 0x03, 0x00, 0x02, 0xff, 0x50, 0x01, 0x07, 0x07, 0x05,
320 0x84, 0x02, 0x10, 0x00, 0x0a, 0x07, 0x05, 0x04, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04,
321 0x04, 0x00, 0x02, 0xff, 0x50, 0x01, 0x08, 0x07, 0x05, 0x85, 0x02, 0x10, 0x00, 0x0a,
322 0x07, 0x05, 0x05, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04, 0x05, 0x00, 0x02, 0xff, 0x53,
323 0xff, 0x09, 0x07, 0x05, 0x86, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x06, 0x02, 0x40,
324 0x00, 0x00,
325 ];
326
327 // Note: configuration 1 has bNumInterfaces == 6, but it actually only contains 5
328 // interface descriptors. This causes us to try to read beyond EOF, which should be
329 // silently ignored by parse_usbfs_descriptors so that we can use the rest of the
330 // descriptors.
331 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
332
333 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
334
335 assert_eq!(u16::from(d.bcdUSB), 0x02_00);
336 assert_eq!(d.bDeviceClass, 0x00);
337 assert_eq!(d.bDeviceSubClass, 0x00);
338 assert_eq!(d.bDeviceProtocol, 0x00);
339 assert_eq!(d.bMaxPacketSize0, 64);
340 assert_eq!(u16::from(d.idVendor), 0x18d1);
341 assert_eq!(u16::from(d.idProduct), 0x501b);
342 assert_eq!(u16::from(d.bcdDevice), 0x01_00);
343 assert_eq!(d.iManufacturer, 1);
344 assert_eq!(d.iProduct, 2);
345 assert_eq!(d.iSerialNumber, 3);
346 assert_eq!(d.bNumConfigurations, 1);
347
348 let c = d
349 .get_config_descriptor(1)
350 .expect("could not get config descriptor 1");
351 assert_eq!(u16::from(c.wTotalLength), 124);
352 assert_eq!(c.bNumInterfaces, 6);
353 assert_eq!(c.bConfigurationValue, 1);
354 assert_eq!(c.iConfiguration, 4);
355 assert_eq!(c.bmAttributes, 0xc0);
356 assert_eq!(c.bMaxPower, 250);
357
358 let i = c
359 .get_interface_descriptor(0, 0)
360 .expect("could not get interface descriptor 0 alt setting 0");
361 assert_eq!(i.bInterfaceNumber, 0);
362 assert_eq!(i.bAlternateSetting, 0);
363 assert_eq!(i.bNumEndpoints, 2);
364 assert_eq!(i.bInterfaceClass, 0xff);
365 assert_eq!(i.bInterfaceSubClass, 0x50);
366 assert_eq!(i.bInterfaceProtocol, 0x01);
367 assert_eq!(i.iInterface, 6);
368
369 let e = i
370 .get_endpoint_descriptor(0)
371 .expect("could not get endpoint 0 descriptor");
372 assert_eq!(e.bEndpointAddress, 0x81);
373 assert_eq!(e.bmAttributes, 0x02);
374 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
375 assert_eq!(e.bInterval, 10);
376
377 let e = i
378 .get_endpoint_descriptor(1)
379 .expect("could not get endpoint 1 descriptor");
380 assert_eq!(e.bEndpointAddress, 0x01);
381 assert_eq!(e.bmAttributes, 0x02);
382 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
383 assert_eq!(e.bInterval, 0);
384
385 let i = c
386 .get_interface_descriptor(2, 0)
387 .expect("could not get interface descriptor 2 alt setting 0");
388 assert_eq!(i.bInterfaceNumber, 2);
389 assert_eq!(i.bAlternateSetting, 0);
390 assert_eq!(i.bNumEndpoints, 2);
391 assert_eq!(i.bInterfaceClass, 0xff);
392 assert_eq!(i.bInterfaceSubClass, 0x52);
393 assert_eq!(i.bInterfaceProtocol, 0x01);
394 assert_eq!(i.iInterface, 5);
395
396 let e = i
397 .get_endpoint_descriptor(0)
398 .expect("could not get endpoint 0 descriptor");
399 assert_eq!(e.bEndpointAddress, 0x83);
400 assert_eq!(e.bmAttributes, 0x02);
401 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
402 assert_eq!(e.bInterval, 10);
403
404 let e = i
405 .get_endpoint_descriptor(1)
406 .expect("could not get endpoint 1 descriptor");
407 assert_eq!(e.bEndpointAddress, 0x03);
408 assert_eq!(e.bmAttributes, 0x02);
409 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
410 assert_eq!(e.bInterval, 0);
411
412 let i = c
413 .get_interface_descriptor(3, 0)
414 .expect("could not get interface descriptor 3 alt setting 0");
415 assert_eq!(i.bInterfaceNumber, 3);
416 assert_eq!(i.bAlternateSetting, 0);
417 assert_eq!(i.bNumEndpoints, 2);
418 assert_eq!(i.bInterfaceClass, 0xff);
419 assert_eq!(i.bInterfaceSubClass, 0x50);
420 assert_eq!(i.bInterfaceProtocol, 0x01);
421 assert_eq!(i.iInterface, 7);
422
423 let e = i
424 .get_endpoint_descriptor(0)
425 .expect("could not get endpoint 0 descriptor");
426 assert_eq!(e.bEndpointAddress, 0x84);
427 assert_eq!(e.bmAttributes, 0x02);
428 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
429 assert_eq!(e.bInterval, 10);
430
431 let e = i
432 .get_endpoint_descriptor(1)
433 .expect("could not get endpoint 1 descriptor");
434 assert_eq!(e.bEndpointAddress, 0x04);
435 assert_eq!(e.bmAttributes, 0x02);
436 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
437 assert_eq!(e.bInterval, 0);
438
439 let i = c
440 .get_interface_descriptor(4, 0)
441 .expect("could not get interface descriptor 4 alt setting 0");
442 assert_eq!(i.bInterfaceNumber, 4);
443 assert_eq!(i.bAlternateSetting, 0);
444 assert_eq!(i.bNumEndpoints, 2);
445 assert_eq!(i.bInterfaceClass, 0xff);
446 assert_eq!(i.bInterfaceSubClass, 0x50);
447 assert_eq!(i.bInterfaceProtocol, 0x01);
448 assert_eq!(i.iInterface, 8);
449
450 let e = i
451 .get_endpoint_descriptor(0)
452 .expect("could not get endpoint 0 descriptor");
453 assert_eq!(e.bEndpointAddress, 0x85);
454 assert_eq!(e.bmAttributes, 0x02);
455 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
456 assert_eq!(e.bInterval, 10);
457
458 let e = i
459 .get_endpoint_descriptor(1)
460 .expect("could not get endpoint 1 descriptor");
461 assert_eq!(e.bEndpointAddress, 0x05);
462 assert_eq!(e.bmAttributes, 0x02);
463 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
464 assert_eq!(e.bInterval, 0);
465
466 let i = c
467 .get_interface_descriptor(5, 0)
468 .expect("could not get interface descriptor 5 alt setting 0");
469 assert_eq!(i.bInterfaceNumber, 5);
470 assert_eq!(i.bAlternateSetting, 0);
471 assert_eq!(i.bNumEndpoints, 2);
472 assert_eq!(i.bInterfaceClass, 0xff);
473 assert_eq!(i.bInterfaceSubClass, 0x53);
474 assert_eq!(i.bInterfaceProtocol, 0xff);
475 assert_eq!(i.iInterface, 9);
476
477 let e = i
478 .get_endpoint_descriptor(0)
479 .expect("could not get endpoint 0 descriptor");
480 assert_eq!(e.bEndpointAddress, 0x86);
481 assert_eq!(e.bmAttributes, 0x02);
482 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
483 assert_eq!(e.bInterval, 10);
484
485 let e = i
486 .get_endpoint_descriptor(1)
487 .expect("could not get endpoint 1 descriptor");
488 assert_eq!(e.bEndpointAddress, 0x06);
489 assert_eq!(e.bmAttributes, 0x02);
490 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
491 assert_eq!(e.bInterval, 0);
492 }
493
494 #[test]
parse_descriptors_adb()495 fn parse_descriptors_adb() {
496 let data: &[u8] = &[
497 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0xe7, 0x4e, 0x10, 0x03,
498 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xfa, 0x09,
499 0x04, 0x00, 0x00, 0x02, 0xff, 0x42, 0x01, 0x05, 0x07, 0x05, 0x01, 0x02, 0x00, 0x02,
500 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x02, 0x00,
501 ];
502
503 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
504
505 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
506
507 assert_eq!(u16::from(d.bcdUSB), 0x02_00);
508 assert_eq!(d.bDeviceClass, 0x00);
509 assert_eq!(d.bDeviceSubClass, 0x00);
510 assert_eq!(d.bDeviceProtocol, 0x00);
511 assert_eq!(d.bMaxPacketSize0, 64);
512 assert_eq!(u16::from(d.idVendor), 0x18d1);
513 assert_eq!(u16::from(d.idProduct), 0x4ee7);
514 assert_eq!(u16::from(d.bcdDevice), 0x03_10);
515 assert_eq!(d.iManufacturer, 1);
516 assert_eq!(d.iProduct, 2);
517 assert_eq!(d.iSerialNumber, 3);
518 assert_eq!(d.bNumConfigurations, 1);
519
520 let c = d
521 .get_config_descriptor(1)
522 .expect("could not get config descriptor 1");
523 assert_eq!(u16::from(c.wTotalLength), 32);
524 assert_eq!(c.bNumInterfaces, 1);
525 assert_eq!(c.bConfigurationValue, 1);
526 assert_eq!(c.iConfiguration, 0);
527 assert_eq!(c.bmAttributes, 0x80);
528 assert_eq!(c.bMaxPower, 250);
529
530 let i = c
531 .get_interface_descriptor(0, 0)
532 .expect("could not get interface descriptor 0 alt setting 0");
533 assert_eq!(i.bInterfaceNumber, 0);
534 assert_eq!(i.bAlternateSetting, 0);
535 assert_eq!(i.bNumEndpoints, 2);
536 assert_eq!(i.bInterfaceClass, 0xff);
537 assert_eq!(i.bInterfaceSubClass, 0x42);
538 assert_eq!(i.bInterfaceProtocol, 0x01);
539 assert_eq!(i.iInterface, 5);
540
541 let e = i
542 .get_endpoint_descriptor(0)
543 .expect("could not get endpoint 0 descriptor");
544 assert_eq!(e.bEndpointAddress, 0x01);
545 assert_eq!(e.bmAttributes, 0x02);
546 assert_eq!(u16::from(e.wMaxPacketSize), 0x200);
547 assert_eq!(e.bInterval, 0);
548
549 let e = i
550 .get_endpoint_descriptor(1)
551 .expect("could not get endpoint 1 descriptor");
552 assert_eq!(e.bEndpointAddress, 0x81);
553 assert_eq!(e.bmAttributes, 0x02);
554 assert_eq!(u16::from(e.wMaxPacketSize), 0x0200);
555 assert_eq!(e.bInterval, 0);
556 }
557
558 #[test]
parse_descriptors_multiple_altsettings()559 fn parse_descriptors_multiple_altsettings() {
560 let data: &[u8] = &[
561 // DeviceDescriptor
562 0x12, 0x01, 0x00, 0x02, 0xef, 0x02, 0x01, 0x40, 0x6d, 0x04, 0x43, 0x08, 0x13, 0x00,
563 0x00, 0x02, 0x01, 0x01, // ConfigDescriptor
564 0x09, 0x02, 0x0d, 0x0a, 0x03, 0x01, 0x00, 0x80, 0xfa,
565 // InterfaceDescriptor 0, 0
566 0x09, 0x04, 0x00, 0x00, 0x01, 0x0e, 0x01, 0x00, 0x00, // EndpointDescriptor
567 0x07, 0x05, 0x86, 0x03, 0x40, 0x00, 0x08, // InterfaceDescriptor 1, 0
568 0x09, 0x04, 0x01, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x00,
569 // InterfaceDescriptor 1, 1
570 0x09, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x02, 0x00, 0x00, // EndpointDescriptor
571 0x07, 0x05, 0x81, 0x05, 0xc0, 0x00, 0x01, // InterfaceDescriptor 2, 0
572 0x09, 0x04, 0x02, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
573 ];
574
575 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
576
577 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
578
579 assert_eq!(u16::from(d.bcdUSB), 0x02_00);
580 assert_eq!(d.bDeviceClass, 0xef);
581 assert_eq!(d.bDeviceSubClass, 0x02);
582 assert_eq!(d.bDeviceProtocol, 0x01);
583 assert_eq!(d.bMaxPacketSize0, 64);
584 assert_eq!(u16::from(d.idVendor), 0x046d);
585 assert_eq!(u16::from(d.idProduct), 0x0843);
586 assert_eq!(u16::from(d.bcdDevice), 0x00_13);
587 assert_eq!(d.iManufacturer, 0);
588 assert_eq!(d.iProduct, 2);
589 assert_eq!(d.iSerialNumber, 1);
590 assert_eq!(d.bNumConfigurations, 1);
591
592 let c = d
593 .get_config_descriptor(1)
594 .expect("could not get config descriptor 1");
595 assert_eq!(u16::from(c.wTotalLength), 2573);
596 assert_eq!(c.bNumInterfaces, 3);
597 assert_eq!(c.bConfigurationValue, 1);
598 assert_eq!(c.iConfiguration, 0);
599 assert_eq!(c.bmAttributes, 0x80);
600 assert_eq!(c.bMaxPower, 250);
601
602 let i = c
603 .get_interface_descriptor(0, 0)
604 .expect("could not get interface descriptor 0 alt setting 0");
605 assert_eq!(i.bInterfaceNumber, 0);
606 assert_eq!(i.bAlternateSetting, 0);
607 assert_eq!(i.bNumEndpoints, 1);
608 assert_eq!(i.bInterfaceClass, 0x0e);
609 assert_eq!(i.bInterfaceSubClass, 0x01);
610 assert_eq!(i.bInterfaceProtocol, 0x00);
611 assert_eq!(i.iInterface, 0x00);
612
613 let e = i
614 .get_endpoint_descriptor(0)
615 .expect("could not get endpoint 0 descriptor");
616 assert_eq!(e.bEndpointAddress, 0x86);
617 assert_eq!(e.bmAttributes, 0x03);
618 assert_eq!(u16::from(e.wMaxPacketSize), 0x40);
619 assert_eq!(e.bInterval, 0x08);
620
621 let i = c
622 .get_interface_descriptor(1, 0)
623 .expect("could not get interface descriptor 1 alt setting 0");
624 assert_eq!(i.bInterfaceNumber, 1);
625 assert_eq!(i.bAlternateSetting, 0);
626 assert_eq!(i.bNumEndpoints, 0);
627 assert_eq!(i.bInterfaceClass, 0x0e);
628 assert_eq!(i.bInterfaceSubClass, 0x02);
629 assert_eq!(i.bInterfaceProtocol, 0x00);
630 assert_eq!(i.iInterface, 0x00);
631
632 let i = c
633 .get_interface_descriptor(1, 1)
634 .expect("could not get interface descriptor 1 alt setting 1");
635 assert_eq!(i.bInterfaceNumber, 1);
636 assert_eq!(i.bAlternateSetting, 1);
637 assert_eq!(i.bNumEndpoints, 1);
638 assert_eq!(i.bInterfaceClass, 0x0e);
639 assert_eq!(i.bInterfaceSubClass, 0x02);
640 assert_eq!(i.bInterfaceProtocol, 0x00);
641 assert_eq!(i.iInterface, 0x00);
642
643 let e = i
644 .get_endpoint_descriptor(0)
645 .expect("could not get endpoint 0 descriptor");
646 assert_eq!(e.bEndpointAddress, 0x81);
647 assert_eq!(e.bmAttributes, 0x05);
648 assert_eq!(u16::from(e.wMaxPacketSize), 0xc0);
649 assert_eq!(e.bInterval, 0x01);
650
651 let i = c
652 .get_interface_descriptor(2, 0)
653 .expect("could not get interface descriptor 2 alt setting 0");
654 assert_eq!(i.bInterfaceNumber, 2);
655 assert_eq!(i.bAlternateSetting, 0);
656 assert_eq!(i.bNumEndpoints, 0);
657 assert_eq!(i.bInterfaceClass, 0x01);
658 assert_eq!(i.bInterfaceSubClass, 0x01);
659 assert_eq!(i.bInterfaceProtocol, 0x00);
660 assert_eq!(i.iInterface, 0x00);
661 }
662
663 #[test]
parse_descriptors_length_0()664 fn parse_descriptors_length_0() {
665 // Device descriptor followed by a bogus descriptor with bLength == 0.
666 // Note that this was generated by a fuzzer, so field values may not make sense.
667 let data: &[u8] = &[
668 0x10, 0x00, 0x18, 0x25, 0x80, 0x80, 0xAC, 0x03, 0x22, 0x05, 0x00, 0x00, 0x00, 0x00,
669 0xC3, 0x2A, 0x00, 0x32, 0x00,
670 ];
671
672 let d = parse_usbfs_descriptors(data);
673 if d.is_ok() {
674 panic!("parse_usbfs_descriptors should have failed");
675 }
676 }
677 }
678