forked from badgeteam/mch2022-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebusb.py
329 lines (272 loc) · 11.4 KB
/
webusb.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import os
import usb.core
import usb.util
from enum import Enum
import struct
import time
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def printProgressTime(
totalMilliSeconds,
steps=10,
prefix="",
suffix="",
decimals=1,
length=20,
fill="█",
printEnd="\r",
):
"""
prints progress while sleeping totalMilliSeconds, bar is updated every `steps` milliseconds
other params
"""
for i in range(0, totalMilliSeconds, steps):
printProgressBar(
i,
totalMilliSeconds - steps,
prefix,
suffix,
decimals,
length,
fill,
printEnd,
)
time.sleep(steps / 1000)
class Commands(Enum):
EXECFILE = 0
HEARTBEAT = 1
APPFSBOOT = 3
GETDIR = 4096
READFILE = 4097
WRITEFILE = 4098
DELFILE = 4099
DUPLFILE = 4100
MVFILE = 4101
MAKEDIR = 4102
APPFSFDIR = 4103
APPFSDEL = 4104
APPFSWRITE = 4105
class WebUSBPacket():
def __init__(self, command, message_id, payload=None):
self.command = command
self.message_id = message_id
if (payload == None):
self.payload = b""
else:
self.payload = payload
def getMessage(self):
return self._generateHeader() + self.payload
def _generateHeader(self):
return struct.pack("<HIHI", self.command.value, len(self.payload), 0xADDE, self.message_id)
class WebUSB():
def __init__(self, boot = True):
if os.name == 'nt':
from usb.backend import libusb1
lube = libusb1.get_backend(find_library=lambda x: os.path.dirname(__file__) + "\\libusb-1.0.dll")
self.device = usb.core.find(idVendor=0x16d0, idProduct=0x0f9a, backend=lube)
else:
self.device = usb.core.find(idVendor=0x16d0, idProduct=0x0f9a)
if self.device is None:
raise ValueError("Badge not found")
self.configuration = self.device.get_active_configuration()
self.webusb_esp32 = self.configuration[(4,0)]
self.ep_out = usb.util.find_descriptor(self.webusb_esp32, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
self.ep_in = usb.util.find_descriptor(self.webusb_esp32, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
self.REQUEST_TYPE_CLASS_TO_INTERFACE = usb.util.CTRL_TYPE_CLASS + usb.util.CTRL_RECIPIENT_INTERFACE
self.REQUEST_STATE = 0x22
self.REQUEST_RESET = 0x23
self.REQUEST_BAUDRATE = 0x24
self.REQUEST_MODE = 0x25
self.TIMEOUT = 60
self.PAYLOADHEADERLEN = 12
if boot:
self.bootWebUSB()
self.message_id = 1
def getMessageId(self):
self.message_id += 1
return self.message_id
def bootWebUSB(self):
"""
Boots the badge into webusb mode
"""
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_STATE, 0x0001, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_MODE, 0x0001, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_RESET, 0x0000, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_BAUDRATE, 9216, self.webusb_esp32.bInterfaceNumber)
print("Booting into WebUSB, please wait ...")
printProgressTime(4000) # Wait for the badge to have boot into webusb mode
while True:
try:
self.ep_in.read(128)
except Exception as e:
break
def receiveResponse(self, show_hourglass=False):
"""
receveives the response, for longish response times,
you can keep the user occupied by setting show_hourglass to True
"""
response = bytes([])
starttime = time.time()
anim = 0
while (time.time() - starttime) < self.TIMEOUT:
if show_hourglass:
print(f"\rWaiting for response { (1+(anim%3)) * '.'} ", end="\r")
anim += 1
data = None
try:
data = bytes(self.ep_in.read(128))
except Exception as e:
pass
if data != None:
response += data
if len(response) >= self.PAYLOADHEADERLEN:
if show_hourglass:
print()
command, payloadlen, verif, message_id = struct.unpack_from("<HIHI", response)
if verif != 0xADDE:
raise Exception("Failed verification")
if len(response) >= (payloadlen + self.PAYLOADHEADERLEN):
return (command, message_id, response[self.PAYLOADHEADERLEN:])
raise Exception("Timeout in receiving")
def sendPacket(self, packet, transfersize=2048, show_response_hourglass=False):
transfersize = 2048
msg = packet.getMessage()
starttime = time.time()
if len(msg) > transfersize:
printProgressBar(0, len(msg) // transfersize, length=20)
for i in range(0, len(msg), transfersize):
self.ep_out.write(msg[i:i+transfersize])
time.sleep(0.01)
if len(msg) > transfersize:
printProgressBar(i//transfersize, len(msg) // transfersize, length=20)
#self.ep_out.write(packet.getMessage())
print(f"transfer speed: {(len(msg)/(time.time()-starttime)/1024):0.2f} kb/s")
command, message_id, data = self.receiveResponse(show_hourglass=show_response_hourglass)
if message_id != packet.message_id:
raise Exception("Mismatch in id")
if command != packet.command.value:
raise Exception("Mismatch in command")
return data
def sendHeartbeat(self):
"""
Send heartbeat towards the badges
parameters:
None
returns:
bool : True if badge responded, false if no response
"""
data = self.sendPacket(WebUSBPacket(Commands.HEARTBEAT, self.getMessageId()))
return data.decode().rstrip('\x00') == "ok"
def getFSDir(self, dir):
"""
Get files and directories on the badge filesystem
parameters:
dir (str) : Directory to get contents from
returns:
res (dict) : Dict containing 'dir' which is the requested directory, 'files' list of files in the directory, 'dirs' list of directories
"""
data = self.sendPacket(WebUSBPacket(Commands.GETDIR, self.getMessageId(), dir.encode(encoding='ascii')))
data = data.decode()
data = data.split("\n")
result = dict()
result["dir"] = data[0]
result["files"] = list()
result["dirs"] = list()
for i in range(1, len(data)):
fd = data[i]
if fd[0] == "f":
result["files"].append(fd[1:])
else:
result["dirs"].append(fd[1:])
return result
def pushFSfile(self, filename, file):
"""
Upload file to fs
root path should /flash or /sdcard
parameters:
filename (str) : name of the file
file (bytes) : file contents as byte array
returns:
bool : true if file was uploaded
"""
payload = filename.encode(encoding='ascii') + b"\x00" + file
data = self.sendPacket(WebUSBPacket(Commands.WRITEFILE, self.getMessageId(), payload))
return data.decode().rstrip('\x00') == "ok"
def appfsUpload(self, appname, file):
"""
Upload app to appfs
parameters:
appname (str) : name of the app
file (bytes) : the app to be upload
returns:
bool : true if app was uploaded succesfully
"""
payload = appname.encode(encoding="ascii") + b"\x00" + file
data = self.sendPacket(WebUSBPacket(Commands.APPFSWRITE, self.getMessageId(), payload), show_response_hourglass=True)
return data.decode().rstrip('\x00') == "ok"
def appfsRemove(self, appname):
"""
Remove app from appfs
parameters:
appname (str) : name of the app to be removed
returns:
bool : true if app was deleted succesfully
"""
payload = appname.encode(encoding="ascii") + b"\x00"
data = self.sendPacket(WebUSBPacket(Commands.APPFSDEL, self.getMessageId(), payload))
return data.decode().rstrip('\x00') == "ok"
def appfsExecute(self, appname):
"""
Execute app from appfs
parameters:
appname (str) : name of the app to be executed
returns:
bool : true if app was executed
"""
payload = appname.encode(encoding="ascii") + b"\x00"
data = self.sendPacket(WebUSBPacket(Commands.APPFSBOOT, self.getMessageId(), payload))
return data.decode().rstrip('\x00') == "ok"
def appfsList(self):
"""
Get apps from appfs
parameters:
None
returns:
list : list of dicts containing 'name' and 'size'
"""
data = self.sendPacket(WebUSBPacket(Commands.APPFSFDIR, self.getMessageId()))
num_apps, = struct.unpack_from("<I", data)
data = data[4:]
res = list()
for i in range(0, num_apps):
appsize, lenname = struct.unpack_from("<II", data)
data = data[8:]
name = data[0:lenname].decode(encoding="ascii")
data = data[lenname:]
res.append({"name":name, "size":appsize})
return res
def disconnect(self):
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_BAUDRATE, 1152, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_MODE, 0x0000, self.webusb_esp32.bInterfaceNumber)
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_STATE, 0x0000, self.webusb_esp32.bInterfaceNumber)
def reset(self):
self.device.ctrl_transfer(self.REQUEST_TYPE_CLASS_TO_INTERFACE, self.REQUEST_RESET, 0x0000, self.webusb_esp32.bInterfaceNumber)