xref: /dragonfly/sys/tools/fw_stub.awk (revision a4da4a90)
1#!/usr/bin/awk -f
2
3#-
4# Copyright (c) 2006 Max Laier.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD: src/sys/tools/fw_stub.awk,v 1.6.10.2 2009/11/02 09:47:15 fjoe Exp $
29
30#
31# Script to generate module .c file from a list of firmware images
32#
33
34function usage ()
35{
36	print "usage: fw_stub <firmware:name[:version[:parent]]>* [-l name] [-c outfile] -m modname";
37	exit 1;
38}
39
40#   These are just for convenience ...
41function printc(s)
42{
43	if (opt_c)
44		print s > ctmpfilename;
45	else
46		print s > "/dev/stdout";
47}
48
49BEGIN {
50
51#
52#   Process the command line.
53#
54
55num_files = 0;
56
57for (i = 1; i < ARGC; i++) {
58	if (ARGV[i] ~ /^-/) {
59		#
60		#   awk doesn't have getopt(), so we have to do it ourselves.
61		#   This is a bit clumsy, but it works.
62		#
63		for (j = 2; j <= length(ARGV[i]); j++) {
64			o = substr(ARGV[i], j, 1);
65			if (o == "c") {
66				if (length(ARGV[i]) > j) {
67					opt_c = substr(ARGV[i], j + 1);
68					break;
69				}
70				else {
71					if (++i < ARGC) {
72						opt_c = ARGV[i];
73						break;
74					} else {
75						usage();
76					}
77				}
78			} else if (o == "m") {
79				if (length(ARGV[i]) > j) {
80					opt_m = substr(ARGV[i], j + 1);
81					break;
82				}
83				else {
84					if (++i < ARGC) {
85						opt_m = ARGV[i];
86						break;
87					} else {
88						usage();
89					}
90				}
91			} else if (o == "l") {
92				if (length(ARGV[i]) > j) {
93					opt_l = substr(ARGV[i], j + 1);
94					break;
95				}
96				else {
97					if (++i < ARGC) {
98						opt_l = ARGV[i];
99						break;
100					} else {
101						usage();
102					}
103				}
104			} else
105				usage();
106		}
107	} else {
108		split(ARGV[i], curr, ":");
109		filenames[num_files] = curr[1];
110		if (length(curr[2]) > 0)
111			shortnames[num_files] = curr[2];
112		else
113			shortnames[num_files] = curr[1];
114		if (length(curr[3]) > 0)
115			versions[num_files] = int(curr[3]);
116		else
117			versions[num_files] = 0;
118		num_files++;
119	}
120}
121
122if (!num_files || !opt_m)
123	usage();
124
125cfilename = opt_c;
126ctmpfilename = cfilename ".tmp";
127modname = opt_m;
128gsub(/[-\.]/, "_", modname);
129
130printc("#include <sys/param.h>\n"\
131    "#include <sys/errno.h>\n"\
132    "#include <sys/kernel.h>\n"\
133    "#include <sys/module.h>\n"\
134    "#include <sys/linker.h>\n"\
135    "#include <sys/firmware.h>\n"\
136    "#include <sys/systm.h>\n");
137
138if (opt_l) {
139	printc("static long " opt_l "_license_ack = 0;");
140}
141
142for (file_i = 0; file_i < num_files; file_i++) {
143	symb = filenames[file_i];
144	# '-', '.' and '/' are converted to '_' by ld/objcopy
145	gsub(/-|\.|\//, "_", symb);
146	printc("extern char _binary_" symb "_start[], _binary_" symb "_end[];");
147}
148
149printc("");
150printc("static int\n"\
151	modname "_fw_modevent(module_t mod, int type, void *unused)\n"\
152	"{\n"\
153	"	const struct firmware *fp;");
154
155if (num_files > 1)
156	printc("\tconst struct firmware *parent;");
157
158printc("	int error;\n"\
159	"\n"\
160	"	switch (type) {\n"\
161	"	case MOD_LOAD:\n");
162
163if (opt_l) {
164	printc("");
165	printc("\tTUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\n"\
166	    "\tif (!" opt_l "_license_ack) {\n"\
167	    "\t	kprintf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l "/.\\n\");\n"\
168	    "\t	kprintf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\n"\
169	    "\t	return(EPERM);\n"\
170	    "\t}\n");
171}
172
173for (file_i = 0; file_i < num_files; file_i++) {
174	short = shortnames[file_i];
175	symb = filenames[file_i];
176	version = versions[file_i];
177	# '-', '.' and '/' are converted to '_' by ld/objcopy
178	gsub(/-|\.|\//, "_", symb);
179
180	reg = "\t\tfp = ";
181	reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , ";
182	reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), ";
183	reg = reg version ", ";
184
185	if (file_i == 0)
186		reg = reg "NULL);";
187	else
188		reg = reg "parent);";
189
190	printc(reg);
191
192	printc("\t\tif (fp == NULL)");
193	printc("\t\t\tgoto fail_" file_i ";");
194	if (file_i == 0 && num_files > 1)
195		printc("\t\tparent = fp;");
196}
197
198printc("\t\treturn (0);");
199
200for (file_i = num_files - 1; file_i > 0; file_i--) {
201	printc("fail_" file_i ":")
202	printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");");
203}
204
205printc("\tfail_0:");
206printc("\t\treturn (ENXIO);");
207
208printc("\tcase MOD_UNLOAD:");
209
210for (file_i = 1; file_i < num_files; file_i++) {
211	printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");");
212	printc("\t\tif (error)");
213	printc("\t\t\treturn (error);");
214}
215
216printc("\t\terror = firmware_unregister(\"" shortnames[0] "\");");
217
218printc("\t\treturn (error);\n"\
219    "	}\n"\
220    "	return (EINVAL);\n"\
221    "}\n"\
222    "\n"\
223    "static moduledata_t " modname "_fw_mod = {\n"\
224    "        \"" modname "_fw\",\n"\
225    "        " modname "_fw_modevent,\n"\
226    "        0\n"\
227    "};\n"\
228    "DECLARE_MODULE(" modname "_fw, " modname "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\n"\
229    "MODULE_VERSION(" modname "_fw, 1);\n"\
230    "MODULE_DEPEND(" modname "_fw, firmware, 1, 1, 1);\n");
231
232if (opt_c)
233	if ((rc = system("mv -f " ctmpfilename " " cfilename))) {
234		print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \
235		    > "/dev/stderr";
236		exit 1;
237	}
238
239exit 0;
240
241}
242