+
+class GoodFETMAXUSBMassFile(GoodFETMAXUSBMass):
+ """This emulates a USB Mass Storage Device, providing a file as
+ its image. Writes are not yet supported, and this is very slow.
+ Performance hacks will come after the code stabilizes."""
+
+ datafile=None;
+ datafilelen=None;
+ def openImage(self,filename):
+ """Opens an image for use. Call this *before* massinit()."""
+ self.datafile=open(filename,"rb");
+ print "Opened an image with %i blocks." % self.getSectorCount();
+ def putSectorData(self,lba,block):
+ """Writes a 512-byte sector to the lba address."""
+ print "Writes aren't yet supported.";
+ return;
+
+ def getSectorData(self,lba):
+ """Returns data from a 512-byte sector."""
+ toret="";
+
+ #Seek to the appropraite block.
+ #print "Seeking to position %i"% (lba*512)
+ self.datafile.seek(lba*512,0);
+ pos=self.datafile.tell();
+ if pos!=lba*512:
+ print "SEEK ERROR: Seeked to %i (lba=%i), but now I'm at %i (lba=%i)" % (
+ lba*512,lba,
+ pos,pos/512);
+ #sys.exit();
+
+ #Dump the data out, assuming no blocking and filling with nonsense.
+ toret=self.datafile.read(512);
+ if len(toret)<512:
+ print "Holy hell, I only have %i bytes of 512." % len(toret);
+
+ toretbytes=range(0,len(toret));
+ for b in range(0,len(toret)): toretbytes[b]=ord(toret[b]);
+ return toretbytes;
+
+ def getSectorCount(self):
+ """Returns the number of viable Logical Block Addresses."""
+
+ # Python must have a better way to read a file length, but for
+ # now we just read the whole damned thing and then throw it
+ # away. With present performance, we can't read anything
+ # large enough for this to be a problem.
+ if self.datafilelen==None:
+ self.datafile.seek(0);
+ self.datafilelen=len(self.datafile.read());
+ if self.datafilelen%512!=0:
+ print "ERROR: Image does not have an integer number of blocks!"
+ print "%i \% 512 == %i" % (self.datafilelen,
+ self.datafilelen%512);
+ sys.exit();
+ return self.datafilelen/512-1;
+if(len(sys.argv)==1):
+ print "Usage: %s disk.img\n" % sys.argv[0];
+ sys.exit();
+
+