mk_update.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # mk_update.py
  2. #
  3. # Convert a raw firmware binary into an update file for our bootloader.
  4. #
  5. # Update Format (Little endian, unless otherwise stated):
  6. # Catalogue Header:
  7. # 2 bytes: <length> (excludes Catalogue Header)
  8. # 2 bytes: <hw_type>
  9. # Payload:
  10. # N bytes: <raw binary data>
  11. # Footer:
  12. # 2 bytes: 'GW'
  13. # 2 bytes: major, minor
  14. # 2 bytes: <hw_type>
  15. # 2 bytes: CRC16-CCITT, seed 0xFFFF (big endian, excludes Catalogue Header)
  16. #
  17. # Written & released by Keir Fraser <keir.xen@gmail.com>
  18. #
  19. # This is free and unencumbered software released into the public domain.
  20. # See the file COPYING for more details, or visit <http://unlicense.org>.
  21. import crcmod.predefined
  22. import re, struct, sys
  23. from greaseweazle import version
  24. def main(argv):
  25. in_f = open(argv[1], "rb")
  26. out_f = open(argv[2], "wb")
  27. hw_type = int(re.match("f(\d)", argv[3]).group(1))
  28. in_dat = in_f.read()
  29. in_len = len(in_dat)
  30. assert (in_len & 3) == 0, "input is not longword padded"
  31. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  32. out_f.write(struct.pack("<2H", in_len + 8, hw_type))
  33. out_f.write(in_dat)
  34. crc16.update(in_dat)
  35. in_dat = struct.pack("<2s2BH", b'GW', version.major, version.minor, hw_type)
  36. out_f.write(in_dat)
  37. crc16.update(in_dat)
  38. in_dat = struct.pack(">H", crc16.crcValue)
  39. out_f.write(in_dat)
  40. if __name__ == "__main__":
  41. main(sys.argv)
  42. # Local variables:
  43. # python-indent: 4
  44. # End: