freemyipod r484 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r483‎ | r484 | r485 >
Date:18:23, 22 January 2011
Author:theseven
Status:new
Tags:
Comment:
libemcore.py: Fix some bugs and add lots of debugging output
Modified paths:
  • /emcore/trunk/tools/libemcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/libemcore.py
@@ -610,6 +610,7 @@
611611 @command()
612612 def storage_get_info(self, volume):
613613 """ Get information about a storage device """
 614+ self.logger.debug("Getting storage information\n")
614615 result = self.lib.monitorcommand(struct.pack("IIII", 27, volume, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
615616 if result.version != 1:
616617 raise ValueError("Unknown version of storage_info struct: %d" % result.version)
@@ -616,12 +617,20 @@
617618 result.vendor = self.readstring(result.vendorptr)
618619 result.product = self.readstring(result.productptr)
619620 result.revision = self.readstring(result.revisionptr)
 621+ self.logger.debug("Got storage information:\n")
 622+ self.logger.debug("Vendor: %s\n" % result.vendor)
 623+ self.logger.debug("Product: %s\n" % result.product)
 624+ self.logger.debug("Revision: %s\n" % result.revision)
 625+ self.logger.debug("Sector size: %d\n" % result.sectorsize)
 626+ self.logger.debug("Number of sectors: %d\n" % result.numsectors)
620627 return result
621628
622629 @command(timeout = 50000)
623630 def storage_read_sectors_md(self, volume, sector, count, addr):
624631 """ Read sectors from as storage device """
 632+ self.logger.debug("Reading %d sectors from disk at volume %d, sector %d to memory at 0x%x\n" % (count, volume, sector, addr)
625633 result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 634+ self.logger.debug("Read sectors, result: 0x%x\n" % result.rc)
626635 if result.rc > 0x80000000:
627636 raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
628637
@@ -628,7 +637,9 @@
629638 @command(timeout = 50000)
630639 def storage_write_sectors_md(self, volume, sector, count, addr):
631640 """ Read sectors from as storage device """
 641+ self.logger.debug("Writing %d sectors from memory at 0x%x to disk at volume %d, sector %d\n" % (count, addr, volume, sector)
632642 result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
 643+ self.logger.debug("Wrote sectors, result: 0x%x\n" % result.rc)
633644 if result.rc > 0x80000000:
634645 raise DeviceError("storage_write_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
635646
@@ -635,22 +646,30 @@
636647 @command(timeout = 30000)
637648 def fat_enable_flushing(self, state):
638649 """ Enables/disables flushing the FAT cache after every transaction """
 650+ if state != 0: self.logger.debug("Enabling FAT flushing\n")
 651+ else: self.logger.debug("Disabling FAT flushing\n")
639652 self.lib.monitorcommand(struct.pack("IIII", 58, state, 0, 0), "III", (None, None, None))
 653+ if state != 0: self.logger.debug("Enabled FAT flushing\n")
 654+ else: self.logger.debug("Disabled FAT flushing\n")
640655
641656 @command(timeout = 30000)
642657 def file_open(self, filename, mode):
643658 """ Opens a file and returns the handle """
 659+ self.logger.debug("Opening remote file %s with mode %s\n" % (filename, mode))
644660 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(filename), 30, mode, 0, 0, filename, 0), "III", ("fd", None, None))
645661 if result.fd > 0x80000000:
646662 raise DeviceError("file_open(filename=\"%s\", mode=0x%X) failed with RC=0x%08X, errno=%d" % (filename, mode, result.fd, self.errno()))
 663+ self.logger.debug("Opened file as handle 0x%x\n" % result.fd)
647664 return result.fd
648665
649666 @command(timeout = 30000)
650667 def file_size(self, fd):
651668 """ Gets the size of a file referenced by a handle """
 669+ self.logger.debug("Getting file size of handle 0x%x\n" % fd)
652670 result = self.lib.monitorcommand(struct.pack("IIII", 31, fd, 0, 0), "III", ("size", None, None))
653671 if result.size > 0x80000000:
654672 raise DeviceError("file_size(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.size, self.errno()))
 673+ self.logger.debug("Got file size: %d bytes\n" % result.size)
655674 return result.size
656675
657676 @command(timeout = 30000)
@@ -661,6 +680,7 @@
662681 malloc = True
663682 else:
664683 malloc = False
 684+ self.logger.debug("Reading %d bytes from file handle 0x%x to 0x%x\n" % (size, fd, addr))
665685 try:
666686 result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
667687 if result.rc > 0x80000000:
@@ -669,91 +689,113 @@
670690 if malloc == True:
671691 self.free(addr)
672692 raise
 693+ self.logger.debug("File read result: 0x%x\n" % result.rc)
673694 return Bunch(rc = result.rc, addr = addr)
674695
675696 @command(timeout = 30000)
676697 def file_write(self, fd, size, addr):
677698 """ Writes data from a file referenced by a handle. """
 699+ self.logger.debug("Writing %d bytes from 0x%x to file handle 0x%x\n" % (size, addr, fd))
678700 result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
679701 if result.rc > 0x80000000:
680702 raise DeviceError("file_write(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
 703+ self.logger.debug("File write result: 0x%x\n" % result.rc)
681704 return result.rc
682705
683706 @command(timeout = 30000)
684707 def file_seek(self, fd, offset, whence):
685708 """ Seeks the file handle to the specified position in the file """
 709+ self.logger.debug("Seeking file handle 0x%x to whence=%d, offset=0x%x\n" % (fd, whence, offset))
686710 result = self.lib.monitorcommand(struct.pack("IIII", 34, fd, offset, whence), "III", ("rc", None, None))
687711 if result.rc > 0x80000000:
688712 raise DeviceError("file_seek(fd=%d, offset=0x%08X, whence=%d) failed with RC=0x%08X, errno=%d" % (fd, offset, whence, result.rc, self.errno()))
 713+ self.logger.debug("File seek result: 0x%x\n" % (result.rc))
689714 return result.rc
690715
691716 @command(timeout = 30000)
692717 def file_truncate(self, fd, length):
693718 """ Truncates a file referenced by a handle to a specified length """
 719+ self.logger.debug("Truncating file with handle 0x%x to 0x%x bytes\n" % (fd, length))
694720 result = self.lib.monitorcommand(struct.pack("IIII", 35, fd, offset, 0), "III", ("rc", None, None))
695721 if result.rc > 0x80000000:
696722 raise DeviceError("file_truncate(fd=%d, length=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, length, result.rc, self.errno()))
 723+ self.logger.debug("File truncate result: 0x%x\n" % (result.rc))
697724 return result.rc
698725
699726 @command(timeout = 30000)
700727 def file_sync(self, fd):
701728 """ Flushes a file handles' buffers """
 729+ self.logger.debug("Flushing buffers of file with handle 0x%x\n" % (fd))
702730 result = self.lib.monitorcommand(struct.pack("IIII", 36, fd, 0, 0), "III", ("rc", None, None))
703731 if result.rc > 0x80000000:
704732 raise DeviceError("file_sync(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
 733+ self.logger.debug("File flush result: 0x%x\n" % (result.rc))
705734 return result.rc
706735
707736 @command(timeout = 30000)
708737 def file_close(self, fd):
709738 """ Closes a file handle """
 739+ self.logger.debug("Closing file handle 0x%x\n" % (fd))
710740 result = self.lib.monitorcommand(struct.pack("IIII", 37, fd, 0, 0), "III", ("rc", None, None))
711741 if result.rc > 0x80000000:
712742 raise DeviceError("file_close(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
 743+ self.logger.debug("File close result: 0x%x\n" % (result.rc))
713744 return result.rc
714745
715746 @command(timeout = 30000)
716747 def file_close_all(self):
717748 """ Closes all file handles opened through the debugger """
 749+ self.logger.debug("Closing all files that were opened via USB\n")
718750 result = self.lib.monitorcommand(struct.pack("IIII", 38, 0, 0, 0), "III", ("rc", None, None))
719751 if result.rc > 0x80000000:
720752 raise DeviceError("file_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
 753+ self.logger.debug("Closed %d files\n" % (result.rc))
721754 return result.rc
722755
723756 @command(timeout = 30000)
724 - def file_kill_all(self):
725 - """ Kills all file handles (in the whole system) """
726 - result = self.lib.monitorcommand(struct.pack("IIII", 39, 0, 0, 0), "III", ("rc", None, None))
 757+ def file_kill_all(self, volume):
 758+ """ Kills all file handles of a volume (in the whole system) """
 759+ self.logger.debug("Killing all file handles of volume %d\n" % (volume))
 760+ result = self.lib.monitorcommand(struct.pack("IIII", 39, volume, 0, 0), "III", ("rc", None, None))
727761 if result.rc > 0x80000000:
728762 raise DeviceError("file_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
 763+ self.logger.debug("Closed %d files\n" % (result.rc))
729764 return result.rc
730765
731766 @command(timeout = 30000)
732767 def file_unlink(self, filename):
733768 """ Removes a file """
 769+ self.logger.debug("Deleting file %s\n" % (filename))
734770 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(filename), 40, 0, 0, 0, filename, 0), "III", ("rc", None, None))
735771 if result.rc > 0x80000000:
736772 raise DeviceError("file_unlink(filename=\"%s\") failed with RC=0x%08X, errno=%d" % (filename, result.rc, self.errno()))
 773+ self.logger.debug("Delete file result: 0x%x\n" % (result.rc))
737774 return result.rc
738775
739776 @command(timeout = 30000)
740777 def file_rename(self, oldname, newname):
741778 """ Renames a file """
 779+ self.logger.debug("Renaming file %s to %s\n" % (oldname, newname))
742780 result = self.lib.monitorcommand(struct.pack("IIII248s%dsB" % min(247, len(newname)), 41, 0, 0, 0, oldname, newname, 0), "III", ("rc", None, None))
743781 if result.rc > 0x80000000:
744782 raise DeviceError("file_rename(oldname=\"%s\", newname=\"%s\") failed with RC=0x%08X, errno=%d" % (oldname, newname, result.rc, self.errno()))
 783+ self.logger.debug("Rename file result: 0x%x\n" % (result.rc))
745784 return result.rc
746785
747786 @command(timeout = 30000)
748787 def dir_open(self, dirname):
749788 """ Opens a directory and returns the handle """
 789+ self.logger.debug("Opening directory %s\n" % (dirname))
750790 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 42, 0, 0, 0, dirname, 0), "III", ("handle", None, None))
751791 if result.handle == 0:
752792 raise DeviceError("dir_open(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.handle, self.errno()))
 793+ self.logger.debug("Opened directory as handle 0x%x\n" % (result.handle))
753794 return result.handle
754795
755796 @command(timeout = 30000)
756797 def dir_read(self, handle):
757798 """ Reads the next entry from a directory """
 799+ self.logger.debug("Reading next entry of directory handle 0x%x\n" % (handle))
758800 result = self.lib.monitorcommand(struct.pack("IIII", 43, handle, 0, 0), "III", ("version", "maxpath", "ptr"))
759801 if result.ptr == 0:
760802 raise DeviceError("dir_read(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.ptr, self.errno()))
@@ -763,68 +805,91 @@
764806 ret = Bunch()
765807 (ret.name, ret.attributes, ret.size, ret.startcluster, ret.wrtdate, ret.wrttime) = struct.unpack("%dsIIIHH" % result.maxpath, dirent)
766808 ret.name = ret.name[:ret.name.index('\x00')]
 809+ self.logger.debug("Read directory entry:\n")
 810+ self.logger.debug("Name: %s\n" % ret.name)
 811+ self.logger.debug("Attributes: 0x%x\n" % ret.attributes)
 812+ self.logger.debug("Size: %d\n" % ret.size)
 813+ self.logger.debug("Start cluster: %d\n" % ret.startcluster)
 814+ self.logger.debug("Last written date: 0x%x\n" % ret.wrtdate)
 815+ self.logger.debug("Last written time: 0x%x\n" % ret.wrttime)
767816 return ret
768817
769818 @command(timeout = 30000)
770819 def dir_close(self, handle):
771820 """ Closes a directory handle """
 821+ self.logger.debug("Closing directory handle 0x%x\n" % (handle))
772822 result = self.lib.monitorcommand(struct.pack("IIII", 44, handle, 0, 0), "III", ("rc", None, None))
773823 if result.rc > 0x80000000:
774824 raise DeviceError("dir_close(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.rc, self.errno()))
 825+ self.logger.debug("Close directory result: 0x%x\n" % (result.rc))
775826 return result.rc
776827
777828 @command(timeout = 30000)
778829 def dir_close_all(self):
779830 """ Closes all directory handles opened through the debugger """
 831+ self.logger.debug("Closing all directories that were opened via USB\n")
780832 result = self.lib.monitorcommand(struct.pack("IIII", 45, 0, 0, 0), "III", ("rc", None, None))
781833 if result.rc > 0x80000000:
782834 raise DeviceError("dir_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
 835+ self.logger.debug("Closed %d directories\n" % (result.rc))
783836 return result.rc
784837
785838 @command(timeout = 30000)
786 - def dir_kill_all(self):
787 - """ Kills all directory handles (in the whole system) """
788 - result = self.lib.monitorcommand(struct.pack("IIII", 46, 0, 0, 0), "III", ("rc", None, None))
 839+ def dir_kill_all(self, volume):
 840+ """ Kills all directory handles of a volume (in the whole system) """
 841+ self.logger.debug("Closing all directories of volume %d\n" % (volume))
 842+ result = self.lib.monitorcommand(struct.pack("IIII", 46, volume, 0, 0), "III", ("rc", None, None))
789843 if result.rc > 0x80000000:
790844 raise DeviceError("dir_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
 845+ self.logger.debug("Closed %d directories\n" % (result.rc))
791846 return result.rc
792847
793848 @command(timeout = 30000)
794849 def dir_create(self, dirname):
795850 """ Creates a directory """
 851+ self.logger.debug("Creating directory %s\n" % (dirname))
796852 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 47, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
797853 if result.rc > 0x80000000:
798854 raise DeviceError("dir_create(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
 855+ self.logger.debug("Create directory result: 0x%x\n" % (result.rc))
799856 return result.rc
800857
801858 @command(timeout = 30000)
802859 def dir_remove(self, dirname):
803860 """ Removes an (empty) directory """
 861+ self.logger.debug("Removing directory %s\n" % (dirname))
804862 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 48, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
805863 if result.rc > 0x80000000:
806864 raise DeviceError("dir_remove(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
 865+ self.logger.debug("Remove directory result: 0x%x\n" % (result.rc))
807866 return result.rc
808867
809868 @command()
810869 def errno(self):
811870 """ Returns the number of the last error that happened """
 871+ self.logger.debug("Getting last error number\n")
812872 result = self.lib.monitorcommand(struct.pack("IIII", 49, 0, 0, 0), "III", ("errno", None, None))
 873+ self.logger.debug("Last error: 0x%x\n" % (result.errno))
813874 return result.errno
814875
815876 @command()
816877 def disk_mount(self, volume):
817878 """ Mounts a volume """
 879+ self.logger.debug("Mounting volume %d\n" % (volume))
818880 result = self.lib.monitorcommand(struct.pack("IIII", 50, volume, 0, 0), "III", ("rc", None, None))
819881 if result.rc > 0x80000000:
820882 raise DeviceError("disk_mount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
 883+ self.logger.debug("Mount volume result: 0x%x\n" % (result.rc))
821884 return result.rc
822885
823886 @command()
824887 def disk_unmount(self, volume):
825888 """ Unmounts a volume """
 889+ self.logger.debug("Unmounting volume %d\n" % (volume))
826890 result = self.lib.monitorcommand(struct.pack("IIII", 51, volume, 0, 0), "III", ("rc", None, None))
827891 if result.rc > 0x80000000:
828892 raise DeviceError("disk_unmount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
 893+ self.logger.debug("Unmount volume result: 0x%x\n" % (result.rc))
829894 return result.rc
830895
831896 @command()