source: rtems-libbsd/firmware-gen.py @ 20fa599

5-freebsd-12
Last change on this file since 20fa599 was 20fa599, checked in by Christian Mauderer <Christian.Mauderer@…>, on Nov 18, 2016 at 10:11:27 AM

firmware-gen.py: Add script to generate firmware c files.

  • Property mode set to 100755
File size: 3.5 KB
Line 
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
33import argparse
34import re
35
36parser = 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
42parser.add_argument(
43    "name",
44    help="Name of the firmware",
45)
46parser.add_argument(
47    "fw_bin",
48    help="Binary firmware file.",
49    type=argparse.FileType("rb"),
50)
51parser.add_argument(
52    "out",
53    help="Output file.",
54    type=argparse.FileType("w"),
55)
56parser.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
62args = parser.parse_args()
63
64if 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
70name = args.name
71cname = re.sub(r'[^0-9a-zA-Z]', "_", name)
72
73args.out.write("#include <machine/rtems-bsd-kernel-space.h>\n")
74args.out.write("#include <sys/types.h>\n")
75args.out.write("#include <sys/kernel.h>\n")
76args.out.write("#include <sys/firmware.h>\n")
77args.out.write("\n")
78args.out.write("static const unsigned char %s[] = {" % (cname))
79count = 0
80while 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
89args.out.write("\n};\n")
90args.out.write("static const size_t %s_size = sizeof(%s);\n" % (cname, cname))
91args.out.write("\n")
92args.out.write("static void\n")
93args.out.write("%s_sysinit(void)\n" % (cname))
94args.out.write("{\n")
95args.out.write("\tconst struct firmware *fp;\n")
96args.out.write("\tfp = firmware_register(\"%s\",\n" % (name))
97args.out.write("\t    %s,\n" % (cname))
98args.out.write("\t    %s_size,\n" % (cname))
99args.out.write("\t    1, NULL);\n")
100args.out.write("\tBSD_ASSERT(fp != NULL);\n")
101args.out.write("}\n")
102args.out.write("SYSINIT(%s, SI_SUB_DRIVERS, SI_ORDER_ANY,\n" % (cname))
103args.out.write("    %s_sysinit, NULL);\n" % (cname))
104
105# vim: set ts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.