-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_firmware.py
More file actions
394 lines (338 loc) · 13.5 KB
/
upload_firmware.py
File metadata and controls
394 lines (338 loc) · 13.5 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
#!/usr/bin/env python3
"""
PRO-668 Firmware Recovery Tool
==============================
Recovers bricked Radio Shack PRO-668 scanners by transcoding and uploading
Whistler WS1080 firmware using the GRE serial protocol.
CREDITS & ATTRIBUTION:
----------------------
This tool is based on the work of Eric A. Cottrell (WB1HBU) and his GREFwTool:
- GitHub: https://github.com/philcovington/GREFwTool
- The XOR transcoding tables used in this tool are derived from GREFwTool
- The GRE serial protocol implementation is based on GREFwTool source code
- Firmware files (WS1080e_U3.8.bin) are from the GREFwTool repository
Additional information from RadioReference.com community:
- https://forums.radioreference.com/
- https://wiki.radioreference.com/index.php/Pro-668
TECHNICAL BACKGROUND:
--------------------
The PRO-668, WS1080, PSR-800, and Pro-18 are identical hardware with different
platform codes. This tool transcodes WS1080 (0xE6) firmware to PRO-668 (0xE4)
format using a 256-byte XOR transformation table.
LICENSE: GPL-3.0 (to comply with GREFwTool license)
"""
import serial
import sys
import time
# XOR table for transcoding WS1080 (0xE6) to PRO-668 (0xE4)
WS1080_TO_PRO668_TABLE = bytes([
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0xF7, 0xDF, 0xF7, 0xDF, 0xF7, 0xDF, 0xF7, 0xDF, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x0A, 0xF1, 0x0A, 0xF1, 0x0A, 0xF1, 0x0A, 0xF1, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x2E, 0x93, 0x2E, 0x93, 0x2E, 0x93, 0x2E, 0x93, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x0A, 0xF1, 0x0A, 0xF1, 0x0A, 0xF1, 0x0A, 0xF1, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x18, 0xC0, 0x99, 0xC8,
0x99, 0xC8, 0x99, 0xC8, 0x99, 0xC8, 0xF7, 0xDF, 0xF7, 0xDF, 0xF7, 0xDF, 0xF7, 0xDF, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x9B, 0x79, 0x9B, 0x79, 0x9B, 0x79, 0x9B, 0x79, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0xBF, 0x1B, 0xBF, 0x1B, 0xBF, 0x1B, 0xBF, 0x1B, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x89, 0x48, 0x08, 0x40,
0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x9B, 0x79, 0x9B, 0x79, 0x9B, 0x79, 0x9B, 0x79, 0x08, 0x40,
])
# Protocol constants (GRE scanner protocol)
STX = 0x02 # Start of packet
ETX = 0x03 # End of packet
EOT = 0x04 # End of transmission (update complete)
ENQ = 0x05 # Enquiry (ready for next packet)
ACK = 0x06 # Acknowledgement
DLE = 0x10 # Data link escape (update start)
NAK = 0x15 # Negative acknowledgement
CAN = 0x18 # Cancel (update error)
def transcode_firmware(data, xor_table):
"""Transcode firmware data using XOR table"""
result = bytearray(data)
table_size = len(xor_table)
for i in range(len(result)):
result[i] ^= xor_table[i % table_size]
return bytes(result)
def make_packet(data):
"""Create a GRE protocol packet: STX + data + ETX + checksum"""
checksum = ETX
for b in data:
checksum = (checksum + b) & 0xFF
return bytes([STX]) + data + bytes([ETX, checksum])
def load_and_prepare_firmware(filepath, no_transcode=False):
"""Load WS1080 firmware and prepare for PRO-668"""
print(f"Loading firmware: {filepath}")
with open(filepath, 'rb') as f:
data = f.read()
# Parse header
platform = data[0]
size = (data[1] << 16) | (data[2] << 8) | data[3]
image_data = bytearray(data[4:])
print(f" Original platform: 0x{platform:02X}")
print(f" Image size: {size} bytes")
if no_transcode:
print(" NO TRANSCODE MODE - sending firmware as-is")
return platform, size, bytes(image_data)
if platform == 0xE6:
print(" Transcoding WS1080 -> PRO-668...")
transcoded = transcode_firmware(image_data, WS1080_TO_PRO668_TABLE)
new_platform = 0xE4 # PRO-668
elif platform == 0xE4:
print(" Firmware is already PRO-668 format")
transcoded = bytes(image_data)
new_platform = 0xE4
else:
print(f" WARNING: Unknown platform 0x{platform:02X}, using as-is")
transcoded = bytes(image_data)
new_platform = platform
return new_platform, size, transcoded
def get_first_packet(platform, size):
"""Create the firmware header packet"""
# Format: platform byte + 6 ASCII hex chars for size
size_hex = f"{size:06X}"
packet_data = bytes([platform]) + size_hex.encode('ascii')
return packet_data
def get_data_packets(image_data):
"""Generator for 50-byte data packets as hex strings"""
offset = 0
while offset < len(image_data):
chunk = image_data[offset:offset + 50]
hex_data = chunk.hex().upper().encode('ascii')
offset += 50
yield hex_data, offset
def wait_for_ready(port, timeout=30):
"""Wait for bootloader ready signal (C characters)"""
print("Waiting for bootloader ready signal...")
start = time.time()
c_count = 0
while time.time() - start < timeout:
if port.in_waiting:
data = port.read(port.in_waiting)
for b in data:
if b == ord('C'):
c_count += 1
if c_count >= 3:
print(f"Bootloader ready! (received {c_count} 'C' characters)")
return True
time.sleep(0.1)
print("Timeout waiting for bootloader")
return False
def send_version_query(port):
"""Send version query to bootloader"""
print("Querying bootloader version...")
# Version command is 'V' without null byte in bootloader mode
packet = make_packet(b'V')
port.write(packet)
port.flush()
time.sleep(0.5)
response = b''
while port.in_waiting:
response += port.read(port.in_waiting)
time.sleep(0.1)
if response:
print(f" Version response: {response.hex()} = {response}")
# Send ACK after receiving version response
port.write(bytes([ACK]))
port.flush()
time.sleep(0.2)
return response
def send_packet(port, packet_data):
"""Send a framed packet"""
packet = make_packet(packet_data)
port.write(packet)
port.flush()
return packet
def wait_for_response(port, timeout=10):
"""Wait for and return a response byte"""
start = time.time()
while time.time() - start < timeout:
if port.in_waiting:
return port.read(1)[0]
time.sleep(0.01)
return None
def upload_firmware(port, platform, size, image_data):
"""Upload firmware to scanner using GRE protocol"""
# Wait for bootloader
if not wait_for_ready(port):
return False
# Query version first (bootloader expects this)
send_version_query(port)
time.sleep(0.5)
# Clear any pending data
port.reset_input_buffer()
# Send header packet
print("Sending firmware header...")
header_data = get_first_packet(platform, size)
print(f" Header: platform=0x{platform:02X}, size={size} (0x{size:06X})")
packet = send_packet(port, header_data)
print(f" Sent header packet: {packet.hex()}")
# Wait for DLE (update start) followed by ENQ (ready for data)
print("Waiting for bootloader to start update...")
update_started = False
ready_for_data = False
timeout = time.time() + 30
while time.time() < timeout:
response = wait_for_response(port, timeout=1)
if response is None:
continue
print(f" Response: 0x{response:02X} ({chr(response) if 32 <= response < 127 else '?'})")
if response == DLE:
print(" DLE - Update starting...")
update_started = True
elif response == ENQ:
print(" ENQ - Ready for data!")
ready_for_data = True
break
elif response == ACK:
print(" ACK - Header acknowledged!")
ready_for_data = True
break
elif response == NAK:
print(" NAK - Header rejected!")
return False
elif response == CAN:
print(" CAN - Update cancelled by bootloader!")
return False
elif response == ord('C'):
print(" Still receiving 'C' - bootloader waiting...")
continue
if not ready_for_data:
print("Timeout waiting for update to start")
return False
# Send data packets
print(f"Sending firmware data ({size} bytes)...")
total_packets = (size + 49) // 50
packet_num = 0
retry_count = 0
max_retries = 3
data_generator = get_data_packets(image_data)
for hex_data, offset in data_generator:
packet_num += 1
progress = min(offset, size) * 100 // size
while retry_count < max_retries:
print(f"\rPacket {packet_num}/{total_packets} ({progress}%)", end='', flush=True)
packet = send_packet(port, hex_data)
# Wait for response
response = wait_for_response(port, timeout=5)
if response is None:
print(f" - Timeout, retry {retry_count + 1}/{max_retries}")
retry_count += 1
continue
elif response == ACK:
# Packet accepted, move to next
retry_count = 0
break
elif response == NAK:
print(f" - NAK, retry {retry_count + 1}/{max_retries}")
retry_count += 1
continue
elif response == ENQ:
# Ready for next packet (treat as ACK)
retry_count = 0
break
elif response == CAN:
print(f"\n CAN received - Update cancelled!")
return False
elif response == EOT:
print(f"\n EOT received - Update complete!")
return True
else:
print(f" - Unknown response 0x{response:02X}")
retry_count += 1
continue
if retry_count >= max_retries:
print(f"\nFailed at packet {packet_num} after {max_retries} retries")
return False
# Wait for final EOT
print("\n\nWaiting for completion signal...")
response = wait_for_response(port, timeout=10)
if response == EOT:
print("EOT received - Firmware upload complete!")
return True
elif response == ACK:
print("ACK received - Firmware upload complete!")
return True
elif response:
print(f"Final response: 0x{response:02X}")
return True
print("Upload finished (no final response)")
return True
def main():
if len(sys.argv) < 3:
print("PRO-668 Firmware Uploader")
print("=" * 40)
print("\nUsage: python upload_firmware.py <COM_PORT> <firmware.bin> [--no-transcode]")
print("\nOptions:")
print(" --no-transcode Send firmware without platform transcoding")
print("\nExample:")
print(" python upload_firmware.py COM11 C:\\PRO-668\\firmware\\WS1080e_U4.5.bin")
print("\nMake sure your scanner is showing 'Waiting for USB' before running!")
sys.exit(1)
com_port = sys.argv[1]
firmware_file = sys.argv[2]
no_transcode = "--no-transcode" in sys.argv
print("PRO-668 Firmware Uploader")
print("=" * 40)
# Load and prepare firmware
try:
platform, size, image_data = load_and_prepare_firmware(firmware_file, no_transcode)
print(f" Prepared firmware: {len(image_data)} bytes for platform 0x{platform:02X}")
except Exception as e:
print(f"Error loading firmware: {e}")
sys.exit(1)
# Open serial port at 115200 (the correct baud rate for GRE scanners)
print(f"\nOpening {com_port} at 115200 baud...")
try:
port = serial.Serial(
port=com_port,
baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1,
rtscts=False,
dsrdtr=False,
xonxoff=False
)
port.reset_input_buffer()
port.reset_output_buffer()
print("Port opened successfully!")
except Exception as e:
print(f"Failed to open port: {e}")
sys.exit(1)
# Upload firmware
try:
success = upload_firmware(port, platform, size, image_data)
except KeyboardInterrupt:
print("\n\nUpload cancelled by user")
success = False
except Exception as e:
print(f"\nError during upload: {e}")
import traceback
traceback.print_exc()
success = False
finally:
port.close()
if success:
print("\n" + "=" * 40)
print("*** FIRMWARE UPLOAD SUCCESSFUL! ***")
print("=" * 40)
print("\nPlease power cycle your scanner now.")
print("The scanner should boot with the new firmware.")
else:
print("\n" + "=" * 40)
print("*** FIRMWARE UPLOAD FAILED ***")
print("=" * 40)
print("\nTry:")
print("1. Power cycle the scanner")
print("2. Make sure it shows 'Waiting for USB'")
print("3. Run this script again")
sys.exit(1)
if __name__ == '__main__':
main()