X-Git-Url: http://git.rot13.org/?p=goodfet;a=blobdiff_plain;f=client%2FGoodFET.py;h=1caad81ecf3e64fefb04033023461366a4c84d0d;hp=10f2e6f01f499b17e25a0117b7d9cc67159fded0;hb=effb18af1d73fbdf0f1e8aa57478e289935d353c;hpb=052771a83e020314b718e82d0b1b2a645a7c3d4f diff --git a/client/GoodFET.py b/client/GoodFET.py index 10f2e6f..1caad81 100755 --- a/client/GoodFET.py +++ b/client/GoodFET.py @@ -5,15 +5,16 @@ # # This code is being rewritten and refactored. You've been warned! -import sys, time, string, cStringIO, struct, glob, serial, os; +import sys, time, string, cStringIO, struct, glob, os; import sqlite3; fmt = ("B", " 0: port = glob_list[0]; - if os.name=='nt': + if port is None: + glob_list = glob.glob("/dev/ttyU0"); + if len(glob_list) > 0: + port = glob_list[0]; + if port is None and os.name=='nt': from scanwin32 import winScan; scan=winScan(); for order,comport,desc,hwid in sorted(scan.comports()): @@ -101,10 +169,13 @@ class GoodFET: #Do nothing. a=1; + baud=115200; + if(os.environ.get("platform")=='arduino'): + baud=19200; #Slower, for now. self.serialport = serial.Serial( port, #9600, - 115200, + baud, parity = serial.PARITY_NONE, timeout=timeout ) @@ -114,41 +185,56 @@ class GoodFET: connected=0; while connected==0: while self.verb!=0x7F or self.data!="http://goodfet.sf.net/": - #print "Resyncing."; + #while self.data!="http://goodfet.sf.net/": + #print "'%s'!=\n'%s'" % (self.data,"http://goodfet.sf.net/"); + if attemptlimit is not None and attempts >= attemptlimit: + return + elif attempts>2: + print "Resyncing."; self.serialport.flushInput() self.serialport.flushOutput() - #Explicitly set RTS and DTR to halt board. - self.serialport.setRTS(1); - self.serialport.setDTR(1); - #Drop DTR, which is !RST, low to begin the app. - self.serialport.setDTR(0); #TelosB reset, prefer software to I2C SPST Switch. if(os.environ.get("platform")=='telosb'): + #print "TelosB Reset"; self.telosBReset(); + else: + #Explicitly set RTS and DTR to halt board. + self.serialport.setRTS(1); + self.serialport.setDTR(1); + #Drop DTR, which is !RST, low to begin the app. + self.serialport.setDTR(0); + #self.serialport.write(chr(0x80)); #self.serialport.write(chr(0x80)); #self.serialport.write(chr(0x80)); #self.serialport.write(chr(0x80)); - self.serialport.flushInput() - self.serialport.flushOutput() + #self.serialport.flushInput() + #self.serialport.flushOutput() #time.sleep(60); attempts=attempts+1; self.readcmd(); #Read the first command. + #print "Got %02x,%02x:'%s'" % (self.app,self.verb,self.data); #Here we have a connection, but maybe not a good one. + #print "We have a connection." connected=1; olds=self.infostring(); clocking=self.monitorclocking(); + #if(os.environ.get("platform")!='arduino'): for foo in range(1,30): if not self.monitorecho(): - if self.verbose: print "Comm error on %i try, resyncing out of %s." % (foo, - clocking); - connected=0; - break; + if self.verbose: + print "Comm error on %i try, resyncing out of %s." % (foo, + clocking); + connected=0; + break; if self.verbose: print "Connected after %02i attempts." % attempts; self.mon_connected(); + self.serialport.setTimeout(12); + def serClose(self): + self.serialport.close(); def telosSetSCL(self, level): self.serialport.setRTS(not level) def telosSetSDA(self, level): @@ -259,8 +345,14 @@ class GoodFET: try: #print "Reading..."; self.app=ord(self.serialport.read(1)); - #print "APP=%2x" % self.app; + #print "APP=%02x" % self.app; self.verb=ord(self.serialport.read(1)); + + #Fixes an obscure bug in the TelosB. + if self.app==0x00: + while self.verb==0x00: + self.verb=ord(self.serialport.read(1)); + #print "VERB=%02x" % self.verb; self.count=( ord(self.serialport.read(1)) @@ -286,9 +378,9 @@ class GoodFET: return self.data; except TypeError: if self.connected: - print "Error: waiting for serial read timed out (most likely)."; - print "This shouldn't happen after syncing. Exiting for safety."; - sys.exit(-1) + print "Warning: waiting for serial read timed out (most likely)."; + #print "This shouldn't happen after syncing. Exiting for safety."; + #sys.exit(-1) return self.data; #Glitching stuff. def glitchApp(self,app): @@ -312,8 +404,11 @@ class GoodFET: """Time the execution of a verb.""" if data==None: data=[]; self.data=[app&0xff, verb&0xFF]+data; + print "Timing app %02x verb %02x." % (app,verb); self.writecmd(self.GLITCHAPP,0x82,len(self.data),self.data); - return ord(self.data[0])+(ord(self.data[1])<<8); + time=ord(self.data[0])+(ord(self.data[1])<<8); + print "Timed to be %i." % time; + return time; def glitchVoltages(self,low=0x0880, high=0x0fff): """Set glitching voltages. (0x0fff is max.)""" self.data=[low&0xff, (low>>8)&0xff, @@ -353,27 +448,60 @@ class GoodFET: """Execute supplied code.""" self.writecmd(0,0x31,2,#len(code), code); - def peekbyte(self,address): + def MONpeek8(self,address): """Read a byte of memory from the monitor.""" self.data=[address&0xff,address>>8]; self.writecmd(0,0x02,2,self.data); #self.readcmd(); return ord(self.data[0]); - def peekword(self,address): + def MONpeek16(self,address): """Read a word of memory from the monitor.""" - return self.peekbyte(address)+(self.peekbyte(address+1)<<8); + return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8); def peek(self,address): """Read a word of memory from the monitor.""" - return self.peekbyte(address)+(self.peekbyte(address+1)<<8); - def pokebyte(self,address,value): + return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8); + def eeprompeek(self,address): + """Read a word of memory from the monitor.""" + print "EEPROM peeking not supported for the monitor."; + #return self.MONpeek8(address)+(self.MONpeek8(address+1)<<8); + def peekbysym(self,name): + """Read a value by its symbol name.""" + #TODO include memory in symbol. + reg=self.symbols.get(name); + return self.peek8(reg,"data"); + def pokebysym(self,name,val): + """Write a value by its symbol name.""" + #TODO include memory in symbol. + reg=self.symbols.get(name); + return self.pokebyte(reg,val); + def pokebyte(self,address,value,memory="vn"): """Set a byte of memory by the monitor.""" self.data=[address&0xff,address>>8,value]; self.writecmd(0,0x03,3,self.data); return ord(self.data[0]); + def poke16(self,address,value): + """Set a word of memory by the monitor.""" + self.pokebyte(address,value&0xFF); + self.pokebyte(address,(value>>8)&0xFF); + return value; + def setsecret(self,value): + """Set a secret word for later retreival. Used by glitcher.""" + #self.eeprompoke(0,value); + #self.eeprompoke(1,value); + print "Secret setting is not yet suppored for this target."; + print "Aborting."; + + def getsecret(self): + """Get a secret word. Used by glitcher.""" + #self.eeprompeek(0); + print "Secret getting is not yet suppored for this target."; + print "Aborting."; + sys.exit(); + def dumpmem(self,begin,end): i=begin; while i