1#!/usr/bin/python 2# Copyright (c) 2008-2018 Alexander Belchenko 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, 6# with or without modification, are permitted provided 7# that the following conditions are met: 8# 9# * Redistributions of source code must retain 10# the above copyright notice, this list of conditions 11# and the following disclaimer. 12# * Redistributions in binary form must reproduce 13# the above copyright notice, this list of conditions 14# and the following disclaimer in the documentation 15# and/or other materials provided with the distribution. 16# * Neither the name of the author nor the names 17# of its contributors may be used to endorse 18# or promote products derived from this software 19# without specific prior written permission. 20# 21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 23# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 24# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 26# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 27# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 29# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 33# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 35'''Intel HEX file format bin2hex convertor utility.''' 36 37VERSION = '2.3.0' 38 39if __name__ == '__main__': 40 import getopt 41 import os 42 import sys 43 44 usage = '''Bin2Hex convertor utility. 45Usage: 46 python bin2hex.py [options] INFILE [OUTFILE] 47 48Arguments: 49 INFILE name of bin file for processing. 50 Use '-' for reading from stdin. 51 52 OUTFILE name of output file. If omitted then output 53 will be writing to stdout. 54 55Options: 56 -h, --help this help message. 57 -v, --version version info. 58 --offset=N offset for loading bin file (default: 0). 59''' 60 61 offset = 0 62 63 try: 64 opts, args = getopt.getopt(sys.argv[1:], "hv", 65 ["help", "version", "offset="]) 66 67 for o, a in opts: 68 if o in ("-h", "--help"): 69 print(usage) 70 sys.exit(0) 71 elif o in ("-v", "--version"): 72 print(VERSION) 73 sys.exit(0) 74 elif o in ("--offset"): 75 base = 10 76 if a[:2].lower() == '0x': 77 base = 16 78 try: 79 offset = int(a, base) 80 except: 81 raise getopt.GetoptError('Bad offset value') 82 83 if not args: 84 raise getopt.GetoptError('Input file is not specified') 85 86 if len(args) > 2: 87 raise getopt.GetoptError('Too many arguments') 88 89 except getopt.GetoptError: 90 msg = sys.exc_info()[1] # current exception 91 txt = 'ERROR: '+str(msg) # that's required to get not-so-dumb result from 2to3 tool 92 print(txt) 93 print(usage) 94 sys.exit(2) 95 96 from intelhex import compat 97 98 fin = args[0] 99 if fin == '-': 100 # read from stdin 101 fin = compat.get_binary_stdin() 102 elif not os.path.isfile(fin): 103 txt = "ERROR: File not found: %s" % fin # that's required to get not-so-dumb result from 2to3 tool 104 print(txt) 105 sys.exit(1) 106 107 if len(args) == 2: 108 fout = args[1] 109 else: 110 # write to stdout 111 fout = sys.stdout # compat.get_binary_stdout() 112 113 from intelhex import bin2hex 114 sys.exit(bin2hex(fin, fout, offset)) 115