mk_update.py 3.9 KB

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