forked from ohjeongwook/dumpflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashDevice.py
710 lines (598 loc) · 19.4 KB
/
FlashDevice.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
from array import array as Array
import re
import time
import sys
import pprint
import traceback
from pyftdi.ftdi import *
from DumpUBoot import *
from ECC import *
class NandIO:
ADR_CE=0x10
ADR_WP=0x20
ADR_CL=0x40
ADR_AL=0x80
NAND_CMD_READID=0x90
NAND_CMD_READ0=0
NAND_CMD_READ1=1
NAND_CMD_RNDOUT=5
NAND_CMD_PAGEPROG=0x10
NAND_CMD_READOOB=0x50
NAND_CMD_ERASE1=0x60
NAND_CMD_STATUS=0x70
NAND_CMD_STATUS_MULTI=0x71
NAND_CMD_SEQIN=0x80
NAND_CMD_RNDIN=0x85
NAND_CMD_READID=0x90
NAND_CMD_ERASE2=0xd0
NAND_CMD_PARAM=0xec
NAND_CMD_RESET=0xff
NAND_CMD_LOCK=0x2a
NAND_CMD_UNLOCK1=0x23
NAND_CMD_UNLOCK2=0x24
NAND_CMD_READSTART=0x30
NAND_CMD_RNDOUTSTART=0xE0
NAND_CMD_CACHEDPROG=0x15
NAND_CMD_ONFI=0xEC
NAND_CI_CHIPNR_MSK=0x03
NAND_CI_CELLTYPE_MSK=0x0C
NAND_CI_CELLTYPE_SHIFT=2
NAND_STATUS_FAIL=(1<<0) # HIGH - FAIL, LOW - PASS
NAND_STATUS_IDLE=(1<<5) # HIGH - IDLE, LOW - ACTIVE
NAND_STATUS_READY=(1<<6) # HIGH - READY, LOW - BUSY
NAND_STATUS_NOT_PROTECTED=(1<<7) # HIGH - NOT, LOW - PROTECTED
LP_Options=1
DeviceDescriptions=[
["NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0, 3],
["NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0, 3],
["NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0, 3],
["NAND 1MiB 3,3V 8-bit", 0xe8, 256, 1, 0x1000, 0, 3],
["NAND 1MiB 3,3V 8-bit", 0xec, 256, 1, 0x1000, 0, 3],
["NAND 2MiB 3,3V 8-bit", 0xea, 256, 2, 0x1000, 0, 3],
["NAND 4MiB 3,3V 8-bit", 0xe3, 512, 4, 0x2000, 0, 3],
["NAND 4MiB 3,3V 8-bit", 0xe5, 512, 4, 0x2000, 0, 3],
["NAND 8MiB 3,3V 8-bit", 0xd6, 512, 8, 0x2000, 0, 3],
["NAND 8MiB 1,8V 8-bit", 0x39, 512, 8, 0x2000, 0, 3],
["NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0, 3],
["NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0, 3],
["NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0, 3],
["NAND 32MiB 1,8V 8-bit", 0x35, 512, 32, 0x4000, 0, 3],
["NAND 32MiB 3,3V 8-bit", 0x75, 512, 32, 0x4000, 0, 3],
["NAND 64MiB 1,8V 8-bit", 0x36, 512, 64, 0x4000, 0, 4],
["NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000, 0, 4],
["NAND 128MiB 1,8V 8-bit", 0x78, 512, 128, 0x4000, 0, 3],
["NAND 128MiB 1,8V 8-bit", 0x39, 512, 128, 0x4000, 0, 3],
["NAND 128MiB 3,3V 8-bit", 0x79, 512, 128, 0x4000, 0, 4],
["NAND 256MiB 3,3V 8-bit", 0x71, 512, 256, 0x4000, 0, 4],
# 512 Megabit
["NAND 64MiB 1,8V 8-bit", 0xA2, 0, 64, 0, LP_Options, 4],
["NAND 64MiB 1,8V 8-bit", 0xA0, 0, 64, 0, LP_Options, 4],
["NAND 64MiB 3,3V 8-bit", 0xF2, 0, 64, 0, LP_Options, 4],
["NAND 64MiB 3,3V 8-bit", 0xD0, 0, 64, 0, LP_Options, 4],
["NAND 64MiB 3,3V 8-bit", 0xF0, 0, 64, 0, LP_Options, 4],
# 1 Gigabit
["NAND 128MiB 1,8V 8-bit", 0xA1, 0, 128, 0, LP_Options, 4],
["NAND 128MiB 3,3V 8-bit", 0xF1, 0, 128, 0, LP_Options, 4],
["NAND 128MiB 3,3V 8-bit", 0xD1, 0, 128, 0, LP_Options, 4],
# 2 Gigabit
["NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_Options, 5],
["NAND 256MiB 3,3V 8-bit", 0xDA, 0, 256, 0, LP_Options, 5],
# 4 Gigabit
["NAND 512MiB 1,8V 8-bit", 0xAC, 0, 512, 0, LP_Options, 5],
["NAND 512MiB 3,3V 8-bit", 0xDC, 0, 512, 0, LP_Options, 5],
# 8 Gigabit
["NAND 1GiB 1,8V 8-bit", 0xA3, 0, 1024, 0, LP_Options, 5],
["NAND 1GiB 3,3V 8-bit", 0xD3, 0, 1024, 0, LP_Options, 5],
# 16 Gigabit
["NAND 2GiB 1,8V 8-bit", 0xA5, 0, 2048, 0, LP_Options, 5],
["NAND 2GiB 3,3V 8-bit", 0xD5, 0, 2048, 0, LP_Options, 5],
# 32 Gigabit
["NAND 4GiB 1,8V 8-bit", 0xA7, 0, 4096, 0, LP_Options, 5],
["NAND 4GiB 3,3V 8-bit", 0xD7, 0, 4096, 0, LP_Options, 5],
# 64 Gigabit
["NAND 8GiB 1,8V 8-bit", 0xAE, 0, 8192, 0, LP_Options, 5],
["NAND 8GiB 3,3V 8-bit", 0xDE, 0, 8192, 0, LP_Options, 5],
# 128 Gigabit
["NAND 16GiB 1,8V 8-bit", 0x1A, 0, 16384, 0, LP_Options, 5],
["NAND 16GiB 3,3V 8-bit", 0x3A, 0, 16384, 0, LP_Options, 5],
# 256 Gigabit
["NAND 32GiB 1,8V 8-bit", 0x1C, 0, 32768, 0, LP_Options, 6],
["NAND 32GiB 3,3V 8-bit", 0x3C, 0, 32768, 0, LP_Options, 6],
# 512 Gigabit
["NAND 64GiB 1,8V 8-bit", 0x1E, 0, 65536, 0, LP_Options, 6],
["NAND 64GiB 3,3V 8-bit", 0x3E, 0, 65536, 0, LP_Options, 6],
#["NAND 4MiB 3,3V 8-bit", 0xd5, 512, 4, 0x2000, 0, 3]
]
Debug=0
Slow=False
PageSize=0
OOBSize=0
PageCount=0
BlockCount=0
PagePerBlock=0
BitsPerCell=0
WriteProtect=True
CheckBadBlock=True
RemoveOOB=False
UseSequentialMode=False
def __init__(self, do_slow=False):
self.Slow=do_slow
self.UseAnsi=False
self.Ftdi = Ftdi()
try:
self.Ftdi.open(0x0403,0x6010,interface=1)
except:
traceback.print_exc(file=sys.stdout)
return
self.Ftdi.set_bitmode(0, self.Ftdi.BITMODE_MCU)
if (self.Slow==True):
# Clock FTDI chip at 12MHz instead of 60MHz
self.Ftdi.write_data(Array('B', [Ftdi.ENABLE_CLK_DIV5]))
else:
self.Ftdi.write_data(Array('B', [Ftdi.DISABLE_CLK_DIV5]))
self.Ftdi.set_latency_timer(1)
self.Ftdi.purge_buffers()
self.Ftdi.write_data(Array('B', [Ftdi.SET_BITS_HIGH,0x0,0x1]))
self.WaitReady()
self.GetID()
def IsInitialized(self):
return self.Identified
def SetUseAnsi(self,use_ansi):
self.UseAnsi=use_ansi
def WaitReady(self):
while 1:
self.Ftdi.write_data(Array('B', [Ftdi.GET_BITS_HIGH]))
data = self.Ftdi.read_data_bytes(1)
if data[0]&2==0x2:
return
else:
if self.Debug>0:
print 'Not Ready', data
return
def nandRead(self,cl,al,count):
cmds=[]
cmd_type=0
if cl==1:
cmd_type|=self.ADR_CL
if al==1:
cmd_type|=self.ADR_AL
cmds+=[Ftdi.READ_EXTENDED, cmd_type, 0]
for i in range(1,count,1):
cmds+=[Ftdi.READ_SHORT, 0]
cmds.append(Ftdi.SEND_IMMEDIATE)
self.Ftdi.write_data(Array('B', cmds))
if (self.getSlow()):
data = self.Ftdi.read_data_bytes(count*2)
data = data[0:-1:2]
else:
data = self.Ftdi.read_data_bytes(count)
return data.tolist()
def nandWrite(self,cl,al,data):
cmds=[]
cmd_type=0
if cl==1:
cmd_type|=self.ADR_CL
if al==1:
cmd_type|=self.ADR_AL
if not self.WriteProtect:
cmd_type|=self.ADR_WP
cmds+=[Ftdi.WRITE_EXTENDED, cmd_type, 0, ord(data[0])]
for i in range(1,len(data),1):
#if i == 256:
# cmds+=[Ftdi.WRITE_SHORT, 0, ord(data[i])]
cmds+=[Ftdi.WRITE_SHORT, 0, ord(data[i])]
self.Ftdi.write_data(Array('B', cmds))
def sendCmd(self,cmd):
self.nandWrite(1,0,chr(cmd))
def sendAddr(self,addr,count):
data=''
for i in range(0,count,1):
data += chr(addr & 0xff)
addr=addr>>8
self.nandWrite(0,1,data)
def Status(self):
self.sendCmd(0x70)
status=self.readFlashData(1)[0]
return status
def readFlashData(self,count):
return self.nandRead(0,0,count)
def writeData(self,data):
return self.nandWrite(0,0,data)
def getSlow(self):
return self.Slow
def GetID(self):
self.sendCmd(self.NAND_CMD_READID)
self.sendAddr(0,1)
id=self.readFlashData(8)
self.Identified=False
self.Name=''
self.ID=0
self.PageSize=0
self.ChipSizeMB=0
self.EraseSize=0
self.Options=0
self.AddrCycles=0
device_found=False
for device_description in self.DeviceDescriptions:
if device_description[1]==id[1]:
(self.Name,self.ID,self.PageSize,self.ChipSizeMB,self.EraseSize,self.Options,self.AddrCycles)=device_description
self.Identified=True
break
if not self.Identified:
return
#Check ONFI
self.sendCmd(self.NAND_CMD_READID)
self.sendAddr(0x20,1)
onfitmp=self.readFlashData(4)
onfi=False
if onfitmp[0]==0x4F and onfitmp[1]==0x4E and onfitmp[2]==0x46 and onfitmp[3]==0x49:
onfi=True
if onfi:
self.sendCmd(self.NAND_CMD_ONFI)
self.sendAddr(0,1)
self.WaitReady()
onfi_data=self.readFlashData(0x100)
if onfi_data[0]==0x4F and onfi_data[1]==0x4E and onfi_data[2]==0x46 and onfi_data[3]==0x49:
onfi=True
else:
onfi=False
if id[0]==0x98:
self.Manufacturer="Toshiba"
elif id[0]==0xec:
self.Manufacturer="Samsung"
elif id[0]==0x04:
self.Manufacturer="Fujitsu"
elif id[0]==0x8f:
self.Manufacturer="National Semiconductors"
elif id[0]==0x07:
self.Manufacturer="Renesas"
elif id[0]==0x20:
self.Manufacturer="ST Micro"
elif id[0]==0xad:
self.Manufacturer="Hynix"
elif id[0]==0x2c:
self.Manufacturer="Micron"
elif id[0]==0x01:
self.Manufacturer="AMD"
elif id[0]==0xc2:
self.Manufacturer="Macronix"
else:
self.Manufacturer='Unknown'
idstr=''
for idbyte in id:
idstr += "%X" % idbyte
if (idstr[0:4] == idstr[-4:]):
idstr = idstr[:-4]
if (idstr[0:2] == idstr[-2:]):
idstr = idstr[:-2]
self.IDString = idstr
self.IDLength = (len(idstr) / 2)
self.BitsPerCell = self.GetBitsPerCell(id[2])
if self.PageSize==0:
extid=id[3]
if ((self.IDLength == 6) and (self.Manufacturer == "Samsung") and (self.BitsPerCell > 1)):
self.Pagesize = 2048 << (extid & 0x03)
extid >>= 2
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 1:
flash.oobsize = 128
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 2:
flash.oobsize = 218
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 3:
flash.oobsize = 400
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 4:
flash.oobsize = 436
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 5:
flash.oobsize = 512
if (((extid >> 2) & 0x04) | (extid & 0x03)) == 6:
flash.oobsize = 640
else:
flash.oobsize = 1024
extid >>= 2
self.EraseSize = (128 * 1024) << (((extid >> 1) & 0x04) | (extid & 0x03))
elif ((self.IDLength == 6) and (self.Manufacturer == "Hynix") and (self.BitsPerCell > 1)):
self.PageSize = 2048 << (extid & 0x03)
extid >>= 2
if ((((extid >> 2) & 0x04) | (extid & 0x03)) == 0):
self.OOBSize = 128
elif ((((extid >> 2) & 0x04) | (extid & 0x03)) == 1):
self.OOBSize = 224
elif ((((extid >> 2) & 0x04) | (extid & 0x03)) == 2):
self.OOBSize = 448
elif ((((extid >> 2) & 0x04) | (extid & 0x03)) == 3):
self.OOBSize = 64
elif ((((extid >> 2) & 0x04) | (extid & 0x03)) == 4):
self.OOBSize = 32
elif ((((extid >> 2) & 0x04) | (extid & 0x03)) == 5):
self.OOBSize = 16
else:
self.OOBSize = 640
tmp = ((extid >> 1) & 0x04) | (extid & 0x03)
if (tmp < 0x03): self.EraseSize = (128 * 1024) << tmp
elif (tmp == 0x03): self.EraseSize = 768 * 1024
else: self.EraseSize = (64 * 1024) << tmp
else:
self.PageSize = 1024 << (extid & 0x03)
extid >>= 2
self.OOBSize = (8 << (extid & 0x01)) * (self.PageSize >> 9)
extid >>= 2
self.EraseSize = (64 * 1024) << (extid & 0x03)
if ((self.IDLength >= 6) and (self.Manufacturer == "Toshiba") and (self.BitsPerCell > 1) and ((id[5] & 0x7) == 0x6) and not (id[4] & 0x80)): Self.OOBSize = 32 * Self.PageSize >> 9
else:
self.OOBSize = self.PageSize / 32
if self.PageSize>0:
self.PageCount=(self.ChipSizeMB*1024*1024)/self.PageSize
self.RawPageSize=self.PageSize+self.OOBSize
self.BlockSize=self.EraseSize
self.BlockCount=(self.ChipSizeMB*1024*1024)/self.BlockSize
self.PagePerBlock=self.PageCount/self.BlockCount
self.RawBlockSize=self.PagePerBlock*(self.PageSize + self.OOBSize)
return True
def GetBitsPerCell(self, cellinfo):
bits = cellinfo & self.NAND_CI_CELLTYPE_MSK
bits >>= self.NAND_CI_CELLTYPE_SHIFT
return bits+1
def DumpInfo(self):
print 'Full ID:\t',self.IDString
print 'ID Length:\t',self.IDLength
print 'Name:\t\t',self.Name
print 'ID:\t\t0x%x' % self.ID
print 'Page size:\t0x%x' % self.PageSize
print 'OOB size:\t0x%x' % self.OOBSize
print 'Page count:\t0x%x' % self.PageCount
print 'Size:\t\t0x%x' % self.ChipSizeMB
print 'Erase size:\t0x%x' % self.EraseSize
print 'Block count:\t', self.BlockCount
print 'Options:\t',self.Options
print 'Address cycle:\t',self.AddrCycles
print 'Bits per Cell:\t',self.BitsPerCell
print 'Manufacturer:\t',self.Manufacturer
print ''
def CheckBadBlocks(self):
bad_blocks={}
end_page=self.PageCount
if self.PageCount%self.PagePerBlock>0.0:
self.BlockCount+=1
curblock=1
for block in range(0,self.BlockCount):
page+=self.PagePerBlock
curblock = curblock + 1
if self.UseAnsi:
sys.stdout.write('Checking bad blocks %d Block: %d/%d\n\033[A' % (curblock/self.BlockCount*100.0, curblock, self.BlockCount))
else:
sys.stdout.write('Checking bad blocks %d Block: %d/%d\n' % (curblock/self.BlockCount*100.0, curblock, self.BlockCount))
for pageoff in range(0,2,1):
oob=self.ReadOOB(page+pageoff)
if oob[5]!='\xff':
print 'Bad block found:', block
bad_blocks[page]=1
break
print 'Checked %d blocks and found %d bad blocks' % ( block+1, len(bad_blocks))
return bad_blocks
def ReadOOB(self,pageno):
bytes=[]
if self.Options&self.LP_Options:
self.sendCmd(self.NAND_CMD_READ0)
self.sendAddr((pageno<<16L),self.AddrCycles)
self.sendCmd(self.NAND_CMD_READSTART)
self.WaitReady()
bytes += self.readFlashData(self.OOBSize)
else:
self.sendCmd(self.NAND_CMD_READOOB)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
bytes+=self.readFlashData(self.OOBSize)
data=''
for ch in bytes:
data+=chr(ch)
return data
def ReadPage(self,pageno,remove_oob=False):
bytes=[]
if self.Options&self.LP_Options:
self.sendCmd(self.NAND_CMD_READ0)
self.sendAddr(pageno<<16,self.AddrCycles)
self.sendCmd(self.NAND_CMD_READSTART)
if self.PageSize>0x1000:
len=self.PageSize+self.OOBSize
while len>0:
read_len=0x1000
if len<0x1000:
read_len=len
bytes+=self.readFlashData(read_len)
len-=0x1000
else:
bytes=self.readFlashData(self.PageSize+self.OOBSize)
#TODO: Implement remove_oob
else:
self.sendCmd(self.NAND_CMD_READ0)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
bytes+=self.readFlashData(self.PageSize/2)
self.sendCmd(self.NAND_CMD_READ1)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
bytes+=self.readFlashData(self.PageSize/2)
if not remove_oob:
self.sendCmd(self.NAND_CMD_READOOB)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
bytes+=self.readFlashData(self.OOBSize)
data=''
for ch in bytes:
data+=chr(ch)
return data
def ReadSeq(self,pageno,remove_oob=False,raw_mode=False):
page=[]
self.sendCmd(self.NAND_CMD_READ0)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
bad_block=False
for i in range(0,self.PagePerBlock,1):
page_data = self.readFlashData(self.RawPageSize)
if i==0 or i==1:
if page_data[self.PageSize+5]!=0xff:
bad_block = True
if remove_oob:
page += page_data[0:self.PageSize]
else:
page += page_data
self.WaitReady()
self.Ftdi.write_data(Array('B', [Ftdi.SET_BITS_HIGH,0x1,0x1]))
self.Ftdi.write_data(Array('B', [Ftdi.SET_BITS_HIGH,0x0,0x1]))
data=''
if bad_block and not raw_mode:
print '\nSkipping bad block at %d' % (pageno/self.PagePerBlock)
else:
for ch in page:
data+=chr(ch)
return data
def EraseBlockByPage(self,pageno):
self.WriteProtect=False
self.sendCmd(self.NAND_CMD_ERASE1);
self.sendAddr(pageno, self.AddrCycles);
self.sendCmd(self.NAND_CMD_ERASE2);
self.WaitReady();
err=self.Status()
self.WriteProtect=True
return err
def WritePage(self,pageno,data):
err=0
self.WriteProtect=False
if self.Options&self.LP_Options:
self.sendCmd(self.NAND_CMD_SEQIN)
self.WaitReady()
self.sendAddr(pageno<<16,self.AddrCycles)
self.WaitReady()
self.writeData(data)
self.sendCmd(self.NAND_CMD_PAGEPROG)
self.WaitReady()
else:
while 1:
self.sendCmd(self.NAND_CMD_READ0)
self.sendCmd(self.NAND_CMD_SEQIN)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
self.writeData(data[0:256])
self.sendCmd(self.NAND_CMD_PAGEPROG)
err=self.Status()
if err&self.NAND_STATUS_FAIL:
print 'Failed to write 1st half of ', pageno, err
continue
break
while 1:
self.sendCmd(self.NAND_CMD_READ1)
self.sendCmd(self.NAND_CMD_SEQIN)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
self.writeData(data[self.PageSize/2:self.PageSize])
self.sendCmd(self.NAND_CMD_PAGEPROG)
err=self.Status()
if err&self.NAND_STATUS_FAIL:
print 'Failed to write 2nd half of ', pageno, err
continue
break
while 1:
self.sendCmd(self.NAND_CMD_READOOB)
self.sendCmd(self.NAND_CMD_SEQIN)
self.WaitReady()
self.sendAddr(pageno<<8,self.AddrCycles)
self.WaitReady()
self.writeData(data[self.PageSize:self.RawPageSize])
self.sendCmd(self.NAND_CMD_PAGEPROG)
err=self.Status()
if err&self.NAND_STATUS_FAIL:
print 'Failed to write OOB of ', pageno, err
continue
break
self.WriteProtect=True
return err
def writeBlock(self,block_data):
nand_tool.EraseBlockByPage(0) #need to fix
page=0
for i in range(0,len(data),self.RawPageSize):
nand_tool.WritePage(pageno,data[i:i+self.RawPageSize])
page+=1
def WritePages(self,filename,offset=0,start_page=-1,end_page=-1,add_oob=False,add_jffs2_eraser_marker=False,raw_mode=False):
fd=open(filename,'rb')
fd.seek(offset)
data=fd.read()
if start_page==-1:
start_page=0
if end_page==-1:
end_page=self.PageCount-1
end_block=end_page/self.PagePerBlock
if end_page%self.PagePerBlock>0:
end_block+=1
start = time.time()
ecc=ECC()
page=start_page
block=page/self.PagePerBlock
current_data_offset=0
length=0
while page<=end_page and current_data_offset<len(data) and block<self.BlockCount:
oob_postfix='\xFF' * 13
if page%self.PagePerBlock == 0:
if not raw_mode:
bad_block_found=False
for pageoff in range(0,2,1):
oob=self.ReadOOB(page+pageoff)
if oob[5]!='\xff':
bad_block_found=True
break
if bad_block_found:
print '\nSkipping bad block at ', block
page+=self.PagePerBlock
block+=1
continue
if add_jffs2_eraser_marker:
oob_postfix="\xFF\xFF\xFF\xFF\xFF\x85\x19\x03\x20\x08\x00\x00\x00"
self.EraseBlockByPage(page)
if add_oob:
orig_page_data=data[current_data_offset:current_data_offset+self.PageSize]
current_data_offset+=self.PageSize
length+=len(orig_page_data)
orig_page_data+=(self.PageSize-len(orig_page_data))*'\x00'
import copy
(ecc0, ecc1, ecc2) = ecc.CalcECC(orig_page_data)
oob=struct.pack('BBB',ecc0,ecc1,ecc2) + oob_postfix
page_data=orig_page_data+oob
else:
page_data=data[current_data_offset:current_data_offset+self.RawPageSize]
current_data_offset+=self.RawPageSize
length+=len(page_data)
if len(page_data)!=self.RawPageSize:
print 'Not enough source data'
break
current = time.time()
if end_page==start_page:
progress=100
else:
progress=(page-start_page) * 100 / (end_page-start_page)
lapsed_time=current-start
if lapsed_time>0:
if self.UseAnsi:
sys.stdout.write('Writing %d%% Page: %d/%d Block: %d/%d Speed: %d bytes/s\n\033[A' % (progress, page, end_page, block, end_block, length/lapsed_time))
else:
sys.stdout.write('Writing %d%% Page: %d/%d Block: %d/%d Speed: %d bytes/s\n' % (progress, page, end_page, block, end_block, length/lapsed_time))
self.WritePage(page,page_data)
if page%self.PagePerBlock == 0:
block=page/self.PagePerBlock
page+=1
fd.close()
print '\nWritten %x bytes / %x byte' % (length, len(data))
def Erase(self):
block=0
while block<self.BlockCount:
self.EraseBlockByPage(block * self.PagePerBlock)
block+=1
def EraseBlock(self,start_block, end_block):
print 'Erasing Block: 0x%x ~ 0x%x' % (start_block, end_block)
for block in range(start_block, end_block+1, 1):
print "Erasing block", block
self.EraseBlockByPage(block * self.PagePerBlock)