blob: aba8b43559638190f1ed54d7d65615db260a32ae [file] [log] [blame]
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +00001#!/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
Sebastian Bøe6d8d4442019-01-16 14:23:40 +010011from intelhex import AddressOverlapError
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000012
13import argparse
14
15
Øyvind Rønningstad79d2e312019-05-15 10:33:09 +020016def merge_hex_files(output, input_hex_files, overlap):
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000017 ih = IntelHex()
18
19 for hex_file_path in input_hex_files:
20 to_merge = IntelHex(hex_file_path)
21
Sebastian Bøe6d8d4442019-01-16 14:23:40 +010022 # 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.
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000025 to_merge.start_addr = None
26
Sebastian Bøe6d8d4442019-01-16 14:23:40 +010027 try:
Øyvind Rønningstad79d2e312019-05-15 10:33:09 +020028 ih.merge(to_merge, overlap=overlap)
Ulf Magnusson12ba9df2019-03-19 19:28:24 +010029 except AddressOverlapError:
Sebastian Bøe6d8d4442019-01-16 14:23:40 +010030 raise AddressOverlapError("{} has merge issues".format(hex_file_path))
31
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000032 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.")
Øyvind Rønningstad79d2e312019-05-15 10:33:09 +020042 parser.add_argument("--overlap", default="error",
43 help="What to do when files overlap (error, ignore, replace). "
44 "See IntelHex.merge() for more info.")
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000045 parser.add_argument("input_files", nargs='*')
46 return parser.parse_args()
47
48
49def main():
50 args = parse_args()
51
Øyvind Rønningstad79d2e312019-05-15 10:33:09 +020052 merge_hex_files(args.output, args.input_files, args.overlap)
Håkon Øye Amundsen81c66622018-10-30 07:39:13 +000053
54
55if __name__ == "__main__":
56 main()