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::io::{self, Read};
11 use std::mem::size_of;
12 use std::ops::Deref;
13
14 #[derive(Clone)]
15 pub struct DeviceDescriptorTree {
16 inner: types::DeviceDescriptor,
17 // Map of bConfigurationValue to ConfigDescriptor
18 config_descriptors: BTreeMap<u8, ConfigDescriptorTree>,
19 }
20
21 #[derive(Clone)]
22 pub struct ConfigDescriptorTree {
23 inner: types::ConfigDescriptor,
24 // Map of (bInterfaceNumber, bAlternateSetting) to InterfaceDescriptor
25 interface_descriptors: BTreeMap<(u8, u8), InterfaceDescriptorTree>,
26 }
27
28 #[derive(Clone)]
29 pub struct InterfaceDescriptorTree {
30 inner: types::InterfaceDescriptor,
31 // Map of bEndpointAddress to EndpointDescriptor
32 endpoint_descriptors: BTreeMap<u8, EndpointDescriptor>,
33 }
34
35 impl DeviceDescriptorTree {
get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree>36 pub fn get_config_descriptor(&self, config_value: u8) -> Option<&ConfigDescriptorTree> {
37 self.config_descriptors.get(&config_value)
38 }
39 }
40
41 impl Deref for DeviceDescriptorTree {
42 type Target = types::DeviceDescriptor;
43
deref(&self) -> &types::DeviceDescriptor44 fn deref(&self) -> &types::DeviceDescriptor {
45 &self.inner
46 }
47 }
48
49 impl ConfigDescriptorTree {
50 /// Get interface by number and alt setting.
get_interface_descriptor( &self, interface_num: u8, alt_setting: u8, ) -> Option<&InterfaceDescriptorTree>51 pub fn get_interface_descriptor(
52 &self,
53 interface_num: u8,
54 alt_setting: u8,
55 ) -> Option<&InterfaceDescriptorTree> {
56 self.interface_descriptors
57 .get(&(interface_num, alt_setting))
58 }
59 }
60
61 impl Deref for ConfigDescriptorTree {
62 type Target = types::ConfigDescriptor;
63
deref(&self) -> &types::ConfigDescriptor64 fn deref(&self) -> &types::ConfigDescriptor {
65 &self.inner
66 }
67 }
68
69 impl InterfaceDescriptorTree {
get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor>70 pub fn get_endpoint_descriptor(&self, ep_idx: u8) -> Option<&EndpointDescriptor> {
71 self.endpoint_descriptors.get(&ep_idx)
72 }
73 }
74
75 impl Deref for InterfaceDescriptorTree {
76 type Target = types::InterfaceDescriptor;
77
deref(&self) -> &types::InterfaceDescriptor78 fn deref(&self) -> &types::InterfaceDescriptor {
79 &self.inner
80 }
81 }
82
83 /// Given a `reader` for a full set of descriptors as provided by the Linux kernel
84 /// usbdevfs `descriptors` file, parse the descriptors into a tree data structure.
parse_usbfs_descriptors<R: Read>(mut reader: R) -> Result<DeviceDescriptorTree>85 pub fn parse_usbfs_descriptors<R: Read>(mut reader: R) -> Result<DeviceDescriptorTree> {
86 // Given a structure of length `struct_length`, of which `bytes_consumed` have
87 // already been read, skip the remainder of the struct. If `bytes_consumed` is
88 // more than `struct_length`, no additional bytes are skipped.
89 fn skip<R: Read>(reader: R, bytes_consumed: usize, struct_length: u8) -> io::Result<u64> {
90 let bytes_to_skip = u64::from(struct_length).saturating_sub(bytes_consumed as u64);
91 io::copy(&mut reader.take(bytes_to_skip), &mut io::sink())
92 }
93
94 // Find the next descriptor of type T and return it.
95 // Any other descriptors encountered while searching for the expected type are skipped.
96 fn next_descriptor<R: Read, T: Descriptor + DataInit>(mut reader: R) -> Result<T> {
97 let desc_type = T::descriptor_type() as u8;
98 loop {
99 let hdr = DescriptorHeader::from_reader(&mut reader).map_err(Error::DescriptorRead)?;
100 if hdr.bDescriptorType == desc_type {
101 if usize::from(hdr.bLength) < size_of::<DescriptorHeader>() + size_of::<T>() {
102 return Err(Error::DescriptorParse);
103 }
104
105 let desc = T::from_reader(&mut reader).map_err(Error::DescriptorRead)?;
106
107 // Skip any extra data beyond the standard descriptor length.
108 skip(
109 &mut reader,
110 size_of::<DescriptorHeader>() + size_of::<T>(),
111 hdr.bLength,
112 )
113 .map_err(Error::DescriptorRead)?;
114 return Ok(desc);
115 }
116
117 // Skip this entire descriptor, since it's not the right type.
118 skip(&mut reader, size_of::<DescriptorHeader>(), hdr.bLength)
119 .map_err(Error::DescriptorRead)?;
120 }
121 }
122
123 let raw_device_descriptor: types::DeviceDescriptor = next_descriptor(&mut reader)?;
124 let mut device_descriptor = DeviceDescriptorTree {
125 inner: raw_device_descriptor,
126 config_descriptors: BTreeMap::new(),
127 };
128
129 for cfg_idx in 0..device_descriptor.bNumConfigurations {
130 if let Ok(raw_config_descriptor) =
131 next_descriptor::<_, types::ConfigDescriptor>(&mut reader)
132 {
133 let mut config_descriptor = ConfigDescriptorTree {
134 inner: raw_config_descriptor,
135 interface_descriptors: BTreeMap::new(),
136 };
137
138 for intf_idx in 0..config_descriptor.bNumInterfaces {
139 if let Ok(raw_interface_descriptor) =
140 next_descriptor::<_, types::InterfaceDescriptor>(&mut reader)
141 {
142 let mut interface_descriptor = InterfaceDescriptorTree {
143 inner: raw_interface_descriptor,
144 endpoint_descriptors: BTreeMap::new(),
145 };
146
147 for ep_idx in 0..interface_descriptor.bNumEndpoints {
148 if let Ok(endpoint_descriptor) =
149 next_descriptor::<_, EndpointDescriptor>(&mut reader)
150 {
151 interface_descriptor
152 .endpoint_descriptors
153 .insert(ep_idx, endpoint_descriptor);
154 } else {
155 warn!("Could not read endpoint descriptor {}", ep_idx);
156 break;
157 }
158 }
159
160 config_descriptor.interface_descriptors.insert(
161 (
162 interface_descriptor.bInterfaceNumber,
163 interface_descriptor.bAlternateSetting,
164 ),
165 interface_descriptor,
166 );
167 } else {
168 warn!("Could not read interface descriptor {}", intf_idx);
169 break;
170 }
171 }
172
173 device_descriptor
174 .config_descriptors
175 .insert(config_descriptor.bConfigurationValue, config_descriptor);
176 } else {
177 warn!("Could not read config descriptor {}", cfg_idx);
178 break;
179 }
180 }
181
182 Ok(device_descriptor)
183 }
184
185 #[cfg(test)]
186 mod tests {
187 use super::*;
188 #[test]
parse_descriptors_mass_storage()189 fn parse_descriptors_mass_storage() {
190 let data: &[u8] = &[
191 0x12, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x81, 0x07, 0x80, 0x55, 0x10, 0x00,
192 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x2C, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32, 0x09,
193 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50, 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x04,
194 0x00, 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00, 0x07, 0x05, 0x02, 0x02, 0x00, 0x04, 0x00,
195 0x06, 0x30, 0x0F, 0x00, 0x00, 0x00,
196 ];
197
198 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
199
200 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
201
202 assert_eq!(u16::from(d.bcdUSB), 0x03_00);
203 assert_eq!(d.bDeviceClass, 0x00);
204 assert_eq!(d.bDeviceSubClass, 0x00);
205 assert_eq!(d.bDeviceProtocol, 0x00);
206 assert_eq!(d.bMaxPacketSize0, 9);
207 assert_eq!(u16::from(d.idVendor), 0x0781);
208 assert_eq!(u16::from(d.idProduct), 0x5580);
209 assert_eq!(u16::from(d.bcdDevice), 0x00_10);
210 assert_eq!(d.iManufacturer, 1);
211 assert_eq!(d.iProduct, 2);
212 assert_eq!(d.iSerialNumber, 3);
213 assert_eq!(d.bNumConfigurations, 1);
214
215 let c = d
216 .get_config_descriptor(1)
217 .expect("could not get config descriptor 1");
218 assert_eq!(u16::from(c.wTotalLength), 44);
219 assert_eq!(c.bNumInterfaces, 1);
220 assert_eq!(c.bConfigurationValue, 1);
221 assert_eq!(c.iConfiguration, 0);
222 assert_eq!(c.bmAttributes, 0x80);
223 assert_eq!(c.bMaxPower, 50);
224
225 let i = c
226 .get_interface_descriptor(0, 0)
227 .expect("could not get interface descriptor 0 alt setting 0");
228 assert_eq!(i.bInterfaceNumber, 0);
229 assert_eq!(i.bAlternateSetting, 0);
230 assert_eq!(i.bNumEndpoints, 2);
231 assert_eq!(i.bInterfaceClass, 0x08);
232 assert_eq!(i.bInterfaceSubClass, 0x06);
233 assert_eq!(i.bInterfaceProtocol, 0x50);
234 assert_eq!(i.iInterface, 0);
235
236 let e = i
237 .get_endpoint_descriptor(0)
238 .expect("could not get endpoint 0 descriptor");
239 assert_eq!(e.bEndpointAddress, 0x81);
240 assert_eq!(e.bmAttributes, 0x02);
241 assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
242 assert_eq!(e.bInterval, 0);
243
244 let e = i
245 .get_endpoint_descriptor(1)
246 .expect("could not get endpoint 1 descriptor");
247 assert_eq!(e.bEndpointAddress, 0x02);
248 assert_eq!(e.bmAttributes, 0x02);
249 assert_eq!(u16::from(e.wMaxPacketSize), 0x0400);
250 assert_eq!(e.bInterval, 0);
251 }
252
253 #[test]
parse_descriptors_servo()254 fn parse_descriptors_servo() {
255 let data: &[u8] = &[
256 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0x1b, 0x50, 0x00, 0x01,
257 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x7c, 0x00, 0x06, 0x01, 0x04, 0xc0, 0xfa, 0x09,
258 0x04, 0x00, 0x00, 0x02, 0xff, 0x50, 0x01, 0x06, 0x07, 0x05, 0x81, 0x02, 0x40, 0x00,
259 0x0a, 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00, 0x09, 0x04, 0x02, 0x00, 0x02, 0xff,
260 0x52, 0x01, 0x05, 0x07, 0x05, 0x83, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x03, 0x02,
261 0x40, 0x00, 0x00, 0x09, 0x04, 0x03, 0x00, 0x02, 0xff, 0x50, 0x01, 0x07, 0x07, 0x05,
262 0x84, 0x02, 0x10, 0x00, 0x0a, 0x07, 0x05, 0x04, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04,
263 0x04, 0x00, 0x02, 0xff, 0x50, 0x01, 0x08, 0x07, 0x05, 0x85, 0x02, 0x10, 0x00, 0x0a,
264 0x07, 0x05, 0x05, 0x02, 0x10, 0x00, 0x00, 0x09, 0x04, 0x05, 0x00, 0x02, 0xff, 0x53,
265 0xff, 0x09, 0x07, 0x05, 0x86, 0x02, 0x40, 0x00, 0x0a, 0x07, 0x05, 0x06, 0x02, 0x40,
266 0x00, 0x00,
267 ];
268
269 // Note: configuration 1 has bNumInterfaces == 6, but it actually only contains 5
270 // interface descriptors. This causes us to try to read beyond EOF, which should be
271 // silently ignored by parse_usbfs_descriptors so that we can use the rest of the
272 // descriptors.
273 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
274
275 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
276
277 assert_eq!(u16::from(d.bcdUSB), 0x02_00);
278 assert_eq!(d.bDeviceClass, 0x00);
279 assert_eq!(d.bDeviceSubClass, 0x00);
280 assert_eq!(d.bDeviceProtocol, 0x00);
281 assert_eq!(d.bMaxPacketSize0, 64);
282 assert_eq!(u16::from(d.idVendor), 0x18d1);
283 assert_eq!(u16::from(d.idProduct), 0x501b);
284 assert_eq!(u16::from(d.bcdDevice), 0x01_00);
285 assert_eq!(d.iManufacturer, 1);
286 assert_eq!(d.iProduct, 2);
287 assert_eq!(d.iSerialNumber, 3);
288 assert_eq!(d.bNumConfigurations, 1);
289
290 let c = d
291 .get_config_descriptor(1)
292 .expect("could not get config descriptor 1");
293 assert_eq!(u16::from(c.wTotalLength), 124);
294 assert_eq!(c.bNumInterfaces, 6);
295 assert_eq!(c.bConfigurationValue, 1);
296 assert_eq!(c.iConfiguration, 4);
297 assert_eq!(c.bmAttributes, 0xc0);
298 assert_eq!(c.bMaxPower, 250);
299
300 let i = c
301 .get_interface_descriptor(0, 0)
302 .expect("could not get interface descriptor 0 alt setting 0");
303 assert_eq!(i.bInterfaceNumber, 0);
304 assert_eq!(i.bAlternateSetting, 0);
305 assert_eq!(i.bNumEndpoints, 2);
306 assert_eq!(i.bInterfaceClass, 0xff);
307 assert_eq!(i.bInterfaceSubClass, 0x50);
308 assert_eq!(i.bInterfaceProtocol, 0x01);
309 assert_eq!(i.iInterface, 6);
310
311 let e = i
312 .get_endpoint_descriptor(0)
313 .expect("could not get endpoint 0 descriptor");
314 assert_eq!(e.bEndpointAddress, 0x81);
315 assert_eq!(e.bmAttributes, 0x02);
316 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
317 assert_eq!(e.bInterval, 10);
318
319 let e = i
320 .get_endpoint_descriptor(1)
321 .expect("could not get endpoint 1 descriptor");
322 assert_eq!(e.bEndpointAddress, 0x01);
323 assert_eq!(e.bmAttributes, 0x02);
324 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
325 assert_eq!(e.bInterval, 0);
326
327 let i = c
328 .get_interface_descriptor(2, 0)
329 .expect("could not get interface descriptor 2 alt setting 0");
330 assert_eq!(i.bInterfaceNumber, 2);
331 assert_eq!(i.bAlternateSetting, 0);
332 assert_eq!(i.bNumEndpoints, 2);
333 assert_eq!(i.bInterfaceClass, 0xff);
334 assert_eq!(i.bInterfaceSubClass, 0x52);
335 assert_eq!(i.bInterfaceProtocol, 0x01);
336 assert_eq!(i.iInterface, 5);
337
338 let e = i
339 .get_endpoint_descriptor(0)
340 .expect("could not get endpoint 0 descriptor");
341 assert_eq!(e.bEndpointAddress, 0x83);
342 assert_eq!(e.bmAttributes, 0x02);
343 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
344 assert_eq!(e.bInterval, 10);
345
346 let e = i
347 .get_endpoint_descriptor(1)
348 .expect("could not get endpoint 1 descriptor");
349 assert_eq!(e.bEndpointAddress, 0x03);
350 assert_eq!(e.bmAttributes, 0x02);
351 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
352 assert_eq!(e.bInterval, 0);
353
354 let i = c
355 .get_interface_descriptor(3, 0)
356 .expect("could not get interface descriptor 3 alt setting 0");
357 assert_eq!(i.bInterfaceNumber, 3);
358 assert_eq!(i.bAlternateSetting, 0);
359 assert_eq!(i.bNumEndpoints, 2);
360 assert_eq!(i.bInterfaceClass, 0xff);
361 assert_eq!(i.bInterfaceSubClass, 0x50);
362 assert_eq!(i.bInterfaceProtocol, 0x01);
363 assert_eq!(i.iInterface, 7);
364
365 let e = i
366 .get_endpoint_descriptor(0)
367 .expect("could not get endpoint 0 descriptor");
368 assert_eq!(e.bEndpointAddress, 0x84);
369 assert_eq!(e.bmAttributes, 0x02);
370 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
371 assert_eq!(e.bInterval, 10);
372
373 let e = i
374 .get_endpoint_descriptor(1)
375 .expect("could not get endpoint 1 descriptor");
376 assert_eq!(e.bEndpointAddress, 0x04);
377 assert_eq!(e.bmAttributes, 0x02);
378 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
379 assert_eq!(e.bInterval, 0);
380
381 let i = c
382 .get_interface_descriptor(4, 0)
383 .expect("could not get interface descriptor 4 alt setting 0");
384 assert_eq!(i.bInterfaceNumber, 4);
385 assert_eq!(i.bAlternateSetting, 0);
386 assert_eq!(i.bNumEndpoints, 2);
387 assert_eq!(i.bInterfaceClass, 0xff);
388 assert_eq!(i.bInterfaceSubClass, 0x50);
389 assert_eq!(i.bInterfaceProtocol, 0x01);
390 assert_eq!(i.iInterface, 8);
391
392 let e = i
393 .get_endpoint_descriptor(0)
394 .expect("could not get endpoint 0 descriptor");
395 assert_eq!(e.bEndpointAddress, 0x85);
396 assert_eq!(e.bmAttributes, 0x02);
397 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
398 assert_eq!(e.bInterval, 10);
399
400 let e = i
401 .get_endpoint_descriptor(1)
402 .expect("could not get endpoint 1 descriptor");
403 assert_eq!(e.bEndpointAddress, 0x05);
404 assert_eq!(e.bmAttributes, 0x02);
405 assert_eq!(u16::from(e.wMaxPacketSize), 0x0010);
406 assert_eq!(e.bInterval, 0);
407
408 let i = c
409 .get_interface_descriptor(5, 0)
410 .expect("could not get interface descriptor 5 alt setting 0");
411 assert_eq!(i.bInterfaceNumber, 5);
412 assert_eq!(i.bAlternateSetting, 0);
413 assert_eq!(i.bNumEndpoints, 2);
414 assert_eq!(i.bInterfaceClass, 0xff);
415 assert_eq!(i.bInterfaceSubClass, 0x53);
416 assert_eq!(i.bInterfaceProtocol, 0xff);
417 assert_eq!(i.iInterface, 9);
418
419 let e = i
420 .get_endpoint_descriptor(0)
421 .expect("could not get endpoint 0 descriptor");
422 assert_eq!(e.bEndpointAddress, 0x86);
423 assert_eq!(e.bmAttributes, 0x02);
424 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
425 assert_eq!(e.bInterval, 10);
426
427 let e = i
428 .get_endpoint_descriptor(1)
429 .expect("could not get endpoint 1 descriptor");
430 assert_eq!(e.bEndpointAddress, 0x06);
431 assert_eq!(e.bmAttributes, 0x02);
432 assert_eq!(u16::from(e.wMaxPacketSize), 0x0040);
433 assert_eq!(e.bInterval, 0);
434 }
435
436 #[test]
parse_descriptors_adb()437 fn parse_descriptors_adb() {
438 let data: &[u8] = &[
439 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0xd1, 0x18, 0xe7, 0x4e, 0x10, 0x03,
440 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0xfa, 0x09,
441 0x04, 0x00, 0x00, 0x02, 0xff, 0x42, 0x01, 0x05, 0x07, 0x05, 0x01, 0x02, 0x00, 0x02,
442 0x00, 0x07, 0x05, 0x81, 0x02, 0x00, 0x02, 0x00,
443 ];
444
445 let d = parse_usbfs_descriptors(data).expect("parse_usbfs_descriptors failed");
446
447 // The seemingly-redundant u16::from() calls avoid borrows of packed fields.
448
449 assert_eq!(u16::from(d.bcdUSB), 0x02_00);
450 assert_eq!(d.bDeviceClass, 0x00);
451 assert_eq!(d.bDeviceSubClass, 0x00);
452 assert_eq!(d.bDeviceProtocol, 0x00);
453 assert_eq!(d.bMaxPacketSize0, 64);
454 assert_eq!(u16::from(d.idVendor), 0x18d1);
455 assert_eq!(u16::from(d.idProduct), 0x4ee7);
456 assert_eq!(u16::from(d.bcdDevice), 0x03_10);
457 assert_eq!(d.iManufacturer, 1);
458 assert_eq!(d.iProduct, 2);
459 assert_eq!(d.iSerialNumber, 3);
460 assert_eq!(d.bNumConfigurations, 1);
461
462 let c = d
463 .get_config_descriptor(1)
464 .expect("could not get config descriptor 1");
465 assert_eq!(u16::from(c.wTotalLength), 32);
466 assert_eq!(c.bNumInterfaces, 1);
467 assert_eq!(c.bConfigurationValue, 1);
468 assert_eq!(c.iConfiguration, 0);
469 assert_eq!(c.bmAttributes, 0x80);
470 assert_eq!(c.bMaxPower, 250);
471
472 let i = c
473 .get_interface_descriptor(0, 0)
474 .expect("could not get interface descriptor 0 alt setting 0");
475 assert_eq!(i.bInterfaceNumber, 0);
476 assert_eq!(i.bAlternateSetting, 0);
477 assert_eq!(i.bNumEndpoints, 2);
478 assert_eq!(i.bInterfaceClass, 0xff);
479 assert_eq!(i.bInterfaceSubClass, 0x42);
480 assert_eq!(i.bInterfaceProtocol, 0x01);
481 assert_eq!(i.iInterface, 5);
482
483 let e = i
484 .get_endpoint_descriptor(0)
485 .expect("could not get endpoint 0 descriptor");
486 assert_eq!(e.bEndpointAddress, 0x01);
487 assert_eq!(e.bmAttributes, 0x02);
488 assert_eq!(u16::from(e.wMaxPacketSize), 0x200);
489 assert_eq!(e.bInterval, 0);
490
491 let e = i
492 .get_endpoint_descriptor(1)
493 .expect("could not get endpoint 1 descriptor");
494 assert_eq!(e.bEndpointAddress, 0x81);
495 assert_eq!(e.bmAttributes, 0x02);
496 assert_eq!(u16::from(e.wMaxPacketSize), 0x0200);
497 assert_eq!(e.bInterval, 0);
498 }
499 }
500