-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathUSBInterface.py
132 lines (96 loc) · 3.92 KB
/
USBInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# USBInterface.py
#
# Contains class definition for USBInterface.
from USB import *
class USBInterface:
name = "generic USB interface"
def __init__(self, maxusb_app, interface_number, interface_alternate, interface_class,
interface_subclass, interface_protocol, interface_string_index,
verbose=0, endpoints=[], descriptors={}, cs_interfaces=[]):
self.maxusb_app = maxusb_app
self.number = interface_number
self.alternate = interface_alternate
self.iclass = interface_class
self.subclass = interface_subclass
self.protocol = interface_protocol
self.string_index = interface_string_index
self.endpoints = endpoints
self.descriptors = descriptors
self.cs_interfaces = cs_interfaces
self.verbose = verbose
self.descriptors[USB.desc_type_interface] = self.get_descriptor
self.request_handlers = {
6 : self.handle_get_descriptor_request,
11 : self.handle_set_interface_request
}
self.configuration = None
for e in self.endpoints:
e.set_interface(self)
self.device_class = None
self.device_vendor = None
def set_configuration(self, config):
self.configuration = config
# USB 2.0 specification, section 9.4.3 (p 281 of pdf)
# HACK: blatant copypasta from USBDevice pains me deeply
def handle_get_descriptor_request(self, req):
dtype = (req.value >> 8) & 0xff
dindex = req.value & 0xff
lang = req.index
n = req.length
response = None
trace = "Int:GetDes:%d:%d" % (dtype,dindex)
self.maxusb_app.fingerprint.append(trace)
if self.verbose > 2:
print(self.name, ("received GET_DESCRIPTOR req %d, index %d, " \
+ "language 0x%04x, length %d") \
% (dtype, dindex, lang, n))
# TODO: handle KeyError
response = self.descriptors[dtype]
if callable(response):
response = response(dindex)
if response:
n = min(n, len(response))
self.configuration.device.maxusb_app.send_on_endpoint(0, response[:n])
if self.verbose > 5:
print(self.name, "sent", n, "bytes in response")
def handle_set_interface_request(self, req):
trace = "Int:SetInt"
self.maxusb_app.fingerprint.append(trace)
if self.verbose > 0:
print(self.name, "received SET_INTERFACE request")
self.configuration.device.maxusb_app.stall_ep0()
#self.configuration.device.maxusb_app.send_on_endpoint(0, b'')
# Table 9-12 of USB 2.0 spec (pdf page 296)
def get_descriptor(self):
if self.maxusb_app.testcase[1] == "int_bLength":
bLength = self.maxusb_app.testcase[2]
else:
bLength = 9
if self.maxusb_app.testcase[1] == "int_bDescriptorType":
bDescriptorType = self.maxusb_app.testcase[2]
else:
bDescriptorType = 4
if self.maxusb_app.testcase[1] == "int_bNumEndpoints":
bNumEndpoints = self.maxusb_app.testcase[2]
else:
bNumEndpoints = len(self.endpoints)
d = bytearray([
bLength, # length of descriptor in bytes
bDescriptorType, # descriptor type 4 == interface
self.number,
self.alternate,
bNumEndpoints,
self.iclass,
self.subclass,
self.protocol,
self.string_index
])
if self.iclass:
iclass_desc_num = USB.interface_class_to_descriptor_type(self.iclass)
if iclass_desc_num:
d += self.descriptors[iclass_desc_num]
for e in self.cs_interfaces:
d += e.get_descriptor()
for e in self.endpoints:
d += e.get_descriptor()
return d