headerize-axs.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. #we can use binascii instead of zlib
  3. import os, getopt, sys, zlib
  4. from elftools.elf.elffile import ELFFile
  5. def usage(exit_code):
  6. print("typical usage:")
  7. print("AXS101:")
  8. print(sys.argv[0] + \
  9. " --header-type v1 --arc-id 0x434 --spi-flash-offset 0x0 --image u-boot.bin --elf u-boot")
  10. print("AXS103:")
  11. print(sys.argv[0] + \
  12. " --header-type v2 --arc-id 0x53 --spi-flash-offset 0x200000 --image u-boot.bin --elf u-boot")
  13. sys.exit(exit_code)
  14. def elf_get_entry(filename):
  15. with open(filename, 'rb') as f:
  16. elffile = ELFFile(f)
  17. return elffile.header['e_entry']
  18. def calc_check_sum(filename):
  19. # Calculate u-boot image check_sum: it is sum of all u-boot binary bytes
  20. with open(filename, "rb") as file:
  21. ba = bytearray(file.read())
  22. return sum(ba) & 0xFF
  23. def arg_verify(uboot_bin_filename, uboot_elf_filename, header_type):
  24. if not os.path.isfile(uboot_bin_filename):
  25. print("uboot bin file not exists: " + uboot_bin_filename)
  26. sys.exit(2)
  27. if not os.path.isfile(uboot_elf_filename):
  28. print("uboot elf file not exists: " + uboot_elf_filename)
  29. sys.exit(2)
  30. if header_type not in ("v1", "v2"):
  31. print("unknown header type: " + header_type)
  32. print("choose between 'v1' (most likely AXS101) and 'v2' (most likely AXS103)")
  33. sys.exit(2)
  34. def main():
  35. try:
  36. opts, args = getopt.getopt(sys.argv[1:],
  37. "ht:a:s:i:l:e:",
  38. ["help", "header-type=", "arc-id=", "spi-flash-offset=", "image=", "elf="])
  39. except getopt.GetoptError as err:
  40. print(err)
  41. usage(2)
  42. # default filenames
  43. uboot_elf_filename = "u-boot"
  44. uboot_bin_filename = "u-boot.bin"
  45. headerised_filename = "u-boot.head"
  46. uboot_scrypt_file = "u-boot-update.txt"
  47. # default values
  48. spi_flash_offset = 0x200000
  49. header_type = "v2"
  50. arc_id = 0x53
  51. # initial header values: place where preloader will store u-boot binary,
  52. # should be equal to CONFIG_SYS_TEXT_BASE
  53. image_copy_adr = 0x81000000
  54. # initial constant header values, do not change these values
  55. magic1 = 0xdeadbeafaf # big endian byte order
  56. magic2 = [ # big endian byte order
  57. 0x20202a2020202020202020202a20202020207c5c2e20202020202e2f7c20202020207c2d,
  58. 0x2e5c2020202f2e2d7c20202020205c2020602d2d2d6020202f20202020202f205f202020,
  59. 0x205f20205c20202020207c205f60712070205f207c2020202020272e5f3d2f205c3d5f2e,
  60. 0x272020202020202020605c202f60202020202020202020202020206f2020202020202020]
  61. for opt, arg in opts:
  62. if opt in ('-h', "--help"): usage(0)
  63. if opt in ('-t', "--header-type"): header_type = arg
  64. if opt in ('-a', "--arc-id"): arc_id = int(arg, 16)
  65. if opt in ('-s', "--spi-flash-offset"): spi_flash_offset = int(arg, 16)
  66. if opt in ('-i', "--image"): uboot_bin_filename = arg
  67. if opt in ('-e', "--elf"): uboot_elf_filename = arg
  68. arg_verify(uboot_bin_filename, uboot_elf_filename, header_type)
  69. uboot_img_size = os.path.getsize(uboot_bin_filename)
  70. jump_address = elf_get_entry(uboot_elf_filename)
  71. check_sum = calc_check_sum(uboot_bin_filename)
  72. # Calculate header adresses depend on header type
  73. if header_type == "v2":
  74. image_copy_adr -= 0x4
  75. uboot_img_size += 0x4
  76. # we append image so we need to append checksum
  77. jmpchk_sum = sum(jump_address.to_bytes(4, byteorder='big'))
  78. check_sum = (check_sum + jmpchk_sum) & 0xFF
  79. imade_jump_append = True
  80. else:
  81. imade_jump_append = False
  82. # write header to file
  83. with open(headerised_filename, "wb") as file:
  84. file.write(arc_id.to_bytes(2, byteorder='little'))
  85. file.write(uboot_img_size.to_bytes(4, byteorder='little'))
  86. file.write(check_sum.to_bytes(1, byteorder='little'))
  87. file.write(image_copy_adr.to_bytes(4, byteorder='little'))
  88. file.write(magic1.to_bytes(5, byteorder='big'))
  89. for i in range(16): file.write(0x00.to_bytes(1, byteorder='little'))
  90. for byte in magic2: file.write(byte.to_bytes(36, byteorder='big'))
  91. for i in range(224 - len(magic2) * 36):
  92. file.write(0x00.to_bytes(1, byteorder='little'))
  93. if imade_jump_append:
  94. file.write(jump_address.to_bytes(4, byteorder='little'))
  95. # append u-boot image to header
  96. with open(headerised_filename, "ab") as fo:
  97. with open(uboot_bin_filename,'rb') as fi:
  98. fo.write(fi.read())
  99. # calc u-boot headerised image CRC32 (will be used by uboot update
  100. # command for check)
  101. headerised_image_crc = ""
  102. with open(headerised_filename, "rb") as fi:
  103. headerised_image_crc = hex(zlib.crc32(fi.read()) & 0xffffffff)
  104. load_addr = 0x81000000
  105. crc_store_adr = load_addr - 0x8
  106. crc_calc_adr = crc_store_adr - 0x4
  107. load_size = os.path.getsize(headerised_filename)
  108. crc_calc_cmd = \
  109. "crc32 " + hex(load_addr) + " " + hex(load_size) + " " + hex(crc_calc_adr)
  110. crc_check_cmd = \
  111. "mw.l " + hex(crc_store_adr) + " " + headerised_image_crc + " && " + \
  112. crc_calc_cmd + " && " + \
  113. "cmp.l " + hex(crc_store_adr) + " " + hex(crc_calc_adr) + " 1"
  114. # make errase size to be allighned by 64K
  115. if load_size & 0xFFFF == 0:
  116. errase_size = load_size
  117. else:
  118. errase_size = load_size - (load_size & 0xFFFF) + 0x10000
  119. # Hack to handle n25*** flash protect ops weirdness:
  120. # protect unlock return fail status is region is already unlock (entire or
  121. # partially). Same for lock ops.
  122. # As there is no possibility to check current flash status pretend
  123. # unlock & lock always success.
  124. sf_unlock_cmd = \
  125. "if sf protect unlock 0x0 0x4000000 ; then true ; else true ; fi"
  126. sf_lock_cmd = \
  127. "if sf protect lock 0x0 0x4000000 ; then true ; else true ; fi"
  128. # u-bood CMD to load u-bood with header to SPI flash
  129. sf_load_image_cmd = \
  130. "fatload mmc 0:1 " + hex(load_addr) + " " + headerised_filename + " && " + \
  131. "sf probe 0:0 && " + \
  132. sf_unlock_cmd + " && " + \
  133. "sf erase " + hex(spi_flash_offset) + " " + hex(errase_size) + " && " + \
  134. "sf write " + hex(load_addr) + " " + hex(spi_flash_offset) + " " + hex(load_size) + " && " + \
  135. sf_lock_cmd
  136. update_uboot_cmd = sf_load_image_cmd + " && echo \"u-boot update: OK\""
  137. with open(uboot_scrypt_file, "wb") as fo:
  138. fo.write(update_uboot_cmd.encode('ascii'))
  139. if __name__ == "__main__":
  140. try:
  141. main()
  142. except Exception as err:
  143. print(err)
  144. sys.exit(2)