55a7b12e3a06a44742763dc8d3ab99174281293c
[goodfet] / client / goodfet.spiflash
1 #!/usr/bin/env python
2
3 #GoodFET SPI Flash Client
4 #by Travis Goodspeed
5
6 #N.B.,
7 #Might be Winbond W25x80-specific.
8
9 import sys;
10 import binascii;
11 import array;
12
13 from GoodFETSPI import GoodFETSPIFlash;
14 from intelhex import IntelHex;
15
16 if(len(sys.argv)==1):
17     print "Usage: %s verb [objects]\n" % sys.argv[0];
18     print "%s info" % sys.argv[0];
19     print "%s dump $foo.rom [0x$start 0x$stop]" % sys.argv[0];
20     print "%s erase" % sys.argv[0];
21     print "%s flash $foo.rom [0x$start 0x$stop]" % sys.argv[0];
22     print "%s verify $foo.rom [0x$start 0x$stop]" % sys.argv[0];
23     print "%s peek 0x$start [0x$stop]" % sys.argv[0];
24     print "%s poke 0x$adr 0x$val" % sys.argv[0];
25     sys.exit();
26
27 #Initialize FET and set baud rate
28 client=GoodFETSPIFlash();
29 client.serInit()
30
31
32 client.SPIsetup();
33
34 #Dummy read.
35 #Might read as all ones if chip has a startup delay.
36 client.SPIjedec();
37
38 if(sys.argv[1]=="test"):
39     result="";
40     dropped=0;
41     for i in range(40):
42         data=client.SPIjedec();
43         if ord(data[1])==0xFF:
44             result+="-";
45             dropped=dropped+1;
46         else:
47             result+="+";
48     print "Connection Test: (- is bad)\n%s" % result;
49     print "%i misreads" % dropped;
50     if(dropped==40):
51         print "No successful reads.  Is the chip wired correctly?";
52     elif(dropped>0):
53         print "Some success, some failures.  Is a wire loose?";
54     else:
55         print "All reads succeeded.  Wiring is probably good.";
56     print "Erasing.";
57     client.SPIchiperase();
58     print "Testing erase.";
59     data=client.SPIpeekblock(0);
60     for i in data:
61         if ord(i)!=0xFF:
62             print "ERROR not properly erased!";
63     data=range(0,10);
64     client.SPIpokebytes(0,data);
65     print "Testing flash write.";
66     for i in data:
67         if(client.SPIpeek(i)!=i):
68             print "%06x not properly poked to %02x" % (i,i);
69     print "Test complete.";
70 if(sys.argv[1]=="info"):
71     data=client.SPIjedec();
72     print "Ident as %s\nManufacturer: %02x %s\nType: %02x\nCapacity: %02x (%i bytes)" % (
73         client.SPIjedecstr(),
74         ord(data[1]),client.SPIjedecmanstr(),
75         ord(data[2]),
76         ord(data[3]),
77         client.JEDECsize);
78
79 if(sys.argv[1]=="dump"):
80     f = sys.argv[2];
81     start=0x0000;
82     stop=client.JEDECsize;
83     if(len(sys.argv)>3):
84         start=int(sys.argv[3],16);
85     if(len(sys.argv)>4):
86         stop=int(sys.argv[4],16);
87     
88     print "Dumping code from %06x to %06x as %s." % (start,stop,f);
89     file = open(f, mode='wb')
90     
91     i=start;
92     while i<=stop:
93         data=client.SPIpeekblock(i);
94         #if(i%0x1000==0):
95         print "Dumped %06x."%i;
96         for j in data:
97             if i<stop: file.write(j);
98             i+=1;
99     file.close()
100
101 if(sys.argv[1]=="verify"):
102     f = sys.argv[2];
103     start=0x0000;
104     stop=client.JEDECsize;
105     if(len(sys.argv)>3):
106         start=int(sys.argv[3],16);
107     if(len(sys.argv)>4):
108         stop=int(sys.argv[4],16);
109     
110     print "Verifying code from %06x to %06x as %s." % (start,stop,f);
111     file = open(f, mode='rb')
112     
113     i=start;
114     bits=0;
115     while i<=stop:
116         data=client.SPIpeekblock(i,255);
117         print "Verified %06x." % i;
118         for j in data:
119             if i<stop:
120                 bits|=ord(file.read(1))^ord(j);
121             i+=1;
122         if bits!=0:
123             print "Bits don't match."
124
125     file.close()
126
127 if(sys.argv[1]=="flash"):
128     f = sys.argv[2];
129     start=0x0000;
130     stop=client.JEDECsize;
131     
132     if(len(sys.argv)>3):
133         start=int(sys.argv[3],16);
134     if(len(sys.argv)>4):
135         stop=int(sys.argv[4],16);
136     
137     print "Flashing code from %06x to %06x with %s." % (start,stop,f);
138     file = open(f, mode='rb')
139
140     i=start;
141     chars=list(file.read());
142     
143     #N.B., chunksize must be an even fraction of 0x100.
144     chunksize=0x100;
145     
146     #client.silent(1);
147     while i<=stop:
148         bytes=range(0,chunksize);
149         for j in range(0,chunksize):
150             bytes[j]=ord(chars[i+j]);
151         client.SPIpokebytes(i,bytes);
152         
153         i+=chunksize;
154         if(i%0x1000==0):
155             print "Flashed %06x."%i;
156     print "Done, ending silence.";
157     #client.silent(0);
158     file.close()
159
160
161 if(sys.argv[1]=="erase"):
162   client.SPIchiperase();
163
164 if(sys.argv[1]=="peek"):
165     start=0x0000;
166     if(len(sys.argv)>2):
167         start=int(sys.argv[2],16);
168     stop=start;
169     if(len(sys.argv)>3):
170         stop=int(sys.argv[3],16);
171     print "Peeking from %06x to %06x." % (start,stop);
172     while start<=stop:
173         print "%06x: %02x" % (start,client.SPIpeek(start));
174         start=start+1;
175
176 if(sys.argv[1]=="poke"):
177     start=0x0000;
178     val=0x00;
179     if(len(sys.argv)>2):
180         start=int(sys.argv[2],16);
181     if(len(sys.argv)>3):
182         val=int(sys.argv[3],16);
183     print "Poking %06x to become %02x." % (start,val);
184     
185     while client.SPIpeek(start)!=val:
186         client.SPIpokebyte(start,val);
187         print "Poked to %02x" % client.SPIpeek(start);
188