mk_update.py 4.0 KB

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