Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
combine_images.py
00001 #!/usr/bin/env python 00002 00003 # 00004 # DAPLink Interface Firmware 00005 # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved 00006 # SPDX-License-Identifier: Apache-2.0 00007 # 00008 # Licensed under the Apache License, Version 2.0 (the "License"); you may 00009 # not use this file except in compliance with the License. 00010 # You may obtain a copy of the License at 00011 # 00012 # http://www.apache.org/licenses/LICENSE-2.0 00013 # 00014 # Unless required by applicable law or agreed to in writing, software 00015 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00016 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 # See the License for the specific language governing permissions and 00018 # limitations under the License. 00019 # 00020 00021 import argparse 00022 import itertools 00023 import os.path 00024 from intelhex import IntelHex 00025 00026 00027 def ranges(i): 00028 for _, b in itertools.groupby(enumerate(i), lambda (x, y): y - x): 00029 b = list(b) 00030 yield b[0][1], b[-1][1] 00031 00032 00033 def print_hex_info(filename, intel_hex): 00034 print "File: %s" % filename 00035 addresses = intel_hex.addresses() 00036 addresses.sort() 00037 data_list = list(ranges(addresses)) 00038 for start, end in data_list: 00039 print " 0x%x, 0x%x" % (start, end) 00040 00041 00042 def merge_hex(hex1, hex2): 00043 hex1_dict = hex1.todict() 00044 hex2_dict = hex2.todict() 00045 00046 # The key "start_addr" shows up the hex 00047 # dictionary so remove it 00048 if "start_addr" in hex1_dict: 00049 del hex1_dict["start_addr"] 00050 if "start_addr" in hex2_dict: 00051 del hex2_dict["start_addr"] 00052 00053 keys = hex2_dict.keys() 00054 keys.sort() 00055 00056 # Verify nothing unexpected is in the dict 00057 for key in keys: 00058 if not type(key) in (int, long): 00059 print 'Unknown key "%s" of type %s' % (key, type(key)) 00060 00061 for key in keys: 00062 if key in hex1_dict: 00063 raise Exception("Overlapping data at 0x%x" % key) 00064 hex1_dict[key] = hex2_dict[key] 00065 return IntelHex(hex1_dict) 00066 00067 parser = argparse.ArgumentParser(description='Hex file merger') 00068 parser.add_argument("--hex", type=str, default=[], action='append', help="Hex file to add to list of files to merge. This can be specified multiple times.") 00069 parser.add_argument("--bin", nargs=2, type=str, default=[], metavar=('FILE', 'ADDR'), action='append', help="Binary file to add to list of files to merge. This can be specified multiple times.") 00070 parser.add_argument("--output_file", type=str, default='image.hex', help="Name of output hex file.") 00071 parser.add_argument("--output_bin_file", type=str, help="Name of output binary file. May be specified in addition to --output_file.") 00072 00073 00074 def main(): 00075 args = parser.parse_args() 00076 base_hex = IntelHex() 00077 # Merge in hex files 00078 for file_name in args.hex: 00079 file_name = os.path.expanduser(file_name) 00080 new_hex_data = IntelHex() 00081 print "opening file %s" % file_name 00082 new_hex_data.fromfile(file_name, format='hex') 00083 print_hex_info(file_name, new_hex_data) 00084 base_hex = merge_hex(base_hex, new_hex_data) 00085 # Merge in binary files 00086 for file_name, addr_str in args.bin: 00087 file_name = os.path.expanduser(file_name) 00088 offset = int(addr_str, 0) 00089 new_hex_data = IntelHex() 00090 new_hex_data.loadbin(file_name, offset=offset) 00091 print_hex_info(file_name, new_hex_data) 00092 base_hex = merge_hex(base_hex, new_hex_data) 00093 # Write out data 00094 print_hex_info(os.path.expanduser(args.output_file), base_hex) 00095 with open(os.path.expanduser(args.output_file), 'wb') as output_file: 00096 base_hex.tofile(output_file, 'hex') 00097 if args.output_bin_file is not None: 00098 with open(os.path.expanduser(args.output_bin_file), 'wb') as output_file: 00099 base_hex.tofile(output_file, 'bin') 00100 00101 if __name__ == '__main__': 00102 main()
Generated on Tue Jul 12 2022 15:37:14 by
1.7.2