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