xref: /illumos-gate/usr/src/uts/common/os/modsysfile.c (revision eb9a1df2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215f10ef69SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
235c44817cSjw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
252b987d42SAlexander Eremin  * Copyright 2017 Nexenta Systems, Inc.
268eb8717cSJohn Levon  * Copyright 2019 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/user.h>
347c478bd9Sstevel@tonic-gate #include <sys/disp.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
447c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
457c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
487c478bd9Sstevel@tonic-gate #include <sys/kobj_lex.h>
497c478bd9Sstevel@tonic-gate #include <sys/errno.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
527c478bd9Sstevel@tonic-gate #include <sys/callb.h>
537c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
547c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
557c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate struct hwc_class *hcl_head;	/* head of list of classes */
587c478bd9Sstevel@tonic-gate static kmutex_t hcl_lock;	/* for accessing list of classes */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	DAFILE		"/etc/driver_aliases"
61*eb9a1df2SHans Rosenfeld #define	PPTFILE		"/etc/ppt_aliases"
627c478bd9Sstevel@tonic-gate #define	CLASSFILE	"/etc/driver_classes"
637c478bd9Sstevel@tonic-gate #define	DACFFILE	"/etc/dacf.conf"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char class_file[] = CLASSFILE;
66*eb9a1df2SHans Rosenfeld static char pptfile[] = PPTFILE;
677c478bd9Sstevel@tonic-gate static char dafile[] = DAFILE;
687c478bd9Sstevel@tonic-gate static char dacffile[] = DACFFILE;
697c478bd9Sstevel@tonic-gate 
702b987d42SAlexander Eremin char *self_assembly = "/etc/system.d/.self-assembly";
71986fd29aSsetje char *systemfile = "/etc/system";	/* name of ascii system file */
727c478bd9Sstevel@tonic-gate 
738eb8717cSJohn Levon #define	BUILDVERSION_LEN (4096)
748eb8717cSJohn Levon 
758eb8717cSJohn Levon char *versionfile = "/etc/versions/build";
768eb8717cSJohn Levon char buildversion[BUILDVERSION_LEN];
778eb8717cSJohn Levon 
787c478bd9Sstevel@tonic-gate static struct sysparam *sysparam_hd;	/* head of parameters list */
797c478bd9Sstevel@tonic-gate static struct sysparam *sysparam_tl;	/* tail of parameters list */
807c478bd9Sstevel@tonic-gate static vmem_t *mod_sysfile_arena;	/* parser memory */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate char obp_bootpath[BO_MAXOBJNAME];	/* bootpath from obp */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #if defined(_PSM_MODULES)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate struct psm_mach {
877c478bd9Sstevel@tonic-gate 	struct psm_mach *m_next;
887c478bd9Sstevel@tonic-gate 	char		*m_machname;
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static struct psm_mach *pmach_head;	/* head of list of classes */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	MACHFILE	"/etc/mach"
947c478bd9Sstevel@tonic-gate static char mach_file[] = MACHFILE;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #endif	/* _PSM_MODULES */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #if defined(_RTC_CONFIG)
997c478bd9Sstevel@tonic-gate static char rtc_config_file[] = "/etc/rtc_config";
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static void sys_set_var(int, struct sysparam *, void *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static void setparams(void);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * driver.conf parse thread control structure
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate struct hwc_parse_mt {
1107c478bd9Sstevel@tonic-gate 	ksema_t		sema;
1117c478bd9Sstevel@tonic-gate 	char		*name;		/* name of .conf files */
1127c478bd9Sstevel@tonic-gate 	struct par_list	**pl;		/* parsed parent list */
1137c478bd9Sstevel@tonic-gate 	ddi_prop_t	**props;	/* parsed properties */
1147c478bd9Sstevel@tonic-gate 	int		rv;		/* return value */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static int hwc_parse_now(char *, struct par_list **, ddi_prop_t **);
1187c478bd9Sstevel@tonic-gate static void hwc_parse_thread(struct hwc_parse_mt *);
1197c478bd9Sstevel@tonic-gate static struct hwc_parse_mt *hwc_parse_mtalloc(char *, struct par_list **,
1207c478bd9Sstevel@tonic-gate 	ddi_prop_t **);
1217c478bd9Sstevel@tonic-gate static void hwc_parse_mtfree(struct hwc_parse_mt *);
1227c478bd9Sstevel@tonic-gate static void add_spec(struct hwc_spec *, struct par_list **);
1237c478bd9Sstevel@tonic-gate static void add_props(struct hwc_spec *, ddi_prop_t **);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void check_system_file(void);
1267c478bd9Sstevel@tonic-gate static int sysparam_compare_entry(struct sysparam *, struct sysparam *);
1277c478bd9Sstevel@tonic-gate static char *sysparam_type_to_str(int);
1287c478bd9Sstevel@tonic-gate static void sysparam_count_entry(struct sysparam *, int *, u_longlong_t *);
1297c478bd9Sstevel@tonic-gate static void sysparam_print_warning(struct sysparam *, u_longlong_t);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #ifdef DEBUG
1327c478bd9Sstevel@tonic-gate static int parse_debug_on = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*VARARGS1*/
1357c478bd9Sstevel@tonic-gate static void
parse_debug(struct _buf * file,char * fmt,...)1367c478bd9Sstevel@tonic-gate parse_debug(struct _buf *file, char *fmt, ...)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	va_list adx;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (parse_debug_on) {
1417c478bd9Sstevel@tonic-gate 		va_start(adx, fmt);
1427c478bd9Sstevel@tonic-gate 		vprintf(fmt, adx);
1437c478bd9Sstevel@tonic-gate 		if (file)
1447c478bd9Sstevel@tonic-gate 			printf(" on line %d of %s\n", kobj_linenum(file),
145b1b8ab34Slling 			    kobj_filename(file));
1467c478bd9Sstevel@tonic-gate 		va_end(adx);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define	FE_BUFLEN 256
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
1547c478bd9Sstevel@tonic-gate void
kobj_file_err(int type,struct _buf * file,char * fmt,...)1557c478bd9Sstevel@tonic-gate kobj_file_err(int type,  struct _buf *file, char *fmt, ...)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	va_list ap;
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * If we're in trouble, we might be short on stack... be paranoid
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1627c478bd9Sstevel@tonic-gate 	char *trailer = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1637c478bd9Sstevel@tonic-gate 	char *fmt_str = kmem_alloc(FE_BUFLEN, KM_SLEEP);
1647c478bd9Sstevel@tonic-gate 	char prefix = '\0';
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1677c478bd9Sstevel@tonic-gate 	if (strchr("^!?", fmt[0]) != NULL) {
1687c478bd9Sstevel@tonic-gate 		prefix = fmt[0];
1697c478bd9Sstevel@tonic-gate 		fmt++;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, FE_BUFLEN, fmt, ap);
1727c478bd9Sstevel@tonic-gate 	va_end(ap);
1737c478bd9Sstevel@tonic-gate 	(void) snprintf(trailer, FE_BUFLEN, " on line %d of %s",
1747c478bd9Sstevel@tonic-gate 	    kobj_linenum(file), kobj_filename(file));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * If prefixed with !^?, prepend that character
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	if (prefix != '\0') {
1807c478bd9Sstevel@tonic-gate 		(void) snprintf(fmt_str, FE_BUFLEN, "%c%%s%%s", prefix);
1817c478bd9Sstevel@tonic-gate 	} else {
1827c478bd9Sstevel@tonic-gate 		(void) strncpy(fmt_str, "%s%s", FE_BUFLEN);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	cmn_err(type, fmt_str, buf, trailer);
1867c478bd9Sstevel@tonic-gate 	kmem_free(buf, FE_BUFLEN);
1877c478bd9Sstevel@tonic-gate 	kmem_free(trailer, FE_BUFLEN);
1887c478bd9Sstevel@tonic-gate 	kmem_free(fmt_str, FE_BUFLEN);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #ifdef DEBUG
1927c478bd9Sstevel@tonic-gate char *tokennames[] = {
19368ae3684Seota 	"UNEXPECTED",
1947c478bd9Sstevel@tonic-gate 	"EQUALS",
1957c478bd9Sstevel@tonic-gate 	"AMPERSAND",
1967c478bd9Sstevel@tonic-gate 	"BIT_OR",
1977c478bd9Sstevel@tonic-gate 	"STAR",
1987c478bd9Sstevel@tonic-gate 	"POUND",
1997c478bd9Sstevel@tonic-gate 	"COLON",
2007c478bd9Sstevel@tonic-gate 	"SEMICOLON",
2017c478bd9Sstevel@tonic-gate 	"COMMA",
2027c478bd9Sstevel@tonic-gate 	"SLASH",
2037c478bd9Sstevel@tonic-gate 	"WHITE_SPACE",
2047c478bd9Sstevel@tonic-gate 	"NEWLINE",
2057c478bd9Sstevel@tonic-gate 	"EOF",
2067c478bd9Sstevel@tonic-gate 	"STRING",
2077c478bd9Sstevel@tonic-gate 	"HEXVAL",
2087c478bd9Sstevel@tonic-gate 	"DECVAL",
2097c478bd9Sstevel@tonic-gate 	"NAME"
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate token_t
kobj_lex(struct _buf * file,char * val,size_t size)2147c478bd9Sstevel@tonic-gate kobj_lex(struct _buf *file, char *val, size_t size)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	char	*cp;
2177c478bd9Sstevel@tonic-gate 	int	ch, oval, badquote;
2187c478bd9Sstevel@tonic-gate 	size_t	remain;
21968ae3684Seota 	token_t token = UNEXPECTED;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (size < 2)
22268ae3684Seota 		return (token);	/* this token is UNEXPECTED */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	cp = val;
2257c478bd9Sstevel@tonic-gate 	while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
2267c478bd9Sstevel@tonic-gate 		;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	remain = size - 1;
2297c478bd9Sstevel@tonic-gate 	*cp++ = (char)ch;
2307c478bd9Sstevel@tonic-gate 	switch (ch) {
2317c478bd9Sstevel@tonic-gate 	case '=':
2327c478bd9Sstevel@tonic-gate 		token = EQUALS;
2337c478bd9Sstevel@tonic-gate 		break;
2347c478bd9Sstevel@tonic-gate 	case '&':
2357c478bd9Sstevel@tonic-gate 		token = AMPERSAND;
2367c478bd9Sstevel@tonic-gate 		break;
2377c478bd9Sstevel@tonic-gate 	case '|':
2387c478bd9Sstevel@tonic-gate 		token = BIT_OR;
2397c478bd9Sstevel@tonic-gate 		break;
2407c478bd9Sstevel@tonic-gate 	case '*':
2417c478bd9Sstevel@tonic-gate 		token = STAR;
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 	case '#':
2447c478bd9Sstevel@tonic-gate 		token = POUND;
2457c478bd9Sstevel@tonic-gate 		break;
2467c478bd9Sstevel@tonic-gate 	case ':':
2477c478bd9Sstevel@tonic-gate 		token = COLON;
2487c478bd9Sstevel@tonic-gate 		break;
2497c478bd9Sstevel@tonic-gate 	case ';':
2507c478bd9Sstevel@tonic-gate 		token = SEMICOLON;
2517c478bd9Sstevel@tonic-gate 		break;
2527c478bd9Sstevel@tonic-gate 	case ',':
2537c478bd9Sstevel@tonic-gate 		token = COMMA;
2547c478bd9Sstevel@tonic-gate 		break;
2557c478bd9Sstevel@tonic-gate 	case '/':
2567c478bd9Sstevel@tonic-gate 		token = SLASH;
2577c478bd9Sstevel@tonic-gate 		break;
2587c478bd9Sstevel@tonic-gate 	case ' ':
2597c478bd9Sstevel@tonic-gate 	case '\t':
2607c478bd9Sstevel@tonic-gate 	case '\f':
2617c478bd9Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) == ' ' ||
2627c478bd9Sstevel@tonic-gate 		    ch == '\t' || ch == '\f') {
2637c478bd9Sstevel@tonic-gate 			if (--remain == 0) {
26468ae3684Seota 				token = UNEXPECTED;
26568ae3684Seota 				goto out;
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			*cp++ = (char)ch;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		(void) kobj_ungetc(file);
2707c478bd9Sstevel@tonic-gate 		token = WHITE_SPACE;
2717c478bd9Sstevel@tonic-gate 		break;
2727c478bd9Sstevel@tonic-gate 	case '\n':
2737c478bd9Sstevel@tonic-gate 	case '\r':
2747c478bd9Sstevel@tonic-gate 		token = NEWLINE;
2757c478bd9Sstevel@tonic-gate 		break;
2767c478bd9Sstevel@tonic-gate 	case '"':
2777c478bd9Sstevel@tonic-gate 		remain++;
2787c478bd9Sstevel@tonic-gate 		cp--;
2797c478bd9Sstevel@tonic-gate 		badquote = 0;
2807c478bd9Sstevel@tonic-gate 		while (!badquote && (ch  = kobj_getc(file)) != '"') {
2817c478bd9Sstevel@tonic-gate 			switch (ch) {
2827c478bd9Sstevel@tonic-gate 			case '\n':
2837c478bd9Sstevel@tonic-gate 			case -1:
2847c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Missing \"");
2857c478bd9Sstevel@tonic-gate 				remain = size - 1;
2867c478bd9Sstevel@tonic-gate 				cp = val;
2877c478bd9Sstevel@tonic-gate 				*cp++ = '\n';
2887c478bd9Sstevel@tonic-gate 				badquote = 1;
2897c478bd9Sstevel@tonic-gate 				/* since we consumed the newline/EOF */
2907c478bd9Sstevel@tonic-gate 				(void) kobj_ungetc(file);
2917c478bd9Sstevel@tonic-gate 				break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			case '\\':
2947c478bd9Sstevel@tonic-gate 				if (--remain == 0) {
29568ae3684Seota 					token = UNEXPECTED;
29668ae3684Seota 					goto out;
2977c478bd9Sstevel@tonic-gate 				}
2987c478bd9Sstevel@tonic-gate 				ch = (char)kobj_getc(file);
2997c478bd9Sstevel@tonic-gate 				if (!isdigit(ch)) {
3007c478bd9Sstevel@tonic-gate 					/* escape the character */
3017c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
3027c478bd9Sstevel@tonic-gate 					break;
3037c478bd9Sstevel@tonic-gate 				}
3047c478bd9Sstevel@tonic-gate 				oval = 0;
3057c478bd9Sstevel@tonic-gate 				while (ch >= '0' && ch <= '7') {
3067c478bd9Sstevel@tonic-gate 					ch -= '0';
3077c478bd9Sstevel@tonic-gate 					oval = (oval << 3) + ch;
3087c478bd9Sstevel@tonic-gate 					ch = (char)kobj_getc(file);
3097c478bd9Sstevel@tonic-gate 				}
3107c478bd9Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3117c478bd9Sstevel@tonic-gate 				/* check for character overflow? */
3127c478bd9Sstevel@tonic-gate 				if (oval > 127) {
3137c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
3147c478bd9Sstevel@tonic-gate 					    "Character "
3157c478bd9Sstevel@tonic-gate 					    "overflow detected.");
3167c478bd9Sstevel@tonic-gate 				}
3177c478bd9Sstevel@tonic-gate 				*cp++ = (char)oval;
3187c478bd9Sstevel@tonic-gate 				break;
3197c478bd9Sstevel@tonic-gate 			default:
3207c478bd9Sstevel@tonic-gate 				if (--remain == 0) {
32168ae3684Seota 					token = UNEXPECTED;
32268ae3684Seota 					goto out;
3237c478bd9Sstevel@tonic-gate 				}
3247c478bd9Sstevel@tonic-gate 				*cp++ = (char)ch;
3257c478bd9Sstevel@tonic-gate 				break;
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		token = STRING;
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	case -1:
3327c478bd9Sstevel@tonic-gate 		token = EOF;
3337c478bd9Sstevel@tonic-gate 		break;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	default:
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		 * detect a lone '-' (including at the end of a line), and
3387c478bd9Sstevel@tonic-gate 		 * identify it as a 'name'
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if (ch == '-') {
3417c478bd9Sstevel@tonic-gate 			if (--remain == 0) {
34268ae3684Seota 				token = UNEXPECTED;
34368ae3684Seota 				goto out;
3447c478bd9Sstevel@tonic-gate 			}
3457c478bd9Sstevel@tonic-gate 			*cp++ = (char)(ch = kobj_getc(file));
3467c478bd9Sstevel@tonic-gate 			if (iswhite(ch) || (ch == '\n')) {
3477c478bd9Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3487c478bd9Sstevel@tonic-gate 				remain++;
3497c478bd9Sstevel@tonic-gate 				cp--;
3507c478bd9Sstevel@tonic-gate 				token = NAME;
3517c478bd9Sstevel@tonic-gate 				break;
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 		} else if (isunary(ch)) {
3547c478bd9Sstevel@tonic-gate 			if (--remain == 0) {
35568ae3684Seota 				token = UNEXPECTED;
35668ae3684Seota 				goto out;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			*cp++ = (char)(ch = kobj_getc(file));
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		if (isdigit(ch)) {
3637c478bd9Sstevel@tonic-gate 			if (ch == '0') {
3647c478bd9Sstevel@tonic-gate 				if ((ch = kobj_getc(file)) == 'x') {
3657c478bd9Sstevel@tonic-gate 					if (--remain == 0) {
36668ae3684Seota 						token = UNEXPECTED;
36768ae3684Seota 						goto out;
3687c478bd9Sstevel@tonic-gate 					}
3697c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
3707c478bd9Sstevel@tonic-gate 					ch = kobj_getc(file);
3717c478bd9Sstevel@tonic-gate 					while (isxdigit(ch)) {
3727c478bd9Sstevel@tonic-gate 						if (--remain == 0) {
37368ae3684Seota 							token = UNEXPECTED;
37468ae3684Seota 							goto out;
3757c478bd9Sstevel@tonic-gate 						}
3767c478bd9Sstevel@tonic-gate 						*cp++ = (char)ch;
3777c478bd9Sstevel@tonic-gate 						ch = kobj_getc(file);
3787c478bd9Sstevel@tonic-gate 					}
3797c478bd9Sstevel@tonic-gate 					(void) kobj_ungetc(file);
3807c478bd9Sstevel@tonic-gate 					token = HEXVAL;
3817c478bd9Sstevel@tonic-gate 				} else {
3827c478bd9Sstevel@tonic-gate 					goto digit;
3837c478bd9Sstevel@tonic-gate 				}
3847c478bd9Sstevel@tonic-gate 			} else {
3857c478bd9Sstevel@tonic-gate 				ch = kobj_getc(file);
3867c478bd9Sstevel@tonic-gate digit:
3877c478bd9Sstevel@tonic-gate 				while (isdigit(ch)) {
3887c478bd9Sstevel@tonic-gate 					if (--remain == 0) {
38968ae3684Seota 						token = UNEXPECTED;
39068ae3684Seota 						goto out;
3917c478bd9Sstevel@tonic-gate 					}
3927c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
3937c478bd9Sstevel@tonic-gate 					ch = kobj_getc(file);
3947c478bd9Sstevel@tonic-gate 				}
3957c478bd9Sstevel@tonic-gate 				(void) kobj_ungetc(file);
3967c478bd9Sstevel@tonic-gate 				token = DECVAL;
3977c478bd9Sstevel@tonic-gate 			}
3987c478bd9Sstevel@tonic-gate 		} else if (isalpha(ch) || ch == '\\' || ch == '_') {
3997c478bd9Sstevel@tonic-gate 			if (ch != '\\') {
4007c478bd9Sstevel@tonic-gate 				ch = kobj_getc(file);
4017c478bd9Sstevel@tonic-gate 			} else {
4027c478bd9Sstevel@tonic-gate 				/*
4037c478bd9Sstevel@tonic-gate 				 * if the character was a backslash,
4047c478bd9Sstevel@tonic-gate 				 * back up so we can overwrite it with
4057c478bd9Sstevel@tonic-gate 				 * the next (i.e. escaped) character.
4067c478bd9Sstevel@tonic-gate 				 */
4077c478bd9Sstevel@tonic-gate 				remain++;
4087c478bd9Sstevel@tonic-gate 				cp--;
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			while (isnamechar(ch) || ch == '\\') {
4117c478bd9Sstevel@tonic-gate 				if (ch == '\\')
4127c478bd9Sstevel@tonic-gate 					ch = kobj_getc(file);
4137c478bd9Sstevel@tonic-gate 				if (--remain == 0) {
41468ae3684Seota 					token = UNEXPECTED;
41568ae3684Seota 					goto out;
4167c478bd9Sstevel@tonic-gate 				}
4177c478bd9Sstevel@tonic-gate 				*cp++ = (char)ch;
4187c478bd9Sstevel@tonic-gate 				ch = kobj_getc(file);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 			(void) kobj_ungetc(file);
4217c478bd9Sstevel@tonic-gate 			token = NAME;
4227c478bd9Sstevel@tonic-gate 		} else {
42368ae3684Seota 			token = UNEXPECTED;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 	}
42768ae3684Seota out:
4287c478bd9Sstevel@tonic-gate 	*cp = '\0';
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate #ifdef DEBUG
43168ae3684Seota 	/*
43268ae3684Seota 	 * The UNEXPECTED token is the first element of the tokennames array,
43368ae3684Seota 	 * but its token value is -1.  Adjust the value by adding one to it
43468ae3684Seota 	 * to change it to an index of the array.
43568ae3684Seota 	 */
43668ae3684Seota 	parse_debug(NULL, "kobj_lex: token %s value '%s'\n",
43768ae3684Seota 	    tokennames[token+1], val);
4387c478bd9Sstevel@tonic-gate #endif
4397c478bd9Sstevel@tonic-gate 	return (token);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Leave NEWLINE as the next character.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate void
kobj_find_eol(struct _buf * file)4477c478bd9Sstevel@tonic-gate kobj_find_eol(struct _buf *file)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	int ch;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	while ((ch = kobj_getc(file)) != -1) {
4527c478bd9Sstevel@tonic-gate 		if (isnewline(ch)) {
4537c478bd9Sstevel@tonic-gate 			(void) kobj_ungetc(file);
4547c478bd9Sstevel@tonic-gate 			break;
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * The ascii system file is read and processed.
4617c478bd9Sstevel@tonic-gate  *
4627c478bd9Sstevel@tonic-gate  * The syntax of commands is as follows:
4637c478bd9Sstevel@tonic-gate  *
4647c478bd9Sstevel@tonic-gate  * '*' in column 1 is a comment line.
4657c478bd9Sstevel@tonic-gate  * <command> : <value>
4667c478bd9Sstevel@tonic-gate  *
4677c478bd9Sstevel@tonic-gate  * command is EXCLUDE, INCLUDE, FORCELOAD, ROOTDEV, ROOTFS,
4687c478bd9Sstevel@tonic-gate  *	SWAPDEV, SWAPFS, MODDIR, SET
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  * value is an ascii string meaningful for the command.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Table of commands
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate static struct modcmd modcmd[] = {
4777c478bd9Sstevel@tonic-gate 	{ "EXCLUDE",	MOD_EXCLUDE	},
4787c478bd9Sstevel@tonic-gate 	{ "exclude",	MOD_EXCLUDE	},
4797c478bd9Sstevel@tonic-gate 	{ "INCLUDE",	MOD_INCLUDE	},
4807c478bd9Sstevel@tonic-gate 	{ "include",	MOD_INCLUDE	},
4817c478bd9Sstevel@tonic-gate 	{ "FORCELOAD",	MOD_FORCELOAD	},
4827c478bd9Sstevel@tonic-gate 	{ "forceload",	MOD_FORCELOAD	},
4837c478bd9Sstevel@tonic-gate 	{ "ROOTDEV",	MOD_ROOTDEV	},
4847c478bd9Sstevel@tonic-gate 	{ "rootdev",	MOD_ROOTDEV	},
4857c478bd9Sstevel@tonic-gate 	{ "ROOTFS",	MOD_ROOTFS	},
4867c478bd9Sstevel@tonic-gate 	{ "rootfs",	MOD_ROOTFS	},
4877c478bd9Sstevel@tonic-gate 	{ "SWAPDEV",	MOD_SWAPDEV	},
4887c478bd9Sstevel@tonic-gate 	{ "swapdev",	MOD_SWAPDEV	},
4897c478bd9Sstevel@tonic-gate 	{ "SWAPFS",	MOD_SWAPFS	},
4907c478bd9Sstevel@tonic-gate 	{ "swapfs",	MOD_SWAPFS	},
4917c478bd9Sstevel@tonic-gate 	{ "MODDIR",	MOD_MODDIR	},
4927c478bd9Sstevel@tonic-gate 	{ "moddir",	MOD_MODDIR	},
4937c478bd9Sstevel@tonic-gate 	{ "SET",	MOD_SET		},
4947c478bd9Sstevel@tonic-gate 	{ "set",	MOD_SET		},
4957c478bd9Sstevel@tonic-gate 	{ "SET32",	MOD_SET32	},
4967c478bd9Sstevel@tonic-gate 	{ "set32",	MOD_SET32	},
4977c478bd9Sstevel@tonic-gate 	{ "SET64",	MOD_SET64	},
4987c478bd9Sstevel@tonic-gate 	{ "set64",	MOD_SET64	},
4997c478bd9Sstevel@tonic-gate 	{ NULL,		MOD_UNKNOWN	}
5007c478bd9Sstevel@tonic-gate };
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static char bad_op[] = "illegal operator '%s' used on a string";
5047c478bd9Sstevel@tonic-gate static char colon_err[] = "A colon (:) must follow the '%s' command";
5057c478bd9Sstevel@tonic-gate static char tok_err[] = "Unexpected token '%s'";
5067c478bd9Sstevel@tonic-gate static char extra_err[] = "extraneous input ignored starting at '%s'";
5077c478bd9Sstevel@tonic-gate static char oversize_err[] = "value too long";
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static struct sysparam *
do_sysfile_cmd(struct _buf * file,const char * cmd)5107c478bd9Sstevel@tonic-gate do_sysfile_cmd(struct _buf *file, const char *cmd)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct sysparam *sysp;
5137c478bd9Sstevel@tonic-gate 	struct modcmd *mcp;
5147c478bd9Sstevel@tonic-gate 	token_t token, op;
5157c478bd9Sstevel@tonic-gate 	char *cp;
5167c478bd9Sstevel@tonic-gate 	int ch;
5177c478bd9Sstevel@tonic-gate 	char tok1[MOD_MAXPATH + 1]; /* used to read the path set by 'moddir' */
5187c478bd9Sstevel@tonic-gate 	char tok2[64];
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
5217c478bd9Sstevel@tonic-gate 		if (strcmp(mcp->mc_cmdname, cmd) == 0)
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	sysp = vmem_alloc(mod_sysfile_arena, sizeof (struct sysparam),
5257c478bd9Sstevel@tonic-gate 	    VM_SLEEP);
5267c478bd9Sstevel@tonic-gate 	bzero(sysp, sizeof (struct sysparam));
5277c478bd9Sstevel@tonic-gate 	sysp->sys_op = SETOP_NONE; /* set op to noop initially */
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	switch (sysp->sys_type = mcp->mc_type) {
5307c478bd9Sstevel@tonic-gate 	case MOD_INCLUDE:
5317c478bd9Sstevel@tonic-gate 	case MOD_EXCLUDE:
5327c478bd9Sstevel@tonic-gate 	case MOD_FORCELOAD:
5337c478bd9Sstevel@tonic-gate 		/*
5347c478bd9Sstevel@tonic-gate 		 * Are followed by colon.
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 	case MOD_ROOTFS:
5377c478bd9Sstevel@tonic-gate 	case MOD_SWAPFS:
5387c478bd9Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) == COLON) {
5397c478bd9Sstevel@tonic-gate 			token = kobj_lex(file, tok1, sizeof (tok1));
5407c478bd9Sstevel@tonic-gate 		} else {
5417c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		if (token != NAME) {
5447c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "value expected");
5457c478bd9Sstevel@tonic-gate 			goto bad;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		cp = tok1 + strlen(tok1);
5497c478bd9Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
5507c478bd9Sstevel@tonic-gate 		    !isnewline(ch)) {
5517c478bd9Sstevel@tonic-gate 			if (cp - tok1 >= sizeof (tok1) - 1) {
5527c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
5537c478bd9Sstevel@tonic-gate 				goto bad;
5547c478bd9Sstevel@tonic-gate 			}
5557c478bd9Sstevel@tonic-gate 			*cp++ = (char)ch;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		*cp = '\0';
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		if (ch != -1)
5607c478bd9Sstevel@tonic-gate 			(void) kobj_ungetc(file);
5617c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_INCLUDE)
5627c478bd9Sstevel@tonic-gate 			return (NULL);
5637c478bd9Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
5647c478bd9Sstevel@tonic-gate 		    VM_SLEEP);
5657c478bd9Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
5667c478bd9Sstevel@tonic-gate 		break;
5677c478bd9Sstevel@tonic-gate 	case MOD_SET:
5687c478bd9Sstevel@tonic-gate 	case MOD_SET64:
5697c478bd9Sstevel@tonic-gate 	case MOD_SET32:
5707c478bd9Sstevel@tonic-gate 	{
5717c478bd9Sstevel@tonic-gate 		char *var;
5727c478bd9Sstevel@tonic-gate 		token_t tok3;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		if (kobj_lex(file, tok1, sizeof (tok1)) != NAME) {
5757c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "value expected");
5767c478bd9Sstevel@tonic-gate 			goto bad;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/*
5807c478bd9Sstevel@tonic-gate 		 * If the next token is a colon (:),
5817c478bd9Sstevel@tonic-gate 		 * we have the <modname>:<variable> construct.
5827c478bd9Sstevel@tonic-gate 		 */
5837c478bd9Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok2, sizeof (tok2))) == COLON) {
5847c478bd9Sstevel@tonic-gate 			if ((token = kobj_lex(file, tok2,
5857c478bd9Sstevel@tonic-gate 			    sizeof (tok2))) == NAME) {
5867c478bd9Sstevel@tonic-gate 				var = tok2;
5877c478bd9Sstevel@tonic-gate 				/*
5887c478bd9Sstevel@tonic-gate 				 * Save the module name.
5897c478bd9Sstevel@tonic-gate 				 */
5907c478bd9Sstevel@tonic-gate 				sysp->sys_modnam = vmem_alloc(mod_sysfile_arena,
5917c478bd9Sstevel@tonic-gate 				    strlen(tok1) + 1, VM_SLEEP);
5927c478bd9Sstevel@tonic-gate 				(void) strcpy(sysp->sys_modnam, tok1);
5937c478bd9Sstevel@tonic-gate 				op = kobj_lex(file, tok1, sizeof (tok1));
5947c478bd9Sstevel@tonic-gate 			} else {
5957c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "value expected");
5967c478bd9Sstevel@tonic-gate 				goto bad;
5977c478bd9Sstevel@tonic-gate 			}
5987c478bd9Sstevel@tonic-gate 		} else {
5997c478bd9Sstevel@tonic-gate 			/* otherwise, it was the op */
6007c478bd9Sstevel@tonic-gate 			var = tok1;
6017c478bd9Sstevel@tonic-gate 			op = token;
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		/*
6047c478bd9Sstevel@tonic-gate 		 * kernel param - place variable name in sys_ptr.
6057c478bd9Sstevel@tonic-gate 		 */
6067c478bd9Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(var) + 1,
6077c478bd9Sstevel@tonic-gate 		    VM_SLEEP);
6087c478bd9Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, var);
6097c478bd9Sstevel@tonic-gate 		/* set operation */
6107c478bd9Sstevel@tonic-gate 		switch (op) {
6117c478bd9Sstevel@tonic-gate 		case EQUALS:
6127c478bd9Sstevel@tonic-gate 			/* simple assignment */
6137c478bd9Sstevel@tonic-gate 			sysp->sys_op = SETOP_ASSIGN;
6147c478bd9Sstevel@tonic-gate 			break;
6157c478bd9Sstevel@tonic-gate 		case AMPERSAND:
6167c478bd9Sstevel@tonic-gate 			/* bitwise AND */
6177c478bd9Sstevel@tonic-gate 			sysp->sys_op = SETOP_AND;
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		case BIT_OR:
6207c478bd9Sstevel@tonic-gate 			/* bitwise OR */
6217c478bd9Sstevel@tonic-gate 			sysp->sys_op = SETOP_OR;
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 		default:
6247c478bd9Sstevel@tonic-gate 			/* unsupported operation */
6257c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file,
6267c478bd9Sstevel@tonic-gate 			    "unsupported operator %s", tok2);
6277c478bd9Sstevel@tonic-gate 			goto bad;
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		switch ((tok3 = kobj_lex(file, tok1, sizeof (tok1)))) {
6317c478bd9Sstevel@tonic-gate 		case STRING:
6327c478bd9Sstevel@tonic-gate 			/* string variable */
6337c478bd9Sstevel@tonic-gate 			if (sysp->sys_op != SETOP_ASSIGN) {
6347c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, bad_op, tok1);
6357c478bd9Sstevel@tonic-gate 				goto bad;
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 			if (kobj_get_string(&sysp->sys_info, tok1) == 0) {
6387c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "string garbled");
6397c478bd9Sstevel@tonic-gate 				goto bad;
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate 			/*
6427c478bd9Sstevel@tonic-gate 			 * Set SYSPARAM_STR_TOKEN in sys_flags to notify
6437c478bd9Sstevel@tonic-gate 			 * sysparam_print_warning() that this is a string
6447c478bd9Sstevel@tonic-gate 			 * token.
6457c478bd9Sstevel@tonic-gate 			 */
6467c478bd9Sstevel@tonic-gate 			sysp->sys_flags |= SYSPARAM_STR_TOKEN;
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		case HEXVAL:
6497c478bd9Sstevel@tonic-gate 		case DECVAL:
6507c478bd9Sstevel@tonic-gate 			if (kobj_getvalue(tok1, &sysp->sys_info) == -1) {
6517c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
6527c478bd9Sstevel@tonic-gate 				    "invalid number '%s'", tok1);
6537c478bd9Sstevel@tonic-gate 				goto bad;
6547c478bd9Sstevel@tonic-gate 			}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 			/*
6577c478bd9Sstevel@tonic-gate 			 * Set the appropriate flag (hexadecimal or decimal)
6587c478bd9Sstevel@tonic-gate 			 * in sys_flags for sysparam_print_warning() to be
6597c478bd9Sstevel@tonic-gate 			 * able to print the number with the correct format.
6607c478bd9Sstevel@tonic-gate 			 */
6617c478bd9Sstevel@tonic-gate 			if (tok3 == HEXVAL) {
6627c478bd9Sstevel@tonic-gate 				sysp->sys_flags |= SYSPARAM_HEX_TOKEN;
6637c478bd9Sstevel@tonic-gate 			} else {
6647c478bd9Sstevel@tonic-gate 				sysp->sys_flags |= SYSPARAM_DEC_TOKEN;
6657c478bd9Sstevel@tonic-gate 			}
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		default:
6687c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "bad rvalue '%s'", tok1);
6697c478bd9Sstevel@tonic-gate 			goto bad;
6707c478bd9Sstevel@tonic-gate 		} /* end switch */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		/*
6737c478bd9Sstevel@tonic-gate 		 * Now that we've parsed it to check the syntax, consider
6747c478bd9Sstevel@tonic-gate 		 * discarding it (because it -doesn't- apply to this flavor
6757c478bd9Sstevel@tonic-gate 		 * of the kernel)
6767c478bd9Sstevel@tonic-gate 		 */
6777c478bd9Sstevel@tonic-gate #ifdef _LP64
6787c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SET32)
6797c478bd9Sstevel@tonic-gate 			return (NULL);
6807c478bd9Sstevel@tonic-gate #else
6817c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SET64)
6827c478bd9Sstevel@tonic-gate 			return (NULL);
6837c478bd9Sstevel@tonic-gate #endif
6847c478bd9Sstevel@tonic-gate 		sysp->sys_type = MOD_SET;
6857c478bd9Sstevel@tonic-gate 		break;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	case MOD_MODDIR:
6887c478bd9Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
6897c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
6907c478bd9Sstevel@tonic-gate 			goto bad;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		cp = tok1;
6947c478bd9Sstevel@tonic-gate 		while ((token = kobj_lex(file, cp,
6957c478bd9Sstevel@tonic-gate 		    sizeof (tok1) - (cp - tok1))) != NEWLINE && token != EOF) {
6967c478bd9Sstevel@tonic-gate 			if (token == -1) {
6977c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
6987c478bd9Sstevel@tonic-gate 				goto bad;
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 			cp += strlen(cp);
7017c478bd9Sstevel@tonic-gate 			while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
7027c478bd9Sstevel@tonic-gate 			    !isnewline(ch) && ch != ':') {
7037c478bd9Sstevel@tonic-gate 				if (cp - tok1 >= sizeof (tok1) - 1) {
7047c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
7057c478bd9Sstevel@tonic-gate 					    oversize_err);
7067c478bd9Sstevel@tonic-gate 					goto bad;
7077c478bd9Sstevel@tonic-gate 				}
7087c478bd9Sstevel@tonic-gate 				*cp++ = (char)ch;
7097c478bd9Sstevel@tonic-gate 			}
710b2940a7cSRichard Lowe 			*cp++ = ' ';
7116f8113b7Sgp 			if (isnewline(ch)) {
7126f8113b7Sgp 				cp--;
7137c478bd9Sstevel@tonic-gate 				(void) kobj_ungetc(file);
7146f8113b7Sgp 			}
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		(void) kobj_ungetc(file);
7177c478bd9Sstevel@tonic-gate 		*cp  = '\0';
7187c478bd9Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
7197c478bd9Sstevel@tonic-gate 		    VM_SLEEP);
7207c478bd9Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
7217c478bd9Sstevel@tonic-gate 		break;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	case MOD_SWAPDEV:
7247c478bd9Sstevel@tonic-gate 	case MOD_ROOTDEV:
7257c478bd9Sstevel@tonic-gate 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
7267c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, colon_err, cmd);
7277c478bd9Sstevel@tonic-gate 			goto bad;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
7307c478bd9Sstevel@tonic-gate 			;
7317c478bd9Sstevel@tonic-gate 		cp = tok1;
7327c478bd9Sstevel@tonic-gate 		while (!iswhite(ch) && !isnewline(ch) && ch != -1) {
7337c478bd9Sstevel@tonic-gate 			if (cp - tok1 >= sizeof (tok1) - 1) {
7347c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, oversize_err);
7357c478bd9Sstevel@tonic-gate 				goto bad;
7367c478bd9Sstevel@tonic-gate 			}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 			*cp++ = (char)ch;
7397c478bd9Sstevel@tonic-gate 			ch = kobj_getc(file);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 		if (ch != -1)
7427c478bd9Sstevel@tonic-gate 			(void) kobj_ungetc(file);
7437c478bd9Sstevel@tonic-gate 		*cp = '\0';
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
7467c478bd9Sstevel@tonic-gate 		    VM_SLEEP);
7477c478bd9Sstevel@tonic-gate 		(void) strcpy(sysp->sys_ptr, tok1);
7487c478bd9Sstevel@tonic-gate 		break;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	case MOD_UNKNOWN:
7517c478bd9Sstevel@tonic-gate 	default:
7527c478bd9Sstevel@tonic-gate 		kobj_file_err(CE_WARN, file, "unknown command '%s'", cmd);
7537c478bd9Sstevel@tonic-gate 		goto bad;
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (sysp);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate bad:
7597c478bd9Sstevel@tonic-gate 	kobj_find_eol(file);
7607c478bd9Sstevel@tonic-gate 	return (NULL);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7632b987d42SAlexander Eremin static void
read_system_file(char * name)7642b987d42SAlexander Eremin read_system_file(char *name)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	register struct sysparam *sp;
7677c478bd9Sstevel@tonic-gate 	register struct _buf *file;
7687c478bd9Sstevel@tonic-gate 	register token_t token, last_tok;
7697c478bd9Sstevel@tonic-gate 	char tokval[MAXLINESIZE];
7707c478bd9Sstevel@tonic-gate 
7712b987d42SAlexander Eremin 	if ((file = kobj_open_file(name)) ==
7722b987d42SAlexander Eremin 	    (struct _buf *)-1) {
7732b987d42SAlexander Eremin 		if (strcmp(name, systemfile) == 0)
7747c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot open system file: %s",
7752b987d42SAlexander Eremin 			    name);
7762b987d42SAlexander Eremin 	} else {
7772b987d42SAlexander Eremin 		if (sysparam_tl == NULL)
7787c478bd9Sstevel@tonic-gate 			sysparam_tl = (struct sysparam *)&sysparam_hd;
7797c478bd9Sstevel@tonic-gate 
7802b987d42SAlexander Eremin 		last_tok = NEWLINE;
7812b987d42SAlexander Eremin 		while ((token = kobj_lex(file, tokval,
7822b987d42SAlexander Eremin 		    sizeof (tokval))) != EOF) {
7832b987d42SAlexander Eremin 			switch (token) {
7842b987d42SAlexander Eremin 			case STAR:
7852b987d42SAlexander Eremin 			case POUND:
7862b987d42SAlexander Eremin 				/*
7872b987d42SAlexander Eremin 				 * Skip comments.
7882b987d42SAlexander Eremin 				 */
7892b987d42SAlexander Eremin 				kobj_find_eol(file);
7902b987d42SAlexander Eremin 				break;
7912b987d42SAlexander Eremin 			case NEWLINE:
7922b987d42SAlexander Eremin 				kobj_newline(file);
7932b987d42SAlexander Eremin 				last_tok = NEWLINE;
7942b987d42SAlexander Eremin 				break;
7952b987d42SAlexander Eremin 			case NAME:
7962b987d42SAlexander Eremin 				if (last_tok != NEWLINE) {
7972b987d42SAlexander Eremin 					kobj_file_err(CE_WARN, file,
7982b987d42SAlexander Eremin 					    extra_err, tokval);
7997c478bd9Sstevel@tonic-gate 					kobj_find_eol(file);
8002b987d42SAlexander Eremin 				} else if ((sp = do_sysfile_cmd(file,
8012b987d42SAlexander Eremin 				    tokval)) != NULL) {
8022b987d42SAlexander Eremin 					sp->sys_next = NULL;
8032b987d42SAlexander Eremin 					sysparam_tl->sys_next = sp;
8042b987d42SAlexander Eremin 					sysparam_tl = sp;
8057c478bd9Sstevel@tonic-gate 				}
8062b987d42SAlexander Eremin 				last_tok = NAME;
8072b987d42SAlexander Eremin 				break;
8082b987d42SAlexander Eremin 			default:
8092b987d42SAlexander Eremin 				kobj_file_err(CE_WARN,
8102b987d42SAlexander Eremin 				    file, tok_err, tokval);
8112b987d42SAlexander Eremin 				kobj_find_eol(file);
8122b987d42SAlexander Eremin 				break;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		}
8152b987d42SAlexander Eremin 		kobj_close_file(file);
8167c478bd9Sstevel@tonic-gate 	}
8172b987d42SAlexander Eremin }
8182b987d42SAlexander Eremin 
8192b987d42SAlexander Eremin void
mod_read_system_file(int ask)8202b987d42SAlexander Eremin mod_read_system_file(int ask)
8212b987d42SAlexander Eremin {
8228eb8717cSJohn Levon 	struct _buf *file;
8238eb8717cSJohn Levon 
8242b987d42SAlexander Eremin 	mod_sysfile_arena = vmem_create("mod_sysfile", NULL, 0, 8,
8252b987d42SAlexander Eremin 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
8262b987d42SAlexander Eremin 
8272b987d42SAlexander Eremin 	if (ask)
8282b987d42SAlexander Eremin 		mod_askparams();
8292b987d42SAlexander Eremin 
8302b987d42SAlexander Eremin 	/*
8312b987d42SAlexander Eremin 	 * Read the user self-assembly file first
8322b987d42SAlexander Eremin 	 * to preserve existing system settings.
8332b987d42SAlexander Eremin 	 */
8342b987d42SAlexander Eremin 	if (self_assembly != NULL)
8352b987d42SAlexander Eremin 		read_system_file(self_assembly);
8362b987d42SAlexander Eremin 
8372b987d42SAlexander Eremin 	if (systemfile != NULL)
8382b987d42SAlexander Eremin 		read_system_file(systemfile);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/*
8417c478bd9Sstevel@tonic-gate 	 * Sanity check of /etc/system.
8427c478bd9Sstevel@tonic-gate 	 */
8437c478bd9Sstevel@tonic-gate 	check_system_file();
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	param_preset();
8467c478bd9Sstevel@tonic-gate 	(void) mod_sysctl(SYS_SET_KVAR, NULL);
8477d692464Sdp 	param_check();
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	if (ask == 0)
8507c478bd9Sstevel@tonic-gate 		setparams();
8518eb8717cSJohn Levon 
8528eb8717cSJohn Levon 	/*
8538eb8717cSJohn Levon 	 * A convenient place to read in our build version string.
8548eb8717cSJohn Levon 	 */
8558eb8717cSJohn Levon 
8568eb8717cSJohn Levon 	if ((file = kobj_open_file(versionfile)) != (struct _buf *)-1) {
8578eb8717cSJohn Levon 		if (kobj_read_file(file, buildversion,
8588eb8717cSJohn Levon 		    sizeof (buildversion) - 1, 0) == -1) {
8598eb8717cSJohn Levon 			cmn_err(CE_WARN, "failed to read %s\n", versionfile);
8608eb8717cSJohn Levon 		}
8618eb8717cSJohn Levon 		kobj_close_file(file);
8628eb8717cSJohn Levon 	}
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate  * Search for a specific module variable assignment in /etc/system.  If
8677c478bd9Sstevel@tonic-gate  * successful, 1 is returned and the value is stored in '*value'.
8687c478bd9Sstevel@tonic-gate  * Otherwise 0 is returned and '*value' isn't modified.  If 'module' is
8697c478bd9Sstevel@tonic-gate  * NULL we look for global definitions.
8707c478bd9Sstevel@tonic-gate  *
8717c478bd9Sstevel@tonic-gate  * This is useful if the value of an assignment is needed before a
8727c478bd9Sstevel@tonic-gate  * module is loaded (e.g. to obtain a default privileged rctl limit).
8737c478bd9Sstevel@tonic-gate  */
8747c478bd9Sstevel@tonic-gate int
mod_sysvar(const char * module,const char * name,u_longlong_t * value)8757c478bd9Sstevel@tonic-gate mod_sysvar(const char *module, const char *name, u_longlong_t *value)
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate 	struct sysparam	*sysp;
8787c478bd9Sstevel@tonic-gate 	int cnt = 0; /* dummy */
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
8817c478bd9Sstevel@tonic-gate 	ASSERT(value != NULL);
8827c478bd9Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		if ((sysp->sys_type == MOD_SET) &&
8857c478bd9Sstevel@tonic-gate 		    (((module == NULL) && (sysp->sys_modnam == NULL)) ||
8867c478bd9Sstevel@tonic-gate 		    ((module != NULL) && (sysp->sys_modnam != NULL) &&
8877c478bd9Sstevel@tonic-gate 		    (strcmp(module, sysp->sys_modnam) == 0)))) {
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			ASSERT(sysp->sys_ptr != NULL);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 			if (strcmp(name, sysp->sys_ptr) == 0) {
8927c478bd9Sstevel@tonic-gate 				sysparam_count_entry(sysp, &cnt, value);
8937c478bd9Sstevel@tonic-gate 				if ((sysp->sys_flags & SYSPARAM_TERM) != 0)
8947c478bd9Sstevel@tonic-gate 					return (1);
8957c478bd9Sstevel@tonic-gate 				continue;
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 	ASSERT(cnt == 0);
9007c478bd9Sstevel@tonic-gate 	return (0);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate  * This function scans sysparam records, which are created from the
9057c478bd9Sstevel@tonic-gate  * contents of /etc/system, for entries which are logical duplicates,
9067c478bd9Sstevel@tonic-gate  * and prints warning messages as appropriate.  When multiple "set"
9077c478bd9Sstevel@tonic-gate  * commands are encountered, the pileup of values with "&", "|"
9087c478bd9Sstevel@tonic-gate  * and "=" operators results in the final value.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate static void
check_system_file(void)9117c478bd9Sstevel@tonic-gate check_system_file(void)
9127c478bd9Sstevel@tonic-gate {
9137c478bd9Sstevel@tonic-gate 	struct sysparam	*sysp;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
9167c478bd9Sstevel@tonic-gate 		struct sysparam *entry, *final;
9177c478bd9Sstevel@tonic-gate 		u_longlong_t value = 0;
9187c478bd9Sstevel@tonic-gate 		int cnt = 1;
9197c478bd9Sstevel@tonic-gate 		/*
9207c478bd9Sstevel@tonic-gate 		 * If the entry is already checked, skip it.
9217c478bd9Sstevel@tonic-gate 		 */
9227c478bd9Sstevel@tonic-gate 		if ((sysp->sys_flags & SYSPARAM_DUP) != 0)
9237c478bd9Sstevel@tonic-gate 			continue;
9247c478bd9Sstevel@tonic-gate 		/*
9257c478bd9Sstevel@tonic-gate 		 * Check if there is a duplicate entry by doing a linear
9267c478bd9Sstevel@tonic-gate 		 * search.
9277c478bd9Sstevel@tonic-gate 		 */
9287c478bd9Sstevel@tonic-gate 		final = sysp;
9297c478bd9Sstevel@tonic-gate 		for (entry = sysp->sys_next; entry != NULL;
9307c478bd9Sstevel@tonic-gate 		    entry = entry->sys_next) {
9317c478bd9Sstevel@tonic-gate 			/*
9327c478bd9Sstevel@tonic-gate 			 * Check the entry. if it's different, skip this.
9337c478bd9Sstevel@tonic-gate 			 */
9347c478bd9Sstevel@tonic-gate 			if (sysparam_compare_entry(sysp, entry) != 0)
9357c478bd9Sstevel@tonic-gate 				continue;
9367c478bd9Sstevel@tonic-gate 			/*
9377c478bd9Sstevel@tonic-gate 			 * Count the entry and put the mark.
9387c478bd9Sstevel@tonic-gate 			 */
9397c478bd9Sstevel@tonic-gate 			sysparam_count_entry(entry, &cnt, &value);
9407c478bd9Sstevel@tonic-gate 			entry->sys_flags |= SYSPARAM_DUP;
9417c478bd9Sstevel@tonic-gate 			final = entry;
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		final->sys_flags |= SYSPARAM_TERM;
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * Print the warning if it's duplicated.
9467c478bd9Sstevel@tonic-gate 		 */
9477c478bd9Sstevel@tonic-gate 		if (cnt >= 2)
9487c478bd9Sstevel@tonic-gate 			sysparam_print_warning(final, value);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * Compare the sysparam records.
9547c478bd9Sstevel@tonic-gate  * Return 0 if they are the same, return 1 if not.
9557c478bd9Sstevel@tonic-gate  */
9567c478bd9Sstevel@tonic-gate static int
sysparam_compare_entry(struct sysparam * sysp,struct sysparam * entry)9577c478bd9Sstevel@tonic-gate sysparam_compare_entry(struct sysparam *sysp, struct sysparam *entry)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	ASSERT(sysp->sys_ptr != NULL && entry->sys_ptr != NULL);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * If the command is rootdev, rootfs, swapdev, swapfs or moddir,
9637c478bd9Sstevel@tonic-gate 	 * the record with the same type is treated as a duplicate record.
9647c478bd9Sstevel@tonic-gate 	 * In other cases, the record is treated as a duplicate record when
9657c478bd9Sstevel@tonic-gate 	 * its type, its module name (if it exists), and its variable name
9667c478bd9Sstevel@tonic-gate 	 * are the same.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	switch (sysp->sys_type) {
9697c478bd9Sstevel@tonic-gate 	case MOD_ROOTDEV:
9707c478bd9Sstevel@tonic-gate 	case MOD_ROOTFS:
9717c478bd9Sstevel@tonic-gate 	case MOD_SWAPDEV:
9727c478bd9Sstevel@tonic-gate 	case MOD_SWAPFS:
9737c478bd9Sstevel@tonic-gate 	case MOD_MODDIR:
9747c478bd9Sstevel@tonic-gate 		return (sysp->sys_type == entry->sys_type ? 0 : 1);
9757c478bd9Sstevel@tonic-gate 	default: /* In other cases, just go through it. */
9767c478bd9Sstevel@tonic-gate 		break;
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	if (sysp->sys_type != entry->sys_type)
9807c478bd9Sstevel@tonic-gate 		return (1);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (sysp->sys_modnam != NULL && entry->sys_modnam == NULL)
9837c478bd9Sstevel@tonic-gate 		return (1);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if (sysp->sys_modnam == NULL && entry->sys_modnam != NULL)
9867c478bd9Sstevel@tonic-gate 		return (1);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (sysp->sys_modnam != NULL && entry->sys_modnam != NULL &&
9897c478bd9Sstevel@tonic-gate 	    strcmp(sysp->sys_modnam, entry->sys_modnam) != 0)
9907c478bd9Sstevel@tonic-gate 		return (1);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	return (strcmp(sysp->sys_ptr, entry->sys_ptr));
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate /*
9967c478bd9Sstevel@tonic-gate  * Translate a sysparam type value to a string.
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate static char *
sysparam_type_to_str(int type)9997c478bd9Sstevel@tonic-gate sysparam_type_to_str(int type)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	struct modcmd *mcp;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
10047c478bd9Sstevel@tonic-gate 		if (mcp->mc_type == type)
10057c478bd9Sstevel@tonic-gate 			break;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	ASSERT(mcp->mc_type == type);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (type != MOD_UNKNOWN)
10107c478bd9Sstevel@tonic-gate 		return ((++mcp)->mc_cmdname); /* lower case */
10117c478bd9Sstevel@tonic-gate 	else
10127c478bd9Sstevel@tonic-gate 		return ("");	/* MOD_UNKNOWN */
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate /*
10167c478bd9Sstevel@tonic-gate  * Check the entry and accumulate the number of entries.
10177c478bd9Sstevel@tonic-gate  */
10187c478bd9Sstevel@tonic-gate static void
sysparam_count_entry(struct sysparam * sysp,int * cnt,u_longlong_t * value)10197c478bd9Sstevel@tonic-gate sysparam_count_entry(struct sysparam *sysp, int *cnt, u_longlong_t *value)
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate 	u_longlong_t ul = sysp->sys_info;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	switch (sysp->sys_op) {
10247c478bd9Sstevel@tonic-gate 	case SETOP_ASSIGN:
10257c478bd9Sstevel@tonic-gate 		*value = ul;
10267c478bd9Sstevel@tonic-gate 		(*cnt)++;
10277c478bd9Sstevel@tonic-gate 		return;
10287c478bd9Sstevel@tonic-gate 	case SETOP_AND:
10297c478bd9Sstevel@tonic-gate 		*value &= ul;
10307c478bd9Sstevel@tonic-gate 		return;
10317c478bd9Sstevel@tonic-gate 	case SETOP_OR:
10327c478bd9Sstevel@tonic-gate 		*value |= ul;
10337c478bd9Sstevel@tonic-gate 		return;
10347c478bd9Sstevel@tonic-gate 	default: /* Not MOD_SET */
10357c478bd9Sstevel@tonic-gate 		(*cnt)++;
10367c478bd9Sstevel@tonic-gate 		return;
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate  * Print out the warning if multiple entries are found in the system file.
10427c478bd9Sstevel@tonic-gate  */
10437c478bd9Sstevel@tonic-gate static void
sysparam_print_warning(struct sysparam * sysp,u_longlong_t value)10447c478bd9Sstevel@tonic-gate sysparam_print_warning(struct sysparam *sysp, u_longlong_t value)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	char *modnam = sysp->sys_modnam;
10477c478bd9Sstevel@tonic-gate 	char *varnam = sysp->sys_ptr;
10487c478bd9Sstevel@tonic-gate 	int type = sysp->sys_type;
10497c478bd9Sstevel@tonic-gate 	char *typenam = sysparam_type_to_str(type);
10507c478bd9Sstevel@tonic-gate 	boolean_t str_token = ((sysp->sys_flags & SYSPARAM_STR_TOKEN) != 0);
10517c478bd9Sstevel@tonic-gate 	boolean_t hex_number = ((sysp->sys_flags & SYSPARAM_HEX_TOKEN) != 0);
10527c478bd9Sstevel@tonic-gate #define	warn_format1 " is set more than once in /%s. "
10537c478bd9Sstevel@tonic-gate #define	warn_format2 " applied as the current setting.\n"
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	ASSERT(varnam != NULL);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	if (type == MOD_SET) {
10587c478bd9Sstevel@tonic-gate 		/*
10597c478bd9Sstevel@tonic-gate 		 * If a string token is set, print out the string
10607c478bd9Sstevel@tonic-gate 		 * instead of its pointer value. In other cases,
10617c478bd9Sstevel@tonic-gate 		 * print out the value with the appropriate format
10627c478bd9Sstevel@tonic-gate 		 * for a hexadecimal number or a decimal number.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		if (modnam == NULL) {
10657c478bd9Sstevel@tonic-gate 			if (str_token == B_TRUE) {
10667c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10677c478bd9Sstevel@tonic-gate 				    "\"%s %s = %s\"" warn_format2,
10687c478bd9Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10697c478bd9Sstevel@tonic-gate 				    varnam, (char *)(uintptr_t)value);
10707c478bd9Sstevel@tonic-gate 			} else if (hex_number == B_TRUE) {
10717c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10727c478bd9Sstevel@tonic-gate 				    "\"%s %s = 0x%llx\"" warn_format2,
10737c478bd9Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10747c478bd9Sstevel@tonic-gate 				    varnam, value);
10757c478bd9Sstevel@tonic-gate 			} else {
10767c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s" warn_format1
10777c478bd9Sstevel@tonic-gate 				    "\"%s %s = %lld\"" warn_format2,
10787c478bd9Sstevel@tonic-gate 				    varnam, systemfile, typenam,
10797c478bd9Sstevel@tonic-gate 				    varnam, value);
10807c478bd9Sstevel@tonic-gate 			}
10817c478bd9Sstevel@tonic-gate 		} else {
10827c478bd9Sstevel@tonic-gate 			if (str_token == B_TRUE) {
10837c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10847c478bd9Sstevel@tonic-gate 				    "\"%s %s:%s = %s\"" warn_format2,
10857c478bd9Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10867c478bd9Sstevel@tonic-gate 				    typenam, modnam, varnam,
10877c478bd9Sstevel@tonic-gate 				    (char *)(uintptr_t)value);
10887c478bd9Sstevel@tonic-gate 			} else if (hex_number == B_TRUE) {
10897c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10907c478bd9Sstevel@tonic-gate 				    "\"%s %s:%s = 0x%llx\"" warn_format2,
10917c478bd9Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10927c478bd9Sstevel@tonic-gate 				    typenam, modnam, varnam, value);
10937c478bd9Sstevel@tonic-gate 			} else {
10947c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s:%s" warn_format1
10957c478bd9Sstevel@tonic-gate 				    "\"%s %s:%s = %lld\"" warn_format2,
10967c478bd9Sstevel@tonic-gate 				    modnam, varnam, systemfile,
10977c478bd9Sstevel@tonic-gate 				    typenam, modnam, varnam, value);
10987c478bd9Sstevel@tonic-gate 			}
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 	} else {
11017c478bd9Sstevel@tonic-gate 		/*
11027c478bd9Sstevel@tonic-gate 		 * If the type is MOD_ROOTDEV, MOD_ROOTFS, MOD_SWAPDEV,
11037c478bd9Sstevel@tonic-gate 		 * MOD_SWAPFS or MOD_MODDIR, the entry is treated as
11047c478bd9Sstevel@tonic-gate 		 * a duplicate one if it has the same type regardless
11057c478bd9Sstevel@tonic-gate 		 * of its variable name.
11067c478bd9Sstevel@tonic-gate 		 */
11077c478bd9Sstevel@tonic-gate 		switch (type) {
11087c478bd9Sstevel@tonic-gate 		case MOD_ROOTDEV:
11097c478bd9Sstevel@tonic-gate 		case MOD_ROOTFS:
11107c478bd9Sstevel@tonic-gate 		case MOD_SWAPDEV:
11117c478bd9Sstevel@tonic-gate 		case MOD_SWAPFS:
11127c478bd9Sstevel@tonic-gate 		case MOD_MODDIR:
11137c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "\"%s\" appears more than once "
11147c478bd9Sstevel@tonic-gate 			    "in /%s.", typenam, systemfile);
11157c478bd9Sstevel@tonic-gate 			break;
11167c478bd9Sstevel@tonic-gate 		default:
11177c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "\"%s: %s\" appears more than once "
11187c478bd9Sstevel@tonic-gate 			    "in /%s.", typenam, varnam, systemfile);
11197c478bd9Sstevel@tonic-gate 			break;
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate /*
11257c478bd9Sstevel@tonic-gate  * Process the system file commands.
11267c478bd9Sstevel@tonic-gate  */
11277c478bd9Sstevel@tonic-gate int
mod_sysctl(int fcn,void * p)11287c478bd9Sstevel@tonic-gate mod_sysctl(int fcn, void *p)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	static char wmesg[] = "forceload of %s failed";
11317c478bd9Sstevel@tonic-gate 	struct sysparam *sysp;
11327c478bd9Sstevel@tonic-gate 	char *name;
11337c478bd9Sstevel@tonic-gate 	struct modctl *modp;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (sysparam_hd == NULL)
11367c478bd9Sstevel@tonic-gate 		return (0);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		switch (fcn) {
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		case SYS_FORCELOAD:
11437c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_FORCELOAD) {
11447c478bd9Sstevel@tonic-gate 			name = sysp->sys_ptr;
11457c478bd9Sstevel@tonic-gate 			if (modload(NULL, name) == -1)
11467c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, wmesg, name);
11477c478bd9Sstevel@tonic-gate 			/*
11487c478bd9Sstevel@tonic-gate 			 * The following works because it
11497c478bd9Sstevel@tonic-gate 			 * runs before autounloading is started!!
11507c478bd9Sstevel@tonic-gate 			 */
11517c478bd9Sstevel@tonic-gate 			modp = mod_find_by_filename(NULL, name);
11527c478bd9Sstevel@tonic-gate 			if (modp != NULL)
11537c478bd9Sstevel@tonic-gate 				modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
11547c478bd9Sstevel@tonic-gate 			/*
11557c478bd9Sstevel@tonic-gate 			 * For drivers, attempt to install it.
11567c478bd9Sstevel@tonic-gate 			 */
11577c478bd9Sstevel@tonic-gate 			if (strncmp(sysp->sys_ptr, "drv", 3) == 0) {
11587c478bd9Sstevel@tonic-gate 				(void) ddi_install_driver(name + 4);
11597c478bd9Sstevel@tonic-gate 			}
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 		break;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		case SYS_SET_KVAR:
11647c478bd9Sstevel@tonic-gate 		case SYS_SET_MVAR:
11657c478bd9Sstevel@tonic-gate 			if (sysp->sys_type == MOD_SET)
11667c478bd9Sstevel@tonic-gate 				sys_set_var(fcn, sysp, p);
11677c478bd9Sstevel@tonic-gate 			break;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		case SYS_CHECK_EXCLUDE:
11707c478bd9Sstevel@tonic-gate 			if (sysp->sys_type == MOD_EXCLUDE) {
11717c478bd9Sstevel@tonic-gate 				if (p == NULL || sysp->sys_ptr == NULL)
11727c478bd9Sstevel@tonic-gate 					return (0);
11737c478bd9Sstevel@tonic-gate 				if (strcmp((char *)p, sysp->sys_ptr) == 0)
11747c478bd9Sstevel@tonic-gate 					return (1);
11757c478bd9Sstevel@tonic-gate 			}
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	return (0);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate  * Process the system file commands, by type.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate int
mod_sysctl_type(int type,int (* func)(struct sysparam *,void *),void * p)11867c478bd9Sstevel@tonic-gate mod_sysctl_type(int type, int (*func)(struct sysparam *, void *), void *p)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	struct sysparam *sysp;
11897c478bd9Sstevel@tonic-gate 	int	err;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next)
11927c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == type)
11937c478bd9Sstevel@tonic-gate 			if (err = (*(func))(sysp, p))
11947c478bd9Sstevel@tonic-gate 				return (err);
11957c478bd9Sstevel@tonic-gate 	return (0);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate static char seterr[] = "Symbol %s has size of 0 in symbol table. %s";
12007c478bd9Sstevel@tonic-gate static char assumption[] = "Assuming it is an 'int'";
12017c478bd9Sstevel@tonic-gate static char defmsg[] = "Trying to set a variable that is of size %d";
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate static void set_int8_var(uintptr_t, struct sysparam *);
12047c478bd9Sstevel@tonic-gate static void set_int16_var(uintptr_t, struct sysparam *);
12057c478bd9Sstevel@tonic-gate static void set_int32_var(uintptr_t, struct sysparam *);
12067c478bd9Sstevel@tonic-gate static void set_int64_var(uintptr_t, struct sysparam *);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate static void
sys_set_var(int fcn,struct sysparam * sysp,void * p)12097c478bd9Sstevel@tonic-gate sys_set_var(int fcn, struct sysparam *sysp, void *p)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	uintptr_t symaddr;
12127c478bd9Sstevel@tonic-gate 	int size;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (fcn == SYS_SET_KVAR && sysp->sys_modnam == NULL) {
12157c478bd9Sstevel@tonic-gate 		symaddr = kobj_getelfsym(sysp->sys_ptr, NULL, &size);
12167c478bd9Sstevel@tonic-gate 	} else if (fcn == SYS_SET_MVAR) {
12177c478bd9Sstevel@tonic-gate 		if (sysp->sys_modnam == (char *)NULL ||
1218b1b8ab34Slling 		    strcmp(((struct modctl *)p)->mod_modname,
1219b1b8ab34Slling 		    sysp->sys_modnam) != 0)
1220b1b8ab34Slling 			return;
12217c478bd9Sstevel@tonic-gate 		symaddr = kobj_getelfsym(sysp->sys_ptr,
12227c478bd9Sstevel@tonic-gate 		    ((struct modctl *)p)->mod_mp, &size);
12237c478bd9Sstevel@tonic-gate 	} else
12247c478bd9Sstevel@tonic-gate 		return;
12257c478bd9Sstevel@tonic-gate 
12267e12ceb3SToomas Soome 	if (symaddr != (uintptr_t)NULL) {
12277c478bd9Sstevel@tonic-gate 		switch (size) {
12287c478bd9Sstevel@tonic-gate 		case 1:
12297c478bd9Sstevel@tonic-gate 			set_int8_var(symaddr, sysp);
12307c478bd9Sstevel@tonic-gate 			break;
12317c478bd9Sstevel@tonic-gate 		case 2:
12327c478bd9Sstevel@tonic-gate 			set_int16_var(symaddr, sysp);
12337c478bd9Sstevel@tonic-gate 			break;
12347c478bd9Sstevel@tonic-gate 		case 0:
12357c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, seterr, sysp->sys_ptr, assumption);
12367c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
12377c478bd9Sstevel@tonic-gate 		case 4:
12387c478bd9Sstevel@tonic-gate 			set_int32_var(symaddr, sysp);
12397c478bd9Sstevel@tonic-gate 			break;
12407c478bd9Sstevel@tonic-gate 		case 8:
12417c478bd9Sstevel@tonic-gate 			set_int64_var(symaddr, sysp);
12427c478bd9Sstevel@tonic-gate 			break;
12437c478bd9Sstevel@tonic-gate 		default:
12447c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, defmsg, size);
12457c478bd9Sstevel@tonic-gate 			break;
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 	} else {
12487c478bd9Sstevel@tonic-gate 		printf("sorry, variable '%s' is not defined in the '%s' ",
12497c478bd9Sstevel@tonic-gate 		    sysp->sys_ptr,
12507c478bd9Sstevel@tonic-gate 		    sysp->sys_modnam ? sysp->sys_modnam : "kernel");
12517c478bd9Sstevel@tonic-gate 		if (sysp->sys_modnam)
12527c478bd9Sstevel@tonic-gate 			printf("module");
12537c478bd9Sstevel@tonic-gate 		printf("\n");
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate static void
set_int8_var(uintptr_t symaddr,struct sysparam * sysp)12587c478bd9Sstevel@tonic-gate set_int8_var(uintptr_t symaddr, struct sysparam *sysp)
12597c478bd9Sstevel@tonic-gate {
12607c478bd9Sstevel@tonic-gate 	uint8_t uc = (uint8_t)sysp->sys_info;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12637c478bd9Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx8
12647c478bd9Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
12657c478bd9Sstevel@tonic-gate 		    *(uint8_t *)symaddr, sysp->sys_modnam);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	switch (sysp->sys_op) {
12687c478bd9Sstevel@tonic-gate 	case SETOP_ASSIGN:
12697c478bd9Sstevel@tonic-gate 		*(uint8_t *)symaddr = uc;
12707c478bd9Sstevel@tonic-gate 		break;
12717c478bd9Sstevel@tonic-gate 	case SETOP_AND:
12727c478bd9Sstevel@tonic-gate 		*(uint8_t *)symaddr &= uc;
12737c478bd9Sstevel@tonic-gate 		break;
12747c478bd9Sstevel@tonic-gate 	case SETOP_OR:
12757c478bd9Sstevel@tonic-gate 		*(uint8_t *)symaddr |= uc;
12767c478bd9Sstevel@tonic-gate 		break;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12807c478bd9Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx8 "'.\n",
12817c478bd9Sstevel@tonic-gate 		    *(uint8_t *)symaddr);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static void
set_int16_var(uintptr_t symaddr,struct sysparam * sysp)12857c478bd9Sstevel@tonic-gate set_int16_var(uintptr_t symaddr, struct sysparam *sysp)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	uint16_t us = (uint16_t)sysp->sys_info;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
12907c478bd9Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx16
12917c478bd9Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
12927c478bd9Sstevel@tonic-gate 		    *(uint16_t *)symaddr, sysp->sys_modnam);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	switch (sysp->sys_op) {
12957c478bd9Sstevel@tonic-gate 	case SETOP_ASSIGN:
12967c478bd9Sstevel@tonic-gate 		*(uint16_t *)symaddr = us;
12977c478bd9Sstevel@tonic-gate 		break;
12987c478bd9Sstevel@tonic-gate 	case SETOP_AND:
12997c478bd9Sstevel@tonic-gate 		*(uint16_t *)symaddr &= us;
13007c478bd9Sstevel@tonic-gate 		break;
13017c478bd9Sstevel@tonic-gate 	case SETOP_OR:
13027c478bd9Sstevel@tonic-gate 		*(uint16_t *)symaddr |= us;
13037c478bd9Sstevel@tonic-gate 		break;
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13077c478bd9Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx16 "'.\n",
13087c478bd9Sstevel@tonic-gate 		    *(uint16_t *)symaddr);
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate static void
set_int32_var(uintptr_t symaddr,struct sysparam * sysp)13127c478bd9Sstevel@tonic-gate set_int32_var(uintptr_t symaddr, struct sysparam *sysp)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	uint32_t ui = (uint32_t)sysp->sys_info;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13177c478bd9Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx32
13187c478bd9Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
13197c478bd9Sstevel@tonic-gate 		    *(uint32_t *)symaddr, sysp->sys_modnam);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	switch (sysp->sys_op) {
13227c478bd9Sstevel@tonic-gate 	case SETOP_ASSIGN:
13237c478bd9Sstevel@tonic-gate 		*(uint32_t *)symaddr = ui;
13247c478bd9Sstevel@tonic-gate 		break;
13257c478bd9Sstevel@tonic-gate 	case SETOP_AND:
13267c478bd9Sstevel@tonic-gate 		*(uint32_t *)symaddr &= ui;
13277c478bd9Sstevel@tonic-gate 		break;
13287c478bd9Sstevel@tonic-gate 	case SETOP_OR:
13297c478bd9Sstevel@tonic-gate 		*(uint32_t *)symaddr |= ui;
13307c478bd9Sstevel@tonic-gate 		break;
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13347c478bd9Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx32 "'.\n",
13357c478bd9Sstevel@tonic-gate 		    *(uint32_t *)symaddr);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate static void
set_int64_var(uintptr_t symaddr,struct sysparam * sysp)13397c478bd9Sstevel@tonic-gate set_int64_var(uintptr_t symaddr, struct sysparam *sysp)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	uint64_t ul = sysp->sys_info;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13447c478bd9Sstevel@tonic-gate 		printf("OP: %x: param '%s' was '0x%" PRIx64
13457c478bd9Sstevel@tonic-gate 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
13467c478bd9Sstevel@tonic-gate 		    *(uint64_t *)symaddr, sysp->sys_modnam);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	switch (sysp->sys_op) {
13497c478bd9Sstevel@tonic-gate 	case SETOP_ASSIGN:
13507c478bd9Sstevel@tonic-gate 		*(uint64_t *)symaddr = ul;
13517c478bd9Sstevel@tonic-gate 		break;
13527c478bd9Sstevel@tonic-gate 	case SETOP_AND:
13537c478bd9Sstevel@tonic-gate 		*(uint64_t *)symaddr &= ul;
13547c478bd9Sstevel@tonic-gate 		break;
13557c478bd9Sstevel@tonic-gate 	case SETOP_OR:
13567c478bd9Sstevel@tonic-gate 		*(uint64_t *)symaddr |= ul;
13577c478bd9Sstevel@tonic-gate 		break;
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
13617c478bd9Sstevel@tonic-gate 		printf("now it is set to '0x%" PRIx64 "'.\n",
13627c478bd9Sstevel@tonic-gate 		    *(uint64_t *)symaddr);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * The next item on the line is a string value. Allocate memory for
13677c478bd9Sstevel@tonic-gate  * it and copy the string. Return 1, and set arg ptr to newly allocated
13687c478bd9Sstevel@tonic-gate  * and initialized buffer, or NULL if an error occurs.
13697c478bd9Sstevel@tonic-gate  */
13707c478bd9Sstevel@tonic-gate int
kobj_get_string(u_longlong_t * llptr,char * tchar)13717c478bd9Sstevel@tonic-gate kobj_get_string(u_longlong_t *llptr, char *tchar)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	char *cp;
13747c478bd9Sstevel@tonic-gate 	char *start = (char *)0;
13757c478bd9Sstevel@tonic-gate 	int len = 0;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	len = strlen(tchar);
13787c478bd9Sstevel@tonic-gate 	start = tchar;
13797c478bd9Sstevel@tonic-gate 	/* copy string */
13807c478bd9Sstevel@tonic-gate 	cp = vmem_alloc(mod_sysfile_arena, len + 1, VM_SLEEP);
13817c478bd9Sstevel@tonic-gate 	bzero(cp, len + 1);
13827c478bd9Sstevel@tonic-gate 	*llptr = (u_longlong_t)(uintptr_t)cp;
13837c478bd9Sstevel@tonic-gate 	for (; len > 0; len--) {
13847c478bd9Sstevel@tonic-gate 		/* convert some common escape sequences */
13857c478bd9Sstevel@tonic-gate 		if (*start == '\\') {
13867c478bd9Sstevel@tonic-gate 			switch (*(start + 1)) {
13877c478bd9Sstevel@tonic-gate 			case 't':
13887c478bd9Sstevel@tonic-gate 				/* tab */
13897c478bd9Sstevel@tonic-gate 				*cp++ = '\t';
13907c478bd9Sstevel@tonic-gate 				len--;
13917c478bd9Sstevel@tonic-gate 				start += 2;
13927c478bd9Sstevel@tonic-gate 				break;
13937c478bd9Sstevel@tonic-gate 			case 'n':
13947c478bd9Sstevel@tonic-gate 				/* new line */
13957c478bd9Sstevel@tonic-gate 				*cp++ = '\n';
13967c478bd9Sstevel@tonic-gate 				len--;
13977c478bd9Sstevel@tonic-gate 				start += 2;
13987c478bd9Sstevel@tonic-gate 				break;
13997c478bd9Sstevel@tonic-gate 			case 'b':
14007c478bd9Sstevel@tonic-gate 				/* back space */
14017c478bd9Sstevel@tonic-gate 				*cp++ = '\b';
14027c478bd9Sstevel@tonic-gate 				len--;
14037c478bd9Sstevel@tonic-gate 				start += 2;
14047c478bd9Sstevel@tonic-gate 				break;
14057c478bd9Sstevel@tonic-gate 			default:
14067c478bd9Sstevel@tonic-gate 				/* simply copy it */
14077c478bd9Sstevel@tonic-gate 				*cp++ = *start++;
14087c478bd9Sstevel@tonic-gate 				break;
14097c478bd9Sstevel@tonic-gate 			}
14107c478bd9Sstevel@tonic-gate 		} else
14117c478bd9Sstevel@tonic-gate 			*cp++ = *start++;
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 	*cp = '\0';
14147c478bd9Sstevel@tonic-gate 	return (1);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate /*
14197c478bd9Sstevel@tonic-gate  * this function frees the memory allocated by kobj_get_string
14207c478bd9Sstevel@tonic-gate  */
14217c478bd9Sstevel@tonic-gate void
kobj_free_string(void * ptr,int len)14227c478bd9Sstevel@tonic-gate kobj_free_string(void *ptr, int len)
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate 	vmem_free(mod_sysfile_arena, ptr, len);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate /*
14297c478bd9Sstevel@tonic-gate  * get a decimal octal or hex number. Handle '~' for one's complement.
14307c478bd9Sstevel@tonic-gate  */
14317c478bd9Sstevel@tonic-gate int
kobj_getvalue(const char * token,u_longlong_t * valuep)14327c478bd9Sstevel@tonic-gate kobj_getvalue(const char *token, u_longlong_t *valuep)
14337c478bd9Sstevel@tonic-gate {
14347c478bd9Sstevel@tonic-gate 	int radix;
14357c478bd9Sstevel@tonic-gate 	u_longlong_t retval = 0;
14367c478bd9Sstevel@tonic-gate 	int onescompl = 0;
14377c478bd9Sstevel@tonic-gate 	int negate = 0;
14387c478bd9Sstevel@tonic-gate 	char c;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	if (*token == '~') {
14417c478bd9Sstevel@tonic-gate 		onescompl++; /* perform one's complement on result */
14427c478bd9Sstevel@tonic-gate 		token++;
14437c478bd9Sstevel@tonic-gate 	} else if (*token == '-') {
14447c478bd9Sstevel@tonic-gate 		negate++;
14457c478bd9Sstevel@tonic-gate 		token++;
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 	if (*token == '0') {
14487c478bd9Sstevel@tonic-gate 		token++;
14497c478bd9Sstevel@tonic-gate 		c = *token;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		if (c == '\0') {
14527c478bd9Sstevel@tonic-gate 			*valuep = 0;	/* value is 0 */
14537c478bd9Sstevel@tonic-gate 			return (0);
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		if (c == 'x' || c == 'X') {
14577c478bd9Sstevel@tonic-gate 			radix = 16;
14587c478bd9Sstevel@tonic-gate 			token++;
14597c478bd9Sstevel@tonic-gate 		} else
14607c478bd9Sstevel@tonic-gate 			radix = 8;
14617c478bd9Sstevel@tonic-gate 	} else
14627c478bd9Sstevel@tonic-gate 		radix = 10;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	while ((c = *token++)) {
14657c478bd9Sstevel@tonic-gate 		switch (radix) {
14667c478bd9Sstevel@tonic-gate 		case 8:
14677c478bd9Sstevel@tonic-gate 			if (c >= '0' && c <= '7')
14687c478bd9Sstevel@tonic-gate 				c -= '0';
14697c478bd9Sstevel@tonic-gate 			else
14707c478bd9Sstevel@tonic-gate 				return (-1);	/* invalid number */
14717c478bd9Sstevel@tonic-gate 			retval = (retval << 3) + c;
14727c478bd9Sstevel@tonic-gate 			break;
14737c478bd9Sstevel@tonic-gate 		case 10:
14747c478bd9Sstevel@tonic-gate 			if (c >= '0' && c <= '9')
14757c478bd9Sstevel@tonic-gate 				c -= '0';
14767c478bd9Sstevel@tonic-gate 			else
14777c478bd9Sstevel@tonic-gate 				return (-1);	/* invalid number */
14787c478bd9Sstevel@tonic-gate 			retval = (retval * 10) + c;
14797c478bd9Sstevel@tonic-gate 			break;
14807c478bd9Sstevel@tonic-gate 		case 16:
14817c478bd9Sstevel@tonic-gate 			if (c >= 'a' && c <= 'f')
14827c478bd9Sstevel@tonic-gate 				c = c - 'a' + 10;
14837c478bd9Sstevel@tonic-gate 			else if (c >= 'A' && c <= 'F')
14847c478bd9Sstevel@tonic-gate 				c = c - 'A' + 10;
14857c478bd9Sstevel@tonic-gate 			else if (c >= '0' && c <= '9')
14867c478bd9Sstevel@tonic-gate 				c -= '0';
14877c478bd9Sstevel@tonic-gate 			else
14887c478bd9Sstevel@tonic-gate 				return (-1);	/* invalid number */
14897c478bd9Sstevel@tonic-gate 			retval = (retval << 4) + c;
14907c478bd9Sstevel@tonic-gate 			break;
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 	if (onescompl)
14947c478bd9Sstevel@tonic-gate 		retval = ~retval;
14957c478bd9Sstevel@tonic-gate 	if (negate)
14967c478bd9Sstevel@tonic-gate 		retval = -retval;
14977c478bd9Sstevel@tonic-gate 	*valuep = retval;
14987c478bd9Sstevel@tonic-gate 	return (0);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate /*
15027c478bd9Sstevel@tonic-gate  * Path to the root device and root filesystem type from
15037c478bd9Sstevel@tonic-gate  * property information derived from the boot subsystem
15047c478bd9Sstevel@tonic-gate  */
15057c478bd9Sstevel@tonic-gate void
setbootpath(char * path)15067c478bd9Sstevel@tonic-gate setbootpath(char *path)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate 	rootfs.bo_flags |= BO_VALID;
15097c478bd9Sstevel@tonic-gate 	(void) copystr(path, rootfs.bo_name, BO_MAXOBJNAME, NULL);
15107c478bd9Sstevel@tonic-gate 	BMDPRINTF(("rootfs bootpath: %s\n", rootfs.bo_name));
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate void
setbootfstype(char * fstype)15147c478bd9Sstevel@tonic-gate setbootfstype(char *fstype)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate 	(void) copystr(fstype, rootfs.bo_fstype, BO_MAXFSNAME, NULL);
15177c478bd9Sstevel@tonic-gate 	BMDPRINTF(("rootfs fstype: %s\n", rootfs.bo_fstype));
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate /*
15217c478bd9Sstevel@tonic-gate  * set parameters that can be set early during initialization.
15227c478bd9Sstevel@tonic-gate  */
15237c478bd9Sstevel@tonic-gate static void
setparams()15247c478bd9Sstevel@tonic-gate setparams()
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate 	struct sysparam *sysp;
15277c478bd9Sstevel@tonic-gate 	struct bootobj *bootobjp;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_MODDIR) {
15327c478bd9Sstevel@tonic-gate 			default_path = sysp->sys_ptr;
15337c478bd9Sstevel@tonic-gate 			continue;
15347c478bd9Sstevel@tonic-gate 		}
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		if (sysp->sys_type == MOD_SWAPDEV ||
15377c478bd9Sstevel@tonic-gate 		    sysp->sys_type == MOD_SWAPFS)
15387c478bd9Sstevel@tonic-gate 			bootobjp = &swapfile;
15397c478bd9Sstevel@tonic-gate 		else if (sysp->sys_type == MOD_ROOTFS)
15407c478bd9Sstevel@tonic-gate 			bootobjp = &rootfs;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 		switch (sysp->sys_type) {
15437c478bd9Sstevel@tonic-gate 		case MOD_SWAPDEV:
15447c478bd9Sstevel@tonic-gate 			bootobjp->bo_flags |= BO_VALID;
15457c478bd9Sstevel@tonic-gate 			(void) copystr(sysp->sys_ptr, bootobjp->bo_name,
15467c478bd9Sstevel@tonic-gate 			    BO_MAXOBJNAME, NULL);
15477c478bd9Sstevel@tonic-gate 			break;
15487c478bd9Sstevel@tonic-gate 		case MOD_ROOTFS:
15497c478bd9Sstevel@tonic-gate 		case MOD_SWAPFS:
15507c478bd9Sstevel@tonic-gate 			bootobjp->bo_flags |= BO_VALID;
15517c478bd9Sstevel@tonic-gate 			(void) copystr(sysp->sys_ptr, bootobjp->bo_fstype,
15527c478bd9Sstevel@tonic-gate 			    BO_MAXOBJNAME, NULL);
15537c478bd9Sstevel@tonic-gate 			break;
15545f10ef69SYuri Pankov 		case MOD_ROOTDEV:
15557c478bd9Sstevel@tonic-gate 		default:
15567c478bd9Sstevel@tonic-gate 			break;
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 	}
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /*
15627c478bd9Sstevel@tonic-gate  * clean up after an error.
15637c478bd9Sstevel@tonic-gate  */
15647c478bd9Sstevel@tonic-gate static void
hwc_free(struct hwc_spec * hwcp)15657c478bd9Sstevel@tonic-gate hwc_free(struct hwc_spec *hwcp)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	char *name;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	if ((name = hwcp->hwc_parent_name) != NULL)
15707c478bd9Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15717c478bd9Sstevel@tonic-gate 	if ((name = hwcp->hwc_class_name) != NULL)
15727c478bd9Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15737c478bd9Sstevel@tonic-gate 	if ((name = hwcp->hwc_devi_name) != NULL)
15747c478bd9Sstevel@tonic-gate 		kmem_free(name, strlen(name) + 1);
15757c478bd9Sstevel@tonic-gate 	i_ddi_prop_list_delete(hwcp->hwc_devi_sys_prop_ptr);
15767c478bd9Sstevel@tonic-gate 	kmem_free(hwcp, sizeof (struct hwc_spec));
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate /*
15807c478bd9Sstevel@tonic-gate  * Free a list of specs
15817c478bd9Sstevel@tonic-gate  */
15827c478bd9Sstevel@tonic-gate void
hwc_free_spec_list(struct hwc_spec * list)15837c478bd9Sstevel@tonic-gate hwc_free_spec_list(struct hwc_spec *list)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	while (list) {
15867c478bd9Sstevel@tonic-gate 		struct hwc_spec *tmp = list;
15877c478bd9Sstevel@tonic-gate 		list = tmp->hwc_next;
15887c478bd9Sstevel@tonic-gate 		hwc_free(tmp);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate struct val_list {
15937c478bd9Sstevel@tonic-gate 	struct val_list *val_next;
159488c958b4Sprabahar 	enum {
159588c958b4Sprabahar 		VAL_STRING,
159688c958b4Sprabahar 		VAL_INTEGER
159788c958b4Sprabahar 	} val_type;
15987c478bd9Sstevel@tonic-gate 	int		val_size;
15997c478bd9Sstevel@tonic-gate 	union {
16007c478bd9Sstevel@tonic-gate 		char *string;
16017c478bd9Sstevel@tonic-gate 		int integer;
16027c478bd9Sstevel@tonic-gate 	} val;
16037c478bd9Sstevel@tonic-gate };
16047c478bd9Sstevel@tonic-gate 
160530ef842dSbmc static struct val_list *
add_val(struct val_list ** val_listp,struct val_list * tail,int val_type,caddr_t val)160630ef842dSbmc add_val(struct val_list **val_listp, struct val_list *tail,
160730ef842dSbmc     int val_type, caddr_t val)
16087c478bd9Sstevel@tonic-gate {
160988c958b4Sprabahar 	struct val_list *new_val;
161088c958b4Sprabahar #ifdef DEBUG
161188c958b4Sprabahar 	struct val_list *listp = *val_listp;
161288c958b4Sprabahar #endif
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	new_val = kmem_alloc(sizeof (struct val_list), KM_SLEEP);
16157c478bd9Sstevel@tonic-gate 	new_val->val_next = NULL;
161688c958b4Sprabahar 	if ((new_val->val_type = val_type) == VAL_STRING) {
16177c478bd9Sstevel@tonic-gate 		new_val->val_size = strlen((char *)val) + 1;
161888c958b4Sprabahar 		new_val->val.string = kmem_alloc(new_val->val_size, KM_SLEEP);
161988c958b4Sprabahar 		(void) strcpy(new_val->val.string, (char *)val);
16207c478bd9Sstevel@tonic-gate 	} else {
16217c478bd9Sstevel@tonic-gate 		new_val->val_size = sizeof (int);
16227c478bd9Sstevel@tonic-gate 		new_val->val.integer = (int)(uintptr_t)val;
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 
162530ef842dSbmc 	ASSERT((listp == NULL && tail == NULL) ||
162630ef842dSbmc 	    (listp != NULL && tail != NULL));
162730ef842dSbmc 
162830ef842dSbmc 	if (tail != NULL) {
162930ef842dSbmc 		ASSERT(tail->val_next == NULL);
163030ef842dSbmc 		tail->val_next = new_val;
16317c478bd9Sstevel@tonic-gate 	} else {
16327c478bd9Sstevel@tonic-gate 		*val_listp = new_val;
16337c478bd9Sstevel@tonic-gate 	}
163430ef842dSbmc 
163530ef842dSbmc 	return (new_val);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
163888c958b4Sprabahar static void
free_val_list(struct val_list * head)163988c958b4Sprabahar free_val_list(struct val_list *head)
164088c958b4Sprabahar {
164188c958b4Sprabahar 	struct val_list *tval_list;
164288c958b4Sprabahar 
164388c958b4Sprabahar 	for (/* CSTYLED */; head != NULL; /* CSTYLED */) {
164488c958b4Sprabahar 		tval_list = head;
164588c958b4Sprabahar 		head = head->val_next;
164688c958b4Sprabahar 		if (tval_list->val_type == VAL_STRING)
164788c958b4Sprabahar 			kmem_free(tval_list->val.string, tval_list->val_size);
164888c958b4Sprabahar 		kmem_free(tval_list, sizeof (struct val_list));
164988c958b4Sprabahar 	}
165088c958b4Sprabahar }
165188c958b4Sprabahar 
16527c478bd9Sstevel@tonic-gate /*
16537c478bd9Sstevel@tonic-gate  * make sure there are no reserved IEEE 1275 characters (except
16547c478bd9Sstevel@tonic-gate  * for uppercase characters).
16557c478bd9Sstevel@tonic-gate  */
16567c478bd9Sstevel@tonic-gate static int
valid_prop_name(char * name)16577c478bd9Sstevel@tonic-gate valid_prop_name(char *name)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	int i;
16607c478bd9Sstevel@tonic-gate 	int len = strlen(name);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
16637c478bd9Sstevel@tonic-gate 		if (name[i] < 0x21 ||
1664b1b8ab34Slling 		    name[i] == '/' ||
1665b1b8ab34Slling 		    name[i] == '\\' ||
1666b1b8ab34Slling 		    name[i] == ':' ||
1667b1b8ab34Slling 		    name[i] == '[' ||
1668b1b8ab34Slling 		    name[i] == ']' ||
1669b1b8ab34Slling 		    name[i] == '@')
1670b1b8ab34Slling 			return (0);
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 	return (1);
16737c478bd9Sstevel@tonic-gate }
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate static void
make_prop(struct _buf * file,dev_info_t * devi,char * name,struct val_list * val)16767c478bd9Sstevel@tonic-gate make_prop(struct _buf *file, dev_info_t *devi, char *name, struct val_list *val)
16777c478bd9Sstevel@tonic-gate {
16787c478bd9Sstevel@tonic-gate 	int propcnt = 0, val_type;
16797c478bd9Sstevel@tonic-gate 	struct val_list *vl, *tvl;
16807c478bd9Sstevel@tonic-gate 	caddr_t valbuf = NULL;
16817c478bd9Sstevel@tonic-gate 	char **valsp;
16827c478bd9Sstevel@tonic-gate 	int *valip;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if (name == NULL)
16857c478bd9Sstevel@tonic-gate 		return;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate #ifdef DEBUG
16887c478bd9Sstevel@tonic-gate 	parse_debug(NULL, "%s", name);
16897c478bd9Sstevel@tonic-gate #endif
16907c478bd9Sstevel@tonic-gate 	if (!valid_prop_name(name)) {
16917c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "invalid property name '%s'", name);
16927c478bd9Sstevel@tonic-gate 		return;
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 	if (val) {
16957c478bd9Sstevel@tonic-gate 		for (vl = val, val_type = vl->val_type; vl; vl = vl->val_next) {
16967c478bd9Sstevel@tonic-gate 			if (val_type != vl->val_type) {
16977c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "Mixed types in value list");
16987c478bd9Sstevel@tonic-gate 				return;
16997c478bd9Sstevel@tonic-gate 			}
17007c478bd9Sstevel@tonic-gate 			propcnt++;
17017c478bd9Sstevel@tonic-gate 		}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 		vl = val;
17047c478bd9Sstevel@tonic-gate 
170588c958b4Sprabahar 		if (val_type == VAL_INTEGER) {
17067c478bd9Sstevel@tonic-gate 			valip = (int *)kmem_alloc(
17077c478bd9Sstevel@tonic-gate 			    (propcnt * sizeof (int)), KM_SLEEP);
17087c478bd9Sstevel@tonic-gate 			valbuf = (caddr_t)valip;
17097c478bd9Sstevel@tonic-gate 			while (vl) {
17107c478bd9Sstevel@tonic-gate 				tvl = vl;
17117c478bd9Sstevel@tonic-gate 				vl = vl->val_next;
17127c478bd9Sstevel@tonic-gate #ifdef DEBUG
17137c478bd9Sstevel@tonic-gate 				parse_debug(NULL, " %x",  tvl->val.integer);
17147c478bd9Sstevel@tonic-gate #endif
17157c478bd9Sstevel@tonic-gate 				*valip = tvl->val.integer;
17167c478bd9Sstevel@tonic-gate 				valip++;
17177c478bd9Sstevel@tonic-gate 			}
17187c478bd9Sstevel@tonic-gate 			/* restore valip */
17197c478bd9Sstevel@tonic-gate 			valip = (int *)valbuf;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 			/* create the property */
17227c478bd9Sstevel@tonic-gate 			if (e_ddi_prop_update_int_array(DDI_DEV_T_NONE, devi,
17237c478bd9Sstevel@tonic-gate 			    name, valip, propcnt) != DDI_PROP_SUCCESS) {
17247c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
17257c478bd9Sstevel@tonic-gate 				    "cannot create property %s", name);
17267c478bd9Sstevel@tonic-gate 			}
17277c478bd9Sstevel@tonic-gate 			/* cleanup */
17287c478bd9Sstevel@tonic-gate 			kmem_free(valip, (propcnt * sizeof (int)));
172988c958b4Sprabahar 		} else if (val_type == VAL_STRING) {
17307c478bd9Sstevel@tonic-gate 			valsp = (char **)kmem_alloc(
17317c478bd9Sstevel@tonic-gate 			    ((propcnt + 1) * sizeof (char *)), KM_SLEEP);
17327c478bd9Sstevel@tonic-gate 			valbuf = (caddr_t)valsp;
17337c478bd9Sstevel@tonic-gate 			while (vl) {
17347c478bd9Sstevel@tonic-gate 				tvl = vl;
17357c478bd9Sstevel@tonic-gate 				vl = vl->val_next;
17367c478bd9Sstevel@tonic-gate #ifdef DEBUG
17377c478bd9Sstevel@tonic-gate 				parse_debug(NULL, " %s", tvl->val.string);
17387c478bd9Sstevel@tonic-gate #endif
17397c478bd9Sstevel@tonic-gate 				*valsp = tvl->val.string;
17407c478bd9Sstevel@tonic-gate 				valsp++;
17417c478bd9Sstevel@tonic-gate 			}
17427c478bd9Sstevel@tonic-gate 			/* terminate array with NULL */
17437c478bd9Sstevel@tonic-gate 			*valsp = NULL;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 			/* restore valsp */
17467c478bd9Sstevel@tonic-gate 			valsp = (char **)valbuf;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 			/* create the property */
17497c478bd9Sstevel@tonic-gate 			if (e_ddi_prop_update_string_array(DDI_DEV_T_NONE,
17507c478bd9Sstevel@tonic-gate 			    devi, name, valsp, propcnt)
17517c478bd9Sstevel@tonic-gate 			    != DDI_PROP_SUCCESS) {
17527c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
17537c478bd9Sstevel@tonic-gate 				    "cannot create property %s", name);
17547c478bd9Sstevel@tonic-gate 			}
17557c478bd9Sstevel@tonic-gate 			/* Clean up */
17567c478bd9Sstevel@tonic-gate 			kmem_free(valsp, ((propcnt + 1) * sizeof (char *)));
17577c478bd9Sstevel@tonic-gate 		} else {
17587c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Invalid property type");
17597c478bd9Sstevel@tonic-gate 			return;
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 	} else {
17627c478bd9Sstevel@tonic-gate 		/*
17637c478bd9Sstevel@tonic-gate 		 * No value was passed in with property so we will assume
17647c478bd9Sstevel@tonic-gate 		 * it is a "boolean" property and create an integer
17657c478bd9Sstevel@tonic-gate 		 * property with 0 value.
17667c478bd9Sstevel@tonic-gate 		 */
17677c478bd9Sstevel@tonic-gate #ifdef DEBUG
17687c478bd9Sstevel@tonic-gate 		parse_debug(NULL, "\n");
17697c478bd9Sstevel@tonic-gate #endif
17707c478bd9Sstevel@tonic-gate 		if (e_ddi_prop_update_int(DDI_DEV_T_NONE, devi, name, 0)
17717c478bd9Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS) {
17727c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file,
17737c478bd9Sstevel@tonic-gate 			    "cannot create property %s", name);
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate static char omit_err[] = "(the ';' may have been omitted on previous spec!)";
17797c478bd9Sstevel@tonic-gate static char prnt_err[] = "'parent' property already specified";
17807c478bd9Sstevel@tonic-gate static char nm_err[] = "'name' property already specified";
17817c478bd9Sstevel@tonic-gate static char class_err[] = "'class' property already specified";
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate typedef enum {
17847c478bd9Sstevel@tonic-gate 	hwc_begin, parent, drvname, drvclass, prop,
17857c478bd9Sstevel@tonic-gate 	parent_equals, name_equals, drvclass_equals,
17867c478bd9Sstevel@tonic-gate 	parent_equals_string, name_equals_string,
17877c478bd9Sstevel@tonic-gate 	drvclass_equals_string,
17887c478bd9Sstevel@tonic-gate 	prop_equals, prop_equals_string, prop_equals_integer,
17897c478bd9Sstevel@tonic-gate 	prop_equals_string_comma, prop_equals_integer_comma
17907c478bd9Sstevel@tonic-gate } hwc_state_t;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate static struct hwc_spec *
get_hwc_spec(struct _buf * file,char * tokbuf,size_t linesize)17937c478bd9Sstevel@tonic-gate get_hwc_spec(struct _buf *file, char *tokbuf, size_t linesize)
17947c478bd9Sstevel@tonic-gate {
179588c958b4Sprabahar 	char *prop_name;
17967c478bd9Sstevel@tonic-gate 	token_t token;
17977c478bd9Sstevel@tonic-gate 	struct hwc_spec *hwcp;
17987c478bd9Sstevel@tonic-gate 	struct dev_info *devi;
179930ef842dSbmc 	struct val_list *val_list, *tail;
18007c478bd9Sstevel@tonic-gate 	hwc_state_t state;
18017c478bd9Sstevel@tonic-gate 	u_longlong_t ival;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	hwcp = kmem_zalloc(sizeof (*hwcp), KM_SLEEP);
18047c478bd9Sstevel@tonic-gate 	devi = kmem_zalloc(sizeof (*devi), KM_SLEEP);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	state = hwc_begin;
18077c478bd9Sstevel@tonic-gate 	token = NAME;
18087c478bd9Sstevel@tonic-gate 	prop_name = NULL;
18097c478bd9Sstevel@tonic-gate 	val_list = NULL;
181030ef842dSbmc 	tail = NULL;
18117c478bd9Sstevel@tonic-gate 	do {
18127c478bd9Sstevel@tonic-gate #ifdef DEBUG
18137c478bd9Sstevel@tonic-gate 		parse_debug(NULL, "state 0x%x\n", state);
18147c478bd9Sstevel@tonic-gate #endif
18157c478bd9Sstevel@tonic-gate 		switch (token) {
18167c478bd9Sstevel@tonic-gate 		case NAME:
18177c478bd9Sstevel@tonic-gate 			switch (state) {
18187c478bd9Sstevel@tonic-gate 			case prop:
18197c478bd9Sstevel@tonic-gate 			case prop_equals_string:
18207c478bd9Sstevel@tonic-gate 			case prop_equals_integer:
18217c478bd9Sstevel@tonic-gate 				make_prop(file, (dev_info_t *)devi,
18227c478bd9Sstevel@tonic-gate 				    prop_name, val_list);
182388c958b4Sprabahar 				if (prop_name) {
182488c958b4Sprabahar 					kmem_free(prop_name,
182588c958b4Sprabahar 					    strlen(prop_name) + 1);
182688c958b4Sprabahar 					prop_name = NULL;
182788c958b4Sprabahar 				}
182888c958b4Sprabahar 				if (val_list) {
182988c958b4Sprabahar 					free_val_list(val_list);
183088c958b4Sprabahar 					val_list = NULL;
183188c958b4Sprabahar 				}
183230ef842dSbmc 				tail = NULL;
18337c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
18347c478bd9Sstevel@tonic-gate 			case hwc_begin:
18357c478bd9Sstevel@tonic-gate 				if (strcmp(tokbuf, "PARENT") == 0 ||
18367c478bd9Sstevel@tonic-gate 				    strcmp(tokbuf, "parent") == 0) {
18377c478bd9Sstevel@tonic-gate 					state = parent;
18387c478bd9Sstevel@tonic-gate 				} else if (strcmp(tokbuf, "NAME") == 0 ||
18397c478bd9Sstevel@tonic-gate 				    strcmp(tokbuf, "name") == 0) {
18407c478bd9Sstevel@tonic-gate 					state = drvname;
18417c478bd9Sstevel@tonic-gate 				} else if (strcmp(tokbuf, "CLASS") == 0 ||
18427c478bd9Sstevel@tonic-gate 				    strcmp(tokbuf, "class") == 0) {
18437c478bd9Sstevel@tonic-gate 					state = drvclass;
18447c478bd9Sstevel@tonic-gate 					prop_name = kmem_alloc(strlen(tokbuf) +
1845b1b8ab34Slling 					    1, KM_SLEEP);
18467c478bd9Sstevel@tonic-gate 					(void) strcpy(prop_name, tokbuf);
18477c478bd9Sstevel@tonic-gate 				} else {
18487c478bd9Sstevel@tonic-gate 					state = prop;
18497c478bd9Sstevel@tonic-gate 					prop_name = kmem_alloc(strlen(tokbuf) +
1850b1b8ab34Slling 					    1, KM_SLEEP);
18517c478bd9Sstevel@tonic-gate 					(void) strcpy(prop_name, tokbuf);
18527c478bd9Sstevel@tonic-gate 				}
18537c478bd9Sstevel@tonic-gate 				break;
18547c478bd9Sstevel@tonic-gate 			default:
18557c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
18567c478bd9Sstevel@tonic-gate 			}
18577c478bd9Sstevel@tonic-gate 			break;
18587c478bd9Sstevel@tonic-gate 		case EQUALS:
18597c478bd9Sstevel@tonic-gate 			switch (state) {
18607c478bd9Sstevel@tonic-gate 			case drvname:
18617c478bd9Sstevel@tonic-gate 				state = name_equals;
18627c478bd9Sstevel@tonic-gate 				break;
18637c478bd9Sstevel@tonic-gate 			case parent:
18647c478bd9Sstevel@tonic-gate 				state = parent_equals;
18657c478bd9Sstevel@tonic-gate 				break;
18667c478bd9Sstevel@tonic-gate 			case drvclass:
18677c478bd9Sstevel@tonic-gate 				state = drvclass_equals;
18687c478bd9Sstevel@tonic-gate 				break;
18697c478bd9Sstevel@tonic-gate 			case prop:
18707c478bd9Sstevel@tonic-gate 				state = prop_equals;
18717c478bd9Sstevel@tonic-gate 				break;
18727c478bd9Sstevel@tonic-gate 			default:
18737c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
18747c478bd9Sstevel@tonic-gate 			}
18757c478bd9Sstevel@tonic-gate 			break;
18767c478bd9Sstevel@tonic-gate 		case STRING:
18777c478bd9Sstevel@tonic-gate 			switch (state) {
18787c478bd9Sstevel@tonic-gate 			case name_equals:
18797c478bd9Sstevel@tonic-gate 				if (ddi_get_name((dev_info_t *)devi)) {
18807c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, "%s %s",
1881b1b8ab34Slling 					    nm_err, omit_err);
18827c478bd9Sstevel@tonic-gate 					goto bad;
18837c478bd9Sstevel@tonic-gate 				}
188488c958b4Sprabahar 				devi->devi_name = kmem_alloc(strlen(tokbuf) + 1,
188588c958b4Sprabahar 				    KM_SLEEP);
188688c958b4Sprabahar 				(void) strcpy(devi->devi_name, tokbuf);
18877c478bd9Sstevel@tonic-gate 				state = hwc_begin;
18887c478bd9Sstevel@tonic-gate 				break;
18897c478bd9Sstevel@tonic-gate 			case parent_equals:
18907c478bd9Sstevel@tonic-gate 				if (hwcp->hwc_parent_name) {
18917c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, "%s %s",
1892b1b8ab34Slling 					    prnt_err, omit_err);
18937c478bd9Sstevel@tonic-gate 					goto bad;
18947c478bd9Sstevel@tonic-gate 				}
189588c958b4Sprabahar 				hwcp->hwc_parent_name = kmem_alloc(strlen
189688c958b4Sprabahar 				    (tokbuf) + 1, KM_SLEEP);
189788c958b4Sprabahar 				(void) strcpy(hwcp->hwc_parent_name, tokbuf);
18987c478bd9Sstevel@tonic-gate 				state = hwc_begin;
18997c478bd9Sstevel@tonic-gate 				break;
19007c478bd9Sstevel@tonic-gate 			case drvclass_equals:
19017c478bd9Sstevel@tonic-gate 				if (hwcp->hwc_class_name) {
19027c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, class_err);
19037c478bd9Sstevel@tonic-gate 					goto bad;
19047c478bd9Sstevel@tonic-gate 				}
190588c958b4Sprabahar 				hwcp->hwc_class_name = kmem_alloc(
190688c958b4Sprabahar 				    strlen(tokbuf) + 1, KM_SLEEP);
190788c958b4Sprabahar 				(void) strcpy(hwcp->hwc_class_name, tokbuf);
19087c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
19097c478bd9Sstevel@tonic-gate 			case prop_equals:
19107c478bd9Sstevel@tonic-gate 			case prop_equals_string_comma:
191188c958b4Sprabahar 				tail = add_val(&val_list, tail, VAL_STRING,
191288c958b4Sprabahar 				    tokbuf);
19137c478bd9Sstevel@tonic-gate 				state = prop_equals_string;
19147c478bd9Sstevel@tonic-gate 				break;
19157c478bd9Sstevel@tonic-gate 			default:
19167c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19177c478bd9Sstevel@tonic-gate 			}
19187c478bd9Sstevel@tonic-gate 			break;
19197c478bd9Sstevel@tonic-gate 		case HEXVAL:
19207c478bd9Sstevel@tonic-gate 		case DECVAL:
19217c478bd9Sstevel@tonic-gate 			switch (state) {
19227c478bd9Sstevel@tonic-gate 			case prop_equals:
19237c478bd9Sstevel@tonic-gate 			case prop_equals_integer_comma:
19247c478bd9Sstevel@tonic-gate 				(void) kobj_getvalue(tokbuf, &ival);
192530ef842dSbmc 				tail = add_val(&val_list, tail,
192688c958b4Sprabahar 				    VAL_INTEGER, (caddr_t)(uintptr_t)ival);
19277c478bd9Sstevel@tonic-gate 				state = prop_equals_integer;
19287c478bd9Sstevel@tonic-gate 				break;
19297c478bd9Sstevel@tonic-gate 			default:
19307c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19317c478bd9Sstevel@tonic-gate 			}
19327c478bd9Sstevel@tonic-gate 			break;
19337c478bd9Sstevel@tonic-gate 		case COMMA:
19347c478bd9Sstevel@tonic-gate 			switch (state) {
19357c478bd9Sstevel@tonic-gate 			case prop_equals_string:
19367c478bd9Sstevel@tonic-gate 				state = prop_equals_string_comma;
19377c478bd9Sstevel@tonic-gate 				break;
19387c478bd9Sstevel@tonic-gate 			case prop_equals_integer:
19397c478bd9Sstevel@tonic-gate 				state = prop_equals_integer_comma;
19407c478bd9Sstevel@tonic-gate 				break;
19417c478bd9Sstevel@tonic-gate 			default:
19427c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19437c478bd9Sstevel@tonic-gate 			}
19447c478bd9Sstevel@tonic-gate 			break;
19457c478bd9Sstevel@tonic-gate 		case NEWLINE:
19467c478bd9Sstevel@tonic-gate 			kobj_newline(file);
19477c478bd9Sstevel@tonic-gate 			break;
19487c478bd9Sstevel@tonic-gate 		case POUND:
19491ca93273Seota 			/*
19501ca93273Seota 			 * Skip comments.
19511ca93273Seota 			 */
19527c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
19537c478bd9Sstevel@tonic-gate 			break;
19547c478bd9Sstevel@tonic-gate 		case EOF:
19557c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "Unexpected EOF");
19567c478bd9Sstevel@tonic-gate 			goto bad;
19577c478bd9Sstevel@tonic-gate 		default:
19587c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
19597c478bd9Sstevel@tonic-gate 			goto bad;
19607c478bd9Sstevel@tonic-gate 		}
19617c478bd9Sstevel@tonic-gate 	} while ((token = kobj_lex(file, tokbuf, linesize)) != SEMICOLON);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	switch (state) {
19647c478bd9Sstevel@tonic-gate 	case prop:
19657c478bd9Sstevel@tonic-gate 	case prop_equals_string:
19667c478bd9Sstevel@tonic-gate 	case prop_equals_integer:
19677c478bd9Sstevel@tonic-gate 		make_prop(file, (dev_info_t *)devi,
1968b1b8ab34Slling 		    prop_name, val_list);
19697c478bd9Sstevel@tonic-gate 		break;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	case hwc_begin:
19727c478bd9Sstevel@tonic-gate 		break;
19737c478bd9Sstevel@tonic-gate 	default:
19747c478bd9Sstevel@tonic-gate 		kobj_file_err(CE_WARN, file, "Unexpected end of line");
19757c478bd9Sstevel@tonic-gate 		break;
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/* copy 2 relevant members of devi to hwcp */
19797c478bd9Sstevel@tonic-gate 	hwcp->hwc_devi_sys_prop_ptr = devi->devi_sys_prop_ptr;
19807c478bd9Sstevel@tonic-gate 	hwcp->hwc_devi_name = devi->devi_name;
19817c478bd9Sstevel@tonic-gate 
198288c958b4Sprabahar 	if (prop_name)
198388c958b4Sprabahar 		kmem_free(prop_name, strlen(prop_name) + 1);
198488c958b4Sprabahar 	if (val_list)
198588c958b4Sprabahar 		free_val_list(val_list);
198688c958b4Sprabahar 
19877c478bd9Sstevel@tonic-gate 	kmem_free(devi, sizeof (struct dev_info));
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	return (hwcp);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate bad:
199288c958b4Sprabahar 	if (prop_name)
199388c958b4Sprabahar 		kmem_free(prop_name, strlen(prop_name) + 1);
199488c958b4Sprabahar 	if (val_list)
199588c958b4Sprabahar 		free_val_list(val_list);
199688c958b4Sprabahar 
199788c958b4Sprabahar 	hwc_free(hwcp);
199888c958b4Sprabahar 
199988c958b4Sprabahar 	if (devi->devi_name)
200088c958b4Sprabahar 		kmem_free(devi->devi_name, strlen(devi->devi_name) + 1);
200188c958b4Sprabahar 
200288c958b4Sprabahar 	kmem_free(devi, sizeof (struct dev_info));
200388c958b4Sprabahar 
20047c478bd9Sstevel@tonic-gate 	return (NULL);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate  * This is the primary kernel interface to parse driver.conf files.
20097c478bd9Sstevel@tonic-gate  *
20107c478bd9Sstevel@tonic-gate  * Yet another bigstk thread handoff due to deep kernel stacks when booting
20117c478bd9Sstevel@tonic-gate  * cache-only-clients.
20127c478bd9Sstevel@tonic-gate  */
20137c478bd9Sstevel@tonic-gate int
hwc_parse(char * fname,struct par_list ** pl,ddi_prop_t ** props)20147c478bd9Sstevel@tonic-gate hwc_parse(char *fname, struct par_list **pl, ddi_prop_t **props)
20157c478bd9Sstevel@tonic-gate {
20167c478bd9Sstevel@tonic-gate 	int ret;
20177c478bd9Sstevel@tonic-gate 	struct hwc_parse_mt *pltp = hwc_parse_mtalloc(fname, pl, props);
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	if (curthread != &t0) {
20207c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
20217c478bd9Sstevel@tonic-gate 		    hwc_parse_thread, pltp, 0, &p0, TS_RUN, maxclsyspri);
20227c478bd9Sstevel@tonic-gate 		sema_p(&pltp->sema);
20237c478bd9Sstevel@tonic-gate 	} else {
20247c478bd9Sstevel@tonic-gate 		pltp->rv = hwc_parse_now(fname, pl, props);
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 	ret = pltp->rv;
20277c478bd9Sstevel@tonic-gate 	hwc_parse_mtfree(pltp);
20287c478bd9Sstevel@tonic-gate 	return (ret);
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate  * Calls to hwc_parse() are handled off to this routine in a separate
20337c478bd9Sstevel@tonic-gate  * thread.
20347c478bd9Sstevel@tonic-gate  */
20357c478bd9Sstevel@tonic-gate static void
hwc_parse_thread(struct hwc_parse_mt * pltp)20367c478bd9Sstevel@tonic-gate hwc_parse_thread(struct hwc_parse_mt *pltp)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	kmutex_t	cpr_lk;
20397c478bd9Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
20427c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "hwc_parse");
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/*
20457c478bd9Sstevel@tonic-gate 	 * load and parse the .conf file
20467c478bd9Sstevel@tonic-gate 	 * return the hwc_spec list (if any) to the creator of this thread
20477c478bd9Sstevel@tonic-gate 	 */
20487c478bd9Sstevel@tonic-gate 	pltp->rv = hwc_parse_now(pltp->name, pltp->pl, pltp->props);
20497c478bd9Sstevel@tonic-gate 	sema_v(&pltp->sema);
20507c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
20517c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
20527c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
20537c478bd9Sstevel@tonic-gate 	thread_exit();
20547c478bd9Sstevel@tonic-gate }
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate /*
20577c478bd9Sstevel@tonic-gate  * allocate and initialize a hwc_parse thread control structure
20587c478bd9Sstevel@tonic-gate  */
20597c478bd9Sstevel@tonic-gate static struct hwc_parse_mt *
hwc_parse_mtalloc(char * name,struct par_list ** pl,ddi_prop_t ** props)20607c478bd9Sstevel@tonic-gate hwc_parse_mtalloc(char *name, struct par_list **pl, ddi_prop_t **props)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	struct hwc_parse_mt *pltp = kmem_zalloc(sizeof (*pltp), KM_SLEEP);
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	pltp->name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
20677c478bd9Sstevel@tonic-gate 	bcopy(name, pltp->name, strlen(name) + 1);
20687c478bd9Sstevel@tonic-gate 	pltp->pl = pl;
20697c478bd9Sstevel@tonic-gate 	pltp->props = props;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	sema_init(&pltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
20727c478bd9Sstevel@tonic-gate 	return (pltp);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate /*
20767c478bd9Sstevel@tonic-gate  * free a hwc_parse thread control structure
20777c478bd9Sstevel@tonic-gate  */
20787c478bd9Sstevel@tonic-gate static void
hwc_parse_mtfree(struct hwc_parse_mt * pltp)20797c478bd9Sstevel@tonic-gate hwc_parse_mtfree(struct hwc_parse_mt *pltp)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	sema_destroy(&pltp->sema);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	kmem_free(pltp->name, strlen(pltp->name) + 1);
20847c478bd9Sstevel@tonic-gate 	kmem_free(pltp, sizeof (*pltp));
20857c478bd9Sstevel@tonic-gate }
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate /*
20887c478bd9Sstevel@tonic-gate  * hwc_parse -- parse an hwconf file.  Ignore error lines and parse
20897c478bd9Sstevel@tonic-gate  * as much as possible.
20907c478bd9Sstevel@tonic-gate  */
20917c478bd9Sstevel@tonic-gate static int
hwc_parse_now(char * fname,struct par_list ** pl,ddi_prop_t ** props)20927c478bd9Sstevel@tonic-gate hwc_parse_now(char *fname, struct par_list **pl, ddi_prop_t **props)
20937c478bd9Sstevel@tonic-gate {
20947c478bd9Sstevel@tonic-gate 	struct _buf *file;
20957c478bd9Sstevel@tonic-gate 	struct hwc_spec *hwcp;
20967c478bd9Sstevel@tonic-gate 	char *tokval;
20977c478bd9Sstevel@tonic-gate 	token_t token;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	/*
21007c478bd9Sstevel@tonic-gate 	 * Don't use kobj_open_path's use_moddir_suffix option, we only
21017c478bd9Sstevel@tonic-gate 	 * expect to find conf files in the base module directory, not
21027c478bd9Sstevel@tonic-gate 	 * an ISA-specific subdirectory.
21037c478bd9Sstevel@tonic-gate 	 */
21047c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_path(fname, 1, 0)) == (struct _buf *)-1) {
21057c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_ERRMSG)
21067c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Cannot open %s", fname);
21077c478bd9Sstevel@tonic-gate 		return (-1);
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	/*
21117c478bd9Sstevel@tonic-gate 	 * Initialize variables
21127c478bd9Sstevel@tonic-gate 	 */
21137c478bd9Sstevel@tonic-gate 	tokval = kmem_alloc(MAX_HWC_LINESIZE, KM_SLEEP);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	while ((token = kobj_lex(file, tokval, MAX_HWC_LINESIZE)) != EOF) {
21167c478bd9Sstevel@tonic-gate 		switch (token) {
21177c478bd9Sstevel@tonic-gate 		case POUND:
21187c478bd9Sstevel@tonic-gate 			/*
21197c478bd9Sstevel@tonic-gate 			 * Skip comments.
21207c478bd9Sstevel@tonic-gate 			 */
21217c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
21227c478bd9Sstevel@tonic-gate 			break;
21237c478bd9Sstevel@tonic-gate 		case NAME:
21247c478bd9Sstevel@tonic-gate 			hwcp = get_hwc_spec(file, tokval, MAX_HWC_LINESIZE);
21257c478bd9Sstevel@tonic-gate 			if (hwcp == NULL)
21267c478bd9Sstevel@tonic-gate 				break;
21277c478bd9Sstevel@tonic-gate 			/*
21287c478bd9Sstevel@tonic-gate 			 * No devi_name indicates global property.
21297c478bd9Sstevel@tonic-gate 			 * Make sure parent and class not NULL.
21307c478bd9Sstevel@tonic-gate 			 */
21317c478bd9Sstevel@tonic-gate 			if (hwcp->hwc_devi_name == NULL) {
21327c478bd9Sstevel@tonic-gate 				if (hwcp->hwc_parent_name ||
21337c478bd9Sstevel@tonic-gate 				    hwcp->hwc_class_name) {
21347c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
21357c478bd9Sstevel@tonic-gate 					    "missing name attribute");
21367c478bd9Sstevel@tonic-gate 					hwc_free(hwcp);
21377c478bd9Sstevel@tonic-gate 					continue;
21387c478bd9Sstevel@tonic-gate 				}
21397c478bd9Sstevel@tonic-gate 				/* Add to global property list */
21407c478bd9Sstevel@tonic-gate 				add_props(hwcp, props);
21417c478bd9Sstevel@tonic-gate 				break;
21427c478bd9Sstevel@tonic-gate 			}
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 			/*
21457c478bd9Sstevel@tonic-gate 			 * This is a node spec, either parent or class
21467c478bd9Sstevel@tonic-gate 			 * must be specified.
21477c478bd9Sstevel@tonic-gate 			 */
21487c478bd9Sstevel@tonic-gate 			if ((hwcp->hwc_parent_name == NULL) &&
21497c478bd9Sstevel@tonic-gate 			    (hwcp->hwc_class_name == NULL)) {
21507c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
21517c478bd9Sstevel@tonic-gate 				    "missing parent or class attribute");
21527c478bd9Sstevel@tonic-gate 				hwc_free(hwcp);
21537c478bd9Sstevel@tonic-gate 				continue;
21547c478bd9Sstevel@tonic-gate 			}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 			/* add to node spec list */
21577c478bd9Sstevel@tonic-gate 			add_spec(hwcp, pl);
21587c478bd9Sstevel@tonic-gate 			break;
21597c478bd9Sstevel@tonic-gate 		case NEWLINE:
21607c478bd9Sstevel@tonic-gate 			kobj_newline(file);
21617c478bd9Sstevel@tonic-gate 			break;
21627c478bd9Sstevel@tonic-gate 		default:
21637c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokval);
21647c478bd9Sstevel@tonic-gate 			break;
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 	/*
21687c478bd9Sstevel@tonic-gate 	 * XXX - Check for clean termination.
21697c478bd9Sstevel@tonic-gate 	 */
21707c478bd9Sstevel@tonic-gate 	kmem_free(tokval, MAX_HWC_LINESIZE);
21717c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
21727c478bd9Sstevel@tonic-gate 	return (0);	/* always return success */
21737c478bd9Sstevel@tonic-gate }
21747c478bd9Sstevel@tonic-gate 
2175*eb9a1df2SHans Rosenfeld static void
parse_aliases(struct bind ** bhash,struct _buf * file)2176*eb9a1df2SHans Rosenfeld parse_aliases(struct bind **bhash, struct _buf *file)
21777c478bd9Sstevel@tonic-gate {
21787c478bd9Sstevel@tonic-gate 	enum {
21797c478bd9Sstevel@tonic-gate 		AL_NEW, AL_DRVNAME, AL_DRVNAME_COMMA, AL_ALIAS, AL_ALIAS_COMMA
21807c478bd9Sstevel@tonic-gate 	} state;
21817c478bd9Sstevel@tonic-gate 
2182f4da9be0Scth 	char tokbuf[MAXPATHLEN];
2183f4da9be0Scth 	char drvbuf[MAXPATHLEN];
21847c478bd9Sstevel@tonic-gate 	token_t token;
21857c478bd9Sstevel@tonic-gate 	major_t major;
21867c478bd9Sstevel@tonic-gate 	int done = 0;
21877c478bd9Sstevel@tonic-gate 	static char dupwarn[] = "!Driver alias \"%s\" conflicts with "
21887c478bd9Sstevel@tonic-gate 	    "an existing driver name or alias.";
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	state = AL_NEW;
2191a204de77Scth 	major = DDI_MAJOR_T_NONE;
21927c478bd9Sstevel@tonic-gate 	while (!done) {
21937c478bd9Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
21947c478bd9Sstevel@tonic-gate 		switch (token) {
21957c478bd9Sstevel@tonic-gate 		case POUND:
21961ca93273Seota 			/*
21971ca93273Seota 			 * Skip comments.
21981ca93273Seota 			 */
21997c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
22007c478bd9Sstevel@tonic-gate 			break;
22017c478bd9Sstevel@tonic-gate 		case NAME:
22027c478bd9Sstevel@tonic-gate 		case STRING:
22037c478bd9Sstevel@tonic-gate 			switch (state) {
22047c478bd9Sstevel@tonic-gate 			case AL_NEW:
22057c478bd9Sstevel@tonic-gate 				(void) strcpy(drvbuf, tokbuf);
22067c478bd9Sstevel@tonic-gate 				state = AL_DRVNAME;
22077c478bd9Sstevel@tonic-gate 				break;
22087c478bd9Sstevel@tonic-gate 			case AL_DRVNAME_COMMA:
22097c478bd9Sstevel@tonic-gate 				(void) strcat(drvbuf, tokbuf);
22107c478bd9Sstevel@tonic-gate 				state = AL_DRVNAME;
22117c478bd9Sstevel@tonic-gate 				break;
22127c478bd9Sstevel@tonic-gate 			case AL_ALIAS_COMMA:
22137c478bd9Sstevel@tonic-gate 				(void) strcat(drvbuf, tokbuf);
22147c478bd9Sstevel@tonic-gate 				state = AL_ALIAS;
22157c478bd9Sstevel@tonic-gate 				break;
22167c478bd9Sstevel@tonic-gate 			case AL_DRVNAME:
22177c478bd9Sstevel@tonic-gate 				major = mod_name_to_major(drvbuf);
2218a204de77Scth 				if (major == DDI_MAJOR_T_NONE) {
22197c478bd9Sstevel@tonic-gate 					kobj_find_eol(file);
22207c478bd9Sstevel@tonic-gate 					state = AL_NEW;
22217c478bd9Sstevel@tonic-gate 				} else {
22227c478bd9Sstevel@tonic-gate 					(void) strcpy(drvbuf, tokbuf);
22237c478bd9Sstevel@tonic-gate 					state = AL_ALIAS;
22247c478bd9Sstevel@tonic-gate 				}
22257c478bd9Sstevel@tonic-gate 				break;
22267c478bd9Sstevel@tonic-gate 			case AL_ALIAS:
22277c478bd9Sstevel@tonic-gate 				if (make_mbind(drvbuf, major, NULL, bhash)
22287c478bd9Sstevel@tonic-gate 				    != 0) {
22297c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, drvbuf);
22307c478bd9Sstevel@tonic-gate 				}
22311ca93273Seota 				/*
22321ca93273Seota 				 * copy this token just in case that there
22331ca93273Seota 				 * are multiple names on the same line.
22341ca93273Seota 				 */
22351ca93273Seota 				(void) strcpy(drvbuf, tokbuf);
22367c478bd9Sstevel@tonic-gate 				break;
22377c478bd9Sstevel@tonic-gate 			}
22387c478bd9Sstevel@tonic-gate 			break;
22397c478bd9Sstevel@tonic-gate 		case COMMA:
22407c478bd9Sstevel@tonic-gate 			(void) strcat(drvbuf, tokbuf);
22417c478bd9Sstevel@tonic-gate 			switch (state) {
22427c478bd9Sstevel@tonic-gate 			case AL_DRVNAME:
22437c478bd9Sstevel@tonic-gate 				state = AL_DRVNAME_COMMA;
22447c478bd9Sstevel@tonic-gate 				break;
22457c478bd9Sstevel@tonic-gate 			case AL_ALIAS:
22467c478bd9Sstevel@tonic-gate 				state = AL_ALIAS_COMMA;
22477c478bd9Sstevel@tonic-gate 				break;
22487c478bd9Sstevel@tonic-gate 			default:
22497c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
22507c478bd9Sstevel@tonic-gate 			}
22517c478bd9Sstevel@tonic-gate 			break;
22527c478bd9Sstevel@tonic-gate 		case EOF:
22537c478bd9Sstevel@tonic-gate 			done = 1;
22547c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
22557c478bd9Sstevel@tonic-gate 		case NEWLINE:
22567c478bd9Sstevel@tonic-gate 			if (state == AL_ALIAS) {
22577c478bd9Sstevel@tonic-gate 				if (make_mbind(drvbuf, major, NULL, bhash)
22587c478bd9Sstevel@tonic-gate 				    != 0) {
22597c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, drvbuf);
22607c478bd9Sstevel@tonic-gate 				}
22617c478bd9Sstevel@tonic-gate 			} else if (state != AL_NEW) {
22627c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
22637c478bd9Sstevel@tonic-gate 				    "Missing alias for %s", drvbuf);
22647c478bd9Sstevel@tonic-gate 			}
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 			kobj_newline(file);
22677c478bd9Sstevel@tonic-gate 			state = AL_NEW;
2268a204de77Scth 			major = DDI_MAJOR_T_NONE;
22697c478bd9Sstevel@tonic-gate 			break;
22707c478bd9Sstevel@tonic-gate 		default:
22717c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
22727c478bd9Sstevel@tonic-gate 		}
22737c478bd9Sstevel@tonic-gate 	}
2274*eb9a1df2SHans Rosenfeld }
22757c478bd9Sstevel@tonic-gate 
2276*eb9a1df2SHans Rosenfeld void
make_aliases(struct bind ** bhash)2277*eb9a1df2SHans Rosenfeld make_aliases(struct bind **bhash)
2278*eb9a1df2SHans Rosenfeld {
2279*eb9a1df2SHans Rosenfeld 	struct _buf *file;
2280*eb9a1df2SHans Rosenfeld 
2281*eb9a1df2SHans Rosenfeld 	if ((file = kobj_open_file(pptfile)) != (struct _buf *)-1) {
2282*eb9a1df2SHans Rosenfeld 		parse_aliases(bhash, file);
2283*eb9a1df2SHans Rosenfeld 		kobj_close_file(file);
2284*eb9a1df2SHans Rosenfeld 	}
2285*eb9a1df2SHans Rosenfeld 
2286*eb9a1df2SHans Rosenfeld 	if ((file = kobj_open_file(dafile)) != (struct _buf *)-1) {
2287*eb9a1df2SHans Rosenfeld 		parse_aliases(bhash, file);
2288*eb9a1df2SHans Rosenfeld 		kobj_close_file(file);
2289*eb9a1df2SHans Rosenfeld 	}
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate /*
22947c478bd9Sstevel@tonic-gate  * It is called for parsing these files:
22957c478bd9Sstevel@tonic-gate  * - /etc/path_to_inst
22967c478bd9Sstevel@tonic-gate  * - /etc/name_to_major
22977c478bd9Sstevel@tonic-gate  * - /etc/name_to_sysnum
22987c478bd9Sstevel@tonic-gate  * A callback "int (*line_parser)(char *, int, char *, struct bind **)"
22997c478bd9Sstevel@tonic-gate  * is invoked for each line of the file.
23007c478bd9Sstevel@tonic-gate  * The callback can inhash the entry into a hashtable by supplying
23017c478bd9Sstevel@tonic-gate  * a pre-allocated hashtable in "struct bind **hashtab".
23027c478bd9Sstevel@tonic-gate  */
23037c478bd9Sstevel@tonic-gate int
read_binding_file(char * bindfile,struct bind ** hashtab,int (* line_parser)(char *,int,char *,struct bind **))23047c478bd9Sstevel@tonic-gate read_binding_file(char *bindfile, struct bind **hashtab,
23057c478bd9Sstevel@tonic-gate     int (*line_parser)(char *, int, char *, struct bind **))
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	enum {
23087c478bd9Sstevel@tonic-gate 		B_NEW, B_NAME, B_VAL, B_BIND_NAME
23097c478bd9Sstevel@tonic-gate 	} state;
23107c478bd9Sstevel@tonic-gate 	struct _buf *file;
23117c478bd9Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
23127c478bd9Sstevel@tonic-gate 	token_t token;
23137c478bd9Sstevel@tonic-gate 	int maxnum = 0;
23147c478bd9Sstevel@tonic-gate 	char *bind_name = NULL, *name = NULL, *bn = NULL;
23157c478bd9Sstevel@tonic-gate 	u_longlong_t val;
23167c478bd9Sstevel@tonic-gate 	int done = 0;
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	static char num_err[] = "Missing number on preceding line?";
23197c478bd9Sstevel@tonic-gate 	static char dupwarn[] = "!The binding file entry \"%s %u\" conflicts "
23207c478bd9Sstevel@tonic-gate 	    "with a previous entry";
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	if (hashtab != NULL) {
23237c478bd9Sstevel@tonic-gate 		clear_binding_hash(hashtab);
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_file(bindfile)) == (struct _buf *)-1)
23277c478bd9Sstevel@tonic-gate 		panic("read_binding_file: %s file not found", bindfile);
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	state = B_NEW;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	while (!done) {
23327c478bd9Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 		switch (token) {
23357c478bd9Sstevel@tonic-gate 		case POUND:
23361ca93273Seota 			/*
23371ca93273Seota 			 * Skip comments.
23381ca93273Seota 			 */
23397c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
23407c478bd9Sstevel@tonic-gate 			break;
23417c478bd9Sstevel@tonic-gate 		case NAME:
23427c478bd9Sstevel@tonic-gate 		case STRING:
23437c478bd9Sstevel@tonic-gate 			switch (state) {
23447c478bd9Sstevel@tonic-gate 			case B_NEW:
23457c478bd9Sstevel@tonic-gate 				/*
23467c478bd9Sstevel@tonic-gate 				 * This case is for the first name and
23477c478bd9Sstevel@tonic-gate 				 * possibly only name in an entry.
23487c478bd9Sstevel@tonic-gate 				 */
23497c478bd9Sstevel@tonic-gate 				ASSERT(name == NULL);
23507c478bd9Sstevel@tonic-gate 				name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
23517c478bd9Sstevel@tonic-gate 				(void) strcpy(name, tokbuf);
23527c478bd9Sstevel@tonic-gate 				state = B_NAME;
23537c478bd9Sstevel@tonic-gate 				break;
23547c478bd9Sstevel@tonic-gate 			case B_VAL:
23557c478bd9Sstevel@tonic-gate 				/*
23567c478bd9Sstevel@tonic-gate 				 * This case is for a second name, which
23577c478bd9Sstevel@tonic-gate 				 * would be the binding name if the first
23587c478bd9Sstevel@tonic-gate 				 * name was actually a generic name.
23597c478bd9Sstevel@tonic-gate 				 */
23607c478bd9Sstevel@tonic-gate 				ASSERT(bind_name == NULL);
23617c478bd9Sstevel@tonic-gate 				bind_name = kmem_alloc(strlen(tokbuf) + 1,
23627c478bd9Sstevel@tonic-gate 				    KM_SLEEP);
23637c478bd9Sstevel@tonic-gate 				(void) strcpy(bind_name, tokbuf);
23647c478bd9Sstevel@tonic-gate 				state = B_BIND_NAME;
23657c478bd9Sstevel@tonic-gate 				break;
23667c478bd9Sstevel@tonic-gate 			default:
23677c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, num_err);
23687c478bd9Sstevel@tonic-gate 			}
23697c478bd9Sstevel@tonic-gate 			break;
23707c478bd9Sstevel@tonic-gate 		case HEXVAL:
23717c478bd9Sstevel@tonic-gate 		case DECVAL:
23727c478bd9Sstevel@tonic-gate 			if (state != B_NAME) {
23737c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Missing name?");
23747c478bd9Sstevel@tonic-gate 				state = B_NEW;
23757c478bd9Sstevel@tonic-gate 				continue;
23767c478bd9Sstevel@tonic-gate 			}
23777c478bd9Sstevel@tonic-gate 			(void) kobj_getvalue(tokbuf, &val);
23787c478bd9Sstevel@tonic-gate 			if (val > (u_longlong_t)INT_MAX) {
23797c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
23807c478bd9Sstevel@tonic-gate 				    "value %llu too large", val);
23817c478bd9Sstevel@tonic-gate 				state = B_NEW;
23827c478bd9Sstevel@tonic-gate 				continue;
23837c478bd9Sstevel@tonic-gate 			}
23847c478bd9Sstevel@tonic-gate 			state = B_VAL;
23857c478bd9Sstevel@tonic-gate 			break;
23867c478bd9Sstevel@tonic-gate 		case EOF:
23877c478bd9Sstevel@tonic-gate 			done = 1;
23887c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
23897c478bd9Sstevel@tonic-gate 		case NEWLINE:
23907c478bd9Sstevel@tonic-gate 			if ((state == B_BIND_NAME) || (state == B_VAL)) {
23917c478bd9Sstevel@tonic-gate 				if (state == B_BIND_NAME)
23927c478bd9Sstevel@tonic-gate 					bn = bind_name;
23937c478bd9Sstevel@tonic-gate 				else
23947c478bd9Sstevel@tonic-gate 					bn = NULL;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 				if (line_parser != NULL) {
23977c478bd9Sstevel@tonic-gate 					if ((*line_parser)(name, (int)val, bn,
23987c478bd9Sstevel@tonic-gate 					    hashtab) == 0)
23997c478bd9Sstevel@tonic-gate 						maxnum = MAX((int)val, maxnum);
24007c478bd9Sstevel@tonic-gate 					else
24017c478bd9Sstevel@tonic-gate 						kobj_file_err(CE_WARN, file,
24027c478bd9Sstevel@tonic-gate 						    dupwarn, name, (uint_t)val);
24037c478bd9Sstevel@tonic-gate 				}
24047c478bd9Sstevel@tonic-gate 			} else if (state != B_NEW)
24057c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, "Syntax error?");
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 			if (name) {
24087c478bd9Sstevel@tonic-gate 				kmem_free(name, strlen(name) + 1);
24097c478bd9Sstevel@tonic-gate 				name = NULL;
24107c478bd9Sstevel@tonic-gate 			}
24117c478bd9Sstevel@tonic-gate 			if (bind_name) {
24127c478bd9Sstevel@tonic-gate 				kmem_free(bind_name, strlen(bind_name) + 1);
24137c478bd9Sstevel@tonic-gate 				bind_name = NULL;
24147c478bd9Sstevel@tonic-gate 			}
24157c478bd9Sstevel@tonic-gate 			state = B_NEW;
24167c478bd9Sstevel@tonic-gate 			kobj_newline(file);
24177c478bd9Sstevel@tonic-gate 			break;
24187c478bd9Sstevel@tonic-gate 		default:
24197c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, "Missing name/number?");
24207c478bd9Sstevel@tonic-gate 			break;
24217c478bd9Sstevel@tonic-gate 		}
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	ASSERT(name == NULL);		/* any leaks? */
24257c478bd9Sstevel@tonic-gate 	ASSERT(bind_name == NULL);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
24287c478bd9Sstevel@tonic-gate 	return (maxnum);
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate /*
24327c478bd9Sstevel@tonic-gate  * read_dacf_binding_file()
24332b987d42SAlexander Eremin  *	Read the /etc/dacf.conf file and build the dacf_rule_t database from it.
24347c478bd9Sstevel@tonic-gate  *
24357c478bd9Sstevel@tonic-gate  * The syntax of a line in the dacf.conf file is:
24362b987d42SAlexander Eremin  *   dev-spec	[module:]op-set	operation options	[config-args];
24377c478bd9Sstevel@tonic-gate  *
24387c478bd9Sstevel@tonic-gate  * Where:
24392b987d42SAlexander Eremin  *	1. dev-spec is of the format: name="data"
24402b987d42SAlexander Eremin  *	2. operation is the operation that this rule matches. (i.e. pre-detach)
24412b987d42SAlexander Eremin  *	3. options is a comma delimited list of options (i.e. debug,foobar)
24422b987d42SAlexander Eremin  *	4. config-data is a whitespace delimited list of the format: name="data"
24437c478bd9Sstevel@tonic-gate  */
24447c478bd9Sstevel@tonic-gate int
read_dacf_binding_file(char * filename)24457c478bd9Sstevel@tonic-gate read_dacf_binding_file(char *filename)
24467c478bd9Sstevel@tonic-gate {
24477c478bd9Sstevel@tonic-gate 	enum {
24487c478bd9Sstevel@tonic-gate 		DACF_BEGIN,
24497c478bd9Sstevel@tonic-gate 		/* minor_nodetype="ddi_mouse:serial" */
24507c478bd9Sstevel@tonic-gate 		DACF_NT_SPEC, DACF_NT_EQUALS, DACF_NT_DATA,
24517c478bd9Sstevel@tonic-gate 		/* consconfig:mouseconfig */
24527c478bd9Sstevel@tonic-gate 		DACF_MN_MODNAME, DACF_MN_COLON, DACF_MN_OPSET,
24537c478bd9Sstevel@tonic-gate 		/* op */
24547c478bd9Sstevel@tonic-gate 		DACF_OP_NAME,
24557c478bd9Sstevel@tonic-gate 		/* [ option1, option2, option3... | - ] */
24567c478bd9Sstevel@tonic-gate 		DACF_OPT_OPTION, DACF_OPT_COMMA, DACF_OPT_END,
24577c478bd9Sstevel@tonic-gate 		/* argname1="argval1" argname2="argval2" ... */
24587c478bd9Sstevel@tonic-gate 		DACF_OPARG_SPEC, DACF_OPARG_EQUALS, DACF_OPARG_DATA,
24597c478bd9Sstevel@tonic-gate 		DACF_ERR, DACF_ERR_NEWLINE, DACF_COMMENT
24607c478bd9Sstevel@tonic-gate 	} state = DACF_BEGIN;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	struct _buf *file;
24637c478bd9Sstevel@tonic-gate 	char *fname;
24647c478bd9Sstevel@tonic-gate 	token_t token;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
24677c478bd9Sstevel@tonic-gate 	char mn_modname_buf[MAXNAMELEN], *mn_modnamep = NULL;
24687c478bd9Sstevel@tonic-gate 	char mn_opset_buf[MAXNAMELEN], *mn_opsetp = NULL;
24697c478bd9Sstevel@tonic-gate 	char nt_data_buf[MAXNAMELEN], *nt_datap = NULL;
24707c478bd9Sstevel@tonic-gate 	char arg_spec_buf[MAXNAMELEN];
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	uint_t opts = 0;
24737c478bd9Sstevel@tonic-gate 	dacf_devspec_t nt_spec_type = DACF_DS_ERROR;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	dacf_arg_t *arg_list = NULL;
24767c478bd9Sstevel@tonic-gate 	dacf_opid_t opid = DACF_OPID_ERROR;
24777c478bd9Sstevel@tonic-gate 	int done = 0;
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	static char w_syntax[] = "'%s' unexpected";
24807c478bd9Sstevel@tonic-gate 	static char w_equals[] = "'=' is illegal in the current context";
24817c478bd9Sstevel@tonic-gate 	static char w_baddevspec[] = "device specification '%s' unrecognized";
24827c478bd9Sstevel@tonic-gate 	static char w_badop[] = "operation '%s' unrecognized";
24837c478bd9Sstevel@tonic-gate 	static char w_badopt[] = "option '%s' unrecognized, ignoring";
24847c478bd9Sstevel@tonic-gate 	static char w_newline[] = "rule is incomplete";
24857c478bd9Sstevel@tonic-gate 	static char w_insert[] = "failed to register rule";
24867c478bd9Sstevel@tonic-gate 	static char w_comment[] = "'#' not allowed except at start of line";
24877c478bd9Sstevel@tonic-gate 	static char w_dupargs[] =
24887c478bd9Sstevel@tonic-gate 	    "argument '%s' duplicates a previous argument, skipping";
24897c478bd9Sstevel@tonic-gate 	static char w_nt_empty[] = "empty device specification not allowed";
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	if (filename == NULL) {
24927c478bd9Sstevel@tonic-gate 		fname = dacffile;	/* default binding file */
24937c478bd9Sstevel@tonic-gate 	} else {
24947c478bd9Sstevel@tonic-gate 		fname = filename;	/* user specified */
24957c478bd9Sstevel@tonic-gate 	}
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_file(fname)) == (struct _buf *)-1) {
24987c478bd9Sstevel@tonic-gate 		return (ENOENT);
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
25027c478bd9Sstevel@tonic-gate 		printf("dacf debug: clearing rules database\n");
25037c478bd9Sstevel@tonic-gate 	}
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
25067c478bd9Sstevel@tonic-gate 	dacf_clear_rules();
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
25097c478bd9Sstevel@tonic-gate 		printf("dacf debug: parsing %s\n", fname);
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	while (!done) {
25137c478bd9Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 		switch (token) {
25167c478bd9Sstevel@tonic-gate 		case POUND:	/* comment line */
25177c478bd9Sstevel@tonic-gate 			if (state != DACF_BEGIN) {
25187c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_comment);
25197c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
25207c478bd9Sstevel@tonic-gate 				break;
25217c478bd9Sstevel@tonic-gate 			}
25227c478bd9Sstevel@tonic-gate 			state = DACF_COMMENT;
25237c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
25247c478bd9Sstevel@tonic-gate 			break;
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 		case EQUALS:
25277c478bd9Sstevel@tonic-gate 			switch (state) {
25287c478bd9Sstevel@tonic-gate 			case DACF_NT_SPEC:
25297c478bd9Sstevel@tonic-gate 				state = DACF_NT_EQUALS;
25307c478bd9Sstevel@tonic-gate 				break;
25317c478bd9Sstevel@tonic-gate 			case DACF_OPARG_SPEC:
25327c478bd9Sstevel@tonic-gate 				state = DACF_OPARG_EQUALS;
25337c478bd9Sstevel@tonic-gate 				break;
25347c478bd9Sstevel@tonic-gate 			default:
25357c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_equals);
25367c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
25377c478bd9Sstevel@tonic-gate 			}
25387c478bd9Sstevel@tonic-gate 			break;
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 		case NAME:
25417c478bd9Sstevel@tonic-gate 			switch (state) {
25427c478bd9Sstevel@tonic-gate 			case DACF_BEGIN:
25437c478bd9Sstevel@tonic-gate 				nt_spec_type = dacf_get_devspec(tokbuf);
25447c478bd9Sstevel@tonic-gate 				if (nt_spec_type == DACF_DS_ERROR) {
25457c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
25467c478bd9Sstevel@tonic-gate 					    w_baddevspec, tokbuf);
25477c478bd9Sstevel@tonic-gate 					state = DACF_ERR;
25487c478bd9Sstevel@tonic-gate 					break;
25497c478bd9Sstevel@tonic-gate 				}
25507c478bd9Sstevel@tonic-gate 				state = DACF_NT_SPEC;
25517c478bd9Sstevel@tonic-gate 				break;
25527c478bd9Sstevel@tonic-gate 			case DACF_NT_DATA:
25537c478bd9Sstevel@tonic-gate 				(void) strncpy(mn_modname_buf, tokbuf,
25547c478bd9Sstevel@tonic-gate 				    sizeof (mn_modname_buf));
25557c478bd9Sstevel@tonic-gate 				mn_modnamep = mn_modname_buf;
25567c478bd9Sstevel@tonic-gate 				state = DACF_MN_MODNAME;
25577c478bd9Sstevel@tonic-gate 				break;
25587c478bd9Sstevel@tonic-gate 			case DACF_MN_MODNAME:
25597c478bd9Sstevel@tonic-gate 				/*
25607c478bd9Sstevel@tonic-gate 				 * This handles the 'optional' modname.
25617c478bd9Sstevel@tonic-gate 				 * What we thought was the modname is really
25627c478bd9Sstevel@tonic-gate 				 * the op-set.  So it is copied over.
25637c478bd9Sstevel@tonic-gate 				 */
25647c478bd9Sstevel@tonic-gate 				ASSERT(mn_modnamep);
25657c478bd9Sstevel@tonic-gate 				(void) strncpy(mn_opset_buf, mn_modnamep,
25667c478bd9Sstevel@tonic-gate 				    sizeof (mn_opset_buf));
25677c478bd9Sstevel@tonic-gate 				mn_opsetp = mn_opset_buf;
25687c478bd9Sstevel@tonic-gate 				mn_modnamep = NULL;
25697c478bd9Sstevel@tonic-gate 				/*
25707c478bd9Sstevel@tonic-gate 				 * Now, the token we just read is the opset,
25717c478bd9Sstevel@tonic-gate 				 * so look that up and fill in opid
25727c478bd9Sstevel@tonic-gate 				 */
25737c478bd9Sstevel@tonic-gate 				if ((opid = dacf_get_op(tokbuf)) ==
25747c478bd9Sstevel@tonic-gate 				    DACF_OPID_ERROR) {
25757c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badop,
25767c478bd9Sstevel@tonic-gate 					    tokbuf);
25777c478bd9Sstevel@tonic-gate 					state = DACF_ERR;
25787c478bd9Sstevel@tonic-gate 					break;
25797c478bd9Sstevel@tonic-gate 				}
25807c478bd9Sstevel@tonic-gate 				state = DACF_OP_NAME;
25817c478bd9Sstevel@tonic-gate 				break;
25827c478bd9Sstevel@tonic-gate 			case DACF_MN_COLON:
25837c478bd9Sstevel@tonic-gate 				(void) strncpy(mn_opset_buf, tokbuf,
25847c478bd9Sstevel@tonic-gate 				    sizeof (mn_opset_buf));
25857c478bd9Sstevel@tonic-gate 				mn_opsetp = mn_opset_buf;
25867c478bd9Sstevel@tonic-gate 				state = DACF_MN_OPSET;
25877c478bd9Sstevel@tonic-gate 				break;
25887c478bd9Sstevel@tonic-gate 			case DACF_MN_OPSET:
25897c478bd9Sstevel@tonic-gate 				if ((opid = dacf_get_op(tokbuf)) ==
25907c478bd9Sstevel@tonic-gate 				    DACF_OPID_ERROR) {
25917c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badop,
25927c478bd9Sstevel@tonic-gate 					    tokbuf);
25937c478bd9Sstevel@tonic-gate 					state = DACF_ERR;
25947c478bd9Sstevel@tonic-gate 					break;
25957c478bd9Sstevel@tonic-gate 				}
25967c478bd9Sstevel@tonic-gate 				state = DACF_OP_NAME;
25977c478bd9Sstevel@tonic-gate 				break;
25987c478bd9Sstevel@tonic-gate 			case DACF_OP_NAME:
25997c478bd9Sstevel@tonic-gate 				/*
26007c478bd9Sstevel@tonic-gate 				 * This case is just like DACF_OPT_COMMA below,
26017c478bd9Sstevel@tonic-gate 				 * but we check for the sole '-' argument
26027c478bd9Sstevel@tonic-gate 				 */
26037c478bd9Sstevel@tonic-gate 				if (strcmp(tokbuf, "-") == 0) {
26047c478bd9Sstevel@tonic-gate 					state = DACF_OPT_END;
26057c478bd9Sstevel@tonic-gate 					break;
26067c478bd9Sstevel@tonic-gate 				}
26077c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
26087c478bd9Sstevel@tonic-gate 			case DACF_OPT_COMMA:
26097c478bd9Sstevel@tonic-gate 				/*
26107c478bd9Sstevel@tonic-gate 				 * figure out what option was given, but don't
26117c478bd9Sstevel@tonic-gate 				 * make a federal case if invalid, just skip it
26127c478bd9Sstevel@tonic-gate 				 */
26137c478bd9Sstevel@tonic-gate 				if (dacf_getopt(tokbuf, &opts) != 0) {
26147c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_badopt,
26157c478bd9Sstevel@tonic-gate 					    tokbuf);
26167c478bd9Sstevel@tonic-gate 				}
26177c478bd9Sstevel@tonic-gate 				state = DACF_OPT_OPTION;
26187c478bd9Sstevel@tonic-gate 				break;
26197c478bd9Sstevel@tonic-gate 			case DACF_OPT_END:
26207c478bd9Sstevel@tonic-gate 			case DACF_OPT_OPTION:
26217c478bd9Sstevel@tonic-gate 			case DACF_OPARG_DATA:
26227c478bd9Sstevel@tonic-gate 				(void) strncpy(arg_spec_buf, tokbuf,
26237c478bd9Sstevel@tonic-gate 				    sizeof (arg_spec_buf));
26247c478bd9Sstevel@tonic-gate 				state = DACF_OPARG_SPEC;
26257c478bd9Sstevel@tonic-gate 				break;
26267c478bd9Sstevel@tonic-gate 			case DACF_OPARG_EQUALS:
26277c478bd9Sstevel@tonic-gate 				/*
26287c478bd9Sstevel@tonic-gate 				 * Add the arg.  Warn if it's a duplicate
26297c478bd9Sstevel@tonic-gate 				 */
26307c478bd9Sstevel@tonic-gate 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
26317c478bd9Sstevel@tonic-gate 				    tokbuf) != 0) {
26327c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_dupargs,
26337c478bd9Sstevel@tonic-gate 					    arg_spec_buf);
26347c478bd9Sstevel@tonic-gate 				}
26357c478bd9Sstevel@tonic-gate 				state = DACF_OPARG_DATA;
26367c478bd9Sstevel@tonic-gate 				break;
26377c478bd9Sstevel@tonic-gate 			default:
26387c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
26397c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
26407c478bd9Sstevel@tonic-gate 				break;
26417c478bd9Sstevel@tonic-gate 			}
26427c478bd9Sstevel@tonic-gate 			break;
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 		case STRING:
26457c478bd9Sstevel@tonic-gate 			/*
26467c478bd9Sstevel@tonic-gate 			 * We need to check to see if the string has a \n in it.
26477c478bd9Sstevel@tonic-gate 			 * If so, we had an unmatched " mark error, and lex has
26487c478bd9Sstevel@tonic-gate 			 * already emitted an error for us, so we need to enter
26497c478bd9Sstevel@tonic-gate 			 * the error state.  Stupid lex.
26507c478bd9Sstevel@tonic-gate 			 */
26517c478bd9Sstevel@tonic-gate 			if (strchr(tokbuf, '\n')) {
26527c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
26537c478bd9Sstevel@tonic-gate 				break;
26547c478bd9Sstevel@tonic-gate 			}
26557c478bd9Sstevel@tonic-gate 			switch (state) {
26567c478bd9Sstevel@tonic-gate 			case DACF_NT_EQUALS:
26577c478bd9Sstevel@tonic-gate 				if (strlen(tokbuf) == 0) {
26587c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
26597c478bd9Sstevel@tonic-gate 					    w_nt_empty);
26607c478bd9Sstevel@tonic-gate 					state = DACF_ERR;
26617c478bd9Sstevel@tonic-gate 					break;
26627c478bd9Sstevel@tonic-gate 				}
26637c478bd9Sstevel@tonic-gate 				state = DACF_NT_DATA;
26647c478bd9Sstevel@tonic-gate 				nt_datap = nt_data_buf;
26657c478bd9Sstevel@tonic-gate 				(void) strncpy(nt_datap, tokbuf,
26667c478bd9Sstevel@tonic-gate 				    sizeof (nt_data_buf));
26677c478bd9Sstevel@tonic-gate 				break;
26687c478bd9Sstevel@tonic-gate 			case DACF_OPARG_EQUALS:
26697c478bd9Sstevel@tonic-gate 				/*
26707c478bd9Sstevel@tonic-gate 				 * Add the arg.  Warn if it's a duplicate
26717c478bd9Sstevel@tonic-gate 				 */
26727c478bd9Sstevel@tonic-gate 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
26737c478bd9Sstevel@tonic-gate 				    tokbuf) != 0) {
26747c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file, w_dupargs,
26757c478bd9Sstevel@tonic-gate 					    arg_spec_buf);
26767c478bd9Sstevel@tonic-gate 				}
26777c478bd9Sstevel@tonic-gate 				state = DACF_OPARG_DATA;
26787c478bd9Sstevel@tonic-gate 				break;
26797c478bd9Sstevel@tonic-gate 			default:
26807c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
26817c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
26827c478bd9Sstevel@tonic-gate 				break;
26837c478bd9Sstevel@tonic-gate 			}
26847c478bd9Sstevel@tonic-gate 			break;
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 		case COMMA:
26877c478bd9Sstevel@tonic-gate 			switch (state) {
26887c478bd9Sstevel@tonic-gate 			case DACF_OPT_OPTION:
26897c478bd9Sstevel@tonic-gate 				state = DACF_OPT_COMMA;
26907c478bd9Sstevel@tonic-gate 				break;
26917c478bd9Sstevel@tonic-gate 			default:
26927c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, ",");
26937c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
26947c478bd9Sstevel@tonic-gate 				break;
26957c478bd9Sstevel@tonic-gate 			}
26967c478bd9Sstevel@tonic-gate 			break;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 		case COLON:
26997c478bd9Sstevel@tonic-gate 			if (state == DACF_MN_MODNAME)
27007c478bd9Sstevel@tonic-gate 				state = DACF_MN_COLON;
27017c478bd9Sstevel@tonic-gate 			else {
27027c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_syntax, ":");
27037c478bd9Sstevel@tonic-gate 				state = DACF_ERR;
27047c478bd9Sstevel@tonic-gate 			}
27057c478bd9Sstevel@tonic-gate 			break;
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 		case EOF:
27087c478bd9Sstevel@tonic-gate 			done = 1;
27097c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
27107c478bd9Sstevel@tonic-gate 		case NEWLINE:
27117c478bd9Sstevel@tonic-gate 			if (state == DACF_COMMENT || state == DACF_BEGIN) {
27127c478bd9Sstevel@tonic-gate 				state = DACF_BEGIN;
27137c478bd9Sstevel@tonic-gate 				kobj_newline(file);
27147c478bd9Sstevel@tonic-gate 				break;
27157c478bd9Sstevel@tonic-gate 			}
27167c478bd9Sstevel@tonic-gate 			if ((state != DACF_OPT_OPTION) &&
27177c478bd9Sstevel@tonic-gate 			    (state != DACF_OPARG_DATA) &&
27187c478bd9Sstevel@tonic-gate 			    (state != DACF_OPT_END)) {
27197c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_newline);
27207c478bd9Sstevel@tonic-gate 				/*
27217c478bd9Sstevel@tonic-gate 				 * We can't just do DACF_ERR here, since we'll
27227c478bd9Sstevel@tonic-gate 				 * wind up eating the _next_ newline if so.
27237c478bd9Sstevel@tonic-gate 				 */
27247c478bd9Sstevel@tonic-gate 				state = DACF_ERR_NEWLINE;
27257c478bd9Sstevel@tonic-gate 				kobj_newline(file);
27267c478bd9Sstevel@tonic-gate 				break;
27277c478bd9Sstevel@tonic-gate 			}
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 			/*
27307c478bd9Sstevel@tonic-gate 			 * insert the rule.
27317c478bd9Sstevel@tonic-gate 			 */
27327c478bd9Sstevel@tonic-gate 			if (dacf_rule_insert(nt_spec_type, nt_datap,
27337c478bd9Sstevel@tonic-gate 			    mn_modnamep, mn_opsetp, opid, opts, arg_list) < 0) {
27347c478bd9Sstevel@tonic-gate 				/*
27357c478bd9Sstevel@tonic-gate 				 * We can't just do DACF_ERR here, since we'll
27367c478bd9Sstevel@tonic-gate 				 * wind up eating the _next_ newline if so.
27377c478bd9Sstevel@tonic-gate 				 */
27387c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, w_insert);
27397c478bd9Sstevel@tonic-gate 				state = DACF_ERR_NEWLINE;
27407c478bd9Sstevel@tonic-gate 				kobj_newline(file);
27417c478bd9Sstevel@tonic-gate 				break;
27427c478bd9Sstevel@tonic-gate 			}
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 			state = DACF_BEGIN;
27457c478bd9Sstevel@tonic-gate 			kobj_newline(file);
27467c478bd9Sstevel@tonic-gate 			break;
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 		default:
27497c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
27507c478bd9Sstevel@tonic-gate 			break;
27517c478bd9Sstevel@tonic-gate 		} /* switch */
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 		/*
27547c478bd9Sstevel@tonic-gate 		 * Clean up after ourselves, either after a line has terminated
27557c478bd9Sstevel@tonic-gate 		 * successfully or because of a syntax error; or when we reach
27567c478bd9Sstevel@tonic-gate 		 * EOF (remember, we may reach EOF without being 'done' with
27577c478bd9Sstevel@tonic-gate 		 * handling a particular line).
27587c478bd9Sstevel@tonic-gate 		 */
27597c478bd9Sstevel@tonic-gate 		if (state == DACF_ERR) {
27607c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
27617c478bd9Sstevel@tonic-gate 		}
27627c478bd9Sstevel@tonic-gate 		if ((state == DACF_BEGIN) || (state == DACF_ERR) ||
27637c478bd9Sstevel@tonic-gate 		    (state == DACF_ERR_NEWLINE) || done) {
27647c478bd9Sstevel@tonic-gate 			nt_datap = NULL;
27657c478bd9Sstevel@tonic-gate 			mn_modnamep = mn_opsetp = NULL;
27667c478bd9Sstevel@tonic-gate 			opts = 0;
27677c478bd9Sstevel@tonic-gate 			opid = DACF_OPID_ERROR;
27687c478bd9Sstevel@tonic-gate 			nt_spec_type = DACF_DS_ERROR;
27697c478bd9Sstevel@tonic-gate 			dacf_arglist_delete(&arg_list);
27707c478bd9Sstevel@tonic-gate 			state = DACF_BEGIN;
27717c478bd9Sstevel@tonic-gate 		}
27727c478bd9Sstevel@tonic-gate 	} /* while */
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
27757c478bd9Sstevel@tonic-gate 		printf("\ndacf debug: done!\n");
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
27817c478bd9Sstevel@tonic-gate 	return (0);
27827c478bd9Sstevel@tonic-gate }
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate void
lock_hw_class_list()27857c478bd9Sstevel@tonic-gate lock_hw_class_list()
27867c478bd9Sstevel@tonic-gate {
27877c478bd9Sstevel@tonic-gate 	mutex_enter(&hcl_lock);
27887c478bd9Sstevel@tonic-gate }
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate void
unlock_hw_class_list()27917c478bd9Sstevel@tonic-gate unlock_hw_class_list()
27927c478bd9Sstevel@tonic-gate {
27937c478bd9Sstevel@tonic-gate 	mutex_exit(&hcl_lock);
27947c478bd9Sstevel@tonic-gate }
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate void
add_class(char * exporter,char * class)27977c478bd9Sstevel@tonic-gate add_class(char *exporter, char *class)
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	struct hwc_class *hcl;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/*
28027c478bd9Sstevel@tonic-gate 	 * If exporter's major is not registered in /etc/name_to_major,
28037c478bd9Sstevel@tonic-gate 	 * don't update hwc_class, but just return here.
28047c478bd9Sstevel@tonic-gate 	 */
28057c478bd9Sstevel@tonic-gate 	if (ddi_name_to_major(exporter) >= devcnt) {
28067c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "No major number for driver %s"
2807b1b8ab34Slling 		    " in class %s", exporter, class);
28087c478bd9Sstevel@tonic-gate 		return;
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 	hcl = kmem_zalloc(sizeof (struct hwc_class), KM_SLEEP);
28117c478bd9Sstevel@tonic-gate 	hcl->class_exporter = kmem_alloc(strlen(exporter) + 1, KM_SLEEP);
28127c478bd9Sstevel@tonic-gate 	hcl->class_name = kmem_alloc(strlen(class) + 1, KM_SLEEP);
28137c478bd9Sstevel@tonic-gate 	(void) strcpy(hcl->class_exporter, exporter);
28147c478bd9Sstevel@tonic-gate 	(void) strcpy(hcl->class_name, class);
28157c478bd9Sstevel@tonic-gate 	lock_hw_class_list();
28167c478bd9Sstevel@tonic-gate 	hcl->class_next = hcl_head;
28177c478bd9Sstevel@tonic-gate 	hcl_head = hcl;
28187c478bd9Sstevel@tonic-gate 	unlock_hw_class_list();
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate /*
28227c478bd9Sstevel@tonic-gate  * Return the number of classes exported. If buf is not NULL, fill in
28237c478bd9Sstevel@tonic-gate  * the array of the class names as well.
28247c478bd9Sstevel@tonic-gate  *
28257c478bd9Sstevel@tonic-gate  * Caller must hold hcl_lock to ensure the class list unmodified while
28267c478bd9Sstevel@tonic-gate  * it is accessed. A typical caller will get a count first and then
28277c478bd9Sstevel@tonic-gate  * allocate buf. The lock should be held by the caller.
28287c478bd9Sstevel@tonic-gate  */
28297c478bd9Sstevel@tonic-gate int
get_class(const char * exporter,char ** buf)28307c478bd9Sstevel@tonic-gate get_class(const char *exporter, char **buf)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	int n = 0;
28337c478bd9Sstevel@tonic-gate 	struct hwc_class *hcl;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&hcl_lock));
28367c478bd9Sstevel@tonic-gate 	for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
28377c478bd9Sstevel@tonic-gate 		if (strcmp(exporter, hcl->class_exporter) == 0) {
28387c478bd9Sstevel@tonic-gate 			if (buf)
28397c478bd9Sstevel@tonic-gate 				buf[n] = hcl->class_name;
28407c478bd9Sstevel@tonic-gate 			++n;
28417c478bd9Sstevel@tonic-gate 		}
28427c478bd9Sstevel@tonic-gate 	}
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	return (n);
28457c478bd9Sstevel@tonic-gate }
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate void
read_class_file(void)28487c478bd9Sstevel@tonic-gate read_class_file(void)
28497c478bd9Sstevel@tonic-gate {
28507c478bd9Sstevel@tonic-gate 	struct _buf *file;
28517c478bd9Sstevel@tonic-gate 	struct hwc_class *hcl, *hcl1;
28527c478bd9Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
28537c478bd9Sstevel@tonic-gate 	enum {
28547c478bd9Sstevel@tonic-gate 		C_BEGIN, C_EXPORTER, C_END
28557c478bd9Sstevel@tonic-gate 	} state;
28567c478bd9Sstevel@tonic-gate 	token_t token;
28577c478bd9Sstevel@tonic-gate 	int done = 0;
28587c478bd9Sstevel@tonic-gate 	char *exporter = NULL, *class = NULL, *name = NULL;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	if (hcl_head != NULL) {
28617c478bd9Sstevel@tonic-gate 		hcl = hcl_head;
28627c478bd9Sstevel@tonic-gate 		while (hcl != NULL) {
28637c478bd9Sstevel@tonic-gate 			kmem_free(hcl->class_exporter,
28647c478bd9Sstevel@tonic-gate 			    strlen(hcl->class_exporter) + 1);
28657c478bd9Sstevel@tonic-gate 			hcl1 = hcl;
28667c478bd9Sstevel@tonic-gate 			hcl = hcl->class_next;
28677c478bd9Sstevel@tonic-gate 			kmem_free(hcl1, sizeof (struct hwc_class));
28687c478bd9Sstevel@tonic-gate 		}
28697c478bd9Sstevel@tonic-gate 		hcl_head = NULL;
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_file(class_file)) == (struct _buf *)-1)
28737c478bd9Sstevel@tonic-gate 		return;
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 	state = C_BEGIN;
28767c478bd9Sstevel@tonic-gate 	while (!done) {
28777c478bd9Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 		switch (token) {
28807c478bd9Sstevel@tonic-gate 		case POUND:
28811ca93273Seota 			/*
28821ca93273Seota 			 * Skip comments.
28831ca93273Seota 			 */
28847c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
28857c478bd9Sstevel@tonic-gate 			break;
28867c478bd9Sstevel@tonic-gate 		case NAME:
28877c478bd9Sstevel@tonic-gate 		case STRING:
28887c478bd9Sstevel@tonic-gate 			name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
28897c478bd9Sstevel@tonic-gate 			(void) strcpy(name, tokbuf);
28907c478bd9Sstevel@tonic-gate 			switch (state) {
28917c478bd9Sstevel@tonic-gate 			case C_BEGIN:
28927c478bd9Sstevel@tonic-gate 				exporter = name;
28937c478bd9Sstevel@tonic-gate 				state = C_EXPORTER;
28947c478bd9Sstevel@tonic-gate 				break;
28957c478bd9Sstevel@tonic-gate 			case C_EXPORTER:
28967c478bd9Sstevel@tonic-gate 				class = name;
28977c478bd9Sstevel@tonic-gate 				add_class(exporter, class);
28987c478bd9Sstevel@tonic-gate 				state = C_END;
28997c478bd9Sstevel@tonic-gate 				break;
29007c478bd9Sstevel@tonic-gate 			case C_END:
29017c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
29027c478bd9Sstevel@tonic-gate 				    "Extra noise after entry");
29037c478bd9Sstevel@tonic-gate 				kmem_free(name, strlen(name) + 1);
29047c478bd9Sstevel@tonic-gate 				kobj_find_eol(file);
29057c478bd9Sstevel@tonic-gate 				break;
29067c478bd9Sstevel@tonic-gate 			} /* End Switch */
29077c478bd9Sstevel@tonic-gate 			break;
29087c478bd9Sstevel@tonic-gate 		case EOF:
29097c478bd9Sstevel@tonic-gate 			done = 1;
29107c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
29117c478bd9Sstevel@tonic-gate 		case NEWLINE:
29127c478bd9Sstevel@tonic-gate 			kobj_newline(file);
29137c478bd9Sstevel@tonic-gate 			if (state == C_EXPORTER)
29147c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
29157c478bd9Sstevel@tonic-gate 				    "Partial entry ignored");
29167c478bd9Sstevel@tonic-gate 			state = C_BEGIN;
29177c478bd9Sstevel@tonic-gate 			if (exporter)
29187c478bd9Sstevel@tonic-gate 				kmem_free(exporter, strlen(exporter) + 1);
29197c478bd9Sstevel@tonic-gate 			if (class)
29207c478bd9Sstevel@tonic-gate 				kmem_free(class, strlen(class) + 1);
29217c478bd9Sstevel@tonic-gate 			exporter = NULL;
29227c478bd9Sstevel@tonic-gate 			class = NULL;
29237c478bd9Sstevel@tonic-gate 			break;
29247c478bd9Sstevel@tonic-gate 		default:
29257c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
29267c478bd9Sstevel@tonic-gate 			break;
29277c478bd9Sstevel@tonic-gate 		}
29287c478bd9Sstevel@tonic-gate 	}
29297c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
29307c478bd9Sstevel@tonic-gate }
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * Given par_list, get a list of parent major number
29347c478bd9Sstevel@tonic-gate  */
29357c478bd9Sstevel@tonic-gate int
impl_parlist_to_major(struct par_list * pl,char parents[])29367c478bd9Sstevel@tonic-gate impl_parlist_to_major(struct par_list *pl, char parents[])
29377c478bd9Sstevel@tonic-gate {
29387c478bd9Sstevel@tonic-gate 	struct hwc_spec *hwcp;
29397c478bd9Sstevel@tonic-gate 	struct hwc_class *hcl;
29407c478bd9Sstevel@tonic-gate 	major_t major;
29417c478bd9Sstevel@tonic-gate 	int nmajor = 0;
29427c478bd9Sstevel@tonic-gate 	extern int devcnt;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	for (; pl != NULL; pl = pl->par_next) {
29457c478bd9Sstevel@tonic-gate 		if ((pl->par_major < devcnt) && (parents[pl->par_major] == 0)) {
29467c478bd9Sstevel@tonic-gate 			parents[pl->par_major] = 1;
29477c478bd9Sstevel@tonic-gate 			nmajor++;
29487c478bd9Sstevel@tonic-gate 			continue;
29497c478bd9Sstevel@tonic-gate 		}
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 		/* parent specs cannot be mapped to a driver */
2952a204de77Scth 		if (pl->par_major != DDI_MAJOR_T_NONE)
29537c478bd9Sstevel@tonic-gate 			continue;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 		/* class spec */
29567c478bd9Sstevel@tonic-gate 		hwcp = pl->par_specs;
29577c478bd9Sstevel@tonic-gate 		ASSERT(hwcp->hwc_class_name);
29587c478bd9Sstevel@tonic-gate 		ASSERT(hwcp->hwc_parent_name == NULL);
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 		for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
29617c478bd9Sstevel@tonic-gate 			if (strcmp(hwcp->hwc_class_name, hcl->class_name) != 0)
29627c478bd9Sstevel@tonic-gate 				continue;
29637c478bd9Sstevel@tonic-gate 			major = ddi_name_to_major(hcl->class_exporter);
2964a204de77Scth 			ASSERT(major != DDI_MAJOR_T_NONE);
29657c478bd9Sstevel@tonic-gate 			if (parents[major] == 0) {
29667c478bd9Sstevel@tonic-gate 				parents[major] = 1;
29677c478bd9Sstevel@tonic-gate 				nmajor++;
29687c478bd9Sstevel@tonic-gate 			}
29697c478bd9Sstevel@tonic-gate 		}
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 	return (nmajor);
29727c478bd9Sstevel@tonic-gate }
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate /*
29757c478bd9Sstevel@tonic-gate  * delete a parent list and all its hwc specs
29767c478bd9Sstevel@tonic-gate  */
29777c478bd9Sstevel@tonic-gate void
impl_delete_par_list(struct par_list * pl)29787c478bd9Sstevel@tonic-gate impl_delete_par_list(struct par_list *pl)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	struct par_list *saved_pl;
29817c478bd9Sstevel@tonic-gate 	struct hwc_spec *hp, *hp1;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	while (pl) {
29847c478bd9Sstevel@tonic-gate 		hp = pl->par_specs;
29857c478bd9Sstevel@tonic-gate 		while (hp) {
29867c478bd9Sstevel@tonic-gate 			hp1 = hp;
29877c478bd9Sstevel@tonic-gate 			hp = hp->hwc_next;
29887c478bd9Sstevel@tonic-gate 			hwc_free(hp1);
29897c478bd9Sstevel@tonic-gate 		}
29907c478bd9Sstevel@tonic-gate 		saved_pl = pl;
29917c478bd9Sstevel@tonic-gate 		pl = pl->par_next;
29927c478bd9Sstevel@tonic-gate 		kmem_free(saved_pl, sizeof (*saved_pl));
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate }
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate #if defined(_PSM_MODULES)
29977c478bd9Sstevel@tonic-gate void
open_mach_list(void)29987c478bd9Sstevel@tonic-gate open_mach_list(void)
29997c478bd9Sstevel@tonic-gate {
30007c478bd9Sstevel@tonic-gate 	struct _buf *file;
30017c478bd9Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
30027c478bd9Sstevel@tonic-gate 	token_t token;
30037c478bd9Sstevel@tonic-gate 	struct psm_mach *machp;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_file(mach_file)) == (struct _buf *)-1)
30067c478bd9Sstevel@tonic-gate 		return;
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	while ((token = kobj_lex(file, tokbuf, sizeof (tokbuf))) != EOF) {
30097c478bd9Sstevel@tonic-gate 		switch (token) {
30107c478bd9Sstevel@tonic-gate 		case POUND:
30111ca93273Seota 			/*
30121ca93273Seota 			 * Skip comments.
30131ca93273Seota 			 */
30147c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
30157c478bd9Sstevel@tonic-gate 			break;
30167c478bd9Sstevel@tonic-gate 		case NAME:
30177c478bd9Sstevel@tonic-gate 		case STRING:
30187c478bd9Sstevel@tonic-gate 			machp = kmem_alloc((sizeof (struct psm_mach) +
30197c478bd9Sstevel@tonic-gate 			    strlen(tokbuf) + 1), KM_SLEEP);
30207c478bd9Sstevel@tonic-gate 			machp->m_next = pmach_head;
30217c478bd9Sstevel@tonic-gate 			machp->m_machname = (char *)(machp + 1);
30227c478bd9Sstevel@tonic-gate 			(void) strcpy(machp->m_machname, tokbuf);
30237c478bd9Sstevel@tonic-gate 			pmach_head = machp;
30247c478bd9Sstevel@tonic-gate 			break;
30257c478bd9Sstevel@tonic-gate 		case NEWLINE:
30267c478bd9Sstevel@tonic-gate 			kobj_newline(file);
30277c478bd9Sstevel@tonic-gate 			break;
30287c478bd9Sstevel@tonic-gate 		default:
30297c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
30307c478bd9Sstevel@tonic-gate 			break;
30317c478bd9Sstevel@tonic-gate 		}
30327c478bd9Sstevel@tonic-gate 	}
30337c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
30347c478bd9Sstevel@tonic-gate }
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate void *
get_next_mach(void * handle,char * buf)30377c478bd9Sstevel@tonic-gate get_next_mach(void *handle, char *buf)
30387c478bd9Sstevel@tonic-gate {
30397c478bd9Sstevel@tonic-gate 	struct psm_mach *machp;
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	machp = (struct psm_mach *)handle;
30427c478bd9Sstevel@tonic-gate 	if (machp)
30437c478bd9Sstevel@tonic-gate 		machp = machp->m_next;
30447c478bd9Sstevel@tonic-gate 	else
30457c478bd9Sstevel@tonic-gate 		machp = pmach_head;
30467c478bd9Sstevel@tonic-gate 	if (machp)
30477c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, machp->m_machname);
30487c478bd9Sstevel@tonic-gate 	return (machp);
30497c478bd9Sstevel@tonic-gate }
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate void
close_mach_list(void)30527c478bd9Sstevel@tonic-gate close_mach_list(void)
30537c478bd9Sstevel@tonic-gate {
30547c478bd9Sstevel@tonic-gate 	struct psm_mach *machp;
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	while (pmach_head) {
30577c478bd9Sstevel@tonic-gate 		machp = pmach_head;
30587c478bd9Sstevel@tonic-gate 		pmach_head = machp->m_next;
30597c478bd9Sstevel@tonic-gate 		kmem_free(machp, sizeof (struct psm_mach) +
3060b1b8ab34Slling 		    strlen(machp->m_machname) + 1);
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate }
30637c478bd9Sstevel@tonic-gate #endif	/* _PSM_MODULES */
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate #if defined(_RTC_CONFIG)
30667c478bd9Sstevel@tonic-gate /*
30677c478bd9Sstevel@tonic-gate  * Read in the 'zone_lag' value from the rtc configuration file,
30687c478bd9Sstevel@tonic-gate  * and return the value to the caller.  Note that there is other information
30697c478bd9Sstevel@tonic-gate  * in this file (zone_info), so we ignore unknown values.  We do spit out
30707c478bd9Sstevel@tonic-gate  * warnings if the line doesn't begin with an identifier, or if we don't find
30717c478bd9Sstevel@tonic-gate  * exactly "zone_lag=value".  No one should be editing this file by hand
30727c478bd9Sstevel@tonic-gate  * (use the rtc command instead), but it's better to be careful.
30737c478bd9Sstevel@tonic-gate  */
30747c478bd9Sstevel@tonic-gate long
process_rtc_config_file(void)30757c478bd9Sstevel@tonic-gate process_rtc_config_file(void)
30767c478bd9Sstevel@tonic-gate {
30777c478bd9Sstevel@tonic-gate 	enum {
30787c478bd9Sstevel@tonic-gate 		R_NEW, R_NAME, R_EQUALS, R_VALUE
30797c478bd9Sstevel@tonic-gate 	} state;
30807c478bd9Sstevel@tonic-gate 	struct _buf *file;
30817c478bd9Sstevel@tonic-gate 	char tokbuf[MAXNAMELEN];
30827c478bd9Sstevel@tonic-gate 	token_t token;
30837c478bd9Sstevel@tonic-gate 	long zone_lag = 0;
30847c478bd9Sstevel@tonic-gate 	u_longlong_t tmp;
30857c478bd9Sstevel@tonic-gate 	int done = 0;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	if ((file = kobj_open_file(rtc_config_file)) == (struct _buf *)-1)
30887c478bd9Sstevel@tonic-gate 		return (0);
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	state = R_NEW;
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	while (!done) {
30937c478bd9Sstevel@tonic-gate 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 		switch (token) {
30967c478bd9Sstevel@tonic-gate 		case POUND:
30971ca93273Seota 			/*
30981ca93273Seota 			 * Skip comments.
30991ca93273Seota 			 */
31007c478bd9Sstevel@tonic-gate 			kobj_find_eol(file);
31017c478bd9Sstevel@tonic-gate 			break;
31027c478bd9Sstevel@tonic-gate 		case NAME:
31037c478bd9Sstevel@tonic-gate 		case STRING:
31047c478bd9Sstevel@tonic-gate 			if (state == R_NEW) {
31057c478bd9Sstevel@tonic-gate 				if (strcmp(tokbuf, "zone_lag") == 0)
31067c478bd9Sstevel@tonic-gate 					state = R_NAME;
31077c478bd9Sstevel@tonic-gate 				else
31087c478bd9Sstevel@tonic-gate 					kobj_find_eol(file);   /* Ignore */
31097c478bd9Sstevel@tonic-gate 			} else
31107c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
31117c478bd9Sstevel@tonic-gate 			break;
31127c478bd9Sstevel@tonic-gate 		case EQUALS:
31137c478bd9Sstevel@tonic-gate 			if (state == R_NAME)
31147c478bd9Sstevel@tonic-gate 				state = R_EQUALS;
31157c478bd9Sstevel@tonic-gate 			else
31167c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
31177c478bd9Sstevel@tonic-gate 			break;
31187c478bd9Sstevel@tonic-gate 		case DECVAL:
31197c478bd9Sstevel@tonic-gate 			if (state == R_EQUALS) {
31207c478bd9Sstevel@tonic-gate 				if (kobj_getvalue(tokbuf, &tmp) != 0)
31217c478bd9Sstevel@tonic-gate 					kobj_file_err(CE_WARN, file,
31227c478bd9Sstevel@tonic-gate 					    "Bad value %s for zone_lag",
31237c478bd9Sstevel@tonic-gate 					    tokbuf);
31247c478bd9Sstevel@tonic-gate 				else
31257c478bd9Sstevel@tonic-gate 					zone_lag = (long)tmp;
31267c478bd9Sstevel@tonic-gate 				state = R_VALUE;
31277c478bd9Sstevel@tonic-gate 			} else
31287c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
31297c478bd9Sstevel@tonic-gate 			break;
31307c478bd9Sstevel@tonic-gate 		case EOF:
31317c478bd9Sstevel@tonic-gate 			done = 1;
31327c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
31337c478bd9Sstevel@tonic-gate 		case NEWLINE:
31347c478bd9Sstevel@tonic-gate 			if (state != R_NEW && state != R_VALUE)
31357c478bd9Sstevel@tonic-gate 				kobj_file_err(CE_WARN, file,
31367c478bd9Sstevel@tonic-gate 				    "Partial zone_lag entry ignored");
31377c478bd9Sstevel@tonic-gate 			kobj_newline(file);
31387c478bd9Sstevel@tonic-gate 			state = R_NEW;
31397c478bd9Sstevel@tonic-gate 			break;
31407c478bd9Sstevel@tonic-gate 		default:
31417c478bd9Sstevel@tonic-gate 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
31427c478bd9Sstevel@tonic-gate 			break;
31437c478bd9Sstevel@tonic-gate 		}
31447c478bd9Sstevel@tonic-gate 	}
31457c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
31467c478bd9Sstevel@tonic-gate 	return (zone_lag);
31477c478bd9Sstevel@tonic-gate }
31487c478bd9Sstevel@tonic-gate #endif /* _RTC_CONFIG */
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate /*
31527c478bd9Sstevel@tonic-gate  * Append node spec to the end of par_list
31537c478bd9Sstevel@tonic-gate  */
31547c478bd9Sstevel@tonic-gate static void
append(struct hwc_spec * spec,struct par_list * par)31557c478bd9Sstevel@tonic-gate append(struct hwc_spec *spec, struct par_list *par)
31567c478bd9Sstevel@tonic-gate {
3157c6f039c7SToomas Soome 	struct hwc_spec *hwc, *last = NULL;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	ASSERT(par->par_specs);
31607c478bd9Sstevel@tonic-gate 	for (hwc = par->par_specs; hwc; hwc = hwc->hwc_next)
31617c478bd9Sstevel@tonic-gate 		last = hwc;
3162c6f039c7SToomas Soome 
3163c6f039c7SToomas Soome 	if (last != NULL)
3164c6f039c7SToomas Soome 		last->hwc_next = spec;
31657c478bd9Sstevel@tonic-gate }
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate /*
31687c478bd9Sstevel@tonic-gate  * Given a parent=/full-pathname, see if the platform
31697c478bd9Sstevel@tonic-gate  * can resolve the pathname to driver, otherwise, try
31707c478bd9Sstevel@tonic-gate  * the leaf node name.
31717c478bd9Sstevel@tonic-gate  */
31727c478bd9Sstevel@tonic-gate static major_t
get_major(char * parent)31737c478bd9Sstevel@tonic-gate get_major(char *parent)
31747c478bd9Sstevel@tonic-gate {
3175a204de77Scth 	major_t major = DDI_MAJOR_T_NONE;
31767c478bd9Sstevel@tonic-gate 	char *tmp, *driver = NULL;
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	if (*parent == '/')
31797c478bd9Sstevel@tonic-gate 		major = path_to_major(parent);
31807c478bd9Sstevel@tonic-gate 
3181a204de77Scth 	if (major != DDI_MAJOR_T_NONE)
31827c478bd9Sstevel@tonic-gate 		return (major);
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	/* extract the name between '/' and '@' */
31857c478bd9Sstevel@tonic-gate 	if (*parent == '/')
31867c478bd9Sstevel@tonic-gate 		driver = strrchr(parent, '/') + 1;
31877c478bd9Sstevel@tonic-gate 	else
31887c478bd9Sstevel@tonic-gate 		driver = parent;
31897c478bd9Sstevel@tonic-gate 	if ((tmp = strchr(driver, '@')) != NULL)
31907c478bd9Sstevel@tonic-gate 		*tmp = '\0';
31917c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(driver);
31927c478bd9Sstevel@tonic-gate 	if (tmp)
31937c478bd9Sstevel@tonic-gate 		*tmp = '@';
31947c478bd9Sstevel@tonic-gate 	return (major);
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate /*
31987c478bd9Sstevel@tonic-gate  * Chain together specs whose parent's module name is the same.
31997c478bd9Sstevel@tonic-gate  */
32007c478bd9Sstevel@tonic-gate static void
add_spec(struct hwc_spec * spec,struct par_list ** par)32017c478bd9Sstevel@tonic-gate add_spec(struct hwc_spec *spec, struct par_list **par)
32027c478bd9Sstevel@tonic-gate {
32037c478bd9Sstevel@tonic-gate 	major_t maj;
32047c478bd9Sstevel@tonic-gate 	struct par_list *pl, *par_last = NULL;
32057c478bd9Sstevel@tonic-gate 	char *parent = spec->hwc_parent_name;
32065c44817cSjw 	char *class = spec->hwc_class_name;
32077c478bd9Sstevel@tonic-gate 
32085c44817cSjw 	ASSERT(parent || class);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	/*
32117c478bd9Sstevel@tonic-gate 	 * If given a parent=/full-pathname, see if the platform
32127c478bd9Sstevel@tonic-gate 	 * can resolve the pathname to driver, otherwise, try
32137c478bd9Sstevel@tonic-gate 	 * the leaf node name.
32147c478bd9Sstevel@tonic-gate 	 *
32157c478bd9Sstevel@tonic-gate 	 * If parent=/full-pathname doesn't resolve to a driver,
32167c478bd9Sstevel@tonic-gate 	 * this could be cause by DR removal of the device.
32177c478bd9Sstevel@tonic-gate 	 * We put it on the major=-2 list in case the device
32187c478bd9Sstevel@tonic-gate 	 * is brought back into the system by DR.
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 	if (parent) {
32217c478bd9Sstevel@tonic-gate 		maj = get_major(parent);
3222a204de77Scth 		if (maj == DDI_MAJOR_T_NONE) {
32237c478bd9Sstevel@tonic-gate 			if ((*parent == '/') &&
32247c478bd9Sstevel@tonic-gate 			    (strncmp(parent, "/pseudo", 7) != 0)) {
32257c478bd9Sstevel@tonic-gate 				maj = (major_t)-2;
32267c478bd9Sstevel@tonic-gate 			} else {
32277c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
32287c478bd9Sstevel@tonic-gate 				    "add_spec: No major number for %s",
32297c478bd9Sstevel@tonic-gate 				    parent);
32307c478bd9Sstevel@tonic-gate 				hwc_free(spec);
32317c478bd9Sstevel@tonic-gate 				return;
32327c478bd9Sstevel@tonic-gate 			}
32337c478bd9Sstevel@tonic-gate 		}
32347c478bd9Sstevel@tonic-gate 	} else
3235a204de77Scth 		maj = DDI_MAJOR_T_NONE;
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	/*
32385c44817cSjw 	 * Scan the list looking for a matching parent. When parent is
32395c44817cSjw 	 * not NULL, we match the parent by major. If parent is NULL but
32405c44817cSjw 	 * class is not NULL, we mache the pl by class name.
32417c478bd9Sstevel@tonic-gate 	 */
32427c478bd9Sstevel@tonic-gate 	for (pl = *par; pl; pl = pl->par_next) {
32435c44817cSjw 		if ((parent && (maj == pl->par_major)) || ((parent == NULL) &&
32445c44817cSjw 		    class && pl->par_specs->hwc_class_name && (strncmp(class,
32455c44817cSjw 		    pl->par_specs->hwc_class_name, strlen(class)) == 0))) {
32467c478bd9Sstevel@tonic-gate 			append(spec, pl);
32477c478bd9Sstevel@tonic-gate 			return;
32487c478bd9Sstevel@tonic-gate 		}
32497c478bd9Sstevel@tonic-gate 		par_last = pl;
32507c478bd9Sstevel@tonic-gate 	}
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	/*
32537c478bd9Sstevel@tonic-gate 	 * Didn't find a match on the list.  Make a new parent list.
32547c478bd9Sstevel@tonic-gate 	 */
32557c478bd9Sstevel@tonic-gate 	pl = kmem_zalloc(sizeof (*pl), KM_SLEEP);
32567c478bd9Sstevel@tonic-gate 	pl->par_major = maj;
32577c478bd9Sstevel@tonic-gate 	pl->par_specs = spec;
32587c478bd9Sstevel@tonic-gate 	if (*par == NULL) {	/* null par list */
32597c478bd9Sstevel@tonic-gate 		*par = pl;
32607c478bd9Sstevel@tonic-gate 		return;
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 	/* put "class=" entries last (lower pri if dups) */
3263a204de77Scth 	if (maj == DDI_MAJOR_T_NONE) {
32647c478bd9Sstevel@tonic-gate 		par_last->par_next = pl;
32657c478bd9Sstevel@tonic-gate 		return;
32667c478bd9Sstevel@tonic-gate 	}
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	/* ensure unresolved "parent=/full-path" goes first */
32697c478bd9Sstevel@tonic-gate 	if ((maj != (major_t)-2) && ((*par)->par_major == (major_t)-2))
32707c478bd9Sstevel@tonic-gate 		par = &(*par)->par_next;
32717c478bd9Sstevel@tonic-gate 	pl->par_next = *par;
32727c478bd9Sstevel@tonic-gate 	*par = pl;
32737c478bd9Sstevel@tonic-gate }
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate /*
32767c478bd9Sstevel@tonic-gate  * Add property spec to property list in original order
32777c478bd9Sstevel@tonic-gate  */
32787c478bd9Sstevel@tonic-gate static void
add_props(struct hwc_spec * spec,ddi_prop_t ** props)32797c478bd9Sstevel@tonic-gate add_props(struct hwc_spec *spec, ddi_prop_t **props)
32807c478bd9Sstevel@tonic-gate {
32817c478bd9Sstevel@tonic-gate 	ASSERT(spec->hwc_devi_name == NULL);
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	if (spec->hwc_devi_sys_prop_ptr) {
32847c478bd9Sstevel@tonic-gate 		while (*props)
32857c478bd9Sstevel@tonic-gate 			props = &(*props)->prop_next;
32867c478bd9Sstevel@tonic-gate 		*props = spec->hwc_devi_sys_prop_ptr;
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 		/* remove these properties from the spec */
32897c478bd9Sstevel@tonic-gate 		spec->hwc_devi_sys_prop_ptr = NULL;
32907c478bd9Sstevel@tonic-gate 	}
32917c478bd9Sstevel@tonic-gate 	hwc_free(spec);
32927c478bd9Sstevel@tonic-gate }
3293