-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidBoot.py
executable file
·266 lines (235 loc) · 7.82 KB
/
hidBoot.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
#!/usr/bin/python3
import hidBlProtocol
import sys
import platform
from sys import exit
from enum import Enum
import subprocess
import sys
try:
import usb.core
import usb.util
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyusb"])
import usb.core
import usb.util
class Command(Enum):
NONE = 1
LIST = 2
ERASE = 3
WRITE = 4
DEV_VER = 5
EXT_WRITE = 6
EXT_READ = 7
CP_EXT_INT = 8
REBOOT_BOOTLOADER = 9
usbBuf = bytearray(64)
def READ_RETRIES():
return 4
def usbSendRecv(ep, sendBuf):
recvBuf = bytearray(64)
retries = READ_RETRIES()
ep.write(sendBuf)
waiting = 0
# RUN_INT is the only command not to ACK
if hidBlProtocol.Packet.RUN_INT.value is not sendBuf[0]:
while retries > 0:
try:
recvBuf = dev.read(0x81, 64, 2500)
# print("Success")
if hidBlProtocol.Packet.USB_WAIT.value == recvBuf[0]:
if 0 == waiting:
print("Waiting.", end="", flush=True)
waiting = 1
else:
print(".", end="", flush=True)
else:
return recvBuf
except:
print("timeout")
retries = retries - 1
ep.write(sendBuf)
print("Error: USB timeout.")
exit(1)
def printUsage():
print("Usage examples:\n")
print("Copy external flash to internal:")
print(" hidBoot c\r\n")
print("Device bootloader version:")
print(" hidBoot d\r\n")
print("Erase internal flash:")
print(" hidBoot e\r\n")
print("List supported controllers:")
print(" hidBoot l\r\n")
print("Reboot application into bootloader:")
print(" hidBoot r\r\n")
print("Write internal flash:")
print(" hidBoot w 0x3000 blinky.bin\r\n")
print("Write external flash:")
print(" hidBoot x blinky.bin\r\n")
exit(1)
# ########### MAIN ##############
if len(sys.argv) >= 2:
if 'c' == sys.argv[1]:
command = Command.CP_EXT_INT
elif 'd' == sys.argv[1]:
command = Command.DEV_VER
elif 'e' == sys.argv[1]:
command = Command.ERASE
elif 'l' == sys.argv[1]:
command = Command.LIST
print("Supported controllers:\n")
print("Nuvoton M032LG6AE")
print("Atmel ATSAMD21E17A")
print("\n")
exit(0)
elif 'r' == sys.argv[1]:
command = Command.REBOOT_BOOTLOADER
elif 'w' == sys.argv[1]:
if len(sys.argv) < 4:
print("Error, too few arguments for command.\n")
printUsage()
command = Command.WRITE
elif 'x' == sys.argv[1]:
if len(sys.argv) < 2:
print("Error, too few arguments for command.\n")
printUsage()
command = Command.EXT_WRITE
elif 'y' == sys.argv[1]:
command = Command.EXT_READ
else:
printUsage()
else:
printUsage()
# find our device
dev = usb.core.find(idVendor=0x0416, idProduct=0x5020)
# was it found?
if dev is None:
print('Device not found')
exit(1)
if "Linux" == platform.system():
if dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
# print("kernel driver detached")
except usb.core.USBError as e:
sys.exit("Could not detach kernel driver: ")
# else:
# print("no kernel driver attached")
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0, 0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match=lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
assert ep is not None
if Command.DEV_VER == command:
print("Checking firmware bootloader version:\n")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.GET_BL_VER, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
print("Version: %d.%d.%d" % (usbBuf[8], usbBuf[7], usbBuf[6]))
elif Command.CP_EXT_INT == command:
print("Copying external flash to internal...", end='', flush=True)
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.COPY_EXT_TO_INT, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
if hidBlProtocol.Packet.ACK.value != usbBuf[0]:
print("Copy failed")
else:
print("Done.")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.RUN_INT, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
elif Command.ERASE == command:
print("Eraseing firmware...", end='', flush=True)
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.ERASE_INT_FLASH, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
if hidBlProtocol.Packet.ACK.value != usbBuf[0]:
print("Erase failed")
else:
print("Done.")
elif Command.WRITE == command:
try:
f = open(sys.argv[3], "rb")
image = f.read()
f.close()
# print("Len: ", len(image))
print("Writing firmware")
filePtr = 0
progress = 0
while filePtr < len(image):
progress = (filePtr / len(image)) * 100
print("Progress: " + "{:.0f}".format(progress) + "% \r", end='')
if (len(image) - filePtr) >= 32:
segLen = 32
else:
segLen = len(image) - filePtr
hidBlProtocol.hidBlProtocolEncodePacket(
0, int(sys.argv[2], 16) + filePtr, hidBlProtocol.Packet.WRITE_INT_FLASH, image, filePtr, segLen, usbBuf)
filePtr = filePtr + 32
usbBuf = usbSendRecv(ep, usbBuf)
if hidBlProtocol.Packet.ACK.value != usbBuf[0]:
print("\nWrite failed, check address?")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.RUN_INT, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
print('')
except:
print("File not found!")
elif Command.EXT_WRITE == command:
try:
f = open(sys.argv[3], "rb")
image = f.read()
f.close()
except:
print("File not found!")
exit(1)
# print(image)
# print("Len: ", len(image))
print("Erasing ext flash")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.ERASE_EXT_FLASH, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
print("Writing ext flash image")
filePtr = 0
progress = 0
while filePtr < len(image):
progress = (filePtr / len(image)) * 100
print("Progress: " + "{:.0f}".format(progress) + "% \r", end='')
if (len(image) - filePtr) >= 32:
segLen = 32
else:
segLen = len(image) - filePtr
hidBlProtocol.hidBlProtocolEncodePacket(
0, filePtr, hidBlProtocol.Packet.WRITE_EXT_FLASH, image, filePtr, segLen, usbBuf)
filePtr = filePtr + 32
usbBuf = usbSendRecv(ep, usbBuf)
if hidBlProtocol.Packet.ACK.value != usbBuf[0]:
print("\nWrite failed, check address?")
# hidBlProtocol.hidBlProtocolEncodePacket(
# 0, 0, hidBlProtocol.Packet.RUN_INT, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
print('')
elif Command.EXT_READ == command:
# try:
# f = open(sys.argv[4], "rb")
# image = f.read()
# f.close()
# except:
# print("File not found!")
print("Reading ext flash image")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.READ_EXT_FLASH, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)
print('Sent')
elif Command.REBOOT_BOOTLOADER == command:
print("Rebooting into bootloader")
hidBlProtocol.hidBlProtocolEncodePacket(
0, 0, hidBlProtocol.Packet.REBOOT_BOOTLOADER, '', 0, 0, usbBuf)
usbBuf = usbSendRecv(ep, usbBuf)