The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/tools/fw_stub.awk

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 #!/usr/bin/awk -f
    2 
    3 #-
    4 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    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 # $FreeBSD$
   31 
   32 #
   33 # Script to generate module .c file from a list of firmware images
   34 #
   35 
   36 function usage ()
   37 {
   38         print "usage: fw_stub <firmware:name>* [-l name] [-m modname] [-c outfile]";
   39         exit 1;
   40 }
   41 
   42 #   These are just for convenience ...
   43 function printc(s)
   44 {
   45         if (opt_c)
   46                 print s > ctmpfilename;
   47         else
   48                 print s > "/dev/stdout";
   49 }
   50 
   51 BEGIN {
   52 
   53 #
   54 #   Process the command line.
   55 #
   56 
   57 num_files = 0;
   58 
   59 for (i = 1; i < ARGC; i++) {
   60         if (ARGV[i] ~ /^-/) {
   61                 #
   62                 #   awk doesn't have getopt(), so we have to do it ourselves.
   63                 #   This is a bit clumsy, but it works.
   64                 #
   65                 for (j = 2; j <= length(ARGV[i]); j++) {
   66                         o = substr(ARGV[i], j, 1);
   67                         if (o == "c") {
   68                                 if (length(ARGV[i]) > j) {
   69                                         opt_c = substr(ARGV[i], j + 1);
   70                                         break;
   71                                 }
   72                                 else {
   73                                         if (++i < ARGC)
   74                                                 opt_c = ARGV[i];
   75                                         else
   76                                                 usage();
   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                                         else
   87                                                 usage();
   88                                 }
   89                         } else if (o == "l") {
   90                                 if (length(ARGV[i]) > j) {
   91                                         opt_l = substr(ARGV[i], j + 1);
   92                                         break;
   93                                 }
   94                                 else {
   95                                         if (++i < ARGC)
   96                                                 opt_l = ARGV[i];
   97                                         else
   98                                                 usage();
   99                                 }
  100                         } else
  101                                 usage();
  102                 }
  103         } else {
  104                 split(ARGV[i], curr, ":");
  105                 filenames[num_files] = curr[1];
  106                 if (length(curr[2]) > 0)
  107                         shortnames[num_files] = curr[2];
  108                 else
  109                         shortnames[num_files] = curr[1];
  110                 if (length(curr[3]) > 0)
  111                         versions[num_files] = int(curr[3]);
  112                 else
  113                         versions[num_files] = 0;
  114                 num_files++;
  115         }
  116 }
  117 
  118 if (!num_files || !opt_m)
  119         usage();
  120 
  121 cfilename = opt_c;
  122 ctmpfilename = cfilename ".tmp";
  123 modname = opt_m;
  124 gsub(/[-\.]/, "_", modname);
  125 
  126 # Avoid a literal generated file tag here.
  127 generated = "@" "generated";
  128 printc("/*\
  129  * Automatically " generated " by:\
  130  * $FreeBSD$\
  131  */");
  132 printc("#include <sys/param.h>");
  133 printc("#include <sys/errno.h>");
  134 printc("#include <sys/kernel.h>");
  135 printc("#include <sys/module.h>");
  136 printc("#include <sys/linker.h>");
  137 printc("#include <sys/firmware.h>");
  138 printc("#include <sys/systm.h>\n");
  139 
  140 if (opt_l) {
  141         printc("static long " opt_l "_license_ack = 0;");
  142 }
  143 
  144 for (file_i = 0; file_i < num_files; file_i++) {
  145         symb = filenames[file_i];
  146         # '-', '.', '/', and '@' are converted to '_'
  147         gsub(/-|\.|\/|@/, "_", symb);
  148         printc("extern char _binary_" symb "_start[], _binary_" symb "_end[];");
  149 }
  150 
  151 printc("\nstatic int\n"\
  152 modname "_fw_modevent(module_t mod, int type, void *unused)\
  153 {\
  154         const struct firmware *fp;");
  155 if (num_files > 1)
  156         printc("\tconst struct firmware *parent;");
  157 printc("\tint error;\
  158         switch (type) {\
  159         case MOD_LOAD:\n");
  160 
  161 if (opt_l) {
  162                 printc("\
  163                 TUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\
  164                 if (!" opt_l "_license_ack) {\
  165                         printf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l ".LICENSE.\\n\");\
  166                         printf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\
  167                         return(EPERM);\
  168                 }\n");
  169 }
  170 
  171 for (file_i = 0; file_i < num_files; file_i++) {
  172         short = shortnames[file_i];
  173         symb = filenames[file_i];
  174         version = versions[file_i];
  175         # '-', '.', '/', and '@' are converted to '_'
  176         gsub(/-|\.|\/|@/, "_", symb);
  177 
  178         reg = "\t\tfp = ";
  179         reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , ";
  180         reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), ";
  181         reg = reg version ", ";
  182 
  183         if (file_i == 0)
  184                 reg = reg "NULL);";
  185         else
  186                 reg = reg "parent);";
  187 
  188         printc(reg);
  189 
  190         printc("\t\tif (fp == NULL)");
  191         printc("\t\t\tgoto fail_" file_i ";");
  192         if (file_i == 0 && num_files > 1)
  193                 printc("\t\tparent = fp;");
  194 }
  195 
  196 printc("\t\treturn (0);");
  197 
  198 for (file_i = num_files - 1; file_i > 0; file_i--) {
  199         printc("fail_" file_i ":")
  200         printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");");
  201 }
  202 
  203 printc("\tfail_0:");
  204 printc("\t\treturn (ENXIO);");
  205 
  206 printc("\tcase MOD_UNLOAD:");
  207 
  208 for (file_i = 1; file_i < num_files; file_i++) {
  209         printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");");
  210         printc("\t\tif (error)");
  211         printc("\t\t\treturn (error);");
  212 }
  213 
  214 printc("\t\terror = firmware_unregister(\"" shortnames[0] "\");");
  215 
  216 printc("\t\treturn (error);\
  217         }\
  218         return (EINVAL);\
  219 }\
  220 \
  221 static moduledata_t " modname "_fw_mod = {\
  222         \"" modname "_fw\",\
  223         " modname "_fw_modevent,\
  224         0\
  225 };\
  226 DECLARE_MODULE(" modname "_fw, " modname "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\
  227 MODULE_VERSION(" modname "_fw, 1);\
  228 MODULE_DEPEND(" modname "_fw, firmware, 1, 1, 1);\
  229 ");
  230 
  231 if (opt_c)
  232         if ((rc = system("mv -f " ctmpfilename " " cfilename))) {
  233                 print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \
  234                     > "/dev/stderr";
  235                 exit 1;
  236         }
  237 
  238 exit 0;
  239 
  240 }

Cache object: 52440b79828b96aba053b3f5da2c0be1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.