xref: /onnv-gate/usr/src/uts/common/os/modsysfile.c (revision 7009:a2cd0bb4d403)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226777Sjw149990  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/inttypes.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/disp.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/bootconf.h>
360Sstevel@tonic-gate #include <sys/sysconf.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/esunddi.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/kmem.h>
410Sstevel@tonic-gate #include <sys/vmem.h>
420Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
430Sstevel@tonic-gate #include <sys/hwconf.h>
440Sstevel@tonic-gate #include <sys/modctl.h>
450Sstevel@tonic-gate #include <sys/cmn_err.h>
460Sstevel@tonic-gate #include <sys/kobj.h>
470Sstevel@tonic-gate #include <sys/kobj_lex.h>
480Sstevel@tonic-gate #include <sys/errno.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/autoconf.h>
510Sstevel@tonic-gate #include <sys/callb.h>
520Sstevel@tonic-gate #include <sys/sysmacros.h>
530Sstevel@tonic-gate #include <sys/dacf.h>
540Sstevel@tonic-gate #include <vm/seg_kmem.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate struct hwc_class *hcl_head;	/* head of list of classes */
570Sstevel@tonic-gate static kmutex_t hcl_lock;	/* for accessing list of classes */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	DAFILE		"/etc/driver_aliases"
600Sstevel@tonic-gate #define	CLASSFILE	"/etc/driver_classes"
610Sstevel@tonic-gate #define	DACFFILE	"/etc/dacf.conf"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static char class_file[] = CLASSFILE;
640Sstevel@tonic-gate static char dafile[] = DAFILE;
650Sstevel@tonic-gate static char dacffile[] = DACFFILE;
660Sstevel@tonic-gate 
675648Ssetje char *systemfile = "/etc/system";	/* name of ascii system file */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static struct sysparam *sysparam_hd;	/* head of parameters list */
700Sstevel@tonic-gate static struct sysparam *sysparam_tl;	/* tail of parameters list */
710Sstevel@tonic-gate static vmem_t *mod_sysfile_arena;	/* parser memory */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate char obp_bootpath[BO_MAXOBJNAME];	/* bootpath from obp */
740Sstevel@tonic-gate char svm_bootpath[BO_MAXOBJNAME];	/* bootpath redirected via rootdev */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #if defined(_PSM_MODULES)
770Sstevel@tonic-gate 
780Sstevel@tonic-gate struct psm_mach {
790Sstevel@tonic-gate 	struct psm_mach *m_next;
800Sstevel@tonic-gate 	char		*m_machname;
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static struct psm_mach *pmach_head;	/* head of list of classes */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	MACHFILE	"/etc/mach"
860Sstevel@tonic-gate static char mach_file[] = MACHFILE;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #endif	/* _PSM_MODULES */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #if defined(_RTC_CONFIG)
910Sstevel@tonic-gate static char rtc_config_file[] = "/etc/rtc_config";
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static void sys_set_var(int, struct sysparam *, void *);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static void setparams(void);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * driver.conf parse thread control structure
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate struct hwc_parse_mt {
1020Sstevel@tonic-gate 	ksema_t		sema;
1030Sstevel@tonic-gate 	char		*name;		/* name of .conf files */
1040Sstevel@tonic-gate 	struct par_list	**pl;		/* parsed parent list */
1050Sstevel@tonic-gate 	ddi_prop_t	**props;	/* parsed properties */
1060Sstevel@tonic-gate 	int		rv;		/* return value */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static int hwc_parse_now(char *, struct par_list **, ddi_prop_t **);
1100Sstevel@tonic-gate static void hwc_parse_thread(struct hwc_parse_mt *);
1110Sstevel@tonic-gate static struct hwc_parse_mt *hwc_parse_mtalloc(char *, struct par_list **,
1120Sstevel@tonic-gate 	ddi_prop_t **);
1130Sstevel@tonic-gate static void hwc_parse_mtfree(struct hwc_parse_mt *);
1140Sstevel@tonic-gate static void add_spec(struct hwc_spec *, struct par_list **);
1150Sstevel@tonic-gate static void add_props(struct hwc_spec *, ddi_prop_t **);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static void check_system_file(void);
1180Sstevel@tonic-gate static int sysparam_compare_entry(struct sysparam *, struct sysparam *);
1190Sstevel@tonic-gate static char *sysparam_type_to_str(int);
1200Sstevel@tonic-gate static void sysparam_count_entry(struct sysparam *, int *, u_longlong_t *);
1210Sstevel@tonic-gate static void sysparam_print_warning(struct sysparam *, u_longlong_t);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #ifdef DEBUG
1240Sstevel@tonic-gate static int parse_debug_on = 0;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*VARARGS1*/
1270Sstevel@tonic-gate static void
parse_debug(struct _buf * file,char * fmt,...)1280Sstevel@tonic-gate parse_debug(struct _buf *file, char *fmt, ...)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	va_list adx;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (parse_debug_on) {
1330Sstevel@tonic-gate 		va_start(adx, fmt);
1340Sstevel@tonic-gate 		vprintf(fmt, adx);
1350Sstevel@tonic-gate 		if (file)
1360Sstevel@tonic-gate 			printf(" on line %d of %s\n", kobj_linenum(file),
1373912Slling 			    kobj_filename(file));
1380Sstevel@tonic-gate 		va_end(adx);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate #endif /* DEBUG */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	FE_BUFLEN 256
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*PRINTFLIKE3*/
1460Sstevel@tonic-gate void
kobj_file_err(int type,struct _buf * file,char * fmt,...)1470Sstevel@tonic-gate kobj_file_err(int type,  struct _buf *file, char *fmt, ...)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	va_list ap;
1500Sstevel@tonic-gate 	/*
1510Sstevel@tonic-gate 	 * If we're in trouble, we might be short on stack... be paranoid
1520Sstevel@tonic-gate 	 */
1530Sstevel@tonic-gate 	char *buf = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1540Sstevel@tonic-gate 	char *trailer = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1550Sstevel@tonic-gate 	char *fmt_str = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1560Sstevel@tonic-gate 	char prefix = '\0';
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	va_start(ap, fmt);
1590Sstevel@tonic-gate 	if (strchr("^!?", fmt[0]) != NULL) {
1600Sstevel@tonic-gate 		prefix = fmt[0];
1610Sstevel@tonic-gate 		fmt++;
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	(void) vsnprintf(buf, FE_BUFLEN, fmt, ap);
1640Sstevel@tonic-gate 	va_end(ap);
1650Sstevel@tonic-gate 	(void) snprintf(trailer, FE_BUFLEN, " on line %d of %s",
1660Sstevel@tonic-gate 	    kobj_linenum(file), kobj_filename(file));
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * If prefixed with !^?, prepend that character
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	if (prefix != '\0') {
1720Sstevel@tonic-gate 		(void) snprintf(fmt_str, FE_BUFLEN, "%c%%s%%s", prefix);
1730Sstevel@tonic-gate 	} else {
1740Sstevel@tonic-gate 		(void) strncpy(fmt_str, "%s%s", FE_BUFLEN);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	cmn_err(type, fmt_str, buf, trailer);
1780Sstevel@tonic-gate 	kmem_free(buf, FE_BUFLEN);
1790Sstevel@tonic-gate 	kmem_free(trailer, FE_BUFLEN);
1800Sstevel@tonic-gate 	kmem_free(fmt_str, FE_BUFLEN);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #ifdef DEBUG
1840Sstevel@tonic-gate char *tokennames[] = {
185692Seota 	"UNEXPECTED",
1860Sstevel@tonic-gate 	"EQUALS",
1870Sstevel@tonic-gate 	"AMPERSAND",
1880Sstevel@tonic-gate 	"BIT_OR",
1890Sstevel@tonic-gate 	"STAR",
1900Sstevel@tonic-gate 	"POUND",
1910Sstevel@tonic-gate 	"COLON",
1920Sstevel@tonic-gate 	"SEMICOLON",
1930Sstevel@tonic-gate 	"COMMA",
1940Sstevel@tonic-gate 	"SLASH",
1950Sstevel@tonic-gate 	"WHITE_SPACE",
1960Sstevel@tonic-gate 	"NEWLINE",
1970Sstevel@tonic-gate 	"EOF",
1980Sstevel@tonic-gate 	"STRING",
1990Sstevel@tonic-gate 	"HEXVAL",
2000Sstevel@tonic-gate 	"DECVAL",
2010Sstevel@tonic-gate 	"NAME"
2020Sstevel@tonic-gate };
2030Sstevel@tonic-gate #endif /* DEBUG */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate token_t
kobj_lex(struct _buf * file,char * val,size_t size)2060Sstevel@tonic-gate kobj_lex(struct _buf *file, char *val, size_t size)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	char	*cp;
2090Sstevel@tonic-gate 	int	ch, oval, badquote;
2100Sstevel@tonic-gate 	size_t	remain;
211692Seota 	token_t token = UNEXPECTED;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if (size < 2)
214692Seota 		return (token);	/* this token is UNEXPECTED */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	cp = val;
2170Sstevel@tonic-gate 	while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
2180Sstevel@tonic-gate 		;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	remain = size - 1;
2210Sstevel@tonic-gate 	*cp++ = (char)ch;
2220Sstevel@tonic-gate 	switch (ch) {
2230Sstevel@tonic-gate 	case '=':
2240Sstevel@tonic-gate 		token = EQUALS;
2250Sstevel@tonic-gate 		break;
2260Sstevel@tonic-gate 	case '&':
2270Sstevel@tonic-gate 		token = AMPERSAND;
2280Sstevel@tonic-gate 		break;
2290Sstevel@tonic-gate 	case '|':
2300Sstevel@tonic-gate 		token = BIT_OR;
2310Sstevel@tonic-gate 		break;
2320Sstevel@tonic-gate 	case '*':
2330Sstevel@tonic-gate 		token = STAR;
2340Sstevel@tonic-gate 		break;
2350Sstevel@tonic-gate 	case '#':
2360Sstevel@tonic-gate 		token = POUND;
2370Sstevel@tonic-gate 		break;
2380Sstevel@tonic-gate 	case ':':
2390Sstevel@tonic-gate 		token = COLON;
2400Sstevel@tonic-gate 		break;
2410Sstevel@tonic-gate 	case ';':
2420Sstevel@tonic-gate 		token = SEMICOLON;
2430Sstevel@tonic-gate 		break;
2440Sstevel@tonic-gate 	case ',':
2450Sstevel@tonic-gate 		token = COMMA;
2460Sstevel@tonic-gate 		break;
2470Sstevel@tonic-gate 	case '/':
2480Sstevel@tonic-gate 		token = SLASH;
2490Sstevel@tonic-gate 		break;
2500Sstevel@tonic-gate 	case ' ':
2510Sstevel@tonic-gate 	case '\t':
2520Sstevel@tonic-gate 	case '\f':
2530Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) == ' ' ||
2540Sstevel@tonic-gate 		    ch == '\t' || ch == '\f') {
2550Sstevel@tonic-gate 			if (--remain == 0) {
256692Seota 				token = UNEXPECTED;
257692Seota 				goto out;
2580Sstevel@tonic-gate 			}
2590Sstevel@tonic-gate 			*cp++ = (char)ch;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 		(void) kobj_ungetc(file);
2620Sstevel@tonic-gate 		token = WHITE_SPACE;
2630Sstevel@tonic-gate 		break;
2640Sstevel@tonic-gate 	case '\n':
2650Sstevel@tonic-gate 	case '\r':
2660Sstevel@tonic-gate 		token = NEWLINE;
2670Sstevel@tonic-gate 		break;
2680Sstevel@tonic-gate 	case '"':
2690Sstevel@tonic-gate 		remain++;
2700Sstevel@tonic-gate 		cp--;
2710Sstevel@tonic-gate 		badquote = 0;
2720Sstevel@tonic-gate 		while (!badquote && (ch  = kobj_getc(file)) != '"') {
2730Sstevel@tonic-gate 			switch (ch) {
2740Sstevel@tonic-gate 			case '\n':
2750Sstevel@tonic-gate 			case -1:
2760Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Missing \"");
2770Sstevel@tonic-gate 				remain = size - 1;
2780Sstevel@tonic-gate 				cp = val;
2790Sstevel@tonic-gate 				*cp++ = '\n';
2800Sstevel@tonic-gate 				badquote = 1;
2810Sstevel@tonic-gate 				/* since we consumed the newline/EOF */
2820Sstevel@tonic-gate 				(void) kobj_ungetc(file);
2830Sstevel@tonic-gate 				break;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 			case '\\':
2860Sstevel@tonic-gate 				if (--remain == 0) {
287692Seota 					token = UNEXPECTED;
288692Seota 					goto out;
2890Sstevel@tonic-gate 				}
2900Sstevel@tonic-gate 				ch = (char)kobj_getc(file);
2910Sstevel@tonic-gate 				if (!isdigit(ch)) {
2920Sstevel@tonic-gate 					/* escape the character */
2930Sstevel@tonic-gate 					*cp++ = (char)ch;
2940Sstevel@tonic-gate 					break;
2950Sstevel@tonic-gate 				}
2960Sstevel@tonic-gate 				oval = 0;
2970Sstevel@tonic-gate 				while (ch >= '0' && ch <= '7') {
2980Sstevel@tonic-gate 					ch -= '0';
2990Sstevel@tonic-gate 					oval = (oval << 3) + ch;
3000Sstevel@tonic-gate 					ch = (char)kobj_getc(file);
3010Sstevel@tonic-gate 				}
3020Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3030Sstevel@tonic-gate 				/* check for character overflow? */
3040Sstevel@tonic-gate 				if (oval > 127) {
3050Sstevel@tonic-gate 					cmn_err(CE_WARN,
3060Sstevel@tonic-gate 					    "Character "
3070Sstevel@tonic-gate 					    "overflow detected.");
3080Sstevel@tonic-gate 				}
3090Sstevel@tonic-gate 				*cp++ = (char)oval;
3100Sstevel@tonic-gate 				break;
3110Sstevel@tonic-gate 			default:
3120Sstevel@tonic-gate 				if (--remain == 0) {
313692Seota 					token = UNEXPECTED;
314692Seota 					goto out;
3150Sstevel@tonic-gate 				}
3160Sstevel@tonic-gate 				*cp++ = (char)ch;
3170Sstevel@tonic-gate 				break;
3180Sstevel@tonic-gate 			}
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 		token = STRING;
3210Sstevel@tonic-gate 		break;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	case -1:
3240Sstevel@tonic-gate 		token = EOF;
3250Sstevel@tonic-gate 		break;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	default:
3280Sstevel@tonic-gate 		/*
3290Sstevel@tonic-gate 		 * detect a lone '-' (including at the end of a line), and
3300Sstevel@tonic-gate 		 * identify it as a 'name'
3310Sstevel@tonic-gate 		 */
3320Sstevel@tonic-gate 		if (ch == '-') {
3330Sstevel@tonic-gate 			if (--remain == 0) {
334692Seota 				token = UNEXPECTED;
335692Seota 				goto out;
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 			*cp++ = (char)(ch = kobj_getc(file));
3380Sstevel@tonic-gate 			if (iswhite(ch) || (ch == '\n')) {
3390Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3400Sstevel@tonic-gate 				remain++;
3410Sstevel@tonic-gate 				cp--;
3420Sstevel@tonic-gate 				token = NAME;
3430Sstevel@tonic-gate 				break;
3440Sstevel@tonic-gate 			}
3450Sstevel@tonic-gate 		} else if (isunary(ch)) {
3460Sstevel@tonic-gate 			if (--remain == 0) {
347692Seota 				token = UNEXPECTED;
348692Seota 				goto out;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 			*cp++ = (char)(ch = kobj_getc(file));
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		if (isdigit(ch)) {
3550Sstevel@tonic-gate 			if (ch == '0') {
3560Sstevel@tonic-gate 				if ((ch = kobj_getc(file)) == 'x') {
3570Sstevel@tonic-gate 					if (--remain == 0) {
358692Seota 						token = UNEXPECTED;
359692Seota 						goto out;
3600Sstevel@tonic-gate 					}
3610Sstevel@tonic-gate 					*cp++ = (char)ch;
3620Sstevel@tonic-gate 					ch = kobj_getc(file);
3630Sstevel@tonic-gate 					while (isxdigit(ch)) {
3640Sstevel@tonic-gate 						if (--remain == 0) {
365692Seota 							token = UNEXPECTED;
366692Seota 							goto out;
3670Sstevel@tonic-gate 						}
3680Sstevel@tonic-gate 						*cp++ = (char)ch;
3690Sstevel@tonic-gate 						ch = kobj_getc(file);
3700Sstevel@tonic-gate 					}
3710Sstevel@tonic-gate 					(void) kobj_ungetc(file);
3720Sstevel@tonic-gate 					token = HEXVAL;
3730Sstevel@tonic-gate 				} else {
3740Sstevel@tonic-gate 					goto digit;
3750Sstevel@tonic-gate 				}
3760Sstevel@tonic-gate 			} else {
3770Sstevel@tonic-gate 				ch = kobj_getc(file);
3780Sstevel@tonic-gate digit:
3790Sstevel@tonic-gate 				while (isdigit(ch)) {
3800Sstevel@tonic-gate 					if (--remain == 0) {
381692Seota 						token = UNEXPECTED;
382692Seota 						goto out;
3830Sstevel@tonic-gate 					}
3840Sstevel@tonic-gate 					*cp++ = (char)ch;
3850Sstevel@tonic-gate 					ch = kobj_getc(file);
3860Sstevel@tonic-gate 				}
3870Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3880Sstevel@tonic-gate 				token = DECVAL;
3890Sstevel@tonic-gate 			}
3900Sstevel@tonic-gate 		} else if (isalpha(ch) || ch == '\\' || ch == '_') {
3910Sstevel@tonic-gate 			if (ch != '\\') {
3920Sstevel@tonic-gate 				ch = kobj_getc(file);
3930Sstevel@tonic-gate 			} else {
3940Sstevel@tonic-gate 				/*
3950Sstevel@tonic-gate 				 * if the character was a backslash,
3960Sstevel@tonic-gate 				 * back up so we can overwrite it with
3970Sstevel@tonic-gate 				 * the next (i.e. escaped) character.
3980Sstevel@tonic-gate 				 */
3990Sstevel@tonic-gate 				remain++;
4000Sstevel@tonic-gate 				cp--;
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 			while (isnamechar(ch) || ch == '\\') {
4030Sstevel@tonic-gate 				if (ch == '\\')
4040Sstevel@tonic-gate 					ch = kobj_getc(file);
4050Sstevel@tonic-gate 				if (--remain == 0) {
406692Seota 					token = UNEXPECTED;
407692Seota 					goto out;
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 				*cp++ = (char)ch;
4100Sstevel@tonic-gate 				ch = kobj_getc(file);
4110Sstevel@tonic-gate 			}
4120Sstevel@tonic-gate 			(void) kobj_ungetc(file);
4130Sstevel@tonic-gate 			token = NAME;
4140Sstevel@tonic-gate 		} else {
415692Seota 			token = UNEXPECTED;
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 	}
419692Seota out:
4200Sstevel@tonic-gate 	*cp = '\0';
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate #ifdef DEBUG
423692Seota 	/*
424692Seota 	 * The UNEXPECTED token is the first element of the tokennames array,
425692Seota 	 * but its token value is -1.  Adjust the value by adding one to it
426692Seota 	 * to change it to an index of the array.
427692Seota 	 */
428692Seota 	parse_debug(NULL, "kobj_lex: token %s value '%s'\n",
429692Seota 	    tokennames[token+1], val);
4300Sstevel@tonic-gate #endif
4310Sstevel@tonic-gate 	return (token);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate  * Leave NEWLINE as the next character.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate void
kobj_find_eol(struct _buf * file)4390Sstevel@tonic-gate kobj_find_eol(struct _buf *file)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	int ch;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	while ((ch = kobj_getc(file)) != -1) {
4440Sstevel@tonic-gate 		if (isnewline(ch)) {
4450Sstevel@tonic-gate 			(void) kobj_ungetc(file);
4460Sstevel@tonic-gate 			break;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * The ascii system file is read and processed.
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  * The syntax of commands is as follows:
4550Sstevel@tonic-gate  *
4560Sstevel@tonic-gate  * '*' in column 1 is a comment line.
4570Sstevel@tonic-gate  * <command> : <value>
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  * command is EXCLUDE, INCLUDE, FORCELOAD, ROOTDEV, ROOTFS,
4600Sstevel@tonic-gate  *	SWAPDEV, SWAPFS, MODDIR, SET
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * value is an ascii string meaningful for the command.
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  * Table of commands
4670Sstevel@tonic-gate  */
4680Sstevel@tonic-gate static struct modcmd modcmd[] = {
4690Sstevel@tonic-gate 	{ "EXCLUDE",	MOD_EXCLUDE	},
4700Sstevel@tonic-gate 	{ "exclude",	MOD_EXCLUDE	},
4710Sstevel@tonic-gate 	{ "INCLUDE",	MOD_INCLUDE	},
4720Sstevel@tonic-gate 	{ "include",	MOD_INCLUDE	},
4730Sstevel@tonic-gate 	{ "FORCELOAD",	MOD_FORCELOAD	},
4740Sstevel@tonic-gate 	{ "forceload",	MOD_FORCELOAD	},
4750Sstevel@tonic-gate 	{ "ROOTDEV",	MOD_ROOTDEV	},
4760Sstevel@tonic-gate 	{ "rootdev",	MOD_ROOTDEV	},
4770Sstevel@tonic-gate 	{ "ROOTFS",	MOD_ROOTFS	},
4780Sstevel@tonic-gate 	{ "rootfs",	MOD_ROOTFS	},
4790Sstevel@tonic-gate 	{ "SWAPDEV",	MOD_SWAPDEV	},
4800Sstevel@tonic-gate 	{ "swapdev",	MOD_SWAPDEV	},
4810Sstevel@tonic-gate 	{ "SWAPFS",	MOD_SWAPFS	},
4820Sstevel@tonic-gate 	{ "swapfs",	MOD_SWAPFS	},
4830Sstevel@tonic-gate 	{ "MODDIR",	MOD_MODDIR	},
4840Sstevel@tonic-gate 	{ "moddir",	MOD_MODDIR	},
4850Sstevel@tonic-gate 	{ "SET",	MOD_SET		},
4860Sstevel@tonic-gate 	{ "set",	MOD_SET		},
4870Sstevel@tonic-gate 	{ "SET32",	MOD_SET32	},
4880Sstevel@tonic-gate 	{ "set32",	MOD_SET32	},
4890Sstevel@tonic-gate 	{ "SET64",	MOD_SET64	},
4900Sstevel@tonic-gate 	{ "set64",	MOD_SET64	},
4910Sstevel@tonic-gate 	{ NULL,		MOD_UNKNOWN	}
4920Sstevel@tonic-gate };
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate static char bad_op[] = "illegal operator '%s' used on a string";
4960Sstevel@tonic-gate static char colon_err[] = "A colon (:) must follow the '%s' command";
4970Sstevel@tonic-gate static char tok_err[] = "Unexpected token '%s'";
4980Sstevel@tonic-gate static char extra_err[] = "extraneous input ignored starting at '%s'";
4990Sstevel@tonic-gate static char oversize_err[] = "value too long";
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static struct sysparam *
do_sysfile_cmd(struct _buf * file,const char * cmd)5020Sstevel@tonic-gate do_sysfile_cmd(struct _buf *file, const char *cmd)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct sysparam *sysp;
5050Sstevel@tonic-gate 	struct modcmd *mcp;
5060Sstevel@tonic-gate 	token_t token, op;
5070Sstevel@tonic-gate 	char *cp;
5080Sstevel@tonic-gate 	int ch;
5090Sstevel@tonic-gate 	char tok1[MOD_MAXPATH + 1]; /* used to read the path set by 'moddir' */
5100Sstevel@tonic-gate 	char tok2[64];
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
5130Sstevel@tonic-gate 		if (strcmp(mcp->mc_cmdname, cmd) == 0)
5140Sstevel@tonic-gate 			break;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	sysp = vmem_alloc(mod_sysfile_arena, sizeof (struct sysparam),
5170Sstevel@tonic-gate 	    VM_SLEEP);
5180Sstevel@tonic-gate 	bzero(sysp, sizeof (struct sysparam));
5190Sstevel@tonic-gate 	sysp->sys_op = SETOP_NONE; /* set op to noop initially */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	switch (sysp->sys_type = mcp->mc_type) {
5220Sstevel@tonic-gate 	case MOD_INCLUDE:
5230Sstevel@tonic-gate 	case MOD_EXCLUDE:
5240Sstevel@tonic-gate 	case MOD_FORCELOAD:
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * Are followed by colon.
5270Sstevel@tonic-gate 		 */
5280Sstevel@tonic-gate 	case MOD_ROOTFS:
5290Sstevel@tonic-gate 	case MOD_SWAPFS:
5300Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) == COLON) {
5310Sstevel@tonic-gate 			token = kobj_lex(file, tok1, sizeof (tok1));
5320Sstevel@tonic-gate 		} else {
5330Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
5340Sstevel@tonic-gate 		}
5350Sstevel@tonic-gate 		if (token != NAME) {
5360Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "value expected");
5370Sstevel@tonic-gate 			goto bad;
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		cp = tok1 + strlen(tok1);
5410Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
5420Sstevel@tonic-gate 		    !isnewline(ch)) {
5430Sstevel@tonic-gate 			if (cp - tok1 >= sizeof (tok1) - 1) {
5440Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
5450Sstevel@tonic-gate 				goto bad;
5460Sstevel@tonic-gate 			}
5470Sstevel@tonic-gate 			*cp++ = (char)ch;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		*cp = '\0';
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		if (ch != -1)
5520Sstevel@tonic-gate 			(void) kobj_ungetc(file);
5530Sstevel@tonic-gate 		if (sysp->sys_type == MOD_INCLUDE)
5540Sstevel@tonic-gate 			return (NULL);
5550Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
5560Sstevel@tonic-gate 		    VM_SLEEP);
5570Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
5580Sstevel@tonic-gate 		break;
5590Sstevel@tonic-gate 	case MOD_SET:
5600Sstevel@tonic-gate 	case MOD_SET64:
5610Sstevel@tonic-gate 	case MOD_SET32:
5620Sstevel@tonic-gate 	{
5630Sstevel@tonic-gate 		char *var;
5640Sstevel@tonic-gate 		token_t tok3;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		if (kobj_lex(file, tok1, sizeof (tok1)) != NAME) {
5670Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "value expected");
5680Sstevel@tonic-gate 			goto bad;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		/*
5720Sstevel@tonic-gate 		 * If the next token is a colon (:),
5730Sstevel@tonic-gate 		 * we have the <modname>:<variable> construct.
5740Sstevel@tonic-gate 		 */
5750Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok2, sizeof (tok2))) == COLON) {
5760Sstevel@tonic-gate 			if ((token = kobj_lex(file, tok2,
5770Sstevel@tonic-gate 			    sizeof (tok2))) == NAME) {
5780Sstevel@tonic-gate 				var = tok2;
5790Sstevel@tonic-gate 				/*
5800Sstevel@tonic-gate 				 * Save the module name.
5810Sstevel@tonic-gate 				 */
5820Sstevel@tonic-gate 				sysp->sys_modnam = vmem_alloc(mod_sysfile_arena,
5830Sstevel@tonic-gate 				    strlen(tok1) + 1, VM_SLEEP);
5840Sstevel@tonic-gate 				(void) strcpy(sysp->sys_modnam, tok1);
5850Sstevel@tonic-gate 				op = kobj_lex(file, tok1, sizeof (tok1));
5860Sstevel@tonic-gate 			} else {
5870Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "value expected");
5880Sstevel@tonic-gate 				goto bad;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 		} else {
5910Sstevel@tonic-gate 			/* otherwise, it was the op */
5920Sstevel@tonic-gate 			var = tok1;
5930Sstevel@tonic-gate 			op = token;
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 		/*
5960Sstevel@tonic-gate 		 * kernel param - place variable name in sys_ptr.
5970Sstevel@tonic-gate 		 */
5980Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(var) + 1,
5990Sstevel@tonic-gate 		    VM_SLEEP);
6000Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, var);
6010Sstevel@tonic-gate 		/* set operation */
6020Sstevel@tonic-gate 		switch (op) {
6030Sstevel@tonic-gate 		case EQUALS:
6040Sstevel@tonic-gate 			/* simple assignment */
6050Sstevel@tonic-gate 			sysp->sys_op = SETOP_ASSIGN;
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 		case AMPERSAND:
6080Sstevel@tonic-gate 			/* bitwise AND */
6090Sstevel@tonic-gate 			sysp->sys_op = SETOP_AND;
6100Sstevel@tonic-gate 			break;
6110Sstevel@tonic-gate 		case BIT_OR:
6120Sstevel@tonic-gate 			/* bitwise OR */
6130Sstevel@tonic-gate 			sysp->sys_op = SETOP_OR;
6140Sstevel@tonic-gate 			break;
6150Sstevel@tonic-gate 		default:
6160Sstevel@tonic-gate 			/* unsupported operation */
6170Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file,
6180Sstevel@tonic-gate 			    "unsupported operator %s", tok2);
6190Sstevel@tonic-gate 			goto bad;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		switch ((tok3 = kobj_lex(file, tok1, sizeof (tok1)))) {
6230Sstevel@tonic-gate 		case STRING:
6240Sstevel@tonic-gate 			/* string variable */
6250Sstevel@tonic-gate 			if (sysp->sys_op != SETOP_ASSIGN) {
6260Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, bad_op, tok1);
6270Sstevel@tonic-gate 				goto bad;
6280Sstevel@tonic-gate 			}
6290Sstevel@tonic-gate 			if (kobj_get_string(&sysp->sys_info, tok1) == 0) {
6300Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "string garbled");
6310Sstevel@tonic-gate 				goto bad;
6320Sstevel@tonic-gate 			}
6330Sstevel@tonic-gate 			/*
6340Sstevel@tonic-gate 			 * Set SYSPARAM_STR_TOKEN in sys_flags to notify
6350Sstevel@tonic-gate 			 * sysparam_print_warning() that this is a string
6360Sstevel@tonic-gate 			 * token.
6370Sstevel@tonic-gate 			 */
6380Sstevel@tonic-gate 			sysp->sys_flags |= SYSPARAM_STR_TOKEN;
6390Sstevel@tonic-gate 			break;
6400Sstevel@tonic-gate 		case HEXVAL:
6410Sstevel@tonic-gate 		case DECVAL:
6420Sstevel@tonic-gate 			if (kobj_getvalue(tok1, &sysp->sys_info) == -1) {
6430Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
6440Sstevel@tonic-gate 				    "invalid number '%s'", tok1);
6450Sstevel@tonic-gate 				goto bad;
6460Sstevel@tonic-gate 			}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 			/*
6490Sstevel@tonic-gate 			 * Set the appropriate flag (hexadecimal or decimal)
6500Sstevel@tonic-gate 			 * in sys_flags for sysparam_print_warning() to be
6510Sstevel@tonic-gate 			 * able to print the number with the correct format.
6520Sstevel@tonic-gate 			 */
6530Sstevel@tonic-gate 			if (tok3 == HEXVAL) {
6540Sstevel@tonic-gate 				sysp->sys_flags |= SYSPARAM_HEX_TOKEN;
6550Sstevel@tonic-gate 			} else {
6560Sstevel@tonic-gate 				sysp->sys_flags |= SYSPARAM_DEC_TOKEN;
6570Sstevel@tonic-gate 			}
6580Sstevel@tonic-gate 			break;
6590Sstevel@tonic-gate 		default:
6600Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "bad rvalue '%s'", tok1);
6610Sstevel@tonic-gate 			goto bad;
6620Sstevel@tonic-gate 		} /* end switch */
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		/*
6650Sstevel@tonic-gate 		 * Now that we've parsed it to check the syntax, consider
6660Sstevel@tonic-gate 		 * discarding it (because it -doesn't- apply to this flavor
6670Sstevel@tonic-gate 		 * of the kernel)
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate #ifdef _LP64
6700Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SET32)
6710Sstevel@tonic-gate 			return (NULL);
6720Sstevel@tonic-gate #else
6730Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SET64)
6740Sstevel@tonic-gate 			return (NULL);
6750Sstevel@tonic-gate #endif
6760Sstevel@tonic-gate 		sysp->sys_type = MOD_SET;
6770Sstevel@tonic-gate 		break;
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 	case MOD_MODDIR:
6800Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
6810Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
6820Sstevel@tonic-gate 			goto bad;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		cp = tok1;
6860Sstevel@tonic-gate 		while ((token = kobj_lex(file, cp,
6870Sstevel@tonic-gate 		    sizeof (tok1) - (cp - tok1))) != NEWLINE && token != EOF) {
6880Sstevel@tonic-gate 			if (token == -1) {
6890Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
6900Sstevel@tonic-gate 				goto bad;
6910Sstevel@tonic-gate 			}
6920Sstevel@tonic-gate 			cp += strlen(cp);
6930Sstevel@tonic-gate 			while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
6940Sstevel@tonic-gate 			    !isnewline(ch) && ch != ':') {
6950Sstevel@tonic-gate 				if (cp - tok1 >= sizeof (tok1) - 1) {
6960Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
6970Sstevel@tonic-gate 					    oversize_err);
6980Sstevel@tonic-gate 					goto bad;
6990Sstevel@tonic-gate 				}
7000Sstevel@tonic-gate 				*cp++ = (char)ch;
7010Sstevel@tonic-gate 			}
7024371Sgp87344 			*cp++ = ':';
7034371Sgp87344 			if (isnewline(ch)) {
7044371Sgp87344 				cp--;
7050Sstevel@tonic-gate 				(void) kobj_ungetc(file);
7064371Sgp87344 			}
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		(void) kobj_ungetc(file);
7090Sstevel@tonic-gate 		*cp  = '\0';
7100Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
7110Sstevel@tonic-gate 		    VM_SLEEP);
7120Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
7130Sstevel@tonic-gate 		break;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	case MOD_SWAPDEV:
7160Sstevel@tonic-gate 	case MOD_ROOTDEV:
7170Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
7180Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
7190Sstevel@tonic-gate 			goto bad;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
7220Sstevel@tonic-gate 			;
7230Sstevel@tonic-gate 		cp = tok1;
7240Sstevel@tonic-gate 		while (!iswhite(ch) && !isnewline(ch) && ch != -1) {
7250Sstevel@tonic-gate 			if (cp - tok1 >= sizeof (tok1) - 1) {
7260Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
7270Sstevel@tonic-gate 				goto bad;
7280Sstevel@tonic-gate 			}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 			*cp++ = (char)ch;
7310Sstevel@tonic-gate 			ch = kobj_getc(file);
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 		if (ch != -1)
7340Sstevel@tonic-gate 			(void) kobj_ungetc(file);
7350Sstevel@tonic-gate 		*cp = '\0';
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
7380Sstevel@tonic-gate 		    VM_SLEEP);
7390Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
7400Sstevel@tonic-gate 		break;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	case MOD_UNKNOWN:
7430Sstevel@tonic-gate 	default:
7440Sstevel@tonic-gate 		kobj_file_err(CE_WARN, file, "unknown command '%s'", cmd);
7450Sstevel@tonic-gate 		goto bad;
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	return (sysp);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate bad:
7510Sstevel@tonic-gate 	kobj_find_eol(file);
7520Sstevel@tonic-gate 	return (NULL);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate void
mod_read_system_file(int ask)7560Sstevel@tonic-gate mod_read_system_file(int ask)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	register struct sysparam *sp;
7590Sstevel@tonic-gate 	register struct _buf *file;
7600Sstevel@tonic-gate 	register token_t token, last_tok;
7610Sstevel@tonic-gate 	char tokval[MAXLINESIZE];
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	mod_sysfile_arena = vmem_create("mod_sysfile", NULL, 0, 8,
7640Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (ask)
7670Sstevel@tonic-gate 		mod_askparams();
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if (systemfile != NULL) {
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 		if ((file = kobj_open_file(systemfile)) ==
7720Sstevel@tonic-gate 		    (struct _buf *)-1) {
7730Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot open system file: %s",
7740Sstevel@tonic-gate 			    systemfile);
7750Sstevel@tonic-gate 		} else {
7760Sstevel@tonic-gate 			sysparam_tl = (struct sysparam *)&sysparam_hd;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 			last_tok = NEWLINE;
7790Sstevel@tonic-gate 			while ((token = kobj_lex(file, tokval,
7800Sstevel@tonic-gate 			    sizeof (tokval))) != EOF) {
7810Sstevel@tonic-gate 				switch (token) {
7820Sstevel@tonic-gate 				case STAR:
7830Sstevel@tonic-gate 				case POUND:
7840Sstevel@tonic-gate 					/*
7850Sstevel@tonic-gate 					 * Skip comments.
7860Sstevel@tonic-gate 					 */
7870Sstevel@tonic-gate 					kobj_find_eol(file);
7880Sstevel@tonic-gate 					break;
7890Sstevel@tonic-gate 				case NEWLINE:
7900Sstevel@tonic-gate 					kobj_newline(file);
7910Sstevel@tonic-gate 					last_tok = NEWLINE;
7920Sstevel@tonic-gate 					break;
7930Sstevel@tonic-gate 				case NAME:
7940Sstevel@tonic-gate 					if (last_tok != NEWLINE) {
7950Sstevel@tonic-gate 						kobj_file_err(CE_WARN, file,
7960Sstevel@tonic-gate 						    extra_err, tokval);
7970Sstevel@tonic-gate 						kobj_find_eol(file);
7980Sstevel@tonic-gate 					} else if ((sp = do_sysfile_cmd(file,
7990Sstevel@tonic-gate 					    tokval)) != NULL) {
8000Sstevel@tonic-gate 						sp->sys_next = NULL;
8010Sstevel@tonic-gate 						sysparam_tl->sys_next = sp;
8020Sstevel@tonic-gate 						sysparam_tl = sp;
8030Sstevel@tonic-gate 					}
8040Sstevel@tonic-gate 					last_tok = NAME;
8050Sstevel@tonic-gate 					break;
8060Sstevel@tonic-gate 				default:
8070Sstevel@tonic-gate 					kobj_file_err(CE_WARN,
8080Sstevel@tonic-gate 					    file, tok_err, tokval);
8090Sstevel@tonic-gate 					kobj_find_eol(file);
8100Sstevel@tonic-gate 					break;
8110Sstevel@tonic-gate 				}
8120Sstevel@tonic-gate 			}
8130Sstevel@tonic-gate 			kobj_close_file(file);
8140Sstevel@tonic-gate 		}
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Sanity check of /etc/system.
8190Sstevel@tonic-gate 	 */
8200Sstevel@tonic-gate 	check_system_file();
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	param_preset();
8230Sstevel@tonic-gate 	(void) mod_sysctl(SYS_SET_KVAR, NULL);
8242208Sdp201428 	param_check();
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (ask == 0)
8270Sstevel@tonic-gate 		setparams();
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Search for a specific module variable assignment in /etc/system.  If
8320Sstevel@tonic-gate  * successful, 1 is returned and the value is stored in '*value'.
8330Sstevel@tonic-gate  * Otherwise 0 is returned and '*value' isn't modified.  If 'module' is
8340Sstevel@tonic-gate  * NULL we look for global definitions.
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  * This is useful if the value of an assignment is needed before a
8370Sstevel@tonic-gate  * module is loaded (e.g. to obtain a default privileged rctl limit).
8380Sstevel@tonic-gate  */
8390Sstevel@tonic-gate int
mod_sysvar(const char * module,const char * name,u_longlong_t * value)8400Sstevel@tonic-gate mod_sysvar(const char *module, const char *name, u_longlong_t *value)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	struct sysparam	*sysp;
8430Sstevel@tonic-gate 	int cnt = 0; /* dummy */
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	ASSERT(name != NULL);
8460Sstevel@tonic-gate 	ASSERT(value != NULL);
8470Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 		if ((sysp->sys_type == MOD_SET) &&
8500Sstevel@tonic-gate 		    (((module == NULL) && (sysp->sys_modnam == NULL)) ||
8510Sstevel@tonic-gate 		    ((module != NULL) && (sysp->sys_modnam != NULL) &&
8520Sstevel@tonic-gate 		    (strcmp(module, sysp->sys_modnam) == 0)))) {
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 			ASSERT(sysp->sys_ptr != NULL);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 			if (strcmp(name, sysp->sys_ptr) == 0) {
8570Sstevel@tonic-gate 				sysparam_count_entry(sysp, &cnt, value);
8580Sstevel@tonic-gate 				if ((sysp->sys_flags & SYSPARAM_TERM) != 0)
8590Sstevel@tonic-gate 					return (1);
8600Sstevel@tonic-gate 				continue;
8610Sstevel@tonic-gate 			}
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 	ASSERT(cnt == 0);
8650Sstevel@tonic-gate 	return (0);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate  * This function scans sysparam records, which are created from the
8700Sstevel@tonic-gate  * contents of /etc/system, for entries which are logical duplicates,
8710Sstevel@tonic-gate  * and prints warning messages as appropriate.  When multiple "set"
8720Sstevel@tonic-gate  * commands are encountered, the pileup of values with "&", "|"
8730Sstevel@tonic-gate  * and "=" operators results in the final value.
8740Sstevel@tonic-gate  */
8750Sstevel@tonic-gate static void
check_system_file(void)8760Sstevel@tonic-gate check_system_file(void)
8770Sstevel@tonic-gate {
8780Sstevel@tonic-gate 	struct sysparam	*sysp;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
8810Sstevel@tonic-gate 		struct sysparam *entry, *final;
8820Sstevel@tonic-gate 		u_longlong_t value = 0;
8830Sstevel@tonic-gate 		int cnt = 1;
8840Sstevel@tonic-gate 		/*
8850Sstevel@tonic-gate 		 * If the entry is already checked, skip it.
8860Sstevel@tonic-gate 		 */
8870Sstevel@tonic-gate 		if ((sysp->sys_flags & SYSPARAM_DUP) != 0)
8880Sstevel@tonic-gate 			continue;
8890Sstevel@tonic-gate 		/*
8900Sstevel@tonic-gate 		 * Check if there is a duplicate entry by doing a linear
8910Sstevel@tonic-gate 		 * search.
8920Sstevel@tonic-gate 		 */
8930Sstevel@tonic-gate 		final = sysp;
8940Sstevel@tonic-gate 		for (entry = sysp->sys_next; entry != NULL;
8950Sstevel@tonic-gate 		    entry = entry->sys_next) {
8960Sstevel@tonic-gate 			/*
8970Sstevel@tonic-gate 			 * Check the entry. if it's different, skip this.
8980Sstevel@tonic-gate 			 */
8990Sstevel@tonic-gate 			if (sysparam_compare_entry(sysp, entry) != 0)
9000Sstevel@tonic-gate 				continue;
9010Sstevel@tonic-gate 			/*
9020Sstevel@tonic-gate 			 * Count the entry and put the mark.
9030Sstevel@tonic-gate 			 */
9040Sstevel@tonic-gate 			sysparam_count_entry(entry, &cnt, &value);
9050Sstevel@tonic-gate 			entry->sys_flags |= SYSPARAM_DUP;
9060Sstevel@tonic-gate 			final = entry;
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 		final->sys_flags |= SYSPARAM_TERM;
9090Sstevel@tonic-gate 		/*
9100Sstevel@tonic-gate 		 * Print the warning if it's duplicated.
9110Sstevel@tonic-gate 		 */
9120Sstevel@tonic-gate 		if (cnt >= 2)
9130Sstevel@tonic-gate 			sysparam_print_warning(final, value);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate  * Compare the sysparam records.
9190Sstevel@tonic-gate  * Return 0 if they are the same, return 1 if not.
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate static int
sysparam_compare_entry(struct sysparam * sysp,struct sysparam * entry)9220Sstevel@tonic-gate sysparam_compare_entry(struct sysparam *sysp, struct sysparam *entry)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	ASSERT(sysp->sys_ptr != NULL && entry->sys_ptr != NULL);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * If the command is rootdev, rootfs, swapdev, swapfs or moddir,
9280Sstevel@tonic-gate 	 * the record with the same type is treated as a duplicate record.
9290Sstevel@tonic-gate 	 * In other cases, the record is treated as a duplicate record when
9300Sstevel@tonic-gate 	 * its type, its module name (if it exists), and its variable name
9310Sstevel@tonic-gate 	 * are the same.
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 	switch (sysp->sys_type) {
9340Sstevel@tonic-gate 	case MOD_ROOTDEV:
9350Sstevel@tonic-gate 	case MOD_ROOTFS:
9360Sstevel@tonic-gate 	case MOD_SWAPDEV:
9370Sstevel@tonic-gate 	case MOD_SWAPFS:
9380Sstevel@tonic-gate 	case MOD_MODDIR:
9390Sstevel@tonic-gate 		return (sysp->sys_type == entry->sys_type ? 0 : 1);
9400Sstevel@tonic-gate 	default: /* In other cases, just go through it. */
9410Sstevel@tonic-gate 		break;
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (sysp->sys_type != entry->sys_type)
9450Sstevel@tonic-gate 		return (1);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	if (sysp->sys_modnam != NULL && entry->sys_modnam == NULL)
9480Sstevel@tonic-gate 		return (1);
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (sysp->sys_modnam == NULL && entry->sys_modnam != NULL)
9510Sstevel@tonic-gate 		return (1);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	if (sysp->sys_modnam != NULL && entry->sys_modnam != NULL &&
9540Sstevel@tonic-gate 	    strcmp(sysp->sys_modnam, entry->sys_modnam) != 0)
9550Sstevel@tonic-gate 		return (1);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	return (strcmp(sysp->sys_ptr, entry->sys_ptr));
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /*
9610Sstevel@tonic-gate  * Translate a sysparam type value to a string.
9620Sstevel@tonic-gate  */
9630Sstevel@tonic-gate static char *
sysparam_type_to_str(int type)9640Sstevel@tonic-gate sysparam_type_to_str(int type)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	struct modcmd *mcp;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
9690Sstevel@tonic-gate 		if (mcp->mc_type == type)
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 	ASSERT(mcp->mc_type == type);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	if (type != MOD_UNKNOWN)
9750Sstevel@tonic-gate 		return ((++mcp)->mc_cmdname); /* lower case */
9760Sstevel@tonic-gate 	else
9770Sstevel@tonic-gate 		return ("");	/* MOD_UNKNOWN */
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * Check the entry and accumulate the number of entries.
9820Sstevel@tonic-gate  */
9830Sstevel@tonic-gate static void
sysparam_count_entry(struct sysparam * sysp,int * cnt,u_longlong_t * value)9840Sstevel@tonic-gate sysparam_count_entry(struct sysparam *sysp, int *cnt, u_longlong_t *value)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate 	u_longlong_t ul = sysp->sys_info;
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	switch (sysp->sys_op) {
9890Sstevel@tonic-gate 	case SETOP_ASSIGN:
9900Sstevel@tonic-gate 		*value = ul;
9910Sstevel@tonic-gate 		(*cnt)++;
9920Sstevel@tonic-gate 		return;
9930Sstevel@tonic-gate 	case SETOP_AND:
9940Sstevel@tonic-gate 		*value &= ul;
9950Sstevel@tonic-gate 		return;
9960Sstevel@tonic-gate 	case SETOP_OR:
9970Sstevel@tonic-gate 		*value |= ul;
9980Sstevel@tonic-gate 		return;
9990Sstevel@tonic-gate 	default: /* Not MOD_SET */
10000Sstevel@tonic-gate 		(*cnt)++;
10010Sstevel@tonic-gate 		return;
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * Print out the warning if multiple entries are found in the system file.
10070Sstevel@tonic-gate  */
10080Sstevel@tonic-gate static void
sysparam_print_warning(struct sysparam * sysp,u_longlong_t value)10090Sstevel@tonic-gate sysparam_print_warning(struct sysparam *sysp, u_longlong_t value)
10100Sstevel@tonic-gate {
10110Sstevel@tonic-gate 	char *modnam = sysp->sys_modnam;
10120Sstevel@tonic-gate 	char *varnam = sysp->sys_ptr;
10130Sstevel@tonic-gate 	int type = sysp->sys_type;
10140Sstevel@tonic-gate 	char *typenam = sysparam_type_to_str(type);
10150Sstevel@tonic-gate 	boolean_t str_token = ((sysp->sys_flags & SYSPARAM_STR_TOKEN) != 0);
10160Sstevel@tonic-gate 	boolean_t hex_number = ((sysp->sys_flags & SYSPARAM_HEX_TOKEN) != 0);
10170Sstevel@tonic-gate #define	warn_format1 " is set more than once in /%s. "
10180Sstevel@tonic-gate #define	warn_format2 " applied as the current setting.\n"
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	ASSERT(varnam != NULL);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (type == MOD_SET) {
10230Sstevel@tonic-gate 		/*
10240Sstevel@tonic-gate 		 * If a string token is set, print out the string
10250Sstevel@tonic-gate 		 * instead of its pointer value. In other cases,
10260Sstevel@tonic-gate 		 * print out the value with the appropriate format
10270Sstevel@tonic-gate 		 * for a hexadecimal number or a decimal number.
10280Sstevel@tonic-gate 		 */
10290Sstevel@tonic-gate 		if (modnam == NULL) {
10300Sstevel@tonic-gate 			if (str_token == B_TRUE) {
10310Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10320Sstevel@tonic-gate 				    "\"%s %s = %s\"" warn_format2,
10330Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10340Sstevel@tonic-gate 				    varnam, (char *)(uintptr_t)value);
10350Sstevel@tonic-gate 			} else if (hex_number == B_TRUE) {
10360Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10370Sstevel@tonic-gate 				    "\"%s %s = 0x%llx\"" warn_format2,
10380Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10390Sstevel@tonic-gate 				    varnam, value);
10400Sstevel@tonic-gate 			} else {
10410Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10420Sstevel@tonic-gate 				    "\"%s %s = %lld\"" warn_format2,
10430Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10440Sstevel@tonic-gate 				    varnam, value);
10450Sstevel@tonic-gate 			}
10460Sstevel@tonic-gate 		} else {
10470Sstevel@tonic-gate 			if (str_token == B_TRUE) {
10480Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10490Sstevel@tonic-gate 				    "\"%s %s:%s = %s\"" warn_format2,
10500Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10510Sstevel@tonic-gate 				    typenam, modnam, varnam,
10520Sstevel@tonic-gate 				    (char *)(uintptr_t)value);
10530Sstevel@tonic-gate 			} else if (hex_number == B_TRUE) {
10540Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10550Sstevel@tonic-gate 				    "\"%s %s:%s = 0x%llx\"" warn_format2,
10560Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10570Sstevel@tonic-gate 				    typenam, modnam, varnam, value);
10580Sstevel@tonic-gate 			} else {
10590Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10600Sstevel@tonic-gate 				    "\"%s %s:%s = %lld\"" warn_format2,
10610Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10620Sstevel@tonic-gate 				    typenam, modnam, varnam, value);
10630Sstevel@tonic-gate 			}
10640Sstevel@tonic-gate 		}
10650Sstevel@tonic-gate 	} else {
10660Sstevel@tonic-gate 		/*
10670Sstevel@tonic-gate 		 * If the type is MOD_ROOTDEV, MOD_ROOTFS, MOD_SWAPDEV,
10680Sstevel@tonic-gate 		 * MOD_SWAPFS or MOD_MODDIR, the entry is treated as
10690Sstevel@tonic-gate 		 * a duplicate one if it has the same type regardless
10700Sstevel@tonic-gate 		 * of its variable name.
10710Sstevel@tonic-gate 		 */
10720Sstevel@tonic-gate 		switch (type) {
10730Sstevel@tonic-gate 		case MOD_ROOTDEV:
10740Sstevel@tonic-gate 		case MOD_ROOTFS:
10750Sstevel@tonic-gate 		case MOD_SWAPDEV:
10760Sstevel@tonic-gate 		case MOD_SWAPFS:
10770Sstevel@tonic-gate 		case MOD_MODDIR:
10780Sstevel@tonic-gate 			cmn_err(CE_WARN, "\"%s\" appears more than once "
10790Sstevel@tonic-gate 			    "in /%s.", typenam, systemfile);
10800Sstevel@tonic-gate 			break;
10810Sstevel@tonic-gate 		default:
10820Sstevel@tonic-gate 			cmn_err(CE_NOTE, "\"%s: %s\" appears more than once "
10830Sstevel@tonic-gate 			    "in /%s.", typenam, varnam, systemfile);
10840Sstevel@tonic-gate 			break;
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate  * Process the system file commands.
10910Sstevel@tonic-gate  */
10920Sstevel@tonic-gate int
mod_sysctl(int fcn,void * p)10930Sstevel@tonic-gate mod_sysctl(int fcn, void *p)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	static char wmesg[] = "forceload of %s failed";
10960Sstevel@tonic-gate 	struct sysparam *sysp;
10970Sstevel@tonic-gate 	char *name;
10980Sstevel@tonic-gate 	struct modctl *modp;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (sysparam_hd == NULL)
11010Sstevel@tonic-gate 		return (0);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		switch (fcn) {
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		case SYS_FORCELOAD:
11080Sstevel@tonic-gate 		if (sysp->sys_type == MOD_FORCELOAD) {
11090Sstevel@tonic-gate 			name = sysp->sys_ptr;
11100Sstevel@tonic-gate 			if (modload(NULL, name) == -1)
11110Sstevel@tonic-gate 				cmn_err(CE_WARN, wmesg, name);
11120Sstevel@tonic-gate 			/*
11130Sstevel@tonic-gate 			 * The following works because it
11140Sstevel@tonic-gate 			 * runs before autounloading is started!!
11150Sstevel@tonic-gate 			 */
11160Sstevel@tonic-gate 			modp = mod_find_by_filename(NULL, name);
11170Sstevel@tonic-gate 			if (modp != NULL)
11180Sstevel@tonic-gate 				modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
11190Sstevel@tonic-gate 			/*
11200Sstevel@tonic-gate 			 * For drivers, attempt to install it.
11210Sstevel@tonic-gate 			 */
11220Sstevel@tonic-gate 			if (strncmp(sysp->sys_ptr, "drv", 3) == 0) {
11230Sstevel@tonic-gate 				(void) ddi_install_driver(name + 4);
11240Sstevel@tonic-gate 			}
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 		break;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		case SYS_SET_KVAR:
11290Sstevel@tonic-gate 		case SYS_SET_MVAR:
11300Sstevel@tonic-gate 			if (sysp->sys_type == MOD_SET)
11310Sstevel@tonic-gate 				sys_set_var(fcn, sysp, p);
11320Sstevel@tonic-gate 			break;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 		case SYS_CHECK_EXCLUDE:
11350Sstevel@tonic-gate 			if (sysp->sys_type == MOD_EXCLUDE) {
11360Sstevel@tonic-gate 				if (p == NULL || sysp->sys_ptr == NULL)
11370Sstevel@tonic-gate 					return (0);
11380Sstevel@tonic-gate 				if (strcmp((char *)p, sysp->sys_ptr) == 0)
11390Sstevel@tonic-gate 					return (1);
11400Sstevel@tonic-gate 			}
11410Sstevel@tonic-gate 		}
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	return (0);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * Process the system file commands, by type.
11490Sstevel@tonic-gate  */
11500Sstevel@tonic-gate int
mod_sysctl_type(int type,int (* func)(struct sysparam *,void *),void * p)11510Sstevel@tonic-gate mod_sysctl_type(int type, int (*func)(struct sysparam *, void *), void *p)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	struct sysparam *sysp;
11540Sstevel@tonic-gate 	int	err;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next)
11570Sstevel@tonic-gate 		if (sysp->sys_type == type)
11580Sstevel@tonic-gate 			if (err = (*(func))(sysp, p))
11590Sstevel@tonic-gate 				return (err);
11600Sstevel@tonic-gate 	return (0);
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate static char seterr[] = "Symbol %s has size of 0 in symbol table. %s";
11650Sstevel@tonic-gate static char assumption[] = "Assuming it is an 'int'";
11660Sstevel@tonic-gate static char defmsg[] = "Trying to set a variable that is of size %d";
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate static void set_int8_var(uintptr_t, struct sysparam *);
11690Sstevel@tonic-gate static void set_int16_var(uintptr_t, struct sysparam *);
11700Sstevel@tonic-gate static void set_int32_var(uintptr_t, struct sysparam *);
11710Sstevel@tonic-gate static void set_int64_var(uintptr_t, struct sysparam *);
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate static void
sys_set_var(int fcn,struct sysparam * sysp,void * p)11740Sstevel@tonic-gate sys_set_var(int fcn, struct sysparam *sysp, void *p)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	uintptr_t symaddr;
11770Sstevel@tonic-gate 	int size;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	if (fcn == SYS_SET_KVAR && sysp->sys_modnam == NULL) {
11800Sstevel@tonic-gate 		symaddr = kobj_getelfsym(sysp->sys_ptr, NULL, &size);
11810Sstevel@tonic-gate 	} else if (fcn == SYS_SET_MVAR) {
11820Sstevel@tonic-gate 		if (sysp->sys_modnam == (char *)NULL ||
11833912Slling 		    strcmp(((struct modctl *)p)->mod_modname,
11843912Slling 		    sysp->sys_modnam) != 0)
11853912Slling 			return;
11860Sstevel@tonic-gate 		symaddr = kobj_getelfsym(sysp->sys_ptr,
11870Sstevel@tonic-gate 		    ((struct modctl *)p)->mod_mp, &size);
11880Sstevel@tonic-gate 	} else
11890Sstevel@tonic-gate 		return;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (symaddr != NULL) {
11920Sstevel@tonic-gate 		switch (size) {
11930Sstevel@tonic-gate 		case 1:
11940Sstevel@tonic-gate 			set_int8_var(symaddr, sysp);
11950Sstevel@tonic-gate 			break;
11960Sstevel@tonic-gate 		case 2:
11970Sstevel@tonic-gate 			set_int16_var(symaddr, sysp);
11980Sstevel@tonic-gate 			break;
11990Sstevel@tonic-gate 		case 0:
12000Sstevel@tonic-gate 			cmn_err(CE_WARN, seterr, sysp->sys_ptr, assumption);
12010Sstevel@tonic-gate 			/*FALLTHROUGH*/
12020Sstevel@tonic-gate 		case 4:
12030Sstevel@tonic-gate 			set_int32_var(symaddr, sysp);
12040Sstevel@tonic-gate 			break;
12050Sstevel@tonic-gate 		case 8:
12060Sstevel@tonic-gate 			set_int64_var(symaddr, sysp);
12070Sstevel@tonic-gate 			break;
12080Sstevel@tonic-gate 		default:
12090Sstevel@tonic-gate 			cmn_err(CE_WARN, defmsg, size);
12100Sstevel@tonic-gate 			break;
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 	} else {
12130Sstevel@tonic-gate 		printf("sorry, variable '%s' is not defined in the '%s' ",
12140Sstevel@tonic-gate 		    sysp->sys_ptr,
12150Sstevel@tonic-gate 		    sysp->sys_modnam ? sysp->sys_modnam : "kernel");
12160Sstevel@tonic-gate 		if (sysp->sys_modnam)
12170Sstevel@tonic-gate 			printf("module");
12180Sstevel@tonic-gate 		printf("\n");
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate static void
set_int8_var(uintptr_t symaddr,struct sysparam * sysp)12230Sstevel@tonic-gate set_int8_var(uintptr_t symaddr, struct sysparam *sysp)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	uint8_t uc = (uint8_t)sysp->sys_info;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12280Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx8
12290Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
12300Sstevel@tonic-gate 		    *(uint8_t *)symaddr, sysp->sys_modnam);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	switch (sysp->sys_op) {
12330Sstevel@tonic-gate 	case SETOP_ASSIGN:
12340Sstevel@tonic-gate 		*(uint8_t *)symaddr = uc;
12350Sstevel@tonic-gate 		break;
12360Sstevel@tonic-gate 	case SETOP_AND:
12370Sstevel@tonic-gate 		*(uint8_t *)symaddr &= uc;
12380Sstevel@tonic-gate 		break;
12390Sstevel@tonic-gate 	case SETOP_OR:
12400Sstevel@tonic-gate 		*(uint8_t *)symaddr |= uc;
12410Sstevel@tonic-gate 		break;
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12450Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx8 "'.\n",
12460Sstevel@tonic-gate 		    *(uint8_t *)symaddr);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate static void
set_int16_var(uintptr_t symaddr,struct sysparam * sysp)12500Sstevel@tonic-gate set_int16_var(uintptr_t symaddr, struct sysparam *sysp)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	uint16_t us = (uint16_t)sysp->sys_info;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12550Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx16
12560Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
12570Sstevel@tonic-gate 		    *(uint16_t *)symaddr, sysp->sys_modnam);
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	switch (sysp->sys_op) {
12600Sstevel@tonic-gate 	case SETOP_ASSIGN:
12610Sstevel@tonic-gate 		*(uint16_t *)symaddr = us;
12620Sstevel@tonic-gate 		break;
12630Sstevel@tonic-gate 	case SETOP_AND:
12640Sstevel@tonic-gate 		*(uint16_t *)symaddr &= us;
12650Sstevel@tonic-gate 		break;
12660Sstevel@tonic-gate 	case SETOP_OR:
12670Sstevel@tonic-gate 		*(uint16_t *)symaddr |= us;
12680Sstevel@tonic-gate 		break;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12720Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx16 "'.\n",
12730Sstevel@tonic-gate 		    *(uint16_t *)symaddr);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate static void
set_int32_var(uintptr_t symaddr,struct sysparam * sysp)12770Sstevel@tonic-gate set_int32_var(uintptr_t symaddr, struct sysparam *sysp)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	uint32_t ui = (uint32_t)sysp->sys_info;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12820Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx32
12830Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
12840Sstevel@tonic-gate 		    *(uint32_t *)symaddr, sysp->sys_modnam);
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	switch (sysp->sys_op) {
12870Sstevel@tonic-gate 	case SETOP_ASSIGN:
12880Sstevel@tonic-gate 		*(uint32_t *)symaddr = ui;
12890Sstevel@tonic-gate 		break;
12900Sstevel@tonic-gate 	case SETOP_AND:
12910Sstevel@tonic-gate 		*(uint32_t *)symaddr &= ui;
12920Sstevel@tonic-gate 		break;
12930Sstevel@tonic-gate 	case SETOP_OR:
12940Sstevel@tonic-gate 		*(uint32_t *)symaddr |= ui;
12950Sstevel@tonic-gate 		break;
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12990Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx32 "'.\n",
13000Sstevel@tonic-gate 		    *(uint32_t *)symaddr);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate static void
set_int64_var(uintptr_t symaddr,struct sysparam * sysp)13040Sstevel@tonic-gate set_int64_var(uintptr_t symaddr, struct sysparam *sysp)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	uint64_t ul = sysp->sys_info;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13090Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx64
13100Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
13110Sstevel@tonic-gate 		    *(uint64_t *)symaddr, sysp->sys_modnam);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	switch (sysp->sys_op) {
13140Sstevel@tonic-gate 	case SETOP_ASSIGN:
13150Sstevel@tonic-gate 		*(uint64_t *)symaddr = ul;
13160Sstevel@tonic-gate 		break;
13170Sstevel@tonic-gate 	case SETOP_AND:
13180Sstevel@tonic-gate 		*(uint64_t *)symaddr &= ul;
13190Sstevel@tonic-gate 		break;
13200Sstevel@tonic-gate 	case SETOP_OR:
13210Sstevel@tonic-gate 		*(uint64_t *)symaddr |= ul;
13220Sstevel@tonic-gate 		break;
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13260Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx64 "'.\n",
13270Sstevel@tonic-gate 		    *(uint64_t *)symaddr);
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate /*
13310Sstevel@tonic-gate  * The next item on the line is a string value. Allocate memory for
13320Sstevel@tonic-gate  * it and copy the string. Return 1, and set arg ptr to newly allocated
13330Sstevel@tonic-gate  * and initialized buffer, or NULL if an error occurs.
13340Sstevel@tonic-gate  */
13350Sstevel@tonic-gate int
kobj_get_string(u_longlong_t * llptr,char * tchar)13360Sstevel@tonic-gate kobj_get_string(u_longlong_t *llptr, char *tchar)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	char *cp;
13390Sstevel@tonic-gate 	char *start = (char *)0;
13400Sstevel@tonic-gate 	int len = 0;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	len = strlen(tchar);
13430Sstevel@tonic-gate 	start = tchar;
13440Sstevel@tonic-gate 	/* copy string */
13450Sstevel@tonic-gate 	cp = vmem_alloc(mod_sysfile_arena, len + 1, VM_SLEEP);
13460Sstevel@tonic-gate 	bzero(cp, len + 1);
13470Sstevel@tonic-gate 	*llptr = (u_longlong_t)(uintptr_t)cp;
13480Sstevel@tonic-gate 	for (; len > 0; len--) {
13490Sstevel@tonic-gate 		/* convert some common escape sequences */
13500Sstevel@tonic-gate 		if (*start == '\\') {
13510Sstevel@tonic-gate 			switch (*(start + 1)) {
13520Sstevel@tonic-gate 			case 't':
13530Sstevel@tonic-gate 				/* tab */
13540Sstevel@tonic-gate 				*cp++ = '\t';
13550Sstevel@tonic-gate 				len--;
13560Sstevel@tonic-gate 				start += 2;
13570Sstevel@tonic-gate 				break;
13580Sstevel@tonic-gate 			case 'n':
13590Sstevel@tonic-gate 				/* new line */
13600Sstevel@tonic-gate 				*cp++ = '\n';
13610Sstevel@tonic-gate 				len--;
13620Sstevel@tonic-gate 				start += 2;
13630Sstevel@tonic-gate 				break;
13640Sstevel@tonic-gate 			case 'b':
13650Sstevel@tonic-gate 				/* back space */
13660Sstevel@tonic-gate 				*cp++ = '\b';
13670Sstevel@tonic-gate 				len--;
13680Sstevel@tonic-gate 				start += 2;
13690Sstevel@tonic-gate 				break;
13700Sstevel@tonic-gate 			default:
13710Sstevel@tonic-gate 				/* simply copy it */
13720Sstevel@tonic-gate 				*cp++ = *start++;
13730Sstevel@tonic-gate 				break;
13740Sstevel@tonic-gate 			}
13750Sstevel@tonic-gate 		} else
13760Sstevel@tonic-gate 			*cp++ = *start++;
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate 	*cp = '\0';
13790Sstevel@tonic-gate 	return (1);
13800Sstevel@tonic-gate }
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate  * this function frees the memory allocated by kobj_get_string
13850Sstevel@tonic-gate  */
13860Sstevel@tonic-gate void
kobj_free_string(void * ptr,int len)13870Sstevel@tonic-gate kobj_free_string(void *ptr, int len)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	vmem_free(mod_sysfile_arena, ptr, len);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate /*
13940Sstevel@tonic-gate  * get a decimal octal or hex number. Handle '~' for one's complement.
13950Sstevel@tonic-gate  */
13960Sstevel@tonic-gate int
kobj_getvalue(const char * token,u_longlong_t * valuep)13970Sstevel@tonic-gate kobj_getvalue(const char *token, u_longlong_t *valuep)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	int radix;
14000Sstevel@tonic-gate 	u_longlong_t retval = 0;
14010Sstevel@tonic-gate 	int onescompl = 0;
14020Sstevel@tonic-gate 	int negate = 0;
14030Sstevel@tonic-gate 	char c;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	if (*token == '~') {
14060Sstevel@tonic-gate 		onescompl++; /* perform one's complement on result */
14070Sstevel@tonic-gate 		token++;
14080Sstevel@tonic-gate 	} else if (*token == '-') {
14090Sstevel@tonic-gate 		negate++;
14100Sstevel@tonic-gate 		token++;
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	if (*token == '0') {
14130Sstevel@tonic-gate 		token++;
14140Sstevel@tonic-gate 		c = *token;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 		if (c == '\0') {
14170Sstevel@tonic-gate 			*valuep = 0;	/* value is 0 */
14180Sstevel@tonic-gate 			return (0);
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 		if (c == 'x' || c == 'X') {
14220Sstevel@tonic-gate 			radix = 16;
14230Sstevel@tonic-gate 			token++;
14240Sstevel@tonic-gate 		} else
14250Sstevel@tonic-gate 			radix = 8;
14260Sstevel@tonic-gate 	} else
14270Sstevel@tonic-gate 		radix = 10;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	while ((c = *token++)) {
14300Sstevel@tonic-gate 		switch (radix) {
14310Sstevel@tonic-gate 		case 8:
14320Sstevel@tonic-gate 			if (c >= '0' && c <= '7')
14330Sstevel@tonic-gate 				c -= '0';
14340Sstevel@tonic-gate 			else
14350Sstevel@tonic-gate 				return (-1);	/* invalid number */
14360Sstevel@tonic-gate 			retval = (retval << 3) + c;
14370Sstevel@tonic-gate 			break;
14380Sstevel@tonic-gate 		case 10:
14390Sstevel@tonic-gate 			if (c >= '0' && c <= '9')
14400Sstevel@tonic-gate 				c -= '0';
14410Sstevel@tonic-gate 			else
14420Sstevel@tonic-gate 				return (-1);	/* invalid number */
14430Sstevel@tonic-gate 			retval = (retval * 10) + c;
14440Sstevel@tonic-gate 			break;
14450Sstevel@tonic-gate 		case 16:
14460Sstevel@tonic-gate 			if (c >= 'a' && c <= 'f')
14470Sstevel@tonic-gate 				c = c - 'a' + 10;
14480Sstevel@tonic-gate 			else if (c >= 'A' && c <= 'F')
14490Sstevel@tonic-gate 				c = c - 'A' + 10;
14500Sstevel@tonic-gate 			else if (c >= '0' && c <= '9')
14510Sstevel@tonic-gate 				c -= '0';
14520Sstevel@tonic-gate 			else
14530Sstevel@tonic-gate 				return (-1);	/* invalid number */
14540Sstevel@tonic-gate 			retval = (retval << 4) + c;
14550Sstevel@tonic-gate 			break;
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 	if (onescompl)
14590Sstevel@tonic-gate 		retval = ~retval;
14600Sstevel@tonic-gate 	if (negate)
14610Sstevel@tonic-gate 		retval = -retval;
14620Sstevel@tonic-gate 	*valuep = retval;
14630Sstevel@tonic-gate 	return (0);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate /*
14670Sstevel@tonic-gate  * Path to the root device and root filesystem type from
14680Sstevel@tonic-gate  * property information derived from the boot subsystem
14690Sstevel@tonic-gate  */
14700Sstevel@tonic-gate void
setbootpath(char * path)14710Sstevel@tonic-gate setbootpath(char *path)
14720Sstevel@tonic-gate {
14730Sstevel@tonic-gate 	rootfs.bo_flags |= BO_VALID;
14740Sstevel@tonic-gate 	(void) copystr(path, rootfs.bo_name, BO_MAXOBJNAME, NULL);
14750Sstevel@tonic-gate 	BMDPRINTF(("rootfs bootpath: %s\n", rootfs.bo_name));
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate void
setbootfstype(char * fstype)14790Sstevel@tonic-gate setbootfstype(char *fstype)
14800Sstevel@tonic-gate {
14810Sstevel@tonic-gate 	(void) copystr(fstype, rootfs.bo_fstype, BO_MAXFSNAME, NULL);
14820Sstevel@tonic-gate 	BMDPRINTF(("rootfs fstype: %s\n", rootfs.bo_fstype));
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate /*
14860Sstevel@tonic-gate  * set parameters that can be set early during initialization.
14870Sstevel@tonic-gate  */
14880Sstevel@tonic-gate static void
setparams()14890Sstevel@tonic-gate setparams()
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	struct sysparam *sysp;
14920Sstevel@tonic-gate 	struct bootobj *bootobjp;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 		if (sysp->sys_type == MOD_MODDIR) {
14970Sstevel@tonic-gate 			default_path = sysp->sys_ptr;
14980Sstevel@tonic-gate 			continue;
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SWAPDEV ||
15020Sstevel@tonic-gate 		    sysp->sys_type == MOD_SWAPFS)
15030Sstevel@tonic-gate 			bootobjp = &swapfile;
15040Sstevel@tonic-gate 		else if (sysp->sys_type == MOD_ROOTFS)
15050Sstevel@tonic-gate 			bootobjp = &rootfs;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 		switch (sysp->sys_type) {
15080Sstevel@tonic-gate 		case MOD_ROOTDEV:
15090Sstevel@tonic-gate 			root_is_svm = 1;
15100Sstevel@tonic-gate 			(void) copystr(sysp->sys_ptr, svm_bootpath,
15110Sstevel@tonic-gate 			    BO_MAXOBJNAME, NULL);
15120Sstevel@tonic-gate 			break;
15130Sstevel@tonic-gate 		case MOD_SWAPDEV:
15140Sstevel@tonic-gate 			bootobjp->bo_flags |= BO_VALID;
15150Sstevel@tonic-gate 			(void) copystr(sysp->sys_ptr, bootobjp->bo_name,
15160Sstevel@tonic-gate 			    BO_MAXOBJNAME, NULL);
15170Sstevel@tonic-gate 			break;
15180Sstevel@tonic-gate 		case MOD_ROOTFS:
15190Sstevel@tonic-gate 		case MOD_SWAPFS:
15200Sstevel@tonic-gate 			bootobjp->bo_flags |= BO_VALID;
15210Sstevel@tonic-gate 			(void) copystr(sysp->sys_ptr, bootobjp->bo_fstype,
15220Sstevel@tonic-gate 			    BO_MAXOBJNAME, NULL);
15230Sstevel@tonic-gate 			break;
15240Sstevel@tonic-gate 		default:
15250Sstevel@tonic-gate 			break;
15260Sstevel@tonic-gate 		}
15270Sstevel@tonic-gate 	}
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * clean up after an error.
15320Sstevel@tonic-gate  */
15330Sstevel@tonic-gate static void
hwc_free(struct hwc_spec * hwcp)15340Sstevel@tonic-gate hwc_free(struct hwc_spec *hwcp)
15350Sstevel@tonic-gate {
15360Sstevel@tonic-gate 	char *name;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if ((name = hwcp->hwc_parent_name) != NULL)
15390Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15400Sstevel@tonic-gate 	if ((name = hwcp->hwc_class_name) != NULL)
15410Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15420Sstevel@tonic-gate 	if ((name = hwcp->hwc_devi_name) != NULL)
15430Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15440Sstevel@tonic-gate 	i_ddi_prop_list_delete(hwcp->hwc_devi_sys_prop_ptr);
15450Sstevel@tonic-gate 	kmem_free(hwcp, sizeof (struct hwc_spec));
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate /*
15490Sstevel@tonic-gate  * Free a list of specs
15500Sstevel@tonic-gate  */
15510Sstevel@tonic-gate void
hwc_free_spec_list(struct hwc_spec * list)15520Sstevel@tonic-gate hwc_free_spec_list(struct hwc_spec *list)
15530Sstevel@tonic-gate {
15540Sstevel@tonic-gate 	while (list) {
15550Sstevel@tonic-gate 		struct hwc_spec *tmp = list;
15560Sstevel@tonic-gate 		list = tmp->hwc_next;
15570Sstevel@tonic-gate 		hwc_free(tmp);
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate }
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate struct val_list {
15620Sstevel@tonic-gate 	struct val_list *val_next;
15632075Sprabahar 	enum {
15642075Sprabahar 		VAL_STRING,
15652075Sprabahar 		VAL_INTEGER
15662075Sprabahar 	} val_type;
15670Sstevel@tonic-gate 	int		val_size;
15680Sstevel@tonic-gate 	union {
15690Sstevel@tonic-gate 		char *string;
15700Sstevel@tonic-gate 		int integer;
15710Sstevel@tonic-gate 	} val;
15720Sstevel@tonic-gate };
15730Sstevel@tonic-gate 
15741017Sbmc static struct val_list *
add_val(struct val_list ** val_listp,struct val_list * tail,int val_type,caddr_t val)15751017Sbmc add_val(struct val_list **val_listp, struct val_list *tail,
15761017Sbmc     int val_type, caddr_t val)
15770Sstevel@tonic-gate {
15782075Sprabahar 	struct val_list *new_val;
15792075Sprabahar #ifdef DEBUG
15802075Sprabahar 	struct val_list *listp = *val_listp;
15812075Sprabahar #endif
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	new_val = kmem_alloc(sizeof (struct val_list), KM_SLEEP);
15840Sstevel@tonic-gate 	new_val->val_next = NULL;
15852075Sprabahar 	if ((new_val->val_type = val_type) == VAL_STRING) {
15860Sstevel@tonic-gate 		new_val->val_size = strlen((char *)val) + 1;
15872075Sprabahar 		new_val->val.string = kmem_alloc(new_val->val_size, KM_SLEEP);
15882075Sprabahar 		(void) strcpy(new_val->val.string, (char *)val);
15890Sstevel@tonic-gate 	} else {
15900Sstevel@tonic-gate 		new_val->val_size = sizeof (int);
15910Sstevel@tonic-gate 		new_val->val.integer = (int)(uintptr_t)val;
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 
15941017Sbmc 	ASSERT((listp == NULL && tail == NULL) ||
15951017Sbmc 	    (listp != NULL && tail != NULL));
15961017Sbmc 
15971017Sbmc 	if (tail != NULL) {
15981017Sbmc 		ASSERT(tail->val_next == NULL);
15991017Sbmc 		tail->val_next = new_val;
16000Sstevel@tonic-gate 	} else {
16010Sstevel@tonic-gate 		*val_listp = new_val;
16020Sstevel@tonic-gate 	}
16031017Sbmc 
16041017Sbmc 	return (new_val);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate 
16072075Sprabahar static void
free_val_list(struct val_list * head)16082075Sprabahar free_val_list(struct val_list *head)
16092075Sprabahar {
16102075Sprabahar 	struct val_list *tval_list;
16112075Sprabahar 
16122075Sprabahar 	for (/* CSTYLED */; head != NULL; /* CSTYLED */) {
16132075Sprabahar 		tval_list = head;
16142075Sprabahar 		head = head->val_next;
16152075Sprabahar 		if (tval_list->val_type == VAL_STRING)
16162075Sprabahar 			kmem_free(tval_list->val.string, tval_list->val_size);
16172075Sprabahar 		kmem_free(tval_list, sizeof (struct val_list));
16182075Sprabahar 	}
16192075Sprabahar }
16202075Sprabahar 
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate  * make sure there are no reserved IEEE 1275 characters (except
16230Sstevel@tonic-gate  * for uppercase characters).
16240Sstevel@tonic-gate  */
16250Sstevel@tonic-gate static int
valid_prop_name(char * name)16260Sstevel@tonic-gate valid_prop_name(char *name)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate 	int i;
16290Sstevel@tonic-gate 	int len = strlen(name);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
16320Sstevel@tonic-gate 		if (name[i] < 0x21 ||
16333912Slling 		    name[i] == '/' ||
16343912Slling 		    name[i] == '\\' ||
16353912Slling 		    name[i] == ':' ||
16363912Slling 		    name[i] == '[' ||
16373912Slling 		    name[i] == ']' ||
16383912Slling 		    name[i] == '@')
16393912Slling 			return (0);
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate 	return (1);
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate static void
make_prop(struct _buf * file,dev_info_t * devi,char * name,struct val_list * val)16450Sstevel@tonic-gate make_prop(struct _buf *file, dev_info_t *devi, char *name, struct val_list *val)
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	int propcnt = 0, val_type;
16480Sstevel@tonic-gate 	struct val_list *vl, *tvl;
16490Sstevel@tonic-gate 	caddr_t valbuf = NULL;
16500Sstevel@tonic-gate 	char **valsp;
16510Sstevel@tonic-gate 	int *valip;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	if (name == NULL)
16540Sstevel@tonic-gate 		return;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate #ifdef DEBUG
16570Sstevel@tonic-gate 	parse_debug(NULL, "%s", name);
16580Sstevel@tonic-gate #endif
16590Sstevel@tonic-gate 	if (!valid_prop_name(name)) {
16600Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid property name '%s'", name);
16610Sstevel@tonic-gate 		return;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 	if (val) {
16640Sstevel@tonic-gate 		for (vl = val, val_type = vl->val_type; vl; vl = vl->val_next) {
16650Sstevel@tonic-gate 			if (val_type != vl->val_type) {
16660Sstevel@tonic-gate 				cmn_err(CE_WARN, "Mixed types in value list");
16670Sstevel@tonic-gate 				return;
16680Sstevel@tonic-gate 			}
16690Sstevel@tonic-gate 			propcnt++;
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		vl = val;
16730Sstevel@tonic-gate 
16742075Sprabahar 		if (val_type == VAL_INTEGER) {
16750Sstevel@tonic-gate 			valip = (int *)kmem_alloc(
16760Sstevel@tonic-gate 			    (propcnt * sizeof (int)), KM_SLEEP);
16770Sstevel@tonic-gate 			valbuf = (caddr_t)valip;
16780Sstevel@tonic-gate 			while (vl) {
16790Sstevel@tonic-gate 				tvl = vl;
16800Sstevel@tonic-gate 				vl = vl->val_next;
16810Sstevel@tonic-gate #ifdef DEBUG
16820Sstevel@tonic-gate 				parse_debug(NULL, " %x",  tvl->val.integer);
16830Sstevel@tonic-gate #endif
16840Sstevel@tonic-gate 				*valip = tvl->val.integer;
16850Sstevel@tonic-gate 				valip++;
16860Sstevel@tonic-gate 			}
16870Sstevel@tonic-gate 			/* restore valip */
16880Sstevel@tonic-gate 			valip = (int *)valbuf;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			/* create the property */
16910Sstevel@tonic-gate 			if (e_ddi_prop_update_int_array(DDI_DEV_T_NONE, devi,
16920Sstevel@tonic-gate 			    name, valip, propcnt) != DDI_PROP_SUCCESS) {
16930Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
16940Sstevel@tonic-gate 				    "cannot create property %s", name);
16950Sstevel@tonic-gate 			}
16960Sstevel@tonic-gate 			/* cleanup */
16970Sstevel@tonic-gate 			kmem_free(valip, (propcnt * sizeof (int)));
16982075Sprabahar 		} else if (val_type == VAL_STRING) {
16990Sstevel@tonic-gate 			valsp = (char **)kmem_alloc(
17000Sstevel@tonic-gate 			    ((propcnt + 1) * sizeof (char *)), KM_SLEEP);
17010Sstevel@tonic-gate 			valbuf = (caddr_t)valsp;
17020Sstevel@tonic-gate 			while (vl) {
17030Sstevel@tonic-gate 				tvl = vl;
17040Sstevel@tonic-gate 				vl = vl->val_next;
17050Sstevel@tonic-gate #ifdef DEBUG
17060Sstevel@tonic-gate 				parse_debug(NULL, " %s", tvl->val.string);
17070Sstevel@tonic-gate #endif
17080Sstevel@tonic-gate 				*valsp = tvl->val.string;
17090Sstevel@tonic-gate 				valsp++;
17100Sstevel@tonic-gate 			}
17110Sstevel@tonic-gate 			/* terminate array with NULL */
17120Sstevel@tonic-gate 			*valsp = NULL;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 			/* restore valsp */
17150Sstevel@tonic-gate 			valsp = (char **)valbuf;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 			/* create the property */
17180Sstevel@tonic-gate 			if (e_ddi_prop_update_string_array(DDI_DEV_T_NONE,
17190Sstevel@tonic-gate 			    devi, name, valsp, propcnt)
17200Sstevel@tonic-gate 			    != DDI_PROP_SUCCESS) {
17210Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
17220Sstevel@tonic-gate 				    "cannot create property %s", name);
17230Sstevel@tonic-gate 			}
17240Sstevel@tonic-gate 			/* Clean up */
17250Sstevel@tonic-gate 			kmem_free(valsp, ((propcnt + 1) * sizeof (char *)));
17260Sstevel@tonic-gate 		} else {
17270Sstevel@tonic-gate 			cmn_err(CE_WARN, "Invalid property type");
17280Sstevel@tonic-gate 			return;
17290Sstevel@tonic-gate 		}
17300Sstevel@tonic-gate 	} else {
17310Sstevel@tonic-gate 		/*
17320Sstevel@tonic-gate 		 * No value was passed in with property so we will assume
17330Sstevel@tonic-gate 		 * it is a "boolean" property and create an integer
17340Sstevel@tonic-gate 		 * property with 0 value.
17350Sstevel@tonic-gate 		 */
17360Sstevel@tonic-gate #ifdef DEBUG
17370Sstevel@tonic-gate 		parse_debug(NULL, "\n");
17380Sstevel@tonic-gate #endif
17390Sstevel@tonic-gate 		if (e_ddi_prop_update_int(DDI_DEV_T_NONE, devi, name, 0)
17400Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS) {
17410Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file,
17420Sstevel@tonic-gate 			    "cannot create property %s", name);
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate static char omit_err[] = "(the ';' may have been omitted on previous spec!)";
17480Sstevel@tonic-gate static char prnt_err[] = "'parent' property already specified";
17490Sstevel@tonic-gate static char nm_err[] = "'name' property already specified";
17500Sstevel@tonic-gate static char class_err[] = "'class' property already specified";
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate typedef enum {
17530Sstevel@tonic-gate 	hwc_begin, parent, drvname, drvclass, prop,
17540Sstevel@tonic-gate 	parent_equals, name_equals, drvclass_equals,
17550Sstevel@tonic-gate 	parent_equals_string, name_equals_string,
17560Sstevel@tonic-gate 	drvclass_equals_string,
17570Sstevel@tonic-gate 	prop_equals, prop_equals_string, prop_equals_integer,
17580Sstevel@tonic-gate 	prop_equals_string_comma, prop_equals_integer_comma
17590Sstevel@tonic-gate } hwc_state_t;
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate static struct hwc_spec *
get_hwc_spec(struct _buf * file,char * tokbuf,size_t linesize)17620Sstevel@tonic-gate get_hwc_spec(struct _buf *file, char *tokbuf, size_t linesize)
17630Sstevel@tonic-gate {
17642075Sprabahar 	char *prop_name;
17650Sstevel@tonic-gate 	token_t token;
17660Sstevel@tonic-gate 	struct hwc_spec *hwcp;
17670Sstevel@tonic-gate 	struct dev_info *devi;
17681017Sbmc 	struct val_list *val_list, *tail;
17690Sstevel@tonic-gate 	hwc_state_t state;
17700Sstevel@tonic-gate 	u_longlong_t ival;
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	hwcp = kmem_zalloc(sizeof (*hwcp), KM_SLEEP);
17730Sstevel@tonic-gate 	devi = kmem_zalloc(sizeof (*devi), KM_SLEEP);
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	state = hwc_begin;
17760Sstevel@tonic-gate 	token = NAME;
17770Sstevel@tonic-gate 	prop_name = NULL;
17780Sstevel@tonic-gate 	val_list = NULL;
17791017Sbmc 	tail = NULL;
17800Sstevel@tonic-gate 	do {
17810Sstevel@tonic-gate #ifdef DEBUG
17820Sstevel@tonic-gate 		parse_debug(NULL, "state 0x%x\n", state);
17830Sstevel@tonic-gate #endif
17840Sstevel@tonic-gate 		switch (token) {
17850Sstevel@tonic-gate 		case NAME:
17860Sstevel@tonic-gate 			switch (state) {
17870Sstevel@tonic-gate 			case prop:
17880Sstevel@tonic-gate 			case prop_equals_string:
17890Sstevel@tonic-gate 			case prop_equals_integer:
17900Sstevel@tonic-gate 				make_prop(file, (dev_info_t *)devi,
17910Sstevel@tonic-gate 				    prop_name, val_list);
17922075Sprabahar 				if (prop_name) {
17932075Sprabahar 					kmem_free(prop_name,
17942075Sprabahar 					    strlen(prop_name) + 1);
17952075Sprabahar 					prop_name = NULL;
17962075Sprabahar 				}
17972075Sprabahar 				if (val_list) {
17982075Sprabahar 					free_val_list(val_list);
17992075Sprabahar 					val_list = NULL;
18002075Sprabahar 				}
18011017Sbmc 				tail = NULL;
18020Sstevel@tonic-gate 				/*FALLTHROUGH*/
18030Sstevel@tonic-gate 			case hwc_begin:
18040Sstevel@tonic-gate 				if (strcmp(tokbuf, "PARENT") == 0 ||
18050Sstevel@tonic-gate 				    strcmp(tokbuf, "parent") == 0) {
18060Sstevel@tonic-gate 					state = parent;
18070Sstevel@tonic-gate 				} else if (strcmp(tokbuf, "NAME") == 0 ||
18080Sstevel@tonic-gate 				    strcmp(tokbuf, "name") == 0) {
18090Sstevel@tonic-gate 					state = drvname;
18100Sstevel@tonic-gate 				} else if (strcmp(tokbuf, "CLASS") == 0 ||
18110Sstevel@tonic-gate 				    strcmp(tokbuf, "class") == 0) {
18120Sstevel@tonic-gate 					state = drvclass;
18130Sstevel@tonic-gate 					prop_name = kmem_alloc(strlen(tokbuf) +
18143912Slling 					    1, KM_SLEEP);
18150Sstevel@tonic-gate 					(void) strcpy(prop_name, tokbuf);
18160Sstevel@tonic-gate 				} else {
18170Sstevel@tonic-gate 					state = prop;
18180Sstevel@tonic-gate 					prop_name = kmem_alloc(strlen(tokbuf) +
18193912Slling 					    1, KM_SLEEP);
18200Sstevel@tonic-gate 					(void) strcpy(prop_name, tokbuf);
18210Sstevel@tonic-gate 				}
18220Sstevel@tonic-gate 				break;
18230Sstevel@tonic-gate 			default:
18240Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 			break;
18270Sstevel@tonic-gate 		case EQUALS:
18280Sstevel@tonic-gate 			switch (state) {
18290Sstevel@tonic-gate 			case drvname:
18300Sstevel@tonic-gate 				state = name_equals;
18310Sstevel@tonic-gate 				break;
18320Sstevel@tonic-gate 			case parent:
18330Sstevel@tonic-gate 				state = parent_equals;
18340Sstevel@tonic-gate 				break;
18350Sstevel@tonic-gate 			case drvclass:
18360Sstevel@tonic-gate 				state = drvclass_equals;
18370Sstevel@tonic-gate 				break;
18380Sstevel@tonic-gate 			case prop:
18390Sstevel@tonic-gate 				state = prop_equals;
18400Sstevel@tonic-gate 				break;
18410Sstevel@tonic-gate 			default:
18420Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
18430Sstevel@tonic-gate 			}
18440Sstevel@tonic-gate 			break;
18450Sstevel@tonic-gate 		case STRING:
18460Sstevel@tonic-gate 			switch (state) {
18470Sstevel@tonic-gate 			case name_equals:
18480Sstevel@tonic-gate 				if (ddi_get_name((dev_info_t *)devi)) {
18490Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, "%s %s",
18503912Slling 					    nm_err, omit_err);
18510Sstevel@tonic-gate 					goto bad;
18520Sstevel@tonic-gate 				}
18532075Sprabahar 				devi->devi_name = kmem_alloc(strlen(tokbuf) + 1,
18542075Sprabahar 				    KM_SLEEP);
18552075Sprabahar 				(void) strcpy(devi->devi_name, tokbuf);
18560Sstevel@tonic-gate 				state = hwc_begin;
18570Sstevel@tonic-gate 				break;
18580Sstevel@tonic-gate 			case parent_equals:
18590Sstevel@tonic-gate 				if (hwcp->hwc_parent_name) {
18600Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, "%s %s",
18613912Slling 					    prnt_err, omit_err);
18620Sstevel@tonic-gate 					goto bad;
18630Sstevel@tonic-gate 				}
18642075Sprabahar 				hwcp->hwc_parent_name = kmem_alloc(strlen
18652075Sprabahar 				    (tokbuf) + 1, KM_SLEEP);
18662075Sprabahar 				(void) strcpy(hwcp->hwc_parent_name, tokbuf);
18670Sstevel@tonic-gate 				state = hwc_begin;
18680Sstevel@tonic-gate 				break;
18690Sstevel@tonic-gate 			case drvclass_equals:
18700Sstevel@tonic-gate 				if (hwcp->hwc_class_name) {
18710Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, class_err);
18720Sstevel@tonic-gate 					goto bad;
18730Sstevel@tonic-gate 				}
18742075Sprabahar 				hwcp->hwc_class_name = kmem_alloc(
18752075Sprabahar 				    strlen(tokbuf) + 1, KM_SLEEP);
18762075Sprabahar 				(void) strcpy(hwcp->hwc_class_name, tokbuf);
18770Sstevel@tonic-gate 				/*FALLTHROUGH*/
18780Sstevel@tonic-gate 			case prop_equals:
18790Sstevel@tonic-gate 			case prop_equals_string_comma:
18802075Sprabahar 				tail = add_val(&val_list, tail, VAL_STRING,
18812075Sprabahar 				    tokbuf);
18820Sstevel@tonic-gate 				state = prop_equals_string;
18830Sstevel@tonic-gate 				break;
18840Sstevel@tonic-gate 			default:
18850Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
18860Sstevel@tonic-gate 			}
18870Sstevel@tonic-gate 			break;
18880Sstevel@tonic-gate 		case HEXVAL:
18890Sstevel@tonic-gate 		case DECVAL:
18900Sstevel@tonic-gate 			switch (state) {
18910Sstevel@tonic-gate 			case prop_equals:
18920Sstevel@tonic-gate 			case prop_equals_integer_comma:
18930Sstevel@tonic-gate 				(void) kobj_getvalue(tokbuf, &ival);
18941017Sbmc 				tail = add_val(&val_list, tail,
18952075Sprabahar 				    VAL_INTEGER, (caddr_t)(uintptr_t)ival);
18960Sstevel@tonic-gate 				state = prop_equals_integer;
18970Sstevel@tonic-gate 				break;
18980Sstevel@tonic-gate 			default:
18990Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19000Sstevel@tonic-gate 			}
19010Sstevel@tonic-gate 			break;
19020Sstevel@tonic-gate 		case COMMA:
19030Sstevel@tonic-gate 			switch (state) {
19040Sstevel@tonic-gate 			case prop_equals_string:
19050Sstevel@tonic-gate 				state = prop_equals_string_comma;
19060Sstevel@tonic-gate 				break;
19070Sstevel@tonic-gate 			case prop_equals_integer:
19080Sstevel@tonic-gate 				state = prop_equals_integer_comma;
19090Sstevel@tonic-gate 				break;
19100Sstevel@tonic-gate 			default:
19110Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19120Sstevel@tonic-gate 			}
19130Sstevel@tonic-gate 			break;
19140Sstevel@tonic-gate 		case NEWLINE:
19150Sstevel@tonic-gate 			kobj_newline(file);
19160Sstevel@tonic-gate 			break;
19170Sstevel@tonic-gate 		case POUND:
19182805Seota 			/*
19192805Seota 			 * Skip comments.
19202805Seota 			 */
19210Sstevel@tonic-gate 			kobj_find_eol(file);
19220Sstevel@tonic-gate 			break;
19230Sstevel@tonic-gate 		case EOF:
19240Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "Unexpected EOF");
19250Sstevel@tonic-gate 			goto bad;
19260Sstevel@tonic-gate 		default:
19270Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19280Sstevel@tonic-gate 			goto bad;
19290Sstevel@tonic-gate 		}
19300Sstevel@tonic-gate 	} while ((token = kobj_lex(file, tokbuf, linesize)) != SEMICOLON);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	switch (state) {
19330Sstevel@tonic-gate 	case prop:
19340Sstevel@tonic-gate 	case prop_equals_string:
19350Sstevel@tonic-gate 	case prop_equals_integer:
19360Sstevel@tonic-gate 		make_prop(file, (dev_info_t *)devi,
19373912Slling 		    prop_name, val_list);
19380Sstevel@tonic-gate 		break;
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	case hwc_begin:
19410Sstevel@tonic-gate 		break;
19420Sstevel@tonic-gate 	default:
19430Sstevel@tonic-gate 		kobj_file_err(CE_WARN, file, "Unexpected end of line");
19440Sstevel@tonic-gate 		break;
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	/* copy 2 relevant members of devi to hwcp */
19480Sstevel@tonic-gate 	hwcp->hwc_devi_sys_prop_ptr = devi->devi_sys_prop_ptr;
19490Sstevel@tonic-gate 	hwcp->hwc_devi_name = devi->devi_name;
19500Sstevel@tonic-gate 
19512075Sprabahar 	if (prop_name)
19522075Sprabahar 		kmem_free(prop_name, strlen(prop_name) + 1);
19532075Sprabahar 	if (val_list)
19542075Sprabahar 		free_val_list(val_list);
19552075Sprabahar 
19560Sstevel@tonic-gate 	kmem_free(devi, sizeof (struct dev_info));
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	return (hwcp);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate bad:
19612075Sprabahar 	if (prop_name)
19622075Sprabahar 		kmem_free(prop_name, strlen(prop_name) + 1);
19632075Sprabahar 	if (val_list)
19642075Sprabahar 		free_val_list(val_list);
19652075Sprabahar 
19662075Sprabahar 	hwc_free(hwcp);
19672075Sprabahar 
19682075Sprabahar 	if (devi->devi_name)
19692075Sprabahar 		kmem_free(devi->devi_name, strlen(devi->devi_name) + 1);
19702075Sprabahar 
19712075Sprabahar 	kmem_free(devi, sizeof (struct dev_info));
19722075Sprabahar 
19730Sstevel@tonic-gate 	return (NULL);
19740Sstevel@tonic-gate }
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate /*
19770Sstevel@tonic-gate  * This is the primary kernel interface to parse driver.conf files.
19780Sstevel@tonic-gate  *
19790Sstevel@tonic-gate  * Yet another bigstk thread handoff due to deep kernel stacks when booting
19800Sstevel@tonic-gate  * cache-only-clients.
19810Sstevel@tonic-gate  */
19820Sstevel@tonic-gate int
hwc_parse(char * fname,struct par_list ** pl,ddi_prop_t ** props)19830Sstevel@tonic-gate hwc_parse(char *fname, struct par_list **pl, ddi_prop_t **props)
19840Sstevel@tonic-gate {
19850Sstevel@tonic-gate 	int ret;
19860Sstevel@tonic-gate 	struct hwc_parse_mt *pltp = hwc_parse_mtalloc(fname, pl, props);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	if (curthread != &t0) {
19890Sstevel@tonic-gate 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
19900Sstevel@tonic-gate 		    hwc_parse_thread, pltp, 0, &p0, TS_RUN, maxclsyspri);
19910Sstevel@tonic-gate 		sema_p(&pltp->sema);
19920Sstevel@tonic-gate 	} else {
19930Sstevel@tonic-gate 		pltp->rv = hwc_parse_now(fname, pl, props);
19940Sstevel@tonic-gate 	}
19950Sstevel@tonic-gate 	ret = pltp->rv;
19960Sstevel@tonic-gate 	hwc_parse_mtfree(pltp);
19970Sstevel@tonic-gate 	return (ret);
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate /*
20010Sstevel@tonic-gate  * Calls to hwc_parse() are handled off to this routine in a separate
20020Sstevel@tonic-gate  * thread.
20030Sstevel@tonic-gate  */
20040Sstevel@tonic-gate static void
hwc_parse_thread(struct hwc_parse_mt * pltp)20050Sstevel@tonic-gate hwc_parse_thread(struct hwc_parse_mt *pltp)
20060Sstevel@tonic-gate {
20070Sstevel@tonic-gate 	kmutex_t	cpr_lk;
20080Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
20110Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "hwc_parse");
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	/*
20140Sstevel@tonic-gate 	 * load and parse the .conf file
20150Sstevel@tonic-gate 	 * return the hwc_spec list (if any) to the creator of this thread
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	pltp->rv = hwc_parse_now(pltp->name, pltp->pl, pltp->props);
20180Sstevel@tonic-gate 	sema_v(&pltp->sema);
20190Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
20200Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
20210Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
20220Sstevel@tonic-gate 	thread_exit();
20230Sstevel@tonic-gate }
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate /*
20260Sstevel@tonic-gate  * allocate and initialize a hwc_parse thread control structure
20270Sstevel@tonic-gate  */
20280Sstevel@tonic-gate static struct hwc_parse_mt *
hwc_parse_mtalloc(char * name,struct par_list ** pl,ddi_prop_t ** props)20290Sstevel@tonic-gate hwc_parse_mtalloc(char *name, struct par_list **pl, ddi_prop_t **props)
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	struct hwc_parse_mt *pltp = kmem_zalloc(sizeof (*pltp), KM_SLEEP);
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	ASSERT(name != NULL);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	pltp->name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
20360Sstevel@tonic-gate 	bcopy(name, pltp->name, strlen(name) + 1);
20370Sstevel@tonic-gate 	pltp->pl = pl;
20380Sstevel@tonic-gate 	pltp->props = props;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	sema_init(&pltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
20410Sstevel@tonic-gate 	return (pltp);
20420Sstevel@tonic-gate }
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate /*
20450Sstevel@tonic-gate  * free a hwc_parse thread control structure
20460Sstevel@tonic-gate  */
20470Sstevel@tonic-gate static void
hwc_parse_mtfree(struct hwc_parse_mt * pltp)20480Sstevel@tonic-gate hwc_parse_mtfree(struct hwc_parse_mt *pltp)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 	sema_destroy(&pltp->sema);
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 	kmem_free(pltp->name, strlen(pltp->name) + 1);
20530Sstevel@tonic-gate 	kmem_free(pltp, sizeof (*pltp));
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate /*
20570Sstevel@tonic-gate  * hwc_parse -- parse an hwconf file.  Ignore error lines and parse
20580Sstevel@tonic-gate  * as much as possible.
20590Sstevel@tonic-gate  */
20600Sstevel@tonic-gate static int
hwc_parse_now(char * fname,struct par_list ** pl,ddi_prop_t ** props)20610Sstevel@tonic-gate hwc_parse_now(char *fname, struct par_list **pl, ddi_prop_t **props)
20620Sstevel@tonic-gate {
20630Sstevel@tonic-gate 	struct _buf *file;
20640Sstevel@tonic-gate 	struct hwc_spec *hwcp;
20650Sstevel@tonic-gate 	char *tokval;
20660Sstevel@tonic-gate 	token_t token;
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	/*
20690Sstevel@tonic-gate 	 * Don't use kobj_open_path's use_moddir_suffix option, we only
20700Sstevel@tonic-gate 	 * expect to find conf files in the base module directory, not
20710Sstevel@tonic-gate 	 * an ISA-specific subdirectory.
20720Sstevel@tonic-gate 	 */
20730Sstevel@tonic-gate 	if ((file = kobj_open_path(fname, 1, 0)) == (struct _buf *)-1) {
20740Sstevel@tonic-gate 		if (moddebug & MODDEBUG_ERRMSG)
20750Sstevel@tonic-gate 			cmn_err(CE_WARN, "Cannot open %s", fname);
20760Sstevel@tonic-gate 		return (-1);
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	/*
20800Sstevel@tonic-gate 	 * Initialize variables
20810Sstevel@tonic-gate 	 */
20820Sstevel@tonic-gate 	tokval = kmem_alloc(MAX_HWC_LINESIZE, KM_SLEEP);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	while ((token = kobj_lex(file, tokval, MAX_HWC_LINESIZE)) != EOF) {
20850Sstevel@tonic-gate 		switch (token) {
20860Sstevel@tonic-gate 		case POUND:
20870Sstevel@tonic-gate 			/*
20880Sstevel@tonic-gate 			 * Skip comments.
20890Sstevel@tonic-gate 			 */
20900Sstevel@tonic-gate 			kobj_find_eol(file);
20910Sstevel@tonic-gate 			break;
20920Sstevel@tonic-gate 		case NAME:
20930Sstevel@tonic-gate 			hwcp = get_hwc_spec(file, tokval, MAX_HWC_LINESIZE);
20940Sstevel@tonic-gate 			if (hwcp == NULL)
20950Sstevel@tonic-gate 				break;
20960Sstevel@tonic-gate 			/*
20970Sstevel@tonic-gate 			 * No devi_name indicates global property.
20980Sstevel@tonic-gate 			 * Make sure parent and class not NULL.
20990Sstevel@tonic-gate 			 */
21000Sstevel@tonic-gate 			if (hwcp->hwc_devi_name == NULL) {
21010Sstevel@tonic-gate 				if (hwcp->hwc_parent_name ||
21020Sstevel@tonic-gate 				    hwcp->hwc_class_name) {
21030Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
21040Sstevel@tonic-gate 					    "missing name attribute");
21050Sstevel@tonic-gate 					hwc_free(hwcp);
21060Sstevel@tonic-gate 					continue;
21070Sstevel@tonic-gate 				}
21080Sstevel@tonic-gate 				/* Add to global property list */
21090Sstevel@tonic-gate 				add_props(hwcp, props);
21100Sstevel@tonic-gate 				break;
21110Sstevel@tonic-gate 			}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 			/*
21140Sstevel@tonic-gate 			 * This is a node spec, either parent or class
21150Sstevel@tonic-gate 			 * must be specified.
21160Sstevel@tonic-gate 			 */
21170Sstevel@tonic-gate 			if ((hwcp->hwc_parent_name == NULL) &&
21180Sstevel@tonic-gate 			    (hwcp->hwc_class_name == NULL)) {
21190Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
21200Sstevel@tonic-gate 				    "missing parent or class attribute");
21210Sstevel@tonic-gate 				hwc_free(hwcp);
21220Sstevel@tonic-gate 				continue;
21230Sstevel@tonic-gate 			}
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 			/* add to node spec list */
21260Sstevel@tonic-gate 			add_spec(hwcp, pl);
21270Sstevel@tonic-gate 			break;
21280Sstevel@tonic-gate 		case NEWLINE:
21290Sstevel@tonic-gate 			kobj_newline(file);
21300Sstevel@tonic-gate 			break;
21310Sstevel@tonic-gate 		default:
21320Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokval);
21330Sstevel@tonic-gate 			break;
21340Sstevel@tonic-gate 		}
21350Sstevel@tonic-gate 	}
21360Sstevel@tonic-gate 	/*
21370Sstevel@tonic-gate 	 * XXX - Check for clean termination.
21380Sstevel@tonic-gate 	 */
21390Sstevel@tonic-gate 	kmem_free(tokval, MAX_HWC_LINESIZE);
21400Sstevel@tonic-gate 	kobj_close_file(file);
21410Sstevel@tonic-gate 	return (0);	/* always return success */
21420Sstevel@tonic-gate }
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate void
make_aliases(struct bind ** bhash)21450Sstevel@tonic-gate make_aliases(struct bind **bhash)
21460Sstevel@tonic-gate {
21470Sstevel@tonic-gate 	enum {
21480Sstevel@tonic-gate 		AL_NEW, AL_DRVNAME, AL_DRVNAME_COMMA, AL_ALIAS, AL_ALIAS_COMMA
21490Sstevel@tonic-gate 	} state;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	struct _buf *file;
21524145Scth 	char tokbuf[MAXPATHLEN];
21534145Scth 	char drvbuf[MAXPATHLEN];
21540Sstevel@tonic-gate 	token_t token;
21550Sstevel@tonic-gate 	major_t major;
21560Sstevel@tonic-gate 	int done = 0;
21570Sstevel@tonic-gate 	static char dupwarn[] = "!Driver alias \"%s\" conflicts with "
21580Sstevel@tonic-gate 	    "an existing driver name or alias.";
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	if ((file = kobj_open_file(dafile)) == (struct _buf *)-1)
21610Sstevel@tonic-gate 		return;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	state = AL_NEW;
2164*7009Scth 	major = DDI_MAJOR_T_NONE;
21650Sstevel@tonic-gate 	while (!done) {
21660Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
21670Sstevel@tonic-gate 		switch (token) {
21680Sstevel@tonic-gate 		case POUND:
21692805Seota 			/*
21702805Seota 			 * Skip comments.
21712805Seota 			 */
21720Sstevel@tonic-gate 			kobj_find_eol(file);
21730Sstevel@tonic-gate 			break;
21740Sstevel@tonic-gate 		case NAME:
21750Sstevel@tonic-gate 		case STRING:
21760Sstevel@tonic-gate 			switch (state) {
21770Sstevel@tonic-gate 			case AL_NEW:
21780Sstevel@tonic-gate 				(void) strcpy(drvbuf, tokbuf);
21790Sstevel@tonic-gate 				state = AL_DRVNAME;
21800Sstevel@tonic-gate 				break;
21810Sstevel@tonic-gate 			case AL_DRVNAME_COMMA:
21820Sstevel@tonic-gate 				(void) strcat(drvbuf, tokbuf);
21830Sstevel@tonic-gate 				state = AL_DRVNAME;
21840Sstevel@tonic-gate 				break;
21850Sstevel@tonic-gate 			case AL_ALIAS_COMMA:
21860Sstevel@tonic-gate 				(void) strcat(drvbuf, tokbuf);
21870Sstevel@tonic-gate 				state = AL_ALIAS;
21880Sstevel@tonic-gate 				break;
21890Sstevel@tonic-gate 			case AL_DRVNAME:
21900Sstevel@tonic-gate 				major = mod_name_to_major(drvbuf);
2191*7009Scth 				if (major == DDI_MAJOR_T_NONE) {
21920Sstevel@tonic-gate 					kobj_find_eol(file);
21930Sstevel@tonic-gate 					state = AL_NEW;
21940Sstevel@tonic-gate 				} else {
21950Sstevel@tonic-gate 					(void) strcpy(drvbuf, tokbuf);
21960Sstevel@tonic-gate 					state = AL_ALIAS;
21970Sstevel@tonic-gate 				}
21980Sstevel@tonic-gate 				break;
21990Sstevel@tonic-gate 			case AL_ALIAS:
22000Sstevel@tonic-gate 				if (make_mbind(drvbuf, major, NULL, bhash)
22010Sstevel@tonic-gate 				    != 0) {
22020Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, drvbuf);
22030Sstevel@tonic-gate 				}
22042805Seota 				/*
22052805Seota 				 * copy this token just in case that there
22062805Seota 				 * are multiple names on the same line.
22072805Seota 				 */
22082805Seota 				(void) strcpy(drvbuf, tokbuf);
22090Sstevel@tonic-gate 				break;
22100Sstevel@tonic-gate 			}
22110Sstevel@tonic-gate 			break;
22120Sstevel@tonic-gate 		case COMMA:
22130Sstevel@tonic-gate 			(void) strcat(drvbuf, tokbuf);
22140Sstevel@tonic-gate 			switch (state) {
22150Sstevel@tonic-gate 			case AL_DRVNAME:
22160Sstevel@tonic-gate 				state = AL_DRVNAME_COMMA;
22170Sstevel@tonic-gate 				break;
22180Sstevel@tonic-gate 			case AL_ALIAS:
22190Sstevel@tonic-gate 				state = AL_ALIAS_COMMA;
22200Sstevel@tonic-gate 				break;
22210Sstevel@tonic-gate 			default:
22220Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
22230Sstevel@tonic-gate 			}
22240Sstevel@tonic-gate 			break;
22250Sstevel@tonic-gate 		case EOF:
22260Sstevel@tonic-gate 			done = 1;
22270Sstevel@tonic-gate 			/*FALLTHROUGH*/
22280Sstevel@tonic-gate 		case NEWLINE:
22290Sstevel@tonic-gate 			if (state == AL_ALIAS) {
22300Sstevel@tonic-gate 				if (make_mbind(drvbuf, major, NULL, bhash)
22310Sstevel@tonic-gate 				    != 0) {
22320Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, drvbuf);
22330Sstevel@tonic-gate 				}
22340Sstevel@tonic-gate 			} else if (state != AL_NEW) {
22350Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
22360Sstevel@tonic-gate 				    "Missing alias for %s", drvbuf);
22370Sstevel@tonic-gate 			}
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 			kobj_newline(file);
22400Sstevel@tonic-gate 			state = AL_NEW;
2241*7009Scth 			major = DDI_MAJOR_T_NONE;
22420Sstevel@tonic-gate 			break;
22430Sstevel@tonic-gate 		default:
22440Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 	}
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 	kobj_close_file(file);
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate /*
22530Sstevel@tonic-gate  * It is called for parsing these files:
22540Sstevel@tonic-gate  * - /etc/path_to_inst
22550Sstevel@tonic-gate  * - /etc/name_to_major
22560Sstevel@tonic-gate  * - /etc/name_to_sysnum
22570Sstevel@tonic-gate  * A callback "int (*line_parser)(char *, int, char *, struct bind **)"
22580Sstevel@tonic-gate  * is invoked for each line of the file.
22590Sstevel@tonic-gate  * The callback can inhash the entry into a hashtable by supplying
22600Sstevel@tonic-gate  * a pre-allocated hashtable in "struct bind **hashtab".
22610Sstevel@tonic-gate  */
22620Sstevel@tonic-gate int
read_binding_file(char * bindfile,struct bind ** hashtab,int (* line_parser)(char *,int,char *,struct bind **))22630Sstevel@tonic-gate read_binding_file(char *bindfile, struct bind **hashtab,
22640Sstevel@tonic-gate     int (*line_parser)(char *, int, char *, struct bind **))
22650Sstevel@tonic-gate {
22660Sstevel@tonic-gate 	enum {
22670Sstevel@tonic-gate 		B_NEW, B_NAME, B_VAL, B_BIND_NAME
22680Sstevel@tonic-gate 	} state;
22690Sstevel@tonic-gate 	struct _buf *file;
22700Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
22710Sstevel@tonic-gate 	token_t token;
22720Sstevel@tonic-gate 	int maxnum = 0;
22730Sstevel@tonic-gate 	char *bind_name = NULL, *name = NULL, *bn = NULL;
22740Sstevel@tonic-gate 	u_longlong_t val;
22750Sstevel@tonic-gate 	int done = 0;
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	static char num_err[] = "Missing number on preceding line?";
22780Sstevel@tonic-gate 	static char dupwarn[] = "!The binding file entry \"%s %u\" conflicts "
22790Sstevel@tonic-gate 	    "with a previous entry";
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (hashtab != NULL) {
22820Sstevel@tonic-gate 		clear_binding_hash(hashtab);
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	if ((file = kobj_open_file(bindfile)) == (struct _buf *)-1)
22860Sstevel@tonic-gate 		panic("read_binding_file: %s file not found", bindfile);
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	state = B_NEW;
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	while (!done) {
22910Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 		switch (token) {
22940Sstevel@tonic-gate 		case POUND:
22952805Seota 			/*
22962805Seota 			 * Skip comments.
22972805Seota 			 */
22980Sstevel@tonic-gate 			kobj_find_eol(file);
22990Sstevel@tonic-gate 			break;
23000Sstevel@tonic-gate 		case NAME:
23010Sstevel@tonic-gate 		case STRING:
23020Sstevel@tonic-gate 			switch (state) {
23030Sstevel@tonic-gate 			case B_NEW:
23040Sstevel@tonic-gate 				/*
23050Sstevel@tonic-gate 				 * This case is for the first name and
23060Sstevel@tonic-gate 				 * possibly only name in an entry.
23070Sstevel@tonic-gate 				 */
23080Sstevel@tonic-gate 				ASSERT(name == NULL);
23090Sstevel@tonic-gate 				name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
23100Sstevel@tonic-gate 				(void) strcpy(name, tokbuf);
23110Sstevel@tonic-gate 				state = B_NAME;
23120Sstevel@tonic-gate 				break;
23130Sstevel@tonic-gate 			case B_VAL:
23140Sstevel@tonic-gate 				/*
23150Sstevel@tonic-gate 				 * This case is for a second name, which
23160Sstevel@tonic-gate 				 * would be the binding name if the first
23170Sstevel@tonic-gate 				 * name was actually a generic name.
23180Sstevel@tonic-gate 				 */
23190Sstevel@tonic-gate 				ASSERT(bind_name == NULL);
23200Sstevel@tonic-gate 				bind_name = kmem_alloc(strlen(tokbuf) + 1,
23210Sstevel@tonic-gate 				    KM_SLEEP);
23220Sstevel@tonic-gate 				(void) strcpy(bind_name, tokbuf);
23230Sstevel@tonic-gate 				state = B_BIND_NAME;
23240Sstevel@tonic-gate 				break;
23250Sstevel@tonic-gate 			default:
23260Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, num_err);
23270Sstevel@tonic-gate 			}
23280Sstevel@tonic-gate 			break;
23290Sstevel@tonic-gate 		case HEXVAL:
23300Sstevel@tonic-gate 		case DECVAL:
23310Sstevel@tonic-gate 			if (state != B_NAME) {
23320Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Missing name?");
23330Sstevel@tonic-gate 				state = B_NEW;
23340Sstevel@tonic-gate 				continue;
23350Sstevel@tonic-gate 			}
23360Sstevel@tonic-gate 			(void) kobj_getvalue(tokbuf, &val);
23370Sstevel@tonic-gate 			if (val > (u_longlong_t)INT_MAX) {
23380Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
23390Sstevel@tonic-gate 				    "value %llu too large", val);
23400Sstevel@tonic-gate 				state = B_NEW;
23410Sstevel@tonic-gate 				continue;
23420Sstevel@tonic-gate 			}
23430Sstevel@tonic-gate 			state = B_VAL;
23440Sstevel@tonic-gate 			break;
23450Sstevel@tonic-gate 		case EOF:
23460Sstevel@tonic-gate 			done = 1;
23470Sstevel@tonic-gate 			/*FALLTHROUGH*/
23480Sstevel@tonic-gate 		case NEWLINE:
23490Sstevel@tonic-gate 			if ((state == B_BIND_NAME) || (state == B_VAL)) {
23500Sstevel@tonic-gate 				if (state == B_BIND_NAME)
23510Sstevel@tonic-gate 					bn = bind_name;
23520Sstevel@tonic-gate 				else
23530Sstevel@tonic-gate 					bn = NULL;
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 				if (line_parser != NULL) {
23560Sstevel@tonic-gate 					if ((*line_parser)(name, (int)val, bn,
23570Sstevel@tonic-gate 					    hashtab) == 0)
23580Sstevel@tonic-gate 						maxnum = MAX((int)val, maxnum);
23590Sstevel@tonic-gate 					else
23600Sstevel@tonic-gate 						kobj_file_err(CE_WARN, file,
23610Sstevel@tonic-gate 						    dupwarn, name, (uint_t)val);
23620Sstevel@tonic-gate 				}
23630Sstevel@tonic-gate 			} else if (state != B_NEW)
23640Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Syntax error?");
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 			if (name) {
23670Sstevel@tonic-gate 				kmem_free(name, strlen(name) + 1);
23680Sstevel@tonic-gate 				name = NULL;
23690Sstevel@tonic-gate 			}
23700Sstevel@tonic-gate 			if (bind_name) {
23710Sstevel@tonic-gate 				kmem_free(bind_name, strlen(bind_name) + 1);
23720Sstevel@tonic-gate 				bind_name = NULL;
23730Sstevel@tonic-gate 			}
23740Sstevel@tonic-gate 			state = B_NEW;
23750Sstevel@tonic-gate 			kobj_newline(file);
23760Sstevel@tonic-gate 			break;
23770Sstevel@tonic-gate 		default:
23780Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "Missing name/number?");
23790Sstevel@tonic-gate 			break;
23800Sstevel@tonic-gate 		}
23810Sstevel@tonic-gate 	}
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	ASSERT(name == NULL);		/* any leaks? */
23840Sstevel@tonic-gate 	ASSERT(bind_name == NULL);
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	kobj_close_file(file);
23870Sstevel@tonic-gate 	return (maxnum);
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate /*
23910Sstevel@tonic-gate  * read_dacf_binding_file()
23920Sstevel@tonic-gate  * 	Read the /etc/dacf.conf file and build the dacf_rule_t database from it.
23930Sstevel@tonic-gate  *
23940Sstevel@tonic-gate  * The syntax of a line in the dacf.conf file is:
23950Sstevel@tonic-gate  *   dev-spec 	[module:]op-set	operation options 	[config-args];
23960Sstevel@tonic-gate  *
23970Sstevel@tonic-gate  * Where:
23980Sstevel@tonic-gate  *   	1. dev-spec is of the format: name="data"
23990Sstevel@tonic-gate  *   	2. operation is the operation that this rule matches. (i.e. pre-detach)
24000Sstevel@tonic-gate  *   	3. options is a comma delimited list of options (i.e. debug,foobar)
24010Sstevel@tonic-gate  *   	4. config-data is a whitespace delimited list of the format: name="data"
24020Sstevel@tonic-gate  */
24030Sstevel@tonic-gate int
read_dacf_binding_file(char * filename)24040Sstevel@tonic-gate read_dacf_binding_file(char *filename)
24050Sstevel@tonic-gate {
24060Sstevel@tonic-gate 	enum {
24070Sstevel@tonic-gate 		DACF_BEGIN,
24080Sstevel@tonic-gate 		/* minor_nodetype="ddi_mouse:serial" */
24090Sstevel@tonic-gate 		DACF_NT_SPEC, DACF_NT_EQUALS, DACF_NT_DATA,
24100Sstevel@tonic-gate 		/* consconfig:mouseconfig */
24110Sstevel@tonic-gate 		DACF_MN_MODNAME, DACF_MN_COLON, DACF_MN_OPSET,
24120Sstevel@tonic-gate 		/* op */
24130Sstevel@tonic-gate 		DACF_OP_NAME,
24140Sstevel@tonic-gate 		/* [ option1, option2, option3... | - ] */
24150Sstevel@tonic-gate 		DACF_OPT_OPTION, DACF_OPT_COMMA, DACF_OPT_END,
24160Sstevel@tonic-gate 		/* argname1="argval1" argname2="argval2" ... */
24170Sstevel@tonic-gate 		DACF_OPARG_SPEC, DACF_OPARG_EQUALS, DACF_OPARG_DATA,
24180Sstevel@tonic-gate 		DACF_ERR, DACF_ERR_NEWLINE, DACF_COMMENT
24190Sstevel@tonic-gate 	} state = DACF_BEGIN;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	struct _buf *file;
24220Sstevel@tonic-gate 	char *fname;
24230Sstevel@tonic-gate 	token_t token;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
24260Sstevel@tonic-gate 	char mn_modname_buf[MAXNAMELEN], *mn_modnamep = NULL;
24270Sstevel@tonic-gate 	char mn_opset_buf[MAXNAMELEN], *mn_opsetp = NULL;
24280Sstevel@tonic-gate 	char nt_data_buf[MAXNAMELEN], *nt_datap = NULL;
24290Sstevel@tonic-gate 	char arg_spec_buf[MAXNAMELEN];
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	uint_t opts = 0;
24320Sstevel@tonic-gate 	dacf_devspec_t nt_spec_type = DACF_DS_ERROR;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	dacf_arg_t *arg_list = NULL;
24350Sstevel@tonic-gate 	dacf_opid_t opid = DACF_OPID_ERROR;
24360Sstevel@tonic-gate 	int done = 0;
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	static char w_syntax[] = "'%s' unexpected";
24390Sstevel@tonic-gate 	static char w_equals[] = "'=' is illegal in the current context";
24400Sstevel@tonic-gate 	static char w_baddevspec[] = "device specification '%s' unrecognized";
24410Sstevel@tonic-gate 	static char w_badop[] = "operation '%s' unrecognized";
24420Sstevel@tonic-gate 	static char w_badopt[] = "option '%s' unrecognized, ignoring";
24430Sstevel@tonic-gate 	static char w_newline[] = "rule is incomplete";
24440Sstevel@tonic-gate 	static char w_insert[] = "failed to register rule";
24450Sstevel@tonic-gate 	static char w_comment[] = "'#' not allowed except at start of line";
24460Sstevel@tonic-gate 	static char w_dupargs[] =
24470Sstevel@tonic-gate 	    "argument '%s' duplicates a previous argument, skipping";
24480Sstevel@tonic-gate 	static char w_nt_empty[] = "empty device specification not allowed";
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	if (filename == NULL) {
24510Sstevel@tonic-gate 		fname = dacffile;	/* default binding file */
24520Sstevel@tonic-gate 	} else {
24530Sstevel@tonic-gate 		fname = filename;	/* user specified */
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	if ((file = kobj_open_file(fname)) == (struct _buf *)-1) {
24570Sstevel@tonic-gate 		return (ENOENT);
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
24610Sstevel@tonic-gate 		printf("dacf debug: clearing rules database\n");
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
24650Sstevel@tonic-gate 	dacf_clear_rules();
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
24680Sstevel@tonic-gate 		printf("dacf debug: parsing %s\n", fname);
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	while (!done) {
24720Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 		switch (token) {
24750Sstevel@tonic-gate 		case POUND:	/* comment line */
24760Sstevel@tonic-gate 			if (state != DACF_BEGIN) {
24770Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_comment);
24780Sstevel@tonic-gate 				state = DACF_ERR;
24790Sstevel@tonic-gate 				break;
24800Sstevel@tonic-gate 			}
24810Sstevel@tonic-gate 			state = DACF_COMMENT;
24820Sstevel@tonic-gate 			kobj_find_eol(file);
24830Sstevel@tonic-gate 			break;
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 		case EQUALS:
24860Sstevel@tonic-gate 			switch (state) {
24870Sstevel@tonic-gate 			case DACF_NT_SPEC:
24880Sstevel@tonic-gate 				state = DACF_NT_EQUALS;
24890Sstevel@tonic-gate 				break;
24900Sstevel@tonic-gate 			case DACF_OPARG_SPEC:
24910Sstevel@tonic-gate 				state = DACF_OPARG_EQUALS;
24920Sstevel@tonic-gate 				break;
24930Sstevel@tonic-gate 			default:
24940Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_equals);
24950Sstevel@tonic-gate 				state = DACF_ERR;
24960Sstevel@tonic-gate 			}
24970Sstevel@tonic-gate 			break;
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 		case NAME:
25000Sstevel@tonic-gate 			switch (state) {
25010Sstevel@tonic-gate 			case DACF_BEGIN:
25020Sstevel@tonic-gate 				nt_spec_type = dacf_get_devspec(tokbuf);
25030Sstevel@tonic-gate 				if (nt_spec_type == DACF_DS_ERROR) {
25040Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
25050Sstevel@tonic-gate 					    w_baddevspec, tokbuf);
25060Sstevel@tonic-gate 					state = DACF_ERR;
25070Sstevel@tonic-gate 					break;
25080Sstevel@tonic-gate 				}
25090Sstevel@tonic-gate 				state = DACF_NT_SPEC;
25100Sstevel@tonic-gate 				break;
25110Sstevel@tonic-gate 			case DACF_NT_DATA:
25120Sstevel@tonic-gate 				(void) strncpy(mn_modname_buf, tokbuf,
25130Sstevel@tonic-gate 				    sizeof (mn_modname_buf));
25140Sstevel@tonic-gate 				mn_modnamep = mn_modname_buf;
25150Sstevel@tonic-gate 				state = DACF_MN_MODNAME;
25160Sstevel@tonic-gate 				break;
25170Sstevel@tonic-gate 			case DACF_MN_MODNAME:
25180Sstevel@tonic-gate 				/*
25190Sstevel@tonic-gate 				 * This handles the 'optional' modname.
25200Sstevel@tonic-gate 				 * What we thought was the modname is really
25210Sstevel@tonic-gate 				 * the op-set.  So it is copied over.
25220Sstevel@tonic-gate 				 */
25230Sstevel@tonic-gate 				ASSERT(mn_modnamep);
25240Sstevel@tonic-gate 				(void) strncpy(mn_opset_buf, mn_modnamep,
25250Sstevel@tonic-gate 				    sizeof (mn_opset_buf));
25260Sstevel@tonic-gate 				mn_opsetp = mn_opset_buf;
25270Sstevel@tonic-gate 				mn_modnamep = NULL;
25280Sstevel@tonic-gate 				/*
25290Sstevel@tonic-gate 				 * Now, the token we just read is the opset,
25300Sstevel@tonic-gate 				 * so look that up and fill in opid
25310Sstevel@tonic-gate 				 */
25320Sstevel@tonic-gate 				if ((opid = dacf_get_op(tokbuf)) ==
25330Sstevel@tonic-gate 				    DACF_OPID_ERROR) {
25340Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badop,
25350Sstevel@tonic-gate 					    tokbuf);
25360Sstevel@tonic-gate 					state = DACF_ERR;
25370Sstevel@tonic-gate 					break;
25380Sstevel@tonic-gate 				}
25390Sstevel@tonic-gate 				state = DACF_OP_NAME;
25400Sstevel@tonic-gate 				break;
25410Sstevel@tonic-gate 			case DACF_MN_COLON:
25420Sstevel@tonic-gate 				(void) strncpy(mn_opset_buf, tokbuf,
25430Sstevel@tonic-gate 				    sizeof (mn_opset_buf));
25440Sstevel@tonic-gate 				mn_opsetp = mn_opset_buf;
25450Sstevel@tonic-gate 				state = DACF_MN_OPSET;
25460Sstevel@tonic-gate 				break;
25470Sstevel@tonic-gate 			case DACF_MN_OPSET:
25480Sstevel@tonic-gate 				if ((opid = dacf_get_op(tokbuf)) ==
25490Sstevel@tonic-gate 				    DACF_OPID_ERROR) {
25500Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badop,
25510Sstevel@tonic-gate 					    tokbuf);
25520Sstevel@tonic-gate 					state = DACF_ERR;
25530Sstevel@tonic-gate 					break;
25540Sstevel@tonic-gate 				}
25550Sstevel@tonic-gate 				state = DACF_OP_NAME;
25560Sstevel@tonic-gate 				break;
25570Sstevel@tonic-gate 			case DACF_OP_NAME:
25580Sstevel@tonic-gate 				/*
25590Sstevel@tonic-gate 				 * This case is just like DACF_OPT_COMMA below,
25600Sstevel@tonic-gate 				 * but we check for the sole '-' argument
25610Sstevel@tonic-gate 				 */
25620Sstevel@tonic-gate 				if (strcmp(tokbuf, "-") == 0) {
25630Sstevel@tonic-gate 					state = DACF_OPT_END;
25640Sstevel@tonic-gate 					break;
25650Sstevel@tonic-gate 				}
25660Sstevel@tonic-gate 				/*FALLTHROUGH*/
25670Sstevel@tonic-gate 			case DACF_OPT_COMMA:
25680Sstevel@tonic-gate 				/*
25690Sstevel@tonic-gate 				 * figure out what option was given, but don't
25700Sstevel@tonic-gate 				 * make a federal case if invalid, just skip it
25710Sstevel@tonic-gate 				 */
25720Sstevel@tonic-gate 				if (dacf_getopt(tokbuf, &opts) != 0) {
25730Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badopt,
25740Sstevel@tonic-gate 					    tokbuf);
25750Sstevel@tonic-gate 				}
25760Sstevel@tonic-gate 				state = DACF_OPT_OPTION;
25770Sstevel@tonic-gate 				break;
25780Sstevel@tonic-gate 			case DACF_OPT_END:
25790Sstevel@tonic-gate 			case DACF_OPT_OPTION:
25800Sstevel@tonic-gate 			case DACF_OPARG_DATA:
25810Sstevel@tonic-gate 				(void) strncpy(arg_spec_buf, tokbuf,
25820Sstevel@tonic-gate 				    sizeof (arg_spec_buf));
25830Sstevel@tonic-gate 				state = DACF_OPARG_SPEC;
25840Sstevel@tonic-gate 				break;
25850Sstevel@tonic-gate 			case DACF_OPARG_EQUALS:
25860Sstevel@tonic-gate 				/*
25870Sstevel@tonic-gate 				 * Add the arg.  Warn if it's a duplicate
25880Sstevel@tonic-gate 				 */
25890Sstevel@tonic-gate 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
25900Sstevel@tonic-gate 				    tokbuf) != 0) {
25910Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_dupargs,
25920Sstevel@tonic-gate 					    arg_spec_buf);
25930Sstevel@tonic-gate 				}
25940Sstevel@tonic-gate 				state = DACF_OPARG_DATA;
25950Sstevel@tonic-gate 				break;
25960Sstevel@tonic-gate 			default:
25970Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
25980Sstevel@tonic-gate 				state = DACF_ERR;
25990Sstevel@tonic-gate 				break;
26000Sstevel@tonic-gate 			}
26010Sstevel@tonic-gate 			break;
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 		case STRING:
26040Sstevel@tonic-gate 			/*
26050Sstevel@tonic-gate 			 * We need to check to see if the string has a \n in it.
26060Sstevel@tonic-gate 			 * If so, we had an unmatched " mark error, and lex has
26070Sstevel@tonic-gate 			 * already emitted an error for us, so we need to enter
26080Sstevel@tonic-gate 			 * the error state.  Stupid lex.
26090Sstevel@tonic-gate 			 */
26100Sstevel@tonic-gate 			if (strchr(tokbuf, '\n')) {
26110Sstevel@tonic-gate 				state = DACF_ERR;
26120Sstevel@tonic-gate 				break;
26130Sstevel@tonic-gate 			}
26140Sstevel@tonic-gate 			switch (state) {
26150Sstevel@tonic-gate 			case DACF_NT_EQUALS:
26160Sstevel@tonic-gate 				if (strlen(tokbuf) == 0) {
26170Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
26180Sstevel@tonic-gate 					    w_nt_empty);
26190Sstevel@tonic-gate 					state = DACF_ERR;
26200Sstevel@tonic-gate 					break;
26210Sstevel@tonic-gate 				}
26220Sstevel@tonic-gate 				state = DACF_NT_DATA;
26230Sstevel@tonic-gate 				nt_datap = nt_data_buf;
26240Sstevel@tonic-gate 				(void) strncpy(nt_datap, tokbuf,
26250Sstevel@tonic-gate 				    sizeof (nt_data_buf));
26260Sstevel@tonic-gate 				break;
26270Sstevel@tonic-gate 			case DACF_OPARG_EQUALS:
26280Sstevel@tonic-gate 				/*
26290Sstevel@tonic-gate 				 * Add the arg.  Warn if it's a duplicate
26300Sstevel@tonic-gate 				 */
26310Sstevel@tonic-gate 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
26320Sstevel@tonic-gate 				    tokbuf) != 0) {
26330Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_dupargs,
26340Sstevel@tonic-gate 					    arg_spec_buf);
26350Sstevel@tonic-gate 				}
26360Sstevel@tonic-gate 				state = DACF_OPARG_DATA;
26370Sstevel@tonic-gate 				break;
26380Sstevel@tonic-gate 			default:
26390Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
26400Sstevel@tonic-gate 				state = DACF_ERR;
26410Sstevel@tonic-gate 				break;
26420Sstevel@tonic-gate 			}
26430Sstevel@tonic-gate 			break;
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 		case COMMA:
26460Sstevel@tonic-gate 			switch (state) {
26470Sstevel@tonic-gate 			case DACF_OPT_OPTION:
26480Sstevel@tonic-gate 				state = DACF_OPT_COMMA;
26490Sstevel@tonic-gate 				break;
26500Sstevel@tonic-gate 			default:
26510Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, ",");
26520Sstevel@tonic-gate 				state = DACF_ERR;
26530Sstevel@tonic-gate 				break;
26540Sstevel@tonic-gate 			}
26550Sstevel@tonic-gate 			break;
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 		case COLON:
26580Sstevel@tonic-gate 			if (state == DACF_MN_MODNAME)
26590Sstevel@tonic-gate 				state = DACF_MN_COLON;
26600Sstevel@tonic-gate 			else {
26610Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, ":");
26620Sstevel@tonic-gate 				state = DACF_ERR;
26630Sstevel@tonic-gate 			}
26640Sstevel@tonic-gate 			break;
26650Sstevel@tonic-gate 
26660Sstevel@tonic-gate 		case EOF:
26670Sstevel@tonic-gate 			done = 1;
26680Sstevel@tonic-gate 			/*FALLTHROUGH*/
26690Sstevel@tonic-gate 		case NEWLINE:
26700Sstevel@tonic-gate 			if (state == DACF_COMMENT || state == DACF_BEGIN) {
26710Sstevel@tonic-gate 				state = DACF_BEGIN;
26720Sstevel@tonic-gate 				kobj_newline(file);
26730Sstevel@tonic-gate 				break;
26740Sstevel@tonic-gate 			}
26750Sstevel@tonic-gate 			if ((state != DACF_OPT_OPTION) &&
26760Sstevel@tonic-gate 			    (state != DACF_OPARG_DATA) &&
26770Sstevel@tonic-gate 			    (state != DACF_OPT_END)) {
26780Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_newline);
26790Sstevel@tonic-gate 				/*
26800Sstevel@tonic-gate 				 * We can't just do DACF_ERR here, since we'll
26810Sstevel@tonic-gate 				 * wind up eating the _next_ newline if so.
26820Sstevel@tonic-gate 				 */
26830Sstevel@tonic-gate 				state = DACF_ERR_NEWLINE;
26840Sstevel@tonic-gate 				kobj_newline(file);
26850Sstevel@tonic-gate 				break;
26860Sstevel@tonic-gate 			}
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 			/*
26890Sstevel@tonic-gate 			 * insert the rule.
26900Sstevel@tonic-gate 			 */
26910Sstevel@tonic-gate 			if (dacf_rule_insert(nt_spec_type, nt_datap,
26920Sstevel@tonic-gate 			    mn_modnamep, mn_opsetp, opid, opts, arg_list) < 0) {
26930Sstevel@tonic-gate 				/*
26940Sstevel@tonic-gate 				 * We can't just do DACF_ERR here, since we'll
26950Sstevel@tonic-gate 				 * wind up eating the _next_ newline if so.
26960Sstevel@tonic-gate 				 */
26970Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_insert);
26980Sstevel@tonic-gate 				state = DACF_ERR_NEWLINE;
26990Sstevel@tonic-gate 				kobj_newline(file);
27000Sstevel@tonic-gate 				break;
27010Sstevel@tonic-gate 			}
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 			state = DACF_BEGIN;
27040Sstevel@tonic-gate 			kobj_newline(file);
27050Sstevel@tonic-gate 			break;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 		default:
27080Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
27090Sstevel@tonic-gate 			break;
27100Sstevel@tonic-gate 		} /* switch */
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 		/*
27130Sstevel@tonic-gate 		 * Clean up after ourselves, either after a line has terminated
27140Sstevel@tonic-gate 		 * successfully or because of a syntax error; or when we reach
27150Sstevel@tonic-gate 		 * EOF (remember, we may reach EOF without being 'done' with
27160Sstevel@tonic-gate 		 * handling a particular line).
27170Sstevel@tonic-gate 		 */
27180Sstevel@tonic-gate 		if (state == DACF_ERR) {
27190Sstevel@tonic-gate 			kobj_find_eol(file);
27200Sstevel@tonic-gate 		}
27210Sstevel@tonic-gate 		if ((state == DACF_BEGIN) || (state == DACF_ERR) ||
27220Sstevel@tonic-gate 		    (state == DACF_ERR_NEWLINE) || done) {
27230Sstevel@tonic-gate 			nt_datap = NULL;
27240Sstevel@tonic-gate 			mn_modnamep = mn_opsetp = NULL;
27250Sstevel@tonic-gate 			opts = 0;
27260Sstevel@tonic-gate 			opid = DACF_OPID_ERROR;
27270Sstevel@tonic-gate 			nt_spec_type = DACF_DS_ERROR;
27280Sstevel@tonic-gate 			dacf_arglist_delete(&arg_list);
27290Sstevel@tonic-gate 			state = DACF_BEGIN;
27300Sstevel@tonic-gate 		}
27310Sstevel@tonic-gate 	} /* while */
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
27340Sstevel@tonic-gate 		printf("\ndacf debug: done!\n");
27350Sstevel@tonic-gate 	}
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	kobj_close_file(file);
27400Sstevel@tonic-gate 	return (0);
27410Sstevel@tonic-gate }
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate void
lock_hw_class_list()27440Sstevel@tonic-gate lock_hw_class_list()
27450Sstevel@tonic-gate {
27460Sstevel@tonic-gate 	mutex_enter(&hcl_lock);
27470Sstevel@tonic-gate }
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate void
unlock_hw_class_list()27500Sstevel@tonic-gate unlock_hw_class_list()
27510Sstevel@tonic-gate {
27520Sstevel@tonic-gate 	mutex_exit(&hcl_lock);
27530Sstevel@tonic-gate }
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate void
add_class(char * exporter,char * class)27560Sstevel@tonic-gate add_class(char *exporter, char *class)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	struct hwc_class *hcl;
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	/*
27610Sstevel@tonic-gate 	 * If exporter's major is not registered in /etc/name_to_major,
27620Sstevel@tonic-gate 	 * don't update hwc_class, but just return here.
27630Sstevel@tonic-gate 	 */
27640Sstevel@tonic-gate 	if (ddi_name_to_major(exporter) >= devcnt) {
27650Sstevel@tonic-gate 		cmn_err(CE_WARN, "No major number for driver %s"
27663912Slling 		    " in class %s", exporter, class);
27670Sstevel@tonic-gate 		return;
27680Sstevel@tonic-gate 	}
27690Sstevel@tonic-gate 	hcl = kmem_zalloc(sizeof (struct hwc_class), KM_SLEEP);
27700Sstevel@tonic-gate 	hcl->class_exporter = kmem_alloc(strlen(exporter) + 1, KM_SLEEP);
27710Sstevel@tonic-gate 	hcl->class_name = kmem_alloc(strlen(class) + 1, KM_SLEEP);
27720Sstevel@tonic-gate 	(void) strcpy(hcl->class_exporter, exporter);
27730Sstevel@tonic-gate 	(void) strcpy(hcl->class_name, class);
27740Sstevel@tonic-gate 	lock_hw_class_list();
27750Sstevel@tonic-gate 	hcl->class_next = hcl_head;
27760Sstevel@tonic-gate 	hcl_head = hcl;
27770Sstevel@tonic-gate 	unlock_hw_class_list();
27780Sstevel@tonic-gate }
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate /*
27810Sstevel@tonic-gate  * Return the number of classes exported. If buf is not NULL, fill in
27820Sstevel@tonic-gate  * the array of the class names as well.
27830Sstevel@tonic-gate  *
27840Sstevel@tonic-gate  * Caller must hold hcl_lock to ensure the class list unmodified while
27850Sstevel@tonic-gate  * it is accessed. A typical caller will get a count first and then
27860Sstevel@tonic-gate  * allocate buf. The lock should be held by the caller.
27870Sstevel@tonic-gate  */
27880Sstevel@tonic-gate int
get_class(const char * exporter,char ** buf)27890Sstevel@tonic-gate get_class(const char *exporter, char **buf)
27900Sstevel@tonic-gate {
27910Sstevel@tonic-gate 	int n = 0;
27920Sstevel@tonic-gate 	struct hwc_class *hcl;
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	ASSERT(mutex_owned(&hcl_lock));
27950Sstevel@tonic-gate 	for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
27960Sstevel@tonic-gate 		if (strcmp(exporter, hcl->class_exporter) == 0) {
27970Sstevel@tonic-gate 			if (buf)
27980Sstevel@tonic-gate 				buf[n] = hcl->class_name;
27990Sstevel@tonic-gate 			++n;
28000Sstevel@tonic-gate 		}
28010Sstevel@tonic-gate 	}
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	return (n);
28040Sstevel@tonic-gate }
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate void
read_class_file(void)28070Sstevel@tonic-gate read_class_file(void)
28080Sstevel@tonic-gate {
28090Sstevel@tonic-gate 	struct _buf *file;
28100Sstevel@tonic-gate 	struct hwc_class *hcl, *hcl1;
28110Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
28120Sstevel@tonic-gate 	enum {
28130Sstevel@tonic-gate 		C_BEGIN, C_EXPORTER, C_END
28140Sstevel@tonic-gate 	} state;
28150Sstevel@tonic-gate 	token_t token;
28160Sstevel@tonic-gate 	int done = 0;
28170Sstevel@tonic-gate 	char *exporter = NULL, *class = NULL, *name = NULL;
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	if (hcl_head != NULL) {
28200Sstevel@tonic-gate 		hcl = hcl_head;
28210Sstevel@tonic-gate 		while (hcl != NULL) {
28220Sstevel@tonic-gate 			kmem_free(hcl->class_exporter,
28230Sstevel@tonic-gate 			    strlen(hcl->class_exporter) + 1);
28240Sstevel@tonic-gate 			hcl1 = hcl;
28250Sstevel@tonic-gate 			hcl = hcl->class_next;
28260Sstevel@tonic-gate 			kmem_free(hcl1, sizeof (struct hwc_class));
28270Sstevel@tonic-gate 		}
28280Sstevel@tonic-gate 		hcl_head = NULL;
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	if ((file = kobj_open_file(class_file)) == (struct _buf *)-1)
28320Sstevel@tonic-gate 		return;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	state = C_BEGIN;
28350Sstevel@tonic-gate 	while (!done) {
28360Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 		switch (token) {
28390Sstevel@tonic-gate 		case POUND:
28402805Seota 			/*
28412805Seota 			 * Skip comments.
28422805Seota 			 */
28430Sstevel@tonic-gate 			kobj_find_eol(file);
28440Sstevel@tonic-gate 			break;
28450Sstevel@tonic-gate 		case NAME:
28460Sstevel@tonic-gate 		case STRING:
28470Sstevel@tonic-gate 			name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
28480Sstevel@tonic-gate 			(void) strcpy(name, tokbuf);
28490Sstevel@tonic-gate 			switch (state) {
28500Sstevel@tonic-gate 			case C_BEGIN:
28510Sstevel@tonic-gate 				exporter = name;
28520Sstevel@tonic-gate 				state = C_EXPORTER;
28530Sstevel@tonic-gate 				break;
28540Sstevel@tonic-gate 			case C_EXPORTER:
28550Sstevel@tonic-gate 				class = name;
28560Sstevel@tonic-gate 				add_class(exporter, class);
28570Sstevel@tonic-gate 				state = C_END;
28580Sstevel@tonic-gate 				break;
28590Sstevel@tonic-gate 			case C_END:
28600Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
28610Sstevel@tonic-gate 				    "Extra noise after entry");
28620Sstevel@tonic-gate 				kmem_free(name, strlen(name) + 1);
28630Sstevel@tonic-gate 				kobj_find_eol(file);
28640Sstevel@tonic-gate 				break;
28650Sstevel@tonic-gate 			} /* End Switch */
28660Sstevel@tonic-gate 			break;
28670Sstevel@tonic-gate 		case EOF:
28680Sstevel@tonic-gate 			done = 1;
28690Sstevel@tonic-gate 			/*FALLTHROUGH*/
28700Sstevel@tonic-gate 		case NEWLINE:
28710Sstevel@tonic-gate 			kobj_newline(file);
28720Sstevel@tonic-gate 			if (state == C_EXPORTER)
28730Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
28740Sstevel@tonic-gate 				    "Partial entry ignored");
28750Sstevel@tonic-gate 			state = C_BEGIN;
28760Sstevel@tonic-gate 			if (exporter)
28770Sstevel@tonic-gate 				kmem_free(exporter, strlen(exporter) + 1);
28780Sstevel@tonic-gate 			if (class)
28790Sstevel@tonic-gate 				kmem_free(class, strlen(class) + 1);
28800Sstevel@tonic-gate 			exporter = NULL;
28810Sstevel@tonic-gate 			class = NULL;
28820Sstevel@tonic-gate 			break;
28830Sstevel@tonic-gate 		default:
28840Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
28850Sstevel@tonic-gate 			break;
28860Sstevel@tonic-gate 		}
28870Sstevel@tonic-gate 	}
28880Sstevel@tonic-gate 	kobj_close_file(file);
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate /*
28920Sstevel@tonic-gate  * Given par_list, get a list of parent major number
28930Sstevel@tonic-gate  */
28940Sstevel@tonic-gate int
impl_parlist_to_major(struct par_list * pl,char parents[])28950Sstevel@tonic-gate impl_parlist_to_major(struct par_list *pl, char parents[])
28960Sstevel@tonic-gate {
28970Sstevel@tonic-gate 	struct hwc_spec *hwcp;
28980Sstevel@tonic-gate 	struct hwc_class *hcl;
28990Sstevel@tonic-gate 	major_t major;
29000Sstevel@tonic-gate 	int nmajor = 0;
29010Sstevel@tonic-gate 	extern int devcnt;
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 	for (; pl != NULL; pl = pl->par_next) {
29040Sstevel@tonic-gate 		if ((pl->par_major < devcnt) && (parents[pl->par_major] == 0)) {
29050Sstevel@tonic-gate 			parents[pl->par_major] = 1;
29060Sstevel@tonic-gate 			nmajor++;
29070Sstevel@tonic-gate 			continue;
29080Sstevel@tonic-gate 		}
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 		/* parent specs cannot be mapped to a driver */
2911*7009Scth 		if (pl->par_major != DDI_MAJOR_T_NONE)
29120Sstevel@tonic-gate 			continue;
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 		/* class spec */
29150Sstevel@tonic-gate 		hwcp = pl->par_specs;
29160Sstevel@tonic-gate 		ASSERT(hwcp->hwc_class_name);
29170Sstevel@tonic-gate 		ASSERT(hwcp->hwc_parent_name == NULL);
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 		for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
29200Sstevel@tonic-gate 			if (strcmp(hwcp->hwc_class_name, hcl->class_name) != 0)
29210Sstevel@tonic-gate 				continue;
29220Sstevel@tonic-gate 			major = ddi_name_to_major(hcl->class_exporter);
2923*7009Scth 			ASSERT(major != DDI_MAJOR_T_NONE);
29240Sstevel@tonic-gate 			if (parents[major] == 0) {
29250Sstevel@tonic-gate 				parents[major] = 1;
29260Sstevel@tonic-gate 				nmajor++;
29270Sstevel@tonic-gate 			}
29280Sstevel@tonic-gate 		}
29290Sstevel@tonic-gate 	}
29300Sstevel@tonic-gate 	return (nmajor);
29310Sstevel@tonic-gate }
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate /*
29340Sstevel@tonic-gate  * delete a parent list and all its hwc specs
29350Sstevel@tonic-gate  */
29360Sstevel@tonic-gate void
impl_delete_par_list(struct par_list * pl)29370Sstevel@tonic-gate impl_delete_par_list(struct par_list *pl)
29380Sstevel@tonic-gate {
29390Sstevel@tonic-gate 	struct par_list *saved_pl;
29400Sstevel@tonic-gate 	struct hwc_spec *hp, *hp1;
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	while (pl) {
29430Sstevel@tonic-gate 		hp = pl->par_specs;
29440Sstevel@tonic-gate 		while (hp) {
29450Sstevel@tonic-gate 			hp1 = hp;
29460Sstevel@tonic-gate 			hp = hp->hwc_next;
29470Sstevel@tonic-gate 			hwc_free(hp1);
29480Sstevel@tonic-gate 		}
29490Sstevel@tonic-gate 		saved_pl = pl;
29500Sstevel@tonic-gate 		pl = pl->par_next;
29510Sstevel@tonic-gate 		kmem_free(saved_pl, sizeof (*saved_pl));
29520Sstevel@tonic-gate 	}
29530Sstevel@tonic-gate }
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate #if defined(_PSM_MODULES)
29560Sstevel@tonic-gate void
open_mach_list(void)29570Sstevel@tonic-gate open_mach_list(void)
29580Sstevel@tonic-gate {
29590Sstevel@tonic-gate 	struct _buf *file;
29600Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
29610Sstevel@tonic-gate 	token_t token;
29620Sstevel@tonic-gate 	struct psm_mach *machp;
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	if ((file = kobj_open_file(mach_file)) == (struct _buf *)-1)
29650Sstevel@tonic-gate 		return;
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	while ((token = kobj_lex(file, tokbuf, sizeof (tokbuf))) != EOF) {
29680Sstevel@tonic-gate 		switch (token) {
29690Sstevel@tonic-gate 		case POUND:
29702805Seota 			/*
29712805Seota 			 * Skip comments.
29722805Seota 			 */
29730Sstevel@tonic-gate 			kobj_find_eol(file);
29740Sstevel@tonic-gate 			break;
29750Sstevel@tonic-gate 		case NAME:
29760Sstevel@tonic-gate 		case STRING:
29770Sstevel@tonic-gate 			machp = kmem_alloc((sizeof (struct psm_mach) +
29780Sstevel@tonic-gate 			    strlen(tokbuf) + 1), KM_SLEEP);
29790Sstevel@tonic-gate 			machp->m_next = pmach_head;
29800Sstevel@tonic-gate 			machp->m_machname = (char *)(machp + 1);
29810Sstevel@tonic-gate 			(void) strcpy(machp->m_machname, tokbuf);
29820Sstevel@tonic-gate 			pmach_head = machp;
29830Sstevel@tonic-gate 			break;
29840Sstevel@tonic-gate 		case NEWLINE:
29850Sstevel@tonic-gate 			kobj_newline(file);
29860Sstevel@tonic-gate 			break;
29870Sstevel@tonic-gate 		default:
29880Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
29890Sstevel@tonic-gate 			break;
29900Sstevel@tonic-gate 		}
29910Sstevel@tonic-gate 	}
29920Sstevel@tonic-gate 	kobj_close_file(file);
29930Sstevel@tonic-gate }
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate void *
get_next_mach(void * handle,char * buf)29960Sstevel@tonic-gate get_next_mach(void *handle, char *buf)
29970Sstevel@tonic-gate {
29980Sstevel@tonic-gate 	struct psm_mach *machp;
29990Sstevel@tonic-gate 
30000Sstevel@tonic-gate 	machp = (struct psm_mach *)handle;
30010Sstevel@tonic-gate 	if (machp)
30020Sstevel@tonic-gate 		machp = machp->m_next;
30030Sstevel@tonic-gate 	else
30040Sstevel@tonic-gate 		machp = pmach_head;
30050Sstevel@tonic-gate 	if (machp)
30060Sstevel@tonic-gate 		(void) strcpy(buf, machp->m_machname);
30070Sstevel@tonic-gate 	return (machp);
30080Sstevel@tonic-gate }
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate void
close_mach_list(void)30110Sstevel@tonic-gate close_mach_list(void)
30120Sstevel@tonic-gate {
30130Sstevel@tonic-gate 	struct psm_mach *machp;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	while (pmach_head) {
30160Sstevel@tonic-gate 		machp = pmach_head;
30170Sstevel@tonic-gate 		pmach_head = machp->m_next;
30180Sstevel@tonic-gate 		kmem_free(machp, sizeof (struct psm_mach) +
30193912Slling 		    strlen(machp->m_machname) + 1);
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate }
30220Sstevel@tonic-gate #endif	/* _PSM_MODULES */
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate #if defined(_RTC_CONFIG)
30250Sstevel@tonic-gate /*
30260Sstevel@tonic-gate  * Read in the 'zone_lag' value from the rtc configuration file,
30270Sstevel@tonic-gate  * and return the value to the caller.  Note that there is other information
30280Sstevel@tonic-gate  * in this file (zone_info), so we ignore unknown values.  We do spit out
30290Sstevel@tonic-gate  * warnings if the line doesn't begin with an identifier, or if we don't find
30300Sstevel@tonic-gate  * exactly "zone_lag=value".  No one should be editing this file by hand
30310Sstevel@tonic-gate  * (use the rtc command instead), but it's better to be careful.
30320Sstevel@tonic-gate  */
30330Sstevel@tonic-gate long
process_rtc_config_file(void)30340Sstevel@tonic-gate process_rtc_config_file(void)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate 	enum {
30370Sstevel@tonic-gate 		R_NEW, R_NAME, R_EQUALS, R_VALUE
30380Sstevel@tonic-gate 	} state;
30390Sstevel@tonic-gate 	struct _buf *file;
30400Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
30410Sstevel@tonic-gate 	token_t token;
30420Sstevel@tonic-gate 	long zone_lag = 0;
30430Sstevel@tonic-gate 	u_longlong_t tmp;
30440Sstevel@tonic-gate 	int done = 0;
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 	if ((file = kobj_open_file(rtc_config_file)) == (struct _buf *)-1)
30470Sstevel@tonic-gate 		return (0);
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	state = R_NEW;
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	while (!done) {
30520Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 		switch (token) {
30550Sstevel@tonic-gate 		case POUND:
30562805Seota 			/*
30572805Seota 			 * Skip comments.
30582805Seota 			 */
30590Sstevel@tonic-gate 			kobj_find_eol(file);
30600Sstevel@tonic-gate 			break;
30610Sstevel@tonic-gate 		case NAME:
30620Sstevel@tonic-gate 		case STRING:
30630Sstevel@tonic-gate 			if (state == R_NEW) {
30640Sstevel@tonic-gate 				if (strcmp(tokbuf, "zone_lag") == 0)
30650Sstevel@tonic-gate 					state = R_NAME;
30660Sstevel@tonic-gate 				else
30670Sstevel@tonic-gate 					kobj_find_eol(file);   /* Ignore */
30680Sstevel@tonic-gate 			} else
30690Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
30700Sstevel@tonic-gate 			break;
30710Sstevel@tonic-gate 		case EQUALS:
30720Sstevel@tonic-gate 			if (state == R_NAME)
30730Sstevel@tonic-gate 				state = R_EQUALS;
30740Sstevel@tonic-gate 			else
30750Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
30760Sstevel@tonic-gate 			break;
30770Sstevel@tonic-gate 		case DECVAL:
30780Sstevel@tonic-gate 			if (state == R_EQUALS) {
30790Sstevel@tonic-gate 				if (kobj_getvalue(tokbuf, &tmp) != 0)
30800Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
30810Sstevel@tonic-gate 					    "Bad value %s for zone_lag",
30820Sstevel@tonic-gate 					    tokbuf);
30830Sstevel@tonic-gate 				else
30840Sstevel@tonic-gate 					zone_lag = (long)tmp;
30850Sstevel@tonic-gate 				state = R_VALUE;
30860Sstevel@tonic-gate 			} else
30870Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
30880Sstevel@tonic-gate 			break;
30890Sstevel@tonic-gate 		case EOF:
30900Sstevel@tonic-gate 			done = 1;
30910Sstevel@tonic-gate 			/*FALLTHROUGH*/
30920Sstevel@tonic-gate 		case NEWLINE:
30930Sstevel@tonic-gate 			if (state != R_NEW && state != R_VALUE)
30940Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
30950Sstevel@tonic-gate 				    "Partial zone_lag entry ignored");
30960Sstevel@tonic-gate 			kobj_newline(file);
30970Sstevel@tonic-gate 			state = R_NEW;
30980Sstevel@tonic-gate 			break;
30990Sstevel@tonic-gate 		default:
31000Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
31010Sstevel@tonic-gate 			break;
31020Sstevel@tonic-gate 		}
31030Sstevel@tonic-gate 	}
31040Sstevel@tonic-gate 	kobj_close_file(file);
31050Sstevel@tonic-gate 	return (zone_lag);
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate #endif /* _RTC_CONFIG */
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate /*
31110Sstevel@tonic-gate  * Append node spec to the end of par_list
31120Sstevel@tonic-gate  */
31130Sstevel@tonic-gate static void
append(struct hwc_spec * spec,struct par_list * par)31140Sstevel@tonic-gate append(struct hwc_spec *spec, struct par_list *par)
31150Sstevel@tonic-gate {
31160Sstevel@tonic-gate 	struct hwc_spec *hwc, *last;
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 	ASSERT(par->par_specs);
31190Sstevel@tonic-gate 	for (hwc = par->par_specs; hwc; hwc = hwc->hwc_next)
31200Sstevel@tonic-gate 		last = hwc;
31210Sstevel@tonic-gate 	last->hwc_next = spec;
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate /*
31250Sstevel@tonic-gate  * Given a parent=/full-pathname, see if the platform
31260Sstevel@tonic-gate  * can resolve the pathname to driver, otherwise, try
31270Sstevel@tonic-gate  * the leaf node name.
31280Sstevel@tonic-gate  */
31290Sstevel@tonic-gate static major_t
get_major(char * parent)31300Sstevel@tonic-gate get_major(char *parent)
31310Sstevel@tonic-gate {
3132*7009Scth 	major_t major = DDI_MAJOR_T_NONE;
31330Sstevel@tonic-gate 	char *tmp, *driver = NULL;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	if (*parent == '/')
31360Sstevel@tonic-gate 		major = path_to_major(parent);
31370Sstevel@tonic-gate 
3138*7009Scth 	if (major != DDI_MAJOR_T_NONE)
31390Sstevel@tonic-gate 		return (major);
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	/* extract the name between '/' and '@' */
31420Sstevel@tonic-gate 	if (*parent == '/')
31430Sstevel@tonic-gate 		driver = strrchr(parent, '/') + 1;
31440Sstevel@tonic-gate 	else
31450Sstevel@tonic-gate 		driver = parent;
31460Sstevel@tonic-gate 	if ((tmp = strchr(driver, '@')) != NULL)
31470Sstevel@tonic-gate 		*tmp = '\0';
31480Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
31490Sstevel@tonic-gate 	if (tmp)
31500Sstevel@tonic-gate 		*tmp = '@';
31510Sstevel@tonic-gate 	return (major);
31520Sstevel@tonic-gate }
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate /*
31550Sstevel@tonic-gate  * Chain together specs whose parent's module name is the same.
31560Sstevel@tonic-gate  */
31570Sstevel@tonic-gate static void
add_spec(struct hwc_spec * spec,struct par_list ** par)31580Sstevel@tonic-gate add_spec(struct hwc_spec *spec, struct par_list **par)
31590Sstevel@tonic-gate {
31600Sstevel@tonic-gate 	major_t maj;
31610Sstevel@tonic-gate 	struct par_list *pl, *par_last = NULL;
31620Sstevel@tonic-gate 	char *parent = spec->hwc_parent_name;
31636777Sjw149990 	char *class = spec->hwc_class_name;
31640Sstevel@tonic-gate 
31656777Sjw149990 	ASSERT(parent || class);
31660Sstevel@tonic-gate 
31670Sstevel@tonic-gate 	/*
31680Sstevel@tonic-gate 	 * If given a parent=/full-pathname, see if the platform
31690Sstevel@tonic-gate 	 * can resolve the pathname to driver, otherwise, try
31700Sstevel@tonic-gate 	 * the leaf node name.
31710Sstevel@tonic-gate 	 *
31720Sstevel@tonic-gate 	 * If parent=/full-pathname doesn't resolve to a driver,
31730Sstevel@tonic-gate 	 * this could be cause by DR removal of the device.
31740Sstevel@tonic-gate 	 * We put it on the major=-2 list in case the device
31750Sstevel@tonic-gate 	 * is brought back into the system by DR.
31760Sstevel@tonic-gate 	 */
31770Sstevel@tonic-gate 	if (parent) {
31780Sstevel@tonic-gate 		maj = get_major(parent);
3179*7009Scth 		if (maj == DDI_MAJOR_T_NONE) {
31800Sstevel@tonic-gate 			if ((*parent == '/') &&
31810Sstevel@tonic-gate 			    (strncmp(parent, "/pseudo", 7) != 0)) {
31820Sstevel@tonic-gate 				maj = (major_t)-2;
31830Sstevel@tonic-gate 			} else {
31840Sstevel@tonic-gate 				cmn_err(CE_WARN,
31850Sstevel@tonic-gate 				    "add_spec: No major number for %s",
31860Sstevel@tonic-gate 				    parent);
31870Sstevel@tonic-gate 				hwc_free(spec);
31880Sstevel@tonic-gate 				return;
31890Sstevel@tonic-gate 			}
31900Sstevel@tonic-gate 		}
31910Sstevel@tonic-gate 	} else
3192*7009Scth 		maj = DDI_MAJOR_T_NONE;
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	/*
31956777Sjw149990 	 * Scan the list looking for a matching parent. When parent is
31966777Sjw149990 	 * not NULL, we match the parent by major. If parent is NULL but
31976777Sjw149990 	 * class is not NULL, we mache the pl by class name.
31980Sstevel@tonic-gate 	 */
31990Sstevel@tonic-gate 	for (pl = *par; pl; pl = pl->par_next) {
32006777Sjw149990 		if ((parent && (maj == pl->par_major)) || ((parent == NULL) &&
32016777Sjw149990 		    class && pl->par_specs->hwc_class_name && (strncmp(class,
32026777Sjw149990 		    pl->par_specs->hwc_class_name, strlen(class)) == 0))) {
32030Sstevel@tonic-gate 			append(spec, pl);
32040Sstevel@tonic-gate 			return;
32050Sstevel@tonic-gate 		}
32060Sstevel@tonic-gate 		par_last = pl;
32070Sstevel@tonic-gate 	}
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	/*
32100Sstevel@tonic-gate 	 * Didn't find a match on the list.  Make a new parent list.
32110Sstevel@tonic-gate 	 */
32120Sstevel@tonic-gate 	pl = kmem_zalloc(sizeof (*pl), KM_SLEEP);
32130Sstevel@tonic-gate 	pl->par_major = maj;
32140Sstevel@tonic-gate 	pl->par_specs = spec;
32150Sstevel@tonic-gate 	if (*par == NULL) {	/* null par list */
32160Sstevel@tonic-gate 		*par = pl;
32170Sstevel@tonic-gate 		return;
32180Sstevel@tonic-gate 	}
32190Sstevel@tonic-gate 	/* put "class=" entries last (lower pri if dups) */
3220*7009Scth 	if (maj == DDI_MAJOR_T_NONE) {
32210Sstevel@tonic-gate 		par_last->par_next = pl;
32220Sstevel@tonic-gate 		return;
32230Sstevel@tonic-gate 	}
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	/* ensure unresolved "parent=/full-path" goes first */
32260Sstevel@tonic-gate 	if ((maj != (major_t)-2) && ((*par)->par_major == (major_t)-2))
32270Sstevel@tonic-gate 		par = &(*par)->par_next;
32280Sstevel@tonic-gate 	pl->par_next = *par;
32290Sstevel@tonic-gate 	*par = pl;
32300Sstevel@tonic-gate }
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate /*
32330Sstevel@tonic-gate  * Add property spec to property list in original order
32340Sstevel@tonic-gate  */
32350Sstevel@tonic-gate static void
add_props(struct hwc_spec * spec,ddi_prop_t ** props)32360Sstevel@tonic-gate add_props(struct hwc_spec *spec, ddi_prop_t **props)
32370Sstevel@tonic-gate {
32380Sstevel@tonic-gate 	ASSERT(spec->hwc_devi_name == NULL);
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate 	if (spec->hwc_devi_sys_prop_ptr) {
32410Sstevel@tonic-gate 		while (*props)
32420Sstevel@tonic-gate 			props = &(*props)->prop_next;
32430Sstevel@tonic-gate 		*props = spec->hwc_devi_sys_prop_ptr;
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 		/* remove these properties from the spec */
32460Sstevel@tonic-gate 		spec->hwc_devi_sys_prop_ptr = NULL;
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 	hwc_free(spec);
32490Sstevel@tonic-gate }
3250