debugged some additions to the generation fuzzer
[goodfet] / client / GoodFETAVR.py
1 #!/usr/bin/env python
2 # GoodFET SPI and SPIFlash Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, time, string, cStringIO, struct, glob, os;
9
10 from GoodFET import GoodFET;
11
12 class GoodFETAVR(GoodFET):
13     AVRAPP=0x32;
14     APP=AVRAPP;
15     AVRVendors={0x1E: "Atmel",
16                 0x00: "Locked",
17                 };
18     
19     #List imported from http://avr.fenceline.de/device_data.html
20     AVRDevices={
21         0x9003: "ATtiny10",
22         0x9004: "ATtiny11",
23         0x9005: "ATtiny12",
24         0x9007: "ATtiny13",
25         0x9006: "ATtiny15",
26         0x9106: "ATtiny22",
27         0x910A: "ATtiny2313",
28         0x9108: "ATtiny25",
29         0x9109: "ATtiny26",
30         0x9107: "ATtiny28",
31         0x9206: "ATtiny45",
32         0x930B: "ATtiny85",
33         0x9304: "AT90C8534",
34         0x9001: "AT90S1200",
35         0x9101: "AT90S2313",
36         0x9102: "AT90S2323",
37         0x9105: "AT90S2333",
38         0x9103: "AT90S2343",
39         0x9201: "AT90S4414",
40         0x9203: "AT90S4433",
41         0x9202: "AT90S4434",
42         0x9301: "AT90S8515",
43         0x9303: "AT90S8535",
44         0x9381: "AT90PWM2",
45         0x9381: "AT90PWM3",
46         0x9781: "AT90CAN128",
47         0x9205: "ATmega48",
48         0x9306: "ATmega8515",
49         0x9308: "ATmega8535",
50         0x9307: "ATmega8",
51         0x930A: "ATmega88",
52         0x9403: "ATmega16",
53         0x9401: "ATmega161",
54         0x9404: "ATmega162",
55         0x9402: "ATmega163",
56         0x9407: "ATmega165",
57         0x9406: "ATmega168",
58         0x9405: "ATmega169",
59         0x9502: "ATmega32",
60         0x958a: "ATmega32U2", #TODO add the other U series.
61         0x9501: "ATmega323",
62         0x9503: "ATmega325",
63         0x9504: "ATmega3250",
64         0x9503: "ATmega329",
65         0x9504: "ATmega3290",
66         0x9507: "ATmega406",
67         0x9602: "ATmega64",
68         0x9607: "ATmega640",
69         0x9603: "ATmega645",
70         0x9604: "ATmega6450",
71         0x9603: "ATmega649",
72         0x9604: "ATmega6490",
73         0x0101: "ATmega103",
74         0x9701: "ATmega103",
75         0x9702: "ATmega128",
76         0x9703: "ATmega1280",
77         0x9704: "ATmega1281",
78         0x9801: "ATmega2560",
79         0x9802: "ATmega2561",
80         0x9002: "ATtiny19",
81         0x9302: "ATmega85",
82         0x9305: "ATmega83",
83         0x9601: "ATmega603",
84         
85         #These are missing from the Fenceline DB.
86         0x960a: "ATmega644P",
87         };
88     
89     def setup(self):
90         """Move the FET into the AVR application."""
91         self.writecmd(self.AVRAPP,0x10,0,self.data); #SPI/SETUP
92     
93     def trans(self,data):
94         """Exchange data by AVR.
95         Input should probably be 4 bytes."""
96         self.data=data;
97         self.writecmd(self.AVRAPP,0x00,len(data),data);
98         return self.data;
99
100     def start(self):
101         """Start the connection."""
102         self.writecmd(self.AVRAPP,0x20,0,None);
103     def forcestart(self):
104         """Forcibly start a connection."""
105         
106         for i in range(0x880,0xfff):
107             #self.glitchVoltages(0x880, i);
108             self.start();
109             bits=self.lockbits();
110             print "At %04x, Lockbits: %02x" % (i,bits);
111             if(bits==0xFF): return;
112     def erase(self):
113         """Erase the target chip."""
114         self.writecmd(self.AVRAPP,0xF0,0,None);
115     def lockbits(self):
116         """Read the target's lockbits."""
117         self.writecmd(self.AVRAPP,0x82,0,None);
118         return ord(self.data[0]);
119     def setlockbits(self,bits=0x00):
120         """Read the target's lockbits."""
121         self.writecmd(self.AVRAPP,0x92,1,[bits]);
122         return self.lockbits();
123     def lock(self):
124         self.setlockbits(0xFC);
125     def eeprompeek(self, adr):
126         """Read a byte of the target's EEPROM."""
127         self.writecmd(self.AVRAPP,0x81 ,2,
128                       [ (adr&0xFF), (adr>>8)]
129                       );#little-endian address
130         return ord(self.data[0]);
131     def flashpeek(self, adr):
132         """Read a byte of the target's Flash memory."""
133         self.writecmd(self.AVRAPP,0x02 ,2,
134                       [ (adr&0xFF), (adr>>8)]
135                       );#little-endian address
136         return ord(self.data[0]);
137     def flashpeekblock(self, adr):
138         """Read a byte of the target's Flash memory."""
139         self.writecmd(self.AVRAPP,0x02 ,4,
140                       [ (adr&0xFF), (adr>>8) &0xFF, 0x80, 0x00]
141                       );
142         return self.data;
143     
144     def eeprompoke(self, adr, val):
145         """Write a byte of the target's EEPROM."""
146         self.writecmd(self.AVRAPP,0x91 ,3,
147                       [ (adr&0xFF), (adr>>8), val]
148                       );#little-endian address
149         return ord(self.data[0]);
150     
151     def identstr(self):
152         """Return an identifying string."""
153         self.writecmd(self.AVRAPP,0x83,0, None);
154         vendor=self.AVRVendors.get(ord(self.data[0]));
155         deviceid=(ord(self.data[1])<<8)+ord(self.data[2]);
156         device=self.AVRDevices.get(deviceid);
157         
158         #Return hex if device is unknown.
159         #They are similar enough that it needn't be known.
160         if device==None:
161             device=("0x%04x" % deviceid);
162         
163         return "%s %s" % (vendor,device);