Version 1.0.9.
[gnuk/neug.git] / tool / neug_upgrade.py
index 7a18af0..fc43b8a 100755 (executable)
@@ -47,16 +47,11 @@ class regnual(object):
         if intf.interfaceClass != 0xff:
             raise ValueError("Wrong interface class")
         self.__devhandle = dev.open()
-        try:
-            self.__devhandle.setConfiguration(conf)
-        except:
-            pass
         self.__devhandle.claimInterface(intf)
-        self.__devhandle.setAltInterface(intf)
 
     def mem_info(self):
         mem = self.__devhandle.controlMsg(requestType = 0xc0, request = 0,
-                                          value = 0, index = 0, buffer = 8,
+                                          buffer = 8, value = 0, index = 0,
                                           timeout = 10000)
         start = ((mem[3]*256 + mem[2])*256 + mem[1])*256 + mem[0]
         end = ((mem[7]*256 + mem[6])*256 + mem[5])*256 + mem[4]
@@ -65,7 +60,7 @@ class regnual(object):
     def download(self, start, data, verbose=False):
         addr = start
         addr_end = (start + len(data)) & 0xffffff00
-        i = (addr - 0x08000000) / 0x100
+        i = int((addr - 0x08000000) / 0x100)
         j = 0
         print("start %08x" % addr)
         print("end   %08x" % addr_end)
@@ -73,23 +68,22 @@ class regnual(object):
             if verbose:
                 print("# %08x: %d: %d : %d" % (addr, i, j, 256))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = 0, index = 0,
                                         buffer = data[j*256:j*256+256],
+                                        value = 0, index = 0,
                                         timeout = 10000)
             crc32code = crc32(data[j*256:j*256+256])
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if (crc32code ^ r_value) != 0xffffffff:
                 print("failure")
             self.__devhandle.controlMsg(requestType = 0x40, request = 3,
-                                        value = i, index = 0,
-                                        buffer = None,
+                                        buffer = None, value = i, index = 0,
                                         timeout = 10000)
             time.sleep(0.010)
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if r_value == 0:
@@ -102,23 +96,21 @@ class regnual(object):
             if verbose:
                 print("# %08x: %d : %d" % (addr, i, residue))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = 0, index = 0,
                                         buffer = data[j*256:],
-                                        timeout = 10000)
-            crc32code = crc32(data[j*256:].ljust(256,chr(255)))
+                                        value = 0, index = 0, timeout = 10000)
+            crc32code = crc32(data[j*256:].ljust(256,b'\xff'))
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if (crc32code ^ r_value) != 0xffffffff:
                 print("failure")
             self.__devhandle.controlMsg(requestType = 0x40, request = 3,
-                                        value = i, index = 0,
-                                        buffer = None,
+                                        buffer = None, value = i, index = 0,
                                         timeout = 10000)
             time.sleep(0.010)
             res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                              value = 0, index = 0, buffer = 4,
+                                              buffer = 4, value = 0, index = 0,
                                               timeout = 10000)
             r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
             if r_value == 0:
@@ -126,11 +118,11 @@ class regnual(object):
 
     def protect(self):
         self.__devhandle.controlMsg(requestType = 0x40, request = 4,
-                                    value = 0, index = 0, buffer = None,
+                                    buffer = None, value = 0, index = 0,
                                     timeout = 10000)
         time.sleep(0.100)
         res = self.__devhandle.controlMsg(requestType = 0xc0, request = 2,
-                                          value = 0, index = 0, buffer = 4,
+                                          buffer = 4, value = 0, index = 0,
                                           timeout = 10000)
         r_value = ((res[3]*256 + res[2])*256 + res[1])*256 + res[0]
         if r_value == 0:
@@ -138,7 +130,7 @@ class regnual(object):
 
     def finish(self):
         self.__devhandle.controlMsg(requestType = 0x40, request = 5,
-                                    value = 0, index = 0, buffer = None,
+                                    buffer = None, value = 0, index = 0,
                                     timeout = 10000)
 
     def reset_device(self):
@@ -161,8 +153,6 @@ class neug(object):
         if interface.interfaceSubClass != COM_SUBCLASS:
             raise ValueError("Wrong interface sub class")
         self.__devhandle = device.open()
-        # self.__devhandle.claimInterface(interface)
-        # self.__devhandle.setAltInterface(interface)
 
         self.__intf = interface.interfaceNumber
         self.__alt = interface.alternateSetting
@@ -181,13 +171,13 @@ class neug(object):
 
     def set_passwd(self, passwd):
         self.__devhandle.controlMsg(requestType = 0x40, request = 253,
-                                    value = 0, index = 0, buffer = passwd,
+                                    buffer = passwd, value = 0, index = 0,
                                     timeout = 1000)
         return
 
     def stop_neug(self, passwd):
         self.__devhandle.controlMsg(requestType = 0x40, request = 255,
-                                    value = 0, index = 0, buffer = passwd,
+                                    buffer = passwd, value = 0, index = 0,
                                     timeout = 1000)
         # self.__devhandle.releaseInterface()
         # self.__devhandle.setConfiguration(0)
