Home | History | Annotate | Line # | Download | only in config
mkioconf.c revision 1.1
      1 /*	$NetBSD: mkioconf.c,v 1.1 2005/06/05 18:19:53 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/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_locnames_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 static int
    169 emitcfdrivers(FILE *fp)
    170 {
    171 	struct devbase *d;
    172 	struct nvlist *nv;
    173 	struct attr *a;
    174 	int has_iattrs;
    175 
    176 	NEWLINE;
    177 	TAILQ_FOREACH(d, &allbases, d_next) {
    178 		if (!devbase_has_instances(d, WILD))
    179 			continue;
    180 		has_iattrs = 0;
    181 		for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
    182 			a = nv->nv_ptr;
    183 			if (a->a_iattr == 0)
    184 				continue;
    185 			if (has_iattrs == 0 &&
    186 			    fprintf(fp,
    187 			    	    "static const char * const %s_attrs[] = { ",
    188 			    	    d->d_name) < 0)
    189 				return (1);
    190 			has_iattrs = 1;
    191 			if (fprintf(fp, "\"%s\", ", a->a_name) < 0)
    192 				return (1);
    193 		}
    194 		if (has_iattrs && fprintf(fp, "NULL };\n") < 0)
    195 			return (1);
    196 		if (fprintf(fp, "CFDRIVER_DECL(%s, %s, ", d->d_name, /* ) */
    197 		    d->d_classattr != NULL ? d->d_classattr->a_devclass
    198 					   : "DV_DULL") < 0)
    199 			return (1);
    200 		if (has_iattrs && fprintf(fp, "%s_attrs", d->d_name) < 0)
    201 			return (1);
    202 		else if (has_iattrs == 0 && fprintf(fp, "NULL") < 0)
    203 			return (1);
    204 		if (fprintf(fp, /* ( */ ");\n\n") < 0)
    205 			return (1);
    206 	}
    207 
    208 	NEWLINE;
    209 	if (fprintf(fp,
    210 		  "struct cfdriver * const cfdriver_list_initial[] = {\n") < 0)
    211 		return (1);
    212 	TAILQ_FOREACH(d, &allbases, d_next) {
    213 		if (!devbase_has_instances(d, WILD))
    214 			continue;
    215 		if (fprintf(fp, "\t&%s_cd,\n", d->d_name) < 0)
    216 			return (1);
    217 	}
    218 	if (fprintf(fp, "\tNULL\n};\n") < 0)
    219 		return (1);
    220 
    221 	return (0);
    222 }
    223 
    224 static int
    225 emitexterns(FILE *fp)
    226 {
    227 	struct deva *da;
    228 
    229 	NEWLINE;
    230 	TAILQ_FOREACH(da, &alldevas, d_next) {
    231 		if (!deva_has_instances(da, WILD))
    232 			continue;
    233 		if (fprintf(fp, "extern struct cfattach %s_ca;\n",
    234 			    da->d_name) < 0)
    235 			return (1);
    236 	}
    237 	return (0);
    238 }
    239 
    240 static int
    241 emitcfattachinit(FILE *fp)
    242 {
    243 	struct devbase *d;
    244 	struct deva *da;
    245 
    246 	NEWLINE;
    247 	TAILQ_FOREACH(d, &allbases, d_next) {
    248 		if (!devbase_has_instances(d, WILD))
    249 			continue;
    250 		if (d->d_ahead == NULL)
    251 			continue;
    252 
    253 		if (fprintf(fp,
    254 "static struct cfattach * const %s_cfattachinit[] = {\n\t",
    255 			    d->d_name) < 0)
    256 			return (1);
    257 		for (da = d->d_ahead; da != NULL; da = da->d_bsame) {
    258 			if (!deva_has_instances(da, WILD))
    259 				continue;
    260 			if (fprintf(fp, "&%s_ca, ", da->d_name) < 0)
    261 				return (1);
    262 		}
    263 		if (fprintf(fp, "NULL\n};\n") < 0)
    264 			return (1);
    265 	}
    266 
    267 	NEWLINE;
    268 	if (fprintf(fp,
    269 "const struct cfattachinit cfattachinit[] = {\n") < 0)
    270 		return (1);
    271 
    272 	TAILQ_FOREACH(d, &allbases, d_next) {
    273 		if (!devbase_has_instances(d, WILD))
    274 			continue;
    275 		if (d->d_ahead == NULL)
    276 			continue;
    277 
    278 		if (fprintf(fp, "\t{ \"%s\", %s_cfattachinit },\n",
    279 			    d->d_name, d->d_name) < 0)
    280 			return (1);
    281 	}
    282 
    283 	if (fprintf(fp, "\t{ NULL, NULL }\n};\n") < 0)
    284 		return (1);
    285 
    286 	return (0);
    287 }
    288 
    289 /*
    290  * Emit an initialized array of character strings describing this
    291  * attribute's locators.
    292  */
    293 static int
    294 cf_locnames_print(const char *name, void *value, void *arg)
    295 {
    296 	struct attr *a;
    297 	struct nvlist *nv;
    298 	FILE *fp = arg;
    299 
    300 	a = value;
    301 	if (a->a_locs) {
    302 		if (fprintf(fp, "const char * const %scf_locnames[] = { ",
    303 			    name) < 0)
    304 			return (1);
    305 		for (nv = a->a_locs; nv; nv = nv->nv_next)
    306 			if (fprintf(fp, "\"%s\", ", nv->nv_name) < 0)
    307 				return (1);
    308 		if (fprintf(fp, "NULL};\n") < 0)
    309 			return (1);
    310 	}
    311 	return 0;
    312 }
    313 
    314 static int
    315 emitloc(FILE *fp)
    316 {
    317 	int i;
    318 
    319 	if (locators.used != 0) {
    320 		if (fprintf(fp, "\n/* locators */\n\
    321 static int loc[%d] = {", locators.used) < 0)
    322 			return (1);
    323 		for (i = 0; i < locators.used; i++)
    324 			if (fprintf(fp, "%s%s,", SEP(i, 8),
    325 			    locators.vec[i]) < 0)
    326 				return (1);
    327 		if (fprintf(fp, "\n};\n") < 0)
    328 			return (1);
    329 	} else if (*packed != NULL) {
    330 		/* We need to have *something*. */
    331 		if (fprintf(fp, "\n/* locators */\n\
    332 static int loc[1] = { -1 };\n") < 0)
    333 			return (1);
    334 	}
    335 
    336 	if (*packed != NULL)
    337 		if (fprintf(fp,
    338 		    "\nconst char * const nullcf_locnames[] = {NULL};\n") < 0)
    339 			return (1);
    340 
    341 	if (locators.used != 0)
    342 		return ht_enumerate(attrtab, cf_locnames_print, fp);
    343 
    344 	return (0);
    345 }
    346 
    347 /*
    348  * Emit static parent data.
    349  */
    350 static int
    351 emitparents(FILE *fp)
    352 {
    353 	struct pspec *p;
    354 
    355 	NEWLINE;
    356 	TAILQ_FOREACH(p, &allpspecs, p_list) {
    357 		if (p->p_devs == NULL)
    358 			continue;
    359 		if (fprintf(fp,
    360 "static const struct cfparent pspec%d = {\n", p->p_inst) < 0)
    361 			return (1);
    362 		if (fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name) < 0)
    363 			return (1);
    364 		if (p->p_atdev != NULL) {
    365 			if (fprintf(fp, "\"%s\", ", p->p_atdev->d_name) < 0)
    366 				return (1);
    367 			if (p->p_atunit == WILD &&
    368 			    fprintf(fp, "DVUNIT_ANY") < 0)
    369 				return (1);
    370 			else if (p->p_atunit != WILD &&
    371 				 fprintf(fp, "%d", p->p_atunit) < 0)
    372 				return (1);
    373 		} else if (fprintf(fp, "NULL, 0") < 0)
    374 			return (1);
    375 		if (fprintf(fp, "\n};\n") < 0)
    376 			return (1);
    377 	}
    378 
    379 	return (0);
    380 }
    381 
    382 /*
    383  * Emit the cfdata array.
    384  */
    385 static int
    386 emitcfdata(FILE *fp)
    387 {
    388 	struct devi **p, *i;
    389 	struct pspec *ps;
    390 	int unit, v;
    391 	const char *state, *basename, *attachment;
    392 	struct nvlist *nv;
    393 	struct attr *a;
    394 	char *loc;
    395 	char locbuf[20];
    396 	const char *lastname = "";
    397 
    398 	if (fprintf(fp, "\n\
    399 #define NORM FSTATE_NOTFOUND\n\
    400 #define STAR FSTATE_STAR\n\
    401 \n\
    402 struct cfdata cfdata[] = {\n\
    403     /* driver           attachment    unit state loc   flags pspec\n\
    404        locnames */\n") < 0)
    405 		return (1);
    406 	for (p = packed; (i = *p) != NULL; p++) {
    407 		/* the description */
    408 		if (fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name) < 0)
    409 			return (1);
    410 		if ((ps = i->i_pspec) != NULL) {
    411 			if (ps->p_atdev != NULL &&
    412 			    ps->p_atunit != WILD) {
    413 				if (fprintf(fp, "%s%d", ps->p_atdev->d_name,
    414 					    ps->p_atunit) < 0)
    415 					return (1);
    416 			} else if (ps->p_atdev != NULL) {
    417 				if (fprintf(fp, "%s?", ps->p_atdev->d_name) < 0)
    418 					return (1);
    419 			} else {
    420 				if (fprintf(fp, "%s?", ps->p_iattr->a_name) < 0)
    421 					return (1);
    422 			}
    423 
    424 			a = ps->p_iattr;
    425 			for (nv = a->a_locs, v = 0; nv != NULL;
    426 			     nv = nv->nv_next, v++) {
    427 				if (ARRNAME(nv->nv_name, lastname)) {
    428 					if (fprintf(fp, " %s %s",
    429 					    nv->nv_name, i->i_locs[v]) < 0)
    430 						return (1);
    431 				} else {
    432 					if (fprintf(fp, " %s %s",
    433 						    nv->nv_name,
    434 						    i->i_locs[v]) < 0)
    435 						return (1);
    436 					lastname = nv->nv_name;
    437 				}
    438 			}
    439 		} else {
    440 			a = NULL;
    441 			if (fputs("root", fp) < 0)
    442 				return (1);
    443 		}
    444 
    445 		if (fputs(" */\n", fp) < 0)
    446 			return (-1);
    447 
    448 		/* then the actual defining line */
    449 		basename = i->i_base->d_name;
    450 		attachment = i->i_atdeva->d_name;
    451 		if (i->i_unit == STAR) {
    452 			unit = i->i_base->d_umax;
    453 			state = "STAR";
    454 		} else {
    455 			unit = i->i_unit;
    456 			state = "NORM";
    457 		}
    458 		if (i->i_locoff >= 0) {
    459 			(void)snprintf(locbuf, sizeof(locbuf), "loc+%3d",
    460 			    i->i_locoff);
    461 			loc = locbuf;
    462 		} else
    463 			loc = "loc";
    464 		if (fprintf(fp, "    {\"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ",
    465 			    basename, strlen(basename) < 8 ? "\t\t"
    466 			    				   : "\t",
    467 			    attachment, strlen(attachment) < 5 ? "\t\t"
    468 			    				       : "\t",
    469 			    unit, state, loc, i->i_cfflags) < 0)
    470 			return (1);
    471 		if (ps != NULL) {
    472 			if (fprintf(fp, "&pspec%d,\n", ps->p_inst) < 0)
    473 				return (1);
    474 		} else if (fputs("NULL,\n", fp) < 0)
    475 			return (1);
    476 		if (fprintf(fp, "     %scf_locnames},\n",
    477 			    (a != NULL && a->a_locs != NULL) ? a->a_name
    478 							     : "null") < 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