mk_update.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. name_to_hw_model = { 'stm32f1': 1,
  31. 'stm32f7': 7,
  32. 'at32f4': 4 }
  33. hw_model_to_name = { 1: 'STM32F1',
  34. 7: 'STM32F7',
  35. 4: 'AT32F4' }
  36. def mk_cat_entry(dat, hw_model, sig):
  37. max_kb = { 1: { b'BL': 8, b'GW': 56 },
  38. 7: { b'BL': 16, b'GW': 48 },
  39. 4: { b'BL': 16, b'GW': 48 } }
  40. dlen = len(dat)
  41. assert (dlen & 3) == 0, "input is not longword padded"
  42. assert dlen <= max_kb[hw_model][sig]*1024, "input is too long"
  43. header = struct.pack("<2H", dlen + 8, hw_model)
  44. footer = struct.pack("<2s2BH", sig, version.major, version.minor, hw_model)
  45. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  46. crc16.update(dat)
  47. crc16.update(footer)
  48. footer += struct.pack(">H", crc16.crcValue)
  49. return header + dat + footer
  50. def new_upd(argv):
  51. dat = b'GWUP'
  52. hw_model = name_to_hw_model[argv[2]]
  53. with open(argv[1], "rb") as gw_f:
  54. dat += mk_cat_entry(gw_f.read(), hw_model, b'GW')
  55. with open(argv[0], "rb") as bl_f:
  56. dat += mk_cat_entry(bl_f.read(), hw_model, b'BL')
  57. return dat
  58. def cat_upd(argv):
  59. dat = b'GWUP'
  60. for fname in argv:
  61. with open(fname, "rb") as f:
  62. d = f.read()
  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. dat += d[4:-4]
  68. return dat
  69. def _verify_upd(d):
  70. assert struct.unpack('4s', d[:4])[0] == b'GWUP'
  71. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  72. crc32.update(d)
  73. assert crc32.crcValue == 0
  74. d = d[4:-4]
  75. while d:
  76. upd_len, hw_model = struct.unpack("<2H", d[:4])
  77. upd_type, major, minor = struct.unpack("2s2B", d[upd_len-4:upd_len])
  78. crc16 = crcmod.predefined.Crc('crc-ccitt-false')
  79. crc16.update(d[4:upd_len+4])
  80. assert crc16.crcValue == 0
  81. print('%s %s v%u.%u: %u bytes'
  82. % (hw_model_to_name[hw_model],
  83. {b'BL': 'Boot', b'GW': 'Main'}[upd_type],
  84. major, minor, upd_len))
  85. d = d[upd_len+4:]
  86. def verify_upd(argv):
  87. for fname in argv:
  88. with open(fname, "rb") as f:
  89. d = f.read()
  90. _verify_upd(d)
  91. def main(argv):
  92. if argv[1] == 'new':
  93. dat = new_upd(argv[3:])
  94. elif argv[1] == 'cat':
  95. dat = cat_upd(argv[3:])
  96. elif argv[1] == 'verify':
  97. verify_upd(argv[2:])
  98. return
  99. else:
  100. assert False
  101. crc32 = crcmod.predefined.Crc('crc-32-mpeg')
  102. crc32.update(dat)
  103. dat += struct.pack(">I", crc32.crcValue)
  104. with open(argv[2], "wb") as out_f:
  105. out_f.write(dat)
  106. if __name__ == "__main__":
  107. main(sys.argv)
  108. # Local variables:
  109. # python-indent: 4
  110. # End: