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