freemyipod r172 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r171‎ | r172 | r173 >
Date:17:26, 23 August 2010
Author:farthen
Status:new
Tags:
Comment:
Fix some bugs, add run to run an embios app
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -144,7 +144,7 @@
145145 Decorator for all commands.
146146 The decorated function is called with (self, all, other, arguments, ...)
147147 """
148 - def decorator(args):
 148+ def decorator(*args):
149149 return func(args[0], *args[1:])
150150 func._command = True
151151 decorator.func = func
@@ -180,6 +180,7 @@
181181 except libembios.DeviceNotFoundError:
182182 self.logger.error("No emBIOS device found!")
183183 end(1)
 184+ self.getinfo("version")
184185
185186 def _parsecommand(self, func, args):
186187 # adds self to the commandline args.
@@ -187,7 +188,7 @@
188189 args.insert(0, self)
189190 if func in self.cmddict:
190191 try:
191 - self.cmddict[func](args)
 192+ self.cmddict[func](*args)
192193 except ArgumentError, e:
193194 usage(e)
194195 except ArgumentError:
@@ -196,6 +197,8 @@
197198 usage(e)
198199 except NotImplementedError:
199200 self.logger.error("This function is not implemented yet!")
 201+ except libembios.DeviceNotFoundError:
 202+ self.logger.error("Device not found!")
200203 except libembios.DeviceError, e:
201204 self.logger.error(str(e))
202205 except TypeError, e:
@@ -263,7 +266,7 @@
264267 """
265268 if infotype == "version":
266269 resp = self.embios.getversioninfo()
267 - self.logger.info(libembiosdata.swtypes[resp.swtypeid] + " v" + str(resp.majorv) + "." + str(resp.minorv) +
 270+ self.logger.info("Connected to "+libembiosdata.swtypes[resp.swtypeid] + " v" + str(resp.majorv) + "." + str(resp.minorv) +
268271 "." + str(resp.patchv) + " r" + str(resp.revision) + " running on " + libembiosdata.hwtypes[resp.hwtypeid] + "\n")
269272 elif infotype == "packetsize":
270273 resp = self.embios.getpacketsizeinfo()
@@ -546,14 +549,35 @@
547550 stacksize = self._hexint(stacksize)
548551 priority = self._hexint(priority)
549552 self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
 553+
 554+ @command
 555+ def run(self, filename):
 556+ """
 557+ Uploads the emBIOS application to an address in the user memory
 558+ and executes it
 559+ """
 560+ try:
 561+ f = open(filename, "rb")
 562+ except IOError:
 563+ raise ArgumentError("File not readable. Does it exist?")
 564+ data = self.embios.getusermemrange()
 565+ addr = data.lower
 566+ maxsize = data.upper - data.lower
 567+ filesize = os.path.getsize(filename)
 568+ if filesize > maxsize:
 569+ raise ArgumentError("The file is too big, it doesn't fit into the user memory.")
 570+ self.logger.info("Uploading application to "+hex(addr)+" - "+hex(addr+filesize)+"\n")
 571+ self.embios.write(addr, f.read())
 572+ self.execute(addr)
550573
551574 @command
552 - def run(self, address):
 575+ def execute(self, addr):
553576 """
554577 Executes the emBIOS application at <address>.
555578 """
556 - address = self._hexint(address)
557 - raise NotImplementedError
 579+ addr = self._hexint(addr)
 580+ self.logger.info("Starting emBIOS app at "+hex(addr)+"\n")
 581+ self.embios.execimage(addr)
558582
559583 @command
560584 def readrawbootflash(self, addr_flash, addr_mem, size):
@@ -586,12 +610,14 @@
587611 """
588612 Flushes the CPUs data and instruction caches.
589613 """
590 - raise NotImplementedError
 614+ self.logger.info("Flushing CPU data and instruction caches...")
 615+ self.embios.flushcaches()
 616+ self.logger.info("done\n")
591617
592618 @command
593619 def aesencrypt(self, addr, size, keyindex):
594620 """
595 - Encrypt a buffer using a hardware key
 621+ Encrypts a buffer using a hardware key
596622 """
597623 addr = self._hexint(addr)
598624 size = self._hexint(size)
@@ -601,12 +627,29 @@
602628 @command
603629 def aesdecrypt(self, addr, size, keyindex):
604630 """
605 - Decrypt a buffer using a hardware key
 631+ Decrypts a buffer using a hardware key
606632 """
607633 addr = self._hexint(addr)
608634 size = self._hexint(size)
609635 keyindex = self._hexint(keyindex)
610636 self.embios.aesdecrypt(addr, size, keyindex)
 637+
 638+ @command
 639+ def hmac_sha1(self, addr, size, destination):
 640+ """
 641+ Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination'
 642+ """
 643+ addr = self._hexint(addr)
 644+ size = self._hexint(size)
 645+ destination = self._hexint(destination)
 646+ sha1size = 0x14
 647+ self.logger.info("Generating hmac-sha1 hash from the buffer at "+hex(addr)+" with the size "+hex(size)+
 648+ " and saving it to "+hex(destination)+" - "+hex(destination+sha1size)+"...")
 649+ self.embios.hmac_sha1(addr, size, destination)
 650+ self.logger.info("done\n")
 651+ data = self.embios.readmem(destination, sha1size)
 652+ hash = ord(data)
 653+ self.logger.info("The generated hash is "+hex(hash))
611654
612655 if __name__ == "__main__":
613656 if len(sys.argv) < 2:
Index: embios/trunk/tools/libembios.py
@@ -269,11 +269,11 @@
270270 raise DeviceError("The device returned the error code "+str(resp.id))
271271 return resp
272272
273 - def flushcpucache(self):
 273+ def flushcaches(self):
274274 """ Flushes the CPU instruction and data cache """
275275 return self.lib.monitorcommand(struct.pack("IIII", 20, 0, 0, 0), "III", (None, None, None))
276276
277 - def run(self, addr):
 277+ def execimage(self, addr):
278278 """ Runs the emBIOS app at 'addr' """
279279 return self.lib.monitorcommand(struct.pack("IIII", 21, addr, 0, 0), "III", ("excecimage", None, None))
280280