mkdevsw.c revision 1.11
1/*	$NetBSD: mkdevsw.c,v 1.11 2014/10/29 17:14:50 christos Exp $	*/
2
3/*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by MAEKAWA Masahide (gehenna@NetBSD.org).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_NBTOOL_CONFIG_H
33#include "nbtool_config.h"
34#endif
35
36#include <sys/cdefs.h>
37__RCSID("$NetBSD: mkdevsw.c,v 1.11 2014/10/29 17:14:50 christos Exp $");
38
39#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42#include <err.h>
43
44#include "defs.h"
45
46static void emitconv(FILE *);
47static void emitdev(FILE *);
48static void emitdevm(FILE *);
49static void emitheader(FILE *);
50
51int
52mkdevsw(void)
53{
54	FILE *fp;
55
56	if ((fp = fopen("devsw.c.tmp", "w")) == NULL) {
57		warn("cannot create devsw.c");
58		return (1);
59	}
60
61	emitheader(fp);
62	emitdevm(fp);
63	emitconv(fp);
64	emitdev(fp);
65
66	fflush(fp);
67	if (ferror(fp)) {
68		warn("error writing devsw.c");
69		fclose(fp);
70		return 1;
71	}
72
73	(void)fclose(fp);
74
75	if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
76		warn("error renaming devsw.c");
77		return (1);
78	}
79
80	return (0);
81}
82
83static void
84emitheader(FILE *fp)
85{
86	autogen_comment(fp, "devsw.c");
87
88	fputs("#include <sys/param.h>\n"
89		  "#include <sys/conf.h>\n", fp);
90}
91
92/*
93 * Emit device switch table for character/block device.
94 */
95static void
96emitdevm(FILE *fp)
97{
98	struct devm *dm;
99	char mstr[16];
100	devmajor_t i;
101
102	fputs("\n/* device switch table for block device */\n", fp);
103
104	for (i = 0 ; i <= maxbdevm ; i++) {
105		(void)snprintf(mstr, sizeof(mstr), "%d", i);
106		if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL)
107			continue;
108
109		fprintf(fp, "extern const struct bdevsw %s_bdevsw;\n",
110			    dm->dm_name);
111	}
112
113	fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);
114
115	for (i = 0 ; i <= maxbdevm ; i++) {
116		(void)snprintf(mstr, sizeof(mstr), "%d", i);
117		if ((dm = ht_lookup(bdevmtab, intern(mstr))) == NULL) {
118			fprintf(fp, "\tNULL,\n");
119		} else {
120			fprintf(fp, "\t&%s_bdevsw,\n", dm->dm_name);
121		}
122	}
123
124	fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp);
125
126	fputs("const int sys_bdevsws = __arraycount(bdevsw0);\n"
127		  "int max_bdevsws = __arraycount(bdevsw0);\n", fp);
128
129	fputs("\n/* device switch table for character device */\n", fp);
130
131	for (i = 0 ; i <= maxcdevm ; i++) {
132		(void)snprintf(mstr, sizeof(mstr), "%d", i);
133		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
134			continue;
135
136		fprintf(fp, "extern const struct cdevsw %s_cdevsw;\n",
137			    dm->dm_name);
138	}
139
140	fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp);
141
142	for (i = 0 ; i <= maxcdevm ; i++) {
143		(void)snprintf(mstr, sizeof(mstr), "%d", i);
144		if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL) {
145			fprintf(fp, "\tNULL,\n");
146		} else {
147			fprintf(fp, "\t&%s_cdevsw,\n", dm->dm_name);
148		}
149	}
150
151	fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp);
152
153	fputs("const int sys_cdevsws = __arraycount(cdevsw0);\n"
154		  "int max_cdevsws = __arraycount(cdevsw0);\n", fp);
155}
156
157/*
158 * Emit device major conversion table.
159 */
160static void
161emitconv(FILE *fp)
162{
163	struct devm *dm;
164
165	fputs("\n/* device conversion table */\n"
166		  "struct devsw_conv devsw_conv0[] = {\n", fp);
167	TAILQ_FOREACH(dm, &alldevms, dm_next) {
168		if (version < 20100430) {
169			/* Emit compatible structure */
170			fprintf(fp, "\t{ \"%s\", %d, %d },\n", dm->dm_name,
171			    dm->dm_bmajor, dm->dm_cmajor);
172			continue;
173		}
174		struct nvlist *nv;
175		const char *d_class, *d_flags = "0";
176		int d_vec[2] = { 0, 0 };
177		int i = 0;
178
179		/*
180		 * "parse" info.  currently the rules are simple:
181		 *  1) first entry defines class
182		 *  2) next ones without n_str are d_vectdim
183		 *  3) next one with n_str is d_flags
184		 *  4) EOL
185		 */
186		nv = dm->dm_devnodes;
187		d_class = nv->nv_str;
188		while ((nv = nv->nv_next) != NULL) {
189			if (i > 2)
190				panic("invalid devnode definition");
191			if (nv->nv_str) {
192				d_flags = nv->nv_str;
193				break;
194			}
195			if (nv->nv_num > INT_MAX || nv->nv_num < INT_MIN)
196				panic("out of range devnode definition");
197			d_vec[i++] = (int)nv->nv_num;
198		}
199
200		fprintf(fp, "\t{ \"%s\", %d, %d, %s, %s, { %d, %d }},\n",
201			    dm->dm_name, dm->dm_bmajor, dm->dm_cmajor,
202			    d_class, d_flags, d_vec[0], d_vec[1]);
203
204	}
205	fputs("};\n\n"
206		  "struct devsw_conv *devsw_conv = devsw_conv0;\n"
207		  "int max_devsw_convs = __arraycount(devsw_conv0);\n",
208		  fp);
209}
210
211/*
212 * Emit specific device major informations.
213 */
214static void
215emitdev(FILE *fp)
216{
217	struct devm *dm;
218	char mstr[16];
219
220	fputs("\n", fp);
221
222	(void)strlcpy(mstr, "swap", sizeof(mstr));
223	if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) {
224		fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n",
225			    dm->dm_bmajor);
226	}
227
228	(void)strlcpy(mstr, "mem", sizeof(mstr));
229	if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
230		panic("memory device is not configured");
231	fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n",
232		    dm->dm_cmajor);
233
234	fputs("\n/* mem_no is only used in iskmemdev() */\n", fp);
235	fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor);
236}
237