-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_flash_header.py
executable file
·61 lines (51 loc) · 1.86 KB
/
make_flash_header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
#
"""Embed flash routine as an array of bytes in a header file."""
#
# Copyright 2006 David Anderson <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import argparse
import sys
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('binary',
help='flash routine binary file')
p.add_argument('template',
help='header template')
p.add_argument('-o', '--output', metavar='output', required=True,
help='output header')
options = p.parse_args()
# Read the binary
with open(options.binary, 'rb') as f:
fwbin = f.read()
if len(fwbin) > 1024:
print("The flash driver looks too big, refusing to embed.",
file=sys.stderr)
sys.exit(1)
data = [f'0x{c:02x}' for c in fwbin]
for i in range(0, len(data), 12):
data[i] = "\n " + data[i]
data_str = ', '.join(data)
len_data = "%d" % len(data)
# Read in the template
with open(options.template, 'rt') as tplfile:
template = tplfile.read()
# Replace the values in the template
template = template.replace('___FLASH_BIN___', data_str + '\n')
template = template.replace('___FLASH_LEN___', len_data)
# Output the done header
with open(options.output, 'wt') as out:
out.write(template)