freemyipod r382 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r381‎ | r382 | r383 >
Date:15:00, 23 December 2010
Author:farthen
Status:new
Tags:
Comment:
embios.py: Some cleanup
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -120,7 +120,8 @@
121121 class Logger(object):
122122 """
123123 Simple stdout logger.
124 - Loglevel 4 is most verbose, Loglevel 0 only say something if there is an error.
 124+ Loglevel 4 is most verbose, Loglevel 0: Only say something if there is an error.
 125+ The log function doesn't care about the loglevel and always logs to stdout.
125126 """
126127 def __init__(self):
127128 # Possible values: 0 (only errors), 1 (warnings), 2 (info, recommended for production use), 3 and more (debug)
@@ -131,13 +132,13 @@
132133
133134 def debug(self, text):
134135 if self.loglevel >= 3:
135 - self.log(text)
 136+ self.log("DEBUG: " + text)
136137
137138 def info(self, text):
138139 if self.loglevel >= 2:
139140 self.log(text)
140141
141 - def warning(self, text):
 142+ def warn(self, text):
142143 if self.loglevel >= 1:
143144 self.log("WARNING: " + text)
144145
@@ -269,15 +270,27 @@
270271 hwtype = libembiosdata.hwtypes[self.embios.lib.dev.hwtypeid]
271272 except KeyError:
272273 hwtype = "UNKNOWN (ID = " + self._hex(self.embios.lib.dev.hwtypeid) + ")"
273 - self.logger.info("Connected to "+libembiosdata.swtypes[self.embios.lib.dev.swtypeid] + " v" + str(self.embios.lib.dev.version.majorv) + "." + str(self.embios.lib.dev.version.minorv) +
274 - "." + str(self.embios.lib.dev.version.patchv) + " r" + str(self.embios.lib.dev.version.revision) + " running on " + hwtype + "\n")
 274+ self.logger.info("Connected to " + \
 275+ libembiosdata.swtypes[self.embios.lib.dev.swtypeid] + \
 276+ " v" + str(self.embios.lib.dev.version.majorv) + \
 277+ "." + str(self.embios.lib.dev.version.minorv) + \
 278+ "." + str(self.embios.lib.dev.version.patchv) + \
 279+ " r" + str(self.embios.lib.dev.version.revision) + \
 280+ " running on " + hwtype + "\n")
275281
276282 elif infotype == "packetsize":
277 - self.logger.info("Maximum packet sizes: \n command out: " + str(self.embios.lib.dev.packetsizelimit.cout) + "\n command in: " + str(self.embios.lib.dev.packetsizelimit.cin) + "\n data in: " + str(self.embios.lib.dev.packetsizelimit.din) + "\n data out: " + str(self.embios.lib.dev.packetsizelimit.dout))
 283+ self.logger.info("Maximum packet sizes: \n command out: " + str(self.embios.lib.dev.packetsizelimit.cout) + \
 284+ "\n command in: " + str(self.embios.lib.dev.packetsizelimit.cin) + \
 285+ "\n data in: " + str(self.embios.lib.dev.packetsizelimit.din) + \
 286+ "\n data out: " + str(self.embios.lib.dev.packetsizelimit.dout))
278287
279288 elif infotype == "usermemrange":
280289 resp = self.embios.getusermemrange()
281 - self.logger.info("The user memory range is "+self._hex(self.embios.lib.dev.usermem.lower)+" - "+self._hex(self.embios.lib.dev.usermem.upper - 1))
 290+ self.logger.info("The user memory range is " + \
 291+ self._hex(self.embios.lib.dev.usermem.lower) + \
 292+ " - " + \
 293+ self._hex(self.embios.lib.dev.usermem.upper - 1))
 294+
282295 else:
283296 raise ArgumentTypeError("one out of 'version', 'packetsize', 'usermemrange'", infotype)
284297
@@ -317,7 +330,8 @@
318331 f = open(filename, 'rb')
319332 except IOError:
320333 raise ArgumentError("File not readable. Does it exist?")
321 - self.logger.info("Writing file '"+filename+"' to memory at "+self._hex(addr)+"...")
 334+ self.logger.info("Writing file '" + filename + \
 335+ "' to memory at " + self._hex(addr) + "...")
