|
| 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 | + |
| 10 | +from intelhex import IntelHex |
| 11 | + |
| 12 | +import argparse |
| 13 | + |
| 14 | + |
| 15 | +def merge_hex_files(output, input_hex_files): |
| 16 | + ih = IntelHex() |
| 17 | + |
| 18 | + for hex_file_path in input_hex_files: |
| 19 | + to_merge = IntelHex(hex_file_path) |
| 20 | + |
| 21 | + # Since 'arm-none-eabi-objcopy' incorrectly inserts record type '03 - Start Segment Address', we need to remove |
| 22 | + # the start_addr to avoid conflicts when merging. |
| 23 | + to_merge.start_addr = None |
| 24 | + |
| 25 | + ih.merge(to_merge) |
| 26 | + ih.write_hex_file(output) |
| 27 | + |
| 28 | + |
| 29 | +def parse_args(): |
| 30 | + parser = argparse.ArgumentParser( |
| 31 | + description="Merge hex files.", |
| 32 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 33 | + parser.add_argument("-o", "--output", required=False, default="merged.hex", |
| 34 | + type=argparse.FileType('w', encoding='UTF-8'), |
| 35 | + help="Output file name.") |
| 36 | + parser.add_argument("input_files", nargs='*') |
| 37 | + return parser.parse_args() |
| 38 | + |
| 39 | + |
| 40 | +def main(): |
| 41 | + args = parse_args() |
| 42 | + |
| 43 | + merge_hex_files(args.output, args.input_files) |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + main() |
0 commit comments