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 and tables.
30 self.db=sqlite3.connect("glitch.db");
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 lock=0; #1 locks, 0 unlocked
60 vstop=1024; #Could be as high as 0xFFF
63 tstop=-1; #<0 defaults to full range
65 self.scan(lock,trials,vstart,vstop,tstart,tstop);
67 def scan(self,lock,trials=1,vstart=0,vstop=0xfff,tstart=0,tstop=-1):
72 tstop=client.glitchstarttime(); #Really long; only use for initial investigation.
73 print "-- Start takes %04i cycles." % tstop;
79 while(client.eeprompeek(0)!=self.secret):
80 print "-- Setting secret";
83 #Flash the secret to the first two bytes of CODE memory.
85 client.eeprompoke(0,self.secret);
86 client.eeprompoke(1,self.secret);
89 #Lock chip to unlock it later.
92 voltages=range(vstart,vstop,1);
93 times=range(tstart,tstop,1);
95 gnd=0; #TODO, glitch GND.
97 random.shuffle(voltages);
98 #random.shuffle(times);
100 count=0; #Commit counter.
103 self.scanat(trials,vcc,gnd,time)
111 def scanat(self,trials,vcc,gnd,time):
114 client.glitchRate(time);
115 client.glitchVoltages(gnd, vcc); #drop voltage target
118 print "-- (%5i,%5i)" % (time,vcc);
120 for i in range(0,trials):
121 client.glitchstart();
123 #Try to read *0, which is secret if read works.
124 a=client.eeprompeek(0x0);
125 if self.lock>0: #locked
126 if(a!=0 and a!=0xFF and a!=self.secret):
129 print "-- %04x: %02x HELL YEAH! " % (time, a);
136 #print "values (%i,%i,%i,%i,%i);" % (
137 # time,vcc,gnd,gcount,scount);
138 self.db.execute("insert into glitches(time,vcc,gnd,trials,glitchcount,count,lock)"
139 "values (%i,%i,%i,%i,%i,%i,%i);" % (
140 time,vcc,gnd,trials,gcount,scount,self.lock));