freemyipod r479 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r478‎ | r479 | r480 >
Date:14:40, 22 January 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Fix file I/O via USB
Modified paths:
  • /emcore/trunk/export/syscallapi.h (modified) (history)
  • /emcore/trunk/export/syscallwrappers.h (modified) (history)
  • /emcore/trunk/fat.c (modified) (history)
  • /emcore/trunk/fat.h (modified) (history)
  • /emcore/trunk/syscallapi.c (modified) (history)
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)
  • /emcore/trunk/usb/usb.c (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -886,29 +886,31 @@
887887 sector = self._hexint(sector)
888888 count = self._hexint(count)
889889 buffsize = self._hexint(buffsize)
890 - if buffer is None:
891 - buffer = self.emcore.malloc(buffsize)
892 - malloc = True
893 - else:
894 - buffer = self._hexint(buffer)
895 - malloc = False
896890 try:
 891+ f = open(file, 'wb')
 892+ except IOError:
 893+ raise ArgumentError("Could not open local file for writing.")
 894+ try:
 895+ if buffer is None:
 896+ buffer = self.emcore.malloc(buffsize)
 897+ malloc = True
 898+ else:
 899+ buffer = self._hexint(buffer)
 900+ malloc = False
897901 try:
898 - f = open(file, 'wb')
899 - except IOError:
900 - raise ArgumentError("Could not open local file for writing.")
901 - self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
902 - storageinfo = self.emcore.storage_get_info(volume)
903 - while count > 0:
904 - sectors = min(count, int(buffsize / storageinfo.sectorsize))
905 - self.emcore.storage_read_sectors_md(volume, sector, sectors, buffsize, buffer)
906 - f.write(self.emcore.read(buffer, storageinfo.sectorsize * sectors))
907 - sector = sector + sectors
908 - count = count - sectors
 902+ self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
 903+ storageinfo = self.emcore.storage_get_info(volume)
 904+ while count > 0:
 905+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 906+ self.emcore.storage_read_sectors_md(volume, sector, sectors, buffsize, buffer)
 907+ f.write(self.emcore.read(buffer, storageinfo.sectorsize * sectors))
 908+ sector = sector + sectors
 909+ count = count - sectors
 910+ finally:
 911+ if malloc == True:
 912+ self.emcore.free(buffer)
 913+ finally:
909914 f.close()
910 - finally:
911 - if malloc == True:
912 - self.emcore.free(buffer)
913915 self.logger.info("done\n")
914916
915917 @command
@@ -921,33 +923,35 @@
922924 sector = self._hexint(sector)
923925 count = self._hexint(count)
924926 buffsize = self._hexint(buffsize)
925 - if buffer is None:
926 - buffer = self.emcore.malloc(buffsize)
927 - malloc = True
928 - else:
929 - buffer = self._hexint(buffer)
930 - malloc = False
931927 try:
 928+ f = open(file, 'rb')
 929+ except IOError:
 930+ raise ArgumentError("Could not open local file for reading.")
 931+ try:
 932+ if buffer is None:
 933+ buffer = self.emcore.malloc(buffsize)
 934+ malloc = True
 935+ else:
 936+ buffer = self._hexint(buffer)
 937+ malloc = False
932938 try:
933 - f = open(file, 'rb')
934 - except IOError:
935 - raise ArgumentError("Could not open local file for reading.")
936 - self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
937 - storageinfo = self.emcore.storage_get_info(volume)
938 - while count > 0:
939 - sectors = min(count, int(buffsize / storageinfo.sectorsize))
940 - bytes = storageinfo.sectorsize * sectors
941 - data = f.read(bytes)
942 - if len(data) == 0: break
943 - while len(data) < bytes: data = data + f.read(bytes - len(data))
944 - self.emcore.write(buffer, data)
945 - self.emcore.storage_write_sectors_md(volume, sector, sectors, buffsize, buffer)
946 - sector = sector + sectors
947 - count = count - sectors
 939+ self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
 940+ storageinfo = self.emcore.storage_get_info(volume)
 941+ while count > 0:
 942+ sectors = min(count, int(buffsize / storageinfo.sectorsize))
 943+ bytes = storageinfo.sectorsize * sectors
 944+ data = f.read(bytes)
 945+ if len(data) == 0: break
 946+ while len(data) < bytes: data = data + f.read(bytes - len(data))
 947+ self.emcore.write(buffer, data)
 948+ self.emcore.storage_write_sectors_md(volume, sector, sectors, buffsize, buffer)
 949+ sector = sector + sectors
 950+ count = count - sectors
 951+ finally:
 952+ if malloc == True:
 953+ self.emcore.free(buffer)
 954+ finally:
948955 f.close()
949 - finally:
950 - if malloc == True:
951 - self.emcore.free(buffer)
952956 self.logger.info("done\n")
953957
954958 @command
@@ -1014,29 +1018,33 @@
10151019 [buffer]: buffer address (optional)
10161020 """
10171021 buffsize = self._hexint(buffsize)
1018 - if buffer is None:
1019 - buffer = self.emcore.malloc(buffsize)
1020 - malloc = True
1021 - else:
1022 - buffer = self._hexint(buffer)
1023 - malloc = False
10241022 try:
 1023+ f = open(localname, 'wb')
 1024+ except IOError:
 1025+ raise ArgumentError("Could not open local file for writing.")
 1026+ try:
 1027+ if buffer is None:
 1028+ buffer = self.emcore.malloc(buffsize)
 1029+ malloc = True
 1030+ else:
 1031+ buffer = self._hexint(buffer)
 1032+ malloc = False
10251033 try:
1026 - f = open(localname, 'wb')
1027 - except IOError:
1028 - raise ArgumentError("Could not open local file for writing.")
1029 - self.logger.info("Downloading file " + remotename + " to " + localname + "...")
1030 - fd = self.emcore.file_open(remotename, 0)
1031 - size = self.emcore.file_size(fd)
1032 - while size > 0:
1033 - bytes = self.emcore.file_read(fd, buffsize, buffer)
1034 - f.write(self.emcore.read(buffer, bytes))
1035 - size = size - bytes
1036 - self.emcore.file_close(fd)
 1034+ self.logger.info("Downloading file " + remotename + " to " + localname + "...")
 1035+ fd = self.emcore.file_open(remotename, 0)
 1036+ try:
 1037+ size = self.emcore.file_size(fd)
 1038+ while size > 0:
 1039+ bytes = self.emcore.file_read(fd, buffsize, buffer)
 1040+ f.write(self.emcore.read(buffer, bytes))
 1041+ size = size - bytes
 1042+ finally:
 1043+ self.emcore.file_close(fd)
 1044+ finally:
 1045+ if malloc == True:
 1046+ self.emcore.free(buffer)
 1047+ finally:
10371048 f.close()
1038 - finally:
1039 - if malloc == True:
1040 - self.emcore.free(buffer)
10411049 self.logger.info(" done\n")
10421050
10431051 @command
@@ -1049,29 +1057,30 @@
10501058 [buffer]: buffer address (optional)
10511059 """
10521060 buffsize = self._hexint(buffsize)
1053 - if buffer is None:
1054 - buffer = self.emcore.malloc(buffsize)
1055 - malloc = True
1056 - else:
1057 - buffer = self._hexint(buffer)
1058 - malloc = False
 1061+ handle = self.emcore.dir_open(remotepath)
10591062 try:
1060 - try: os.mkdir(localpath)
1061 - except: pass
1062 -
1063 - handle = self.emcore.dir_open(remotepath)
1064 - while True:
1065 - try:
1066 - entry = self.emcore.dir_read(handle)
1067 - if entry.name == "." or entry.name == "..": continue
1068 - elif entry.attributes & 0x10:
1069 - self.gettree(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffsize, buffer)
1070 - else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffsize, buffer)
1071 - except: break
 1063+ if buffer is None:
 1064+ buffer = self.emcore.malloc(buffsize)
 1065+ malloc = True
 1066+ else:
 1067+ buffer = self._hexint(buffer)
 1068+ malloc = False
 1069+ try:
 1070+ try: os.mkdir(localpath)
 1071+ except: pass
 1072+ while True:
 1073+ try:
 1074+ entry = self.emcore.dir_read(handle)
 1075+ if entry.name == "." or entry.name == "..": continue
 1076+ elif entry.attributes & 0x10:
 1077+ self.gettree(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffsize, buffer)
 1078+ else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffsize, buffer)
 1079+ except: break
 1080+ finally:
 1081+ if malloc == True:
 1082+ self.emcore.free(buffer)
 1083+ finally:
