mkmultiboot.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. import sys
  3. """
  4. 0 0x000000 Multiboot header
  5. 16k 0x004000 Boot stub FPGA Image
  6. 128k 0x020000 Boot stub Software image (not used)
  7. 256k 0x040000 DFU FPGA Image
  8. 384k 0x060000 DFU Software Image
  9. 512k 0x080000 App 1 FPGA Image
  10. 640k 0x0a0000 App 1 Software Image
  11. 768k 0x0c0000 App 2 FPGA Image
  12. 896k 0x0e0000 App 2 Software Image
  13. """
  14. def hdr(mode, offset):
  15. return bytes([
  16. # Sync header
  17. 0x7e, 0xaa, 0x99, 0x7e,
  18. # Boot mode
  19. 0x92, 0x00, (0x01 if mode else 0x00),
  20. # Boot address
  21. 0x44, 0x03,
  22. (offset >> 16) & 0xff,
  23. (offset >> 8) & 0xff,
  24. (offset >> 0) & 0xff,
  25. # Bank offset
  26. 0x82, 0x00, 0x00,
  27. # Reboot
  28. 0x01, 0x08,
  29. # Padding
  30. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32. ])
  33. offset_map = [
  34. (True, 0x004000, 0x020000),
  35. (False, 0x004000, 0x020000),
  36. (False, 0x040000, 0x060000),
  37. (False, 0x080000, 0x0a0000),
  38. (False, 0x0c0000, 0x0e0000),
  39. ]
  40. def load_image(img_name):
  41. # Solit filename
  42. if ':' in img_name:
  43. bs_name, fw_name = img_name.split(':', 2)
  44. bs_name = bs_name or None
  45. fw_name = fw_name or None
  46. else:
  47. bs_name = img_name
  48. fw_name = None
  49. # Read
  50. bs = open(bs_name, 'rb').read() if (bs_name is not None) else b''
  51. fw = open(fw_name, 'rb').read() if (fw_name is not None) else b''
  52. return bs, fw
  53. def main(argv0, out, *images):
  54. # Build the header
  55. mb_hdr = b''.join([hdr(m, o0) for m,o0,o1 in offset_map])
  56. # Load images (if any)
  57. images = [load_image(i) for i in images]
  58. # Build final image
  59. data = bytearray(mb_hdr)
  60. for (_, o_bs, o_fw), (d_bs, d_fw) in zip(offset_map[1:], images):
  61. for o, d in [(o_bs, d_bs), (o_fw, d_fw)]:
  62. if len(d):
  63. data[len(data):] = bytearray(o + len(d) - len(data))
  64. data[o:o+len(d)] = d
  65. # Write final image
  66. with open(out, 'wb') as fh:
  67. fh.write( data )
  68. if __name__ == '__main__':
  69. main(*sys.argv)