mergehex.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2018 Nordic Semiconductor ASA
  4. #
  5. # SPDX-License-Identifier: Apache-2.0
  6. # This merges a set of input hex files into a single output hex file.
  7. # Any conflicts will result in an error being reported.
  8. from intelhex import IntelHex
  9. from intelhex import AddressOverlapError
  10. import argparse
  11. def merge_hex_files(output, input_hex_files, overlap):
  12. ih = IntelHex()
  13. for hex_file_path in input_hex_files:
  14. to_merge = IntelHex(hex_file_path)
  15. # Since 'arm-none-eabi-objcopy' incorrectly inserts record
  16. # type '03 - Start Segment Address', we need to remove the
  17. # start_addr to avoid conflicts when merging.
  18. to_merge.start_addr = None
  19. try:
  20. ih.merge(to_merge, overlap=overlap)
  21. except AddressOverlapError:
  22. raise AddressOverlapError("{} has merge issues".format(hex_file_path))
  23. ih.write_hex_file(output)
  24. def parse_args():
  25. parser = argparse.ArgumentParser(
  26. description="Merge hex files.",
  27. formatter_class=argparse.RawDescriptionHelpFormatter)
  28. parser.add_argument("-o", "--output", required=False, default="merged.hex",
  29. type=argparse.FileType('w', encoding='UTF-8'),
  30. help="Output file name.")
  31. parser.add_argument("--overlap", default="error",
  32. help="What to do when files overlap (error, ignore, replace). "
  33. "See IntelHex.merge() for more info.")
  34. parser.add_argument("input_files", nargs='*')
  35. return parser.parse_args()
  36. def main():
  37. args = parse_args()
  38. merge_hex_files(args.output, args.input_files, args.overlap)
  39. if __name__ == "__main__":
  40. main()