1 /* $NetBSD: mkioconf.c,v 1.35 2017/11/19 01:46:29 christos 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.35 2017/11/19 01:46:29 christos 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 int inst = -1; 342 343 NEWLINE; 344 TAILQ_FOREACH(p, &allpspecs, p_list) { 345 if (p->p_devs == NULL || p->p_active != DEVI_ACTIVE) 346 continue; 347 if (inst >= p->p_inst) 348 continue; 349 inst = p->p_inst; 350 fprintf(fp, 351 "static const struct cfparent pspec%d = {\n", p->p_inst); 352 fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name); 353 if (p->p_atdev != NULL) { 354 fprintf(fp, "\"%s\", ", p->p_atdev->d_name); 355 if (p->p_atunit == WILD) 356 fprintf(fp, "DVUNIT_ANY"); 357 else 358 fprintf(fp, "%d", p->p_atunit); 359 } else 360 fprintf(fp, "NULL, 0"); 361 fprintf(fp, "\n};\n"); 362 } 363 } 364 365 /* 366 * Emit the cfdata array. 367 */ 368 static void 369 emitcfdata(FILE *fp) 370 { 371 struct devi **p, *i; 372 struct pspec *ps; 373 int unit, v; 374 const char *state, *basename, *attachment; 375 struct loclist *ll; 376 struct attr *a; 377 const char *loc; 378 char locbuf[20]; 379 const char *lastname = ""; 380 381 fprintf(fp, "\n" 382 "#define NORM FSTATE_NOTFOUND\n" 383 "#define STAR FSTATE_STAR\n" 384 "\n" 385 "%sstruct cfdata cfdata%s%s[] = {\n" 386 " /* driver attachment unit state " 387 " loc flags pspec */\n", 388 ioconfname ? "static " : "", 389 ioconfname ? "_ioconf_" : "", 390 ioconfname ? ioconfname : ""); 391 for (p = packed; (i = *p) != NULL; p++) { 392 /* the description */ 393 fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name); 394 if ((ps = i->i_pspec) != NULL) { 395 if (ps->p_atdev != NULL && 396 ps->p_atunit != WILD) { 397 fprintf(fp, "%s%d", ps->p_atdev->d_name, 398 ps->p_atunit); 399 } else if (ps->p_atdev != NULL) { 400 fprintf(fp, "%s?", ps->p_atdev->d_name); 401 } else { 402 fprintf(fp, "%s?", ps->p_iattr->a_name); 403 } 404 405 a = ps->p_iattr; 406 for (ll = a->a_locs, v = 0; ll != NULL; 407 ll = ll->ll_next, v++) { 408 if (ARRNAME(ll->ll_name, lastname)) { 409 fprintf(fp, " %s %s", 410 ll->ll_name, i->i_locs[v]); 411 } else { 412 fprintf(fp, " %s %s", 413 ll->ll_name, 414 i->i_locs[v]); 415 lastname = ll->ll_name; 416 } 417 } 418 } else { 419 a = NULL; 420 fputs("root", fp); 421 } 422 423 fputs(" */\n", fp); 424 425 /* then the actual defining line */ 426 basename = i->i_base->d_name; 427 attachment = i->i_atdeva->d_name; 428 if (i->i_unit == STAR) { 429 unit = i->i_base->d_umax; 430 state = "STAR"; 431 } else { 432 unit = i->i_unit; 433 state = "NORM"; 434 } 435 if (i->i_locoff >= 0) { 436 (void)snprintf(locbuf, sizeof(locbuf), "loc+%3d", 437 i->i_locoff); 438 loc = locbuf; 439 } else 440 loc = "NULL"; 441 fprintf(fp, " { \"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ", 442 basename, strlen(basename) < 7 ? "\t\t" 443 : "\t", 444 attachment, strlen(attachment) < 5 ? "\t\t" 445 : "\t", 446 unit, state, loc, i->i_cfflags); 447 if (ps != NULL) 448 fprintf(fp, "&pspec%d },\n", ps->p_inst); 449 else 450 fputs("NULL },\n", fp); 451 } 452 fprintf(fp, " { %s,%s%s,%s%2d, %s, %7s, %#6x, %s }\n};\n", 453 "NULL", "\t\t", "NULL", "\t\t", 0, " 0", "NULL", 0, "NULL"); 454 } 455 456 /* 457 * Emit the table of potential roots. 458 */ 459 static void 460 emitroots(FILE *fp) 461 { 462 struct devi **p, *i; 463 464 fputs("\nconst short cfroots[] = {\n", fp); 465 for (p = packed; (i = *p) != NULL; p++) { 466 if (i->i_at != NULL) 467 continue; 468 if (i->i_unit != 0 && 469 (i->i_unit != STAR || i->i_base->d_umax != 0)) 470 warnx("warning: `%s at root' is not unit 0", i->i_name); 471 fprintf(fp, "\t%2d /* %s */,\n", 472 i->i_cfindex, i->i_name); 473 } 474 fputs("\t-1\n};\n", fp); 475 } 476 477 /* 478 * Emit pseudo-device initialization. 479 */ 480 static void 481 emitpseudo(FILE *fp) 482 { 483 struct devi *i; 484 struct devbase *d; 485 486 fputs("\n/* pseudo-devices */\n", fp); 487 fputs("\nconst struct pdevinit pdevinit[] = {\n", fp); 488 TAILQ_FOREACH(i, &allpseudo, i_next) { 489 d = i->i_base; 490 fprintf(fp, "\t{ %sattach, %d },\n", 491 d->d_name, d->d_umax); 492 } 493 fputs("\t{ 0, 0 }\n};\n", fp); 494 } 495 496 /* 497 * Emit name to major block number table. 498 */ 499 void 500 emitname2blk(FILE *fp) 501 { 502 struct devbase *dev; 503 504 fputs("\n/* device name to major block number */\n", fp); 505 506 fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n"); 507 508 TAILQ_FOREACH(dev, &allbases, d_next) { 509 if (dev->d_major == NODEVMAJOR) 510 continue; 511 512 fprintf(fp, "\t{ \"%s\", %d },\n", 513 dev->d_name, dev->d_major); 514 } 515 fprintf(fp, "\t{ NULL, 0 }\n};\n"); 516 } 517