#!/usr/bin/env python312# flake8: noqa34# Copyright (c) 2008,2010,2011,2012,2013 Alexander Belchenko5# All rights reserved.6#7# Redistribution and use in source and binary forms,8# with or without modification, are permitted provided9# that the following conditions are met:10#11# * Redistributions of source code must retain12# the above copyright notice, this list of conditions13# and the following disclaimer.14# * Redistributions in binary form must reproduce15# the above copyright notice, this list of conditions16# and the following disclaimer in the documentation17# and/or other materials provided with the distribution.18# * Neither the name of the author nor the names19# of its contributors may be used to endorse20# or promote products derived from this software21# without specific prior written permission.22#23# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS24# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,25# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY26# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.27# IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE28# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,29# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,30# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,31# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED32# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,33# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)34# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,35# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3637'''Intel HEX file format bin2hex converter utility.'''3839VERSION = '1.5'4041if __name__ == '__main__':42import getopt43import os44import sys4546from intelhex import bin2hex4748usage = '''Bin2Hex converter utility.49Usage:50python bin2hex.py [options] INFILE [OUTFILE]5152Arguments:53INFILE name of bin file for processing.54Use '-' for reading from stdin.5556OUTFILE name of output file. If omitted then output57will be writing to stdout.5859Options:60-h, --help this help message.61-v, --version version info.62--offset=N offset for loading bin file (default: 0).63'''6465offset = 06667try:68opts, args = getopt.getopt(sys.argv[1:], "hv",69["help", "version", "offset="])7071for o, a in opts:72if o in ("-h", "--help"):73print(usage)74sys.exit(0)75elif o in ("-v", "--version"):76print(VERSION)77sys.exit(0)78elif o in ("--offset"):79base = 1080if a[:2].lower() == '0x':81base = 1682try:83offset = int(a, base)84except:85raise getopt.GetoptError('Bad offset value')8687if not args:88raise getopt.GetoptError('Input file is not specified')8990if len(args) > 2:91raise getopt.GetoptError('Too many arguments')9293except getopt.GetoptError as msg:94txt = 'ERROR: '+str(msg) # that's required to get not-so-dumb result from 2to3 tool95print(txt)96print(usage)97sys.exit(2)9899def force_stream_binary(stream):100"""Force binary mode for stream on Windows."""101if os.name == 'nt':102f_fileno = getattr(stream, 'fileno', None)103if f_fileno:104fileno = f_fileno()105if fileno >= 0:106import msvcrt107msvcrt.setmode(fileno, os.O_BINARY)108109fin = args[0]110if fin == '-':111# read from stdin112fin = sys.stdin113force_stream_binary(fin)114elif not os.path.isfile(fin):115txt = "ERROR: File not found: %s" % fin # that's required to get not-so-dumb result from 2to3 tool116print(txt)117sys.exit(1)118119if len(args) == 2:120fout = args[1]121else:122# write to stdout123fout = sys.stdout124force_stream_binary(fout)125126sys.exit(bin2hex(fin, fout, offset))127128129