Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
tools/combine_images.py@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | #!/usr/bin/env python |
Pawel Zarembski |
0:01f31e923fe2 | 2 | |
Pawel Zarembski |
0:01f31e923fe2 | 3 | # |
Pawel Zarembski |
0:01f31e923fe2 | 4 | # DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 5 | # Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 6 | # SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 7 | # |
Pawel Zarembski |
0:01f31e923fe2 | 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 9 | # not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 10 | # You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 11 | # |
Pawel Zarembski |
0:01f31e923fe2 | 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 13 | # |
Pawel Zarembski |
0:01f31e923fe2 | 14 | # Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 17 | # See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 18 | # limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 19 | # |
Pawel Zarembski |
0:01f31e923fe2 | 20 | |
Pawel Zarembski |
0:01f31e923fe2 | 21 | import argparse |
Pawel Zarembski |
0:01f31e923fe2 | 22 | import itertools |
Pawel Zarembski |
0:01f31e923fe2 | 23 | import os.path |
Pawel Zarembski |
0:01f31e923fe2 | 24 | from intelhex import IntelHex |
Pawel Zarembski |
0:01f31e923fe2 | 25 | |
Pawel Zarembski |
0:01f31e923fe2 | 26 | |
Pawel Zarembski |
0:01f31e923fe2 | 27 | def ranges(i): |
Pawel Zarembski |
0:01f31e923fe2 | 28 | for _, b in itertools.groupby(enumerate(i), lambda (x, y): y - x): |
Pawel Zarembski |
0:01f31e923fe2 | 29 | b = list(b) |
Pawel Zarembski |
0:01f31e923fe2 | 30 | yield b[0][1], b[-1][1] |
Pawel Zarembski |
0:01f31e923fe2 | 31 | |
Pawel Zarembski |
0:01f31e923fe2 | 32 | |
Pawel Zarembski |
0:01f31e923fe2 | 33 | def print_hex_info(filename, intel_hex): |
Pawel Zarembski |
0:01f31e923fe2 | 34 | print "File: %s" % filename |
Pawel Zarembski |
0:01f31e923fe2 | 35 | addresses = intel_hex.addresses() |
Pawel Zarembski |
0:01f31e923fe2 | 36 | addresses.sort() |
Pawel Zarembski |
0:01f31e923fe2 | 37 | data_list = list(ranges(addresses)) |
Pawel Zarembski |
0:01f31e923fe2 | 38 | for start, end in data_list: |
Pawel Zarembski |
0:01f31e923fe2 | 39 | print " 0x%x, 0x%x" % (start, end) |
Pawel Zarembski |
0:01f31e923fe2 | 40 | |
Pawel Zarembski |
0:01f31e923fe2 | 41 | |
Pawel Zarembski |
0:01f31e923fe2 | 42 | def merge_hex(hex1, hex2): |
Pawel Zarembski |
0:01f31e923fe2 | 43 | hex1_dict = hex1.todict() |
Pawel Zarembski |
0:01f31e923fe2 | 44 | hex2_dict = hex2.todict() |
Pawel Zarembski |
0:01f31e923fe2 | 45 | |
Pawel Zarembski |
0:01f31e923fe2 | 46 | # The key "start_addr" shows up the hex |
Pawel Zarembski |
0:01f31e923fe2 | 47 | # dictionary so remove it |
Pawel Zarembski |
0:01f31e923fe2 | 48 | if "start_addr" in hex1_dict: |
Pawel Zarembski |
0:01f31e923fe2 | 49 | del hex1_dict["start_addr"] |
Pawel Zarembski |
0:01f31e923fe2 | 50 | if "start_addr" in hex2_dict: |
Pawel Zarembski |
0:01f31e923fe2 | 51 | del hex2_dict["start_addr"] |
Pawel Zarembski |
0:01f31e923fe2 | 52 | |
Pawel Zarembski |
0:01f31e923fe2 | 53 | keys = hex2_dict.keys() |
Pawel Zarembski |
0:01f31e923fe2 | 54 | keys.sort() |
Pawel Zarembski |
0:01f31e923fe2 | 55 | |
Pawel Zarembski |
0:01f31e923fe2 | 56 | # Verify nothing unexpected is in the dict |
Pawel Zarembski |
0:01f31e923fe2 | 57 | for key in keys: |
Pawel Zarembski |
0:01f31e923fe2 | 58 | if not type(key) in (int, long): |
Pawel Zarembski |
0:01f31e923fe2 | 59 | print 'Unknown key "%s" of type %s' % (key, type(key)) |
Pawel Zarembski |
0:01f31e923fe2 | 60 | |
Pawel Zarembski |
0:01f31e923fe2 | 61 | for key in keys: |
Pawel Zarembski |
0:01f31e923fe2 | 62 | if key in hex1_dict: |
Pawel Zarembski |
0:01f31e923fe2 | 63 | raise Exception("Overlapping data at 0x%x" % key) |
Pawel Zarembski |
0:01f31e923fe2 | 64 | hex1_dict[key] = hex2_dict[key] |
Pawel Zarembski |
0:01f31e923fe2 | 65 | return IntelHex(hex1_dict) |
Pawel Zarembski |
0:01f31e923fe2 | 66 | |
Pawel Zarembski |
0:01f31e923fe2 | 67 | parser = argparse.ArgumentParser(description='Hex file merger') |
Pawel Zarembski |
0:01f31e923fe2 | 68 | 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.") |
Pawel Zarembski |
0:01f31e923fe2 | 69 | 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.") |
Pawel Zarembski |
0:01f31e923fe2 | 70 | parser.add_argument("--output_file", type=str, default='image.hex', help="Name of output hex file.") |
Pawel Zarembski |
0:01f31e923fe2 | 71 | parser.add_argument("--output_bin_file", type=str, help="Name of output binary file. May be specified in addition to --output_file.") |
Pawel Zarembski |
0:01f31e923fe2 | 72 | |
Pawel Zarembski |
0:01f31e923fe2 | 73 | |
Pawel Zarembski |
0:01f31e923fe2 | 74 | def main(): |
Pawel Zarembski |
0:01f31e923fe2 | 75 | args = parser.parse_args() |
Pawel Zarembski |
0:01f31e923fe2 | 76 | base_hex = IntelHex() |
Pawel Zarembski |
0:01f31e923fe2 | 77 | # Merge in hex files |
Pawel Zarembski |
0:01f31e923fe2 | 78 | for file_name in args.hex: |
Pawel Zarembski |
0:01f31e923fe2 | 79 | file_name = os.path.expanduser(file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 80 | new_hex_data = IntelHex() |
Pawel Zarembski |
0:01f31e923fe2 | 81 | print "opening file %s" % file_name |
Pawel Zarembski |
0:01f31e923fe2 | 82 | new_hex_data.fromfile(file_name, format='hex') |
Pawel Zarembski |
0:01f31e923fe2 | 83 | print_hex_info(file_name, new_hex_data) |
Pawel Zarembski |
0:01f31e923fe2 | 84 | base_hex = merge_hex(base_hex, new_hex_data) |
Pawel Zarembski |
0:01f31e923fe2 | 85 | # Merge in binary files |
Pawel Zarembski |
0:01f31e923fe2 | 86 | for file_name, addr_str in args.bin: |
Pawel Zarembski |
0:01f31e923fe2 | 87 | file_name = os.path.expanduser(file_name) |
Pawel Zarembski |
0:01f31e923fe2 | 88 | offset = int(addr_str, 0) |
Pawel Zarembski |
0:01f31e923fe2 | 89 | new_hex_data = IntelHex() |
Pawel Zarembski |
0:01f31e923fe2 | 90 | new_hex_data.loadbin(file_name, offset=offset) |
Pawel Zarembski |
0:01f31e923fe2 | 91 | print_hex_info(file_name, new_hex_data) |
Pawel Zarembski |
0:01f31e923fe2 | 92 | base_hex = merge_hex(base_hex, new_hex_data) |
Pawel Zarembski |
0:01f31e923fe2 | 93 | # Write out data |
Pawel Zarembski |
0:01f31e923fe2 | 94 | print_hex_info(os.path.expanduser(args.output_file), base_hex) |
Pawel Zarembski |
0:01f31e923fe2 | 95 | with open(os.path.expanduser(args.output_file), 'wb') as output_file: |
Pawel Zarembski |
0:01f31e923fe2 | 96 | base_hex.tofile(output_file, 'hex') |
Pawel Zarembski |
0:01f31e923fe2 | 97 | if args.output_bin_file is not None: |
Pawel Zarembski |
0:01f31e923fe2 | 98 | with open(os.path.expanduser(args.output_bin_file), 'wb') as output_file: |
Pawel Zarembski |
0:01f31e923fe2 | 99 | base_hex.tofile(output_file, 'bin') |
Pawel Zarembski |
0:01f31e923fe2 | 100 | |
Pawel Zarembski |
0:01f31e923fe2 | 101 | if __name__ == '__main__': |
Pawel Zarembski |
0:01f31e923fe2 | 102 | main() |