/
cybootloaderhost.py
executable file
·331 lines (275 loc) · 10.9 KB
/
cybootloaderhost.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
#!/usr/bin/env python3
#
# Copyright (C) 2016 Forest Crossman <cyrozap@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import binascii
import sys
import hid
class Bootloader():
MAX_DATA_LENGTH = 64-7-9
STATUSES = {
0x00: "CYRET_SUCCESS",
0x03: "BOOTLOADER_ERR_LENGTH",
0x04: "BOOTLOADER_ERR_DATA",
0x05: "BOOTLOADER_ERR_CMD",
0x08: "BOOTLOADER_ERR_CHECKSUM",
0x09: "BOOTLOADER_ERR_ARRAY",
0x0a: "BOOTLOADER_ERR_ROW",
0x0c: "BOOTLOADER_ERR_APP",
0x0d: "BOOTLOADER_ERR_ACTIVE",
0x0e: "BOOTLOADER_ERR_CALLBACK",
0x0f: "BOOTLOADER_ERR_UNK"
}
def __init__(self, vid=0, pid=0, serial=None):
try:
self._device = hid.device()
self._device.open(vid, pid)
except OSError as error:
sys.stderr.write("Error: {}".format(error))
self.jtag_id = None
self.device_revision = None
self.bootloader_revision = None
def _checksum(self, data):
'''16-bit packet checksum.
:param data: The data to sum
:type data: bytes or list of 8-bit integers
:returns: checksum
:rtype: int
'''
checksum = 0
for byte in data:
checksum += byte
checksum = (~checksum + 1) & 0xffff
return checksum
def _make_packet(self, command, data=[]):
'''Generates a packet to send the bootloader.
:param command: The command to send
:type command: 8-bit int
:param data: The data to send
:type data: bytes or list of 8-bit integers
:returns: Packet data
:rtype: list of 8-bit integers
'''
packet = []
packet.append(0x01)
packet.append(command)
data_length = len(data)
assert data_length <= (64 - 7)
packet.append(data_length & 0xff)
packet.append(data_length >> 8)
for byte in data:
packet.append(byte)
checksum = self._checksum(packet)
packet.append(checksum & 0xff)
packet.append(checksum >> 8)
packet.append(0x17)
return packet
def _parse_response(self, response_data):
'''Parses the bootloader's response.
:param response_data: The response_data to parse
:type response_data: list of 8-bit integers
:returns: Response data
:rtype: dict
'''
response = {
"status": "BOOTLOADER_ERR_UNK",
"data": [],
"checksum_ok": False,
}
sop = response_data[0]
assert sop == 0x01
response["status"] = self.STATUSES[response_data[1]]
data_length = (response_data[3] << 8) | response_data[2]
if data_length > 0:
response["data"] = response_data[4:4+data_length]
checksum_received = response_data[4+data_length]
checksum_received |= response_data[4+data_length+1] << 8
checksum_calculated = self._checksum(response_data[:4+data_length])
response["checksum_ok"] = (checksum_calculated == checksum_received)
eop = response_data[4+data_length+2]
assert eop == 0x17
return response
def send_command(self, command, data=[]):
'''Sends a command with data to the bootloader.
:param command: The command to send
:type command: 8-bit int
:param data: The data to send
:type data: bytes or list of 8-bit integers
:returns: Response data
:rtype: dict
'''
packet = self._make_packet(command, data)
self._device.write(packet)
response_data = self._device.read(64)
response = self._parse_response(response_data)
return response
def enter_bootloader(self):
'''Sends the "Enter Bootloader" command.
:returns: Success/failure
:rtype: bool
'''
response = self.send_command(0x38)
if response["checksum_ok"] and response["status"] == "CYRET_SUCCESS":
self.jtag_id = response["data"][0]
self.jtag_id |= response["data"][1] << 8
self.jtag_id |= response["data"][2] << 16
self.jtag_id |= response["data"][3] << 24
self.device_revision = response["data"][4]
self.bootloader_revision = response["data"][5:8][::-1]
return True
else:
return False
def program_row(self, array_id, row_number, data):
'''Programs a row of flash.
:param array_id: The array_id of the row to program
:type array_id: 8-bit int
:param row_number: The row_number of the row to program
:type row_number: 16-bit int
:param data: The data to program
:type data: bytes or list of 8-bit integers
:returns: Success/failure
:rtype: bool
'''
arguments = [array_id, (row_number & 0xff), (row_number >> 8)]
response = self.send_command(0x39, arguments + data)
if response["checksum_ok"] and response["status"] == "CYRET_SUCCESS":
return True
else:
return False
def erase_row(self, array_id, row_number):
'''Erases a row of flash.
:param array_id: The array_id of the row to erase
:type array_id: 8-bit int
:param row_number: The row_number of the row to erase
:type row_number: 16-bit int
:returns: Success/failure
:rtype: bool
'''
arguments = [array_id, (row_number & 0xff), (row_number >> 8)]
response = self.send_command(0x34, arguments)
if response["checksum_ok"] and response["status"] == "CYRET_SUCCESS":
return True
else:
return False
def send_data(self, data):
'''Sends data to the bootloader to be used by another command.
:param data: The data to send
:type data: bytes or list of 8-bit integers
:returns: Success/failure
:rtype: bool
'''
response = self.send_command(0x37, data)
if response["checksum_ok"] and response["status"] == "CYRET_SUCCESS":
return True
else:
return False
def exit_bootloader(self):
'''Sends the "Exit Bootloader" command.'''
packet = self._make_packet(0x3b, [])
self._device.write(packet)
def flash(self, firmware):
firmware_data = firmware.firmware
for row in firmware_data:
array_id = row[0]
row_number = row[1]
data = list(row[2])
if len(data) >= self.MAX_DATA_LENGTH:
full_packets = len(data)//self.MAX_DATA_LENGTH
partial_packet_length = len(data)%self.MAX_DATA_LENGTH
if partial_packet_length > 0:
for i in range(0, full_packets):
self.send_data(data[self.MAX_DATA_LENGTH*i:self.MAX_DATA_LENGTH*i+self.MAX_DATA_LENGTH])
self.program_row(array_id, row_number, data[self.MAX_DATA_LENGTH*full_packets:self.MAX_DATA_LENGTH*full_packets+partial_packet_length])
else:
for i in range(0, full_packets-1):
self.send_data(data[self.MAX_DATA_LENGTH*i:self.MAX_DATA_LENGTH*i+self.MAX_DATA_LENGTH])
self.program_row(array_id, row_number, data[self.MAX_DATA_LENGTH*(full_packets-1):self.MAX_DATA_LENGTH*(full_packets-1)+self.MAX_DATA_LENGTH])
else:
self.program_row(array_id, row_number, data)
class Cyacd():
def __init__(self, firmware_file):
self.file = firmware_file
self.silicon_id = None
self.silicon_revision = None
self.checksum_type = None
self.firmware = None
def _checksum(self, data):
'''Single-byte flash row checksum.
:param data: The data to sum
:type data: bytes or list of 8-bit integers
:returns: checksum
:rtype: int
'''
checksum = 0
for byte in data:
checksum += byte
checksum = (~checksum + 1) & 0xff
return checksum
def parse(self):
'''Read's the firmware into a list of (array_id, row_number, data)
tuples.
'''
lines = self.file.readlines()
header_line = lines.pop(0).rstrip('\r\n')
header = binascii.a2b_hex(header_line)
self.silicon_id = header[3]
self.silicon_id |= header[2] << 8
self.silicon_id |= header[1] << 16
self.silicon_id |= header[0] << 24
self.silicon_revision = header[4]
self.checksum_type = header[5]
firmware = []
for line in lines:
line = binascii.a2b_hex(line[1:].rstrip('\r\n'))
array_id = line[0]
row_number = (line[1] << 8) | line[2]
data_length = (line[3] << 8) | line[4]
data = line[5:5+data_length]
checksum = line[5+data_length]
checksum_calculated = self._checksum(line[:-1])
assert checksum == checksum_calculated
firmware.append((array_id, row_number, data))
self.firmware = firmware
def main():
parser = argparse.ArgumentParser()
parser.add_argument("vid_pid_string", type=str, help="This is the same VID:PID string lsusb outputs. Example: 04b4:f13b")
parser.add_argument("firmware_file", type=str, help="The name of the *.cyacd file you want to flash.")
args = vars(parser.parse_args())
vid_pid_string = args["vid_pid_string"].split(':')
vid = int(vid_pid_string[0], 16)
pid = int(vid_pid_string[1], 16)
# Search for a device with matching identifiers
found = False
for enumerated in hid.enumerate():
if enumerated["vendor_id"] == vid and enumerated["product_id"] == pid:
found = True
break
if found:
# Connect to the device's bootloader
bootloader = Bootloader(vid, pid)
bootloader.enter_bootloader()
# Load the firmware file
firmware = Cyacd(open(args["firmware_file"], 'r'))
firmware.parse()
# Make sure the firmware is being flashed to the correct chip
if (bootloader.jtag_id == firmware.silicon_id) and (bootloader.device_revision == firmware.silicon_revision):
bootloader.flash(firmware)
bootloader.exit_bootloader()
else:
sys.stderr.write("Device not found!\n")
sys.exit(1)
if __name__ == "__main__":
main()