10721084 self.emcore.dir_close(handle)
1073 - finally:
1074 - if malloc == True:
1075 - self.emcore.free(buffer)
10761085
10771086 @command
10781087 def put(self, localname, remotename, buffsize = 0x10000, buffer = None):
@@ -1083,31 +1092,35 @@
10841093 [buffer]: buffer address (optional)
10851094 """
10861095 buffsize = self._hexint(buffsize)
1087 - if buffer is None:
1088 - buffer = self.emcore.malloc(buffsize)
1089 - malloc = True
1090 - else:
1091 - buffer = self._hexint(buffer)
1092 - malloc = False
10931096 try:
 1097+ f = open(localname, 'rb')
 1098+ except IOError:
 1099+ raise ArgumentError("Could not open local file for reading.")
 1100+ try:
 1101+ if buffer is None:
 1102+ buffer = self.emcore.malloc(buffsize)
 1103+ malloc = True
 1104+ else:
 1105+ buffer = self._hexint(buffer)
 1106+ malloc = False
10941107 try:
1095 - f = open(localname, 'rb')
1096 - except IOError:
1097 - raise ArgumentError("Could not open local file for reading.")
1098 - self.logger.info("Uploading file " + localname + " to " + remotename + "...\n")
1099 - fd = self.emcore.file_open(remotename, 0x15)
1100 - while True:
1101 - data = f.read(buffsize)
1102 - if len(data) == 0: break
1103 - self.emcore.write(buffer, data)
1104 - bytes = 0
1105 - while bytes < len(data):
1106 - bytes = bytes + self.emcore.file_write(fd, len(data) - bytes, buffer + bytes)
1107 - self.emcore.file_close(fd)
 1108+ self.logger.info("Uploading file " + localname + " to " + remotename + "...")
 1109+ fd = self.emcore.file_open(remotename, 0x15)
 1110+ try:
 1111+ while True:
 1112+ data = f.read(buffsize)
 1113+ if len(data) == 0: break
 1114+ self.emcore.write(buffer, data)
 1115+ bytes = 0
 1116+ while bytes < len(data):
 1117+ bytes = bytes + self.emcore.file_write(fd, len(data) - bytes, buffer + bytes)
 1118+ finally:
 1119+ self.emcore.file_close(fd)
 1120+ finally:
 1121+ if malloc == True:
 1122+ self.emcore.free(buffer)
 1123+ finally:
11081124 f.close()
1109 - finally:
1110 - if malloc == True:
1111 - self.emcore.free(buffer)
11121125 self.logger.info(" done\n")
11131126
11141127 @command
@@ -1137,7 +1150,7 @@
11381151 try: self.mkdir(prefix + dir)
11391152 except: self.logger.info(" failed\n")
11401153 for f in d[2]:
1141 - if not prefix.find("/.svn/") > -1:
 1154+ if prefix.find("/.svn/") == -1:
11421155 self.put(d[0] + "/" + f, prefix + f, buffsize, buffer)
11431156 finally:
11441157 if malloc == True:
Index: emcore/trunk/tools/libemcore.py
@@ -222,6 +222,7 @@
223223 din_maxsize = self.lib.dev.packetsizelimit.din
224224 data = ""
225225 (headsize, bodysize, tailsize) = self._alignsplit(addr, size, cin_maxsize, 16)
 226+ self.logger.debug("Downloading %d bytes from 0x%x, split as (%d/%d/%d)\n" % (size, addr, headsize, bodysize, tailsize))
226227 if headsize != 0:
227228 data += self._readmem(addr, headsize)
228229 addr += headsize
@@ -247,6 +248,7 @@
248249 cout_maxsize = self.lib.dev.packetsizelimit.cout - self.lib.headersize
249250 dout_maxsize = self.lib.dev.packetsizelimit.dout
250251 (headsize, bodysize, tailsize) = self._alignsplit(addr, len(data), cout_maxsize, 16)
 252+ self.logger.debug("Uploading %d bytes to 0x%x, split as (%d/%d/%d)\n" % (len(data), addr, headsize, bodysize, tailsize))
251253 offset = 0
252254 if headsize != 0:
253255 self._writemem(addr, data[offset:offset+headsize])
@@ -272,7 +274,6 @@
273275 Returns the address where 'data' is stored
274276 """
275277 addr = self.malloc(len(data))
276 - self.logger.debug("Uploading %d bytes to 0x%x\n" % (len(data), addr))
277278 self.write(addr, data)
278279 return addr
279280
@@ -618,37 +619,25 @@
619620 return result
620621
621622 @command(timeout = 50000)
622 - def storage_read_sectors_md(self, volume, sector, count, size = 0x100000, addr = None):
623 - """ Read sectors from as storage device. If addr is not given it allocates a buffer itself. """
624 - if addr is None:
625 - addr = self.malloc(size)
626 - malloc = True
627 - else:
628 - malloc = False
629 - try:
630 - result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
631 - finally:
632 - if malloc == True:
633 - self.free(addr)
 623+ def storage_read_sectors_md(self, volume, sector, count, addr):
 624+ """ Read sectors from as storage device """
 625+ result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
634626 if result.rc > 0x80000000:
635627 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))
636 -
637 - def storage_write_sectors_md(self, volume, sector, count, size = 0x100000, addr = None):
638 - """ Read sectors from as storage device. If addr is not given it allocates a buffer itself. """
639 - if addr is None:
640 - addr = self.malloc(size)
641 - malloc = True
642 - else:
643 - malloc = False
644 - try:
645 - result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
646 - finally:
647 - if malloc == True:
648 - self.free(addr)
 628+
 629+ @command(timeout = 50000)
 630+ def storage_write_sectors_md(self, volume, sector, count, addr):
 631+ """ Read sectors from as storage device """
 632+ result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
649633 if result.rc > 0x80000000:
650 - 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))
 634+ 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))
651635
652636 @command(timeout = 30000)
 637+ def fat_enable_flushing(self, state):
 638+ """ Enables/disables flushing the FAT cache after every transaction """
 639+ self.lib.monitorcommand(struct.pack("IIII", 58, state, 0, 0), "III", (None, None, None))
 640+
 641+ @command(timeout = 30000)
653642 def file_open(self, filename, mode):
654643 """ Opens a file and returns the handle """
655644 result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(filename), 30, mode, 0, 0, filename, 0), "III", ("fd", None, None))
@@ -665,7 +654,7 @@
666655 return result.size
667656
668657 @command(timeout = 30000)
669 - def file_read(self, fd, size = 0x100000, addr = None):
 658+ def file_read(self, fd, size = 0x10000, addr = None):
670659 """ Reads data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
671660 if addr is None:
672661 addr = self.malloc(size)
@@ -674,26 +663,18 @@
675664 malloc = False
676665 try:
677666 result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
678 - finally:
 667+ if result.rc > 0x80000000:
 668+ raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
 669+ except:
679670 if malloc == True:
680671 self.free(addr)
681 - if result.rc > 0x80000000:
682 - raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
683 - return result.rc
 672+ raise
 673+ return Bunch(rc = result.rc, addr = addr)
684674
685675 @command(timeout = 30000)
686 - def file_write(self, fd, size = 0x100000, addr = None):
687 - """ Writes data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
688 - if addr is None:
689 - addr = self.malloc(size)
690 - malloc = True
691 - else:
692 - malloc = False
693 - try:
694 - result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
695 - finally:
696 - if malloc == True:
697 - self.free(addr)
 676+ def file_write(self, fd, size, addr):
 677+ """ Writes data from a file referenced by a handle. """
 678+ result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
698679 if result.rc > 0x80000000:
699680 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()))
700681 return result.rc
Index: emcore/trunk/export/syscallwrappers.h
@@ -197,6 +197,7 @@
198198 #define get_library_ext __emcore_syscall->get_library_ext
199199 #define release_library __emcore_syscall->release_library
200200 #define release_library_ext __emcore_syscall->release_library_ext
 201+#define fat_enable_flushing __emcore_syscall->fat_enable_flushing
