Index: emcore/trunk/tools/emcore.py |
— | — | @@ -877,7 +877,7 @@ |
878 | 878 | self.logger.info("done\n")
|
879 | 879 |
|
880 | 880 | @command
|
881 | | - def readrawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
|
| 881 | + def readrawstoragefile(self, volume, sector, count, file, buffsize = 100000, buffer = None):
|
882 | 882 | """
|
883 | 883 | Reads <count> sectors starting at <sector> from storage <volume> to file <file>,
|
884 | 884 | buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
|
— | — | @@ -885,26 +885,34 @@ |
886 | 886 | volume = self._hexint(volume)
|
887 | 887 | sector = self._hexint(sector)
|
888 | 888 | count = self._hexint(count)
|
889 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
890 | | - else: buffer = self._hexint(buffer)
|
891 | 889 | 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
|
892 | 896 | try:
|
893 | | - f = open(file, 'wb')
|
894 | | - except IOError:
|
895 | | - raise ArgumentError("Could not open local file for writing.")
|
896 | | - self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
|
897 | | - storageinfo = self.emcore.storage_get_info(volume)
|
898 | | - while count > 0:
|
899 | | - sectors = min(count, int(buffsize / storageinfo.sectorsize))
|
900 | | - self.emcore.storage_read_sectors_md(volume, sector, sectors, buffer)
|
901 | | - f.write(self.emcore.read(buffer, storageinfo.sectorsize * sectors))
|
902 | | - sector = sector + sectors
|
903 | | - count = count - sectors
|
904 | | - f.close()
|
| 897 | + 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
|
| 909 | + f.close()
|
| 910 | + finally:
|
| 911 | + if malloc == True:
|
| 912 | + self.emcore.free(buffer)
|
905 | 913 | self.logger.info("done\n")
|
906 | 914 |
|
907 | 915 | @command
|
908 | | - def writerawstoragefile(self, volume, sector, count, file, buffer = False, buffsize = "100000"):
|
| 916 | + def writerawstoragefile(self, volume, sector, count, file, buffsize = 100000, buffer = None):
|
909 | 917 | """
|
910 | 918 | Writes contents of <file> to <count> sectors starting at <sector> on storage <volume>,
|
911 | 919 | buffering them in memory at [buffer] in chunks of [buffsize] bytes (both optional).
|
— | — | @@ -912,26 +920,34 @@ |
913 | 921 | volume = self._hexint(volume)
|
914 | 922 | sector = self._hexint(sector)
|
915 | 923 | count = self._hexint(count)
|
916 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
917 | | - else: buffer = self._hexint(buffer)
|
918 | 924 | 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
|
919 | 931 | try:
|
920 | | - f = open(file, 'rb')
|
921 | | - except IOError:
|
922 | | - raise ArgumentError("Could not open local file for reading.")
|
923 | | - self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
|
924 | | - storageinfo = self.emcore.storage_get_info(volume)
|
925 | | - while count > 0:
|
926 | | - sectors = min(count, int(buffsize / storageinfo.sectorsize))
|
927 | | - bytes = storageinfo.sectorsize * sectors
|
928 | | - data = f.read(bytes)
|
929 | | - if len(data) == 0: break
|
930 | | - while len(data) < bytes: data = data + f.read(bytes - len(data))
|
931 | | - self.emcore.write(buffer, data)
|
932 | | - self.emcore.storage_write_sectors_md(volume, sector, sectors, buffer)
|
933 | | - sector = sector + sectors
|
934 | | - count = count - sectors
|
935 | | - f.close()
|
| 932 | + 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
|
| 948 | + f.close()
|
| 949 | + finally:
|
| 950 | + if malloc == True:
|
| 951 | + self.emcore.free(buffer)
|
936 | 952 | self.logger.info("done\n")
|
937 | 953 |
|
938 | 954 | @command
|
— | — | @@ -989,111 +1005,143 @@ |
990 | 1006 | self.logger.info(" done\n")
|
991 | 1007 |
|
992 | 1008 | @command
|
993 | | - def get(self, remotename, localname, buffer = False, buffsize = "10000"):
|
| 1009 | + def get(self, remotename, localname, buffsize = 10000, buffer = None):
|
994 | 1010 | """
|
995 | 1011 | Downloads a file
|
996 | 1012 | <remotename>: filename on the device
|
997 | 1013 | <localname>: filename on the computer
|
| 1014 | + [buffsize]: buffer size (optional)
|
998 | 1015 | [buffer]: buffer address (optional)
|
999 | | - [buffsize]: buffer size (optional)
|
1000 | 1016 | """
|
1001 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
1002 | | - else: buffer = self._hexint(buffer)
|
1003 | 1017 | 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
|
1004 | 1024 | try:
|
1005 | | - f = open(localname, 'wb')
|
1006 | | - except IOError:
|
1007 | | - raise ArgumentError("Could not open local file for writing.")
|
1008 | | - self.logger.info("Downloading file " + remotename + " to " + localname + "...")
|
1009 | | - fd = self.emcore.file_open(remotename, 0)
|
1010 | | - size = self.emcore.file_size(fd)
|
1011 | | - while size > 0:
|
1012 | | - bytes = self.emcore.file_read(fd, buffer, buffsize)
|
1013 | | - f.write(self.emcore.read(buffer, bytes))
|
1014 | | - size = size - bytes
|
1015 | | - self.emcore.file_close(fd)
|
1016 | | - f.close()
|
| 1025 | + 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, buffer, buffsize)
|
| 1034 | + f.write(self.emcore.read(buffer, bytes))
|
| 1035 | + size = size - bytes
|
| 1036 | + self.emcore.file_close(fd)
|
| 1037 | + f.close()
|
| 1038 | + finally:
|
| 1039 | + if malloc == True:
|
| 1040 | + self.emcore.free(buffer)
|
1017 | 1041 | self.logger.info(" done\n")
|
1018 | 1042 |
|
1019 | 1043 | @command
|
1020 | | - def gettree(self, remotepath, localpath, buffer = False, buffsize = "10000"):
|
| 1044 | + def gettree(self, remotepath, localpath, buffsize = 10000, buffer = None):
|
1021 | 1045 | """
|
1022 | 1046 | Downloads a directory tree
|
1023 | 1047 | <remotepath>: path on the device
|
1024 | 1048 | <localpath>: path on the computer
|
| 1049 | + [buffsize]: buffer size (optional)
|
1025 | 1050 | [buffer]: buffer address (optional)
|
1026 | | - [buffsize]: buffer size (optional)
|
1027 | 1051 | """
|
1028 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
1029 | | - else: buffer = self._hexint(buffer)
|
1030 | 1052 | buffsize = self._hexint(buffsize)
|
1031 | | - try: os.mkdir(localpath)
|
1032 | | - except: pass
|
| 1053 | + if buffer is None:
|
| 1054 | + buffer = self.emcore.malloc(buffsize)
|
| 1055 | + malloc = True
|
| 1056 | + else:
|
| 1057 | + buffer = self._hexint(buffer)
|
| 1058 | + malloc = False
|
| 1059 | + try:
|
| 1060 | + try: os.mkdir(localpath)
|
| 1061 | + except: pass
|
1033 | 1062 |
|
1034 | | - handle = self.emcore.dir_open(remotepath)
|
1035 | | - while True:
|
1036 | | - try:
|
1037 | | - entry = self.emcore.dir_read(handle)
|
1038 | | - if entry.name == "." or entry.name == "..": continue
|
1039 | | - elif entry.attributes & 0x10:
|
1040 | | - self.gettree(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
|
1041 | | - else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
|
1042 | | - except: break
|
1043 | | - self.emcore.dir_close(handle)
|
| 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, buffer, buffsize)
|
| 1070 | + else: self.get(remotepath + "/" + entry.name, localpath + "/" + entry.name, buffer, buffsize)
|
| 1071 | + except: break
|
| 1072 | + self.emcore.dir_close(handle)
|
| 1073 | + finally:
|
| 1074 | + if malloc == True:
|
| 1075 | + self.emcore.free(buffer)
|
1044 | 1076 |
|
1045 | 1077 | @command
|
1046 | | - def put(self, localname, remotename, buffer = False, buffsize = "10000"):
|
| 1078 | + def put(self, localname, remotename, buffsize = 10000, buffer = None):
|
1047 | 1079 | """
|
1048 | 1080 | Uploads a file
|
1049 | 1081 | <localname>: filename on the computer
|
1050 | 1082 | <remotename>: filename on the device
|
| 1083 | + [buffsize]: buffer size (optional)
|
1051 | 1084 | [buffer]: buffer address (optional)
|
1052 | | - [buffsize]: buffer size (optional)
|
1053 | 1085 | """
|
1054 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
1055 | | - else: buffer = self._hexint(buffer)
|
1056 | 1086 | 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
|
1057 | 1093 | try:
|
1058 | | - f = open(localname, 'rb')
|
1059 | | - except IOError:
|
1060 | | - raise ArgumentError("Could not open local file for reading.")
|
1061 | | - self.logger.info("Uploading file " + localname + " to " + remotename + "...")
|
1062 | | - fd = self.emcore.file_open(remotename, 0x15)
|
1063 | | - while True:
|
1064 | | - data = f.read(buffsize)
|
1065 | | - if len(data) == 0: break
|
1066 | | - self.emcore.write(buffer, data)
|
1067 | | - bytes = 0
|
1068 | | - while bytes < len(data):
|
1069 | | - bytes = bytes + self.emcore.file_write(fd, buffer + bytes, len(data) - bytes)
|
1070 | | - self.emcore.file_close(fd)
|
1071 | | - f.close()
|
| 1094 | + 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 + "...")
|
| 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, buffer + bytes, len(data) - bytes)
|
| 1107 | + self.emcore.file_close(fd)
|
| 1108 | + f.close()
|
| 1109 | + finally:
|
| 1110 | + if malloc == True:
|
| 1111 | + self.emcore.free(buffer)
|
1072 | 1112 | self.logger.info(" done\n")
|
1073 | 1113 |
|
1074 | 1114 | @command
|
1075 | | - def puttree(self, localpath, remotepath, buffer = False, buffsize = "10000"):
|
| 1115 | + def puttree(self, localpath, remotepath, buffsize = 10000, buffer = None):
|
1076 | 1116 | """
|
1077 | 1117 | Uploads a directory tree
|
1078 | 1118 | <localpath>: path on the computer
|
1079 | 1119 | <remotepath>: path on the device
|
| 1120 | + [buffsize]: buffer size (optional)
|
1080 | 1121 | [buffer]: buffer address (optional)
|
1081 | | - [buffsize]: buffer size (optional)
|
1082 | 1122 | """
|
1083 | | - if buffer == False: buffer = self.emcore.lib.dev.usermem.lower
|
1084 | | - else: buffer = self._hexint(buffer)
|
1085 | 1123 | buffsize = self._hexint(buffsize)
|
1086 | | - try: self.mkdir(remotepath)
|
1087 | | - except: self.logger.info(" failed\n")
|
1088 | | - pathlen = len(localpath)
|
1089 | | - for d in os.walk(localpath):
|
1090 | | - prefix = remotepath + "/" + d[0].replace("\\", "/")[pathlen:] + "/"
|
1091 | | - for dir in d[1]:
|
1092 | | - if dir != ".svn":
|
1093 | | - try: self.mkdir(prefix + dir)
|
1094 | | - except: self.logger.info(" failed\n")
|
1095 | | - for f in d[2]:
|
1096 | | - if not prefix.find("/.svn/") > -1:
|
1097 | | - self.put(d[0] + "/" + f, prefix + f, buffer, buffsize)
|
| 1124 | + if buffer is None:
|
| 1125 | + buffer = self.emcore.malloc(buffsize)
|
| 1126 | + malloc = True
|
| 1127 | + else:
|
| 1128 | + buffer = self._hexint(buffer)
|
| 1129 | + malloc = False
|
| 1130 | + try:
|
| 1131 | + try: self.mkdir(remotepath)
|
| 1132 | + except: self.logger.info(" failed\n")
|
| 1133 | + pathlen = len(localpath)
|
| 1134 | + for d in os.walk(localpath):
|
| 1135 | + prefix = remotepath + "/" + d[0].replace("\\", "/")[pathlen:] + "/"
|
| 1136 | + for dir in d[1]:
|
| 1137 | + if dir != ".svn":
|
| 1138 | + try: self.mkdir(prefix + dir)
|
| 1139 | + except: self.logger.info(" failed\n")
|
| 1140 | + for f in d[2]:
|
| 1141 | + if not prefix.find("/.svn/") > -1:
|
| 1142 | + self.put(d[0] + "/" + f, prefix + f, buffer, buffsize)
|
| 1143 | + finally:
|
| 1144 | + if malloc == True:
|
| 1145 | + self.emcore.free(buffer)
|
1098 | 1146 |
|
1099 | 1147 | @command
|
1100 | 1148 | def ls(self, path = "/"):
|
Index: emcore/trunk/tools/libemcore.py |
— | — | @@ -618,16 +618,33 @@ |
619 | 619 | return result
|
620 | 620 |
|
621 | 621 | @command(timeout = 50000)
|
622 | | - def storage_read_sectors_md(self, volume, sector, count, addr):
|
623 | | - """ Read sectors from as storage device """
|
624 | | - result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
| 622 | + def storage_read_sectors_md(self, volume, sector, count, size = 100000, 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)
|
625 | 634 | if result.rc > 0x80000000:
|
626 | 635 | 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))
|
627 | 636 |
|
628 | | - @command(timeout = 50000)
|
629 | | - def storage_write_sectors_md(self, volume, sector, count, addr):
|
630 | | - """ Read sectors from as storage device """
|
631 | | - result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
|
| 637 | + def storage_write_sectors_md(self, volume, sector, count, size = 100000, 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)
|
632 | 649 | if result.rc > 0x80000000:
|
633 | 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 | 651 |
|
— | — | @@ -648,17 +665,35 @@ |
649 | 666 | return result.size
|
650 | 667 |
|
651 | 668 | @command(timeout = 30000)
|
652 | | - def file_read(self, fd, addr, size):
|
653 | | - """ Reads data from a file referenced by a handle """
|
654 | | - result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
|
| 669 | + def file_read(self, fd, size = 100000, addr = None):
|
| 670 | + """ Reads data from a file referenced by a handle. If addr is not given it allocates a buffer itself. """
|
| 671 | + if addr is None:
|
| 672 | + addr = self.malloc(size)
|
| 673 | + malloc = True
|
| 674 | + else:
|
| 675 | + malloc = False
|
| 676 | + try:
|
| 677 | + result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
|
| 678 | + finally:
|
| 679 | + if malloc == True:
|
| 680 | + self.free(addr)
|
655 | 681 | if result.rc > 0x80000000:
|
656 | 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()))
|
657 | 683 | return result.rc
|
658 | 684 |
|
659 | 685 | @command(timeout = 30000)
|
660 | | - def file_write(self, fd, addr, size):
|
661 | | - """ Writes data from a file referenced by a handle """
|
662 | | - result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
|
| 686 | + def file_write(self, fd, size = 100000, 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)
|
663 | 698 | if result.rc > 0x80000000:
|
664 | 699 | 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()))
|
665 | 700 | return result.rc
|