-
Notifications
You must be signed in to change notification settings - Fork 60
/
MAXUSBApp.py
293 lines (221 loc) · 9.54 KB
/
MAXUSBApp.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# MAXUSBApp.py
#
# Contains class definition for MAXUSBApp.
from util import *
from Facedancer import *
from USB import *
from USBDevice import USBDeviceRequest
import sys
class MAXUSBApp(FacedancerApp):
app_name = "MAXUSB"
app_num = 0x40
reg_ep0_fifo = 0x00
reg_ep1_out_fifo = 0x01
reg_ep2_in_fifo = 0x02
reg_ep3_in_fifo = 0x03
reg_setup_data_fifo = 0x04
reg_ep0_byte_count = 0x05
reg_ep1_out_byte_count = 0x06
reg_ep2_in_byte_count = 0x07
reg_ep3_in_byte_count = 0x08
reg_ep_stalls = 0x09
reg_clr_togs = 0x0a
reg_endpoint_irq = 0x0b
reg_endpoint_interrupt_enable = 0x0c
reg_usb_irq = 0x0d
reg_usb_interrupt_enable = 0x0e
reg_usb_control = 0x0f
reg_cpu_control = 0x10
reg_pin_control = 0x11
reg_revision = 0x12
reg_function_address = 0x13
reg_io_pins = 0x14
# bitmask values for reg_endpoint_irq = 0x0b
is_setup_data_avail = 0x20 # SUDAVIRQ
is_in3_buffer_avail = 0x10 # IN3BAVIRQ
is_in2_buffer_avail = 0x08 # IN2BAVIRQ
is_out1_data_avail = 0x04 # OUT1DAVIRQ
is_out0_data_avail = 0x02 # OUT0DAVIRQ
is_in0_buffer_avail = 0x01 # IN0BAVIRQ
# bitmask values for reg_usb_control = 0x0f
usb_control_vbgate = 0x40
usb_control_connect = 0x08
# bitmask values for reg_pin_control = 0x11
interrupt_level = 0x08
full_duplex = 0x10
def __init__(self, device, logfp, mode, testcase, verbose=0):
FacedancerApp.__init__(self, device, verbose)
self.connected_device = None
self.mode = mode
self.netserver_to_endpoint_sd = 0
self.netserver_from_endpoint_sd = 0
self.server_running = False
self.reply_buffer = ""
self.testcase = testcase
self.fplog = 0
if logfp != 0:
self.fplog = logfp
self.fingerprint = []
self.stop = False
self.retries = False
self.enable()
if verbose > 0:
rev = self.read_register(self.reg_revision)
print(self.app_name, "revision", rev)
# set duplex and negative INT level (from GoodFEDMAXUSB.py)
self.write_register(self.reg_pin_control,
self.full_duplex | self.interrupt_level)
def init_commands(self):
self.read_register_cmd = FacedancerCommand(self.app_num, 0x00, b'')
self.write_register_cmd = FacedancerCommand(self.app_num, 0x00, b'')
self.enable_app_cmd = FacedancerCommand(self.app_num, 0x10, b'')
self.ack_cmd = FacedancerCommand(self.app_num, 0x00, b'\x01')
def read_register(self, reg_num, ack=False):
if self.verbose > 1:
print(self.app_name, "reading register 0x%02x" % reg_num)
self.read_register_cmd.data = bytearray([ reg_num << 3, 0 ])
if ack:
self.write_register_cmd.data[0] |= 1
self.device.writecmd(self.read_register_cmd)
resp = self.device.readcmd()
if self.verbose > 2:
print(self.app_name, "read register 0x%02x has value 0x%02x" %
(reg_num, resp.data[1]))
return resp.data[1]
def write_register(self, reg_num, value, ack=False):
if self.verbose > 2:
print(self.app_name, "writing register 0x%02x with value 0x%02x" %
(reg_num, value))
self.write_register_cmd.data = bytearray([ (reg_num << 3) | 2, value ])
if ack:
self.write_register_cmd.data[0] |= 1
self.device.writecmd(self.write_register_cmd)
self.device.readcmd()
def get_version(self):
return self.read_register(self.reg_revision)
def ack_status_stage(self):
if self.verbose > 5:
print(self.app_name, "sending ack!")
self.device.writecmd(self.ack_cmd)
self.device.readcmd()
def connect(self, usb_device):
self.write_register(self.reg_usb_control, self.usb_control_vbgate |
self.usb_control_connect)
self.connected_device = usb_device
if self.verbose > 0:
print(self.app_name, "connected device", self.connected_device.name)
def disconnect(self):
self.write_register(self.reg_usb_control, self.usb_control_vbgate)
if self.verbose > 0:
print(self.app_name, "disconnected device", self.connected_device.name)
self.connected_device = None
def clear_irq_bit(self, reg, bit):
self.write_register(reg, bit)
def read_bytes(self, reg, n):
if self.verbose > 2:
print(self.app_name, "reading", n, "bytes from register", reg)
data = bytes([ (reg << 3) ] + ([0] * n))
cmd = FacedancerCommand(self.app_num, 0x00, data)
self.device.writecmd(cmd)
resp = self.device.readcmd()
if self.verbose > 3:
print(self.app_name, "read", len(resp.data) - 1, "bytes from register", reg)
return resp.data[1:]
def write_bytes(self, reg, data):
data = bytes([ (reg << 3) | 3 ]) + data
cmd = FacedancerCommand(self.app_num, 0x00, data)
self.device.writecmd(cmd)
self.device.readcmd() # null response
if self.verbose > 3:
print(self.app_name, "wrote", len(data) - 1, "bytes to register", reg)
# HACK: but given the limitations of the MAX chips, it seems necessary
def send_on_endpoint(self, ep_num, data):
if ep_num == 0:
fifo_reg = self.reg_ep0_fifo
bc_reg = self.reg_ep0_byte_count
elif ep_num == 2:
fifo_reg = self.reg_ep2_in_fifo
bc_reg = self.reg_ep2_in_byte_count
elif ep_num == 3:
fifo_reg = self.reg_ep3_in_fifo
bc_reg = self.reg_ep3_in_byte_count
else:
raise ValueError('endpoint ' + str(ep_num) + ' not supported')
# FIFO buffer is only 64 bytes, must loop
while len(data) > 64:
self.write_bytes(fifo_reg, data[:64])
self.write_register(bc_reg, 64, ack=True)
data = data[64:]
self.write_bytes(fifo_reg, data)
self.write_register(bc_reg, len(data), ack=True)
if self.verbose > 1:
print(self.app_name, "wrote", bytes_as_hex(data), "to endpoint",
ep_num)
# HACK: but given the limitations of the MAX chips, it seems necessary
def read_from_endpoint(self, ep_num):
if ep_num != 1:
return b''
byte_count = self.read_register(self.reg_ep1_out_byte_count)
if byte_count == 0:
return b''
data = self.read_bytes(self.reg_ep1_out_fifo, byte_count)
if self.verbose > 1:
print(self.app_name, "read", bytes_as_hex(data), "from endpoint",
ep_num)
return data
def stall_ep0(self):
if self.verbose > 0:
print(self.app_name, "stalling endpoint 0")
self.write_register(self.reg_ep_stalls, 0x23)
def service_irqs(self):
count = 0
tmp_irq = 0
while self.stop == False:
irq = self.read_register(self.reg_endpoint_irq)
if irq == tmp_irq:
count +=1
else:
count = 0
if count == 10000 and self.mode == 2: #This needs to be configurable
self.stop = True
if self.fplog:
self.fplog.write("\n")
return
if count == 2000 and (self.mode == 3 or self.mode == 1 or self.mode == 4): #This needs to be configurable
self.stop = True
if len(self.fingerprint) == 0:
print ("\n*** No response from host - check if the host is still functioning correctly ***\n")
self.disconnect()
sys.exit()
self.disconnect()
if self.fplog:
self.fplog.write("\n")
return
if self.verbose > 3:
print(self.app_name, "read endpoint irq: 0x%02x" % irq)
if self.verbose > 2:
if irq & ~ (self.is_in0_buffer_avail \
| self.is_in2_buffer_avail | self.is_in3_buffer_avail):
print(self.app_name, "notable irq: 0x%02x" % irq)
if irq & self.is_setup_data_avail:
self.clear_irq_bit(self.reg_endpoint_irq, self.is_setup_data_avail)
b = self.read_bytes(self.reg_setup_data_fifo, 8)
req = USBDeviceRequest(b)
self.connected_device.handle_request(req)
if irq & self.is_out1_data_avail:
data = self.read_from_endpoint(1)
if data:
self.connected_device.handle_data_available(1, data)
self.clear_irq_bit(self.reg_endpoint_irq, self.is_out1_data_avail)
if irq & self.is_in2_buffer_avail:
try:
self.connected_device.handle_buffer_available(2)
except:
pass
if irq & self.is_in3_buffer_avail:
try:
self.connected_device.handle_buffer_available(3)
except:
pass
tmp_irq = irq
self.disconnect()