Index: embios/trunk/tools/misc.py |
— | — | @@ -27,6 +27,7 @@ |
28 | 28 | """
|
29 | 29 |
|
30 | 30 | import sys
|
| 31 | +import libembiosdata
|
31 | 32 |
|
32 | 33 | class Logger(object):
|
33 | 34 | """
|
— | — | @@ -87,6 +88,14 @@ |
88 | 89 | return repr(self.value)
|
89 | 90 |
|
90 | 91 |
|
| 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 | +
|
91 | 100 | def trimdoc(docstring):
|
92 | 101 | """
|
93 | 102 | Trims whitespace from docstrings
|
Index: embios/trunk/tools/embios.py |
— | — | @@ -31,7 +31,7 @@ |
32 | 32 |
|
33 | 33 | import libembios
|
34 | 34 | import libembiosdata
|
35 | | -from misc import Error, Logger, getfuncdoc
|
| 35 | +from misc import Error, Logger, getfuncdoc, gethwname
|
36 | 36 |
|
37 | 37 |
|
38 | 38 | class NotImplementedError(Error):
|
— | — | @@ -206,10 +206,7 @@ |
207 | 207 | <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
|
208 | 208 | """
|
209 | 209 | 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)
|
214 | 211 | self.logger.info("Connected to " + \
|
215 | 212 | libembiosdata.swtypes[self.embios.lib.dev.swtypeid] + \
|
216 | 213 | " v" + str(self.embios.lib.dev.version.majorv) + \
|
Index: embios/trunk/tools/libembios.py |
— | — | @@ -26,7 +26,7 @@ |
27 | 27 | import usb.core
|
28 | 28 | import libembiosdata
|
29 | 29 |
|
30 | | -from misc import Bunch, Error
|
| 30 | +from misc import Bunch, Error, gethwname
|
31 | 31 | from functools import wraps
|
32 | 32 |
|
33 | 33 | class ArgumentError(Error):
|
— | — | @@ -45,7 +45,7 @@ |
46 | 46 | pass
|
47 | 47 |
|
48 | 48 |
|
49 | | -def command(timeout = None):
|
| 49 | +def command(timeout = None, target = None):
|
50 | 50 | """
|
51 | 51 | Decorator for all commands.
|
52 | 52 | It adds the "timeout" variable to all commands.
|
— | — | @@ -57,8 +57,11 @@ |
58 | 58 | def decorator(func):
|
59 | 59 | @wraps(func)
|
60 | 60 | def wrapper(*args, **kwargs):
|
| 61 | + self = args[0] # little cheat as it expects self being always the first argument
|
61 | 62 | # 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) + "\'")
|
63 | 66 | timeout = None
|
64 | 67 | if "timeout" in kwargs.keys():
|
65 | 68 | timeout = kwargs['timeout']
|
— | — | @@ -521,62 +524,55 @@ |
522 | 525 | """ Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination' """
|
523 | 526 | return self.lib.monitorcommand(struct.pack("IIII", 26, addr, size, destination), "III", (None, None, None))
|
524 | 527 |
|
525 | | - @command()
|
| 528 | + @command(target = 0x47324e49)
|
526 | 529 | def ipodnano2g_getnandinfo(self):
|
527 | 530 | """ Target-specific function: ipodnano2g
|
528 | 531 | Gathers some information about the NAND chip used
|
529 | 532 | """
|
530 | | - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
|
531 | 533 | return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IHHHH", ("type", "pagesperblock", "banks", "userblocks", "blocks"))
|
532 | 534 |
|
533 | | - @command(timeout = 30000)
|
| 535 | + @command(timeout = 30000, target = 0x47324e49)
|
534 | 536 | def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
|
535 | 537 | """ Target-specific function: ipodnano2g
|
536 | 538 | Reads data from the NAND chip into memory
|
537 | 539 | """
|
538 | | - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
|
539 | 540 | 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))
|
540 | 541 |
|
541 | | - @command(timeout = 30000)
|
| 542 | + @command(timeout = 30000, target = 0x47324e49)
|
542 | 543 | def ipodnano2g_nandwrite(self, addr, start, count, doecc):
|
543 | 544 | """ Target-specific function: ipodnano2g
|
544 | 545 | Writes data to the NAND chip
|
545 | 546 | """
|
546 | | - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
|
547 | 547 | return self.lib.monitorcommand(struct.pack("IIII", 0xffff0003, addr | (0x80000000 if doecc != 0 else 0), start, count), "III", (None, None, None))
|
548 | 548 |
|
549 | | - @command(timeout = 30000)
|
| 549 | + @command(timeout = 30000, target = 0x47324e49)
|
550 | 550 | def ipodnano2g_nanderase(self, addr, start, count):
|
551 | 551 | """ Target-specific function: ipodnano2g
|
552 | 552 | Erases blocks on the NAND chip and stores the results to memory
|
553 | 553 | """
|
554 | | - if self.lib.dev.hwtypeid != 0x47324e49: raise DeviceError("Wrong device for target-specific command.")
|
555 | 554 | return self.lib.monitorcommand(struct.pack("IIII", 0xffff0004, addr, start, count), "III", (None, None, None))
|
556 | 555 |
|
557 | | - @command()
|
| 556 | + @command(target = 0x4c435049)
|
558 | 557 | def ipodclassic_gethddinfo(self):
|
559 | 558 | """ Target-specific function: ipodclassic
|
560 | 559 | Gather information about the hard disk drive
|
561 | 560 | """
|
562 | | - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
|
563 | 561 | return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IQQII", ("identifyptr", "totalsectors", "virtualsectors", "bbtptr", "bbtsize"))
|
564 | 562 |
|
565 | | - @command(timeout = 30000)
|
| 563 | + @command(timeout = 30000, target = 0x4c435049)
|
566 | 564 | def ipodclassic_hddaccess(self, type, sector, count, addr):
|
567 | 565 | """ Target-specific function: ipodclassic
|
568 | 566 | Access the hard disk, type = 0 (read) / 1 (write)
|
569 | 567 | """
|
570 | | - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
|
571 | 568 | rc = self.lib.monitorcommand(struct.pack("IIQIIII", 0xffff0002, type, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
572 | 569 | if (rc > 0x80000000):
|
573 | 570 | raise DeviceError("HDD access (type=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (type, sector, count, addr, rc))
|
574 | 571 |
|
575 | | - @command()
|
| 572 | + @command(target = 0x4c435049)
|
576 | 573 | def ipodclassic_writebbt(self, bbt, tempaddr):
|
577 | 574 | """ Target-specific function: ipodclassic
|
578 | 575 | Write hard drive bad block table
|
579 | 576 | """
|
580 | | - if self.lib.dev.hwtypeid != 0x4c435049: raise DeviceError("Wrong device for target-specific command.")
|
581 | 577 | try:
|
582 | 578 | bbtheader = struct.unpack("<8s2024sQII512I", bbt[:4096])
|
583 | 579 | except struct.error:
|