xref: /netbsd-src/usr.bin/config/mkioconf.c (revision bf1e9b32e27832f0c493206710fb8b58a980838a)
1 /*	$NetBSD: mkioconf.c,v 1.2 2005/06/28 20:21:05 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_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,
303 			    "static const char * const %scf_locnames[] = { ",
304 			    name) < 0)
305 			return (1);
306 		for (nv = a->a_locs; nv; nv = nv->nv_next)
307 			if (fprintf(fp, "\"%s\", ", nv->nv_name) < 0)
308 				return (1);
309 		if (fprintf(fp, "NULL};\n") < 0)
310 			return (1);
311 	}
312 	return 0;
313 }
314 
315 static int
316 emitloc(FILE *fp)
317 {
318 	int i;
319 
320 	if (locators.used != 0) {
321 		if (fprintf(fp, "\n/* locators */\n\
322 static int loc[%d] = {", locators.used) < 0)
323 			return (1);
324 		for (i = 0; i < locators.used; i++)
325 			if (fprintf(fp, "%s%s,", SEP(i, 8),
326 			    locators.vec[i]) < 0)
327 				return (1);
328 		if (fprintf(fp, "\n};\n") < 0)
329 			return (1);
330 	} else if (*packed != NULL) {
331 		/* We need to have *something*. */
332 		if (fprintf(fp, "\n/* locators */\n\
333 static int loc[1] = { -1 };\n") < 0)
334 			return (1);
335 	}
336 
337 	if (*packed != NULL)
338 		if (fprintf(fp,
339 	    "\nstatic const char * const nullcf_locnames[] = {NULL};\n") < 0)
340 			return (1);
341 
342 	if (locators.used != 0)
343 		return ht_enumerate(attrtab, cf_locnames_print, fp);
344 
345 	return (0);
346 }
347 
348 /*
349  * Emit static parent data.
350  */
351 static int
352 emitparents(FILE *fp)
353 {
354 	struct pspec *p;
355 
356 	NEWLINE;
357 	TAILQ_FOREACH(p, &allpspecs, p_list) {
358 		if (p->p_devs == NULL)
359 			continue;
360 		if (fprintf(fp,
361 "static const struct cfparent pspec%d = {\n", p->p_inst) < 0)
362 			return (1);
363 		if (fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name) < 0)
364 			return (1);
365 		if (p->p_atdev != NULL) {
366 			if (fprintf(fp, "\"%s\", ", p->p_atdev->d_name) < 0)
367 				return (1);
368 			if (p->p_atunit == WILD &&
369 			    fprintf(fp, "DVUNIT_ANY") < 0)
370 				return (1);
371 			else if (p->p_atunit != WILD &&
372 				 fprintf(fp, "%d", p->p_atunit) < 0)
373 				return (1);
374 		} else if (fprintf(fp, "NULL, 0") < 0)
375 			return (1);
376 		if (fprintf(fp, "\n};\n") < 0)
377 			return (1);
378 	}
379 
380 	return (0);
381 }
382 
383 /*
384  * Emit the cfdata array.
385  */
386 static int
387 emitcfdata(FILE *fp)
388 {
389 	struct devi **p, *i;
390 	struct pspec *ps;
391 	int unit, v;
392 	const char *state, *basename, *attachment;
393 	struct nvlist *nv;
394 	struct attr *a;
395 	char *loc;
396 	char locbuf[20];
397 	const char *lastname = "";
398 
399 	if (fprintf(fp, "\n\
400 #define NORM FSTATE_NOTFOUND\n\
401 #define STAR FSTATE_STAR\n\
402 \n\
403 struct cfdata cfdata[] = {\n\
404     /* driver           attachment    unit state loc   flags pspec\n\
405        locnames */\n") < 0)
406 		return (1);
407 	for (p = packed; (i = *p) != NULL; p++) {
408 		/* the description */
409 		if (fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name) < 0)
410 			return (1);
411 		if ((ps = i->i_pspec) != NULL) {
412 			if (ps->p_atdev != NULL &&
413 			    ps->p_atunit != WILD) {
414 				if (fprintf(fp, "%s%d", ps->p_atdev->d_name,
415 					    ps->p_atunit) < 0)
416 					return (1);
417 			} else if (ps->p_atdev != NULL) {
418 				if (fprintf(fp, "%s?", ps->p_atdev->d_name) < 0)
419 					return (1);
420 			} else {
421 				if (fprintf(fp, "%s?", ps->p_iattr->a_name) < 0)
422 					return (1);
423 			}
424 
425 			a = ps->p_iattr;
426 			for (nv = a->a_locs, v = 0; nv != NULL;
427 			     nv = nv->nv_next, v++) {
428 				if (ARRNAME(nv->nv_name, lastname)) {
429 					if (fprintf(fp, " %s %s",
430 					    nv->nv_name, i->i_locs[v]) < 0)
431 						return (1);
432 				} else {
433 					if (fprintf(fp, " %s %s",
434 						    nv->nv_name,
435 						    i->i_locs[v]) < 0)
436 						return (1);
437 					lastname = nv->nv_name;
438 				}
439 			}
440 		} else {
441 			a = NULL;
442 			if (fputs("root", fp) < 0)
443 				return (1);
444 		}
445 
446 		if (fputs(" */\n", fp) < 0)
447 			return (-1);
448 
449 		/* then the actual defining line */
450 		basename = i->i_base->d_name;
451 		attachment = i->i_atdeva->d_name;
452 		if (i->i_unit == STAR) {
453 			unit = i->i_base->d_umax;
454 			state = "STAR";
455 		} else {
456 			unit = i->i_unit;
457 			state = "NORM";
458 		}
459 		if (i->i_locoff >= 0) {
460 			(void)snprintf(locbuf, sizeof(locbuf), "loc+%3d",
461 			    i->i_locoff);
462 			loc = locbuf;
463 		} else
464 			loc = "loc";
465 		if (fprintf(fp, "    {\"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ",
466 			    basename, strlen(basename) < 8 ? "\t\t"
467 			    				   : "\t",
468 			    attachment, strlen(attachment) < 5 ? "\t\t"
469 			    				       : "\t",
470 			    unit, state, loc, i->i_cfflags) < 0)
471 			return (1);
472 		if (ps != NULL) {
473 			if (fprintf(fp, "&pspec%d,\n", ps->p_inst) < 0)
474 				return (1);
475 		} else if (fputs("NULL,\n", fp) < 0)
476 			return (1);
477 		if (fprintf(fp, "     %scf_locnames},\n",
478 			    (a != NULL && a->a_locs != NULL) ? a->a_name
479 							     : "null") < 0)
480 			return (1);
481 	}
482 	return (fputs("    {0}\n};\n", fp) < 0);
483 }
484 
485 /*
486  * Emit the table of potential roots.
487  */
488 static int
489 emitroots(FILE *fp)
490 {
491 	struct devi **p, *i;
492 
493 	if (fputs("\nconst short cfroots[] = {\n", fp) < 0)
494 		return (1);
495 	for (p = packed; (i = *p) != NULL; p++) {
496 		if (i->i_at != NULL)
497 			continue;
498 		if (i->i_unit != 0 &&
499 		    (i->i_unit != STAR || i->i_base->d_umax != 0))
500 			(void)fprintf(stderr,
501 			    "config: warning: `%s at root' is not unit 0\n",
502 			    i->i_name);
503 		if (fprintf(fp, "\t%2d /* %s */,\n",
504 		    i->i_cfindex, i->i_name) < 0)
505 			return (1);
506 	}
507 	return (fputs("\t-1\n};\n", fp) < 0);
508 }
509 
510 /*
511  * Emit pseudo-device initialization.
512  */
513 static int
514 emitpseudo(FILE *fp)
515 {
516 	struct devi *i;
517 	struct devbase *d;
518 
519 	if (fputs("\n/* pseudo-devices */\n", fp) < 0)
520 		return (1);
521 	TAILQ_FOREACH(i, &allpseudo, i_next) {
522 		if (fprintf(fp, "void %sattach(int);\n",
523 		    i->i_base->d_name) < 0)
524 			return (1);
525 	}
526 	if (fputs("\nstruct pdevinit pdevinit[] = {\n", fp) < 0)
527 		return (1);
528 	TAILQ_FOREACH(i, &allpseudo, i_next) {
529 		d = i->i_base;
530 		if (fprintf(fp, "\t{ %sattach, %d },\n",
531 		    d->d_name, d->d_umax) < 0)
532 			return (1);
533 	}
534 	return (fputs("\t{ 0, 0 }\n};\n", fp) < 0);
535 }
536 
537 /*
538  * Emit name to major block number table.
539  */
540 int
541 emitname2blk(FILE *fp)
542 {
543 	struct devbase *dev;
544 
545 	if (fputs("\n/* device name to major block number */\n", fp) < 0)
546 		return (1);
547 
548 	if (fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n") < 0)
549 		return (1);
550 
551 	TAILQ_FOREACH(dev, &allbases, d_next) {
552 		if (dev->d_major == NODEV)
553 			continue;
554 
555 		if (fprintf(fp, "\t{ \"%s\", %d },\n",
556 			    dev->d_name, dev->d_major) < 0)
557 			return (1);
558 	}
559 	if (fprintf(fp, "\t{ NULL, 0 }\n};\n") < 0)
560 		return (1);
561 
562 	return (0);
563 }
564