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