headerize-hsdk.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # SPDX-License-Identifier: GPL-2.0+
  2. #
  3. # Copyright (C) 2018 Synopsys, Inc. All rights reserved.
  4. # Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  5. import os, getopt, sys, zlib
  6. from elftools.elf.elffile import ELFFile
  7. def usage(exit_code):
  8. print("usage:")
  9. print(sys.argv[0] + " --arc-id 0x52 --image u-boot.bin --elf u-boot")
  10. sys.exit(exit_code)
  11. def elf_get_entry(filename):
  12. with open(filename, 'rb') as f:
  13. elffile = ELFFile(f)
  14. return elffile.header['e_entry']
  15. def calc_check_sum(filename):
  16. # u-boot.head check_sum for preloader - it is sum of all u-boot binary bytes
  17. with open(filename, "rb") as file:
  18. ba = bytearray(file.read())
  19. return sum(ba) & 0xFF
  20. def arg_verify(uboot_bin_filename, uboot_elf_filename, arc_id):
  21. if arc_id not in [0x52, 0x53]:
  22. print("unknown ARC ID: " + hex(arc_id))
  23. sys.exit(2)
  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. def main():
  31. try:
  32. opts, args = getopt.getopt(sys.argv[1:],
  33. "ha:i:l:e:", ["help", "arc-id=", "image=", "elf="])
  34. except getopt.GetoptError as err:
  35. print(err)
  36. usage(2)
  37. # default filenames
  38. uboot_elf_filename = "u-boot"
  39. uboot_bin_filename = "u-boot.bin"
  40. headerised_filename = "u-boot.head"
  41. uboot_scrypt_file = "u-boot-update.txt"
  42. # initial header values: place where preloader will store u-boot binary,
  43. # should be equal to CONFIG_SYS_TEXT_BASE
  44. image_copy_adr = 0x81000000
  45. # initial constant header values, do not change these values
  46. arc_id = 0x52 # 0x52 for 1st HSDK release (hardcoded in RTL)
  47. magic1 = 0xdeadbeafaf # big endian byte order
  48. flash_address = 0x0
  49. flash_type = 0x0 # 0 - SPI flash, 1 - NOR flash
  50. magic2 = [ # big endian byte order
  51. 0x20202a2020202020202020202a20202020207c5c2e20202020202e2f7c20202020207c2d,
  52. 0x2e5c2020202f2e2d7c20202020205c2020602d2d2d6020202f20202020202f205f202020,
  53. 0x205f20205c20202020207c205f60712070205f207c2020202020272e5f3d2f205c3d5f2e,
  54. 0x272020202020202020605c202f60202020202020202020202020206f2020202020202020]
  55. for opt, arg in opts:
  56. if opt in ('-h', "--help"): usage(0)
  57. if opt in ('-a', "--arc-id"): arc_id = int(arg, 16)
  58. if opt in ('-i', "--image"): uboot_bin_filename = arg
  59. if opt in ('-e', "--elf"): uboot_elf_filename = arg
  60. arg_verify(uboot_bin_filename, uboot_elf_filename, arc_id)
  61. uboot_img_size = os.path.getsize(uboot_bin_filename)
  62. jump_address = elf_get_entry(uboot_elf_filename)
  63. check_sum = calc_check_sum(uboot_bin_filename)
  64. # write header to file
  65. with open(headerised_filename, "wb") as file:
  66. file.write(arc_id.to_bytes(2, byteorder='little'))
  67. file.write(uboot_img_size.to_bytes(4, byteorder='little'))
  68. file.write(check_sum.to_bytes(1, byteorder='little'))
  69. file.write(image_copy_adr.to_bytes(4, byteorder='little'))
  70. file.write(magic1.to_bytes(5, byteorder='big'))
  71. file.write(jump_address.to_bytes(4, byteorder='little'))
  72. for i in range(12): file.write(0xFF.to_bytes(1, byteorder='little'))
  73. for byte in magic2: file.write(byte.to_bytes(36, byteorder='big'))
  74. for i in range(208 - len(magic2) * 36):
  75. file.write(0xFF.to_bytes(1, byteorder='little'))
  76. file.write(flash_address.to_bytes(4, byteorder='little'))
  77. for i in range(11): file.write(0xFF.to_bytes(1, byteorder='little'))
  78. file.write(flash_type.to_bytes(1, byteorder='little'))
  79. # append u-boot image to header
  80. with open(headerised_filename, "ab") as fo:
  81. with open(uboot_bin_filename,'rb') as fi:
  82. fo.write(fi.read())
  83. # calc u-boot headerized image CRC32 (will be used by uboot update
  84. # command for check)
  85. headerised_image_crc = ""
  86. with open(headerised_filename, "rb") as fi:
  87. headerised_image_crc = hex(zlib.crc32(fi.read()) & 0xffffffff)
  88. load_addr = 0x81000000
  89. crc_store_adr = load_addr - 0x8
  90. crc_calc_adr = crc_store_adr - 0x4
  91. load_size = os.path.getsize(headerised_filename)
  92. crc_calc_cmd = \
  93. "crc32 " + hex(load_addr) + " " + hex(load_size) + " " + hex(crc_calc_adr)
  94. crc_check_cmd = \
  95. "mw.l " + hex(crc_store_adr) + " " + headerised_image_crc + " && " + \
  96. crc_calc_cmd + " && " + \
  97. "cmp.l " + hex(crc_store_adr) + " " + hex(crc_calc_adr) + " 1"
  98. # make errase size to be allighned by 64K
  99. if load_size & 0xFFFF == 0:
  100. errase_size = load_size
  101. else:
  102. errase_size = load_size - (load_size & 0xFFFF) + 0x10000
  103. # u-bood CMD to load u-bood with header to SPI flash
  104. sf_load_image_cmd = \
  105. "fatload mmc 0:1 " + hex(load_addr) + " " + headerised_filename + " && " + \
  106. "sf probe 0:0 && " + \
  107. crc_check_cmd + " && " + \
  108. "sf protect unlock 0x0 " + hex(errase_size) + " && " + \
  109. "sf erase 0x0 " + hex(errase_size) + " && " + \
  110. "sf write " + hex(load_addr) + " 0x0 " + hex(load_size) + " && " + \
  111. "sf protect lock 0x0 " + hex(errase_size)
  112. update_uboot_cmd = sf_load_image_cmd + " && echo \"u-boot update: OK\""
  113. with open(uboot_scrypt_file, "wb") as fo:
  114. fo.write(update_uboot_cmd.encode('ascii'))
  115. if __name__ == "__main__":
  116. try:
  117. main()
  118. except Exception as err:
  119. print(err)
  120. sys.exit(2)