Repository navigation
Expand file tree
/
Copy pathCS108FwUpgrade.py
More file actions
499 lines (434 loc) · 18.6 KB
/
Copy pathCS108FwUpgrade.py
File metadata and controls
499 lines (434 loc) · 18.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import time
import BTCommands
import SLCommands
import RFIDCommands
import USBSocket
import CRC
import HID
import utils
import Constants
# Consecutive bad/missing replies tolerated for one subpart before giving up
MAX_RETRY = 10
# Seconds to wait after a successful upload while the processor restarts
SILAB_RESTART_WAIT = 5
BT_RESTART_WAIT = 15
RFID_RESTART_WAIT = 6
# RFID NV memory update status codes (index = status value)
NV_STATUS = [
"NVMEMUPD_STAT_UPD_SUCCESS",
"NVMEMUPD_STAT_RESERVED_01",
"NVMEMUPD_STAT_WR_FAIL",
"NVMEMUPD_STAT_UNK_CMD",
"NVMEMUPD_STAT_CMD_IGN",
"NVMEMUPD_STAT_BNDS",
"NVMEMUPD_STAT_MAGIC",
"NVMEMUPD_STAT_PKTLEN",
"NVMEMUPD_STAT_EXIT_ERR",
"NVMEMUPD_STAT_EXIT_SUCCESS",
"NVMEMUPD_STAT_EXIT_NOWRITES",
"NVMEMUPD_STAT_GEN_RXPKT_ERR",
"NVMEMUPD_STAT_RESERVED_02",
"NVMEMUPD_STAT_INT_MEM_BNDS",
"NVMEMUPD_STAT_ENTRY_OK",
"NVMEMUPD_STAT_RXPKT_MAX",
"NVMEMUPD_STAT_RX_TO",
"NVMEMUPD_STAT_CRC_ERR",
]
def PrintProgress(percent):
print("\rCompleted: {0:5.1f}%".format(percent), end="\n" if percent >= 100 else "")
def _ReceiveReply(handle, code, timeout):
# Wait up to timeout (ms) for the CS108 packet whose payload starts with the
# 2-byte command code, skipping unrelated packets that can arrive in between,
# such as notifications (type 0xD9, e.g. event 0xA101 "error code 0").
buffer = bytearray()
deadline = time.monotonic() + timeout / 1000
while time.monotonic() < deadline:
remaining = max(1, (deadline - time.monotonic()) * 1000)
status, data = USBSocket.ReceiveData(handle, 128, remaining)
if not status:
return False, b""
buffer += data
while len(buffer) >= 8:
if buffer[0] != Constants.PREFIX:
del buffer[0] # resync on the next prefix byte
continue
total = buffer[2] + 8
if len(buffer) < total:
break
frame = bytes(buffer[:total])
del buffer[:total]
if total >= 10 and frame[8] == code[0] and frame[9] == code[1]:
return True, frame
return False, b""
def GetBTVersion(handle, log=print):
command = BTCommands.GetVersion()
if not USBSocket.TransmitData(handle, command):
log("Device failed to transmit data.")
return ""
if HID.IsOpen(handle):
status, buffer = _ReceiveReply(handle, (0xC0, 0x00), 2000)
if status:
if (buffer[0] == Constants.PREFIX) and (len(buffer) >= 13) and (buffer[8] == 0xC0) and (buffer[9] == 0x00):
crc = (buffer[6] << 8) | buffer[7]
if (crc != 0) and (not CRC.CheckCRC(buffer, 0, 13, crc)):
log("Wrong CRC received")
return ""
btVersion = "{}.{}.{}".format(buffer[10],buffer[11],buffer[12])
return btVersion
log("Cannot get bluetooth firmware version")
return ""
else:
log("Device is not connected.")
return ""
def GetSLVersion(handle, log=print):
command = RFIDCommands.PowerOn(True)
if not USBSocket.TransmitData(handle, command):
log("Device failed to transmit data.")
return ""
if HID.IsOpen(handle):
_ReceiveReply(handle, (0x80, 0x00), 2000)
else:
log("Device is not connected.")
return ""
command = SLCommands.GetVersion()
if not USBSocket.TransmitData(handle, command):
log("Device failed to transmit data.")
return ""
if HID.IsOpen(handle):
status, buffer = _ReceiveReply(handle, (0xB0, 0x00), 1000)
if status:
if (buffer[0] == Constants.PREFIX) and (len(buffer) >= 13) and (buffer[8] == 0xB0) and (buffer[9] == 0x00):
slVersion = "{}.{}.{}".format(buffer[10],buffer[11],buffer[12])
return slVersion
log("Cannot get Silicon Labs (network processor) firmware version")
return ""
else:
log("Device is not connected.")
return ""
def UpdateBTImage(handle, stream, log=print, progress=PrintProgress, tick=None):
if len(stream) != Constants.BT_IMAGE_SIZE:
log("Incorrect image file size.")
return False
subpart = 1
subpartBuffer = bytes(64)
command = bytes()
retry = 0
while True:
if retry == 0:
if len(stream) >=64:
subpartBuffer = stream[0:64]
stream = stream[64:]
else:
subpartBuffer = stream[0:]
stream = stream[0:]
elif retry > MAX_RETRY:
log("No valid reply from the reader for block {} after {} retries.".format(subpart, MAX_RETRY))
return False
if len(subpartBuffer) > 0:
command = BTCommands.SendImageData(subpartBuffer, subpart)
else:
log("Not enough image data.")
return False
if not USBSocket.TransmitData(handle, command):
log("Device failed to transmit data (block {} of {}).".format(subpart, Constants.BT_IMAGE_TOTAL_SUBPART))
return False
if HID.IsOpen(handle):
readBuffer = bytes(128)
status, readBuffer = _ReceiveReply(handle, (0xC0, 0x01), 5000)
if status:
if (readBuffer[0] == Constants.PREFIX) and (len(readBuffer) >= 11) and (readBuffer[8] == 0xC0) and (readBuffer[9] == 0x01):
if readBuffer[10] == 1:
log("The reader reported a Bluetooth upgrade failure at block {}.".format(subpart))
return False
elif readBuffer[10] == 2:
progress(100.0)
log("Upload complete.")
utils.WaitForSeconds(BT_RESTART_WAIT, tick)
log("")
return True
else:
progress((subpart * 100) / Constants.BT_IMAGE_TOTAL_SUBPART)
subpart+=1
retry = 0
else:
retry+=1
log("Unexpected reply to block {} of {} ({}); resending.".format(subpart, Constants.BT_IMAGE_TOTAL_SUBPART, readBuffer[:40].hex(" ")))
else:
retry+=1
log("No reply to block {} of {}; resending.".format(subpart, Constants.BT_IMAGE_TOTAL_SUBPART))
else:
log("Device is not connected (block {} of {}).".format(subpart, Constants.BT_IMAGE_TOTAL_SUBPART))
return False
def UpdateSilabImage(handle, stream, log=print, progress=PrintProgress, tick=None):
if len(stream) != Constants.SILAB_IMAGE_SIZE:
log("Incorrect image file size.")
return False
subpart = 1
subpartBuffer = bytes(114)
command = bytes()
retry = 0
while True:
if retry == 0:
if len(stream) >= 114:
subpartBuffer = stream[0:114]
stream = stream[114:]
else:
subpartBuffer = stream[0:]
stream = stream[0:]
elif retry > MAX_RETRY:
log("No valid reply from the reader for block {} after {} retries.".format(subpart, MAX_RETRY))
return False
if len(subpartBuffer) > 0:
command = SLCommands.SendImageData(subpartBuffer, subpart)
else:
log("Not enough image data.")
return False
lastBlock = (subpart == Constants.SILAB_IMAGE_TOTAL_SUBPART)
if not USBSocket.TransmitData(handle, command):
if lastBlock and retry > 0:
return _SilabRestartedAfterLastBlock(log, progress, tick)
log("Device failed to transmit data (block {} of {}).".format(subpart, Constants.SILAB_IMAGE_TOTAL_SUBPART))
return False
if HID.IsOpen(handle):
readBuffer = bytes(128)
status, readBuffer = _ReceiveReply(handle, (0xB0, 0x01), 1000)
if status:
if (readBuffer[0] == Constants.PREFIX) and (len(readBuffer) >= 11) and (readBuffer[8] == 0xB0) and (readBuffer[9] == 0x01):
if readBuffer[10] == 1:
log("The reader reported a network processor upgrade failure at block {}.".format(subpart))
return False
elif readBuffer[10] == 2:
progress(100.0)
log("Upload complete.")
utils.WaitForSeconds(SILAB_RESTART_WAIT, tick)
log("")
return True
else:
progress((subpart * 100) / Constants.SILAB_IMAGE_TOTAL_SUBPART)
subpart+=1
retry = 0
else:
retry+=1
log("Unexpected reply to block {} of {} ({}); resending.".format(subpart, Constants.SILAB_IMAGE_TOTAL_SUBPART, readBuffer[:40].hex(" ")))
else:
retry+=1
log("No reply to block {} of {}; resending.".format(subpart, Constants.SILAB_IMAGE_TOTAL_SUBPART))
elif lastBlock:
return _SilabRestartedAfterLastBlock(log, progress, tick)
else:
log("Device is not connected (block {} of {}).".format(subpart, Constants.SILAB_IMAGE_TOTAL_SUBPART))
return False
def _SilabRestartedAfterLastBlock(log, progress, tick):
# The network processor is also the USB bridge: once it has the last block it
# restarts, sometimes before its "complete" reply reaches the host. Treat it as
# done only if the reader comes back and the network processor reports a version.
log("The reader restarted before confirming the last block; checking it after the restart...")
progress(100.0)
utils.WaitForSeconds(SILAB_RESTART_WAIT, tick)
log("")
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
if HID.GetNumHidDevices() > 0:
handle = HID.Open(0)
if handle is not None:
slVersion = GetSLVersion(handle, log=lambda message: None)
HID.Close(handle)
if slVersion:
log("Upload complete. Network processor firmware version: {}".format(slVersion))
return True
time.sleep(1)
log("The reader did not come back after the network processor upgrade.")
return False
class RfidPacket:
def __init__(self, header, data):
self.ver = header[0]
self.flags = header[1]
self.type = header[2] | (header[3] << 8)
self.length = header[4] | (header[5] << 8)
self.reserve = header[6] | (header[7] << 8)
self.data = data
class RfidReceiver:
# Reassembles CS108 packets from USB reads, unwraps the RFID byte stream
# carried in 0x8100 events and splits it into RFID packets
def __init__(self, handle):
self.handle = handle
self.usbBuffer = bytearray()
self.rfidBuffer = bytearray()
self.packets = []
self.sendFailures = 0
self.powerOnStatus = None
def Pump(self):
if not HID.IsOpen(self.handle):
return False
status, data = HID.ReceiveData(self.handle, HID.GetMaxReportRequest(self.handle) * HID.SIZE_MAX_READ)
if not status:
return False
self.Feed(data)
return True
def Feed(self, data):
self.usbBuffer += data
while len(self.usbBuffer) >= 8:
if self.usbBuffer[0] != Constants.PREFIX:
# resync on the next prefix byte
del self.usbBuffer[0]
continue
total = self.usbBuffer[2] + 8
if len(self.usbBuffer) < total:
break
self._HandleFrame(bytes(self.usbBuffer[:total]))
del self.usbBuffer[:total]
while len(self.rfidBuffer) >= 8:
ver = self.rfidBuffer[0]
flags = self.rfidBuffer[1]
# Command/NV-update packets carry len*4 data bytes; register and
# control responses are header only (their len field holds data)
if ver in (0x01, 0x02, 0x03) or (ver == 0x0D and flags == 0xF0):
dataLen = (self.rfidBuffer[4] | (self.rfidBuffer[5] << 8)) * 4
else:
dataLen = 0
if len(self.rfidBuffer) < 8 + dataLen:
break
self.packets.append(RfidPacket(bytes(self.rfidBuffer[:8]), bytes(self.rfidBuffer[8:8 + dataLen])))
del self.rfidBuffer[:8 + dataLen]
def _HandleFrame(self, frame):
if frame[3] != Constants.TYPE_RFID or len(frame) < 10:
return
event = (frame[8] << 8) | frame[9]
if event == 0x8000 and len(frame) > 10:
self.powerOnStatus = frame[10]
elif event == 0x8002 and len(frame) > 10 and frame[10] != 0x00:
self.sendFailures += 1
elif event == 0x8100:
self.rfidBuffer += frame[10:]
def Take(self, match, timeout):
# Return the first received RFID packet matching match(packet), or None on timeout
deadline = time.monotonic() + timeout / 1000
while True:
for i, packet in enumerate(self.packets):
if match(packet):
del self.packets[i]
return packet
if time.monotonic() > deadline:
return None
if not self.Pump():
return None
time.sleep(Constants.TIMER_READ * 0.001)
def Drain(self, duration):
# Read and discard everything that arrives within duration (ms)
deadline = time.monotonic() + duration / 1000
while time.monotonic() < deadline:
self.Pump()
time.sleep(Constants.TIMER_READ * 0.001)
self.packets = []
def _SendRfid(handle, packets):
for packet in packets:
if not USBSocket.TransmitData(handle, packet):
return False
return True
def _RfidPowerOn(handle, receiver):
if not USBSocket.TransmitData(handle, RFIDCommands.PowerOn(True)):
return False
deadline = time.monotonic() + 2
while receiver.powerOnStatus is None and time.monotonic() < deadline:
if not receiver.Pump():
return False
time.sleep(Constants.TIMER_READ * 0.001)
receiver.Drain(500)
return True
def _IsVersionReply(packet):
return packet.ver in (0x70, 0x00) and packet.flags == 0x00 and packet.type == 0x0000
def _IsNVStatus(packet):
return packet.ver == 0x0D and packet.flags == 0xF0 and packet.type == 0x0000 and len(packet.data) >= 8
def _NVStatusOf(packet):
# NV status data: command being answered (LE32), then status (LE32)
return int.from_bytes(packet.data[4:8], "little")
def _ReadRfidVersion(handle, receiver):
if not _SendRfid(handle, RFIDCommands.GetVersion()):
return ""
packet = receiver.Take(_IsVersionReply, 2000)
if packet is None:
return ""
data = (packet.reserve << 16) | packet.length
return "{}.{}.{}".format(data >> 24, (data >> 12) & 0x7FF, data & 0x7FF)
def _ResetRfid(handle, oemValue):
# Select bootloader (0x00000020) or main image (0x80000020), then reset
if not _SendRfid(handle, RFIDCommands.WriteOEM(0x3C4, oemValue)):
return False
time.sleep(0.01)
if not _SendRfid(handle, RFIDCommands.ReadOEM(0x3C4)):
return False
time.sleep(0.2)
if not _SendRfid(handle, RFIDCommands.Reset()):
return False
time.sleep(2)
return True
def GetRFIDVersion(handle, log=print):
receiver = RfidReceiver(handle)
if not _RfidPowerOn(handle, receiver):
log("Cannot power on the RFID module.")
return ""
rfidVersion = _ReadRfidVersion(handle, receiver)
if not rfidVersion:
log("Cannot get RFID firmware version")
return rfidVersion
def UpdateRFIDImage(handle, stream, log=print, progress=PrintProgress, tick=None):
if len(stream) != Constants.RFID_IMAGE_SIZE:
log("Incorrect image file size.")
return False
receiver = RfidReceiver(handle)
if not _RfidPowerOn(handle, receiver):
log("Cannot power on the RFID module.")
return False
log("Switching the RFID module to its bootloader...")
if not _ResetRfid(handle, 0x00000020):
log("Device failed to transmit data.")
return False
receiver.Drain(500)
if not _SendRfid(handle, RFIDCommands.StartNVMemUpdate()):
log("Device failed to transmit data.")
return False
if receiver.Take(lambda p: p.type == 0x300B, 5000) is None or \
receiver.Take(lambda p: p.type in (0x0001, 0x8001), 5000) is None:
log("The RFID module did not enter firmware update mode.")
return False
for block in range(Constants.RFID_IMAGE_TOTAL_SUBPART):
if not _SendRfid(handle, RFIDCommands.SendImageData(stream[block * 32:(block + 1) * 32], block)):
log("Device failed to transmit data (block {} of {}).".format(block + 1, Constants.RFID_IMAGE_TOTAL_SUBPART))
return False
reply = receiver.Take(_IsNVStatus, 2000)
# The module announces NV update mode with an ENTRY_OK status, which can
# arrive after the first block is sent; it is not a reply to a block
while reply is not None and _NVStatusOf(reply) == NV_STATUS.index("NVMEMUPD_STAT_ENTRY_OK"):
reply = receiver.Take(_IsNVStatus, 2000)
if reply is None:
log("No reply from the RFID module for block {}.".format(block + 1))
_SendRfid(handle, RFIDCommands.UpdateComplete())
return False
status = _NVStatusOf(reply)
if status != 0:
name = NV_STATUS[status] if status < len(NV_STATUS) else "0x{:08X}".format(status)
log("RFID update failed at block {}: {}".format(block + 1, name))
_SendRfid(handle, RFIDCommands.UpdateComplete())
return False
if receiver.sendFailures:
log("The reader rejected data for block {}.".format(block + 1))
return False
progress(((block + 1) * 100) / Constants.RFID_IMAGE_TOTAL_SUBPART)
log("Upload complete.")
if not _SendRfid(handle, RFIDCommands.UpdateComplete()):
log("Device failed to transmit data.")
return False
utils.WaitForSeconds(RFID_RESTART_WAIT, tick)
log("")
log("Switching the RFID module back to the main firmware...")
receiver.Drain(200)
if not _ResetRfid(handle, 0x80000020):
log("Device failed to transmit data.")
return False
receiver.Drain(500)
rfidVersion = _ReadRfidVersion(handle, receiver)
if not rfidVersion:
log("The RFID module did not report a version after the upgrade.")
return False
log("RFID firmware version after upgrade: {}".format(rfidVersion))
return True