xref: /freebsd/sys/tools/fw_stub.awk (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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$
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>* [-l name] [-m modname] [-c outfile]";
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					else
74						usage();
75				}
76			} else if (o == "m") {
77				if (length(ARGV[i]) > j) {
78					opt_m = substr(ARGV[i], j + 1);
79					break;
80				}
81				else {
82					if (++i < ARGC)
83						opt_m = ARGV[i];
84					else
85						usage();
86				}
87			} else if (o == "l") {
88				if (length(ARGV[i]) > j) {
89					opt_l = substr(ARGV[i], j + 1);
90					break;
91				}
92				else {
93					if (++i < ARGC)
94						opt_l = ARGV[i];
95					else
96						usage();
97				}
98			} else
99				usage();
100		}
101	} else {
102		split(ARGV[i], curr, ":");
103		filenames[num_files] = curr[1];
104		if (length(curr[2]) > 0)
105			shortnames[num_files] = curr[2];
106		else
107			shortnames[num_files] = curr[1];
108		if (length(curr[3]) > 0)
109			versions[num_files] = int(curr[3]);
110		else
111			versions[num_files] = 0;
112		num_files++;
113	}
114}
115
116if (!num_files || !opt_m)
117	usage();
118
119cfilename = opt_c;
120ctmpfilename = cfilename ".tmp";
121
122printc("#include <sys/param.h>\
123#include <sys/errno.h>\
124#include <sys/kernel.h>\
125#include <sys/module.h>\
126#include <sys/linker.h>\
127#include <sys/firmware.h>\
128#include <sys/systm.h>\n");
129
130if (opt_l) {
131	printc("static long " opt_l "_license_ack = 0;");
132}
133
134for (file_i = 0; file_i < num_files; file_i++) {
135	symb = filenames[file_i];
136	# '-', '.' and '/' are converted to '_' by ld/objcopy
137	gsub(/-|\.|\//, "_", symb);
138	printc("extern char _binary_" symb "_start[], _binary_" symb "_end[];");
139}
140
141printc("\nstatic int\n"\
142opt_m "_fw_modevent(module_t mod, int type, void *unused)\
143{\
144	const struct firmware *fp, *parent;\
145	int error;\
146	switch (type) {\
147	case MOD_LOAD:\n");
148
149if (opt_l) {
150		printc("\
151		TUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\
152		if (!" opt_l "_license_ack) {\
153			printf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l "/.\\n\");\
154			printf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\
155			return(EPERM);\
156		}\n");
157}
158
159for (file_i = 0; file_i < num_files; file_i++) {
160	short = shortnames[file_i];
161	symb = filenames[file_i];
162	version = versions[file_i];
163	# '-', '.' and '/' are converted to '_' by ld/objcopy
164	gsub(/-|\.|\//, "_", symb);
165
166	reg = "\t\tfp = ";
167	reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , ";
168	reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), ";
169	reg = reg version ", ";
170
171	if (file_i == 0)
172		reg = reg "NULL);";
173	else
174		reg = reg "parent);";
175
176	printc(reg);
177
178	printc("\t\tif (fp == NULL)");
179	printc("\t\t\tgoto fail_" file_i ";");
180	if (file_i == 0)
181		printc("\t\tparent = fp;");
182}
183
184printc("\t\treturn (0);");
185
186for (file_i = num_files - 1; file_i > 0; file_i--) {
187	printc("fail_" file_i ":")
188	printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");");
189}
190
191printc("\tfail_0:");
192printc("\t\treturn (ENXIO);");
193
194printc("\tcase MOD_UNLOAD:");
195
196for (file_i = 1; file_i < num_files; file_i++) {
197	printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");");
198	printc("\t\tif (error)");
199	printc("\t\t\treturn (error);");
200}
201
202printc("\t\terror = firmware_unregister(\"" shortnames[0] "\");");
203
204printc("\t\treturn (error);\
205	}\
206	return (EINVAL);\
207}\
208\
209static moduledata_t " opt_m "_fw_mod = {\
210        \"" opt_m "_fw\",\
211        " opt_m "_fw_modevent,\
212        0\
213};\
214DECLARE_MODULE(" opt_m "_fw, " opt_m "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
215MODULE_VERSION(" opt_m "_fw, 1);\
216MODULE_DEPEND(" opt_m "_fw, firmware, 1, 1, 1);\
217");
218
219if (opt_c)
220	if ((rc = system("mv -f " ctmpfilename " " cfilename))) {
221		print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \
222		    > "/dev/stderr";
223		exit 1;
224	}
225
226exit 0;
227
228}
229