forked from MarsTechHAN/ch552tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch55xtool.py
339 lines (261 loc) · 10.8 KB
/
ch55xtool.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
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
import sys, os
import time
import array
import math
import argparse
import usb.core
import usb.util
#======= Some C-like static constants =======
DFU_ID_VENDOR = 0x4348
DFU_ID_PRODUCT = 0x55e0
EP_OUT_ADDR = 0x02
EP_IN_ADDR = 0x82
USB_MAX_TIMEOUT = 2000
DETECT_CHIP_CMD_V2 = [0xa1, 0x12, 0x00, 0x52, 0x11, 0x4d, 0x43, 0x55, 0x20, 0x49, 0x53,
0x50, 0x20, 0x26, 0x20, 0x57, 0x43, 0x48, 0x2e, 0x43, 0x4e]
END_FLASH_CMD_V2 = [0xa2, 0x01, 0x00, 0x00]
RESET_RUN_CMD_V2 = [0xa2, 0x01, 0x00, 0x01]
SEND_KEY_CMD_V20 = [0xa3, 0x30, 0x00]
SEND_KEY_CMD_V23 = [0xa3, 0x38, 0x00] + [0x00] * (0x38)
ERASE_CHIP_CMD_V2 = [0xa4, 0x01, 0x00, 0x08]
WRITE_CMD_V2 = [0xa5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
VERIFY_CMD_V2 = [0xa6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
READ_CFG_CMD_V2 = [0xa7, 0x02, 0x00, 0x1f, 0x00]
CH55X_IC_REF = {}
CH55X_IC_REF[0x51] = {'device_name': 'CH551', 'device_flash_size': 10240, 'device_dataflash_size': 128, 'chip_id': 0x51}
CH55X_IC_REF[0x52] = {'device_name': 'CH552', 'device_flash_size': 16384, 'device_dataflash_size': 128, 'chip_id': 0x52}
CH55X_IC_REF[0x53] = {'device_name': 'CH553', 'device_flash_size': 10240, 'device_dataflash_size': 128, 'chip_id': 0x53}
CH55X_IC_REF[0x54] = {'device_name': 'CH554', 'device_flash_size': 14336, 'device_dataflash_size': 128, 'chip_id': 0x54}
CH55X_IC_REF[0x59] = {'device_name': 'CH559', 'device_flash_size': 61440, 'device_dataflash_size': 128, 'chip_id': 0x59}
#=============================================
def __get_dfu_device(idVendor=DFU_ID_VENDOR, idProduct=DFU_ID_PRODUCT):
dev = usb.core.find(idVendor=idVendor, idProduct=idProduct)
if dev is None:
return (None, 'NO_DEV_FOUND')
try:
if dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
except usb.core.USBError:
return (None, 'USB_ERROR_CANNOT_DETACH_KERNEL_DRIVER')
except:
pass #Windows dont need detach
try:
dev.set_configuration()
except usb.core.USBError:
return (None, 'USB_ERROR_CANNOT_SET_CONFIG')
try:
usb.util.claim_interface(dev, 0)
except usb.core.USBError:
return (None, 'USB_ERROR_CANNOT_CLAIM_INTERFACE')
return (dev, '')
def __detect_ch55x_v2(dev):
dev.write(EP_OUT_ADDR, DETECT_CHIP_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
try:
return CH55X_IC_REF[ret[4]]
except KeyError:
return None
def __read_cfg_ch55x_v2(dev):
dev.write(EP_OUT_ADDR, READ_CFG_CMD_V2)
ret = dev.read(EP_IN_ADDR, 30, USB_MAX_TIMEOUT)
ver_str = 'V%d.%d%d' % (ret[19], ret[20], ret[21])
chk_sum = (ret[22] + ret[23] + ret[24] + ret[25]) % 256
return (ver_str, chk_sum)
def __write_key_ch55x_v20(dev, chk_sum):
SEND_KEY_CMD_V20 = SEND_KEY_CMD_V20 + [chk_sum] * 0x30
dev.write(EP_OUT_ADDR, SEND_KEY_CMD_V20)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
if ret[3] == 0:
return True
else:
return None
def __write_key_ch55x_v23(dev):
dev.write(EP_OUT_ADDR, SEND_KEY_CMD_V23)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
if ret[3] == 0:
return True
else:
return None
def __erase_chip_ch55x_v2(dev):
dev.write(EP_OUT_ADDR, ERASE_CHIP_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
if ret[3] == 0:
return True
else:
return None
def __write_flash_ch55x_v20(dev, chk_sum, chip_id, payload):
payload = payload + [0] * ((math.ceil(len(payload) / 56) * 56) - len(payload)) # Payload needs to be padded, 56 is a good number
file_length = len(payload)
for index in range(file_length):
if index % 8 == 7:
payload[index] = (payload[index] ^ ((chk_sum + chip_id) % 256)) % 256
left_len = file_length
curr_addr = 0
while curr_addr < file_length:
if left_len >= 56:
pkt_length = 56
else:
pkt_length = left_len
__WRITE_CMD_V2 = WRITE_CMD_V2
__WRITE_CMD_V2[1] = pkt_length + 5
__WRITE_CMD_V2[3] = curr_addr % 256
__WRITE_CMD_V2[4] = (curr_addr >> 8) % 256
__WRITE_CMD_V2[7] = left_len % 256
__WRITE_CMD_V2 = __WRITE_CMD_V2 + payload[curr_addr:curr_addr+pkt_length]
dev.write(EP_OUT_ADDR, __WRITE_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
curr_addr = curr_addr + pkt_length
left_len = left_len - pkt_length
if ret[4] != 0x00:
return None
return file_length
def __write_flash_ch55x_v23(dev, chk_sum, chip_id, payload):
payload = payload + [0] * ((math.ceil(len(payload) / 56) * 56) - len(payload)) # Payload needs to be padded, 56 is a good number
file_length = len(payload)
for index in range(file_length):
if index % 8 == 7:
payload[index] = (payload[index] ^ ((chk_sum + chip_id) % 256)) % 256
else:
payload[index] = (payload[index] ^ chk_sum) % 256
left_len = file_length
curr_addr = 0
while curr_addr < file_length:
if left_len >= 56:
pkt_length = 56
else:
pkt_length = left_len
__WRITE_CMD_V2 = WRITE_CMD_V2
__WRITE_CMD_V2[1] = pkt_length + 5
__WRITE_CMD_V2[3] = curr_addr % 256
__WRITE_CMD_V2[4] = (curr_addr >> 8) % 256
__WRITE_CMD_V2[7] = left_len % 256
__WRITE_CMD_V2 = __WRITE_CMD_V2 + payload[curr_addr:curr_addr+pkt_length]
dev.write(EP_OUT_ADDR, __WRITE_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
curr_addr = curr_addr + pkt_length
left_len = left_len - pkt_length
if ret[4] != 0x00:
return None
return file_length
def __verify_flash_ch55x_v20(dev, chk_sum, chip_id, payload):
payload = payload + [0] * ((math.ceil(len(payload) / 56) * 56) - len(payload)) # Payload needs to be padded, 56 is a good number
file_length = len(payload)
for index in range(file_length):
if index % 8 == 7:
payload[index] = (payload[index] ^ ((chk_sum + chip_id) % 256)) % 256
left_len = file_length
curr_addr = 0
while curr_addr < file_length:
if left_len >= 56:
pkt_length = 56
else:
pkt_length = left_len
__VERIFY_CMD_V2 = VERIFY_CMD_V2
__VERIFY_CMD_V2[1] = pkt_length + 5
__VERIFY_CMD_V2[3] = curr_addr % 256
__VERIFY_CMD_V2[4] = (curr_addr >> 8) % 256
__VERIFY_CMD_V2[7] = left_len % 256
__VERIFY_CMD_V2 = __VERIFY_CMD_V2 + payload[curr_addr:curr_addr+pkt_length]
dev.write(EP_OUT_ADDR, __VERIFY_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
curr_addr = curr_addr + pkt_length
left_len = left_len - pkt_length
if ret[4] != 0x00:
return None
return file_length
def __verify_flash_ch55x_v23(dev, chk_sum, chip_id, payload):
payload = payload + [0] * ((math.ceil(len(payload) / 56) * 56) - len(payload)) # Payload needs to be padded, 56 is a good number
file_length = len(payload)
for index in range(file_length):
if index % 8 == 7:
payload[index] = (payload[index] ^ ((chk_sum + chip_id) % 256)) % 256
else:
payload[index] = (payload[index] ^ chk_sum) % 256
left_len = file_length
curr_addr = 0
while curr_addr < file_length:
if left_len >= 56:
pkt_length = 56
else:
pkt_length = left_len
__VERIFY_CMD_V2 = VERIFY_CMD_V2
__VERIFY_CMD_V2[1] = pkt_length + 5
__VERIFY_CMD_V2[3] = curr_addr % 256
__VERIFY_CMD_V2[4] = (curr_addr >> 8) % 256
__VERIFY_CMD_V2[7] = left_len % 256
__VERIFY_CMD_V2 = __VERIFY_CMD_V2 + payload[curr_addr:curr_addr+pkt_length]
dev.write(EP_OUT_ADDR, __VERIFY_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
curr_addr = curr_addr + pkt_length
left_len = left_len - pkt_length
if ret[4] != 0x00:
return None
return file_length
def __end_flash_ch55x_v2(dev):
dev.write(EP_OUT_ADDR, END_FLASH_CMD_V2)
ret = dev.read(EP_IN_ADDR, 6, USB_MAX_TIMEOUT)
if ret[4] != 0x00:
return None
else:
return True
def __restart_run_ch55x_v2(dev):
dev.write(EP_OUT_ADDR, RESET_RUN_CMD_V2)
def __main():
parser = argparse.ArgumentParser(description="USBISP Tool For WinChipHead CH55x.")
parser.add_argument('-f', '--file', type=str, default='', help="The target file to be flashed.")
parser.add_argument('-r', '--reset_after_flash', action='store_true', default=False, help="Reset after finsh flash.")
args = parser.parse_args()
ret = __get_dfu_device()
if ret[0] is None:
sys.exit(ret[1])
dev = ret[0]
ret = __detect_ch55x_v2(dev)
if ret is None:
print('Unable to detect CH55x.')
print('Found %s.' % ret['device_name'])
chip_id = ret['chip_id']
ret = __read_cfg_ch55x_v2(dev)
chk_sum = ret[1]
print('BTVER: %s.' % ret[0])
if args.file != '':
payload = list(open(args.file, 'rb').read())
if ret[0] == 'V2.30':
ret = __write_key_ch55x_v20(dev, chk_sum)
if ret is None:
sys.exit('Failed to write key to CH55x.')
ret = __erase_chip_ch55x_v2(dev)
if ret is None:
sys.exit('Failed to erase CH55x.')
ret = __write_flash_ch55x_v20(dev, chk_sum, chip_id, payload)
if ret is None:
sys.exit('Failed to flash firmware of CH55x.')
ret = __verify_flash_ch55x_v20(dev, chk_sum, chip_id, payload)
if ret is None:
sys.exit('Failed to verify firmware of CH55x.')
else:
if (ret[0] == 'V2.31') or (ret[0] == 'V2.40'):
ret = __write_key_ch55x_v23(dev)
if ret is None:
sys.exit('Failed to write key to CH55x.')
ret = __erase_chip_ch55x_v2(dev)
if ret is None:
sys.exit('Failed to erase CH55x.')
ret = __write_flash_ch55x_v23(dev, chk_sum, chip_id, payload)
if ret is None:
sys.exit('Failed to flash firmware of CH55x.')
ret = __verify_flash_ch55x_v23(dev, chk_sum, chip_id, payload)
if ret is None:
sys.exit('Failed to verify firmware of CH55x.')
else:
sys.exit('Bootloader version not supported.')
ret = __end_flash_ch55x_v2(dev)
if ret is None:
sys.exit('Failed to end flash process.')
print('Flash done.')
if args.reset_after_flash:
__restart_run_ch55x_v2(dev)
print('Restart and run.')
if __name__ == '__main__':
__main()