1 /* $NetBSD: mkioconf.c,v 1.16 2010/02/03 21:32:27 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/param.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include "defs.h" 54 55 /* 56 * Make ioconf.c. 57 */ 58 static int cf_locators_print(const char *, void *, void *); 59 static int cforder(const void *, const void *); 60 static void emitcfdata(FILE *); 61 static void emitcfdrivers(FILE *); 62 static void emitexterns(FILE *); 63 static void emitcfattachinit(FILE *); 64 static void emithdr(FILE *); 65 static void emitloc(FILE *); 66 static void emitpseudo(FILE *); 67 static void emitparents(FILE *); 68 static void emitroots(FILE *); 69 static void emitname2blk(FILE *); 70 71 #define SEP(pos, max) (((u_int)(pos) % (max)) == 0 ? "\n\t" : " ") 72 73 #define ARRNAME(n, l) (strchr((n), ARRCHR) && strncmp((n), (l), strlen((l))) == 0) 74 75 /* 76 * NEWLINE can only be used in the emitXXX functions. 77 * In most cases it can be subsumed into an fprintf. 78 */ 79 #define NEWLINE putc('\n', fp) 80 81 int 82 mkioconf(void) 83 { 84 FILE *fp; 85 86 qsort(packed, npacked, sizeof *packed, cforder); 87 if ((fp = fopen("ioconf.c.tmp", "w")) == NULL) { 88 warn("cannot write ioconf.c"); 89 return (1); 90 } 91 92 emithdr(fp); 93 emitcfdrivers(fp); 94 emitexterns(fp); 95 emitloc(fp); 96 emitparents(fp); 97 emitcfdata(fp); 98 99 if (ioconfname == NULL) { 100 emitcfattachinit(fp); 101 emitroots(fp); 102 emitpseudo(fp); 103 if (!do_devsw) 104 emitname2blk(fp); 105 } 106 107 fflush(fp); 108 if (ferror(fp)) { 109 warn("error writing ioconf.c"); 110 (void)fclose(fp); 111 #if 0 112 (void)unlink("ioconf.c.tmp"); 113 #endif 114 return (1); 115 } 116 117 (void)fclose(fp); 118 if (moveifchanged("ioconf.c.tmp", "ioconf.c") != 0) { 119 warn("error renaming ioconf.c"); 120 return (1); 121 } 122 return (0); 123 } 124 125 static int 126 cforder(const void *a, const void *b) 127 { 128 int n1, n2; 129 130 n1 = (*(const struct devi * const *)a)->i_cfindex; 131 n2 = (*(const struct devi * const *)b)->i_cfindex; 132 return (n1 - n2); 133 } 134 135 static void 136 emithdr(FILE *ofp) 137 { 138 FILE *ifp; 139 int n; 140 char ifnbuf[200], buf[BUFSIZ]; 141 char *ifn; 142 143 autogen_comment(ofp, "ioconf.c"); 144 145 (void)snprintf(ifnbuf, sizeof(ifnbuf), "arch/%s/conf/ioconf.incl.%s", 146 machine, machine); 147 ifn = sourcepath(ifnbuf); 148 if ((ifp = fopen(ifn, "r")) != NULL) { 149 while ((n = fread(buf, 1, sizeof(buf), ifp)) > 0) 150 (void)fwrite(buf, 1, n, ofp); 151 if (ferror(ifp)) 152 err(EXIT_FAILURE, "error reading %s", ifn); 153 (void)fclose(ifp); 154 } else { 155 fputs("#include <sys/param.h>\n" 156 "#include <sys/conf.h>\n" 157 "#include <sys/device.h>\n" 158 "#include <sys/mount.h>\n", ofp); 159 } 160 free(ifn); 161 } 162 163 /* 164 * Emit an initialized array of character strings describing this 165 * attribute's locators. 166 */ 167 static int 168 cf_locators_print(const char *name, void *value, void *arg) 169 { 170 struct attr *a; 171 struct nvlist *nv; 172 FILE *fp = arg; 173 174 a = value; 175 if (!a->a_iattr) 176 return (0); 177 178 if (a->a_locs) { 179 fprintf(fp, 180 "static const struct cfiattrdata %scf_iattrdata = {\n", 181 name); 182 fprintf(fp, "\t\"%s\", %d,\n\t{\n", name, a->a_loclen); 183 for (nv = a->a_locs; nv; nv = nv->nv_next) 184 fprintf(fp, "\t\t{ \"%s\", \"%s\", %s },\n", 185 nv->nv_name, 186 (nv->nv_str ? nv->nv_str : "NULL"), 187 (nv->nv_str ? nv->nv_str : "0")); 188 fprintf(fp, "\t}\n};\n"); 189 } else { 190 fprintf(fp, 191 "static const struct cfiattrdata %scf_iattrdata = {\n" 192 "\t\"%s\", 0, {\n\t\t{ NULL, NULL, 0 },\n\t}\n};\n", 193 name, name); 194 } 195 196 return 0; 197 } 198 199 static void 200 emitcfdrivers(FILE *fp) 201 { 202 struct devbase *d; 203 struct nvlist *nv; 204 struct attr *a; 205 int has_iattrs; 206 207 NEWLINE; 208 ht_enumerate(attrtab, cf_locators_print, fp); 209 210 NEWLINE; 211 TAILQ_FOREACH(d, &allbases, d_next) { 212 if (!devbase_has_instances(d, WILD)) 213 continue; 214 has_iattrs = 0; 215 for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) { 216 a = nv->nv_ptr; 217 if (a->a_iattr == 0) 218 continue; 219 if (has_iattrs == 0) 220 fprintf(fp, 221 "static const struct cfiattrdata * const %s_attrs[] = { ", 222 d->d_name); 223 has_iattrs = 1; 224 fprintf(fp, "&%scf_iattrdata, ", a->a_name); 225 } 226 if (has_iattrs) 227 fprintf(fp, "NULL };\n"); 228 fprintf(fp, "CFDRIVER_DECL(%s, %s, ", d->d_name, /* ) */ 229 d->d_classattr != NULL ? d->d_classattr->a_devclass 230 : "DV_DULL"); 231 if (has_iattrs) 232 fprintf(fp, "%s_attrs", d->d_name); 233 else 234 fprintf(fp, "NULL"); 235 fprintf(fp, /* ( */ ");\n\n"); 236 } 237 238 NEWLINE; 239 240 /* the initial list is not added to ioconf-only configs */ 241 if (ioconfname) 242 return; 243 244 fprintf(fp, "struct cfdriver * const cfdriver_list_initial[] = {\n"); 245 TAILQ_FOREACH(d, &allbases, d_next) { 246 if (!devbase_has_instances(d, WILD)) 247 continue; 248 fprintf(fp, "\t&%s_cd,\n", d->d_name); 249 } 250 fprintf(fp, "\tNULL\n};\n"); 251 } 252 253 static void 254 emitexterns(FILE *fp) 255 { 256 struct deva *da; 257 258 NEWLINE; 259 TAILQ_FOREACH(da, &alldevas, d_next) { 260 if (!deva_has_instances(da, WILD)) 261 continue; 262 fprintf(fp, "extern struct cfattach %s_ca;\n", 263 da->d_name); 264 } 265 } 266 267 static void 268 emitcfattachinit(FILE *fp) 269 { 270 struct devbase *d; 271 struct deva *da; 272 273 NEWLINE; 274 TAILQ_FOREACH(d, &allbases, d_next) { 275 if (!devbase_has_instances(d, WILD)) 276 continue; 277 if (d->d_ahead == NULL) 278 continue; 279 280 fprintf(fp, 281 "static struct cfattach * const %s_cfattachinit[] = {\n\t", 282 d->d_name); 283 for (da = d->d_ahead; da != NULL; da = da->d_bsame) { 284 if (!deva_has_instances(da, WILD)) 285 continue; 286 fprintf(fp, "&%s_ca, ", da->d_name); 287 } 288 fprintf(fp, "NULL\n};\n"); 289 } 290 291 NEWLINE; 292 fprintf(fp, "const struct cfattachinit cfattachinit[] = {\n"); 293 294 TAILQ_FOREACH(d, &allbases, d_next) { 295 if (!devbase_has_instances(d, WILD)) 296 continue; 297 if (d->d_ahead == NULL) 298 continue; 299 300 fprintf(fp, "\t{ \"%s\", %s_cfattachinit },\n", 301 d->d_name, d->d_name); 302 } 303 304 fprintf(fp, "\t{ NULL, NULL }\n};\n"); 305 } 306 307 static void 308 emitloc(FILE *fp) 309 { 310 int i; 311 312 if (locators.used != 0) { 313 fprintf(fp, "\n/* locators */\n" 314 "static int loc[%d] = {", locators.used); 315 for (i = 0; i < locators.used; i++) 316 fprintf(fp, "%s%s,", SEP(i, 8), locators.vec[i]); 317 fprintf(fp, "\n};\n"); 318 } else if (*packed != NULL) { 319 /* We need to have *something*. */ 320 fprintf(fp, "\n/* locators */\n" 321 "static int loc[1] = { -1 };\n"); 322 } 323 } 324 325 /* 326 * Emit static parent data. 327 */ 328 static void 329 emitparents(FILE *fp) 330 { 331 struct pspec *p; 332 333 NEWLINE; 334 TAILQ_FOREACH(p, &allpspecs, p_list) { 335 if (p->p_devs == NULL || p->p_active != DEVI_ACTIVE) 336 continue; 337 fprintf(fp, 338 "static const struct cfparent pspec%d = {\n", p->p_inst); 339 fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name); 340 if (p->p_atdev != NULL) { 341 fprintf(fp, "\"%s\", ", p->p_atdev->d_name); 342 if (p->p_atunit == WILD) 343 fprintf(fp, "DVUNIT_ANY"); 344 else 345 fprintf(fp, "%d", p->p_atunit); 346 } else 347 fprintf(fp, "NULL, 0"); 348 fprintf(fp, "\n};\n"); 349 } 350 } 351 352 /* 353 * Emit the cfdata array. 354 */ 355 static void 356 emitcfdata(FILE *fp) 357 { 358 struct devi **p, *i; 359 struct pspec *ps; 360 int unit, v; 361 const char *state, *basename, *attachment; 362 struct nvlist *nv; 363 struct attr *a; 364 const char *loc; 365 char locbuf[20]; 366 const char *lastname = ""; 367 368 fprintf(fp, "\n" 369 "#define NORM FSTATE_NOTFOUND\n" 370 "#define STAR FSTATE_STAR\n" 371 "\n" 372 "struct cfdata cfdata%s%s[] = {\n" 373 " /* driver attachment unit state " 374 "loc flags pspec */\n", 375 ioconfname ? "_" : "", 376 ioconfname ? ioconfname : ""); 377 for (p = packed; (i = *p) != NULL; p++) { 378 /* the description */ 379 fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name); 380 if ((ps = i->i_pspec) != NULL) { 381 if (ps->p_atdev != NULL && 382 ps->p_atunit != WILD) { 383 fprintf(fp, "%s%d", ps->p_atdev->d_name, 384 ps->p_atunit); 385 } else if (ps->p_atdev != NULL) { 386 fprintf(fp, "%s?", ps->p_atdev->d_name); 387 } else { 388 fprintf(fp, "%s?", ps->p_iattr->a_name); 389 } 390 391 a = ps->p_iattr; 392 for (nv = a->a_locs, v = 0; nv != NULL; 393 nv = nv->nv_next, v++) { 394 if (ARRNAME(nv->nv_name, lastname)) { 395 fprintf(fp, " %s %s", 396 nv->nv_name, i->i_locs[v]); 397 } else { 398 fprintf(fp, " %s %s", 399 nv->nv_name, 400 i->i_locs[v]); 401 lastname = nv->nv_name; 402 } 403 } 404 } else { 405 a = NULL; 406 fputs("root", fp); 407 } 408 409 fputs(" */\n", fp); 410 411 /* then the actual defining line */ 412 basename = i->i_base->d_name; 413 attachment = i->i_atdeva->d_name; 414 if (i->i_unit == STAR) { 415 unit = i->i_base->d_umax; 416 state = "STAR"; 417 } else { 418 unit = i->i_unit; 419 state = "NORM"; 420 } 421 if (i->i_locoff >= 0) { 422 (void)snprintf(locbuf, sizeof(locbuf), "loc+%3d", 423 i->i_locoff); 424 loc = locbuf; 425 } else 426 loc = "loc"; 427 fprintf(fp, " { \"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ", 428 basename, strlen(basename) < 8 ? "\t\t" 429 : "\t", 430 attachment, strlen(attachment) < 5 ? "\t\t" 431 : "\t", 432 unit, state, loc, i->i_cfflags); 433 if (ps != NULL) 434 fprintf(fp, "&pspec%d },\n", ps->p_inst); 435 else 436 fputs("NULL },\n", fp); 437 } 438 fprintf(fp, " { %s,%s%s,%s%2d, %s, %7s, %#6x, %s }\n};\n", 439 "NULL", "\t\t", "NULL", "\t\t", 0, "0", "NULL", 0, "NULL"); 440 } 441 442 /* 443 * Emit the table of potential roots. 444 */ 445 static void 446 emitroots(FILE *fp) 447 { 448 struct devi **p, *i; 449 450 fputs("\nconst short cfroots[] = {\n", fp); 451 for (p = packed; (i = *p) != NULL; p++) { 452 if (i->i_at != NULL) 453 continue; 454 if (i->i_unit != 0 && 455 (i->i_unit != STAR || i->i_base->d_umax != 0)) 456 warnx("warning: `%s at root' is not unit 0", i->i_name); 457 fprintf(fp, "\t%2d /* %s */,\n", 458 i->i_cfindex, i->i_name); 459 } 460 fputs("\t-1\n};\n", fp); 461 } 462 463 /* 464 * Emit pseudo-device initialization. 465 */ 466 static void 467 emitpseudo(FILE *fp) 468 { 469 struct devi *i; 470 struct devbase *d; 471 472 fputs("\n/* pseudo-devices */\n", fp); 473 TAILQ_FOREACH(i, &allpseudo, i_next) { 474 fprintf(fp, "void %sattach(int);\n", 475 i->i_base->d_name); 476 } 477 fputs("\nstruct pdevinit pdevinit[] = {\n", fp); 478 TAILQ_FOREACH(i, &allpseudo, i_next) { 479 d = i->i_base; 480 fprintf(fp, "\t{ %sattach, %d },\n", 481 d->d_name, d->d_umax); 482 } 483 fputs("\t{ 0, 0 }\n};\n", fp); 484 } 485 486 /* 487 * Emit name to major block number table. 488 */ 489 void 490 emitname2blk(FILE *fp) 491 { 492 struct devbase *dev; 493 494 fputs("\n/* device name to major block number */\n", fp); 495 496 fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n"); 497 498 TAILQ_FOREACH(dev, &allbases, d_next) { 499 if (dev->d_major == NODEVMAJOR) 500 continue; 501 502 fprintf(fp, "\t{ \"%s\", %d },\n", 503 dev->d_name, dev->d_major); 504 } 505 fprintf(fp, "\t{ NULL, 0 }\n};\n"); 506 } 507