mkioconf.c revision 1.1 1 1.1 thorpej /* $NetBSD: mkioconf.c,v 1.1 2005/06/05 18:19:53 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software was developed by the Computer Systems Engineering group
8 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 thorpej * contributed to Berkeley.
10 1.1 thorpej *
11 1.1 thorpej * All advertising materials mentioning features or use of this software
12 1.1 thorpej * must display the following acknowledgement:
13 1.1 thorpej * This product includes software developed by the University of
14 1.1 thorpej * California, Lawrence Berkeley Laboratories.
15 1.1 thorpej *
16 1.1 thorpej * Redistribution and use in source and binary forms, with or without
17 1.1 thorpej * modification, are permitted provided that the following conditions
18 1.1 thorpej * are met:
19 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer.
21 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
23 1.1 thorpej * documentation and/or other materials provided with the distribution.
24 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
25 1.1 thorpej * may be used to endorse or promote products derived from this software
26 1.1 thorpej * without specific prior written permission.
27 1.1 thorpej *
28 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 thorpej * SUCH DAMAGE.
39 1.1 thorpej *
40 1.1 thorpej * from: @(#)mkioconf.c 8.1 (Berkeley) 6/6/93
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
44 1.1 thorpej #include "nbtool_config.h"
45 1.1 thorpej #endif
46 1.1 thorpej
47 1.1 thorpej #include <sys/param.h>
48 1.1 thorpej #include <errno.h>
49 1.1 thorpej #include <stdio.h>
50 1.1 thorpej #include <stdlib.h>
51 1.1 thorpej #include <string.h>
52 1.1 thorpej #include "defs.h"
53 1.1 thorpej
54 1.1 thorpej /*
55 1.1 thorpej * Make ioconf.c.
56 1.1 thorpej */
57 1.1 thorpej static int cf_locnames_print(const char *, void *, void *);
58 1.1 thorpej static int cforder(const void *, const void *);
59 1.1 thorpej static int emitcfdata(FILE *);
60 1.1 thorpej static int emitcfdrivers(FILE *);
61 1.1 thorpej static int emitexterns(FILE *);
62 1.1 thorpej static int emitcfattachinit(FILE *);
63 1.1 thorpej static int emithdr(FILE *);
64 1.1 thorpej static int emitloc(FILE *);
65 1.1 thorpej static int emitpseudo(FILE *);
66 1.1 thorpej static int emitparents(FILE *);
67 1.1 thorpej static int emitroots(FILE *);
68 1.1 thorpej static int emitname2blk(FILE *);
69 1.1 thorpej
70 1.1 thorpej #define SEP(pos, max) (((u_int)(pos) % (max)) == 0 ? "\n\t" : " ")
71 1.1 thorpej
72 1.1 thorpej #define ARRNAME(n, l) (strchr((n), ARRCHR) && strncmp((n), (l), strlen((l))) == 0)
73 1.1 thorpej
74 1.1 thorpej /*
75 1.1 thorpej * NEWLINE can only be used in the emitXXX functions.
76 1.1 thorpej * In most cases it can be subsumed into an fprintf.
77 1.1 thorpej */
78 1.1 thorpej #define NEWLINE if (putc('\n', fp) < 0) return (1)
79 1.1 thorpej
80 1.1 thorpej int
81 1.1 thorpej mkioconf(void)
82 1.1 thorpej {
83 1.1 thorpej FILE *fp;
84 1.1 thorpej int v;
85 1.1 thorpej
86 1.1 thorpej qsort(packed, npacked, sizeof *packed, cforder);
87 1.1 thorpej if ((fp = fopen("ioconf.c.tmp", "w")) == NULL) {
88 1.1 thorpej (void)fprintf(stderr, "config: cannot write ioconf.c: %s\n",
89 1.1 thorpej strerror(errno));
90 1.1 thorpej return (1);
91 1.1 thorpej }
92 1.1 thorpej v = emithdr(fp);
93 1.1 thorpej if (v != 0 || emitcfdrivers(fp) || emitexterns(fp) ||
94 1.1 thorpej emitcfattachinit(fp) || emitloc(fp) || emitparents(fp) ||
95 1.1 thorpej emitcfdata(fp) || emitroots(fp) || emitpseudo(fp) ||
96 1.1 thorpej (do_devsw ? 0 : emitname2blk(fp))) {
97 1.1 thorpej if (v >= 0)
98 1.1 thorpej (void)fprintf(stderr,
99 1.1 thorpej "config: error writing ioconf.c: %s\n",
100 1.1 thorpej strerror(errno));
101 1.1 thorpej (void)fclose(fp);
102 1.1 thorpej /* (void)unlink("ioconf.c.tmp"); */
103 1.1 thorpej return (1);
104 1.1 thorpej }
105 1.1 thorpej (void)fclose(fp);
106 1.1 thorpej if (moveifchanged("ioconf.c.tmp", "ioconf.c") != 0) {
107 1.1 thorpej (void)fprintf(stderr, "config: error renaming ioconf.c: %s\n",
108 1.1 thorpej strerror(errno));
109 1.1 thorpej return (1);
110 1.1 thorpej }
111 1.1 thorpej return (0);
112 1.1 thorpej }
113 1.1 thorpej
114 1.1 thorpej static int
115 1.1 thorpej cforder(const void *a, const void *b)
116 1.1 thorpej {
117 1.1 thorpej int n1, n2;
118 1.1 thorpej
119 1.1 thorpej n1 = (*(struct devi **)a)->i_cfindex;
120 1.1 thorpej n2 = (*(struct devi **)b)->i_cfindex;
121 1.1 thorpej return (n1 - n2);
122 1.1 thorpej }
123 1.1 thorpej
124 1.1 thorpej static int
125 1.1 thorpej emithdr(FILE *ofp)
126 1.1 thorpej {
127 1.1 thorpej FILE *ifp;
128 1.1 thorpej int n, rv;
129 1.1 thorpej char ifnbuf[200], buf[BUFSIZ];
130 1.1 thorpej char *ifn;
131 1.1 thorpej
132 1.1 thorpej if (fprintf(ofp, "\
133 1.1 thorpej /*\n\
134 1.1 thorpej * MACHINE GENERATED: DO NOT EDIT\n\
135 1.1 thorpej *\n\
136 1.1 thorpej * ioconf.c, from \"%s\"\n\
137 1.1 thorpej */\n\n", conffile) < 0)
138 1.1 thorpej return (1);
139 1.1 thorpej
140 1.1 thorpej rv = 0;
141 1.1 thorpej (void)snprintf(ifnbuf, sizeof(ifnbuf), "arch/%s/conf/ioconf.incl.%s",
142 1.1 thorpej machine, machine);
143 1.1 thorpej ifn = sourcepath(ifnbuf);
144 1.1 thorpej if ((ifp = fopen(ifn, "r")) != NULL) {
145 1.1 thorpej while ((n = fread(buf, 1, sizeof(buf), ifp)) > 0)
146 1.1 thorpej if (fwrite(buf, 1, n, ofp) != n) {
147 1.1 thorpej rv = 1;
148 1.1 thorpej break;
149 1.1 thorpej }
150 1.1 thorpej if (rv == 0 && ferror(ifp)) {
151 1.1 thorpej (void)fprintf(stderr, "config: error reading %s: %s\n",
152 1.1 thorpej ifn, strerror(errno));
153 1.1 thorpej rv = -1;
154 1.1 thorpej }
155 1.1 thorpej (void)fclose(ifp);
156 1.1 thorpej } else {
157 1.1 thorpej if (fputs("\
158 1.1 thorpej #include <sys/param.h>\n\
159 1.1 thorpej #include <sys/conf.h>\n\
160 1.1 thorpej #include <sys/device.h>\n\
161 1.1 thorpej #include <sys/mount.h>\n", ofp) < 0)
162 1.1 thorpej rv = 1;
163 1.1 thorpej }
164 1.1 thorpej free(ifn);
165 1.1 thorpej return (rv);
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej static int
169 1.1 thorpej emitcfdrivers(FILE *fp)
170 1.1 thorpej {
171 1.1 thorpej struct devbase *d;
172 1.1 thorpej struct nvlist *nv;
173 1.1 thorpej struct attr *a;
174 1.1 thorpej int has_iattrs;
175 1.1 thorpej
176 1.1 thorpej NEWLINE;
177 1.1 thorpej TAILQ_FOREACH(d, &allbases, d_next) {
178 1.1 thorpej if (!devbase_has_instances(d, WILD))
179 1.1 thorpej continue;
180 1.1 thorpej has_iattrs = 0;
181 1.1 thorpej for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) {
182 1.1 thorpej a = nv->nv_ptr;
183 1.1 thorpej if (a->a_iattr == 0)
184 1.1 thorpej continue;
185 1.1 thorpej if (has_iattrs == 0 &&
186 1.1 thorpej fprintf(fp,
187 1.1 thorpej "static const char * const %s_attrs[] = { ",
188 1.1 thorpej d->d_name) < 0)
189 1.1 thorpej return (1);
190 1.1 thorpej has_iattrs = 1;
191 1.1 thorpej if (fprintf(fp, "\"%s\", ", a->a_name) < 0)
192 1.1 thorpej return (1);
193 1.1 thorpej }
194 1.1 thorpej if (has_iattrs && fprintf(fp, "NULL };\n") < 0)
195 1.1 thorpej return (1);
196 1.1 thorpej if (fprintf(fp, "CFDRIVER_DECL(%s, %s, ", d->d_name, /* ) */
197 1.1 thorpej d->d_classattr != NULL ? d->d_classattr->a_devclass
198 1.1 thorpej : "DV_DULL") < 0)
199 1.1 thorpej return (1);
200 1.1 thorpej if (has_iattrs && fprintf(fp, "%s_attrs", d->d_name) < 0)
201 1.1 thorpej return (1);
202 1.1 thorpej else if (has_iattrs == 0 && fprintf(fp, "NULL") < 0)
203 1.1 thorpej return (1);
204 1.1 thorpej if (fprintf(fp, /* ( */ ");\n\n") < 0)
205 1.1 thorpej return (1);
206 1.1 thorpej }
207 1.1 thorpej
208 1.1 thorpej NEWLINE;
209 1.1 thorpej if (fprintf(fp,
210 1.1 thorpej "struct cfdriver * const cfdriver_list_initial[] = {\n") < 0)
211 1.1 thorpej return (1);
212 1.1 thorpej TAILQ_FOREACH(d, &allbases, d_next) {
213 1.1 thorpej if (!devbase_has_instances(d, WILD))
214 1.1 thorpej continue;
215 1.1 thorpej if (fprintf(fp, "\t&%s_cd,\n", d->d_name) < 0)
216 1.1 thorpej return (1);
217 1.1 thorpej }
218 1.1 thorpej if (fprintf(fp, "\tNULL\n};\n") < 0)
219 1.1 thorpej return (1);
220 1.1 thorpej
221 1.1 thorpej return (0);
222 1.1 thorpej }
223 1.1 thorpej
224 1.1 thorpej static int
225 1.1 thorpej emitexterns(FILE *fp)
226 1.1 thorpej {
227 1.1 thorpej struct deva *da;
228 1.1 thorpej
229 1.1 thorpej NEWLINE;
230 1.1 thorpej TAILQ_FOREACH(da, &alldevas, d_next) {
231 1.1 thorpej if (!deva_has_instances(da, WILD))
232 1.1 thorpej continue;
233 1.1 thorpej if (fprintf(fp, "extern struct cfattach %s_ca;\n",
234 1.1 thorpej da->d_name) < 0)
235 1.1 thorpej return (1);
236 1.1 thorpej }
237 1.1 thorpej return (0);
238 1.1 thorpej }
239 1.1 thorpej
240 1.1 thorpej static int
241 1.1 thorpej emitcfattachinit(FILE *fp)
242 1.1 thorpej {
243 1.1 thorpej struct devbase *d;
244 1.1 thorpej struct deva *da;
245 1.1 thorpej
246 1.1 thorpej NEWLINE;
247 1.1 thorpej TAILQ_FOREACH(d, &allbases, d_next) {
248 1.1 thorpej if (!devbase_has_instances(d, WILD))
249 1.1 thorpej continue;
250 1.1 thorpej if (d->d_ahead == NULL)
251 1.1 thorpej continue;
252 1.1 thorpej
253 1.1 thorpej if (fprintf(fp,
254 1.1 thorpej "static struct cfattach * const %s_cfattachinit[] = {\n\t",
255 1.1 thorpej d->d_name) < 0)
256 1.1 thorpej return (1);
257 1.1 thorpej for (da = d->d_ahead; da != NULL; da = da->d_bsame) {
258 1.1 thorpej if (!deva_has_instances(da, WILD))
259 1.1 thorpej continue;
260 1.1 thorpej if (fprintf(fp, "&%s_ca, ", da->d_name) < 0)
261 1.1 thorpej return (1);
262 1.1 thorpej }
263 1.1 thorpej if (fprintf(fp, "NULL\n};\n") < 0)
264 1.1 thorpej return (1);
265 1.1 thorpej }
266 1.1 thorpej
267 1.1 thorpej NEWLINE;
268 1.1 thorpej if (fprintf(fp,
269 1.1 thorpej "const struct cfattachinit cfattachinit[] = {\n") < 0)
270 1.1 thorpej return (1);
271 1.1 thorpej
272 1.1 thorpej TAILQ_FOREACH(d, &allbases, d_next) {
273 1.1 thorpej if (!devbase_has_instances(d, WILD))
274 1.1 thorpej continue;
275 1.1 thorpej if (d->d_ahead == NULL)
276 1.1 thorpej continue;
277 1.1 thorpej
278 1.1 thorpej if (fprintf(fp, "\t{ \"%s\", %s_cfattachinit },\n",
279 1.1 thorpej d->d_name, d->d_name) < 0)
280 1.1 thorpej return (1);
281 1.1 thorpej }
282 1.1 thorpej
283 1.1 thorpej if (fprintf(fp, "\t{ NULL, NULL }\n};\n") < 0)
284 1.1 thorpej return (1);
285 1.1 thorpej
286 1.1 thorpej return (0);
287 1.1 thorpej }
288 1.1 thorpej
289 1.1 thorpej /*
290 1.1 thorpej * Emit an initialized array of character strings describing this
291 1.1 thorpej * attribute's locators.
292 1.1 thorpej */
293 1.1 thorpej static int
294 1.1 thorpej cf_locnames_print(const char *name, void *value, void *arg)
295 1.1 thorpej {
296 1.1 thorpej struct attr *a;
297 1.1 thorpej struct nvlist *nv;
298 1.1 thorpej FILE *fp = arg;
299 1.1 thorpej
300 1.1 thorpej a = value;
301 1.1 thorpej if (a->a_locs) {
302 1.1 thorpej if (fprintf(fp, "const char * const %scf_locnames[] = { ",
303 1.1 thorpej name) < 0)
304 1.1 thorpej return (1);
305 1.1 thorpej for (nv = a->a_locs; nv; nv = nv->nv_next)
306 1.1 thorpej if (fprintf(fp, "\"%s\", ", nv->nv_name) < 0)
307 1.1 thorpej return (1);
308 1.1 thorpej if (fprintf(fp, "NULL};\n") < 0)
309 1.1 thorpej return (1);
310 1.1 thorpej }
311 1.1 thorpej return 0;
312 1.1 thorpej }
313 1.1 thorpej
314 1.1 thorpej static int
315 1.1 thorpej emitloc(FILE *fp)
316 1.1 thorpej {
317 1.1 thorpej int i;
318 1.1 thorpej
319 1.1 thorpej if (locators.used != 0) {
320 1.1 thorpej if (fprintf(fp, "\n/* locators */\n\
321 1.1 thorpej static int loc[%d] = {", locators.used) < 0)
322 1.1 thorpej return (1);
323 1.1 thorpej for (i = 0; i < locators.used; i++)
324 1.1 thorpej if (fprintf(fp, "%s%s,", SEP(i, 8),
325 1.1 thorpej locators.vec[i]) < 0)
326 1.1 thorpej return (1);
327 1.1 thorpej if (fprintf(fp, "\n};\n") < 0)
328 1.1 thorpej return (1);
329 1.1 thorpej } else if (*packed != NULL) {
330 1.1 thorpej /* We need to have *something*. */
331 1.1 thorpej if (fprintf(fp, "\n/* locators */\n\
332 1.1 thorpej static int loc[1] = { -1 };\n") < 0)
333 1.1 thorpej return (1);
334 1.1 thorpej }
335 1.1 thorpej
336 1.1 thorpej if (*packed != NULL)
337 1.1 thorpej if (fprintf(fp,
338 1.1 thorpej "\nconst char * const nullcf_locnames[] = {NULL};\n") < 0)
339 1.1 thorpej return (1);
340 1.1 thorpej
341 1.1 thorpej if (locators.used != 0)
342 1.1 thorpej return ht_enumerate(attrtab, cf_locnames_print, fp);
343 1.1 thorpej
344 1.1 thorpej return (0);
345 1.1 thorpej }
346 1.1 thorpej
347 1.1 thorpej /*
348 1.1 thorpej * Emit static parent data.
349 1.1 thorpej */
350 1.1 thorpej static int
351 1.1 thorpej emitparents(FILE *fp)
352 1.1 thorpej {
353 1.1 thorpej struct pspec *p;
354 1.1 thorpej
355 1.1 thorpej NEWLINE;
356 1.1 thorpej TAILQ_FOREACH(p, &allpspecs, p_list) {
357 1.1 thorpej if (p->p_devs == NULL)
358 1.1 thorpej continue;
359 1.1 thorpej if (fprintf(fp,
360 1.1 thorpej "static const struct cfparent pspec%d = {\n", p->p_inst) < 0)
361 1.1 thorpej return (1);
362 1.1 thorpej if (fprintf(fp, "\t\"%s\", ", p->p_iattr->a_name) < 0)
363 1.1 thorpej return (1);
364 1.1 thorpej if (p->p_atdev != NULL) {
365 1.1 thorpej if (fprintf(fp, "\"%s\", ", p->p_atdev->d_name) < 0)
366 1.1 thorpej return (1);
367 1.1 thorpej if (p->p_atunit == WILD &&
368 1.1 thorpej fprintf(fp, "DVUNIT_ANY") < 0)
369 1.1 thorpej return (1);
370 1.1 thorpej else if (p->p_atunit != WILD &&
371 1.1 thorpej fprintf(fp, "%d", p->p_atunit) < 0)
372 1.1 thorpej return (1);
373 1.1 thorpej } else if (fprintf(fp, "NULL, 0") < 0)
374 1.1 thorpej return (1);
375 1.1 thorpej if (fprintf(fp, "\n};\n") < 0)
376 1.1 thorpej return (1);
377 1.1 thorpej }
378 1.1 thorpej
379 1.1 thorpej return (0);
380 1.1 thorpej }
381 1.1 thorpej
382 1.1 thorpej /*
383 1.1 thorpej * Emit the cfdata array.
384 1.1 thorpej */
385 1.1 thorpej static int
386 1.1 thorpej emitcfdata(FILE *fp)
387 1.1 thorpej {
388 1.1 thorpej struct devi **p, *i;
389 1.1 thorpej struct pspec *ps;
390 1.1 thorpej int unit, v;
391 1.1 thorpej const char *state, *basename, *attachment;
392 1.1 thorpej struct nvlist *nv;
393 1.1 thorpej struct attr *a;
394 1.1 thorpej char *loc;
395 1.1 thorpej char locbuf[20];
396 1.1 thorpej const char *lastname = "";
397 1.1 thorpej
398 1.1 thorpej if (fprintf(fp, "\n\
399 1.1 thorpej #define NORM FSTATE_NOTFOUND\n\
400 1.1 thorpej #define STAR FSTATE_STAR\n\
401 1.1 thorpej \n\
402 1.1 thorpej struct cfdata cfdata[] = {\n\
403 1.1 thorpej /* driver attachment unit state loc flags pspec\n\
404 1.1 thorpej locnames */\n") < 0)
405 1.1 thorpej return (1);
406 1.1 thorpej for (p = packed; (i = *p) != NULL; p++) {
407 1.1 thorpej /* the description */
408 1.1 thorpej if (fprintf(fp, "/*%3d: %s at ", i->i_cfindex, i->i_name) < 0)
409 1.1 thorpej return (1);
410 1.1 thorpej if ((ps = i->i_pspec) != NULL) {
411 1.1 thorpej if (ps->p_atdev != NULL &&
412 1.1 thorpej ps->p_atunit != WILD) {
413 1.1 thorpej if (fprintf(fp, "%s%d", ps->p_atdev->d_name,
414 1.1 thorpej ps->p_atunit) < 0)
415 1.1 thorpej return (1);
416 1.1 thorpej } else if (ps->p_atdev != NULL) {
417 1.1 thorpej if (fprintf(fp, "%s?", ps->p_atdev->d_name) < 0)
418 1.1 thorpej return (1);
419 1.1 thorpej } else {
420 1.1 thorpej if (fprintf(fp, "%s?", ps->p_iattr->a_name) < 0)
421 1.1 thorpej return (1);
422 1.1 thorpej }
423 1.1 thorpej
424 1.1 thorpej a = ps->p_iattr;
425 1.1 thorpej for (nv = a->a_locs, v = 0; nv != NULL;
426 1.1 thorpej nv = nv->nv_next, v++) {
427 1.1 thorpej if (ARRNAME(nv->nv_name, lastname)) {
428 1.1 thorpej if (fprintf(fp, " %s %s",
429 1.1 thorpej nv->nv_name, i->i_locs[v]) < 0)
430 1.1 thorpej return (1);
431 1.1 thorpej } else {
432 1.1 thorpej if (fprintf(fp, " %s %s",
433 1.1 thorpej nv->nv_name,
434 1.1 thorpej i->i_locs[v]) < 0)
435 1.1 thorpej return (1);
436 1.1 thorpej lastname = nv->nv_name;
437 1.1 thorpej }
438 1.1 thorpej }
439 1.1 thorpej } else {
440 1.1 thorpej a = NULL;
441 1.1 thorpej if (fputs("root", fp) < 0)
442 1.1 thorpej return (1);
443 1.1 thorpej }
444 1.1 thorpej
445 1.1 thorpej if (fputs(" */\n", fp) < 0)
446 1.1 thorpej return (-1);
447 1.1 thorpej
448 1.1 thorpej /* then the actual defining line */
449 1.1 thorpej basename = i->i_base->d_name;
450 1.1 thorpej attachment = i->i_atdeva->d_name;
451 1.1 thorpej if (i->i_unit == STAR) {
452 1.1 thorpej unit = i->i_base->d_umax;
453 1.1 thorpej state = "STAR";
454 1.1 thorpej } else {
455 1.1 thorpej unit = i->i_unit;
456 1.1 thorpej state = "NORM";
457 1.1 thorpej }
458 1.1 thorpej if (i->i_locoff >= 0) {
459 1.1 thorpej (void)snprintf(locbuf, sizeof(locbuf), "loc+%3d",
460 1.1 thorpej i->i_locoff);
461 1.1 thorpej loc = locbuf;
462 1.1 thorpej } else
463 1.1 thorpej loc = "loc";
464 1.1 thorpej if (fprintf(fp, " {\"%s\",%s\"%s\",%s%2d, %s, %7s, %#6x, ",
465 1.1 thorpej basename, strlen(basename) < 8 ? "\t\t"
466 1.1 thorpej : "\t",
467 1.1 thorpej attachment, strlen(attachment) < 5 ? "\t\t"
468 1.1 thorpej : "\t",
469 1.1 thorpej unit, state, loc, i->i_cfflags) < 0)
470 1.1 thorpej return (1);
471 1.1 thorpej if (ps != NULL) {
472 1.1 thorpej if (fprintf(fp, "&pspec%d,\n", ps->p_inst) < 0)
473 1.1 thorpej return (1);
474 1.1 thorpej } else if (fputs("NULL,\n", fp) < 0)
475 1.1 thorpej return (1);
476 1.1 thorpej if (fprintf(fp, " %scf_locnames},\n",
477 1.1 thorpej (a != NULL && a->a_locs != NULL) ? a->a_name
478 1.1 thorpej : "null") < 0)
479 1.1 thorpej return (1);
480 1.1 thorpej }
481 1.1 thorpej return (fputs(" {0}\n};\n", fp) < 0);
482 1.1 thorpej }
483 1.1 thorpej
484 1.1 thorpej /*
485 1.1 thorpej * Emit the table of potential roots.
486 1.1 thorpej */
487 1.1 thorpej static int
488 1.1 thorpej emitroots(FILE *fp)
489 1.1 thorpej {
490 1.1 thorpej struct devi **p, *i;
491 1.1 thorpej
492 1.1 thorpej if (fputs("\nconst short cfroots[] = {\n", fp) < 0)
493 1.1 thorpej return (1);
494 1.1 thorpej for (p = packed; (i = *p) != NULL; p++) {
495 1.1 thorpej if (i->i_at != NULL)
496 1.1 thorpej continue;
497 1.1 thorpej if (i->i_unit != 0 &&
498 1.1 thorpej (i->i_unit != STAR || i->i_base->d_umax != 0))
499 1.1 thorpej (void)fprintf(stderr,
500 1.1 thorpej "config: warning: `%s at root' is not unit 0\n",
501 1.1 thorpej i->i_name);
502 1.1 thorpej if (fprintf(fp, "\t%2d /* %s */,\n",
503 1.1 thorpej i->i_cfindex, i->i_name) < 0)
504 1.1 thorpej return (1);
505 1.1 thorpej }
506 1.1 thorpej return (fputs("\t-1\n};\n", fp) < 0);
507 1.1 thorpej }
508 1.1 thorpej
509 1.1 thorpej /*
510 1.1 thorpej * Emit pseudo-device initialization.
511 1.1 thorpej */
512 1.1 thorpej static int
513 1.1 thorpej emitpseudo(FILE *fp)
514 1.1 thorpej {
515 1.1 thorpej struct devi *i;
516 1.1 thorpej struct devbase *d;
517 1.1 thorpej
518 1.1 thorpej if (fputs("\n/* pseudo-devices */\n", fp) < 0)
519 1.1 thorpej return (1);
520 1.1 thorpej TAILQ_FOREACH(i, &allpseudo, i_next) {
521 1.1 thorpej if (fprintf(fp, "void %sattach(int);\n",
522 1.1 thorpej i->i_base->d_name) < 0)
523 1.1 thorpej return (1);
524 1.1 thorpej }
525 1.1 thorpej if (fputs("\nstruct pdevinit pdevinit[] = {\n", fp) < 0)
526 1.1 thorpej return (1);
527 1.1 thorpej TAILQ_FOREACH(i, &allpseudo, i_next) {
528 1.1 thorpej d = i->i_base;
529 1.1 thorpej if (fprintf(fp, "\t{ %sattach, %d },\n",
530 1.1 thorpej d->d_name, d->d_umax) < 0)
531 1.1 thorpej return (1);
532 1.1 thorpej }
533 1.1 thorpej return (fputs("\t{ 0, 0 }\n};\n", fp) < 0);
534 1.1 thorpej }
535 1.1 thorpej
536 1.1 thorpej /*
537 1.1 thorpej * Emit name to major block number table.
538 1.1 thorpej */
539 1.1 thorpej int
540 1.1 thorpej emitname2blk(FILE *fp)
541 1.1 thorpej {
542 1.1 thorpej struct devbase *dev;
543 1.1 thorpej
544 1.1 thorpej if (fputs("\n/* device name to major block number */\n", fp) < 0)
545 1.1 thorpej return (1);
546 1.1 thorpej
547 1.1 thorpej if (fprintf(fp, "struct devnametobdevmaj dev_name2blk[] = {\n") < 0)
548 1.1 thorpej return (1);
549 1.1 thorpej
550 1.1 thorpej TAILQ_FOREACH(dev, &allbases, d_next) {
551 1.1 thorpej if (dev->d_major == NODEV)
552 1.1 thorpej continue;
553 1.1 thorpej
554 1.1 thorpej if (fprintf(fp, "\t{ \"%s\", %d },\n",
555 1.1 thorpej dev->d_name, dev->d_major) < 0)
556 1.1 thorpej return (1);
557 1.1 thorpej }
558 1.1 thorpej if (fprintf(fp, "\t{ NULL, 0 }\n};\n") < 0)
559 1.1 thorpej return (1);
560 1.1 thorpej
561 1.1 thorpej return (0);
562 1.1 thorpej }
563