fw_stub.awk revision 167165
1221167Sgnn#!/usr/bin/awk -f
2221167Sgnn
3221167Sgnn#-
4221167Sgnn# Copyright (c) 2006 Max Laier.
5221167Sgnn# All rights reserved.
6221167Sgnn#
7221167Sgnn# Redistribution and use in source and binary forms, with or without
8221167Sgnn# modification, are permitted provided that the following conditions
9221167Sgnn# are met:
10221167Sgnn# 1. Redistributions of source code must retain the above copyright
11221167Sgnn#    notice, this list of conditions and the following disclaimer.
12221167Sgnn# 2. Redistributions in binary form must reproduce the above copyright
13221167Sgnn#    notice, this list of conditions and the following disclaimer in the
14221167Sgnn#    documentation and/or other materials provided with the distribution.
15221167Sgnn#
16221167Sgnn# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
17221167Sgnn# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18221167Sgnn# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19221167Sgnn# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20221167Sgnn# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21221167Sgnn# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22221167Sgnn# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23221167Sgnn# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24221167Sgnn# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25221167Sgnn# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26221167Sgnn# SUCH DAMAGE.
27221167Sgnn#
28221167Sgnn# $FreeBSD: head/sys/tools/fw_stub.awk 167165 2007-03-02 11:42:56Z flz $
29221167Sgnn
30221167Sgnn#
31221167Sgnn# Script to generate module .c file from a list of firmware images
32221167Sgnn#
33221167Sgnn
34221167Sgnnfunction usage ()
35221167Sgnn{
36221167Sgnn	print "usage: fw_stub <firmware:name>* [-l name] [-m modname] [-c outfile]";
37221167Sgnn	exit 1;
38221167Sgnn}
39221167Sgnn
40221167Sgnn#   These are just for convenience ...
41221167Sgnnfunction printc(s)
42221167Sgnn{
43221167Sgnn	if (opt_c)
44221167Sgnn		print s > ctmpfilename;
45221167Sgnn	else
46221167Sgnn		print s > "/dev/stdout";
47221167Sgnn}
48221167Sgnn
49221167SgnnBEGIN {
50221167Sgnn
51221167Sgnn#
52221167Sgnn#   Process the command line.
53221167Sgnn#
54221167Sgnn
55221167Sgnnnum_files = 0;
56221167Sgnn
57221167Sgnnfor (i = 1; i < ARGC; i++) {
58221167Sgnn	if (ARGV[i] ~ /^-/) {
59221167Sgnn		#
60221167Sgnn		#   awk doesn't have getopt(), so we have to do it ourselves.
61221167Sgnn		#   This is a bit clumsy, but it works.
62221167Sgnn		#
63221167Sgnn		for (j = 2; j <= length(ARGV[i]); j++) {
64221167Sgnn			o = substr(ARGV[i], j, 1);
65221167Sgnn			if (o == "c") {
66221167Sgnn				if (length(ARGV[i]) > j) {
67221167Sgnn					opt_c = substr(ARGV[i], j + 1);
68221167Sgnn					break;
69221167Sgnn				}
70221167Sgnn				else {
71221167Sgnn					if (++i < ARGC)
72221167Sgnn						opt_c = ARGV[i];
73221167Sgnn					else
74221167Sgnn						usage();
75221167Sgnn				}
76221167Sgnn			} else if (o == "m") {
77221167Sgnn				if (length(ARGV[i]) > j) {
78221167Sgnn					opt_m = substr(ARGV[i], j + 1);
79221167Sgnn					break;
80221167Sgnn				}
81221167Sgnn				else {
82221167Sgnn					if (++i < ARGC)
83221167Sgnn						opt_m = ARGV[i];
84221167Sgnn					else
85221167Sgnn						usage();
86221167Sgnn				}
87221167Sgnn			} else if (o == "l") {
88221167Sgnn				if (length(ARGV[i]) > j) {
89221167Sgnn					opt_l = substr(ARGV[i], j + 1);
90221167Sgnn					break;
91221167Sgnn				}
92221167Sgnn				else {
93221167Sgnn					if (++i < ARGC)
94221167Sgnn						opt_l = ARGV[i];
95221167Sgnn					else
96221167Sgnn						usage();
97221167Sgnn				}
98221167Sgnn			} else
99221167Sgnn				usage();
100221167Sgnn		}
101221167Sgnn	} else {
102221167Sgnn		split(ARGV[i], curr, ":");
103221167Sgnn		filenames[num_files] = curr[1];
104221167Sgnn		if (length(curr[2]) > 0)
105221167Sgnn			shortnames[num_files] = curr[2];
106221167Sgnn		else
107221167Sgnn			shortnames[num_files] = curr[1];
108221167Sgnn		if (length(curr[3]) > 0)
109221167Sgnn			versions[num_files] = int(curr[3]);
110221167Sgnn		else
111221167Sgnn			versions[num_files] = 0;
112221167Sgnn		num_files++;
113221167Sgnn	}
114221167Sgnn}
115221167Sgnn
116221167Sgnnif (!num_files || !opt_m)
117221167Sgnn	usage();
118221167Sgnn
119221167Sgnncfilename = opt_c;
120221167Sgnnctmpfilename = cfilename ".tmp";
121221167Sgnn
122221167Sgnnprintc("#include <sys/param.h>\
123221167Sgnn#include <sys/errno.h>\
124221167Sgnn#include <sys/kernel.h>\
125221167Sgnn#include <sys/module.h>\
126221167Sgnn#include <sys/linker.h>\
127221167Sgnn#include <sys/firmware.h>\
128221167Sgnn#include <sys/systm.h>\n");
129221167Sgnn
130221167Sgnnif (opt_l) {
131221167Sgnn	printc("static long " opt_l "_license_ack = 0;");
132221167Sgnn}
133221167Sgnn
134221167Sgnnfor (file_i = 0; file_i < num_files; file_i++) {
135221167Sgnn	symb = filenames[file_i];
136221167Sgnn	# '-', '.' and '/' are converted to '_' by ld/objcopy
137221167Sgnn	gsub(/-|\.|\//, "_", symb);
138221167Sgnn	printc("extern char _binary_" symb "_start[], _binary_" symb "_end[];");
139221167Sgnn}
140221167Sgnn
141221167Sgnnprintc("\nstatic int\n"\
142221167Sgnnopt_m "_fw_modevent(module_t mod, int type, void *unused)\
143221167Sgnn{\
144221167Sgnn	const struct firmware *fp, *parent;\
145221167Sgnn	int error;\
146221167Sgnn	switch (type) {\
147221167Sgnn	case MOD_LOAD:\n");
148221167Sgnn
149221167Sgnnif (opt_l) {
150221167Sgnn		printc("\
151221167Sgnn		TUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\
152221167Sgnn		if (!" opt_l "_license_ack) {\
153221167Sgnn			printf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l "/.\\n\");\
154221167Sgnn			printf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\
155221167Sgnn			return(EPERM);\
156221167Sgnn		}\n");
157221167Sgnn}
158221167Sgnn
159221167Sgnnfor (file_i = 0; file_i < num_files; file_i++) {
160221167Sgnn	short = shortnames[file_i];
161221167Sgnn	symb = filenames[file_i];
162221167Sgnn	version = versions[file_i];
163221167Sgnn	# '-', '.' and '/' are converted to '_' by ld/objcopy
164221167Sgnn	gsub(/-|\.|\//, "_", symb);
165221167Sgnn
166221167Sgnn	reg = "\t\tfp = ";
167221167Sgnn	reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , ";
168221167Sgnn	reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), ";
169221167Sgnn	reg = reg version ", ";
170221167Sgnn
171221167Sgnn	if (file_i == 0)
172221167Sgnn		reg = reg "NULL);";
173221167Sgnn	else
174221167Sgnn		reg = reg "parent);";
175221167Sgnn
176221167Sgnn	printc(reg);
177221167Sgnn
178221167Sgnn	printc("\t\tif (fp == NULL)");
179221167Sgnn	printc("\t\t\tgoto fail_" file_i ";");
180221167Sgnn	if (file_i == 0)
181221167Sgnn		printc("\t\tparent = fp;");
182221167Sgnn}
183221167Sgnn
184221167Sgnnprintc("\t\treturn (0);");
185221167Sgnn
186221167Sgnnfor (file_i = num_files - 1; file_i > 0; file_i--) {
187221167Sgnn	printc("fail_" file_i ":")
188221167Sgnn	printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");");
189221167Sgnn}
190221167Sgnn
191221167Sgnnprintc("\tfail_0:");
192221167Sgnnprintc("\t\treturn (ENXIO);");
193221167Sgnn
194221167Sgnnprintc("\tcase MOD_UNLOAD:");
195221167Sgnn
196221167Sgnnfor (file_i = 1; file_i < num_files; file_i++) {
197221167Sgnn	printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");");
198221167Sgnn	printc("\t\tif (error)");
199221167Sgnn	printc("\t\t\treturn (error);");
200221167Sgnn}
201221167Sgnn
202221167Sgnnprintc("\t\terror = firmware_unregister(\"" shortnames[0] "\");");
203221167Sgnn
204221167Sgnnprintc("\t\treturn (error);\
205221167Sgnn	}\
206221167Sgnn	return (EINVAL);\
207221167Sgnn}\
208221167Sgnn\
209221167Sgnnstatic moduledata_t " opt_m "_fw_mod = {\
210221167Sgnn        \"" opt_m "_fw\",\
211221167Sgnn        " opt_m "_fw_modevent,\
212221167Sgnn        0\
213221167Sgnn};\
214221167SgnnDECLARE_MODULE(" opt_m "_fw, " opt_m "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
215221167SgnnMODULE_VERSION(" opt_m "_fw, 1);\
216221167SgnnMODULE_DEPEND(" opt_m "_fw, firmware, 1, 1, 1);\
217221167Sgnn");
218221167Sgnn
219221167Sgnnif (opt_c)
220221167Sgnn	if ((rc = system("mv -f " ctmpfilename " " cfilename))) {
221221167Sgnn		print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \
222221167Sgnn		    > "/dev/stderr";
223221167Sgnn		exit 1;
224221167Sgnn	}
225221167Sgnn
226221167Sgnnexit 0;
227221167Sgnn
228221167Sgnn}
229221167Sgnn