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