forked from AdamLaurie/CC-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootload.py
executable file
·253 lines (213 loc) · 6.9 KB
/
bootload.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
#!/usr/bin/env python
import serial
bootloader_error_codes = {
'0' : "OK",
'1' : "Intel HEX Invalid",
'2' : "Bad Checksum",
'3' : "Bad Address",
'4' : "Bad Record Type",
'5' : "Record Too Long"
}
def download_code(ihx_file, serial_port):
for line in ihx_file.readlines():
record_type = int(line[7:9], 16)
if (record_type == 0x00):
print "Writing", line[:-1],
serial_port.write(line)
rc = serial_port.read()
print " RC =", rc,
if rc in bootloader_error_codes:
print "(%s)" % bootloader_error_codes[rc]
else:
print "(Unknown Error)"
if (rc != '0'):
print "Error downloading code!"
return False
else:
print "Skipping non data record: '%s'" % line[:-1]
return True
def verify_code(ihx_file, serial_port):
can_read_any= None
for line in ihx_file.readlines():
record_type = int(line[7:9], 16)
if (record_type == 0x00):
length = int(line[1:3], 16)
start_addr = int(line[3:7], 16)
data = line[9:9+(length*2)]
# first time around, check if we can only read 16 byte chunks
if can_read_any == None:
can_read_any = False
do_flash_read(serial_port, start_addr, 1)
for read_data in serial_port:
read_data = read_data.strip()
if not read_data:
continue
if not read_data == ":00000001FF":
can_read_any = True
else:
break
if not can_read_any:
print "*** warning! this version of CC-Bootloader can only read 16 byte blocks!"
print "*** upgrade recommended!"
if can_read_any:
block_length= length
else:
block_length= ((length / 16) + 1) * 16
print "\rVerifying %04d bytes at address: %04X" % (length, start_addr),
do_flash_read(serial_port, start_addr, block_length)
verify_data= ''
for read_data in serial_port:
read_data= read_data.strip()
if (not data or read_data == ":00000001FF"):
break
# strip header and checksum
verify_data += read_data[9:-2]
if (data == verify_data[:length*2]):
print '(OK)',
else:
print 'Failed! Expected:', data, 'Got:', verify_data[:length*2]
exit(1)
sys.stdout.flush()
else:
print "Skipping non data record: '%s'" % line[:-1]
return True
def run_user_code(serial_port):
# User code is entered on intel HEX EOF record
serial_port.write(":00000001FF\n")
return True
def reset_bootloader(serial_port):
serial_port.write(":00000022DE\n")
rc = serial_port.read()
print "RC =", rc,
if rc in bootloader_error_codes:
print "(%s)" % bootloader_error_codes[rc]
else:
print "(Unknown Error)"
if (rc != '0'):
print "Error resetting bootloader!"
return False
return True
def erase_all_user(serial_port):
serial_port.write(":00000023DD\n")
rc = serial_port.read()
print "RC =", rc,
if rc in bootloader_error_codes:
print "(%s)" % bootloader_error_codes[rc]
else:
print "(Unknown Error)"
if (rc != '0'):
print "Error erasing all user flash!"
return False
return True
def erase_user_page(serial_port, page):
chksum = (0xDB + 0x100 - page) & 0xFF
serial_port.write(":01000024%02X%02X\n" % (page, chksum))
rc = serial_port.read()
print "RC =", rc,
if rc in bootloader_error_codes:
print "(%s)" % bootloader_error_codes[rc]
else:
print "(Unknown Error)"
if (rc != '0'):
print "Error erasing user flash page!"
return False
return True
def do_flash_read(serial_port, start_addr, length):
chksum = (0xD9 +
(0x100 - (start_addr & 0xFF)) +
(0x100 - ((start_addr>>8) & 0xFF)) +
(0x100 - (length & 0xFF)) +
(0x100 - ((length>>8) & 0xFF))
) & 0xFF
serial_port.write(":02%04X25%04X%02X\n" % (start_addr, length, chksum))
def flash_read(ihx_file, serial_port, start_addr, length):
do_flash_read(serial_port, start_addr, length)
for line in serial_port:
if not line == "\n":
if(ihx_file):
ihx_file.write(line)
else:
print line,
if (line == ":00000001FF\n"):
break
def print_usage():
import sys
print """
CC Bootloader Download Utility
Usage: ./bootload.py serial_port command
Commands:
download <hex_file>
Download hex_file to the device.
run
Run the user code.
reset
The bootloader will not erase pages that have previously been written to
before writing new data to that page. This allows for random access writes
but prevents you from overwriting downloaded code unless the device is
power cycled. This command will reset the bootloader's record of what
pages have been written to, allowing you to overwrite without power
cycling.
erase_all
Erases the entire user flash area.
erage <n>
Erases page n of the flash memory (organised into 1024 byte pages). The
bootloader occupies the first few pages and the rest are reserved for user
code. Attempting to erase a bootloader page will have no effect. To
determine which page the user code starts on please check the
USER_CODE_BASE setting in main.h.
read <start_addr> <len> [hex_file]
Reads len bytes from flash memory starting from start_addr and optionally
write to hex_file. start_addr and len should be specified in hexadecimal
(e.g. 0x1234).
verify <hex_file>
Verify hex_file matches device flash memory.
"""
if __name__ == '__main__':
import sys
if (len(sys.argv) < 3):
print_usage()
sys.exit(1)
serial_port_name = sys.argv[1]
command = sys.argv[2]
options = sys.argv[3:]
serial_port = serial.Serial(serial_port_name, timeout=1)
try:
if (command == 'download' or command == 'verify'):
if (len(options) < 1):
print_usage()
else:
ihx_filename = options[0]
ihx_file = open(ihx_filename, 'r')
if (command == 'download'):
download_code(ihx_file, serial_port)
else:
verify_code(ihx_file, serial_port)
elif (command == 'run'):
run_user_code(serial_port)
elif (command == 'reset'):
reset_bootloader(serial_port)
elif (command == 'erase_all'):
erase_all_user(serial_port)
elif (command == 'erase'):
if (len(options) < 1):
print_usage()
else:
erase_user_page(serial_port, int(options[0]))
elif (command == 'read'):
if (len(options) < 2):
print_usage()
else:
ihx_file = None
if(len(options) == 3):
try:
ihx_filename = options[2]
ihx_file = open(ihx_filename, 'w')
print 'reading to:', ihx_filename
except:
print "couldn't open output file:", ihx_filename
exit(2)
flash_read(ihx_file, serial_port, int(options[0], 16), int(options[1], 16))
else:
print_usage()
finally:
serial_port.close()