4 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 # Copyright (c) 2006 Max Laier.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
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.
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
33 # Script to generate module .c file from a list of firmware images
38 print "usage: fw_stub <firmware:name>* [-l name] [-m modname] [-c outfile]";
42 # These are just for convenience ...
46 print s > ctmpfilename;
48 print s > "/dev/stdout";
54 # Process the command line.
59 for (i = 1; i < ARGC; i++) {
62 # awk doesn't have getopt(), so we have to do it ourselves.
63 # This is a bit clumsy, but it works.
65 for (j = 2; j <= length(ARGV[i]); j++) {
66 o = substr(ARGV[i], j, 1);
68 if (length(ARGV[i]) > j) {
69 opt_c = substr(ARGV[i], j + 1);
78 } else if (o == "m") {
79 if (length(ARGV[i]) > j) {
80 opt_m = substr(ARGV[i], j + 1);
89 } else if (o == "l") {
90 if (length(ARGV[i]) > j) {
91 opt_l = substr(ARGV[i], j + 1);
104 split(ARGV[i], curr, ":");
105 filenames[num_files] = curr[1];
106 if (length(curr[2]) > 0)
107 shortnames[num_files] = curr[2];
109 shortnames[num_files] = curr[1];
110 if (length(curr[3]) > 0)
111 versions[num_files] = int(curr[3]);
113 versions[num_files] = 0;
118 if (!num_files || !opt_m)
122 ctmpfilename = cfilename ".tmp";
124 gsub(/[-\.]/, "_", modname);
127 * Automatically generated by:\
130 printc("#include <sys/param.h>");
131 printc("#include <sys/errno.h>");
132 printc("#include <sys/kernel.h>");
133 printc("#include <sys/module.h>");
134 printc("#include <sys/linker.h>");
135 printc("#include <sys/firmware.h>");
136 printc("#include <sys/systm.h>\n");
139 printc("static long " opt_l "_license_ack = 0;");
142 for (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[];");
149 printc("\nstatic int\n"\
150 modname "_fw_modevent(module_t mod, int type, void *unused)\
152 const struct firmware *fp, *parent;\
159 TUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\
160 if (!" opt_l "_license_ack) {\
161 printf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l ".LICENSE.\\n\");\
162 printf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\
167 for (file_i = 0; file_i < num_files; file_i++) {
168 short = shortnames[file_i];
169 symb = filenames[file_i];
170 version = versions[file_i];
171 # '-', '.' and '/' are converted to '_' by ld/objcopy
172 gsub(/-|\.|\//, "_", symb);
175 reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , ";
176 reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), ";
177 reg = reg version ", ";
182 reg = reg "parent);";
186 printc("\t\tif (fp == NULL)");
187 printc("\t\t\tgoto fail_" file_i ";");
189 printc("\t\tparent = fp;");
192 printc("\t\treturn (0);");
194 for (file_i = num_files - 1; file_i > 0; file_i--) {
195 printc("fail_" file_i ":")
196 printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");");
200 printc("\t\treturn (ENXIO);");
202 printc("\tcase MOD_UNLOAD:");
204 for (file_i = 1; file_i < num_files; file_i++) {
205 printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");");
206 printc("\t\tif (error)");
207 printc("\t\t\treturn (error);");
210 printc("\t\terror = firmware_unregister(\"" shortnames[0] "\");");
212 printc("\t\treturn (error);\
217 static moduledata_t " modname "_fw_mod = {\
219 " modname "_fw_modevent,\
222 DECLARE_MODULE(" modname "_fw, " modname "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
223 MODULE_VERSION(" modname "_fw, 1);\
224 MODULE_DEPEND(" modname "_fw, firmware, 1, 1, 1);\
228 if ((rc = system("mv -f " ctmpfilename " " cfilename))) {
229 print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \