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