mk_update.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # mk_update.py new <output> <bootloader> <main_firmware> <stm_model>
  2. # mk_update.py cat <output> <update_file>*
  3. # mk_update.py verify <update_file>*
  4. #
  5. # Convert a raw firmware binary into an update file for our bootloader.
  6. #
  7. # Update Format (Little endian, unless otherwise stated):
  8. # File Header:
  9. # 4 bytes: 'GWUP'
  10. # Catalogue Header:
  11. # 2 bytes: <length> (excludes Catalogue Header)
  12. # 2 bytes: <hw_model>
  13. # Payload:
  14. # N bytes: <raw binary data>
  15. # Footer:
  16. # 2 bytes: 'GW' or 'BL'
  17. # 2 bytes: major, minor
  18. # 2 bytes: <hw_model>
  19. # 2 bytes: CRC16-CCITT, seed 0xFFFF (big endian, excludes Catalogue Header)
  20. # File Footer:
  21. # 4 bytes: CRC32 (MPEG-2, big endian)
  22. #
  23. # Written & released by Keir Fraser <keir.xen@gmail.com>
  24. #
  25. # This is free and unencumbered software released into the public domain.
  26. # See the file COPYING for more details, or visit <http://unlicense.org>.
  27. import crcmod.predefined
  28. import re, struct, sys
  29. from greaseweazle import version
  30. def mk_cat_entry(dat, hw_model, sig):
  31. max_kb = { 1: { b'BL': 8, b'GW': 56 },
  32. 7: { b'BL': 16, b'GW': 48 } }
  33. dlen = len(dat)
  34. assert (dlen & 3) == 0, "input is not longword padded"
  35. assert dlen <= max_kb[hw_model][sig]*1024, "input is too long"
  36. header = struct.pack("<2H", dlen + 8, hw_model)
  37. footer = struct.pack("<2s2BH", sig, version.major, version.minor, hw_model)
  38. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  39. crc16.update(dat)
  40. crc16.update(footer)
  41. footer += struct.pack(">H", crc16.crcValue)
  42. return header + dat + footer
  43. def new_upd(argv):
  44. dat = b'GWUP'
  45. hw_model = int(re.match("f(\d)", argv[2]).group(1))
  46. with open(argv[1], "rb") as gw_f:
  47. dat += mk_cat_entry(gw_f.read(), hw_model, b'GW')
  48. with open(argv[0], "rb") as bl_f:
  49. dat += mk_cat_entry(bl_f.read(), hw_model, b'BL')
  50. return dat
  51. def cat_upd(argv):
  52. dat = b'GWUP'
  53. for fname in argv:
  54. with open(fname, "rb") as f:
  55. d = f.read()
  56. assert struct.unpack('4s', d[:4])[0] == b'GWUP'
  57. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  58. crc32.update(d)
  59. assert crc32.crcValue == 0
  60. dat += d[4:-4]
  61. return dat
  62. def _verify_upd(d):
  63. assert struct.unpack('4s', d[:4])[0] == b'GWUP'
  64. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  65. crc32.update(d)
  66. assert crc32.crcValue == 0
  67. d = d[4:-4]
  68. while d:
  69. upd_len, hw_model = struct.unpack("<2H", d[:4])
  70. upd_type, major, minor = struct.unpack("2s2B", d[upd_len-4:upd_len])
  71. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  72. crc16.update(d[4:upd_len+4])
  73. assert crc16.crcValue == 0
  74. print('F%u %s v%u.%u' % (hw_model,
  75. {b'BL': 'Boot', b'GW': 'Main'}[upd_type],
  76. major, minor))
  77. d = d[upd_len+4:]
  78. def verify_upd(argv):
  79. for fname in argv:
  80. with open(fname, "rb") as f:
  81. d = f.read()
  82. _verify_upd(d)
  83. def main(argv):
  84. if argv[1] == 'new':
  85. dat = new_upd(argv[3:])
  86. elif argv[1] == 'cat':
  87. dat = cat_upd(argv[3:])
  88. elif argv[1] == 'verify':
  89. verify_upd(argv[2:])
  90. return
  91. else:
  92. assert False
  93. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  94. crc32.update(dat)
  95. dat += struct.pack(">I", crc32.crcValue)
  96. with open(argv[2], "wb") as out_f:
  97. out_f.write(dat)
  98. if __name__ == "__main__":
  99. main(sys.argv)
  100. # Local variables:
  101. # python-indent: 4
  102. # End: