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