1 /* $NetBSD: mkioconf.c,v 1.37 2026/01/17 02:01:39 thorpej 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.37 2026/01/17 02:01:39 thorpej 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 if (n1 < n2) 140 return -1; 141 else if (n1 > n2) 142 return +1; 143 else 144 abort(); /* no ties possible */ 145 } 146 147 static void 148 emithdr(FILE *ofp) 149 { 150 FILE *ifp; 151 size_t n; 152 char ifnbuf[200], buf[BUFSIZ]; 153 char *ifn; 154 155 autogen_comment(ofp, "ioconf.c"); 156 157 (void)snprintf(ifnbuf, sizeof(ifnbuf), "%s/arch/%s/conf/ioconf.incl.%s", 158 srcdir, 159 machine ? machine : "(null)", machine ? machine : "(null)"); 160 ifn = ifnbuf; 161 if ((ifp = fopen(ifn, "r")) != NULL) { 162 while ((n = fread(buf, 1, sizeof(buf), ifp)) > 0) 163 (void)fwrite(buf, 1, n, ofp); 164 if (ferror(ifp)) 165 err(EXIT_FAILURE, "error reading %s", ifn); 166 (void)fclose(ifp); 167 } else { 168 fputs("#include <sys/param.h>\n" 169 "#include <sys/conf.h>\n" 170 "#include <sys/device.h>\n" 171 "#include <sys/mount.h>\n", ofp); 172 } 173 } 174 175 /* 176 * Emit an initialized array of character strings describing this 177 * attribute's locators. 178 */ 179 static int 180 cf_locators_print(const char *name, void *value, void *arg) 181 { 182 struct attr *a; 183 struct loclist *ll; 184 FILE *fp = arg; 185 186 a = value; 187 if (!a->a_iattr) 188 return (0); 189 if (ht_lookup(selecttab, name) == NULL) 190 return (0); 191 192 if (a->a_locs) { 193 fprintf(fp, 194 "static const struct cfiattrdata %scf_iattrdata = {\n", 195 name); 196 fprintf(fp, "\t\"%s\", %d, {\n", name, a->a_loclen); 197 for (ll = a->a_locs; ll; ll = ll->ll_next) 198 fprintf(fp, "\t\t{ \"%s\", \"%s\", %s },\n", 199 ll->ll_name, 200 (ll->ll_string ? ll->ll_string : "NULL"), 201 (ll->ll_string ? ll->ll_string : "0")); 202 fprintf(fp, "\t}\n};\n"); 203 } else { 204 fprintf(fp, 205 "static const struct cfiattrdata %scf_iattrdata = {\n" 206 "\t\"%s\", 0, {\n\t\t{ NULL, NULL, 0 },\n\t}\n};\n", 207 name, name); 208 } 209 210 return 0; 211 } 212 213 static void 214 emitcfdrivers(FILE *fp) 215 { 216 struct devbase *d; 217 struct attrlist *al; 218 struct attr *a; 219 int has_iattrs; 220 221 NEWLINE; 222 ht_enumerate(attrtab, cf_locators_print, fp); 223 224 NEWLINE; 225 TAILQ_FOREACH(d, &allbases, d_next) { 226 if (!devbase_has_instances(d, WILD)) 227 continue; 228 has_iattrs = 0; 229 for (al = d->d_attrs; al != NULL; al = al->al_next) { 230 a = al->al_this; 231 if (a->a_iattr == 0) 232 continue; 233 if (has_iattrs == 0) 234 fprintf(fp, 235 "static const struct cfiattrdata * const %s_attrs[] = { ", 236 d->d_name); 237 has_iattrs = 1; 238 fprintf(fp, "&%scf_iattrdata, ", a->a_name); 239 } 240 if (has_iattrs) 241 fprintf(fp, "NULL };\n"); 242 fprintf(fp, "CFDRIVER_DECL(%s, %s, ", d->d_name, /* ) */ 243 d->d_classattr != NULL ? d->d_classattr->a_devclass 244 : "DV_DULL"); 245 if (has_iattrs) 246 fprintf(fp, "%s_attrs", d->d_name); 247 else 248 fprintf(fp, "NULL"); 249 fprintf(fp, /* ( */ ");\n\n"); 250 } 251 252 NEWLINE; 253 254 fprintf(fp, 255 "%sstruct cfdriver * const cfdriver_%s_%s[] = {\n", 256 ioconfname ? "static " : "", 257 ioconfname ? "ioconf" : "list", 258 ioconfname ? ioconfname : "initial"); 259 260 TAILQ_FOREACH(d, &allbases, d_next) { 261 if (!devbase_has_instances(d, WILD)) 262 continue; 263 fprintf(fp, "\t&%s_cd,\n", d->d_name); 264 } 265 fprintf(fp, "\tNULL\n};\n"); 266 } 267 268 static void 269 emitexterns(FILE *fp) 270 { 271 struct deva *da; 272 273 NEWLINE; 274 TAILQ_FOREACH(da, &alldevas, d_next) { 275 if (!deva_has_instances(da, WILD)) 276 continue; 277 fprintf(fp, "extern struct cfattach %s_ca;\n", 278 da->d_name); 279 } 280 } 281 282 static void 283 emitcfattachinit(FILE *fp) 284 { 285 struct attrlist *al; 286 struct devbase *d; 287 struct deva *da; 288 int has_iattrs; 289 290 NEWLINE; 291 TAILQ_FOREACH(d, &allbases, d_next) { 292 if (!devbase_has_instances(d, WILD)) 293 continue; 294 if (d->d_ahead == NULL) 295 continue; 296 297 fprintf(fp, 298 "static struct cfattach * const %s_cfattachinit[] = {\n\t", 299 d->d_name); 300 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 301 if (!deva_has_instances(da, WILD)) 302 continue; 303 fprintf(fp, "&%s_ca, ", da->d_name); 304 } 305 fprintf(fp, "NULL\n};\n"); 306 } 307 308 has_iattrs = 0; 309 TAILQ_FOREACH(d, &allbases, d_next) { 310 if (!devbase_has_instances(d, WILD)) 311 continue; 312 if (d->d_ahead == NULL) 313 continue; 314 315 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 316 if (!deva_has_instances(da, WILD)) 317 continue; 318 if (da->d_has_iattrs == 0) 319 continue; 320 321 for (al = da->d_attrs; al != NULL; al = al->al_next) { 322 if (al->al_this->a_iattr) { 323 if (has_iattrs == 0) { 324 NEWLINE; 325 has_iattrs = 1; 326 } 327 fprintf(fp, 328 "static struct cfattachiattr %s_cfattachiattr = {\n\t" 329 "{ NULL }, &%s_ca, &%scf_iattrdata\n};\n", 330 da->d_name, da->d_name, 331 al->al_this->a_name); 332 } 333 } 334 } 335 } 336 337 if (has_iattrs) { 338 NEWLINE; 339 TAILQ_FOREACH(d, &allbases, d_next) { 340 if (!devbase_has_instances(d, WILD)) 341 continue; 342 if (d->d_ahead == NULL) 343 continue; 344 345 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 346 if (!deva_has_instances(da, WILD)) 347 continue; 348 if (da->d_has_iattrs == 0) 349 continue; 350 fprintf(fp, 351 "static struct cfattachiattr * const %s_cfattachiattrs[] = {\n\t", 352 d->d_name); 353 for (al = da->d_attrs; al != NULL; 354 al = al->al_next) { 355 if (al->al_this->a_iattr) { 356 fprintf(fp, 357 "&%s_cfattachiattr, ", 358 da->d_name); 359 } 360 } 361 fprintf(fp, "NULL\n};\n"); 362 } 363 } 364 } 365 366 NEWLINE; 367 fprintf(fp, "%sconst struct cfattachinit cfattach%s%s[] = {\n", 368 ioconfname ? "static " : "", 369 ioconfname ? "_ioconf_" : "init", 370 ioconfname ? ioconfname : ""); 371 372 TAILQ_FOREACH(d, &allbases, d_next) { 373 if (!devbase_has_instances(d, WILD)) 374 continue; 375 if (d->d_ahead == NULL) 376 continue; 377 378 fprintf(fp, "\t{ \"%s\", %s_cfattachinit, ", 379 d->d_name, d->d_name); 380 381 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 382 if (!deva_has_instances(da, WILD)) 383 continue; 384 if (da->d_has_iattrs) 385 break; 386 } 387 if (da != NULL) 388 fprintf(fp, "%s_cfattachiattrs", d->d_name); 389 else 390 fprintf(fp, "NULL"); 391 fprintf(fp, " },\n"); 392 } 393 394 fprintf(fp, "\t{ NULL, NULL, NULL }\n};\n"); 395 } 396 397 static void 398 emitloc(FILE *fp) 399 { 400 int i; 401 402 if (locators.used != 0) { 403 fprintf(fp, "\n/* locators */\n" 404 "static int loc[%d] = {", locators.used); 405 for (i = 0; i < locators.used; i++) 406 fprintf(fp, "%s%s,", SEP(i, 8), locators.vec[i]); 407 fprintf(fp, "\n};\n"); 408 } 409 } 410 411 /* 412 * Emit static parent data. 413 */ 414 static void 415 emitparents(FILE *fp) 416 { 417 struct pspec *p; 418 int inst = -1; 419 420 NEWLINE; 421 TAILQ_FOREACH(p, &allpspecs, p_list) { 422 if (p->p_devs == NULL || p->p_active != DEVI_ACTIVE) 423 continue; 424 if (inst >= p->p_inst) 425 continue; 426 inst = p->p_inst; 427 fprintf(fp, 428 "static const struct cfparent pspec%d = {\n", p->p_inst); 429 fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name); 430 if (p->p_atdev != NULL) { 431 fprintf(fp, "\"%s\", ", p->p_atdev->d_name); 432 if (p->p_atunit == WILD) 433 fprintf(fp, "DVUNIT_ANY"); 434 else 435 fprintf(fp, "%d", p->p_atunit); 436 } else 437 fprintf(fp, "NULL, 0"); 438 fprintf(fp, "\n};\n"); 439 } 440 } 441 442 /* 443 * Emit the cfdata array. 444 */ 445 static void 446 emitcfdata(FILE *fp) 447 { 448 struct devi **p, *i; 449 struct pspec *ps; 450 int unit, v; 451 const char *state, *basename, *attachment; 452 struct loclist *ll; 453 struct attr *a; 454 const char *loc; 455 char locbuf[20]; 456 const char *lastname = ""; 457 458 fprintf(fp, "\n" 459 "#define NORM FSTATE_NOTFOUND\n" 460 "#define STAR FSTATE_STAR\n" 461 "\n" 462 "%sstruct cfdata cfdata%s%s[] = {\n" 463 " /* driver attachment unit state " 464 " loc flags pspec */\n", 465 ioconfname ? "static " : "", 466 ioconfname ? "_ioconf_" : "", 467 ioconfname ? ioconfname : ""); 468 for (p = packed; (i = *p) != NULL; p++) { 469 /* the description */ 470 fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name); 471 if ((ps = i->i_pspec) != NULL) { 472 if (ps->p_atdev != NULL && 473 ps->p_atunit != WILD) { 474 fprintf(fp, "%s%d", ps->p_atdev->d_name, 475 ps->p_atunit); 476 } else if (ps->p_atdev != NULL) { 477 fprintf(fp, "%s?", ps->p_atdev->d_name); 478 } else { 479 fprintf(fp, "%s?", ps->p_iattr->a_name); 480 } 481 482 a = ps->p_iattr; 483 for (ll = a->a_locs, v = 0; ll != NULL; 484 ll = ll->ll_next, v++) { 485 if (ARRNAME(ll->ll_name, lastname)) { 486 fprintf(fp, " %s %s", 487 ll->ll_name, i->i_locs[v]); 488 } else { 489 fprintf(fp, " %s %s", 490 ll->ll_name, 491 i->i_locs[v]); 492 lastname = ll->ll_name; 493 } 494 } 495 } else { 496 a = NULL; 497 fputs("root", fp); 498 } 499 500 fputs(" */\n", fp); 501 502 /* then the actual defining line */ 503 basename = i->i_base->d_name; 504 attachment = i->i_atdeva->d_name; 505 if (i->i_unit == STAR) { 506 unit = i->i_base->d_umax; 507 state = "STAR"; 508 } else { 509 unit = i->i_unit; 510 state = "NORM"; 511 } 512 if (i->i_locoff >= 0) { 513 (void)snprintf(locbuf, sizeof(locbuf), "loc+%3d", 514 i->i_locoff); 515 loc = locbuf; 516 } else 517 loc = "NULL"; 518 fprintf(fp, " { \"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ", 519 basename, strlen(basename) < 7 ? "\t\t" 520 : "\t", 521 attachment, strlen(attachment) < 5 ? "\t\t" 522 : "\t", 523 unit, state, loc, i->i_cfflags); 524 if (ps != NULL) 525 fprintf(fp, "&pspec%d },\n", ps->p_inst); 526 else 527 fputs("NULL },\n", fp); 528 } 529 fprintf(fp, " { %s,%s%s,%s%2d, %s, %7s, %#6x, %s }\n};\n", 530 "NULL", "\t\t", "NULL", "\t\t", 0, " 0", "NULL", 0, "NULL"); 531 } 532 533 /* 534 * Emit the table of potential roots. 535 */ 536 static void 537 emitroots(FILE *fp) 538 { 539 struct devi **p, *i; 540 541 fputs("\nconst short cfroots[] = {\n", fp); 542 for (p = packed; (i = *p) != NULL; p++) { 543 if (i->i_at != NULL) 544 continue; 545 if (i->i_unit != 0 && 546 (i->i_unit != STAR || i->i_base->d_umax != 0)) 547 warnx("warning: `%s at root' is not unit 0", i->i_name); 548 fprintf(fp, "\t%2d /* %s */,\n", 549 i->i_cfindex, i->i_name); 550 } 551 fputs("\t-1\n};\n", fp); 552 } 553 554 /* 555 * Emit pseudo-device initialization. 556 */ 557 static void 558 emitpseudo(FILE *fp) 559 { 560 struct devi *i; 561 struct devbase *d; 562 563 fputs("\n/* pseudo-devices */\n", fp); 564 fputs("\nconst struct pdevinit pdevinit[] = {\n", fp); 565 TAILQ_FOREACH(i, &allpseudo, i_next) { 566 d = i->i_base; 567 fprintf(fp, "\t{ %sattach, %d },\n", 568 d->d_name, d->d_umax); 569 } 570 fputs("\t{ 0, 0 }\n};\n", fp); 571 } 572 573 /* 574 * Emit name to major block number table. 575 */ 576 void 577 emitname2blk(FILE *fp) 578 { 579 struct devbase *dev; 580 581 fputs("\n/* device name to major block number */\n", fp); 582 583 fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n"); 584 585 TAILQ_FOREACH(dev, &allbases, d_next) { 586 if (dev->d_major == NODEVMAJOR) 587 continue; 588 589 fprintf(fp, "\t{ \"%s\", %d },\n", 590 dev->d_name, dev->d_major); 591 } 592 fprintf(fp, "\t{ NULL, 0 }\n};\n"); 593 } 594