-
Notifications
You must be signed in to change notification settings - Fork 57
/
firmware-gen.py
executable file
·105 lines (95 loc) · 3.46 KB
/
firmware-gen.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016 embedded brains GmbH. All rights reserved.
#
# embedded brains GmbH
# Dornierstr. 4
# 82178 Puchheim
# Germany
# <rtems@embedded-brains.de>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
import argparse
import re
parser = argparse.ArgumentParser(
description=(
"Convert a binary firmware file to a rtems-libbsd firmware c file. "
"Note that you have to convert the FreeBSD uu-encoded files first."
))
parser.add_argument(
"name",
help="Name of the firmware",
)
parser.add_argument(
"fw_bin",
help="Binary firmware file.",
type=argparse.FileType("rb"),
)
parser.add_argument(
"out",
help="Output file.",
type=argparse.FileType("w"),
)
parser.add_argument(
"-l", "--license",
help="License file. Will be formatted as a comment and put on top.",
type=argparse.FileType("r"),
)
args = parser.parse_args()
if args.license is not None:
args.out.write("/*\n")
for line in args.license:
args.out.write(" * " + line)
args.out.write(" */\n")
name = args.name
cname = re.sub(r'[^0-9a-zA-Z]', "_", name)
args.out.write("#include <machine/rtems-bsd-kernel-space.h>\n")
args.out.write("#include <sys/types.h>\n")
args.out.write("#include <sys/kernel.h>\n")
args.out.write("#include <sys/firmware.h>\n")
args.out.write("\n")
args.out.write("static const unsigned char %s[] = {" % (cname))
count = 0
while True:
c = args.fw_bin.read(1)
if not c:
break
if count % 12 == 0:
args.out.write("\n\t")
count = count + 1
args.out.write("0x%02x, " % ord(c))
args.out.write("\n};\n")
args.out.write("static const size_t %s_size = sizeof(%s);\n" % (cname, cname))
args.out.write("\n")
args.out.write("static void\n")
args.out.write("%s_sysinit(void)\n" % (cname))
args.out.write("{\n")
args.out.write("\tconst struct firmware *fp;\n")
args.out.write("\tfp = firmware_register(\"%s\",\n" % (name))
args.out.write("\t %s,\n" % (cname))
args.out.write("\t %s_size,\n" % (cname))
args.out.write("\t 1, NULL);\n")
args.out.write("\tBSD_ASSERT(fp != NULL);\n")
args.out.write("}\n")
args.out.write("SYSINIT(%s, SI_SUB_DRIVERS, SI_ORDER_ANY,\n" % (cname))
args.out.write(" %s_sysinit, NULL);\n" % (cname))
# vim: set ts=4 sw=4 et: