Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / config / mkioconf.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)mkioconf.c  8.2 (Berkeley) 1/21/94";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.sbin/config/mkioconf.c,v 1.62 2000/01/29 18:14:59 peter Exp $";
40 #endif /* not lint */
41
42 #include <err.h>
43 #include <stdio.h>
44 #include "y.tab.h"
45 #include "config.h"
46
47 /*
48  * build the ioconf.c file
49  */
50
51 static char *
52 devstr(struct device *dp)
53 {
54     static char buf[100];
55
56     if (dp->d_unit >= 0) {
57         snprintf(buf, sizeof(buf), "%s%d", dp->d_name, dp->d_unit);
58         return buf;
59     } else
60         return dp->d_name;
61 }
62
63 static void
64 write_device_resources(FILE *fp, struct device *dp)
65 {
66     int count = 0;
67     char buf[80];
68
69     fprintf(fp, "struct config_resource %s_resources[] = {\n", devstr(dp));
70     if (dp->d_conn) {
71         if (dp->d_connunit >= 0)
72             snprintf(buf, sizeof(buf), "%s%d", dp->d_conn, dp->d_connunit);
73         else
74             snprintf(buf, sizeof(buf), "%s", dp->d_conn);
75         fprintf(fp, "\t{ \"at\",\tRES_STRING,\t{ (long)\"%s\" }},\n", buf);
76         count++;
77     }
78     if (dp->d_drive != -2) {
79         fprintf(fp, "\t{ \"drive\",\tRES_INT,\t{ %d }},\n", dp->d_drive);
80         count++;
81     }
82     if (dp->d_target != -2) {
83         fprintf(fp, "\t{ \"target\",\tRES_INT,\t{ %d }},\n", dp->d_target);
84         count++;
85     }
86     if (dp->d_lun != -2) {
87         fprintf(fp, "\t{ \"lun\",\tRES_INT,\t{ %d }},\n", dp->d_lun);
88         count++;
89     }
90     if (dp->d_bus != -2) {
91         fprintf(fp, "\t{ \"bus\",\tRES_INT,\t{ %d }},\n", dp->d_bus);
92         count++;
93     }
94     if (dp->d_flags) {
95         fprintf(fp, "\t{ \"flags\",\tRES_INT,\t{ 0x%x }},\n", dp->d_flags);
96         count++;
97     }
98     if (dp->d_disabled) {
99         fprintf(fp, "\t{ \"disabled\",\tRES_INT,\t{ %d }},\n", dp->d_disabled);
100         count++;
101     }
102     if (dp->d_port) {
103         fprintf(fp, "\t{ \"port\",\tRES_INT,\t { %s }},\n", dp->d_port);
104         count++;
105     }
106     if (dp->d_portn > 0) {
107         fprintf(fp, "\t{ \"port\",\tRES_INT,\t{ 0x%x }},\n", dp->d_portn);
108         count++;
109     }
110     if (dp->d_maddr > 0) {
111         fprintf(fp, "\t{ \"maddr\",\tRES_INT,\t{ 0x%x }},\n", dp->d_maddr);
112         count++;
113     }
114     if (dp->d_msize > 0) {
115         fprintf(fp, "\t{ \"msize\",\tRES_INT,\t{ 0x%x }},\n", dp->d_msize);
116         count++;
117     }
118     if (dp->d_drq >= 0) {
119         fprintf(fp, "\t{ \"drq\",\tRES_INT,\t{ %d }},\n", dp->d_drq);
120         count++;
121     }
122     if (dp->d_irq > 0) {
123         fprintf(fp, "\t{ \"irq\",\tRES_INT,\t{ %d }},\n", dp->d_irq);
124         count++;
125     }
126     fprintf(fp, "};\n");
127     fprintf(fp, "#define %s_count %d\n", devstr(dp), count);
128 }
129
130 static void
131 write_all_device_resources(FILE *fp)
132 {
133         struct device *dp;
134
135         for (dp = dtab; dp != 0; dp = dp->d_next) {
136                 if (dp->d_type != DEVICE)
137                         continue;
138                 if (dp->d_unit == UNKNOWN)
139                         continue;
140                 write_device_resources(fp, dp);
141         }
142 }
143
144 static void
145 write_devtab(FILE *fp)
146 {
147         struct device *dp;
148         int count;
149
150         write_all_device_resources(fp);
151
152         count = 0;
153         fprintf(fp, "struct config_device config_devtab[] = {\n");
154         for (dp = dtab; dp != 0; dp = dp->d_next) {
155                 char* n = devstr(dp);
156                 if (dp->d_type != DEVICE)
157                         continue;
158                 if (dp->d_unit == UNKNOWN)
159                         continue;
160                 fprintf(fp, "\t{ \"%s\",\t%d,\t%s_count,\t%s_resources },\n",
161                         dp->d_name, dp->d_unit, n, n);
162                 count++;
163         }
164         fprintf(fp, "};\n");
165         fprintf(fp, "int devtab_count = %d;\n", count);
166 }
167
168 void
169 newbus_ioconf()
170 {
171         FILE *fp;
172
173         fp = fopen(path("ioconf.c.new"), "w");
174         if (fp == 0)
175                 err(1, "%s", path("ioconf.c.new"));
176         fprintf(fp, "/*\n");
177         fprintf(fp, " * I/O configuration.\n");
178         fprintf(fp, " * DO NOT EDIT-- this file is automatically generated.\n");
179         fprintf(fp, " */\n");
180         fprintf(fp, "\n");
181         fprintf(fp, "#include <sys/param.h>\n");
182         fprintf(fp, "\n");
183         fprintf(fp, "/*\n");
184         fprintf(fp, " * New bus architecture devices.\n");
185         fprintf(fp, " */\n");
186         fprintf(fp, "\n");
187         fprintf(fp, "#include <sys/queue.h>\n");
188         fprintf(fp, "#include <sys/sysctl.h>\n");
189         if (machine == MACHINE_PC98)
190                 fprintf(fp, "#include <pc98/pc98/pc98.h>\n");
191         else
192                 fprintf(fp, "#include <isa/isareg.h>\n");
193         fprintf(fp, "#include <sys/bus_private.h>\n");
194         fprintf(fp, "\n");
195
196         write_devtab(fp);
197
198         (void) fclose(fp);
199         moveifchanged(path("ioconf.c.new"), path("ioconf.c"));
200 }