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