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