@@ -195,7 +185,7 @@ class neug(object):
 
     def mem_info(self):
         mem = self.__devhandle.controlMsg(requestType = 0xc0, request = 0,
-                                          value = 0, index = 0, buffer = 8,
+                                          buffer = 8, value = 0, index = 0,
                                           timeout = 1000)
         start = ((mem[3]*256 + mem[2])*256 + mem[1])*256 + mem[0]
         end = ((mem[7]*256 + mem[6])*256 + mem[5])*256 + mem[4]
@@ -204,7 +194,7 @@ class neug(object):
     def download(self, start, data, verbose=False):
         addr = start
         addr_end = (start + len(data)) & 0xffffff00
-        i = (addr - 0x20000000) / 0x100
+        i = int((addr - 0x20000000) / 0x100)
         j = 0
         print("start %08x" % addr)
         print("end   %08x" % addr_end)
@@ -212,9 +202,8 @@ class neug(object):
             if verbose:
                 print("# %08x: %d : %d" % (addr, i, 256))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = i, index = 0,
                                         buffer = data[j*256:j*256+256],
-                                        timeout = 10)
+                                        value = i, index = 0, timeout = 10)
             i = i+1
             j = j+1
             addr = addr + 256
@@ -222,15 +211,14 @@ class neug(object):
         if residue != 0:
             print("# %08x: %d : %d" % (addr, i, residue))
             self.__devhandle.controlMsg(requestType = 0x40, request = 1,
-                                        value = i, index = 0,
                                         buffer = data[j*256:],
-                                        timeout = 10)
+                                        value = i, index = 0, timeout = 10)
 
     def execute(self, last_addr):
-        i = (last_addr - 0x20000000) / 0x100
+        i = int((last_addr - 0x20000000) / 0x100)
         o = (last_addr - 0x20000000) % 0x100
         self.__devhandle.controlMsg(requestType = 0x40, request = 2,
-                                    value = i, index = o, buffer = None,
+                                    buffer = None, value = i, index = o,
                                     timeout = 10)
 
 def compare(data_original, data_in_device):
@@ -280,10 +268,10 @@ def crc32(bytestr):
     crc = binascii.crc32(bytestr)
     return UNSIGNED(crc)
 
-def main(passwd, data_regnual, data_upgrade):
+def main(wait_e, passwd, data_regnual, data_upgrade):
     l = len(data_regnual)
     if (l & 0x03) != 0:
-        data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), chr(0))
+        data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), b'\x00')
     crc32code = crc32(data_regnual)
     print("CRC32: %04x\n" % crc32code)
     data_regnual += pack('<I', crc32code)
@@ -315,17 +303,18 @@ def main(passwd, data_regnual, data_upgrade):
     del com
     com = None
     #
-    print("Wait 3 seconds...")
-    time.sleep(3)
-    # Then, send upgrade program...
     reg = None
-    for dev in gnuk_devices_by_vidpid():
-        try:
-            reg = regnual(dev)
-            print("Device: %s" % dev.filename)
-            break
-        except:
-            pass
+    while reg == None:
+        print("Wait {} second{}...".format(wait_e, 's' if wait_e > 1 else ''))
+        time.sleep(wait_e)
+        for dev in gnuk_devices_by_vidpid():
+            try:
+                reg = regnual(dev)
+                print("Device: %s" % dev.filename)
+                break
+            except:
+                pass
+    # Then, send upgrade program...
     mem_info = reg.mem_info()
     print("%08x:%08x" % mem_info)
     print("Downloading the program")
@@ -336,8 +325,12 @@ def main(passwd, data_regnual, data_upgrade):
     return 0
 
 
+# This should be event driven, not guessing some period.
+DEFAULT_WAIT_FOR_REENUMERATION=1
+
 if __name__ == '__main__':
     passwd = None
+    wait_e = DEFAULT_WAIT_FOR_REENUMERATION
     if len(sys.argv) == 2 and sys.argv[1] == '-s': # S for set passwd
         passwd = getpass("Admin password: ")
         com = None
@@ -359,18 +352,21 @@ if __name__ == '__main__':
         sys.argv.pop(1)
         if option == '-f':      # F for Factory setting
             passwd = DEFAULT_PW3
+        elif option == '-e':    # E for Enumeration
+            wait_e = int(sys.argv[1])
+            sys.argv.pop(1)
         else:
             raise ValueError("unknown option", option)
     if not passwd:
         passwd = getpass("Admin password: ")
     filename_regnual = sys.argv[1]
     filename_upgrade = sys.argv[2]
-    f = open(filename_regnual)
+    f = open(filename_regnual, "rb")
     data_regnual = f.read()
     f.close()
     print("%s: %d" % (filename_regnual, len(data_regnual)))
-    f = open(filename_upgrade)
+    f = open(filename_upgrade, "rb")
     data_upgrade = f.read()
     f.close()
     print("%s: %d" % (filename_upgrade, len(data_upgrade)))
-    main(passwd, data_regnual, data_upgrade[4096:])
+    main(wait_e, passwd, data_regnual, data_upgrade[4096:])