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