makeimg_fls.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding:utf-8 -*_
  2. #
  3. # W600 make img script
  4. # Copyright (c) 2018 Winner Micro Electronic Design Co., Ltd.
  5. # All rights reserved.
  6. #
  7. # image header structure
  8. # --------------------------------------------------------------------------------------------
  9. # | magic number(4B) | image type(2B)| zip type(2B) | run image addr(4B) | run image len(4B) |
  10. # --------------------------------------------------------------------------------------------
  11. # | run image crc(4B) | update image addr(4B) | update image len(4B) | update image crc(4B) |
  12. # ---------------------------------------------------------------------------------------------
  13. # | update no(4B) | ver(16B) |
  14. # ---------------------------------------------------------------------------------------------
  15. #
  16. #
  17. # FLS File structure
  18. # ----------------------------------------------
  19. # | FLS file header (image header) |
  20. # ------------------------------------
  21. # | secboot header (image header) |
  22. # ------------------------------------
  23. # | 256-sizeof(image header) pad 0xFF |
  24. # ------------------------------------
  25. # | secboot img area-(56*1024 - 256) |
  26. # ------------------------------------
  27. # | application image header (image header) |
  28. # ------------------------------------
  29. # | 256-sizeof(image heager)-pad 0xFF |
  30. # ------------------------------------
  31. # | application image |
  32. # ------------------------------------------------
  33. #
  34. #
  35. import sys
  36. import os
  37. import struct
  38. from zlib import crc32
  39. secboot_img_addr = 0x2100
  40. secboot_header_len = 0x100
  41. secboot_header_pos = secboot_img_addr - secboot_header_len
  42. secboot_img_total_len = 56*1024
  43. run_img_header_len = 0x100
  44. magic_no = 0xA0FFFF9F
  45. def main(argv):
  46. if(len(argv) != 4):
  47. print("param cnt error")
  48. print("param 0: .py")
  49. print("param 1: input \"secboot.img\" file")
  50. print("param 2: input image file")
  51. print("param 3: output FLS file")
  52. raise Exception("param cnt error!")
  53. # second boot
  54. try:
  55. f_sec = open(argv[1], "rb+")
  56. except IOError:
  57. print("not find %s file" % argv[1])
  58. raise
  59. else:
  60. magic_word = f_sec.read(4)
  61. magic, = struct.unpack('<I', magic_word)
  62. if magic != magic_no:
  63. f_sec.close()
  64. raise Exception("input %s file error" % argv[1])
  65. # app image
  66. try:
  67. f_app=open(argv[2], "rb+")
  68. except IOError:
  69. f_sec.close()
  70. print("not find %s file" % argv[2])
  71. raise
  72. else:
  73. magic_word=f_app.read(4)
  74. magic, = struct.unpack('<I', magic_word)
  75. if magic != magic_no:
  76. f_sec.close()
  77. f_app.close()
  78. raise Exception("input %s file error" % argv[1])
  79. appimg_len=os.path.getsize(argv[2])
  80. # create outfile
  81. try:
  82. f_out=open(argv[3], 'wb+')
  83. except IOError:
  84. f_app.close()
  85. f_sec.close()
  86. print("create %s file error" % argv[3])
  87. raise
  88. else:
  89. final_len=secboot_img_total_len + appimg_len
  90. data = struct.pack('<B', 0xff) * final_len
  91. f_out.write(data)
  92. # Write SECBOOT header to output file
  93. f_out.seek(56)
  94. f_sec.seek(0)
  95. f_out.write(f_sec.read(56))
  96. # Write SECBOOT image to output file
  97. f_out.seek(56 + 256)
  98. f_out.write(f_sec.read())
  99. # Write run img header to output file
  100. f_out.seek(56 + secboot_img_total_len)
  101. f_app.seek(0)
  102. f_out.write(f_app.read(56))
  103. # Write run img to output file
  104. f_out.seek(56 + secboot_img_total_len + run_img_header_len)
  105. f_out.write(f_app.read())
  106. # Calculate CRC from secboot header
  107. f_out.seek(56)
  108. out_data = f_out.read()
  109. out_crc = crc32(out_data) ^ (0xFFFFFFFF)
  110. out_len = len(out_data)
  111. #print(hex(out_crc))
  112. magic = struct.pack('<I', magic_no)
  113. img_type = struct.pack('<H', 0)
  114. zip_type = struct.pack('<H', 0)
  115. run_img_addr = struct.pack('<I', secboot_header_pos)
  116. upd_img_addr = struct.pack('<I', 0)
  117. upd_img_len = struct.pack('<I', 0)
  118. upd_checksum = struct.pack('<I', 0)
  119. upd_no = struct.pack('<I', 0)
  120. version = '\0' * 16
  121. ver_name = version.encode('utf-8')
  122. run_img_len = struct.pack('<I', out_len)
  123. run_org_checksum = struct.pack('<I', out_crc)
  124. # image header crc
  125. image_header=magic + img_type + zip_type + run_img_addr + run_img_len \
  126. + run_org_checksum + upd_img_addr + upd_img_len + upd_checksum \
  127. + upd_no + ver_name
  128. header_crc=crc32(image_header) ^ (0xFFFFFFFF)
  129. # Write fls's header to output file
  130. f_out.seek(0)
  131. f_out.write(image_header)
  132. f_out.write(struct.pack('<I', header_crc))
  133. f_sec.close()
  134. f_app.close()
  135. f_out.close()
  136. print("create %s file success!" % argv[3])
  137. print("%s size:%dKB. %s size:%dKB" % (argv[2], appimg_len / 1024.0 , argv[3], final_len / 1024.0))
  138. if __name__ == '__main__':
  139. main(sys.argv)