update.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # greaseweazle/tools/update.py
  2. #
  3. # Greaseweazle control script: Firmware Update.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. import sys, argparse
  10. import crcmod.predefined
  11. from greaseweazle.tools import util
  12. from greaseweazle import usb as USB
  13. # update_firmware:
  14. # Updates the Greaseweazle firmware using the specified Update File.
  15. def update_firmware(usb, args):
  16. # Read and check the update file.
  17. with open(args.file, "rb") as f:
  18. dat = f.read()
  19. sig, maj, min, pad1, pad2, crc = struct.unpack(">2s4BH", dat[-8:])
  20. if len(dat) & 3 != 0 or sig != b'GW' or pad1 != 0 or pad2 != 0:
  21. print("%s: Bad update file" % (args.file))
  22. return
  23. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  24. crc16.update(dat)
  25. if crc16.crcValue != 0:
  26. print("%s: Bad CRC" % (args.file))
  27. # Perform the update.
  28. print("Updating to v%u.%u..." % (maj, min))
  29. ack = usb.update_firmware(dat)
  30. if ack != 0:
  31. print("** UPDATE FAILED: Please retry!")
  32. return
  33. print("Done.")
  34. print("** Disconnect Greaseweazle and remove the Programming Jumper.")
  35. def main(argv):
  36. parser = argparse.ArgumentParser(
  37. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  38. parser.add_argument("file", help="update filename")
  39. parser.add_argument("device", help="serial device")
  40. parser.prog += ' ' + argv[1]
  41. args = parser.parse_args(argv[2:])
  42. usb = util.usb_open(args.device, is_update=True)
  43. try:
  44. update_firmware(usb, args)
  45. except USB.CmdError as error:
  46. print("Command Failed: %s" % error)
  47. if __name__ == "__main__":
  48. main(sys.argv)
  49. # Local variables:
  50. # python-indent: 4
  51. # End: