forked from hjudges/NORway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIway.py
566 lines (464 loc) · 15.9 KB
/
SPIway.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# *************************************************************************
# SPIway.py - Teensy++ 2.0 SPI flasher for PS4
#
# Copyright (C) 2017 [email protected]
#
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
# *************************************************************************
import serial, time, datetime, sys, struct
class TeensySerialError(Exception):
pass
class TeensySerial(object):
BUFSIZE = 32768
def __init__(self, port):
self.ser = serial.Serial(port, 9600, timeout=300, rtscts=False, dsrdtr=False, xonxoff=False, writeTimeout=120)
if self.ser is None:
raise TeensySerialError("could not open serial %s")%port
self.ser.flushInput()
self.ser.flushOutput()
self.obuf = ""
def write(self, s):
if isinstance(s,int):
s = chr(s)
elif isinstance(s,tuple) or isinstance(s,list):
s = ''.join([chr(c) for c in s])
self.obuf += s
while len(self.obuf) > self.BUFSIZE:
self.ser.write(self.obuf[:self.BUFSIZE])
self.obuf = self.obuf[self.BUFSIZE:]
def flush(self):
if len(self.obuf):
self.ser.write(self.obuf)
self.ser.flush()
self.obuf = ""
def read(self, size):
self.flush()
data = self.ser.read(size)
return data
def readbyte(self):
return ord(self.read(1))
def close(self):
print
print "Closing serial device..."
if self.ser is None:
print "Device already closed."
else:
self.ser.close()
print "Done."
class SPIError(Exception):
pass
class SPIFlasher(TeensySerial):
VERSION_MAJOR = 0
VERSION_MINOR = 0
SPI_DISABLE_PULLUPS = 0
MF_ID = 0
DEVICE_ID = 0
SPI_SECTOR_SIZE = 0
SPI_TOTAL_SECTORS = 0
SPI_BLOCK_COUNT = 0
SPI_SECTORS_PER_BLOCK = 0
SPI_BLOCK_SIZE = 0
SPI_ADDRESS_LENGTH = 0
SPI_USE_3BYTE_CMDS = 0
# Teensy commands
CMD_PING1 = 0
CMD_PING2 = 1
CMD_BOOTLOADER = 2
CMD_IO_LOCK = 3
CMD_IO_RELEASE = 4
CMD_PULLUPS_DISABLE = 5
CMD_PULLUPS_ENABLE = 6
CMD_SPI_ID = 7
CMD_SPI_READBLOCK = 8
CMD_SPI_WRITESECTOR = 9
CMD_SPI_ERASEBLOCK = 10
CMD_SPI_ERASECHIP = 11
CMD_SPI_3BYTE_ADDRESS = 12
CMD_SPI_4BYTE_ADDRESS = 13
CMD_SPI_3BYTE_CMDS = 14
CMD_SPI_4BYTE_CMDS = 15
def __init__(self, port, ver_major, ver_minor):
if port:
TeensySerial.__init__(self, port)
self.SPI_DISABLE_PULLUPS = 0
self.VERSION_MAJOR = ver_major
self.VERSION_MINOR = ver_minor
def ping(self):
self.write(self.CMD_PING1)
self.write(self.CMD_PING2)
ver_major = self.readbyte()
ver_minor = self.readbyte()
freeram = (self.readbyte() << 8) | self.readbyte()
if (ver_major != self.VERSION_MAJOR) or (ver_minor != self.VERSION_MINOR):
print "Ping failed (expected v%d.%02d, got v%d.%02d)"%(self.VERSION_MAJOR, self.VERSION_MINOR, ver_major, ver_minor)
self.close()
sys.exit(1)
return freeram
def readid(self):
if (self.SPI_DISABLE_PULLUPS == 0):
self.write(self.CMD_PULLUPS_ENABLE)
else:
self.write(self.CMD_PULLUPS_DISABLE)
self.write(self.CMD_SPI_ID)
spi_info = self.read(2)
# print "Raw ID data: 0x%02x 0x%02x"%(ord(spi_info[0]), ord(spi_info[1]))
self.MF_ID = ord(spi_info[0])
self.DEVICE_ID = ord(spi_info[1])
def printstate(self):
print "SPI information"
print "---------------"
self.readid()
if self.MF_ID == 0xC2:
print "Chip manufacturer: Macronix (0x%02x)"%self.MF_ID
if self.DEVICE_ID == 0x18:
print "Chip type: MX25L25635F (0x%02x)"%self.DEVICE_ID
self.SPI_BLOCK_COUNT = 512
self.SPI_SECTORS_PER_BLOCK = 16
self.SPI_SECTOR_SIZE = 0x1000
self.SPI_TOTAL_SECTORS = self.SPI_SECTORS_PER_BLOCK * self.SPI_BLOCK_COUNT
self.SPI_BLOCK_SIZE = self.SPI_SECTORS_PER_BLOCK * self.SPI_SECTOR_SIZE
self.SPI_ADDRESS_LENGTH = 4
self.SPI_USE_3BYTE_CMDS = 0
elif self.DEVICE_ID == 0x10:
print "Chip type: MX25L1006E (0x%02x)"%self.DEVICE_ID
self.SPI_BLOCK_COUNT = 2
self.SPI_SECTORS_PER_BLOCK = 16
self.SPI_SECTOR_SIZE = 0x1000
self.SPI_TOTAL_SECTORS = self.SPI_SECTORS_PER_BLOCK * self.SPI_BLOCK_COUNT
self.SPI_BLOCK_SIZE = self.SPI_SECTORS_PER_BLOCK * self.SPI_SECTOR_SIZE
self.SPI_ADDRESS_LENGTH = 3
self.SPI_USE_3BYTE_CMDS = 0
else:
print "Chip type: unknown (0x%02x)"%self.DEVICE_ID
self.close()
sys.exit(1)
elif self.MF_ID == 0xEF:
print "Chip manufacturer: Winbond (0x%02x)"%self.MF_ID
if self.DEVICE_ID == 0x10:
print "Chip type: W25X10CL (0x%02x)"%self.DEVICE_ID
self.SPI_BLOCK_COUNT = 2
self.SPI_SECTORS_PER_BLOCK = 16
self.SPI_SECTOR_SIZE = 0x1000
self.SPI_TOTAL_SECTORS = self.SPI_SECTORS_PER_BLOCK * self.SPI_BLOCK_COUNT
self.SPI_BLOCK_SIZE = self.SPI_SECTORS_PER_BLOCK * self.SPI_SECTOR_SIZE
self.SPI_ADDRESS_LENGTH = 3
self.SPI_USE_3BYTE_CMDS = 0
elif self.DEVICE_ID == 0x13:
print "Chip type: W25Q80BV (0x%02x)"%self.DEVICE_ID
self.SPI_BLOCK_COUNT = 16
self.SPI_SECTORS_PER_BLOCK = 16
self.SPI_SECTOR_SIZE = 0x1000
self.SPI_TOTAL_SECTORS = self.SPI_SECTORS_PER_BLOCK * self.SPI_BLOCK_COUNT
self.SPI_BLOCK_SIZE = self.SPI_SECTORS_PER_BLOCK * self.SPI_SECTOR_SIZE
self.SPI_ADDRESS_LENGTH = 3
self.SPI_USE_3BYTE_CMDS = 0
elif self.DEVICE_ID == 0x18:
print "Chip type: W25Q256FV (0x%02x)"%self.DEVICE_ID
self.SPI_BLOCK_COUNT = 512
self.SPI_SECTORS_PER_BLOCK = 16
self.SPI_SECTOR_SIZE = 0x1000
self.SPI_TOTAL_SECTORS = self.SPI_SECTORS_PER_BLOCK * self.SPI_BLOCK_COUNT
self.SPI_BLOCK_SIZE = self.SPI_SECTORS_PER_BLOCK * self.SPI_SECTOR_SIZE
self.SPI_ADDRESS_LENGTH = 4
self.SPI_USE_3BYTE_CMDS = 1
else:
print "Chip type: unknown (0x%02x)"%self.DEVICE_ID
self.close()
sys.exit(1)
else:
print "Chip manufacturer: unknown (0x%02x)"%self.MF_ID
print "Chip type: unknown (0x%02x)"%self.DEVICE_ID
self.close()
sys.exit(1)
print
if (self.SPI_BLOCK_SIZE * self.SPI_BLOCK_COUNT / 1024) <= 8192:
print "Chip size: %d KB"%(self.SPI_BLOCK_SIZE * self.SPI_BLOCK_COUNT / 1024)
else:
print "Chip size: %d MB"%(self.SPI_BLOCK_SIZE * self.SPI_BLOCK_COUNT / 1024 / 1024)
print "Sector size: %d bytes"%(self.SPI_SECTOR_SIZE)
print "Block size: %d bytes"%(self.SPI_BLOCK_SIZE)
print "Sectors per block: %d"%(self.SPI_SECTORS_PER_BLOCK)
print "Number of blocks: %d"%(self.SPI_BLOCK_COUNT)
print "Number of sectors: %d"%(self.SPI_TOTAL_SECTORS)
def bootloader(self):
self.write(self.CMD_BOOTLOADER)
self.flush()
def read_result(self):
# read status byte
res = self.readbyte()
# 'K' = okay, 'T' = timeout error when writing, 'R' = Teensy receive buffer timeout, 'V' = Verification error
error_msg = ""
if (res != 75): #'K'
if (res == 84): #'T'
error_msg = "RY/BY timeout error while writing!"
elif (res == 82): #'R'
self.close()
raise SPIError("Teensy receive buffer timeout! Disconnect and reconnect Teensy!")
elif (res == 86): #'V'
error_msg = "Verification error!"
elif (res == 80): #'P'
error_msg = "Device is write-protected!"
else:
self.close()
raise SPIError("Received unknown error! (Got 0x%02x)"%res)
print error_msg
return 0
return 1
def erase_chip(self):
self.write(self.CMD_SPI_ERASECHIP)
if self.read_result() == 0:
print "Error erasing chip!"
return 0
return 1
def erase_block(self, block):
if self.SPI_ADDRESS_LENGTH == 3:
self.write(self.CMD_SPI_3BYTE_ADDRESS)
else:
self.write(self.CMD_SPI_4BYTE_ADDRESS)
if self.SPI_USE_3BYTE_CMDS == 0:
self.write(self.CMD_SPI_4BYTE_CMDS)
else:
self.write(self.CMD_SPI_3BYTE_CMDS)
self.write(self.CMD_SPI_ERASEBLOCK)
# set address (msb first)
address = block * self.SPI_BLOCK_SIZE
self.write((address >> 24) & 0xFF)
self.write((address >> 16) & 0xFF)
self.write((address >> 8) & 0xFF)
self.write(address & 0xFF)
if self.read_result() == 0:
print "Block %d - error erasing block"%(block)
return 0
return 1
def readblock(self, block):
if self.SPI_ADDRESS_LENGTH == 3:
self.write(self.CMD_SPI_3BYTE_ADDRESS)
else:
self.write(self.CMD_SPI_4BYTE_ADDRESS)
if self.SPI_USE_3BYTE_CMDS == 0:
self.write(self.CMD_SPI_4BYTE_CMDS)
else:
self.write(self.CMD_SPI_3BYTE_CMDS)
self.write(self.CMD_SPI_READBLOCK)
# set address (msb first)
address = block * self.SPI_BLOCK_SIZE
self.write((address >> 24) & 0xFF)
self.write((address >> 16) & 0xFF)
self.write((address >> 8) & 0xFF)
self.write(address & 0xFF)
if self.read_result() == 0:
return "error"
data = self.read(self.SPI_BLOCK_SIZE)
return data
def dump(self, filename, block_offset, nblocks):
fo = open(filename,"wb")
if nblocks == 0:
nblocks = self.SPI_BLOCK_COUNT
if nblocks > self.SPI_BLOCK_COUNT:
nblocks = self.SPI_BLOCK_COUNT
for block in range(block_offset, (block_offset+nblocks), 1):
data = self.readblock(block)
fo.write(data)
print "\r%d KB / %d KB"%((block-block_offset+1)*self.SPI_BLOCK_SIZE/1024, nblocks*self.SPI_BLOCK_SIZE/1024),
sys.stdout.flush()
return
def writesector(self, data, sector):
if len(data) != self.SPI_SECTOR_SIZE:
print "Incorrent data size %d"%(len(data))
if self.SPI_ADDRESS_LENGTH == 3:
self.write(self.CMD_SPI_3BYTE_ADDRESS)
else:
self.write(self.CMD_SPI_4BYTE_ADDRESS)
if self.SPI_USE_3BYTE_CMDS == 0:
self.write(self.CMD_SPI_4BYTE_CMDS)
else:
self.write(self.CMD_SPI_3BYTE_CMDS)
self.write(self.CMD_SPI_WRITESECTOR)
# set address (msb first)
address = sector * self.SPI_SECTOR_SIZE
self.write((address >> 24) & 0xFF)
self.write((address >> 16) & 0xFF)
self.write((address >> 8) & 0xFF)
self.write(address & 0xFF)
self.write(data)
if self.read_result() == 0:
return 0
return 1
def program_block(self, data, pgblock, verify):
datasize = len(data)
if datasize != self.SPI_BLOCK_SIZE:
print "Incorrect length %d != %d!"%(datasize, self.SPI_BLOCK_SIZE)
return -1
sectornr = 0
while sectornr < self.SPI_SECTORS_PER_BLOCK:
real_sectornr = (pgblock * self.SPI_SECTORS_PER_BLOCK) + sectornr
if sectornr == 0:
self.erase_block(pgblock)
self.writesector(data[sectornr*self.SPI_SECTOR_SIZE:(sectornr+1)*self.SPI_SECTOR_SIZE], real_sectornr)
sectornr += 1
# verification
if verify == 1:
if data != self.readblock(pgblock):
print
print "Error! Block verification failed (block=%d)."%(pgblock)
return -1
return 0
def program(self, data, verify, block_offset, nblocks):
datasize = len(data)
if nblocks == 0:
nblocks = self.SPI_BLOCK_COUNT - block_offset
# validate that the data is a multiplication of self.SPI_BLOCK_SIZE
if datasize % self.SPI_BLOCK_SIZE:
print "Error: expecting file size to be a multiplication of block size: %d"%(self.SPI_BLOCK_SIZE)
return -1
# validate that the the user didn't want to read from incorrect place in the file
if block_offset + nblocks > datasize/self.SPI_BLOCK_SIZE:
print "Error: file is %d bytes long and last block is at %d!"%(datasize, (block_offset + nblocks + 1) * self.SPI_BLOCK_SIZE)
return -1
# validate that the the user didn't want to write to incorrect place on the chip
if block_offset + nblocks > self.SPI_BLOCK_COUNT:
print "Error: chip has %d blocks. Writing outside the chip's capacity!"%(self.SPI_BLOCK_COUNT, block_offset + nblocks + 1)
return -1
block = 0
print "Writing %d blocks to device (starting at offset %d)..."%(nblocks, block_offset)
while block < nblocks:
pgblock = block+block_offset
self.program_block(data[pgblock*self.SPI_BLOCK_SIZE:(pgblock+1)*self.SPI_BLOCK_SIZE], pgblock, verify)
print "\r%d KB / %d KB"%(((block+1)*self.SPI_BLOCK_SIZE)/1024, (nblocks*self.SPI_BLOCK_SIZE)/1024),
sys.stdout.flush()
block += 1
print
if __name__ == "__main__":
VERSION_MAJOR = 0
VERSION_MINOR = 40
print "SPIway v%d.%02d - Teensy++ 2.0 SPI Flasher for PS4"%(VERSION_MAJOR, VERSION_MINOR)
print "Copyright (C) 2017 [email protected]"
print
if len(sys.argv) == 1:
print "Usage:"
print "SPIway.py Serial-Port Command"
print
print " Serial-Port Name of serial port to open (eg. COM1, COM2, /dev/ttyACM0, etc)"
print " Commands:"
print " * info"
print " Displays chip information"
print " * dump Filename [Offset] [Length]"
print " Dumps to Filename at [Offset] and [Length] "
print " * vwrite/write Filename [Offset] [Length]"
print " Flashes (v=verify) Filename at [Offset] and [Length]"
# print " * vdiffwrite/diffwrite Filename Diff-file"
# print " Flashes (v=verify) Filename using a Diff-file"
print " * erasechip"
print " Erases the entire chip"
print " * bootloader"
print " Enters Teensy's bootloader mode (for Teensy reprogramming)"
print
print " Notes: 1) All offsets and lengths are in decimal (number of blocks)"
# print " 2) The Diff-file is a file which lists all the changed"
# print " offsets of a dump file. This will increase flashing"
# print " time dramatically."
print
print "Examples:"
print " SPIway.py COM1 info"
print " SPIway.py COM1 dump d:\myflash.bin"
print " SPIway.py COM1 dump d:\myflash.bin 10 20"
print " SPIway.py COM1 write d:\myflash.bin"
print " SPIway.py COM3 write d:\myflash.bin 0 20"
print " SPIway.py COM3 vwrite d:\myflash.bin"
print " SPIway.py COM3 vwrite d:\myflash.bin 10 20"
# print " SPIway.py COM4 diffwrite d:\myflash.bin d:\myflash_diff.txt"
# print " SPIway.py COM3 vdiffwrite d:\myflash.bin d:\myflash_diff.txt"
print " SPIway.py COM3 erasechip"
print " SPIway.py COM1 bootloader"
sys.exit(0)
n = SPIFlasher(sys.argv[1], VERSION_MAJOR, VERSION_MINOR)
print "Pinging Teensy..."
freeram = n.ping()
print "Available memory: %d bytes"%(freeram)
print
tStart = time.time()
if len(sys.argv) in (4,5,6) and sys.argv[2] == "dump":
n.printstate()
print
print "Dumping...",
sys.stdout.flush()
print
block_offset=0
nblocks=0
if len(sys.argv) == 5:
block_offset=int(sys.argv[4])
elif len(sys.argv) == 6:
block_offset=int(sys.argv[4])
nblocks=int(sys.argv[5])
n.dump(sys.argv[3], block_offset, nblocks)
print
print "Done. [%s]"%(datetime.timedelta(seconds=time.time() - tStart))
if len(sys.argv) == 3 and sys.argv[2] == "info":
n.printstate()
# print
elif len(sys.argv) in (4,5,6) and (sys.argv[2] == "write" or sys.argv[2] == "vwrite"):
n.printstate()
print
print "Writing...",
sys.stdout.flush()
print
data = open(sys.argv[3],"rb").read()
block_offset=0
nblocks=0
verify=0
if (sys.argv[2] == "vwrite"):
verify=1
if len(sys.argv) == 5:
block_offset=int(sys.argv[4])
elif len(sys.argv) == 6:
block_offset=int(sys.argv[4])
nblocks=int(sys.argv[5])
n.program(data, verify, block_offset, nblocks)
print
print "Done. [%s]"%(datetime.timedelta(seconds=time.time() - tStart))
# elif len(sys.argv) == 5 and (sys.argv[2] == "diffwrite" or sys.argv[2] == "vdiffwrite"):
# n.printstate()
# print
# print "Writing using diff file ..."
# sys.stdout.flush()
# print
#
# data = open(sys.argv[3],"rb").read()
# diff_data = open(sys.argv[4],"rb").readlines()
#
# block_offset=0
# nblocks=0
# verify=0
# nlines=len(diff_data)
# cur_line=0
#
# if (sys.argv[2] == "vdiffwrite"):
# verify=1
#
# for line in diff_data:
# addr=int(line[2:], 16)
# if addr % n.SPI_BLOCK_SIZE:
# print "Error: incorrect address for block addr=%x. addresses must be on a per-block boundary"%(addr)
# sys.exit(0)
#
# block_offset=addr/n.SPI_BLOCK_SIZE
# print "Programming offset %x block %x (%d/%d)"%(addr, block_offset, cur_line+1, nlines)
# n.program(data, verify, block_offset, 1)
# cur_line += 1
#
# print
# print "Done. [%s]"%(datetime.timedelta(seconds=time.time() - tStart))
elif len(sys.argv) == 3 and sys.argv[2] == "erasechip":
n.printstate()
print
print "Erasing chip..."
n.erase_chip()
print
print "Done. [%s]"%(datetime.timedelta(seconds=time.time() - tStart))
elif len(sys.argv) == 3 and sys.argv[2] == "bootloader":
print
print "Entering Teensy's bootloader mode... Goodbye!"
n.bootloader()
sys.exit(0)
n.ping()