1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import sys, argparse, serial, struct, os
- import crcmod.predefined
- from greaseweazle.tools import util
- from greaseweazle import version
- from greaseweazle import usb as USB
- def update_firmware(usb, args):
- filename = args.file
- if filename == "auto":
-
- path = os.path.dirname(os.path.abspath(__file__))
- for _ in range(3):
- path = os.path.join(path, os.pardir)
- path = os.path.normpath(path)
- filename = os.path.join(path, "Greaseweazle-v%d.%d.upd"
- % (version.major, version.minor))
-
-
- with open(filename, "rb") as f:
- dat = f.read()
-
- while dat:
- upd_len, hw_type = struct.unpack("<2H", dat[:4])
- if hw_type == usb.hw_type:
-
- dat = dat[4:upd_len+4]
- break
-
- dat = dat[upd_len+4:]
- if not dat:
- print("%s: No match for hardware type %x" % (filename, usb.hw_type))
- return
-
- sig, maj, min, hw_type = struct.unpack("<2s2BH", dat[-8:-2])
- if len(dat) & 3 != 0 or sig != b'GW' or hw_type != usb.hw_type:
- print("%s: Bad update file" % (filename))
- return
- crc16 = crcmod.predefined.Crc('crc-ccitt-false')
- crc16.update(dat)
- if crc16.crcValue != 0:
- print("%s: Bad CRC" % (filename))
- return
-
- print("Updating to v%u.%u..." % (maj, min))
- ack = usb.update_firmware(dat)
- if ack != 0:
- print("** UPDATE FAILED: Please retry!")
- return
- print("Done.")
-
- if usb.hw_type == 7:
- util.usb_reopen(usb, is_update=False)
- else:
- print("** Disconnect Greaseweazle and remove the Programming Jumper.")
- def main(argv):
- parser = argparse.ArgumentParser(
- formatter_class=argparse.ArgumentDefaultsHelpFormatter)
- parser.add_argument("file", nargs="?", default="auto",
- help="update filename")
- parser.add_argument("device", nargs="?", default="auto",
- help="serial device")
- parser.prog += ' ' + argv[1]
- args = parser.parse_args(argv[2:])
- usb = util.usb_open(args.device, is_update=True)
- try:
- update_firmware(usb, args)
- except USB.CmdError as error:
- print("Command Failed: %s" % error)
- if __name__ == "__main__":
- main(sys.argv)
|