forked from RyanHope/HondaECU
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflash.py
executable file
·184 lines (164 loc) · 6.38 KB
/
flash.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
#!/usr/bin/env python
from __future__ import division, print_function
import struct
import time
import sys
import os
import argparse
from HondaECU import *
def do_validation(binfile, fix=False):
print("==============================================================================")
if fix:
print("Fixing bin file checksum")
else:
print("Validating bin file checksum")
with open(binfile, "rb") as fbin:
byts, fcksum, ccksum, fixed = validate_checksum(bytearray(fbin.read(os.path.getsize(binfile))), fix)
if fixed:
status = "fixed"
elif fcksum == ccksum:
status = "good"
else:
status = "bad"
print(" file checksum: %s" % fcksum)
print(" calculated checksum: %s" % ccksum)
print(" status: %s" % status)
return byts, fcksum, ccksum, fixed
def do_read_flash(ecu, binfile, rom_size, debug=False):
maxbyte = 1024 * rom_size
nbyte = 0
readsize = 8
with open(binfile, "wb") as fbin:
t = time.time()
while nbyte < maxbyte:
info = ecu.send_command([0x82, 0x82, 0x00], [int(nbyte/65536)] + [b for b in struct.pack("<H", nbyte % 65536)] + [readsize], debug=args.debug)
fbin.write(info[2])
fbin.flush()
nbyte += readsize
if nbyte % 64 == 0:
sys.stdout.write(".")
if nbyte % 1024 == 0:
n = time.time()
sys.stdout.write(" %dkB %.02fBps\n" % (int(nbyte/1024),1024/(n-t)))
t = n
sys.stdout.flush()
def do_write_flash(ecu, byts, debug=False, offset=0x00):
writesize = 128
maxi = len(byts)/128
i = 0
t = time.time()
while i < maxi:
bytstart = [s for s in struct.pack(">H",offset+(8*i))]
bytend = [s for s in struct.pack(">H",offset+(8*(i+1)))]
d = list(byts[((i+0)*128):((i+1)*128)])
x = bytstart + d + bytend
c1 = checksum8bit(x)
c2 = checksum8bitHonda(x)
x = [0x01, 0x06] + x + [c1, c2]
info = ecu.send_command([0x7e], x, debug=debug)
if ord(info[1]) != 5:
sys.stdout.write(" error\n")
sys.exit(1)
i += 1
if i % 2 == 0:
ecu.send_command([0x7e], [0x01, 0x08], debug=debug)
sys.stdout.write(".")
if i % 64 == 0:
n = time.time()
w = (i*writesize)
sys.stdout.write(" %dkB %.02fBps\n" % (int(w/1024),(64*128)/(n-t)))
t = n
sys.stdout.flush()
def do_post_write(ecu, debug=False):
ecu.send_command([0x7e], [0x01, 0x01, 0x00], debug=debug)
ecu.send_command([0x7e], [0x01, 0x09], debug=debug)
ecu.send_command([0x7e], [0x01, 0x01, 0x00], debug=debug)
ecu.send_command([0x7e], [0x01, 0x0a], debug=debug)
ecu.send_command([0x7e], [0x01, 0x01, 0x00], debug=debug)
ecu.send_command([0x7e], [0x01, 0x0c], debug=debug)
ecu.send_command([0x7e], [0x01, 0x01, 0x00], debug=debug)
ecu.send_command([0x7e], [0x01, 0x0d], debug=debug)
ecu.send_command([0x7e], [0x01, 0x01, 0x00], debug=debug)
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('mode', choices=["read","write","recover","validate_checksum","fix_checksum"], help="ECU mode")
parser.add_argument('binfile', help="name of bin to read or write")
parser.add_argument('--write-mode', type=int, choices=[0,1], default=0, help="ECU write technique")
parser.add_argument('--recover-mode', type=int, choices=[0,1], default=0, help="ECU recover technique")
parser.add_argument('--rom-size', default=256, type=int, help="size of ECU rom in kilobytes")
db_grp = parser.add_argument_group('debugging options')
db_grp.add_argument('--skip-power-check', action='store_true', help="don't test for k-line activity")
db_grp.add_argument('--fix-checksum', action='store_true', help="fix checksum before write/recover")
db_grp.add_argument('--debug', action='store_true', help="turn on debugging output")
args = parser.parse_args()
offset = 0x00
if os.path.isabs(args.binfile):
binfile = args.binfile
else:
binfile = os.path.abspath(os.path.expanduser(args.binfile))
ret = 1
if args.mode != "read":
byts, fcksum, ccksum, fixed = do_validation(binfile, args.mode == "fix_checksum" or args.fix_checksum)
if (fcksum == ccksum or fixed) and args.mode in ["recover","write"]:
ret = 0
else:
ret = -1
else:
ret = 0
if ret == 0:
ecu = HondaECU()
ecu.setup()
if not args.skip_power_check:
print("===============================================")
# In order to read flash we must send commands while the ECU's
# bootloader is still active which is right after the bike powers up
if ecu.kline():
print("Turn off bike")
while ecu.kline():
time.sleep(.1)
print("Turn on bike")
while not ecu.kline():
time.sleep(.1)
time.sleep(.5)
if args.mode != "recover":
print("===============================================")
print("Initializing ECU communications")
ecu.init(debug=args.debug)
ecu.send_command([0x72],[0x00, 0xf0], debug=args.debug)
if args.mode == "read":
print("===============================================")
print("Entering Boot Mode")
ecu.send_command([0x27],[0xe0, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x48, 0x6f], debug=args.debug)
ecu.send_command([0x27],[0xe0, 0x77, 0x41, 0x72, 0x65, 0x59, 0x6f, 0x75], debug=args.debug)
print("===============================================")
print("Dumping ECU to bin file")
do_read_flash(ecu, binfile, args.rom_size, debug=args.debug)
do_validation(binfile)
elif args.mode == "write":
# if write fails for some reason it looks like you can resume
# starting with the pre_write command, the ECU does not respond to
# any of the normal init commands (write specific or not)
print("===============================================")
print("Writing bin file to ECU")
if args.write_mode == 0:
ecu.do_init_write(debug=args.debug)
elif args.write_mode == 1:
offset = 0x8000
ecu.do_init_recover(debug=args.debug)
ecu.send_command([0x72],[0x00, 0xf1], debug=args.debug)
ecu.send_command([0x27],[0x00, 0x9f, 0x00], debug=args.debug)
ecu.do_pre_write(debug=args.debug)
ecu.do_pre_write_wait(debug=args.debug)
do_write_flash(ecu, byts, offset=offset, debug=args.debug)
do_post_write(ecu, debug=args.debug)
else:
print("===============================================")
print("Recovering ECU")
if args.recover_mode == 1:
ecu.send_command([0x27],[0x00, 0x00, 0x00], debug=args.debug)
ecu.do_pre_write(debug=args.debug)
ecu.do_pre_write_wait(debug=args.debug)
do_write_flash(ecu, byts, offset=offset, debug=args.debug)
do_post_write(ecu, debug=args.debug)
print("===============================================")
sys.exit(ret)