cfe-bin-header.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. import struct
  5. def auto_int(x):
  6. return int(x, 0)
  7. def create_header(args, size):
  8. header = struct.pack('>III', args.entry_addr, args.load_addr, size)
  9. return header
  10. def create_output(args):
  11. in_st = os.stat(args.input_file)
  12. in_size = in_st.st_size
  13. header = create_header(args, in_size)
  14. print(header)
  15. in_f = open(args.input_file, 'r+b')
  16. in_bytes = in_f.read(in_size)
  17. in_f.close()
  18. out_f = open(args.output_file, 'w+b')
  19. out_f.write(header)
  20. out_f.write(in_bytes)
  21. out_f.close()
  22. def main():
  23. global args
  24. parser = argparse.ArgumentParser(description='')
  25. parser.add_argument('--entry-addr',
  26. dest='entry_addr',
  27. action='store',
  28. type=auto_int,
  29. help='Entry Address')
  30. parser.add_argument('--input-file',
  31. dest='input_file',
  32. action='store',
  33. type=str,
  34. help='Input file')
  35. parser.add_argument('--load-addr',
  36. dest='load_addr',
  37. action='store',
  38. type=auto_int,
  39. help='Load Address')
  40. parser.add_argument('--output-file',
  41. dest='output_file',
  42. action='store',
  43. type=str,
  44. help='Output file')
  45. args = parser.parse_args()
  46. if (not args.input_file) or (not args.output_file):
  47. parser.print_help()
  48. if not args.entry_addr:
  49. args.entry_addr = 0x80010000
  50. if not args.load_addr:
  51. args.load_addr = 0x80010000
  52. create_output(args)
  53. main()