mkdevsw.c revision 1.13
1/*	$NetBSD: mkdevsw.c,v 1.13 2015/09/02 05:09:25 uebayasi 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.13 2015/09/02 05:09:25 uebayasi 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	fchdir(buildconfdirfd);
57	if ((fp = fopen("devsw.c.tmp", "w")) == NULL) {
58		warn("cannot create devsw.c");
59		goto err;
60	}
61
62	emitheader(fp);
63	emitdevm(fp);
64	emitconv(fp);
65	emitdev(fp);
66
67	fflush(fp);
68	if (ferror(fp)) {
69		warn("error writing devsw.c");
70		fclose(fp);
71		goto err;
72	}
73
74	(void)fclose(fp);
75
76	if (moveifchanged("devsw.c.tmp", "devsw.c") != 0) {
77		warn("error renaming devsw.c");
78		goto err;
79	}
80	fchdir(builddirfd);
81	return (0);
82
83err:
84	fchdir(builddirfd);
85	return (1);
86}
87
88static void
89emitheader(FILE *fp)
90{
91	autogen_comment(fp, "devsw.c");
92
93	fputs("#include <sys/param.h>\n"
94		  "#include <sys/conf.h>\n", fp);
95}
96
97static void
98dentry(FILE *fp, struct hashtab *t, devmajor_t i, char p)
99{
100	const struct devm *dm;
101	char mstr[16];
102
103	(void)snprintf(mstr, sizeof(mstr), "%d", i);
104	if ((dm = ht_lookup(t, intern(mstr))) == NULL)
105		return;
106
107	fprintf(fp, "extern const struct %cdevsw %s_%cdevsw;\n",
108	    p, dm->dm_name, p);
109}
110
111static void
112pentry(FILE *fp, struct hashtab *t, devmajor_t i, char p)
113{
114	const struct devm *dm;
115	char mstr[16];
116
117	(void)snprintf(mstr, sizeof(mstr), "%d", i);
118	dm = ht_lookup(t, intern(mstr));
119
120	if (dm)
121		fprintf(fp, "\t&%s_%cdevsw", dm->dm_name, p);
122	else
123		fputs("\tNULL", fp);
124
125	fprintf(fp, ",\t// %3d\n", i);
126}
127
128/*
129 * Emit device switch table for character/block device.
130 */
131static void
132emitdevm(FILE *fp)
133{
134	devmajor_t i;
135
136	fputs("\n/* device switch table for block device */\n", fp);
137
138	for (i = 0; i <= maxbdevm ; i++)
139		dentry(fp, cdevmtab, i, 'b');
140
141	fputs("\nconst struct bdevsw *bdevsw0[] = {\n", fp);
142
143	for (i = 0; i <= maxbdevm; i++)
144		pentry(fp, bdevmtab, i, 'b');
145
146	fputs("};\n\nconst struct bdevsw **bdevsw = bdevsw0;\n", fp);
147
148	fputs("const int sys_bdevsws = __arraycount(bdevsw0);\n"
149		  "int max_bdevsws = __arraycount(bdevsw0);\n", fp);
150
151	fputs("\n/* device switch table for character device */\n", fp);
152
153	for (i = 0; i <= maxcdevm; i++)
154		dentry(fp, cdevmtab, i, 'c');
155
156	fputs("\nconst struct cdevsw *cdevsw0[] = {\n", fp);
157
158	for (i = 0; i <= maxcdevm; i++)
159		pentry(fp, cdevmtab, i, 'c');
160
161	fputs("};\n\nconst struct cdevsw **cdevsw = cdevsw0;\n", fp);
162
163	fputs("const int sys_cdevsws = __arraycount(cdevsw0);\n"
164		  "int max_cdevsws = __arraycount(cdevsw0);\n", fp);
165}
166
167/*
168 * Emit device major conversion table.
169 */
170static void
171emitconv(FILE *fp)
172{
173	struct devm *dm;
174
175	fputs("\n/* device conversion table */\n"
176		  "struct devsw_conv devsw_conv0[] = {\n", fp);
177	TAILQ_FOREACH(dm, &alldevms, dm_next) {
178		if (version < 20100430) {
179			/* Emit compatible structure */
180			fprintf(fp, "\t{ \"%s\", %d, %d },\n", dm->dm_name,
181			    dm->dm_bmajor, dm->dm_cmajor);
182			continue;
183		}
184		struct nvlist *nv;
185		const char *d_class, *d_flags = "0";
186		int d_vec[2] = { 0, 0 };
187		int i = 0;
188
189		/*
190		 * "parse" info.  currently the rules are simple:
191		 *  1) first entry defines class
192		 *  2) next ones without n_str are d_vectdim
193		 *  3) next one with n_str is d_flags
194		 *  4) EOL
195		 */
196		nv = dm->dm_devnodes;
197		d_class = nv->nv_str;
198		while ((nv = nv->nv_next) != NULL) {
199			if (i > 2)
200				panic("invalid devnode definition");
201			if (nv->nv_str) {
202				d_flags = nv->nv_str;
203				break;
204			}
205			if (nv->nv_num > INT_MAX || nv->nv_num < INT_MIN)
206				panic("out of range devnode definition");
207			d_vec[i++] = (int)nv->nv_num;
208		}
209
210		fprintf(fp, "\t{ \"%s\", %d, %d, %s, %s, { %d, %d }},\n",
211			    dm->dm_name, dm->dm_bmajor, dm->dm_cmajor,
212			    d_class, d_flags, d_vec[0], d_vec[1]);
213
214	}
215	fputs("};\n\n"
216		  "struct devsw_conv *devsw_conv = devsw_conv0;\n"
217		  "int max_devsw_convs = __arraycount(devsw_conv0);\n",
218		  fp);
219}
220
221/*
222 * Emit specific device major informations.
223 */
224static void
225emitdev(FILE *fp)
226{
227	struct devm *dm;
228	char mstr[16];
229
230	fputs("\n", fp);
231
232	(void)strlcpy(mstr, "swap", sizeof(mstr));
233	if ((dm = ht_lookup(bdevmtab, intern(mstr))) != NULL) {
234		fprintf(fp, "const dev_t swapdev = makedev(%d, 0);\n",
235			    dm->dm_bmajor);
236	}
237
238	(void)strlcpy(mstr, "mem", sizeof(mstr));
239	if ((dm = ht_lookup(cdevmtab, intern(mstr))) == NULL)
240		panic("memory device is not configured");
241	fprintf(fp, "const dev_t zerodev = makedev(%d, DEV_ZERO);\n",
242		    dm->dm_cmajor);
243
244	fputs("\n/* mem_no is only used in iskmemdev() */\n", fp);
245	fprintf(fp, "const int mem_no = %d;\n", dm->dm_cmajor);
246}
247