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