-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbl_protocol.py
346 lines (271 loc) · 10.4 KB
/
bl_protocol.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
340
341
342
343
344
345
346
"""This module contains BLProtocol class, which is programmer-side
implementation Bbouffalolab's ISP protocol.
"""
import time
import logging
import bl_errors
import queue
import threading
rx_queue = queue.Queue()
class BLProtocol:
def __init__(self, interface):
self.interface = interface
self.interface.register_rx_callback(self.rx_callback_handler)
self.cmd_id = {
'handshake' : b'\x55',
'get_boot_info' : b'\x10',
'load_boot_header' : b'\x11',
'load_segment_header' : b'\x17',
'load_segment_data' : b'\x18',
'check_image' : b'\x19',
'mem_write' : b'\x50',
'read_jedecid' : b'\x36',
'flash_erase' : b'\x30',
'flash_write' : b'\x31',
'flash_write_check' : b'\x3A',
'xip_read_start' : b'\x60',
'flash_xip_readsha' : b'\x3E',
'xip_read_finish' : b'\x61',
'efuse_read_mac_addr' : b'\x42',
}
@staticmethod
def rx_callback_handler(data):
rx_queue.put(data)
def get_response(self):
return rx_queue.get()
@staticmethod
def is_response_ok(response):
"""Check MCU's response.
Args:
response (bytearray): MCU response data.
Returns:
bool: True if operation was succesful.
Raises:
TODO
"""
logging.debug(f"Response: {response}")
#if response is None or len(response) < 2:
#raise bl_errors.InvalidResponseError()
if response[0:2] == b'OK':
result = True
elif response[0:2] == b'FL':
err_lsb = response[2]
err_msb = response[3]
logging.debug(f"err_msb: {err_msb}, err_lsb {err_lsb}")
raise bl_errors.errors_agregator[err_msb][err_lsb - 1]()
else:
raise bl_errors.InvalidResponseError()
return result
@staticmethod
def calc_checksum(data):
"""Calculate checksum.
Args:
data (bytearray): Data to calculate checksum of.
Returns:
int: Calculated checksum.
"""
logging.debug("Checksum calculation")
sum = 0
for e in data:
sum = sum + e
checksum = sum & 0xFF
logging.debug(f"Checksum value: {checksum}")
return checksum
def send_data_wait_for_response(self, data, timeout=0.1):
"""Send data to MCU and wait for its response.
Args:
data (bytearray): Data to sent.
timeout (float): Max waiting time for data, given in seconds.
Returns:
bytearray: Data received from UART.
"""
self.interface.send_data(data)
return self.get_response()
def handshake(self):
"""Perform handshake with MCU's bootloader.
Returns:
bool: True if operation was succesful.
"""
logging.debug("Handshake")
command = (self.cmd_id['handshake']
* int(0.005 * (self.interface.uart.baudrate / 10)))
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def get_boot_info(self):
"""Send "get_boot_info" command to MCU.
Returns:
bytearray: MCU's response
"""
logging.debug("GetBootInfo command")
command = self.cmd_id['get_boot_info'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
if self.is_response_ok(response):
return response[2:]
def load_boot_header(self, boot_header):
"""Send "load_boot_header" command to MCU.
Args:
boot_header (bytearray): Boot header data (?).
Returns:
bool: True if operation was succesful.
Raise:
TODO
"""
logging.debug("LoadBootHeader command")
# boot_header is always 176 bytes long
if len(boot_header) != 176:
raise bl_errors.InvalidResponseError()
command = (self.cmd_id['load_boot_header']
+ b'\x00\xB0\x00' + boot_header)
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def load_segment_header(self, segment_header):
"""Send "load_segment_header" command to MCU.
Args:
segment_header (bytearray): Segment header data (?).
Returns:
bytearray: MCU's response.
Raise:
TODO
"""
logging.debug("LoadSegmentHeader command")
# Segment_header is always 16 bytes long
if len(segment_header) != 16 :
raise bl_errors.InvalidResponseError()
command = (self.cmd_id['load_segment_header']
+ b'\x00\x10\x00' + segment_header)
response = self.send_data_wait_for_response(command)
if self.is_response_ok(response):
return response[2:]
def load_segment_data(self, segment_data):
"""Send "load_segment_data" command to MCU.
Args:
segment_data (bytearray): Segment data (?).
Returns:
bytearray: MCU's response.
Raise:
TODO
"""
logging.debug("LoadSegmentData command")
dlen = len(segment_data)
if dlen > 4096:
raise ValueError("Segment_data can't be longer than 4096 bytes")
command = (self.cmd_id['load_segment_data']
+ b'\x00' + dlen.to_bytes(2, 'little') + segment_data)
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def check_image(self):
"""Send "check_image" command to MCU.
Returns:
bool: True if operation was succesful.
"""
logging.debug("CheckImage command")
command = self.cmd_id['check_image'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def memory_write(self, unknown):
"""Send "memory_write" command to MCU.
Args:
unknown (bytearray): IDK what is it (?).
"""
logging.debug("MemoryWrite command")
command = self.cmd_id['mem_write'] + b'\x00\x08\x00' + unknown
self.interface.send_data(command)
#response = self.send_data_wait_for_response(command)
#return self.is_response_ok(response)
def read_jedecid(self):
"""Read JEDEC ID
Returns:
bytearray: MCU's response.
"""
logging.debug("ReadJedecId command")
command = self.cmd_id['read_jedecid'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
if self.is_response_ok(response):
return response[2:]
def flash_erase(self, start_addr, end_addr):
"""Erase MCU's given flash memory region.
Args:
start_addr (int): Starting address for memory erasing.
end_addr (int): Ending address for memory erasing.
Returns:
bool: True if operation was succesful.
"""
logging.debug("FlashErase command")
data = (b'\x08\x00'
+ start_addr.to_bytes(4, 'little')
+ end_addr.to_bytes(4, 'little'))
checksum = self.calc_checksum(data)
command = (self.cmd_id['flash_erase']
+ checksum.to_bytes(1, 'little') + data)
response = self.send_data_wait_for_response(command, 1)
while True:
response = self.get_response()
if response[0:2] == b'PD':
print("Pending...")
if response[0:2] == b'OK':
return True
def flash_write(self, start_addr, payload):
"""Write MCU's flash memory region with given payload.
Args:
start_addr (int): Starting address for memory writing.
payload (bytearray): Data which has to be written to memory.
Returns:
bool: True if operation was succesful.
Raises:
"""
logging.debug("FlashWrite command")
dlen = len(payload)
if dlen > 8000:
raise ValueError("Payload can't be longer than 8000 bytes")
data = ((dlen + 4).to_bytes(2, 'little')
+ start_addr.to_bytes(4, 'little')
+ payload)
checksum = self.calc_checksum(data)
command = (self.cmd_id['flash_write']
+ checksum.to_bytes(1, 'little') + data)
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def flash_write_check(self):
"""Check if flash memory writing was succesful.
Returns:
bool: True if operation was succesful.
"""
logging.debug("FlashWriteCheck command")
command = self.cmd_id['flash_write_check'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def xip_read_start(self):
logging.debug("XipReadStart command")
command = self.cmd_id['xip_read_start'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def flash_xip_readsha(self, unknown):
logging.debug("XipReadSha command")
command = self.cmd_id['flash_xip_readsha'] + unknown
response = self.send_data_wait_for_response(command)
if self.is_response_ok(response):
return response[2:]
def xip_read_finish(self):
logging.debug("XipReadFinish command")
command = self.cmd_id['xip_read_finish'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
return self.is_response_ok(response)
def load_full_data(self, file):
logging.debug("LoadFullData Procedure")
data = file.read(4080)
while len(data) > 0:
self.load_segment_data(data)
data = file.read(4080)
def flash_write_all(self, start_addr, file):
logging.debug("FlashWriteFull Procedure")
data = file.read(2048)
while len(data) > 0:
self.flash_write(start_addr, data)
start_addr = start_addr + len(data)
data = file.read(2048)
def efuse_read_mac_addr(self):
logging.debug("EfuseReadMacAddr command")
command = self.cmd_id['efuse_read_mac_addr'] + b'\x00\x00\x00'
response = self.send_data_wait_for_response(command)
if self.is_response_ok(response):
return response[2:]