mk_update.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(argv):
  63. dat = b'GWUP'
  64. for fname in argv:
  65. with open(fname, "rb") as f:
  66. d = f.read()
  67. assert struct.unpack('4s', d[:4])[0] == b'GWUP'
  68. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  69. crc32.update(d)
  70. assert crc32.crcValue == 0
  71. d = d[4:-4]
  72. while d:
  73. upd_len, hw_model = struct.unpack("<2H", d[:4])
  74. upd_type, = struct.unpack("2s", d[upd_len-4:upd_len-2])
  75. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  76. crc16.update(d[4:upd_len+4])
  77. assert crc16.crcValue == 0
  78. print('F%u %s' % (hw_model, {b'BL': 'Boot',
  79. b'GW': 'Main'}[upd_type]))
  80. d = d[upd_len+4:]
  81. def main(argv):
  82. if argv[1] == 'new':
  83. dat = new_upd(argv[3:])
  84. elif argv[1] == 'cat':
  85. dat = cat_upd(argv[3:])
  86. elif argv[1] == 'verify':
  87. verify_upd(argv[2:])
  88. return
  89. else:
  90. assert False
  91. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  92. crc32.update(dat)
  93. dat += struct.pack(">I", crc32.crcValue)
  94. with open(argv[2], "wb") as out_f:
  95. out_f.write(dat)
  96. if __name__ == "__main__":
  97. main(sys.argv)
  98. # Local variables:
  99. # python-indent: 4
  100. # End: