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