pack_res_chksum.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. #
  3. # Build Actions NVRAM config binary file
  4. #
  5. # Copyright (c) 2017 Actions Semiconductor Co., Ltd
  6. #
  7. # SPDX-License-Identifier: Apache-2.0
  8. #
  9. import os
  10. import sys
  11. import time
  12. import struct
  13. import argparse
  14. import platform
  15. import subprocess
  16. RES_CHKSUM_OFFSET = 28
  17. APP_CHKSUM_OFFSET = 28
  18. def res_read_chksum(res_bin_f):
  19. chksum = b'\x00\x00\x00\x00'
  20. with open(res_bin_f, 'rb+') as f:
  21. f.seek(RES_CHKSUM_OFFSET, 0)
  22. chksum = f.read(4)
  23. f.close()
  24. return chksum
  25. def app_write_chksum(app_bin_f, chksum, res_index):
  26. with open(app_bin_f, 'rb+') as f:
  27. f.seek(APP_CHKSUM_OFFSET + (res_index * 4), 0)
  28. f.write(chksum)
  29. f.close()
  30. print('app_write_chksum: [%d] chksum: %s' %(res_index, chksum.hex()))
  31. def pack_res_chksum(app_bin_f, res_bin_name):
  32. bin_dir = os.path.dirname(app_bin_f)
  33. res_bin_f = os.path.join(bin_dir, res_bin_name)
  34. if os.path.exists(res_bin_f):
  35. chksum = res_read_chksum(res_bin_f)
  36. if(res_bin_name == "res.bin"):
  37. res_index = 0
  38. else:
  39. return
  40. app_write_chksum(app_bin_f, chksum, res_index)
  41. print('%s -> %s: %s' %(res_bin_f, app_bin_f, chksum.hex()))
  42. def main(argv):
  43. parser = argparse.ArgumentParser(
  44. description='Build firmware',
  45. )
  46. parser.add_argument('-b', dest = 'app_bin_dst_path')
  47. parser.add_argument('-r', dest = 'res_file')
  48. args = parser.parse_args();
  49. app_bin_dst_path = args.app_bin_dst_path
  50. res_file = args.res_file
  51. pack_res_chksum(app_bin_dst_path, res_file)
  52. if __name__ == '__main__':
  53. main(sys.argv[1:])