322336 with f:
323337 self.embios.write(addr, f.read())
324338 f.close()
@@ -337,7 +351,9 @@
338352 f = open(filename, 'wb')
339353 except IOError:
340354 raise ArgumentError("Can not open file for write!")
341 - self.logger.info("Reading data from address "+self._hex(addr)+" with the size "+self._hex(size)+" to '"+filename+"'...")
 355+ self.logger.info("Reading data from address " + self._hex(addr) + \
 356+ " with the size " + self._hex(size) + \
 357+ " to '"+filename+"'...")
342358 with f:
343359 f.write(self.embios.read(addr, size))
344360 f.close()
@@ -356,7 +372,8 @@
357373 raise ArgumentError("Specified integer too long")
358374 data = struct.pack("I", integer)
359375 self.embios.write(addr, data)
360 - self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr)+"\n")
 376+ self.logger.info("Integer '" + self._hex(integer) + \
 377+ "' written successfully to " + self._hex(addr) + "\n")
361378
362379 @command
363380 def downloadint(self, addr):
@@ -367,7 +384,8 @@
368385 addr = self._hexint(addr)
369386 data = self.embios.read(addr, 4)
370387 integer = struct.unpack("I", data)[0]
371 - self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr)+"\n")
 388+ self.logger.info("Integer '" + self._hex(integer) + \
 389+ "' read from address " + self._hex(addr) + "\n")
372390
373391 @command
374392 def i2cread(self, bus, slave, addr, size):
@@ -453,7 +471,8 @@
454472 for word in args:
455473 text += word + " "
456474 text = text[:-1]
457 - self.logger.info("Writing '"+text+"' to the device consoles identified with "+self._hex(bitmask)+"\n")
 475+ self.logger.info("Writing '" + text + \
 476+ "' to the device consoles identified with " + self._hex(bitmask) + "\n")
458477 self.embios.cwrite(text, bitmask)
459478
460479 @command
@@ -463,7 +482,8 @@
464483 <bitmask>: the bitmask of the consoles to be flushed
465484 """
466485 bitmask = self._hexint(bitmask)
467 - self.logger.info("Flushing consoles identified with the bitmask "+self._hex(bitmask)+"\n")
 486+ self.logger.info("Flushing consoles identified with the bitmask " + \
 487+ self._hex(bitmask) + "\n")
468488 self.embios.cflush(bitmask)
469489
470490 @command
@@ -473,18 +493,32 @@
474494 """
475495 import datetime
476496 threads = self.embios.getprocinfo()
477 - self.logger.info("The device has "+str(len(threads))+" running threads:\n\n")
 497+ cpuload = 1
 498+ threadload = 0
 499+ idleload = 0
478500 for thread in threads:
 501+ if thread.priority != 0:
 502+ threadload += (thread.cpuload*100)/255
 503+ else:
 504+ idleload += (thread.cpuload*100)/255
 505+ coreload = 1 - (threadload + idleload)
 506+ cpuload = threadload + coreload
 507+ self.logger.info("The device has " + str(len(threads)) + " running threads.\n" + \
 508+ "It is running at " + str(cpuload * 100) + "% cpu load with a core load of " + \
 509+ str(coreload * 100) + "%, a thread load of " + str(threadload * 100) + "% " + \
 510+ "and an idle load of " + str(idleload * 100) + "%\n\n")
 511+ self.logger.info("Thread dump:\n")
 512+ for thread in threads:
479513 self.logger.info(" "+thread.name+":\n")
480 - self.logger.info(" Thread id: "+str(thread.id)+"\n")
481 - self.logger.info(" Thread type: "+thread.type+"\n")
482 - self.logger.info(" Thread state: "+thread.state+"\n")
483 - self.logger.info(" Block type: "+thread.block_type+"\n")
484 - self.logger.info(" Blocked by: "+self._hex(thread.blocked_by_ptr)+"\n")
485 - self.logger.info(" Priority: "+str(thread.priority)+"/255\n")
 514+ self.logger.info(" Thread id: " + str(thread.id)+"\n")
 515+ self.logger.info(" Thread type: " + thread.type+"\n")
 516+ self.logger.info(" Thread state: " + thread.state+"\n")
 517+ self.logger.info(" Block type: " + thread.block_type+"\n")
 518+ self.logger.info(" Blocked by: " + self._hex(thread.blocked_by_ptr)+"\n")
 519+ self.logger.info(" Priority: " + str(thread.priority)+"/255\n")
486520 self.logger.info(" Current CPU load: "+str((thread.cpuload*100)/255)+"%\n")
487521 self.logger.info(" CPU time (total): "+str(datetime.timedelta(microseconds=thread.cputime_total))+"\n")
488 - self.logger.info(" Stack address: "+self._hex(thread.stackaddr)+"\n")
 522+ self.logger.info(" Stack address: " + self._hex(thread.stackaddr)+"\n")
489523 self.logger.info(" Registers:\n")
490524 for registerrange in range(4):
491525 self.logger.info(" ")
@@ -557,7 +591,12 @@
558592 priority = self._hexint(priority)
559593 data = self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
560594 name = self.embios.readstring(nameptr)
561 - self.logger.info("Created a thread with the threadid " + data.id + ", the name \"" + name + "\", the entrypoint at " + self._hex(entrypoint) + ", the stack at " + self._hex(stackpointer) + " with a size of " + self._hex(stacksize) + " and a priority of " + self._hex(priority) + "\n")
 595+ self.logger.info("Created a thread with the threadid " + data.id + \
 596+ ", the name \"" + name + "\"" + \
 597+ ", the entrypoint at " + self._hex(entrypoint) + \
 598+ ", the stack at " + self._hex(stackpointer) + \
 599+ " with a size of " + self._hex(stacksize) + \
 600+ " and a priority of " + self._hex(priority) + "\n")
