1 /* $NetBSD: mkioconf.c,v 1.33 2015/11/12 14:38:21 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratories. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)mkioconf.c 8.1 (Berkeley) 6/6/93 41 */ 42 43 #if HAVE_NBTOOL_CONFIG_H 44 #include "nbtool_config.h" 45 #endif 46 47 #include <sys/cdefs.h> 48 __RCSID("$NetBSD: mkioconf.c,v 1.33 2015/11/12 14:38:21 pooka Exp $"); 49 50 #include <sys/param.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include "defs.h" 57 58 /* 59 * Make ioconf.c. 60 */ 61 static int cf_locators_print(const char *, void *, void *); 62 static int cforder(const void *, const void *); 63 static void emitcfdata(FILE *); 64 static void emitcfdrivers(FILE *); 65 static void emitexterns(FILE *); 66 static void emitcfattachinit(FILE *); 67 static void emithdr(FILE *); 68 static void emitloc(FILE *); 69 static void emitpseudo(FILE *); 70 static void emitparents(FILE *); 71 static void emitroots(FILE *); 72 static void emitname2blk(FILE *); 73 74 #define SEP(pos, max) (((u_int)(pos) % (max)) == 0 ? "\n\t" : " ") 75 76 #define ARRNAME(n, l) (strchr((n), ARRCHR) && strncmp((n), (l), strlen((l))) == 0) 77 78 /* 79 * NEWLINE can only be used in the emitXXX functions. 80 * In most cases it can be subsumed into an fprintf. 81 */ 82 #define NEWLINE putc('\n', fp) 83 84 int 85 mkioconf(void) 86 { 87 FILE *fp; 88 89 qsort(packed, npacked, sizeof *packed, cforder); 90 if ((fp = fopen("ioconf.c.tmp", "w")) == NULL) { 91 warn("cannot write ioconf.c"); 92 return (1); 93 } 94 95 fprintf(fp, "#include \"ioconf.h\"\n"); 96 if (ioconfname) 97 fprintf(fp, "#define IOCONF %s\n", ioconfname); 98 99 emithdr(fp); 100 emitcfdrivers(fp); 101 emitexterns(fp); 102 emitloc(fp); 103 emitparents(fp); 104 emitcfdata(fp); 105 emitcfattachinit(fp); 106 107 if (ioconfname == NULL) { 108 emitroots(fp); 109 emitpseudo(fp); 110 if (!do_devsw) 111 emitname2blk(fp); 112 } 113 114 fflush(fp); 115 if (ferror(fp)) { 116 warn("error writing ioconf.c"); 117 (void)fclose(fp); 118 #if 0 119 (void)unlink("ioconf.c.tmp"); 120 #endif 121 return (1); 122 } 123 124 (void)fclose(fp); 125 if (moveifchanged("ioconf.c.tmp", "ioconf.c") != 0) { 126 warn("error renaming ioconf.c"); 127 return (1); 128 } 129 return (0); 130 } 131 132 static int 133 cforder(const void *a, const void *b) 134 { 135 int n1, n2; 136 137 n1 = (*(const struct devi * const *)a)->i_cfindex; 138 n2 = (*(const struct devi * const *)b)->i_cfindex; 139 return (n1 - n2); 140 } 141 142 static void 143 emithdr(FILE *ofp) 144 { 145 FILE *ifp; 146 size_t n; 147 char ifnbuf[200], buf[BUFSIZ]; 148 char *ifn; 149 150 autogen_comment(ofp, "ioconf.c"); 151 152 (void)snprintf(ifnbuf, sizeof(ifnbuf), "%s/arch/%s/conf/ioconf.incl.%s", 153 srcdir, 154 machine ? machine : "(null)", machine ? machine : "(null)"); 155 ifn = ifnbuf; 156 if ((ifp = fopen(ifn, "r")) != NULL) { 157 while ((n = fread(buf, 1, sizeof(buf), ifp)) > 0) 158 (void)fwrite(buf, 1, n, ofp); 159 if (ferror(ifp)) 160 err(EXIT_FAILURE, "error reading %s", ifn); 161 (void)fclose(ifp); 162 } else { 163 fputs("#include <sys/param.h>\n" 164 "#include <sys/conf.h>\n" 165 "#include <sys/device.h>\n" 166 "#include <sys/mount.h>\n", ofp); 167 } 168 } 169 170 /* 171 * Emit an initialized array of character strings describing this 172 * attribute's locators. 173 */ 174 static int 175 cf_locators_print(const char *name, void *value, void *arg) 176 { 177 struct attr *a; 178 struct loclist *ll; 179 FILE *fp = arg; 180 181 a = value; 182 if (!a->a_iattr) 183 return (0); 184 if (ht_lookup(selecttab, name) == NULL) 185 return (0); 186 187 if (a->a_locs) { 188 fprintf(fp, 189 "static const struct cfiattrdata %scf_iattrdata = {\n", 190 name); 191 fprintf(fp, "\t\"%s\", %d, {\n", name, a->a_loclen); 192 for (ll = a->a_locs; ll; ll = ll->ll_next) 193 fprintf(fp, "\t\t{ \"%s\", \"%s\", %s },\n", 194 ll->ll_name, 195 (ll->ll_string ? ll->ll_string : "NULL"), 196 (ll->ll_string ? ll->ll_string : "0")); 197 fprintf(fp, "\t}\n};\n"); 198 } else { 199 fprintf(fp, 200 "static const struct cfiattrdata %scf_iattrdata = {\n" 201 "\t\"%s\", 0, {\n\t\t{ NULL, NULL, 0 },\n\t}\n};\n", 202 name, name); 203 } 204 205 return 0; 206 } 207 208 static void 209 emitcfdrivers(FILE *fp) 210 { 211 struct devbase *d; 212 struct attrlist *al; 213 struct attr *a; 214 int has_iattrs; 215 216 NEWLINE; 217 ht_enumerate(attrtab, cf_locators_print, fp); 218 219 NEWLINE; 220 TAILQ_FOREACH(d, &allbases, d_next) { 221 if (!devbase_has_instances(d, WILD)) 222 continue; 223 has_iattrs = 0; 224 for (al = d->d_attrs; al != NULL; al = al->al_next) { 225 a = al->al_this; 226 if (a->a_iattr == 0) 227 continue; 228 if (has_iattrs == 0) 229 fprintf(fp, 230 "static const struct cfiattrdata * const %s_attrs[] = { ", 231 d->d_name); 232 has_iattrs = 1; 233 fprintf(fp, "&%scf_iattrdata, ", a->a_name); 234 } 235 if (has_iattrs) 236 fprintf(fp, "NULL };\n"); 237 fprintf(fp, "CFDRIVER_DECL(%s, %s, ", d->d_name, /* ) */ 238 d->d_classattr != NULL ? d->d_classattr->a_devclass 239 : "DV_DULL"); 240 if (has_iattrs) 241 fprintf(fp, "%s_attrs", d->d_name); 242 else 243 fprintf(fp, "NULL"); 244 fprintf(fp, /* ( */ ");\n\n"); 245 } 246 247 NEWLINE; 248 249 fprintf(fp, 250 "%sstruct cfdriver * const cfdriver_%s_%s[] = {\n", 251 ioconfname ? "static " : "", 252 ioconfname ? "ioconf" : "list", 253 ioconfname ? ioconfname : "initial"); 254 255 TAILQ_FOREACH(d, &allbases, d_next) { 256 if (!devbase_has_instances(d, WILD)) 257 continue; 258 fprintf(fp, "\t&%s_cd,\n", d->d_name); 259 } 260 fprintf(fp, "\tNULL\n};\n"); 261 } 262 263 static void 264 emitexterns(FILE *fp) 265 { 266 struct deva *da; 267 268 NEWLINE; 269 TAILQ_FOREACH(da, &alldevas, d_next) { 270 if (!deva_has_instances(da, WILD)) 271 continue; 272 fprintf(fp, "extern struct cfattach %s_ca;\n", 273 da->d_name); 274 } 275 } 276 277 static void 278 emitcfattachinit(FILE *fp) 279 { 280 struct devbase *d; 281 struct deva *da; 282 283 NEWLINE; 284 TAILQ_FOREACH(d, &allbases, d_next) { 285 if (!devbase_has_instances(d, WILD)) 286 continue; 287 if (d->d_ahead == NULL) 288 continue; 289 290 fprintf(fp, 291 "static struct cfattach * const %s_cfattachinit[] = {\n\t", 292 d->d_name); 293 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 294 if (!deva_has_instances(da, WILD)) 295 continue; 296 fprintf(fp, "&%s_ca, ", da->d_name); 297 } 298 fprintf(fp, "NULL\n};\n"); 299 } 300 301 NEWLINE; 302 fprintf(fp, "%sconst struct cfattachinit cfattach%s%s[] = {\n", 303 ioconfname ? "static " : "", 304 ioconfname ? "_ioconf_" : "init", 305 ioconfname ? ioconfname : ""); 306 307 TAILQ_FOREACH(d, &allbases, d_next) { 308 if (!devbase_has_instances(d, WILD)) 309 continue; 310 if (d->d_ahead == NULL) 311 continue; 312 313 fprintf(fp, "\t{ \"%s\", %s_cfattachinit },\n", 314 d->d_name, d->d_name); 315 } 316 317 fprintf(fp, "\t{ NULL, NULL }\n};\n"); 318 } 319 320 static void 321 emitloc(FILE *fp) 322 { 323 int i; 324 325 if (locators.used != 0) { 326 fprintf(fp, "\n/* locators */\n" 327 "static int loc[%d] = {", locators.used); 328 for (i = 0; i < locators.used; i++) 329 fprintf(fp, "%s%s,", SEP(i, 8), locators.vec[i]); 330 fprintf(fp, "\n};\n"); 331 } 332 } 333 334 /* 335 * Emit static parent data. 336 */ 337 static void 338 emitparents(FILE *fp) 339 { 340 struct pspec *p; 341 342 NEWLINE; 343 TAILQ_FOREACH(p, &allpspecs, p_list) { 344 if (p->p_devs == NULL || p->p_active != DEVI_ACTIVE) 345 continue; 346 fprintf(fp, 347 "static const struct cfparent pspec%d = {\n", p->p_inst); 348 fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name); 349 if (p->p_atdev != NULL) { 350 fprintf(fp, "\"%s\", ", p->p_atdev->d_name); 351 if (p->p_atunit == WILD) 352 fprintf(fp, "DVUNIT_ANY"); 353 else 354 fprintf(fp, "%d", p->p_atunit); 355 } else 356 fprintf(fp, "NULL, 0"); 357 fprintf(fp, "\n};\n"); 358 } 359 } 360 361 /* 362 * Emit the cfdata array. 363 */ 364 static void 365 emitcfdata(FILE *fp) 366 { 367 struct devi **p, *i; 368 struct pspec *ps; 369 int unit, v; 370 const char *state, *basename, *attachment; 371 struct loclist *ll; 372 struct attr *a; 373 const char *loc; 374 char locbuf[20]; 375 const char *lastname = ""; 376 377 fprintf(fp, "\n" 378 "#define NORM FSTATE_NOTFOUND\n" 379 "#define STAR FSTATE_STAR\n" 380 "\n" 381 "%sstruct cfdata cfdata%s%s[] = {\n" 382 " /* driver attachment unit state " 383 " loc flags pspec */\n", 384 ioconfname ? "static " : "", 385 ioconfname ? "_ioconf_" : "", 386 ioconfname ? ioconfname : ""); 387 for (p = packed; (i = *p) != NULL; p++) { 388 /* the description */ 389 fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name); 390 if ((ps = i->i_pspec) != NULL) { 391 if (ps->p_atdev != NULL && 392 ps->p_atunit != WILD) { 393 fprintf(fp, "%s%d", ps->p_atdev->d_name, 394 ps->p_atunit); 395 } else if (ps->p_atdev != NULL) { 396 fprintf(fp, "%s?", ps->p_atdev->d_name); 397 } else { 398 fprintf(fp, "%s?", ps->p_iattr->a_name); 399 } 400 401 a = ps->p_iattr; 402 for (ll = a->a_locs, v = 0; ll != NULL; 403 ll = ll->ll_next, v++) { 404 if (ARRNAME(ll->ll_name, lastname)) { 405 fprintf(fp, " %s %s", 406 ll->ll_name, i->i_locs[v]); 407 } else { 408 fprintf(fp, " %s %s", 409 ll->ll_name, 410 i->i_locs[v]); 411 lastname = ll->ll_name; 412 } 413 } 414 } else { 415 a = NULL; 416 fputs("root", fp); 417 } 418 419 fputs(" */\n", fp); 420 421 /* then the actual defining line */ 422 basename = i->i_base->d_name; 423 attachment = i->i_atdeva->d_name; 424 if (i->i_unit == STAR) { 425 unit = i->i_base->d_umax; 426 state = "STAR"; 427 } else { 428 unit = i->i_unit; 429 state = "NORM"; 430 } 431 if (i->i_locoff >= 0) { 432 (void)snprintf(locbuf, sizeof(locbuf), "loc+%3d", 433 i->i_locoff); 434 loc = locbuf; 435 } else 436 loc = "NULL"; 437 fprintf(fp, " { \"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ", 438 basename, strlen(basename) < 7 ? "\t\t" 439 : "\t", 440 attachment, strlen(attachment) < 5 ? "\t\t" 441 : "\t", 442 unit, state, loc, i->i_cfflags); 443 if (ps != NULL) 444 fprintf(fp, "&pspec%d },\n", ps->p_inst); 445 else 446 fputs("NULL },\n", fp); 447 } 448 fprintf(fp, " { %s,%s%s,%s%2d, %s, %7s, %#6x, %s }\n};\n", 449 "NULL", "\t\t", "NULL", "\t\t", 0, " 0", "NULL", 0, "NULL"); 450 } 451 452 /* 453 * Emit the table of potential roots. 454 */ 455 static void 456 emitroots(FILE *fp) 457 { 458 struct devi **p, *i; 459 460 fputs("\nconst short cfroots[] = {\n", fp); 461 for (p = packed; (i = *p) != NULL; p++) { 462 if (i->i_at != NULL) 463 continue; 464 if (i->i_unit != 0 && 465 (i->i_unit != STAR || i->i_base->d_umax != 0)) 466 warnx("warning: `%s at root' is not unit 0", i->i_name); 467 fprintf(fp, "\t%2d /* %s */,\n", 468 i->i_cfindex, i->i_name); 469 } 470 fputs("\t-1\n};\n", fp); 471 } 472 473 /* 474 * Emit pseudo-device initialization. 475 */ 476 static void 477 emitpseudo(FILE *fp) 478 { 479 struct devi *i; 480 struct devbase *d; 481 482 fputs("\n/* pseudo-devices */\n", fp); 483 fputs("\nconst struct pdevinit pdevinit[] = {\n", fp); 484 TAILQ_FOREACH(i, &allpseudo, i_next) { 485 d = i->i_base; 486 fprintf(fp, "\t{ %sattach, %d },\n", 487 d->d_name, d->d_umax); 488 } 489 fputs("\t{ 0, 0 }\n};\n", fp); 490 } 491 492 /* 493 * Emit name to major block number table. 494 */ 495 void 496 emitname2blk(FILE *fp) 497 { 498 struct devbase *dev; 499 500 fputs("\n/* device name to major block number */\n", fp); 501 502 fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n"); 503 504 TAILQ_FOREACH(dev, &allbases, d_next) { 505 if (dev->d_major == NODEVMAJOR) 506 continue; 507 508 fprintf(fp, "\t{ \"%s\", %d },\n", 509 dev->d_name, dev->d_major); 510 } 511 fprintf(fp, "\t{ NULL, 0 }\n};\n"); 512 } 513