freemyipod r398 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r397‎ | r398 | r399 >
Date:01:35, 30 December 2010
Author:farthen
Status:new
Tags:
Comment:
libembios.py: Implement target specific functions.
misc.py: Implement gethwname()
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)
  • /embios/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/misc.py
@@ -27,6 +27,7 @@
2828 """
2929
3030 import sys
 31+import libembiosdata
3132
3233 class Logger(object):
3334 """
@@ -87,6 +88,14 @@
8889 return repr(self.value)
8990
9091
 92+def gethwname(id):
 93+ try:
 94+ hwtype = libembiosdata.hwtypes[id]
 95+ except KeyError:
 96+ hwtype = "UNKNOWN (ID = " + self._hex(id) + ")"
 97+ return hwtype
 98+
 99+
91100 def trimdoc(docstring):
92101 """
93102 Trims whitespace from docstrings
Index: embios/trunk/tools/embios.py
@@ -31,7 +31,7 @@
3232
3333 import libembios
3434 import libembiosdata
35 -from misc import Error, Logger, getfuncdoc
 35+from misc import Error, Logger, getfuncdoc, gethwname
3636
3737
3838 class NotImplementedError(Error):
@@ -206,10 +206,7 @@
207207 <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
208208 """
209209 if infotype == "version":
210 - try:
211 - hwtype = libembiosdata.hwtypes[self.embios.lib.dev.hwtypeid]
212 - except KeyError:
213 - hwtype = "UNKNOWN (ID = " + self._hex(self.embios.lib.dev.hwtypeid) + ")"
 210+ hwtype = gethwname(self.embios.lib.dev.hwtypeid)
214211 self.logger.info("Connected to " + \
215212 libembiosdata.swtypes[self.embios.lib.dev.swtypeid] + \
216213 " v" + str(self.embios.lib.dev.version.majorv) + \
Index: embios/trunk/tools/libembios.py
@@ -26,7 +26,7 @@
2727 import usb.core
2828 import libembiosdata
2929
30 -from misc import Bunch, Error
 30+from misc import Bunch, Error, gethwname
3131 from functools import wraps
3232
3333 class ArgumentError(Error):
@@ -45,7 +45,7 @@
4646 pass
4747
4848
49 -def command(timeout = None):
 49+def command(timeout = None, target = None):
5050 """
5151 Decorator for all commands.
5252 It adds the "timeout" variable to all commands.
@@ -57,8 +57,11 @@
5858 def decorator(func):
5959 @wraps(func)
6060 def wrapper(*args, **kwargs):
 61+ self = args[0] # little cheat as it expects self being always the first argument
6162 # precommand stuff
62 - self = args[0] # little cheat as it expects self being always the first argument
 63+ if target is not None:
 64+ if self.lib.dev.hwtypeid != target:
 65+ raise DeviceError("Wrong device for target-specific command. Expected \'" + gethwname(target) + "\' but got \'" + gethwname(self.lib.dev.hwtypeid) + "\'")
6366 timeout = None
6467 if "timeout" in kwargs.keys():
6568 timeout = kwargs['timeout']
@@ -521,62 +524,55 @@
522525 """ Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination' """
523526 return self.lib.monitorcommand(struct.pack("IIII", 26, addr, size, destination), "III", (None, None, None))
524527
525 - @command()
 528+ @command(target = 0x47324e49)
526529 def ipodnano2g_getnandinfo(self):
527530 """ Target-specific function: ipodnano2g
528531 Gathers some information about the NAND chip used
529532 """
530 - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
531533 return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IHHHH", ("type", "pagesperblock", "banks", "userblocks", "blocks"))
532534
533 - @command(timeout = 30000)
 535+ @command(timeout = 30000, target = 0x47324e49)
534536 def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
535537 """ Target-specific function: ipodnano2g
536538 Reads data from the NAND chip into memory
537539 """
538 - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
539540 return self.lib.monitorcommand(struct.pack("IIII", 0xffff0002, addr | (0x80000000 if doecc != 0 else 0) | (0x40000000 if checkempty != 0 else 0), start, count), "III", (None, None, None))
540541
541 - @command(timeout = 30000)
 542+ @command(timeout = 30000, target = 0x47324e49)
542543 def ipodnano2g_nandwrite(self, addr, start, count, doecc):
543544 """ Target-specific function: ipodnano2g
544545 Writes data to the NAND chip
545546 """
546 - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
547547 return self.lib.monitorcommand(struct.pack("IIII", 0xffff0003, addr | (0x80000000 if doecc != 0 else 0), start, count), "III", (None, None, None))
548548
549 - @command(timeout = 30000)
 549+ @command(timeout = 30000, target = 0x47324e49)
550550 def ipodnano2g_nanderase(self, addr, start, count):
551551 """ Target-specific function: ipodnano2g
552552 Erases blocks on the NAND chip and stores the results to memory
553553 """
554 - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
555554 return self.lib.monitorcommand(struct.pack("IIII", 0xffff0004, addr, start, count), "III", (None, None, None))
556555
557 - @command()
 556+ @command(target = 0x4c435049)
558557 def ipodclassic_gethddinfo(self):
559558 """ Target-specific function: ipodclassic
560559 Gather information about the hard disk drive
561560 """
562 - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
563561 return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IQQII", ("identifyptr", "totalsectors", "virtualsectors", "bbtptr", "bbtsize"))
564562
565 - @command(timeout = 30000)
 563+ @command(timeout = 30000, target = 0x4c435049)
566564 def ipodclassic_hddaccess(self, type, sector, count, addr):
567565 """ Target-specific function: ipodclassic
568566 Access the hard disk, type = 0 (read) / 1 (write)
569567 """
570 - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
571568 rc = self.lib.monitorcommand(struct.pack("IIQIIII", 0xffff0002, type, sector, count, addr, 0, 0), "III", ("rc", None, None))
572569 if (rc > 0x80000000):
573570 raise DeviceError("HDD access (type=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (type, sector, count, addr, rc))
574571
575 - @command()
 572+ @command(target = 0x4c435049)
576573 def ipodclassic_writebbt(self, bbt, tempaddr):
577574 """ Target-specific function: ipodclassic
578575 Write hard drive bad block table
579576 """
580 - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
581577 try:
582578 bbtheader = struct.unpack("<8s2024sQII512I", bbt[:4096])
583579 except struct.error: