fw_stub.awk revision 241394
1236769Sobrien#!/usr/bin/awk -f 2236769Sobrien 3236769Sobrien#- 4236769Sobrien# Copyright (c) 2006 Max Laier. 5236769Sobrien# All rights reserved. 6236769Sobrien# 7236769Sobrien# Redistribution and use in source and binary forms, with or without 8236769Sobrien# modification, are permitted provided that the following conditions 9236769Sobrien# are met: 10236769Sobrien# 1. Redistributions of source code must retain the above copyright 11236769Sobrien# notice, this list of conditions and the following disclaimer. 12236769Sobrien# 2. Redistributions in binary form must reproduce the above copyright 13236769Sobrien# notice, this list of conditions and the following disclaimer in the 14236769Sobrien# documentation and/or other materials provided with the distribution. 15236769Sobrien# 16236769Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND 17236769Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18236769Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19236769Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20236769Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21236769Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22236769Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23236769Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24236769Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25236769Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26236769Sobrien# SUCH DAMAGE. 27236769Sobrien# 28236769Sobrien# $FreeBSD: head/sys/tools/fw_stub.awk 241394 2012-10-10 08:36:38Z kevlo $ 29236769Sobrien 30236769Sobrien# 31236769Sobrien# Script to generate module .c file from a list of firmware images 32236769Sobrien# 33236769Sobrien 34236769Sobrienfunction usage () 35236769Sobrien{ 36236769Sobrien print "usage: fw_stub <firmware:name>* [-l name] [-m modname] [-c outfile]"; 37236769Sobrien exit 1; 38236769Sobrien} 39236769Sobrien 40236769Sobrien# These are just for convenience ... 41236769Sobrienfunction printc(s) 42236769Sobrien{ 43236769Sobrien if (opt_c) 44236769Sobrien print s > ctmpfilename; 45236769Sobrien else 46236769Sobrien print s > "/dev/stdout"; 47236769Sobrien} 48236769Sobrien 49236769SobrienBEGIN { 50236769Sobrien 51236769Sobrien# 52236769Sobrien# Process the command line. 53236769Sobrien# 54236769Sobrien 55236769Sobriennum_files = 0; 56236769Sobrien 57236769Sobrienfor (i = 1; i < ARGC; i++) { 58236769Sobrien if (ARGV[i] ~ /^-/) { 59236769Sobrien # 60236769Sobrien # awk doesn't have getopt(), so we have to do it ourselves. 61236769Sobrien # This is a bit clumsy, but it works. 62236769Sobrien # 63236769Sobrien for (j = 2; j <= length(ARGV[i]); j++) { 64236769Sobrien o = substr(ARGV[i], j, 1); 65236769Sobrien if (o == "c") { 66236769Sobrien if (length(ARGV[i]) > j) { 67236769Sobrien opt_c = substr(ARGV[i], j + 1); 68236769Sobrien break; 69236769Sobrien } 70236769Sobrien else { 71236769Sobrien if (++i < ARGC) 72236769Sobrien opt_c = ARGV[i]; 73236769Sobrien else 74236769Sobrien usage(); 75236769Sobrien } 76236769Sobrien } else if (o == "m") { 77236769Sobrien if (length(ARGV[i]) > j) { 78236769Sobrien opt_m = substr(ARGV[i], j + 1); 79236769Sobrien 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"; 121modname = opt_m; 122gsub(/[-\.]/, "_", modname); 123 124printc("/*\ 125 * Automatically generated by:\ 126 * $FreeBSD: head/sys/tools/fw_stub.awk 241394 2012-10-10 08:36:38Z kevlo $\ 127 */"); 128printc("#include <sys/param.h>"); 129printc("#include <sys/errno.h>"); 130printc("#include <sys/kernel.h>"); 131printc("#include <sys/module.h>"); 132printc("#include <sys/linker.h>"); 133printc("#include <sys/firmware.h>"); 134printc("#include <sys/systm.h>\n"); 135 136if (opt_l) { 137 printc("static long " opt_l "_license_ack = 0;"); 138} 139 140for (file_i = 0; file_i < num_files; file_i++) { 141 symb = filenames[file_i]; 142 # '-', '.' and '/' are converted to '_' by ld/objcopy 143 gsub(/-|\.|\//, "_", symb); 144 printc("extern char _binary_" symb "_start[], _binary_" symb "_end[];"); 145} 146 147printc("\nstatic int\n"\ 148modname "_fw_modevent(module_t mod, int type, void *unused)\ 149{\ 150 const struct firmware *fp, *parent;\ 151 int error;\ 152 switch (type) {\ 153 case MOD_LOAD:\n"); 154 155if (opt_l) { 156 printc("\ 157 TUNABLE_LONG_FETCH(\"legal." opt_l ".license_ack\", &" opt_l "_license_ack);\ 158 if (!" opt_l "_license_ack) {\ 159 printf(\"" opt_m ": You need to read the LICENSE file in /usr/share/doc/legal/" opt_l "/.\\n\");\ 160 printf(\"" opt_m ": If you agree with the license, set legal." opt_l ".license_ack=1 in /boot/loader.conf.\\n\");\ 161 return(EPERM);\ 162 }\n"); 163} 164 165for (file_i = 0; file_i < num_files; file_i++) { 166 short = shortnames[file_i]; 167 symb = filenames[file_i]; 168 version = versions[file_i]; 169 # '-', '.' and '/' are converted to '_' by ld/objcopy 170 gsub(/-|\.|\//, "_", symb); 171 172 reg = "\t\tfp = "; 173 reg = reg "firmware_register(\"" short "\", _binary_" symb "_start , "; 174 reg = reg "(size_t)(_binary_" symb "_end - _binary_" symb "_start), "; 175 reg = reg version ", "; 176 177 if (file_i == 0) 178 reg = reg "NULL);"; 179 else 180 reg = reg "parent);"; 181 182 printc(reg); 183 184 printc("\t\tif (fp == NULL)"); 185 printc("\t\t\tgoto fail_" file_i ";"); 186 if (file_i == 0) 187 printc("\t\tparent = fp;"); 188} 189 190printc("\t\treturn (0);"); 191 192for (file_i = num_files - 1; file_i > 0; file_i--) { 193 printc("fail_" file_i ":") 194 printc("\t\t(void)firmware_unregister(\"" shortnames[file_i - 1] "\");"); 195} 196 197printc("\tfail_0:"); 198printc("\t\treturn (ENXIO);"); 199 200printc("\tcase MOD_UNLOAD:"); 201 202for (file_i = 1; file_i < num_files; file_i++) { 203 printc("\t\terror = firmware_unregister(\"" shortnames[file_i] "\");"); 204 printc("\t\tif (error)"); 205 printc("\t\t\treturn (error);"); 206} 207 208printc("\t\terror = firmware_unregister(\"" shortnames[0] "\");"); 209 210printc("\t\treturn (error);\ 211 }\ 212 return (EINVAL);\ 213}\ 214\ 215static moduledata_t " modname "_fw_mod = {\ 216 \"" modname "_fw\",\ 217 " modname "_fw_modevent,\ 218 0\ 219};\ 220DECLARE_MODULE(" modname "_fw, " modname "_fw_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);\ 221MODULE_VERSION(" modname "_fw, 1);\ 222MODULE_DEPEND(" modname "_fw, firmware, 1, 1, 1);\ 223"); 224 225if (opt_c) 226 if ((rc = system("mv -f " ctmpfilename " " cfilename))) { 227 print "'mv -f " ctmpfilename " " cfilename "' failed: " rc \ 228 > "/dev/stderr"; 229 exit 1; 230 } 231 232exit 0; 233 234} 235