freemyipod r379 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r378‎ | r379 | r380 >
Date:04:24, 23 December 2010
Author:theseven
Status:new
Tags:
Comment:
embios.py: Add lowlevel storage access functions
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -825,6 +825,107 @@
826826 self.logger.info(" done\n")
827827
828828 @command
 829+ def getvolumeinfo(self, volume):
 830+ """
 831+ Gathers some information about a storage volume used
 832+ """
 833+ volume = self._hexint(volume)
 834+ data = self.embios.storage_get_info(volume)
 835+ self.logger.info("Sector size: "+str(data["sectorsize"])+"\n")
 836+ self.logger.info("Number of sectors: "+str(data["numsectors"])+"\n")
 837+ self.logger.info("Vendor: "+data["vendor"]+"\n")
 838+ self.logger.info("Product: "+data["product"]+"\n")
 839+ self.logger.info("Revision: "+data["revision"])
 840+
 841+ @command
 842+ def readrawstorage(self, volume, sector, count, addr):
 843+ """
 844+ Reads <count> sectors starting at <sector> from storage <volume> to memory at <addr>.
 845+ """
 846+ volume = self._hexint(volume)
 847+ sector = self._hexint(sector)
 848+ count = self._hexint(count)
 849+ addr = self._hexint(addr)
 850+ self.logger.info("Reading volume %s sectors %X - %X to %08X..." % (volume, sector, sector + count - 1, addr))
 851+ self.embios.lib.dev.timeout = 50000
 852+ self.embios.storage_read_sectors_md(volume, sector, count, addr)
 853+ self.logger.info("done\n")
 854+
 855+ @command
 856+ def writerawstorage(self, volume, sector, count, addr):
 857+ """
 858+ Writes memory contents at <addr> to <count> sectors starting at <sector> on storage <volume>.
 859+ """
 860+ volume = self._hexint(volume)
 861+ sector = self._hexint(sector)
 862+ count = self._hexint(count)
 863+ addr = self._hexint(addr)
 864+ self.logger.info("Writing %08X to volume %s sectors %X - %X..." % (addr, volume, sector, sector + count - 1))
 865+ self.embios.lib.dev.timeout = 50000
 866+ self.embios.storage_write_sectors_md(volume, sector, count, addr)
 867+ self.logger.info("done\n")
 868+
 869+ @command
 870+ def readrawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
 871+ """
 872+ Reads <count> sectors starting at <sector> from storage <volume> to file <file>,
 873+ buffering them in memory at <buffer> in chunks of <buffsize> bytes (both optional).
 874+ """
 875+ volume = self._hexint(volume)
 876+ sector = self._hexint(sector)
 877+ count = self._hexint(count)
 878+ if buffer == False: buffer = self.embios.lib.dev.usermem.lower
 879+ else: buffer = self._hexint(buffer)
 880+ buffsize = self._hexint(buffsize)
 881+ try:
 882+ f = open(file, 'wb')
 883+ except IOError:
 884+ raise ArgumentError("Could not open local file for writing.")
 885+ self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
 886+ self.embios.lib.dev.timeout = 50000
 887+ storageinfo = self.embios.storage_get_info(volume)
 888+ while count > 0:
 889+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 890+ self.embios.storage_read_sectors_md(volume, sector, sectors, buffer)
 891+ f.write(self.embios.read(buffer, storageinfo.sectorsize * sectors))
 892+ sector = sector + sectors
 893+ count = count - sectors
 894+ f.close()
 895+ self.logger.info("done\n")
 896+
 897+ @command
 898+ def writerawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
 899+ """
 900+ Writes contents of <file> to <count> sectors starting at <sector> on storage <volume>,
 901+ buffering them in memory at <buffer> in chunks of <buffsize> bytes (both optional).
 902+ """
 903+ volume = self._hexint(volume)
 904+ sector = self._hexint(sector)
 905+ count = self._hexint(count)
 906+ if buffer == False: buffer = self.embios.lib.dev.usermem.lower
 907+ else: buffer = self._hexint(buffer)
 908+ buffsize = self._hexint(buffsize)
 909+ try:
 910+ f = open(file, 'rb')
 911+ except IOError:
 912+ raise ArgumentError("Could not open local file for reading.")
 913+ self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
 914+ self.embios.lib.dev.timeout = 50000
 915+ storageinfo = self.embios.storage_get_info(volume)
 916+ while count > 0:
 917+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 918+ bytes = storageinfo.sectorsize * sectors
 919+ data = f.read(bytes)
 920+ if len(data) == 0: break
 921+ while len(data) < bytes: data = data + f.read(bytes - len(data))
 922+ self.embios.write(buffer, data)
 923+ self.embios.storage_write_sectors_md(volume, sector, sectors, buffer)
 924+ sector = sector + sectors
 925+ count = count - sectors
 926+ f.close()
 927+ self.logger.info("done\n")
 928+
 929+ @command
829930 def mkdir(self, dirname):
830931 """
831932 Creates a directory
Index: embios/trunk/tools/libembios.py
@@ -538,11 +538,14 @@
539539 count = 1
540540 self.ipodclassic_hddaccess(1, sector, count, tempaddr + offset)
541541
542 - def storage_get_info(self):
 542+ def storage_get_info(self, volume):
543543 """ Get information about a storage device """
544 - result = self.lib.monitorcommand(struct.pack("IIII", 27, 0, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
 544+ result = self.lib.monitorcommand(struct.pack("IIII", 27, volume, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
545545 if result.version != 1:
546546 raise ValueError("Unknown version of storage_info struct: %d" % result.version)
 547+ result.vendor = self.readstring(result.vendorptr)
 548+ result.product = self.readstring(result.productptr)
 549+ result.revision = self.readstring(result.revisionptr)
547550 return result
548551
549552 def storage_read_sectors_md(self, volume, sector, count, addr):