freemyipod r787 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r786‎ | r787 | r788 >
Date:15:45, 18 November 2011
Author:theseven
Status:new
Tags:
Comment:
(lib)emcore.py: Add ipodclassic_readbbt command, make ipodclassic_writebbt work with Python 3
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -19,7 +19,7 @@
2020 # You should have received a copy of the GNU General Public License
2121 # along with emCORE. If not, see <http://www.gnu.org/licenses/>.
2222 #
23 -#
 23+#
2424
2525 """
2626 Command line interface to communicate with emCORE devices.
@@ -896,6 +896,23 @@
897897 self.emcore.free(buf)
898898
899899 @command
 900+ def ipodclassic_readbbt(self, filename, tempaddr = None):
 901+ """
 902+ Target-specific function: ipodclassic
 903+ Reads the bad block table from the hard disk to memory at <tempaddr>
 904+ (or an allocated block if not given) and writes it to <filename>
 905+ """
 906+ tempaddr = to_int(tempaddr)
 907+ try:
 908+ f = open(filename, 'wb')
 909+ except IOError:
 910+ raise ArgumentError("File not writable.")
 911+ self.logger.info("Reading bad block table from disk...")
 912+ f.write(self.emcore.ipodclassic_readbbt(tempaddr))
 913+ f.close()
 914+ self.logger.info(" done\n")
 915+
 916+ @command
900917 def ipodclassic_writebbt(self, filename, tempaddr = None):
901918 """
902919 Target-specific function: ipodclassic
Index: emcore/trunk/tools/libemcore.py
@@ -586,6 +586,50 @@
587587 rc = self.lib.monitorcommand(struct.pack("<IIII", 0xffff0004, 0, 0, 0), "III", (None, None, None))
588588
589589 @command(target = 0x4c435049)
 590+ def ipodclassic_readbbt(self, tempaddr = None):
 591+ """ Target-specific function: ipodclassic
 592+ Read hard drive bad block table
 593+ """
 594+ if tempaddr is None:
 595+ tempaddr = self.memalign(0x10, 4096)
 596+ malloc = True
 597+ else:
 598+ malloc = False
 599+ try:
 600+ self.ipodclassic_hddaccess(0, 0, 1, tempaddr)
 601+ bbt = self.read(tempaddr, 4096)
 602+ finally:
 603+ if malloc == True:
 604+ self.free(tempaddr)
 605+ try:
 606+ bbtheader = struct.unpack("<8s2024sQII512I", bbt)
 607+ except struct.error:
 608+ raise ArgumentError("There is no emCORE hard disk BBT present on this device")
 609+ if bbtheader[0] != b"emBIbbth":
 610+ raise ArgumentError("There is no emCORE hard disk BBT present on this device")
 611+ bbtsectors = bbtheader[4]
 612+ if malloc:
 613+ tempaddr = self.memalign(0x10, 4096 * bbtsectors)
 614+ try:
 615+ sector = 1
 616+ count = 0
 617+ offset = 0
 618+ for i in range(0, bbtsectors):
 619+ if bbtheader[5 + i] == sector + count:
 620+ count = count + 1
 621+ else:
 622+ self.ipodclassic_hddaccess(0, sector, count, tempaddr + offset)
 623+ offset = offset + count * 4096
 624+ sector = bbtheader[5 + i]
 625+ count = 1
 626+ self.ipodclassic_hddaccess(0, sector, count, tempaddr + offset)
 627+ bbt += self.read(tempaddr, 4096 * bbtsectors)
 628+ finally:
 629+ if malloc == True:
 630+ self.free(tempaddr)
 631+ return bbt
 632+
 633+ @command(target = 0x4c435049)
590634 def ipodclassic_writebbt(self, bbt, tempaddr = None):
591635 """ Target-specific function: ipodclassic
592636 Write hard drive bad block table
@@ -594,12 +638,13 @@
595639 bbtheader = struct.unpack("<8s2024sQII512I", bbt[:4096])
596640 except struct.error:
597641 raise ArgumentError("The specified file is not an emCORE hard disk BBT")
598 - if bbtheader[0] != "emBIbbth":
 642+ if bbtheader[0] != b"emBIbbth":
599643 raise ArgumentError("The specified file is not an emCORE hard disk BBT")
600 - virtualsectors = bbtheader[2]
601644 bbtsectors = bbtheader[4]
 645+ if (bbtsectors + 1) * 4096 != len(bbt):
 646+ raise ArgumentError("Size of BBT is not consistent: Expected %d bytes, got %d" % ((bbtsectors + 1) * 4096, len(bbt)))
602647 if tempaddr is None:
603 - tempaddr = self.malloc(len(bbt))
 648+ tempaddr = self.memalign(0x10, len(bbt))
604649 malloc = True
605650 else:
606651 malloc = False
@@ -615,7 +660,7 @@
616661 else:
617662 self.ipodclassic_hddaccess(1, sector, count, tempaddr + offset)
618663 offset = offset + count * 4096
619 - sector = bbtheader[5 +i]
 664+ sector = bbtheader[5 + i]
620665 count = 1
621666 self.ipodclassic_hddaccess(1, sector, count, tempaddr + offset)
622667 self.ipodclassic_reloadbbt()