2 # GoodFET Client Library
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
6 # This code is being rewritten and refactored. You've been warned!
8 import sys, time, string, cStringIO, struct, glob, serial, os, random;
11 from GoodFET import *;
14 plot "< sqlite3 glitch.db 'select time,vcc,glitchcount from glitches where count=0;'" \
17 "< sqlite3 glitch.db 'select time,vcc,count from glitches where count>0;'" \
20 "< sqlite3 glitch.db 'select time,vcc,count from glitches where count>0 and lock>0;'" \
25 class GoodFETGlitch(GoodFET):
27 def __init__(self, *args, **kargs):
28 print "Initializing GoodFET Glitcher."
29 #Database connection w/ 30 second timeout.
30 self.db=sqlite3.connect("glitch.db",30000);
31 self.db.execute("create table if not exists glitches(time,vcc,gnd,trials,glitchcount,count,lock)");
33 def setup(self,arch="avr"):
34 self.client=getClient(arch);
37 import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils
39 print "gnuplot-py is missing. Can't graph."
41 g = Gnuplot.Gnuplot(debug=1);
44 g.title('Glitch Training Set');
45 g.xlabel('Time (16MHz)');
46 g.ylabel('VCC (DAC12)');
48 g('set datafile separator "|"');
58 import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils
60 # print "py-gnuplot or py-numpy is missing. Can't graph."
62 g = Gnuplot.Gnuplot(debug=1);
65 g.title('Glitch Training Set');
66 g.xlabel('Time (16MHz)');
67 g.ylabel('VCC (DAC12)');
69 g('set datafile separator "|"');
71 g('set output "timevcc.png"');
77 lock=0; #1 locks, 0 unlocked
79 vstop=1024; #Could be as high as 0xFFF
82 tstop=-1; #<0 defaults to full range
84 self.scan(lock,trials,vstart,vstop,tstart,tstop);
86 def scan(self,lock,trials=1,vstart=0,vstop=0xfff,tstart=0,tstop=-1):
91 tstop=client.glitchstarttime(); #Really long; only use for initial investigation.
92 print "-- Start takes %04i cycles." % tstop;
98 while(client.eeprompeek(0)!=self.secret):
99 print "-- Setting secret";
102 #Flash the secret to the first two bytes of CODE memory.
104 client.eeprompoke(0,self.secret);
105 client.eeprompoke(1,self.secret);
108 #Lock chip to unlock it later.
111 voltages=range(vstart,vstop,1);
112 times=range(tstart,tstop,1);
114 gnd=0; #TODO, glitch GND.
116 random.shuffle(voltages);
117 #random.shuffle(times);
120 if not self.vccexplored(vcc):
121 print "Exploring vcc=%i" % vcc;
124 self.scanat(trials,vcc,gnd,time)
128 print "Voltage %i already explored." % vcc;
130 def vccexplored(self,vcc):
132 c.execute("select vcc from glitches where vcc=? limit 1;",[vcc]);
136 #This voltage has been explored, because no complete exception has been thrown.
138 except: #Voltage hasn't been explored
141 def scanat(self,trials,vcc,gnd,time):
144 client.glitchRate(time);
145 client.glitchVoltages(gnd, vcc); #drop voltage target
148 #print "-- (%5i,%5i)" % (time,vcc);
150 for i in range(0,trials):
151 client.glitchstart();
153 #Try to read *0, which is secret if read works.
154 a=client.eeprompeek(0x0);
155 if self.lock>0: #locked
156 if(a!=0 and a!=0xFF and a!=self.secret):
159 print "-- %04x: %02x HELL YEAH! " % (time, a);
166 #print "values (%i,%i,%i,%i,%i);" % (
167 # time,vcc,gnd,gcount,scount);
168 self.db.execute("insert into glitches(time,vcc,gnd,trials,glitchcount,count,lock)"
169 "values (%i,%i,%i,%i,%i,%i,%i);" % (
170 time,vcc,gnd,trials,gcount,scount,self.lock));