CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/scripts/bin2hex.py
Views: 1798
1
#!/usr/bin/env python3
2
3
# Copyright (c) 2008,2010,2011,2012,2013 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
'''Intel HEX file format bin2hex convertor utility.'''
37
38
VERSION = '1.5'
39
40
if __name__ == '__main__':
41
import getopt
42
import os
43
import sys
44
45
from intelhex import bin2hex
46
47
usage = '''Bin2Hex convertor utility.
48
Usage:
49
python bin2hex.py [options] INFILE [OUTFILE]
50
51
Arguments:
52
INFILE name of bin file for processing.
53
Use '-' for reading from stdin.
54
55
OUTFILE name of output file. If omitted then output
56
will be writing to stdout.
57
58
Options:
59
-h, --help this help message.
60
-v, --version version info.
61
--offset=N offset for loading bin file (default: 0).
62
'''
63
64
offset = 0
65
66
try:
67
opts, args = getopt.getopt(sys.argv[1:], "hv",
68
["help", "version", "offset="])
69
70
for o, a in opts:
71
if o in ("-h", "--help"):
72
print(usage)
73
sys.exit(0)
74
elif o in ("-v", "--version"):
75
print(VERSION)
76
sys.exit(0)
77
elif o in ("--offset"):
78
base = 10
79
if a[:2].lower() == '0x':
80
base = 16
81
try:
82
offset = int(a, base)
83
except:
84
raise getopt.GetoptError('Bad offset value')
85
86
if not args:
87
raise getopt.GetoptError('Input file is not specified')
88
89
if len(args) > 2:
90
raise getopt.GetoptError('Too many arguments')
91
92
except getopt.GetoptError as msg:
93
txt = 'ERROR: '+str(msg) # that's required to get not-so-dumb result from 2to3 tool
94
print(txt)
95
print(usage)
96
sys.exit(2)
97
98
def force_stream_binary(stream):
99
"""Force binary mode for stream on Windows."""
100
if os.name == 'nt':
101
f_fileno = getattr(stream, 'fileno', None)
102
if f_fileno:
103
fileno = f_fileno()
104
if fileno >= 0:
105
import msvcrt
106
msvcrt.setmode(fileno, os.O_BINARY)
107
108
fin = args[0]
109
if fin == '-':
110
# read from stdin
111
fin = sys.stdin
112
force_stream_binary(fin)
113
elif not os.path.isfile(fin):
114
txt = "ERROR: File not found: %s" % fin # that's required to get not-so-dumb result from 2to3 tool
115
print(txt)
116
sys.exit(1)
117
118
if len(args) == 2:
119
fout = args[1]
120
else:
121
# write to stdout
122
fout = sys.stdout
123
force_stream_binary(fout)
124
125
sys.exit(bin2hex(fin, fout, offset))
126
127