|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Copyright (c) 2008-2018 Alexander Belchenko |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, |
| 7 | +# with or without modification, are permitted provided |
| 8 | +# that the following conditions are met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain |
| 11 | +# the above copyright notice, this list of conditions |
| 12 | +# and the following disclaimer. |
| 13 | +# * Redistributions in binary form must reproduce |
| 14 | +# the above copyright notice, this list of conditions |
| 15 | +# and the following disclaimer in the documentation |
| 16 | +# and/or other materials provided with the distribution. |
| 17 | +# * Neither the name of the author nor the names |
| 18 | +# of its contributors may be used to endorse |
| 19 | +# or promote products derived from this software |
| 20 | +# without specific prior written permission. |
| 21 | +# |
| 22 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 24 | +# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 25 | +# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 26 | +# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 27 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 28 | +# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 30 | +# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 31 | +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 34 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + |
| 36 | +"""Merge content of several hex files into one file.""" |
| 37 | + |
| 38 | +VERSION = '2.2.1' |
| 39 | + |
| 40 | +USAGE = '''hexmerge: merge content of hex files. |
| 41 | +Usage: |
| 42 | + python hexmerge.py [options] FILES... |
| 43 | +
|
| 44 | +Options: |
| 45 | + -h, --help this help message. |
| 46 | + -v, --version version info. |
| 47 | + -o, --output=FILENAME output file name (emit output to stdout |
| 48 | + if option is not specified) |
| 49 | + -r, --range=START:END specify address range for output |
| 50 | + (ascii hex value). Both values are inclusive. |
| 51 | + Range can be in form 'START:' or ':END'. |
| 52 | + --no-start-addr Don't write start addr to output file. |
| 53 | + --overlap=METHOD What to do when data in files overlapped. |
| 54 | + Supported variants: |
| 55 | + * error -- stop and show error message (default) |
| 56 | + * ignore -- keep data from first file that |
| 57 | + contains data at overlapped address |
| 58 | + * replace -- use data from last file that |
| 59 | + contains data at overlapped address |
| 60 | +
|
| 61 | +Arguments: |
| 62 | + FILES list of hex files for merging |
| 63 | + (use '-' to read content from stdin) |
| 64 | +
|
| 65 | +You can specify address range for each file in the form: |
| 66 | +
|
| 67 | + filename:START:END |
| 68 | +
|
| 69 | +See description of range option above. |
| 70 | +
|
| 71 | +You can omit START or END, so supported variants are: |
| 72 | +
|
| 73 | + filename:START: read filename and use data starting from START addr |
| 74 | + filename::END read filename and use data till END addr |
| 75 | +
|
| 76 | +Use entire file content: |
| 77 | +
|
| 78 | + filename |
| 79 | +or |
| 80 | + filename:: |
| 81 | +''' |
| 82 | + |
| 83 | +import sys |
| 84 | + |
| 85 | + |
| 86 | +def main(args=None): |
| 87 | + import getopt |
| 88 | + |
| 89 | + output = None |
| 90 | + start = None |
| 91 | + end = None |
| 92 | + write_start_addr = True |
| 93 | + overlap = 'error' |
| 94 | + |
| 95 | + if args is None: |
| 96 | + args = sys.argv[1:] |
| 97 | + try: |
| 98 | + opts, args = getopt.gnu_getopt(args, 'hvo:r:', |
| 99 | + ['help', 'version', |
| 100 | + 'output=', 'range=', |
| 101 | + 'no-start-addr', 'overlap=', |
| 102 | + ]) |
| 103 | + |
| 104 | + for o,a in opts: |
| 105 | + if o in ('-h', '--help'): |
| 106 | + print(USAGE) |
| 107 | + return 0 |
| 108 | + elif o in ('-v', '--version'): |
| 109 | + print(VERSION) |
| 110 | + return 0 |
| 111 | + elif o in ('-o', '--output'): |
| 112 | + output = a |
| 113 | + elif o in ("-r", "--range"): |
| 114 | + try: |
| 115 | + l = a.split(":") |
| 116 | + if l[0] != '': |
| 117 | + start = int(l[0], 16) |
| 118 | + if l[1] != '': |
| 119 | + end = int(l[1], 16) |
| 120 | + except (ValueError, IndexError): |
| 121 | + raise getopt.GetoptError('Bad range value(s)') |
| 122 | + elif o == '--no-start-addr': |
| 123 | + write_start_addr = False |
| 124 | + elif o == '--overlap': |
| 125 | + if a in ('error', 'ignore', 'replace'): |
| 126 | + overlap = a |
| 127 | + else: |
| 128 | + raise getopt.GetoptError('Bad overlap value') |
| 129 | + |
| 130 | + if len(args) == 0: |
| 131 | + raise getopt.GetoptError('You should specify file list') |
| 132 | + |
| 133 | + except getopt.GetoptError: |
| 134 | + e = sys.exc_info()[1] # current exception |
| 135 | + sys.stderr.write(str(e)+"\n") |
| 136 | + sys.stderr.write(USAGE+"\n") |
| 137 | + return 1 |
| 138 | + |
| 139 | + import intelhex |
| 140 | + # TODO: move actual merge code into intelhex package as helper function |
| 141 | + # and write couple of tests for it. |
| 142 | + res = intelhex.IntelHex() |
| 143 | + |
| 144 | + def end_addr_inclusive(addr): |
| 145 | + if addr is not None: |
| 146 | + return addr + 1 |
| 147 | + return addr |
| 148 | + |
| 149 | + for f in args: |
| 150 | + try: |
| 151 | + fname, fstart, fend = intelhex._get_file_and_addr_range(f) |
| 152 | + except intelhex._BadFileNotation: |
| 153 | + sys.stderr.write('Bad argument: "%s"\n' % f) |
| 154 | + sys.stderr.write(USAGE+"\n") |
| 155 | + return 1 |
| 156 | + if fname == '-': |
| 157 | + fname = sys.stdin |
| 158 | + ih = intelhex.IntelHex(fname) |
| 159 | + if (fstart, fend) != (None, None): |
| 160 | + ih = ih[fstart:end_addr_inclusive(fend)] |
| 161 | + try: |
| 162 | + res.merge(ih, overlap) |
| 163 | + except intelhex.AddressOverlapError: |
| 164 | + e = sys.exc_info()[1] # current exception |
| 165 | + sys.stderr.write('Merging: '+fname+"\n") |
| 166 | + sys.stderr.write(str(e)+"\n") |
| 167 | + return 1 |
| 168 | + |
| 169 | + if (start, end) != (None, None): |
| 170 | + res = res[start:end_addr_inclusive(end)] |
| 171 | + if output is None: |
| 172 | + output = sys.stdout |
| 173 | + res.write_hex_file(output, write_start_addr) |
| 174 | + return 0 |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + sys.exit(main()) |
0 commit comments