xref: /openbsd/usr.sbin/config/config.h (revision 1e13aedb)
1 /*	$OpenBSD: config.h,v 1.32 2021/11/28 19:26:03 deraadt Exp $	*/
2 /*	$NetBSD: config.h,v 1.30 1997/02/02 21:12:30 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * All advertising materials mentioning features or use of this software
13  * must display the following acknowledgement:
14  *	This product includes software developed by the University of
15  *	California, Lawrence Berkeley Laboratories.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)config.h	8.1 (Berkeley) 6/6/93
42  */
43 
44 /*
45  * config.h:  Global definitions for "config"
46  */
47 
48 #include <sys/types.h>
49 
50 #include <paths.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 
54 /* These are really for MAKE_BOOTSTRAP but harmless. */
55 #ifndef __dead
56 #define __dead
57 #endif
58 #ifndef _PATH_DEVNULL
59 #define _PATH_DEVNULL "/dev/null"
60 #endif
61 
62 
63 /*
64  * Name/value lists.  Values can be strings or pointers and/or can carry
65  * integers.  The names can be NULL, resulting in simple value lists.
66  */
67 struct nvlist {
68 	struct	nvlist *nv_next;
69 	const char *nv_name;
70 	union {
71 		const char *un_str;
72 		void *un_ptr;
73 	} nv_un;
74 #define	nv_str	nv_un.un_str
75 #define	nv_ptr	nv_un.un_ptr
76 	int	nv_int;
77 };
78 
79 /*
80  * Kernel configurations.
81  */
82 struct config {
83 	struct	config *cf_next;	/* linked list */
84 	const char *cf_name;		/* "vmunix" */
85 	int	cf_lineno;		/* source line */
86 	struct	nvlist *cf_root;	/* "root on ra0a" */
87 	struct	nvlist *cf_swap;	/* "swap on ra0b and ra1b" */
88 	struct	nvlist *cf_dump;	/* "dumps on ra0b" */
89 };
90 
91 /*
92  * Attributes.  These come in two flavors: "plain" and "interface".
93  * Plain attributes (e.g., "ether") simply serve to pull in files.
94  * Interface attributes (e.g., "scsi") carry three lists: locators,
95  * child devices, and references.  The locators are those things
96  * that must be specified in order to configure a device instance
97  * using this attribute (e.g., "tg0 at scsi0").  The a_devs field
98  * lists child devices that can connect here (e.g., "tg"s), while
99  * the a_refs are parents that carry the attribute (e.g., actual
100  * SCSI host adapter drivers such as the SPARC "esp").
101  */
102 struct attr {
103 	const char *a_name;		/* name of this attribute */
104 	int	a_iattr;		/* true => allows children */
105 	struct	nvlist *a_locs;		/* locators required */
106 	int	a_loclen;		/* length of above list */
107 	struct	nvlist *a_devs;		/* children */
108 	struct	nvlist *a_refs;		/* parents */
109 };
110 
111 /*
112  * The "base" part (struct devbase) of a device ("uba", "sd"; but not
113  * "uba2" or "sd0").  It may be found "at" one or more attributes,
114  * including "at root" (this is represented by a NULL attribute), as
115  * specified by the device attachments (struct deva).
116  *
117  * Each device may also export attributes.  If any provide an output
118  * interface (e.g., "esp" provides "scsi"), other devices (e.g.,
119  * "tg"s) can be found at instances of this one (e.g., "esp"s).
120  * Such a connection must provide locators as specified by that
121  * interface attribute (e.g., "target").  The base device can
122  * export both output (aka `interface') attributes, as well as
123  * import input (`plain') attributes.  Device attachments may
124  * only import input attributes; it makes no sense to have a
125  * specific attachment export a new interface to other devices.
126  *
127  * Each base carries a list of instances (via d_ihead).  Note that this
128  * list "skips over" aliases; those must be found through the instances
129  * themselves.  Each base also carries a list of possible attachments,
130  * each of which specify a set of devices that the device can attach
131  * to, as well as the device instances that are actually using that
132  * attachment.
133  */
134 struct devbase {
135 	const char *d_name;		/* e.g., "sd" */
136 	struct	devbase *d_next;	/* linked list */
137 	int	d_isdef;		/* set once properly defined */
138 	int	d_ispseudo;		/* is a pseudo-device */
139 	int	d_major;		/* used for "root on sd0", e.g. */
140 	struct	nvlist *d_attrs;	/* attributes, if any */
141 	int	d_umax;			/* highest unit number + 1 */
142 	struct	devi *d_ihead;		/* first instance, if any */
143 	struct	devi **d_ipp;		/* used for tacking on more instances */
144 	struct	deva *d_ahead;		/* first attachment, if any */
145 	struct	deva **d_app;		/* used for tacking on attachments */
146 };
147 
148 struct deva {
149 	const char *d_name;		/* name of attachment, e.g. "com_isa" */
150 	struct	deva *d_next;		/* linked list */
151 	struct	deva *d_bsame;		/* list on same base */
152 	int	d_isdef;		/* set once properly defined */
153 	struct	devbase *d_devbase;	/* the base device */
154 	struct	nvlist *d_atlist;	/* e.g., "at tg" (attr list) */
155 	struct	nvlist *d_attrs;	/* attributes, if any */
156 	struct	devi *d_ihead;		/* first instance, if any */
157 	struct	devi **d_ipp;		/* used for tacking on more instances */
158 };
159 
160 /*
161  * An "instance" of a device.  The same instance may be listed more
162  * than once, e.g., "xx0 at isa? port FOO" + "xx0 at isa? port BAR".
163  *
164  * After everything has been read in and verified, the devi's are
165  * "packed" to collect all the information needed to generate ioconf.c.
166  * In particular, we try to collapse multiple aliases into a single entry.
167  * We then assign each "primary" (non-collapsed) instance a cfdata index.
168  * Note that there may still be aliases among these.
169  */
170 struct devi {
171 	/* created while parsing config file */
172 	const char *i_name;	/* e.g., "sd0" */
173 	int	i_unit;		/* unit from name, e.g., 0 */
174 	int	i_disable;	/* device is disabled */
175 	struct	devbase *i_base;/* e.g., pointer to "sd" base */
176 	struct	devi *i_next;	/* list of all instances */
177 	struct	devi *i_bsame;	/* list on same base */
178 	struct	devi *i_asame;	/* list on same base attachment */
179 	struct	devi *i_alias;	/* other aliases of this instance */
180 	const char *i_at;	/* where this is "at" (NULL if at root) */
181 	struct	attr *i_atattr;	/* attr that allowed attach */
182 	struct	devbase *i_atdev;/* if "at <devname><unit>", else NULL */
183 	struct	deva *i_atdeva;
184 	const char **i_locs;	/* locators (as given by i_atattr) */
185 	int	i_atunit;	/* unit from "at" */
186 	int	i_cfflags;	/* flags from config line */
187 	int	i_lineno;	/* line # in config, for later errors */
188 
189 	/* created during packing or ioconf.c generation */
190 /*		i_loclen	   via i_atattr->a_loclen */
191 	short	i_collapsed;	/* set => this alias no longer needed */
192 	short	i_cfindex;	/* our index in cfdata */
193 	short	i_pvlen;	/* number of parents */
194 	short	i_pvoff;	/* offset in parents.vec */
195 	short	i_locoff;	/* offset in locators.vec */
196 	struct	devi **i_parents;/* the parents themselves */
197 	int	i_locnami;	/* my index into locnami[] */
198 	int	i_plocnami;	/* parent's locnami[] index */
199 };
200 /* special units */
201 #define	STAR	(-1)		/* unit number for, e.g., "sd*" */
202 #define	WILD	(-2)		/* unit number for, e.g., "sd?" */
203 
204 /*
205  * Files.  Each file is either standard (always included) or optional,
206  * depending on whether it has names on which to *be* optional.  The
207  * options field (fi_optx) is actually an expression tree, with nodes
208  * for OR, AND, and NOT, as well as atoms (words) representing some
209  * particular option.  The node type is stored in the nv_int field.
210  * Subexpressions appear in the `next' field; for the binary operators
211  * AND and OR, the left subexpression is first stored in the nv_ptr field.
212  *
213  * For any file marked as needs-count or needs-flag, fixfiles() will
214  * build fi_optf, a `flat list' of the options with nv_int fields that
215  * contain counts or `need' flags; this is used in mkheaders().
216  */
217 struct files {
218 	struct	files *fi_next;	/* linked list */
219 	const char *fi_srcfile;	/* the name of the "files" file that got us */
220 	u_short	fi_srcline;	/* and the line number */
221 	u_char	fi_flags;	/* as below */
222 	char	fi_lastc;	/* last char from path */
223 	struct nvlist *fi_nvpath; /* list of paths */
224 	const char *fi_base;	/* tail minus ".c" (or whatever) */
225 	struct  nvlist *fi_optx;/* options expression */
226 	struct  nvlist *fi_optf;/* flattened version of above, if needed */
227 	const char *fi_mkrule;/* special make rules, if any */
228 };
229 
230 /*
231  * Objects and libraries.  This allows precompiled object and library
232  * files (e.g. binary-only device drivers) to be linked in.
233  */
234 struct objects {
235 	struct  objects *oi_next;/* linked list */
236 	const char *oi_srcfile; /* the name of the "objects" file that got us */
237 	u_short oi_srcline;	/* and the line number */
238 	u_char  oi_flags;	/* as below */
239 	char    oi_lastc;	/* last char from path */
240 	const char *oi_path;    /* full object path */
241 	struct  nvlist *oi_optx;/* options expression */
242 	struct  nvlist *oi_optf;/* flattened version of above, if needed */
243 };
244 
245 #define	OI_SEL		0x01	/* selected */
246 #define	OI_NEEDSFLAG	0x02	/* needs-flag */
247 
248 #define	FX_ATOM		0	/* atom (in nv_name) */
249 #define	FX_NOT		1	/* NOT expr (subexpression in nv_next) */
250 #define	FX_AND		2	/* AND expr (lhs in nv_ptr, rhs in nv_next) */
251 #define	FX_OR		3	/* OR expr (lhs in nv_ptr, rhs in nv_next) */
252 
253 /* flags */
254 #define	FI_SEL		0x01	/* selected */
255 #define	FI_NEEDSCOUNT	0x02	/* needs-count */
256 #define	FI_NEEDSFLAG	0x04	/* needs-flag */
257 #define	FI_HIDDEN	0x08	/* obscured by other(s), base names overlap */
258 
259 /*
260  * Hash tables look up name=value pairs.  The pointer value of the name
261  * is assumed to be constant forever; this can be arranged by interning
262  * the name.  (This is fairly convenient since our lexer does this for
263  * all identifier-like strings---it has to save them anyway, lest yacc's
264  * look-ahead wipe out the current one.)
265  */
266 struct hashtab;
267 
268 extern const char *conffile;		/* source file, e.g., "GENERIC.sparc" */
269 extern const char *last_component;
270 extern const char *machine;		/* machine type, e.g., "sparc" or "sun3" */
271 extern const char *machinearch;	/* machine arch, e.g., "sparc" or "m68k" */
272 extern const char *srcdir;		/* path to source directory (rel. to build) */
273 extern const char *builddir;		/* path to build directory */
274 extern const char *defbuilddir;	/* default build directory */
275 extern int errors;			/* counts calls to error() */
276 extern int minmaxusers;		/* minimum "maxusers" parameter */
277 extern int defmaxusers;		/* default "maxusers" parameter */
278 extern int maxmaxusers;		/* default "maxusers" parameter */
279 extern int maxusers;		/* configuration's "maxusers" parameter */
280 extern int maxpartitions;		/* configuration's "maxpartitions" parameter */
281 extern struct nvlist *options;	/* options */
282 extern struct nvlist *defoptions;	/* "defopt"'d options */
283 extern struct nvlist *mkoptions;	/* makeoptions */
284 extern struct hashtab *devbasetab;	/* devbase lookup */
285 extern struct hashtab *devatab;	/* devbase attachment lookup */
286 extern struct hashtab *selecttab;	/* selects things that are "optional foo" */
287 extern struct hashtab *needcnttab;	/* retains names marked "needs-count" */
288 extern struct hashtab *opttab;	/* table of configured options */
289 extern struct hashtab *defopttab;	/* options that have been "defopt"'d */
290 extern struct devbase *allbases;	/* list of all devbase structures */
291 extern struct deva *alldevas;		/* list of all devbase attachment structures */
292 extern struct config *allcf;		/* list of configured kernels */
293 extern struct devi *alldevi;		/* list of all instances */
294 extern struct devi *allpseudo;	/* list of all pseudo-devices */
295 extern int ndevi;			/* number of devi's (before packing) */
296 extern int npseudo;		/* number of pseudo's */
297 
298 extern struct files *allfiles;	/* list of all kernel source files */
299 extern struct objects *allobjects;	/* list of all kernel object and library files */
300 
301 extern struct devi **packed;		/* arrayified table for packed devi's */
302 extern int npacked;		/* size of packed table, <= ndevi */
303 
304 struct parents {			/* pv[] table for config */
305 	short	*vec;
306 	int	used;
307 };
308 extern struct parents parents;
309 struct locators {			/* loc[] table for config */
310 	const char **vec;
311 	int	used;
312 };
313 extern struct locators locators;
314 
315 /* files.c */
316 void	initfiles(void);
317 void	checkfiles(void);
318 int	fixfiles(void);		/* finalize */
319 int	fixobjects(void);
320 void	addfile(struct nvlist *, struct nvlist *, int, const char *);
321 void	addobject(const char *, struct nvlist *, int);
322 
323 /* hash.c */
324 struct	hashtab *ht_new(void);
325 int	ht_insrep(struct hashtab *, const char *, void *, int);
326 int	ht_remove(struct hashtab *, const char *);
327 #define	ht_insert(ht, nam, val) ht_insrep(ht, nam, val, 0)
328 #define	ht_replace(ht, nam, val) ht_insrep(ht, nam, val, 1)
329 void	*ht_lookup(struct hashtab *, const char *);
330 void	initintern(void);
331 const char *intern(const char *);
332 
333 /* main.c */
334 void	addoption(const char *name, const char *value);
335 void	removeoption(const char *name);
336 void	addmkoption(const char *name, const char *value);
337 void	defoption(const char *name);
338 int	devbase_has_instances(struct devbase *, int);
339 int	deva_has_instances(struct deva *, int);
340 void	setupdirs(void);
341 extern int	pflag;
342 extern char 	*sflag;
343 extern char	*bflag;
344 extern char	*startdir;
345 
346 /* mkheaders.c */
347 int	mkheaders(void);
348 
349 /* mkioconf.c */
350 int	mkioconf(void);
351 
352 /* mkmakefile.c */
353 int	mkmakefile(void);
354 
355 /* mkswap.c */
356 extern dev_t nodev;
357 int	mkswap(void);
358 
359 /* pack.c */
360 void	pack(void);
361 
362 /* scan.l */
363 int	currentline(void);
364 int	firstfile(const char *);
365 int	include(const char *, int);
366 
367 /* sem.c, other than for yacc actions */
368 void	initsem(void);
369 
370 /* util.c */
371 void	*emalloc(size_t);
372 void	*ereallocarray(void *, size_t, size_t);
373 void	*ecalloc(size_t, size_t);
374 char	*sourcepath(const char *);
375 void	error(const char *, ...)			/* immediate errs */
376 		__attribute__((__format__ (printf, 1, 2)));
377 void	xerror(const char *, int, const char *, ...)	/* delayed errs */
378 		__attribute__((__format__ (printf, 3, 4)));
379 __dead void panic(const char *, ...)
380 		__attribute__((__format__ (printf, 1, 2)));
381 struct nvlist *newnv(const char *, const char *, void *, int, struct nvlist *);
382 void	nvfree(struct nvlist *);
383 void	nvfreel(struct nvlist *);
384 
385 int	ukc(char *, char *, int, int);
386