freemyipod r442 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r441‎ | r442 | r443 >
Date:20:33, 17 January 2011
Author:farthen
Status:new
Tags:
Comment:
emcore tools: implement new firmware api, some verbose debug messages have been added
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -170,6 +170,8 @@
171171 """
172172 if type(something) == bool:
173173 return something
 174+ if something is None:
 175+ return False
174176 elif type(something) == int or type(something) == long:
175177 return bool(something)
176178 elif type(something == str):
@@ -195,7 +197,7 @@
196198 return int(something, 16)
197199 except ValueError:
198200 raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
199 - elif type(something) == NoneType:
 201+ elif something is None:
200202 return None
201203 else:
202204 raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
@@ -261,11 +263,11 @@
262264 self.emcore.poweroff(force)
263265
264266 @command
265 - def uploadfile(self, addr, filename):
 267+ def uploadfile(self, filename, addr = None):
266268 """
267269 Uploads a file to the device
268 - <offset>: the address to upload the file to
269 - <filename>: the path to the file
 270+ <filename>: The path to the file
 271+ [addr]: The address to upload the file to. Allocates a chunk of memory if not given.
270272 """
271273 addr = self._hexint(addr)
272274 try:
@@ -272,12 +274,20 @@
273275 f = open(filename, 'rb')
274276 except IOError:
275277 raise ArgumentError("File not readable. Does it exist?")
276 - self.logger.info("Writing file '" + filename + \
277 - "' to memory at " + self._hex(addr) + "...")
 278+ if addr is not None:
 279+ self.logger.info("Writing file '" + filename + \
 280+ "' to memory at " + self._hex(addr) + "...\n")
 281+ else:
 282+ self.logger.info("Writing file '" + filename + " to an allocated memory region...\n")
278283 with f:
279 - self.emcore.write(addr, f.read())
 284+ if addr is not None:
 285+ self.emcore.write(addr, f.read())
 286+ else:
 287+ addr = self.emcore.upload(f.read())
 288+ size = f.tell()
280289 f.close()
281 - self.logger.info("done\n")
 290+ self.logger.info("Done uploading " + str(size) + " bytes to 0x" + self._hex(addr) + "\n")
 291+ return addr, size
282292
283293 @command
284294 def downloadfile(self, addr, size, filename):
@@ -608,23 +618,23 @@
609619 self.emcore.bootflashwrite(addr_mem, addr_flash, size)
610620
611621 @command
612 - def runfirmware(self, addr, filename):
 622+ def runfirmware(self, targetaddr, filename):
613623 """
614624 Uploads the firmware in <filename>
615 - to the address at <addr> and executes it.
 625+ to an allocated buffer and executes it at <targetaddr>.
616626 """
617 - addr = self._hexint(addr)
618 - self.uploadfile(addr, filename)
619 - self.execfirmware(addr)
 627+ targetaddr = self._hexint(targetaddr)
 628+ addr, size = self.uploadfile(filename)
 629+ self.execfirmware(targetaddr, addr, size)
620630
621631 @command
622 - def execfirmware(self, addr):
 632+ def execfirmware(self, targetaddr, addr, size):
623633 """
624 - Executes the firmware at <addr>
 634+ Moves the firmware at <addr> with <size> to <targetaddr> and executes it
