-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathflash_dump.py
More file actions
228 lines (191 loc) · 5.1 KB
/
flash_dump.py
File metadata and controls
228 lines (191 loc) · 5.1 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
#!/bin/env python2
import time, sys, serial, os, struct
CMD_READID = 'i'
CMD_READPAGE = 'p'
CMD_READSPARE = 's'
CMD_PING1 = '1'
CMD_PING2 = '2'
CMD_ERASE = 'e'
CMD_WRITEPAGE = 'w'
CMD_WRITESPARE = 'r'
CMD_WRITE_PAGE_SPARE = 'o'
CMD_RET_OK = 0x00
CMD_RET_ERROR = 0xFF
ser = serial.Serial('COM4', 115200)
print "ping FLASHER"
ser.write(CMD_PING1)
ser.flush()
if ser.read() != 'a':
print "Ping1 Failed"
exit()
ser.write(CMD_PING2)
ser.flush()
if ser.read() != 'b':
print "Ping2 Failed"
exit()
print "ping OK"
def getResult():
header = ser.read(12)
result = struct.unpack('<III', header)
#print "ret: %d sz: %d crc 0x%02x" % (result[0],result[1],result[2])
return {'ret': result[0], 'sz': result[1], 'crc': result[2]}
def readID():
ser.write(CMD_READID)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
nandID = ser.read(result['sz'])
print "NAND ID: ",
for a in nandID:
print "%02x" % ord(a),
print ""
else:
print "Error reading NAND ID"
def readspare(page, block, plane):
ser.write(CMD_READSPARE)
address = struct.pack('<HHH',page, block, plane)
ser.write(address)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
spare = ser.read(result['sz'])
return spare
else:
print "Error reading NAND page"
return None
def readpage(page, block, plane, oob = False):
ser.write(CMD_READPAGE)
address = struct.pack('<HHH',page, block, plane)
ser.write(address)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
data = ser.read(result['sz'])
if oob:
data += readspare(page,block,plane)
return data
else:
print "Error reading NAND page"
return None
def eraseblock(block, plane):
ser.write(CMD_ERASE)
address = struct.pack('<HH', block, plane)
ser.write(address)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
return True
else:
print "Erase Error: Fail"
return False
def programpageoob(page, block, plane, data):
ser.write(CMD_WRITE_PAGE_SPARE)
address = struct.pack('<HHH',page, block, plane)
ser.write(address)
ser.write(data)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
return True
else:
print "Write Error: Fail"
return False
def programpage(page, block, plane, data):
ser.write(CMD_WRITEPAGE)
address = struct.pack('<HHH',page, block, plane)
ser.write(address)
ser.write(data)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
return True
else:
print "Write Error: Fail"
return False
def programspare(page, block, plane, data):
ser.write(CMD_WRITESPARE)
address = struct.pack('<HHH',page, block, plane)
ser.write(address)
ser.write(data)
ser.flush()
result = getResult()
if result['ret'] == CMD_RET_OK:
return True
else:
print "Write Error: Fail"
return False
#######################################################################
readID()
if len(sys.argv) == 5 and sys.argv[1] == 'ERASEBLOCK':
plane = int(sys.argv[2],0)
blk = int(sys.argv[3],0)
count = int(sys.argv[4],0)
for block in range(blk,blk+count):
print "Erasing block: %04x plane: %04x" % (block, plane)
val = eraseblock(block, plane)
if val == False:
print "Erase Failed"
exit()
if len(sys.argv) == 6 and sys.argv[1] == 'WRITEBLOCK':
plane = int(sys.argv[2],0)
blk = int(sys.argv[3],0)
count = int(sys.argv[4],0)
infile = open(sys.argv[5],"rb")
infile.seek(0,os.SEEK_END)
filesize = infile.tell()
if filesize != (4096+224) * 256 * count:
print "File size not correct"
#exit()
infile.seek(0,os.SEEK_SET)
print "Write start at block: 0x%04x" % blk
for block in range(blk,blk+count):
for page in range(0, 0x100):
print "Writing page: %04x block: %04x plane: %04x" % (page, block, plane)
# data = infile.read(4096)
# val = programpage(page, block, plane, data)
# if val == False:
# print "Write Failed"
# exit()
# data = infile.read(224)
# val = programspare(page, block, plane, data)
# if val == False:
# print "Write Failed"
# exit()
data = infile.read(4096+224)
val = programpageoob(page, block, plane, data)
if val == False:
print "Write Failed"
exit()
infile.close()
if len(sys.argv) == 3 and sys.argv[1] == 'TEST':
count = int(sys.argv[2])
for currpos in range(0, count):
print "Reading page: %05x" % currpos
data = readpage(currpos,0,0)
data2 = readpage(currpos,0,0)
if data != data2:
print "FAILED"
break
if len(sys.argv) == 3 and sys.argv[1] == 'DUMPNAND':
outfile = open(sys.argv[2],"wb")
for plane in range(0, 0x2):
for block in range(0, 0x400):
for page in range(0, 0x100):
print "Reading page: %04x block: %04x plane: %04x" % (page, block, plane)
data = readpage(page, block, plane, True)
outfile.write(data)
outfile.flush()
outfile.close()
if len(sys.argv) == 6 and sys.argv[1] == 'DUMPBLOCK':
outfile = open(sys.argv[5],"wb")
plane = int(sys.argv[2],0)
blk = int(sys.argv[3],0)
count = int(sys.argv[4],0)
for block in range(blk, blk+count):
for page in range(0, 0x100):
print "Reading page: %04x block: %04x plane: %04x" % (page, block, plane)
data = readpage(page, block, plane, True)
outfile.write(data)
outfile.flush()
outfile.close()
ser.close()