201202
202203
203204 #endif
Index: emcore/trunk/export/syscallapi.h
@@ -33,6 +33,7 @@
3434 #include "../disk.h"
3535 #include "../dir.h"
3636 #include "../file.h"
 37+#include "../fat.h"
3738 #include "../format.h"
3839 #include "../drawing.h"
3940 #include "../lcdconsole.h"
@@ -243,6 +244,7 @@
244245 typeof(get_library_ext) *get_library_ext;
245246 typeof(release_library) *release_library;
246247 typeof(release_library_ext) *release_library_ext;
 248+ typeof(fat_enable_flushing) *fat_enable_flushing;
247249 };
248250
249251
Index: emcore/trunk/syscallapi.c
@@ -208,5 +208,8 @@
209209 .get_library = get_library,
210210 .get_library_ext = get_library_ext,
211211 .release_library = release_library,
212 - .release_library_ext = release_library_ext
 212+ .release_library_ext = release_library_ext,
 213+#ifdef HAVE_STORAGE
 214+ .fat_enable_flushing = fat_enable_flushing
 215+#endif
213216 };
Index: emcore/trunk/usb/usb.c
@@ -628,6 +628,7 @@
629629 case 50: // DISK_MOUNT
630630 case 51: // DISK_UNMOUNT
631631 #endif
 632+ case 58: // FAT_ENABLE_FLUSHING
632633 if (!set_dbgaction(DBGACTION_STORAGE, 0))
633634 memcpy(dbgasyncsendbuf, dbgrecvbuf, sizeof(dbgasyncsendbuf));
634635 break;
@@ -945,6 +946,11 @@
946947 usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
947948 break;
948949 #endif
 950+ case 58: // FAT_ENABLE_FLUSHING
 951+ dbgasyncsendbuf[0] = 1;
 952+ fat_enable_flushing((bool)(dbgasyncsendbuf[1]));
 953+ usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
 954+ break;
949955 }
950956 break;
951957 #endif
Index: emcore/trunk/fat.c
@@ -179,6 +179,7 @@
180180
181181 static struct bpb fat_bpbs[NUM_VOLUMES]; /* mounted partition info */
182182 static bool initialized = false;
 183+static bool flush_fat_disabled = false;
183184
184185 static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb));
185186 static int flush_fat(IF_MV_NONVOID(struct bpb* fat_bpb));
@@ -1024,6 +1025,11 @@
10251026 int i;
10261027 int rc;
10271028 unsigned char *sec;
 1029+ if (flush_fat_disabled)
 1030+ {
 1031+ DEBUGF("flush_fat() skipped");
 1032+ return 0;
 1033+ }
10281034 DEBUGF("flush_fat()");
10291035
10301036 mutex_lock(&cache_mutex, TIMEOUT_BLOCK);
@@ -2559,3 +2565,9 @@
25602566 return (volume<NUM_VOLUMES && fat_bpbs[volume].mounted);
25612567 }
25622568 #endif
 2569+
 2570+void fat_enable_flushing(bool state)
 2571+{
 2572+ flush_fat_disabled = !state;
 2573+ if (state) flush_fat();
 2574+}
Index: emcore/trunk/fat.h
@@ -132,6 +132,7 @@
133133 extern bool fat_ismounted(int volume);
134134 extern void* fat_get_sector_buffer(void);
135135 extern void fat_release_sector_buffer(void);
 136+extern void fat_enable_flushing(bool state);
136137
137138 #endif
138139 #endif