625635 """
626636 addr = self._hexint(addr)
627 - self.logger.info("Running firmware at "+self._hex(addr)+". Bye.")
628 - self.emcore.execfirmware(addr)
 637+ self.logger.info("Running firmware at "+self._hex(targetaddr)+". Bye.\n")
 638+ self.emcore.execfirmware(targetaddr, addr, size)
629639
630640 @command
631641 def aesencrypt(self, addr, size, keyindex):
Index: emcore/trunk/tools/libemcore.py
@@ -267,6 +267,16 @@
268268 return data
269269
270270 @command()
 271+ def upload(self, data):
 272+ """ Allocates memory of the size of 'data' and uploads 'data' to that memory region.
 273+ Returns the address where 'data' is stored
 274+ """
 275+ addr = self.malloc(len(data))
 276+ self.logger.debug("Uploading %d bytes to 0x%x\n" % (len(data), addr))
 277+ self.write(addr, data)
 278+ return addr
 279+
 280+ @command()
271281 def readstring(self, addr, maxlength = 256):
272282 """ Reads a zero terminated string from memory
273283 Reads only a maximum of 'maxlength' chars.
@@ -521,9 +531,10 @@
522532 return self.lib.monitorcommand(struct.pack("IIII", 23, memaddr, flashaddr, size), "III", (None, None, None))
523533
524534 @command()
525 - def execfirmware(self, addr):
526 - """ Executes the firmware at 'addr' and passes all control to it. """
527 - return self.lib.monitorcommand(struct.pack("IIII", 24, addr, 0, 0))
 535+ def execfirmware(self, targetaddr, addr, size):
 536+ """ Moves the firmware at 'addr' with size 'size' to 'targetaddr' and passes all control to it. """
 537+ self.logger.debug("Moving firmware at 0x%x with the size %d to 0x%x and executing it\n" % (addr, size, targetaddr))
 538+ return self.lib.monitorcommand(struct.pack("IIII", 24, targetaddr, addr, size))
528539
529540 @command(timeout = 30000)
530541 def aesencrypt(self, addr, size, keyindex):
@@ -823,13 +834,17 @@
824835 @command()
825836 def malloc(self, size):
826837 """ Allocates 'size' bytes and returns a pointer to the allocated memory """
 838+ self.logger.debug("Allocating %d bytes of memory\n" % size)
827839 result = self.lib.monitorcommand(struct.pack("IIII", 52, size, 0, 0), "III", ("ptr", None, None))
 840+ self.logger.debug("Allocated %d bytes of memory at 0x%x\n" % (size, result.ptr))
828841 return result.ptr
829842
830843 @command()
831844 def memalign(self, align, size):
832845 """ Allocates 'size' bytes aligned to 'align' and returns a pointer to the allocated memory """
 846+ self.logger.debug("Allocating %d bytes of memory aligned to 0x%x\n" % (size, align))
833847 result = self.lib.monitorcommand(struct.pack("IIII", 53, align, size, 0), "III", ("ptr", None, None))
 848+ self.logger.debug("Allocated %d bytes of memory at 0x%x\n" % (size, result.ptr))
834849 return result.ptr
835850
836851 @command()
@@ -838,17 +853,21 @@
839854 expanding or reducing the amount of memory available in the block.
840855 Returns a pointer to the reallocated memory.
841856 """
 857+ self.logger.debug("Reallocating 0x%x to have the new size %d\n" % (ptr, size))
842858 result = self.lib.monitorcommand(struct.pack("IIII", 54, ptr, size, 0), "III", ("ptr", None, None))
 859+ self.logger.debug("Reallocated memory at 0x%x to 0x%x with the new size %d\n" % (ptr, result.ptr, size))
843860 return result.ptr
844861
845862 @command()
846863 def reownalloc(self, ptr, owner):
847864 """ Changes the owner of the memory allocation 'ptr' to the thread struct at addr 'owner' """
 865+ self.logger.debug("Changing owner of the memory region 0x%x to 0x%x" % (ptr, owner))
848866 return self.lib.monitorcommand(struct.pack("IIII", 55, ptr, owner, 0), "III", (None, None, None))
849867
850868 @command()
851869 def free(self, ptr):
852870 """ Frees the memory space pointed to by 'ptr' """
 871+ self.logger.debug("Freeing the memory region at 0x%x\n" % ptr)
853872 return self.lib.monitorcommand(struct.pack("IIII", 56, addr, 0, 0), "III", (None, None, None))
854873
855874
@@ -868,7 +887,7 @@
869888 self.connected = True
870889
871890 def monitorcommand(self, cmd, rcvdatatypes=None, rcvstruct=None):
872 - self.logger.debug("Sending monitorcommand\n")
 891+ self.logger.debug("Sending monitorcommand [0x%s]\n" % cmd[3::-1].encode("hex"))
873892 writelen = self.dev.cout(cmd)
874893 if rcvdatatypes:
875894 rcvdatatypes = "I" + rcvdatatypes # add the response