562601
563602 @command
564603 def run(self, filename):
@@ -620,10 +659,10 @@
621660 addr_mem = self._hexint(addr_mem)
622661 size = self._hexint(size)
623662 force = self._bool(force)
624 - self.logger.info("Writing boot flash from the memory in "+self._hex(addr_mem)+" - "+
 663+ self.logger.warn("Writing boot flash from the memory in "+self._hex(addr_mem)+" - "+
625664 hex(addr_mem+size)+" to "+self._hex(addr_flash)+" - "+self._hex(addr_flash+size)+"\n")
626665 if force == False:
627 - self.logger.info("If this was not what you intended press Ctrl-C NOW")
 666+ self.logger.warn("If this was not what you intended press Ctrl-C NOW")
628667 for i in range(10):
629668 self.logger.info(".")
630669 time.sleep(1)
@@ -681,8 +720,9 @@
682721 size = self._hexint(size)
683722 destination = self._hexint(destination)
684723 sha1size = 0x14
685 - self.logger.info("Generating hmac-sha1 hash from the buffer at "+self._hex(addr)+" with the size "+self._hex(size)+
686 - " and saving it to "+self._hex(destination)+" - "+self._hex(destination+sha1size)+"...")
 724+ self.logger.info("Generating hmac-sha1 hash from the buffer at " + self._hex(addr) + \
 725+ " with the size " + self._hex(size) + " and saving it to " + \
 726+ self._hex(destination) + " - " + self._hex(destination+sha1size) + "...")
687727 self.embios.lib.dev.timeout = 30000
688728 self.embios.hmac_sha1(addr, size, destination)
689729 self.logger.info("done\n")
@@ -697,11 +737,11 @@
698738 Gathers some information about the NAND chip used
699739 """
700740 data = self.embios.ipodnano2g_getnandinfo()
701 - self.logger.info("NAND chip type: "+self._hex(data["type"])+"\n")
702 - self.logger.info("Number of banks: "+str(data["banks"])+"\n")
703 - self.logger.info("Number of blocks: "+str(data["blocks"])+"\n")
704 - self.logger.info("Number of user blocks: "+str(data["userblocks"])+"\n")
705 - self.logger.info("Pages per block: "+str(data["pagesperblock"]))
 741+ self.logger.info("NAND chip type: " + self._hex(data["type"])+"\n")
 742+ self.logger.info("Number of banks: " + str(data["banks"])+"\n")
 743+ self.logger.info("Number of blocks: " + str(data["blocks"])+"\n")
 744+ self.logger.info("Number of user blocks: " + str(data["userblocks"])+"\n")
 745+ self.logger.info("Pages per block: " + str(data["pagesperblock"]))
706746
707747 @command
708748 def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
@@ -714,7 +754,8 @@
715755 count = self._hexint(count)
716756 doecc = int(doecc)
717757 checkempty = int(checkempty)
718 - self.logger.info("Reading "+self._hex(count)+" NAND pages starting at "+self._hex(start)+" to "+self._hex(addr)+"...")
 758+ self.logger.info("Reading " + self._hex(count) + " NAND pages starting at " + \
 759+ self._hex(start) + " to " + self._hex(addr) + "...")
719760 self.embios.lib.dev.timeout = 30000
720761 self.embios.ipodnano2g_nandread(addr, start, count, doecc, checkempty)
721762 self.logger.info("done\n")
@@ -729,7 +770,8 @@
730771 start = self._hexint(start)
731772 count = self._hexint(count)
732773 doecc = int(doecc)
733 - self.logger.info("Writing "+self._hex(count)+" NAND pages starting at "+self._hex(start)+" from "+self._hex(addr)+"...")
 774+ self.logger.info("Writing " + self._hex(count) + " NAND pages starting at " + \
 775+ self._hex(start) + " from " + self._hex(addr) + "...")
734776 self.embios.lib.dev.timeout = 30000
735777 self.embios.ipodnano2g_nandwrite(addr, start, count, doecc)
736778 self.logger.info("done\n")
@@ -743,7 +785,8 @@
744786 addr = self._hexint(addr)
745787 start = self._hexint(start)
746788 count = self._hexint(count)
747 - self.logger.info("Erasing "+self._hex(count)+" NAND blocks starting at "+self._hex(start)+" and logging to "+self._hex(addr)+"...")
 789+ self.logger.info("Erasing " + self._hex(count) + " NAND blocks starting at " + \
 790+ self._hex(start) + " and logging to " + self._hex(addr) + "...")
748791 self.embios.lib.dev.timeout = 30000
749792 self.embios.ipodnano2g_nanderase(addr, start, count)
750793 self.logger.info("done\n")
@@ -763,11 +806,11 @@
764807 statusfile = open(filenameprefix+"_status.bin", 'wb')
765808 except IOError:
766809 raise ArgumentError("Can not open file for writing!")
767 - infofile.write("NAND chip type: "+self._hex(info["type"])+"\r\n")
768 - infofile.write("Number of banks: "+str(info["banks"])+"\r\n")
769 - infofile.write("Number of blocks: "+str(info["blocks"])+"\r\n")
770 - infofile.write("Number of user blocks: "+str(info["userblocks"])+"\r\n")
771 - infofile.write("Pages per block: "+str(info["pagesperblock"])+"\r\n")
 810+ infofile.write("NAND chip type: " + self._hex(info["type"]) + "\r\n")
 811+ infofile.write("Number of banks: " + str(info["banks"]) + "\r\n")
 812+ infofile.write("Number of blocks: " + str(info["blocks"]) + "\r\n")
 813+ infofile.write("Number of user blocks: "+ str(info["userblocks"]) + "\r\n")
 814+ infofile.write("Pages per block: " + str(info["pagesperblock"]) + "\r\n")
772815 self.embios.lib.dev.timeout = 30000
773816 for i in range(info["banks"] * info["blocks"] * info["pagesperblock"] / 8192):
774817 self.logger.info(".")
@@ -788,9 +831,9 @@
789832 Wipes the whole NAND chip and logs the result to a file
790833 <force>: Use this flag to suppress the 5 seconds delay
791834 """
792 - self.logger.info("Wiping the whole NAND chip!\n")
 835+ self.logger.warn("Wiping the whole NAND chip!\n")
793836 if force == False:
794 - self.logger.info("If this was not what you intended press Ctrl-C NOW")
 837+ self.logger.warn("If this was not what you intended press Ctrl-C NOW")
795838 for i in range(10):
796839 self.logger.info(".")
797840 time.sleep(1)
@@ -1038,5 +1081,8 @@
10391082 if __name__ == "__main__":
10401083 if len(sys.argv) < 2:
10411084 usage("No command specified")
1042 - interface = Commandline()
1043 - interface._parsecommand(sys.argv[1], sys.argv[2:])
\ No newline at end of file
 1085+ try:
 1086+ interface = Commandline()
 1087+ interface._parsecommand(sys.argv[1], sys.argv[2:])
 1088+ except KeyboardInterrupt:
 1089+ sys.exit()
\ No newline at end of file