xref: /titanic_41/usr/src/uts/common/os/modsysfile.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/inttypes.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/user.h>
33 #include <sys/disp.h>
34 #include <sys/conf.h>
35 #include <sys/bootconf.h>
36 #include <sys/sysconf.h>
37 #include <sys/sunddi.h>
38 #include <sys/esunddi.h>
39 #include <sys/ddi_impldefs.h>
40 #include <sys/kmem.h>
41 #include <sys/vmem.h>
42 #include <sys/fs/ufs_fsdir.h>
43 #include <sys/hwconf.h>
44 #include <sys/modctl.h>
45 #include <sys/cmn_err.h>
46 #include <sys/kobj.h>
47 #include <sys/kobj_lex.h>
48 #include <sys/errno.h>
49 #include <sys/debug.h>
50 #include <sys/autoconf.h>
51 #include <sys/callb.h>
52 #include <sys/sysmacros.h>
53 #include <sys/dacf.h>
54 #include <vm/seg_kmem.h>
55 
56 struct hwc_class *hcl_head;	/* head of list of classes */
57 static kmutex_t hcl_lock;	/* for accessing list of classes */
58 
59 #define	DAFILE		"/etc/driver_aliases"
60 #define	CLASSFILE	"/etc/driver_classes"
61 #define	DACFFILE	"/etc/dacf.conf"
62 
63 static char class_file[] = CLASSFILE;
64 static char dafile[] = DAFILE;
65 static char dacffile[] = DACFFILE;
66 
67 char *systemfile = "etc/system";	/* name of ascii system file */
68 
69 static struct sysparam *sysparam_hd;	/* head of parameters list */
70 static struct sysparam *sysparam_tl;	/* tail of parameters list */
71 static vmem_t *mod_sysfile_arena;	/* parser memory */
72 
73 char obp_bootpath[BO_MAXOBJNAME];	/* bootpath from obp */
74 char svm_bootpath[BO_MAXOBJNAME];	/* bootpath redirected via rootdev */
75 char zfs_bootpath[BO_MAXOBJNAME];	/* zfs bootpath, set via zfsroot */
76 
77 #if defined(_PSM_MODULES)
78 
79 struct psm_mach {
80 	struct psm_mach *m_next;
81 	char		*m_machname;
82 };
83 
84 static struct psm_mach *pmach_head;	/* head of list of classes */
85 
86 #define	MACHFILE	"/etc/mach"
87 static char mach_file[] = MACHFILE;
88 
89 #endif	/* _PSM_MODULES */
90 
91 #if defined(_RTC_CONFIG)
92 static char rtc_config_file[] = "/etc/rtc_config";
93 #endif
94 
95 static void sys_set_var(int, struct sysparam *, void *);
96 
97 static void setparams(void);
98 
99 /*
100  * driver.conf parse thread control structure
101  */
102 struct hwc_parse_mt {
103 	ksema_t		sema;
104 	char		*name;		/* name of .conf files */
105 	struct par_list	**pl;		/* parsed parent list */
106 	ddi_prop_t	**props;	/* parsed properties */
107 	int		rv;		/* return value */
108 };
109 
110 static int hwc_parse_now(char *, struct par_list **, ddi_prop_t **);
111 static void hwc_parse_thread(struct hwc_parse_mt *);
112 static struct hwc_parse_mt *hwc_parse_mtalloc(char *, struct par_list **,
113 	ddi_prop_t **);
114 static void hwc_parse_mtfree(struct hwc_parse_mt *);
115 static void add_spec(struct hwc_spec *, struct par_list **);
116 static void add_props(struct hwc_spec *, ddi_prop_t **);
117 
118 static void check_system_file(void);
119 static int sysparam_compare_entry(struct sysparam *, struct sysparam *);
120 static char *sysparam_type_to_str(int);
121 static void sysparam_count_entry(struct sysparam *, int *, u_longlong_t *);
122 static void sysparam_print_warning(struct sysparam *, u_longlong_t);
123 
124 #ifdef DEBUG
125 static int parse_debug_on = 0;
126 
127 /*VARARGS1*/
128 static void
129 parse_debug(struct _buf *file, char *fmt, ...)
130 {
131 	va_list adx;
132 
133 	if (parse_debug_on) {
134 		va_start(adx, fmt);
135 		vprintf(fmt, adx);
136 		if (file)
137 			printf(" on line %d of %s\n", kobj_linenum(file),
138 				kobj_filename(file));
139 		va_end(adx);
140 	}
141 }
142 #endif /* DEBUG */
143 
144 #define	FE_BUFLEN 256
145 
146 /*PRINTFLIKE3*/
147 void
148 kobj_file_err(int type,  struct _buf *file, char *fmt, ...)
149 {
150 	va_list ap;
151 	/*
152 	 * If we're in trouble, we might be short on stack... be paranoid
153 	 */
154 	char *buf = kmem_alloc(FE_BUFLEN, KM_SLEEP);
155 	char *trailer = kmem_alloc(FE_BUFLEN, KM_SLEEP);
156 	char *fmt_str = kmem_alloc(FE_BUFLEN, KM_SLEEP);
157 	char prefix = '\0';
158 
159 	va_start(ap, fmt);
160 	if (strchr("^!?", fmt[0]) != NULL) {
161 		prefix = fmt[0];
162 		fmt++;
163 	}
164 	(void) vsnprintf(buf, FE_BUFLEN, fmt, ap);
165 	va_end(ap);
166 	(void) snprintf(trailer, FE_BUFLEN, " on line %d of %s",
167 	    kobj_linenum(file), kobj_filename(file));
168 
169 	/*
170 	 * If prefixed with !^?, prepend that character
171 	 */
172 	if (prefix != '\0') {
173 		(void) snprintf(fmt_str, FE_BUFLEN, "%c%%s%%s", prefix);
174 	} else {
175 		(void) strncpy(fmt_str, "%s%s", FE_BUFLEN);
176 	}
177 
178 	cmn_err(type, fmt_str, buf, trailer);
179 	kmem_free(buf, FE_BUFLEN);
180 	kmem_free(trailer, FE_BUFLEN);
181 	kmem_free(fmt_str, FE_BUFLEN);
182 }
183 
184 #ifdef DEBUG
185 char *tokennames[] = {
186 	"UNEXPECTED",
187 	"EQUALS",
188 	"AMPERSAND",
189 	"BIT_OR",
190 	"STAR",
191 	"POUND",
192 	"COLON",
193 	"SEMICOLON",
194 	"COMMA",
195 	"SLASH",
196 	"WHITE_SPACE",
197 	"NEWLINE",
198 	"EOF",
199 	"STRING",
200 	"HEXVAL",
201 	"DECVAL",
202 	"NAME"
203 };
204 #endif /* DEBUG */
205 
206 token_t
207 kobj_lex(struct _buf *file, char *val, size_t size)
208 {
209 	char	*cp;
210 	int	ch, oval, badquote;
211 	size_t	remain;
212 	token_t token = UNEXPECTED;
213 
214 	if (size < 2)
215 		return (token);	/* this token is UNEXPECTED */
216 
217 	cp = val;
218 	while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
219 		;
220 
221 	remain = size - 1;
222 	*cp++ = (char)ch;
223 	switch (ch) {
224 	case '=':
225 		token = EQUALS;
226 		break;
227 	case '&':
228 		token = AMPERSAND;
229 		break;
230 	case '|':
231 		token = BIT_OR;
232 		break;
233 	case '*':
234 		token = STAR;
235 		break;
236 	case '#':
237 		token = POUND;
238 		break;
239 	case ':':
240 		token = COLON;
241 		break;
242 	case ';':
243 		token = SEMICOLON;
244 		break;
245 	case ',':
246 		token = COMMA;
247 		break;
248 	case '/':
249 		token = SLASH;
250 		break;
251 	case ' ':
252 	case '\t':
253 	case '\f':
254 		while ((ch = kobj_getc(file)) == ' ' ||
255 		    ch == '\t' || ch == '\f') {
256 			if (--remain == 0) {
257 				token = UNEXPECTED;
258 				goto out;
259 			}
260 			*cp++ = (char)ch;
261 		}
262 		(void) kobj_ungetc(file);
263 		token = WHITE_SPACE;
264 		break;
265 	case '\n':
266 	case '\r':
267 		token = NEWLINE;
268 		break;
269 	case '"':
270 		remain++;
271 		cp--;
272 		badquote = 0;
273 		while (!badquote && (ch  = kobj_getc(file)) != '"') {
274 			switch (ch) {
275 			case '\n':
276 			case -1:
277 				kobj_file_err(CE_WARN, file, "Missing \"");
278 				remain = size - 1;
279 				cp = val;
280 				*cp++ = '\n';
281 				badquote = 1;
282 				/* since we consumed the newline/EOF */
283 				(void) kobj_ungetc(file);
284 				break;
285 
286 			case '\\':
287 				if (--remain == 0) {
288 					token = UNEXPECTED;
289 					goto out;
290 				}
291 				ch = (char)kobj_getc(file);
292 				if (!isdigit(ch)) {
293 					/* escape the character */
294 					*cp++ = (char)ch;
295 					break;
296 				}
297 				oval = 0;
298 				while (ch >= '0' && ch <= '7') {
299 					ch -= '0';
300 					oval = (oval << 3) + ch;
301 					ch = (char)kobj_getc(file);
302 				}
303 				(void) kobj_ungetc(file);
304 				/* check for character overflow? */
305 				if (oval > 127) {
306 					cmn_err(CE_WARN,
307 					    "Character "
308 					    "overflow detected.");
309 				}
310 				*cp++ = (char)oval;
311 				break;
312 			default:
313 				if (--remain == 0) {
314 					token = UNEXPECTED;
315 					goto out;
316 				}
317 				*cp++ = (char)ch;
318 				break;
319 			}
320 		}
321 		token = STRING;
322 		break;
323 
324 	case -1:
325 		token = EOF;
326 		break;
327 
328 	default:
329 		/*
330 		 * detect a lone '-' (including at the end of a line), and
331 		 * identify it as a 'name'
332 		 */
333 		if (ch == '-') {
334 			if (--remain == 0) {
335 				token = UNEXPECTED;
336 				goto out;
337 			}
338 			*cp++ = (char)(ch = kobj_getc(file));
339 			if (iswhite(ch) || (ch == '\n')) {
340 				(void) kobj_ungetc(file);
341 				remain++;
342 				cp--;
343 				token = NAME;
344 				break;
345 			}
346 		} else if (isunary(ch)) {
347 			if (--remain == 0) {
348 				token = UNEXPECTED;
349 				goto out;
350 			}
351 			*cp++ = (char)(ch = kobj_getc(file));
352 		}
353 
354 
355 		if (isdigit(ch)) {
356 			if (ch == '0') {
357 				if ((ch = kobj_getc(file)) == 'x') {
358 					if (--remain == 0) {
359 						token = UNEXPECTED;
360 						goto out;
361 					}
362 					*cp++ = (char)ch;
363 					ch = kobj_getc(file);
364 					while (isxdigit(ch)) {
365 						if (--remain == 0) {
366 							token = UNEXPECTED;
367 							goto out;
368 						}
369 						*cp++ = (char)ch;
370 						ch = kobj_getc(file);
371 					}
372 					(void) kobj_ungetc(file);
373 					token = HEXVAL;
374 				} else {
375 					goto digit;
376 				}
377 			} else {
378 				ch = kobj_getc(file);
379 digit:
380 				while (isdigit(ch)) {
381 					if (--remain == 0) {
382 						token = UNEXPECTED;
383 						goto out;
384 					}
385 					*cp++ = (char)ch;
386 					ch = kobj_getc(file);
387 				}
388 				(void) kobj_ungetc(file);
389 				token = DECVAL;
390 			}
391 		} else if (isalpha(ch) || ch == '\\' || ch == '_') {
392 			if (ch != '\\') {
393 				ch = kobj_getc(file);
394 			} else {
395 				/*
396 				 * if the character was a backslash,
397 				 * back up so we can overwrite it with
398 				 * the next (i.e. escaped) character.
399 				 */
400 				remain++;
401 				cp--;
402 			}
403 			while (isnamechar(ch) || ch == '\\') {
404 				if (ch == '\\')
405 					ch = kobj_getc(file);
406 				if (--remain == 0) {
407 					token = UNEXPECTED;
408 					goto out;
409 				}
410 				*cp++ = (char)ch;
411 				ch = kobj_getc(file);
412 			}
413 			(void) kobj_ungetc(file);
414 			token = NAME;
415 		} else {
416 			token = UNEXPECTED;
417 		}
418 		break;
419 	}
420 out:
421 	*cp = '\0';
422 
423 #ifdef DEBUG
424 	/*
425 	 * The UNEXPECTED token is the first element of the tokennames array,
426 	 * but its token value is -1.  Adjust the value by adding one to it
427 	 * to change it to an index of the array.
428 	 */
429 	parse_debug(NULL, "kobj_lex: token %s value '%s'\n",
430 	    tokennames[token+1], val);
431 #endif
432 	return (token);
433 }
434 
435 /*
436  * Leave NEWLINE as the next character.
437  */
438 
439 void
440 kobj_find_eol(struct _buf *file)
441 {
442 	int ch;
443 
444 	while ((ch = kobj_getc(file)) != -1) {
445 		if (isnewline(ch)) {
446 			(void) kobj_ungetc(file);
447 			break;
448 		}
449 	}
450 }
451 
452 /*
453  * The ascii system file is read and processed.
454  *
455  * The syntax of commands is as follows:
456  *
457  * '*' in column 1 is a comment line.
458  * <command> : <value>
459  *
460  * command is EXCLUDE, INCLUDE, FORCELOAD, ROOTDEV, ROOTFS,
461  *	SWAPDEV, SWAPFS, MODDIR, SET
462  *
463  * value is an ascii string meaningful for the command.
464  */
465 
466 /*
467  * Table of commands
468  */
469 static struct modcmd modcmd[] = {
470 	{ "EXCLUDE",	MOD_EXCLUDE	},
471 	{ "exclude",	MOD_EXCLUDE	},
472 	{ "INCLUDE",	MOD_INCLUDE	},
473 	{ "include",	MOD_INCLUDE	},
474 	{ "FORCELOAD",	MOD_FORCELOAD	},
475 	{ "forceload",	MOD_FORCELOAD	},
476 	{ "ROOTDEV",	MOD_ROOTDEV	},
477 	{ "rootdev",	MOD_ROOTDEV	},
478 	{ "ROOTFS",	MOD_ROOTFS	},
479 	{ "rootfs",	MOD_ROOTFS	},
480 	{ "SWAPDEV",	MOD_SWAPDEV	},
481 	{ "swapdev",	MOD_SWAPDEV	},
482 	{ "SWAPFS",	MOD_SWAPFS	},
483 	{ "swapfs",	MOD_SWAPFS	},
484 	{ "MODDIR",	MOD_MODDIR	},
485 	{ "moddir",	MOD_MODDIR	},
486 	{ "SET",	MOD_SET		},
487 	{ "set",	MOD_SET		},
488 	{ "SET32",	MOD_SET32	},
489 	{ "set32",	MOD_SET32	},
490 	{ "SET64",	MOD_SET64	},
491 	{ "set64",	MOD_SET64	},
492 	{ "ZFSROOT", 	MOD_ZFSROOT	},
493 	{ "zfsroot", 	MOD_ZFSROOT	},
494 	{ NULL,		MOD_UNKNOWN	}
495 };
496 
497 
498 static char bad_op[] = "illegal operator '%s' used on a string";
499 static char colon_err[] = "A colon (:) must follow the '%s' command";
500 static char tok_err[] = "Unexpected token '%s'";
501 static char extra_err[] = "extraneous input ignored starting at '%s'";
502 static char oversize_err[] = "value too long";
503 
504 static struct sysparam *
505 do_sysfile_cmd(struct _buf *file, const char *cmd)
506 {
507 	struct sysparam *sysp;
508 	struct modcmd *mcp;
509 	token_t token, op;
510 	char *cp;
511 	int ch;
512 	char tok1[MOD_MAXPATH + 1]; /* used to read the path set by 'moddir' */
513 	char tok2[64];
514 
515 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
516 		if (strcmp(mcp->mc_cmdname, cmd) == 0)
517 			break;
518 	}
519 	sysp = vmem_alloc(mod_sysfile_arena, sizeof (struct sysparam),
520 	    VM_SLEEP);
521 	bzero(sysp, sizeof (struct sysparam));
522 	sysp->sys_op = SETOP_NONE; /* set op to noop initially */
523 
524 	switch (sysp->sys_type = mcp->mc_type) {
525 	case MOD_INCLUDE:
526 	case MOD_EXCLUDE:
527 	case MOD_FORCELOAD:
528 		/*
529 		 * Are followed by colon.
530 		 */
531 	case MOD_ROOTFS:
532 	case MOD_SWAPFS:
533 	case MOD_ZFSROOT:
534 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) == COLON) {
535 			token = kobj_lex(file, tok1, sizeof (tok1));
536 		} else {
537 			kobj_file_err(CE_WARN, file, colon_err, cmd);
538 		}
539 		if (token != NAME) {
540 			kobj_file_err(CE_WARN, file, "value expected");
541 			goto bad;
542 		}
543 
544 		cp = tok1 + strlen(tok1);
545 		while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
546 		    !isnewline(ch)) {
547 			if (cp - tok1 >= sizeof (tok1) - 1) {
548 				kobj_file_err(CE_WARN, file, oversize_err);
549 				goto bad;
550 			}
551 			*cp++ = (char)ch;
552 		}
553 		*cp = '\0';
554 
555 		if (ch != -1)
556 			(void) kobj_ungetc(file);
557 		if (sysp->sys_type == MOD_INCLUDE)
558 			return (NULL);
559 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
560 		    VM_SLEEP);
561 		(void) strcpy(sysp->sys_ptr, tok1);
562 		break;
563 	case MOD_SET:
564 	case MOD_SET64:
565 	case MOD_SET32:
566 	{
567 		char *var;
568 		token_t tok3;
569 
570 		if (kobj_lex(file, tok1, sizeof (tok1)) != NAME) {
571 			kobj_file_err(CE_WARN, file, "value expected");
572 			goto bad;
573 		}
574 
575 		/*
576 		 * If the next token is a colon (:),
577 		 * we have the <modname>:<variable> construct.
578 		 */
579 		if ((token = kobj_lex(file, tok2, sizeof (tok2))) == COLON) {
580 			if ((token = kobj_lex(file, tok2,
581 			    sizeof (tok2))) == NAME) {
582 				var = tok2;
583 				/*
584 				 * Save the module name.
585 				 */
586 				sysp->sys_modnam = vmem_alloc(mod_sysfile_arena,
587 				    strlen(tok1) + 1, VM_SLEEP);
588 				(void) strcpy(sysp->sys_modnam, tok1);
589 				op = kobj_lex(file, tok1, sizeof (tok1));
590 			} else {
591 				kobj_file_err(CE_WARN, file, "value expected");
592 				goto bad;
593 			}
594 		} else {
595 			/* otherwise, it was the op */
596 			var = tok1;
597 			op = token;
598 		}
599 		/*
600 		 * kernel param - place variable name in sys_ptr.
601 		 */
602 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(var) + 1,
603 		    VM_SLEEP);
604 		(void) strcpy(sysp->sys_ptr, var);
605 		/* set operation */
606 		switch (op) {
607 		case EQUALS:
608 			/* simple assignment */
609 			sysp->sys_op = SETOP_ASSIGN;
610 			break;
611 		case AMPERSAND:
612 			/* bitwise AND */
613 			sysp->sys_op = SETOP_AND;
614 			break;
615 		case BIT_OR:
616 			/* bitwise OR */
617 			sysp->sys_op = SETOP_OR;
618 			break;
619 		default:
620 			/* unsupported operation */
621 			kobj_file_err(CE_WARN, file,
622 			    "unsupported operator %s", tok2);
623 			goto bad;
624 		}
625 
626 		switch ((tok3 = kobj_lex(file, tok1, sizeof (tok1)))) {
627 		case STRING:
628 			/* string variable */
629 			if (sysp->sys_op != SETOP_ASSIGN) {
630 				kobj_file_err(CE_WARN, file, bad_op, tok1);
631 				goto bad;
632 			}
633 			if (kobj_get_string(&sysp->sys_info, tok1) == 0) {
634 				kobj_file_err(CE_WARN, file, "string garbled");
635 				goto bad;
636 			}
637 			/*
638 			 * Set SYSPARAM_STR_TOKEN in sys_flags to notify
639 			 * sysparam_print_warning() that this is a string
640 			 * token.
641 			 */
642 			sysp->sys_flags |= SYSPARAM_STR_TOKEN;
643 			break;
644 		case HEXVAL:
645 		case DECVAL:
646 			if (kobj_getvalue(tok1, &sysp->sys_info) == -1) {
647 				kobj_file_err(CE_WARN, file,
648 				    "invalid number '%s'", tok1);
649 				goto bad;
650 			}
651 
652 			/*
653 			 * Set the appropriate flag (hexadecimal or decimal)
654 			 * in sys_flags for sysparam_print_warning() to be
655 			 * able to print the number with the correct format.
656 			 */
657 			if (tok3 == HEXVAL) {
658 				sysp->sys_flags |= SYSPARAM_HEX_TOKEN;
659 			} else {
660 				sysp->sys_flags |= SYSPARAM_DEC_TOKEN;
661 			}
662 			break;
663 		default:
664 			kobj_file_err(CE_WARN, file, "bad rvalue '%s'", tok1);
665 			goto bad;
666 		} /* end switch */
667 
668 		/*
669 		 * Now that we've parsed it to check the syntax, consider
670 		 * discarding it (because it -doesn't- apply to this flavor
671 		 * of the kernel)
672 		 */
673 #ifdef _LP64
674 		if (sysp->sys_type == MOD_SET32)
675 			return (NULL);
676 #else
677 		if (sysp->sys_type == MOD_SET64)
678 			return (NULL);
679 #endif
680 		sysp->sys_type = MOD_SET;
681 		break;
682 	}
683 	case MOD_MODDIR:
684 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
685 			kobj_file_err(CE_WARN, file, colon_err, cmd);
686 			goto bad;
687 		}
688 
689 		cp = tok1;
690 		while ((token = kobj_lex(file, cp,
691 		    sizeof (tok1) - (cp - tok1))) != NEWLINE && token != EOF) {
692 			if (token == -1) {
693 				kobj_file_err(CE_WARN, file, oversize_err);
694 				goto bad;
695 			}
696 			cp += strlen(cp);
697 			while ((ch = kobj_getc(file)) != -1 && !iswhite(ch) &&
698 			    !isnewline(ch) && ch != ':') {
699 				if (cp - tok1 >= sizeof (tok1) - 1) {
700 					kobj_file_err(CE_WARN, file,
701 					    oversize_err);
702 					goto bad;
703 				}
704 				*cp++ = (char)ch;
705 			}
706 			*cp = ':';
707 			if (isnewline(ch))
708 				(void) kobj_ungetc(file);
709 		}
710 		(void) kobj_ungetc(file);
711 		*cp  = '\0';
712 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
713 		    VM_SLEEP);
714 		(void) strcpy(sysp->sys_ptr, tok1);
715 		break;
716 
717 	case MOD_SWAPDEV:
718 	case MOD_ROOTDEV:
719 		if ((token = kobj_lex(file, tok1, sizeof (tok1))) != COLON) {
720 			kobj_file_err(CE_WARN, file, colon_err, cmd);
721 			goto bad;
722 		}
723 		while ((ch = kobj_getc(file)) == ' ' || ch == '\t')
724 			;
725 		cp = tok1;
726 		while (!iswhite(ch) && !isnewline(ch) && ch != -1) {
727 			if (cp - tok1 >= sizeof (tok1) - 1) {
728 				kobj_file_err(CE_WARN, file, oversize_err);
729 				goto bad;
730 			}
731 
732 			*cp++ = (char)ch;
733 			ch = kobj_getc(file);
734 		}
735 		if (ch != -1)
736 			(void) kobj_ungetc(file);
737 		*cp = '\0';
738 
739 		sysp->sys_ptr = vmem_alloc(mod_sysfile_arena, strlen(tok1) + 1,
740 		    VM_SLEEP);
741 		(void) strcpy(sysp->sys_ptr, tok1);
742 		break;
743 
744 	case MOD_UNKNOWN:
745 	default:
746 		kobj_file_err(CE_WARN, file, "unknown command '%s'", cmd);
747 		goto bad;
748 	}
749 
750 	return (sysp);
751 
752 bad:
753 	kobj_find_eol(file);
754 	return (NULL);
755 }
756 
757 void
758 mod_read_system_file(int ask)
759 {
760 	register struct sysparam *sp;
761 	register struct _buf *file;
762 	register token_t token, last_tok;
763 	char tokval[MAXLINESIZE];
764 
765 	mod_sysfile_arena = vmem_create("mod_sysfile", NULL, 0, 8,
766 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
767 
768 	if (ask)
769 		mod_askparams();
770 
771 	if (systemfile != NULL) {
772 
773 		if ((file = kobj_open_file(systemfile)) ==
774 		    (struct _buf *)-1) {
775 			cmn_err(CE_WARN, "cannot open system file: %s",
776 			    systemfile);
777 		} else {
778 			sysparam_tl = (struct sysparam *)&sysparam_hd;
779 
780 			last_tok = NEWLINE;
781 			while ((token = kobj_lex(file, tokval,
782 			    sizeof (tokval))) != EOF) {
783 				switch (token) {
784 				case STAR:
785 				case POUND:
786 					/*
787 					 * Skip comments.
788 					 */
789 					kobj_find_eol(file);
790 					break;
791 				case NEWLINE:
792 					kobj_newline(file);
793 					last_tok = NEWLINE;
794 					break;
795 				case NAME:
796 					if (last_tok != NEWLINE) {
797 						kobj_file_err(CE_WARN, file,
798 						    extra_err, tokval);
799 						kobj_find_eol(file);
800 					} else if ((sp = do_sysfile_cmd(file,
801 					    tokval)) != NULL) {
802 						sp->sys_next = NULL;
803 						sysparam_tl->sys_next = sp;
804 						sysparam_tl = sp;
805 					}
806 					last_tok = NAME;
807 					break;
808 				default:
809 					kobj_file_err(CE_WARN,
810 					    file, tok_err, tokval);
811 					kobj_find_eol(file);
812 					break;
813 				}
814 			}
815 			kobj_close_file(file);
816 		}
817 	}
818 
819 	/*
820 	 * Sanity check of /etc/system.
821 	 */
822 	check_system_file();
823 
824 	param_preset();
825 	(void) mod_sysctl(SYS_SET_KVAR, NULL);
826 	param_check();
827 
828 	if (ask == 0)
829 		setparams();
830 }
831 
832 /*
833  * Search for a specific module variable assignment in /etc/system.  If
834  * successful, 1 is returned and the value is stored in '*value'.
835  * Otherwise 0 is returned and '*value' isn't modified.  If 'module' is
836  * NULL we look for global definitions.
837  *
838  * This is useful if the value of an assignment is needed before a
839  * module is loaded (e.g. to obtain a default privileged rctl limit).
840  */
841 int
842 mod_sysvar(const char *module, const char *name, u_longlong_t *value)
843 {
844 	struct sysparam	*sysp;
845 	int cnt = 0; /* dummy */
846 
847 	ASSERT(name != NULL);
848 	ASSERT(value != NULL);
849 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
850 
851 		if ((sysp->sys_type == MOD_SET) &&
852 		    (((module == NULL) && (sysp->sys_modnam == NULL)) ||
853 		    ((module != NULL) && (sysp->sys_modnam != NULL) &&
854 		    (strcmp(module, sysp->sys_modnam) == 0)))) {
855 
856 			ASSERT(sysp->sys_ptr != NULL);
857 
858 			if (strcmp(name, sysp->sys_ptr) == 0) {
859 				sysparam_count_entry(sysp, &cnt, value);
860 				if ((sysp->sys_flags & SYSPARAM_TERM) != 0)
861 					return (1);
862 				continue;
863 			}
864 		}
865 	}
866 	ASSERT(cnt == 0);
867 	return (0);
868 }
869 
870 /*
871  * This function scans sysparam records, which are created from the
872  * contents of /etc/system, for entries which are logical duplicates,
873  * and prints warning messages as appropriate.  When multiple "set"
874  * commands are encountered, the pileup of values with "&", "|"
875  * and "=" operators results in the final value.
876  */
877 static void
878 check_system_file(void)
879 {
880 	struct sysparam	*sysp;
881 
882 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
883 		struct sysparam *entry, *final;
884 		u_longlong_t value = 0;
885 		int cnt = 1;
886 		/*
887 		 * If the entry is already checked, skip it.
888 		 */
889 		if ((sysp->sys_flags & SYSPARAM_DUP) != 0)
890 			continue;
891 		/*
892 		 * Check if there is a duplicate entry by doing a linear
893 		 * search.
894 		 */
895 		final = sysp;
896 		for (entry = sysp->sys_next; entry != NULL;
897 		    entry = entry->sys_next) {
898 			/*
899 			 * Check the entry. if it's different, skip this.
900 			 */
901 			if (sysparam_compare_entry(sysp, entry) != 0)
902 				continue;
903 			/*
904 			 * Count the entry and put the mark.
905 			 */
906 			sysparam_count_entry(entry, &cnt, &value);
907 			entry->sys_flags |= SYSPARAM_DUP;
908 			final = entry;
909 		}
910 		final->sys_flags |= SYSPARAM_TERM;
911 		/*
912 		 * Print the warning if it's duplicated.
913 		 */
914 		if (cnt >= 2)
915 			sysparam_print_warning(final, value);
916 	}
917 }
918 
919 /*
920  * Compare the sysparam records.
921  * Return 0 if they are the same, return 1 if not.
922  */
923 static int
924 sysparam_compare_entry(struct sysparam *sysp, struct sysparam *entry)
925 {
926 	ASSERT(sysp->sys_ptr != NULL && entry->sys_ptr != NULL);
927 
928 	/*
929 	 * If the command is rootdev, rootfs, swapdev, swapfs or moddir,
930 	 * the record with the same type is treated as a duplicate record.
931 	 * In other cases, the record is treated as a duplicate record when
932 	 * its type, its module name (if it exists), and its variable name
933 	 * are the same.
934 	 */
935 	switch (sysp->sys_type) {
936 	case MOD_ROOTDEV:
937 	case MOD_ROOTFS:
938 	case MOD_SWAPDEV:
939 	case MOD_SWAPFS:
940 	case MOD_MODDIR:
941 		return (sysp->sys_type == entry->sys_type ? 0 : 1);
942 	default: /* In other cases, just go through it. */
943 		break;
944 	}
945 
946 	if (sysp->sys_type != entry->sys_type)
947 		return (1);
948 
949 	if (sysp->sys_modnam != NULL && entry->sys_modnam == NULL)
950 		return (1);
951 
952 	if (sysp->sys_modnam == NULL && entry->sys_modnam != NULL)
953 		return (1);
954 
955 	if (sysp->sys_modnam != NULL && entry->sys_modnam != NULL &&
956 	    strcmp(sysp->sys_modnam, entry->sys_modnam) != 0)
957 		return (1);
958 
959 	return (strcmp(sysp->sys_ptr, entry->sys_ptr));
960 }
961 
962 /*
963  * Translate a sysparam type value to a string.
964  */
965 static char *
966 sysparam_type_to_str(int type)
967 {
968 	struct modcmd *mcp;
969 
970 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
971 		if (mcp->mc_type == type)
972 			break;
973 	}
974 	ASSERT(mcp->mc_type == type);
975 
976 	if (type != MOD_UNKNOWN)
977 		return ((++mcp)->mc_cmdname); /* lower case */
978 	else
979 		return ("");	/* MOD_UNKNOWN */
980 }
981 
982 /*
983  * Check the entry and accumulate the number of entries.
984  */
985 static void
986 sysparam_count_entry(struct sysparam *sysp, int *cnt, u_longlong_t *value)
987 {
988 	u_longlong_t ul = sysp->sys_info;
989 
990 	switch (sysp->sys_op) {
991 	case SETOP_ASSIGN:
992 		*value = ul;
993 		(*cnt)++;
994 		return;
995 	case SETOP_AND:
996 		*value &= ul;
997 		return;
998 	case SETOP_OR:
999 		*value |= ul;
1000 		return;
1001 	default: /* Not MOD_SET */
1002 		(*cnt)++;
1003 		return;
1004 	}
1005 }
1006 
1007 /*
1008  * Print out the warning if multiple entries are found in the system file.
1009  */
1010 static void
1011 sysparam_print_warning(struct sysparam *sysp, u_longlong_t value)
1012 {
1013 	char *modnam = sysp->sys_modnam;
1014 	char *varnam = sysp->sys_ptr;
1015 	int type = sysp->sys_type;
1016 	char *typenam = sysparam_type_to_str(type);
1017 	boolean_t str_token = ((sysp->sys_flags & SYSPARAM_STR_TOKEN) != 0);
1018 	boolean_t hex_number = ((sysp->sys_flags & SYSPARAM_HEX_TOKEN) != 0);
1019 #define	warn_format1 " is set more than once in /%s. "
1020 #define	warn_format2 " applied as the current setting.\n"
1021 
1022 	ASSERT(varnam != NULL);
1023 
1024 	if (type == MOD_SET) {
1025 		/*
1026 		 * If a string token is set, print out the string
1027 		 * instead of its pointer value. In other cases,
1028 		 * print out the value with the appropriate format
1029 		 * for a hexadecimal number or a decimal number.
1030 		 */
1031 		if (modnam == NULL) {
1032 			if (str_token == B_TRUE) {
1033 				cmn_err(CE_WARN, "%s" warn_format1
1034 				    "\"%s %s = %s\"" warn_format2,
1035 				    varnam, systemfile, typenam,
1036 				    varnam, (char *)(uintptr_t)value);
1037 			} else if (hex_number == B_TRUE) {
1038 				cmn_err(CE_WARN, "%s" warn_format1
1039 				    "\"%s %s = 0x%llx\"" warn_format2,
1040 				    varnam, systemfile, typenam,
1041 				    varnam, value);
1042 			} else {
1043 				cmn_err(CE_WARN, "%s" warn_format1
1044 				    "\"%s %s = %lld\"" warn_format2,
1045 				    varnam, systemfile, typenam,
1046 				    varnam, value);
1047 			}
1048 		} else {
1049 			if (str_token == B_TRUE) {
1050 				cmn_err(CE_WARN, "%s:%s" warn_format1
1051 				    "\"%s %s:%s = %s\"" warn_format2,
1052 				    modnam, varnam, systemfile,
1053 				    typenam, modnam, varnam,
1054 				    (char *)(uintptr_t)value);
1055 			} else if (hex_number == B_TRUE) {
1056 				cmn_err(CE_WARN, "%s:%s" warn_format1
1057 				    "\"%s %s:%s = 0x%llx\"" warn_format2,
1058 				    modnam, varnam, systemfile,
1059 				    typenam, modnam, varnam, value);
1060 			} else {
1061 				cmn_err(CE_WARN, "%s:%s" warn_format1
1062 				    "\"%s %s:%s = %lld\"" warn_format2,
1063 				    modnam, varnam, systemfile,
1064 				    typenam, modnam, varnam, value);
1065 			}
1066 		}
1067 	} else {
1068 		/*
1069 		 * If the type is MOD_ROOTDEV, MOD_ROOTFS, MOD_SWAPDEV,
1070 		 * MOD_SWAPFS or MOD_MODDIR, the entry is treated as
1071 		 * a duplicate one if it has the same type regardless
1072 		 * of its variable name.
1073 		 */
1074 		switch (type) {
1075 		case MOD_ROOTDEV:
1076 		case MOD_ROOTFS:
1077 		case MOD_SWAPDEV:
1078 		case MOD_SWAPFS:
1079 		case MOD_MODDIR:
1080 			cmn_err(CE_WARN, "\"%s\" appears more than once "
1081 			    "in /%s.", typenam, systemfile);
1082 			break;
1083 		default:
1084 			cmn_err(CE_NOTE, "\"%s: %s\" appears more than once "
1085 			    "in /%s.", typenam, varnam, systemfile);
1086 			break;
1087 		}
1088 	}
1089 }
1090 
1091 /*
1092  * Process the system file commands.
1093  */
1094 int
1095 mod_sysctl(int fcn, void *p)
1096 {
1097 	static char wmesg[] = "forceload of %s failed";
1098 	struct sysparam *sysp;
1099 	char *name;
1100 	struct modctl *modp;
1101 
1102 	if (sysparam_hd == NULL)
1103 		return (0);
1104 
1105 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
1106 
1107 		switch (fcn) {
1108 
1109 		case SYS_FORCELOAD:
1110 		if (sysp->sys_type == MOD_FORCELOAD) {
1111 			name = sysp->sys_ptr;
1112 			if (modload(NULL, name) == -1)
1113 				cmn_err(CE_WARN, wmesg, name);
1114 			/*
1115 			 * The following works because it
1116 			 * runs before autounloading is started!!
1117 			 */
1118 			modp = mod_find_by_filename(NULL, name);
1119 			if (modp != NULL)
1120 				modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
1121 			/*
1122 			 * For drivers, attempt to install it.
1123 			 */
1124 			if (strncmp(sysp->sys_ptr, "drv", 3) == 0) {
1125 				(void) ddi_install_driver(name + 4);
1126 			}
1127 		}
1128 		break;
1129 
1130 		case SYS_SET_KVAR:
1131 		case SYS_SET_MVAR:
1132 			if (sysp->sys_type == MOD_SET)
1133 				sys_set_var(fcn, sysp, p);
1134 			break;
1135 
1136 		case SYS_CHECK_EXCLUDE:
1137 			if (sysp->sys_type == MOD_EXCLUDE) {
1138 				if (p == NULL || sysp->sys_ptr == NULL)
1139 					return (0);
1140 				if (strcmp((char *)p, sysp->sys_ptr) == 0)
1141 					return (1);
1142 			}
1143 		}
1144 	}
1145 
1146 	return (0);
1147 }
1148 
1149 /*
1150  * Process the system file commands, by type.
1151  */
1152 int
1153 mod_sysctl_type(int type, int (*func)(struct sysparam *, void *), void *p)
1154 {
1155 	struct sysparam *sysp;
1156 	int	err;
1157 
1158 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next)
1159 		if (sysp->sys_type == type)
1160 			if (err = (*(func))(sysp, p))
1161 				return (err);
1162 	return (0);
1163 }
1164 
1165 
1166 static char seterr[] = "Symbol %s has size of 0 in symbol table. %s";
1167 static char assumption[] = "Assuming it is an 'int'";
1168 static char defmsg[] = "Trying to set a variable that is of size %d";
1169 
1170 static void set_int8_var(uintptr_t, struct sysparam *);
1171 static void set_int16_var(uintptr_t, struct sysparam *);
1172 static void set_int32_var(uintptr_t, struct sysparam *);
1173 static void set_int64_var(uintptr_t, struct sysparam *);
1174 
1175 static void
1176 sys_set_var(int fcn, struct sysparam *sysp, void *p)
1177 {
1178 	uintptr_t symaddr;
1179 	int size;
1180 
1181 	if (fcn == SYS_SET_KVAR && sysp->sys_modnam == NULL) {
1182 		symaddr = kobj_getelfsym(sysp->sys_ptr, NULL, &size);
1183 	} else if (fcn == SYS_SET_MVAR) {
1184 		if (sysp->sys_modnam == (char *)NULL ||
1185 			strcmp(((struct modctl *)p)->mod_modname,
1186 			    sysp->sys_modnam) != 0)
1187 				return;
1188 		symaddr = kobj_getelfsym(sysp->sys_ptr,
1189 		    ((struct modctl *)p)->mod_mp, &size);
1190 	} else
1191 		return;
1192 
1193 	if (symaddr != NULL) {
1194 		switch (size) {
1195 		case 1:
1196 			set_int8_var(symaddr, sysp);
1197 			break;
1198 		case 2:
1199 			set_int16_var(symaddr, sysp);
1200 			break;
1201 		case 0:
1202 			cmn_err(CE_WARN, seterr, sysp->sys_ptr, assumption);
1203 			/*FALLTHROUGH*/
1204 		case 4:
1205 			set_int32_var(symaddr, sysp);
1206 			break;
1207 		case 8:
1208 			set_int64_var(symaddr, sysp);
1209 			break;
1210 		default:
1211 			cmn_err(CE_WARN, defmsg, size);
1212 			break;
1213 		}
1214 	} else {
1215 		printf("sorry, variable '%s' is not defined in the '%s' ",
1216 		    sysp->sys_ptr,
1217 		    sysp->sys_modnam ? sysp->sys_modnam : "kernel");
1218 		if (sysp->sys_modnam)
1219 			printf("module");
1220 		printf("\n");
1221 	}
1222 }
1223 
1224 static void
1225 set_int8_var(uintptr_t symaddr, struct sysparam *sysp)
1226 {
1227 	uint8_t uc = (uint8_t)sysp->sys_info;
1228 
1229 	if (moddebug & MODDEBUG_LOADMSG)
1230 		printf("OP: %x: param '%s' was '0x%" PRIx8
1231 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
1232 		    *(uint8_t *)symaddr, sysp->sys_modnam);
1233 
1234 	switch (sysp->sys_op) {
1235 	case SETOP_ASSIGN:
1236 		*(uint8_t *)symaddr = uc;
1237 		break;
1238 	case SETOP_AND:
1239 		*(uint8_t *)symaddr &= uc;
1240 		break;
1241 	case SETOP_OR:
1242 		*(uint8_t *)symaddr |= uc;
1243 		break;
1244 	}
1245 
1246 	if (moddebug & MODDEBUG_LOADMSG)
1247 		printf("now it is set to '0x%" PRIx8 "'.\n",
1248 		    *(uint8_t *)symaddr);
1249 }
1250 
1251 static void
1252 set_int16_var(uintptr_t symaddr, struct sysparam *sysp)
1253 {
1254 	uint16_t us = (uint16_t)sysp->sys_info;
1255 
1256 	if (moddebug & MODDEBUG_LOADMSG)
1257 		printf("OP: %x: param '%s' was '0x%" PRIx16
1258 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
1259 		    *(uint16_t *)symaddr, sysp->sys_modnam);
1260 
1261 	switch (sysp->sys_op) {
1262 	case SETOP_ASSIGN:
1263 		*(uint16_t *)symaddr = us;
1264 		break;
1265 	case SETOP_AND:
1266 		*(uint16_t *)symaddr &= us;
1267 		break;
1268 	case SETOP_OR:
1269 		*(uint16_t *)symaddr |= us;
1270 		break;
1271 	}
1272 
1273 	if (moddebug & MODDEBUG_LOADMSG)
1274 		printf("now it is set to '0x%" PRIx16 "'.\n",
1275 		    *(uint16_t *)symaddr);
1276 }
1277 
1278 static void
1279 set_int32_var(uintptr_t symaddr, struct sysparam *sysp)
1280 {
1281 	uint32_t ui = (uint32_t)sysp->sys_info;
1282 
1283 	if (moddebug & MODDEBUG_LOADMSG)
1284 		printf("OP: %x: param '%s' was '0x%" PRIx32
1285 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
1286 		    *(uint32_t *)symaddr, sysp->sys_modnam);
1287 
1288 	switch (sysp->sys_op) {
1289 	case SETOP_ASSIGN:
1290 		*(uint32_t *)symaddr = ui;
1291 		break;
1292 	case SETOP_AND:
1293 		*(uint32_t *)symaddr &= ui;
1294 		break;
1295 	case SETOP_OR:
1296 		*(uint32_t *)symaddr |= ui;
1297 		break;
1298 	}
1299 
1300 	if (moddebug & MODDEBUG_LOADMSG)
1301 		printf("now it is set to '0x%" PRIx32 "'.\n",
1302 		    *(uint32_t *)symaddr);
1303 }
1304 
1305 static void
1306 set_int64_var(uintptr_t symaddr, struct sysparam *sysp)
1307 {
1308 	uint64_t ul = sysp->sys_info;
1309 
1310 	if (moddebug & MODDEBUG_LOADMSG)
1311 		printf("OP: %x: param '%s' was '0x%" PRIx64
1312 		    "' in module: '%s'.\n", sysp->sys_op, sysp->sys_ptr,
1313 		    *(uint64_t *)symaddr, sysp->sys_modnam);
1314 
1315 	switch (sysp->sys_op) {
1316 	case SETOP_ASSIGN:
1317 		*(uint64_t *)symaddr = ul;
1318 		break;
1319 	case SETOP_AND:
1320 		*(uint64_t *)symaddr &= ul;
1321 		break;
1322 	case SETOP_OR:
1323 		*(uint64_t *)symaddr |= ul;
1324 		break;
1325 	}
1326 
1327 	if (moddebug & MODDEBUG_LOADMSG)
1328 		printf("now it is set to '0x%" PRIx64 "'.\n",
1329 		    *(uint64_t *)symaddr);
1330 }
1331 
1332 /*
1333  * The next item on the line is a string value. Allocate memory for
1334  * it and copy the string. Return 1, and set arg ptr to newly allocated
1335  * and initialized buffer, or NULL if an error occurs.
1336  */
1337 int
1338 kobj_get_string(u_longlong_t *llptr, char *tchar)
1339 {
1340 	char *cp;
1341 	char *start = (char *)0;
1342 	int len = 0;
1343 
1344 	len = strlen(tchar);
1345 	start = tchar;
1346 	/* copy string */
1347 	cp = vmem_alloc(mod_sysfile_arena, len + 1, VM_SLEEP);
1348 	bzero(cp, len + 1);
1349 	*llptr = (u_longlong_t)(uintptr_t)cp;
1350 	for (; len > 0; len--) {
1351 		/* convert some common escape sequences */
1352 		if (*start == '\\') {
1353 			switch (*(start + 1)) {
1354 			case 't':
1355 				/* tab */
1356 				*cp++ = '\t';
1357 				len--;
1358 				start += 2;
1359 				break;
1360 			case 'n':
1361 				/* new line */
1362 				*cp++ = '\n';
1363 				len--;
1364 				start += 2;
1365 				break;
1366 			case 'b':
1367 				/* back space */
1368 				*cp++ = '\b';
1369 				len--;
1370 				start += 2;
1371 				break;
1372 			default:
1373 				/* simply copy it */
1374 				*cp++ = *start++;
1375 				break;
1376 			}
1377 		} else
1378 			*cp++ = *start++;
1379 	}
1380 	*cp = '\0';
1381 	return (1);
1382 }
1383 
1384 
1385 /*
1386  * this function frees the memory allocated by kobj_get_string
1387  */
1388 void
1389 kobj_free_string(void *ptr, int len)
1390 {
1391 	vmem_free(mod_sysfile_arena, ptr, len);
1392 }
1393 
1394 
1395 /*
1396  * get a decimal octal or hex number. Handle '~' for one's complement.
1397  */
1398 int
1399 kobj_getvalue(const char *token, u_longlong_t *valuep)
1400 {
1401 	int radix;
1402 	u_longlong_t retval = 0;
1403 	int onescompl = 0;
1404 	int negate = 0;
1405 	char c;
1406 
1407 	if (*token == '~') {
1408 		onescompl++; /* perform one's complement on result */
1409 		token++;
1410 	} else if (*token == '-') {
1411 		negate++;
1412 		token++;
1413 	}
1414 	if (*token == '0') {
1415 		token++;
1416 		c = *token;
1417 
1418 		if (c == '\0') {
1419 			*valuep = 0;	/* value is 0 */
1420 			return (0);
1421 		}
1422 
1423 		if (c == 'x' || c == 'X') {
1424 			radix = 16;
1425 			token++;
1426 		} else
1427 			radix = 8;
1428 	} else
1429 		radix = 10;
1430 
1431 	while ((c = *token++)) {
1432 		switch (radix) {
1433 		case 8:
1434 			if (c >= '0' && c <= '7')
1435 				c -= '0';
1436 			else
1437 				return (-1);	/* invalid number */
1438 			retval = (retval << 3) + c;
1439 			break;
1440 		case 10:
1441 			if (c >= '0' && c <= '9')
1442 				c -= '0';
1443 			else
1444 				return (-1);	/* invalid number */
1445 			retval = (retval * 10) + c;
1446 			break;
1447 		case 16:
1448 			if (c >= 'a' && c <= 'f')
1449 				c = c - 'a' + 10;
1450 			else if (c >= 'A' && c <= 'F')
1451 				c = c - 'A' + 10;
1452 			else if (c >= '0' && c <= '9')
1453 				c -= '0';
1454 			else
1455 				return (-1);	/* invalid number */
1456 			retval = (retval << 4) + c;
1457 			break;
1458 		}
1459 	}
1460 	if (onescompl)
1461 		retval = ~retval;
1462 	if (negate)
1463 		retval = -retval;
1464 	*valuep = retval;
1465 	return (0);
1466 }
1467 
1468 /*
1469  * Path to the root device and root filesystem type from
1470  * property information derived from the boot subsystem
1471  */
1472 void
1473 setbootpath(char *path)
1474 {
1475 	rootfs.bo_flags |= BO_VALID;
1476 	(void) copystr(path, rootfs.bo_name, BO_MAXOBJNAME, NULL);
1477 	BMDPRINTF(("rootfs bootpath: %s\n", rootfs.bo_name));
1478 }
1479 
1480 void
1481 setbootfstype(char *fstype)
1482 {
1483 	(void) copystr(fstype, rootfs.bo_fstype, BO_MAXFSNAME, NULL);
1484 	BMDPRINTF(("rootfs fstype: %s\n", rootfs.bo_fstype));
1485 }
1486 
1487 /*
1488  * set parameters that can be set early during initialization.
1489  */
1490 static void
1491 setparams()
1492 {
1493 	struct sysparam *sysp;
1494 	struct bootobj *bootobjp;
1495 
1496 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
1497 
1498 		if (sysp->sys_type == MOD_MODDIR) {
1499 			default_path = sysp->sys_ptr;
1500 			continue;
1501 		}
1502 
1503 		if (sysp->sys_type == MOD_SWAPDEV ||
1504 		    sysp->sys_type == MOD_SWAPFS)
1505 			bootobjp = &swapfile;
1506 		else if (sysp->sys_type == MOD_ROOTFS)
1507 			bootobjp = &rootfs;
1508 
1509 		switch (sysp->sys_type) {
1510 		case MOD_ROOTDEV:
1511 			root_is_svm = 1;
1512 			(void) copystr(sysp->sys_ptr, svm_bootpath,
1513 			    BO_MAXOBJNAME, NULL);
1514 			break;
1515 		case MOD_SWAPDEV:
1516 			bootobjp->bo_flags |= BO_VALID;
1517 			(void) copystr(sysp->sys_ptr, bootobjp->bo_name,
1518 			    BO_MAXOBJNAME, NULL);
1519 			break;
1520 		case MOD_ROOTFS:
1521 		case MOD_SWAPFS:
1522 			bootobjp->bo_flags |= BO_VALID;
1523 			(void) copystr(sysp->sys_ptr, bootobjp->bo_fstype,
1524 			    BO_MAXOBJNAME, NULL);
1525 			break;
1526 		case MOD_ZFSROOT:
1527 			(void) copystr(sysp->sys_ptr, zfs_bootpath,
1528 			    BO_MAXOBJNAME, NULL);
1529 			break;
1530 		default:
1531 			break;
1532 		}
1533 	}
1534 }
1535 
1536 /*
1537  * clean up after an error.
1538  */
1539 static void
1540 hwc_free(struct hwc_spec *hwcp)
1541 {
1542 	char *name;
1543 
1544 	if ((name = hwcp->hwc_parent_name) != NULL)
1545 		kmem_free(name, strlen(name) + 1);
1546 	if ((name = hwcp->hwc_class_name) != NULL)
1547 		kmem_free(name, strlen(name) + 1);
1548 	if ((name = hwcp->hwc_devi_name) != NULL)
1549 		kmem_free(name, strlen(name) + 1);
1550 	i_ddi_prop_list_delete(hwcp->hwc_devi_sys_prop_ptr);
1551 	kmem_free(hwcp, sizeof (struct hwc_spec));
1552 }
1553 
1554 /*
1555  * Free a list of specs
1556  */
1557 void
1558 hwc_free_spec_list(struct hwc_spec *list)
1559 {
1560 	while (list) {
1561 		struct hwc_spec *tmp = list;
1562 		list = tmp->hwc_next;
1563 		hwc_free(tmp);
1564 	}
1565 }
1566 
1567 struct val_list {
1568 	struct val_list *val_next;
1569 	enum {
1570 		VAL_STRING,
1571 		VAL_INTEGER
1572 	} val_type;
1573 	int		val_size;
1574 	union {
1575 		char *string;
1576 		int integer;
1577 	} val;
1578 };
1579 
1580 static struct val_list *
1581 add_val(struct val_list **val_listp, struct val_list *tail,
1582     int val_type, caddr_t val)
1583 {
1584 	struct val_list *new_val;
1585 #ifdef DEBUG
1586 	struct val_list *listp = *val_listp;
1587 #endif
1588 
1589 	new_val = kmem_alloc(sizeof (struct val_list), KM_SLEEP);
1590 	new_val->val_next = NULL;
1591 	if ((new_val->val_type = val_type) == VAL_STRING) {
1592 		new_val->val_size = strlen((char *)val) + 1;
1593 		new_val->val.string = kmem_alloc(new_val->val_size, KM_SLEEP);
1594 		(void) strcpy(new_val->val.string, (char *)val);
1595 	} else {
1596 		new_val->val_size = sizeof (int);
1597 		new_val->val.integer = (int)(uintptr_t)val;
1598 	}
1599 
1600 	ASSERT((listp == NULL && tail == NULL) ||
1601 	    (listp != NULL && tail != NULL));
1602 
1603 	if (tail != NULL) {
1604 		ASSERT(tail->val_next == NULL);
1605 		tail->val_next = new_val;
1606 	} else {
1607 		*val_listp = new_val;
1608 	}
1609 
1610 	return (new_val);
1611 }
1612 
1613 static void
1614 free_val_list(struct val_list *head)
1615 {
1616 	struct val_list *tval_list;
1617 
1618 	for (/* CSTYLED */; head != NULL; /* CSTYLED */) {
1619 		tval_list = head;
1620 		head = head->val_next;
1621 		if (tval_list->val_type == VAL_STRING)
1622 			kmem_free(tval_list->val.string, tval_list->val_size);
1623 		kmem_free(tval_list, sizeof (struct val_list));
1624 	}
1625 }
1626 
1627 /*
1628  * make sure there are no reserved IEEE 1275 characters (except
1629  * for uppercase characters).
1630  */
1631 static int
1632 valid_prop_name(char *name)
1633 {
1634 	int i;
1635 	int len = strlen(name);
1636 
1637 	for (i = 0; i < len; i++) {
1638 		if (name[i] < 0x21 ||
1639 			name[i] == '/' ||
1640 			name[i] == '\\' ||
1641 			name[i] == ':' ||
1642 			name[i] == '[' ||
1643 			name[i] == ']' ||
1644 			name[i] == '@')
1645 				return (0);
1646 	}
1647 	return (1);
1648 }
1649 
1650 static void
1651 make_prop(struct _buf *file, dev_info_t *devi, char *name, struct val_list *val)
1652 {
1653 	int propcnt = 0, val_type;
1654 	struct val_list *vl, *tvl;
1655 	caddr_t valbuf = NULL;
1656 	char **valsp;
1657 	int *valip;
1658 
1659 	if (name == NULL)
1660 		return;
1661 
1662 #ifdef DEBUG
1663 	parse_debug(NULL, "%s", name);
1664 #endif
1665 	if (!valid_prop_name(name)) {
1666 		cmn_err(CE_WARN, "invalid property name '%s'", name);
1667 		return;
1668 	}
1669 	if (val) {
1670 		for (vl = val, val_type = vl->val_type; vl; vl = vl->val_next) {
1671 			if (val_type != vl->val_type) {
1672 				cmn_err(CE_WARN, "Mixed types in value list");
1673 				return;
1674 			}
1675 			propcnt++;
1676 		}
1677 
1678 		vl = val;
1679 
1680 		if (val_type == VAL_INTEGER) {
1681 			valip = (int *)kmem_alloc(
1682 			    (propcnt * sizeof (int)), KM_SLEEP);
1683 			valbuf = (caddr_t)valip;
1684 			while (vl) {
1685 				tvl = vl;
1686 				vl = vl->val_next;
1687 #ifdef DEBUG
1688 				parse_debug(NULL, " %x",  tvl->val.integer);
1689 #endif
1690 				*valip = tvl->val.integer;
1691 				valip++;
1692 			}
1693 			/* restore valip */
1694 			valip = (int *)valbuf;
1695 
1696 			/* create the property */
1697 			if (e_ddi_prop_update_int_array(DDI_DEV_T_NONE, devi,
1698 			    name, valip, propcnt) != DDI_PROP_SUCCESS) {
1699 				kobj_file_err(CE_WARN, file,
1700 				    "cannot create property %s", name);
1701 			}
1702 			/* cleanup */
1703 			kmem_free(valip, (propcnt * sizeof (int)));
1704 		} else if (val_type == VAL_STRING) {
1705 			valsp = (char **)kmem_alloc(
1706 			    ((propcnt + 1) * sizeof (char *)), KM_SLEEP);
1707 			valbuf = (caddr_t)valsp;
1708 			while (vl) {
1709 				tvl = vl;
1710 				vl = vl->val_next;
1711 #ifdef DEBUG
1712 				parse_debug(NULL, " %s", tvl->val.string);
1713 #endif
1714 				*valsp = tvl->val.string;
1715 				valsp++;
1716 			}
1717 			/* terminate array with NULL */
1718 			*valsp = NULL;
1719 
1720 			/* restore valsp */
1721 			valsp = (char **)valbuf;
1722 
1723 			/* create the property */
1724 			if (e_ddi_prop_update_string_array(DDI_DEV_T_NONE,
1725 			    devi, name, valsp, propcnt)
1726 			    != DDI_PROP_SUCCESS) {
1727 				kobj_file_err(CE_WARN, file,
1728 				    "cannot create property %s", name);
1729 			}
1730 			/* Clean up */
1731 			kmem_free(valsp, ((propcnt + 1) * sizeof (char *)));
1732 		} else {
1733 			cmn_err(CE_WARN, "Invalid property type");
1734 			return;
1735 		}
1736 	} else {
1737 		/*
1738 		 * No value was passed in with property so we will assume
1739 		 * it is a "boolean" property and create an integer
1740 		 * property with 0 value.
1741 		 */
1742 #ifdef DEBUG
1743 		parse_debug(NULL, "\n");
1744 #endif
1745 		if (e_ddi_prop_update_int(DDI_DEV_T_NONE, devi, name, 0)
1746 		    != DDI_PROP_SUCCESS) {
1747 			kobj_file_err(CE_WARN, file,
1748 			    "cannot create property %s", name);
1749 		}
1750 	}
1751 }
1752 
1753 static char omit_err[] = "(the ';' may have been omitted on previous spec!)";
1754 static char prnt_err[] = "'parent' property already specified";
1755 static char nm_err[] = "'name' property already specified";
1756 static char class_err[] = "'class' property already specified";
1757 
1758 typedef enum {
1759 	hwc_begin, parent, drvname, drvclass, prop,
1760 	parent_equals, name_equals, drvclass_equals,
1761 	parent_equals_string, name_equals_string,
1762 	drvclass_equals_string,
1763 	prop_equals, prop_equals_string, prop_equals_integer,
1764 	prop_equals_string_comma, prop_equals_integer_comma
1765 } hwc_state_t;
1766 
1767 static struct hwc_spec *
1768 get_hwc_spec(struct _buf *file, char *tokbuf, size_t linesize)
1769 {
1770 	char *prop_name;
1771 	token_t token;
1772 	struct hwc_spec *hwcp;
1773 	struct dev_info *devi;
1774 	struct val_list *val_list, *tail;
1775 	hwc_state_t state;
1776 	u_longlong_t ival;
1777 
1778 	hwcp = kmem_zalloc(sizeof (*hwcp), KM_SLEEP);
1779 	devi = kmem_zalloc(sizeof (*devi), KM_SLEEP);
1780 
1781 	state = hwc_begin;
1782 	token = NAME;
1783 	prop_name = NULL;
1784 	val_list = NULL;
1785 	tail = NULL;
1786 	do {
1787 #ifdef DEBUG
1788 		parse_debug(NULL, "state 0x%x\n", state);
1789 #endif
1790 		switch (token) {
1791 		case NAME:
1792 			switch (state) {
1793 			case prop:
1794 			case prop_equals_string:
1795 			case prop_equals_integer:
1796 				make_prop(file, (dev_info_t *)devi,
1797 				    prop_name, val_list);
1798 				if (prop_name) {
1799 					kmem_free(prop_name,
1800 					    strlen(prop_name) + 1);
1801 					prop_name = NULL;
1802 				}
1803 				if (val_list) {
1804 					free_val_list(val_list);
1805 					val_list = NULL;
1806 				}
1807 				tail = NULL;
1808 				/*FALLTHROUGH*/
1809 			case hwc_begin:
1810 				if (strcmp(tokbuf, "PARENT") == 0 ||
1811 				    strcmp(tokbuf, "parent") == 0) {
1812 					state = parent;
1813 				} else if (strcmp(tokbuf, "NAME") == 0 ||
1814 				    strcmp(tokbuf, "name") == 0) {
1815 					state = drvname;
1816 				} else if (strcmp(tokbuf, "CLASS") == 0 ||
1817 				    strcmp(tokbuf, "class") == 0) {
1818 					state = drvclass;
1819 					prop_name = kmem_alloc(strlen(tokbuf) +
1820 						1, KM_SLEEP);
1821 					(void) strcpy(prop_name, tokbuf);
1822 				} else {
1823 					state = prop;
1824 					prop_name = kmem_alloc(strlen(tokbuf) +
1825 						1, KM_SLEEP);
1826 					(void) strcpy(prop_name, tokbuf);
1827 				}
1828 				break;
1829 			default:
1830 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1831 			}
1832 			break;
1833 		case EQUALS:
1834 			switch (state) {
1835 			case drvname:
1836 				state = name_equals;
1837 				break;
1838 			case parent:
1839 				state = parent_equals;
1840 				break;
1841 			case drvclass:
1842 				state = drvclass_equals;
1843 				break;
1844 			case prop:
1845 				state = prop_equals;
1846 				break;
1847 			default:
1848 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1849 			}
1850 			break;
1851 		case STRING:
1852 			switch (state) {
1853 			case name_equals:
1854 				if (ddi_get_name((dev_info_t *)devi)) {
1855 					kobj_file_err(CE_WARN, file, "%s %s",
1856 						nm_err, omit_err);
1857 					goto bad;
1858 				}
1859 				devi->devi_name = kmem_alloc(strlen(tokbuf) + 1,
1860 				    KM_SLEEP);
1861 				(void) strcpy(devi->devi_name, tokbuf);
1862 				state = hwc_begin;
1863 				break;
1864 			case parent_equals:
1865 				if (hwcp->hwc_parent_name) {
1866 					kobj_file_err(CE_WARN, file, "%s %s",
1867 						prnt_err, omit_err);
1868 					goto bad;
1869 				}
1870 				hwcp->hwc_parent_name = kmem_alloc(strlen
1871 				    (tokbuf) + 1, KM_SLEEP);
1872 				(void) strcpy(hwcp->hwc_parent_name, tokbuf);
1873 				state = hwc_begin;
1874 				break;
1875 			case drvclass_equals:
1876 				if (hwcp->hwc_class_name) {
1877 					kobj_file_err(CE_WARN, file, class_err);
1878 					goto bad;
1879 				}
1880 				hwcp->hwc_class_name = kmem_alloc(
1881 				    strlen(tokbuf) + 1, KM_SLEEP);
1882 				(void) strcpy(hwcp->hwc_class_name, tokbuf);
1883 				/*FALLTHROUGH*/
1884 			case prop_equals:
1885 			case prop_equals_string_comma:
1886 				tail = add_val(&val_list, tail, VAL_STRING,
1887 				    tokbuf);
1888 				state = prop_equals_string;
1889 				break;
1890 			default:
1891 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1892 			}
1893 			break;
1894 		case HEXVAL:
1895 		case DECVAL:
1896 			switch (state) {
1897 			case prop_equals:
1898 			case prop_equals_integer_comma:
1899 				(void) kobj_getvalue(tokbuf, &ival);
1900 				tail = add_val(&val_list, tail,
1901 				    VAL_INTEGER, (caddr_t)(uintptr_t)ival);
1902 				state = prop_equals_integer;
1903 				break;
1904 			default:
1905 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1906 			}
1907 			break;
1908 		case COMMA:
1909 			switch (state) {
1910 			case prop_equals_string:
1911 				state = prop_equals_string_comma;
1912 				break;
1913 			case prop_equals_integer:
1914 				state = prop_equals_integer_comma;
1915 				break;
1916 			default:
1917 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1918 			}
1919 			break;
1920 		case NEWLINE:
1921 			kobj_newline(file);
1922 			break;
1923 		case POUND:
1924 			/*
1925 			 * Skip comments.
1926 			 */
1927 			kobj_find_eol(file);
1928 			break;
1929 		case EOF:
1930 			kobj_file_err(CE_WARN, file, "Unexpected EOF");
1931 			goto bad;
1932 		default:
1933 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1934 			goto bad;
1935 		}
1936 	} while ((token = kobj_lex(file, tokbuf, linesize)) != SEMICOLON);
1937 
1938 	switch (state) {
1939 	case prop:
1940 	case prop_equals_string:
1941 	case prop_equals_integer:
1942 		make_prop(file, (dev_info_t *)devi,
1943 			prop_name, val_list);
1944 		break;
1945 
1946 	case hwc_begin:
1947 		break;
1948 	default:
1949 		kobj_file_err(CE_WARN, file, "Unexpected end of line");
1950 		break;
1951 	}
1952 
1953 	/* copy 2 relevant members of devi to hwcp */
1954 	hwcp->hwc_devi_sys_prop_ptr = devi->devi_sys_prop_ptr;
1955 	hwcp->hwc_devi_name = devi->devi_name;
1956 
1957 	if (prop_name)
1958 		kmem_free(prop_name, strlen(prop_name) + 1);
1959 	if (val_list)
1960 		free_val_list(val_list);
1961 
1962 	kmem_free(devi, sizeof (struct dev_info));
1963 
1964 	return (hwcp);
1965 
1966 bad:
1967 	if (prop_name)
1968 		kmem_free(prop_name, strlen(prop_name) + 1);
1969 	if (val_list)
1970 		free_val_list(val_list);
1971 
1972 	hwc_free(hwcp);
1973 
1974 	if (devi->devi_name)
1975 		kmem_free(devi->devi_name, strlen(devi->devi_name) + 1);
1976 
1977 	kmem_free(devi, sizeof (struct dev_info));
1978 
1979 	return (NULL);
1980 }
1981 
1982 /*
1983  * This is the primary kernel interface to parse driver.conf files.
1984  *
1985  * Yet another bigstk thread handoff due to deep kernel stacks when booting
1986  * cache-only-clients.
1987  */
1988 int
1989 hwc_parse(char *fname, struct par_list **pl, ddi_prop_t **props)
1990 {
1991 	int ret;
1992 	struct hwc_parse_mt *pltp = hwc_parse_mtalloc(fname, pl, props);
1993 
1994 	if (curthread != &t0) {
1995 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
1996 		    hwc_parse_thread, pltp, 0, &p0, TS_RUN, maxclsyspri);
1997 		sema_p(&pltp->sema);
1998 	} else {
1999 		pltp->rv = hwc_parse_now(fname, pl, props);
2000 	}
2001 	ret = pltp->rv;
2002 	hwc_parse_mtfree(pltp);
2003 	return (ret);
2004 }
2005 
2006 /*
2007  * Calls to hwc_parse() are handled off to this routine in a separate
2008  * thread.
2009  */
2010 static void
2011 hwc_parse_thread(struct hwc_parse_mt *pltp)
2012 {
2013 	kmutex_t	cpr_lk;
2014 	callb_cpr_t	cpr_i;
2015 
2016 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
2017 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "hwc_parse");
2018 
2019 	/*
2020 	 * load and parse the .conf file
2021 	 * return the hwc_spec list (if any) to the creator of this thread
2022 	 */
2023 	pltp->rv = hwc_parse_now(pltp->name, pltp->pl, pltp->props);
2024 	sema_v(&pltp->sema);
2025 	mutex_enter(&cpr_lk);
2026 	CALLB_CPR_EXIT(&cpr_i);
2027 	mutex_destroy(&cpr_lk);
2028 	thread_exit();
2029 }
2030 
2031 /*
2032  * allocate and initialize a hwc_parse thread control structure
2033  */
2034 static struct hwc_parse_mt *
2035 hwc_parse_mtalloc(char *name, struct par_list **pl, ddi_prop_t **props)
2036 {
2037 	struct hwc_parse_mt *pltp = kmem_zalloc(sizeof (*pltp), KM_SLEEP);
2038 
2039 	ASSERT(name != NULL);
2040 
2041 	pltp->name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2042 	bcopy(name, pltp->name, strlen(name) + 1);
2043 	pltp->pl = pl;
2044 	pltp->props = props;
2045 
2046 	sema_init(&pltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
2047 	return (pltp);
2048 }
2049 
2050 /*
2051  * free a hwc_parse thread control structure
2052  */
2053 static void
2054 hwc_parse_mtfree(struct hwc_parse_mt *pltp)
2055 {
2056 	sema_destroy(&pltp->sema);
2057 
2058 	kmem_free(pltp->name, strlen(pltp->name) + 1);
2059 	kmem_free(pltp, sizeof (*pltp));
2060 }
2061 
2062 /*
2063  * hwc_parse -- parse an hwconf file.  Ignore error lines and parse
2064  * as much as possible.
2065  */
2066 static int
2067 hwc_parse_now(char *fname, struct par_list **pl, ddi_prop_t **props)
2068 {
2069 	struct _buf *file;
2070 	struct hwc_spec *hwcp;
2071 	char *tokval;
2072 	token_t token;
2073 
2074 	/*
2075 	 * Don't use kobj_open_path's use_moddir_suffix option, we only
2076 	 * expect to find conf files in the base module directory, not
2077 	 * an ISA-specific subdirectory.
2078 	 */
2079 	if ((file = kobj_open_path(fname, 1, 0)) == (struct _buf *)-1) {
2080 		if (moddebug & MODDEBUG_ERRMSG)
2081 			cmn_err(CE_WARN, "Cannot open %s", fname);
2082 		return (-1);
2083 	}
2084 
2085 	/*
2086 	 * Initialize variables
2087 	 */
2088 	tokval = kmem_alloc(MAX_HWC_LINESIZE, KM_SLEEP);
2089 
2090 	while ((token = kobj_lex(file, tokval, MAX_HWC_LINESIZE)) != EOF) {
2091 		switch (token) {
2092 		case POUND:
2093 			/*
2094 			 * Skip comments.
2095 			 */
2096 			kobj_find_eol(file);
2097 			break;
2098 		case NAME:
2099 			hwcp = get_hwc_spec(file, tokval, MAX_HWC_LINESIZE);
2100 			if (hwcp == NULL)
2101 				break;
2102 			/*
2103 			 * No devi_name indicates global property.
2104 			 * Make sure parent and class not NULL.
2105 			 */
2106 			if (hwcp->hwc_devi_name == NULL) {
2107 				if (hwcp->hwc_parent_name ||
2108 				    hwcp->hwc_class_name) {
2109 					kobj_file_err(CE_WARN, file,
2110 					    "missing name attribute");
2111 					hwc_free(hwcp);
2112 					continue;
2113 				}
2114 				/* Add to global property list */
2115 				add_props(hwcp, props);
2116 				break;
2117 			}
2118 
2119 			/*
2120 			 * This is a node spec, either parent or class
2121 			 * must be specified.
2122 			 */
2123 			if ((hwcp->hwc_parent_name == NULL) &&
2124 			    (hwcp->hwc_class_name == NULL)) {
2125 				kobj_file_err(CE_WARN, file,
2126 				    "missing parent or class attribute");
2127 				hwc_free(hwcp);
2128 				continue;
2129 			}
2130 
2131 			/* add to node spec list */
2132 			add_spec(hwcp, pl);
2133 			break;
2134 		case NEWLINE:
2135 			kobj_newline(file);
2136 			break;
2137 		default:
2138 			kobj_file_err(CE_WARN, file, tok_err, tokval);
2139 			break;
2140 		}
2141 	}
2142 	/*
2143 	 * XXX - Check for clean termination.
2144 	 */
2145 	kmem_free(tokval, MAX_HWC_LINESIZE);
2146 	kobj_close_file(file);
2147 	return (0);	/* always return success */
2148 }
2149 
2150 void
2151 make_aliases(struct bind **bhash)
2152 {
2153 	enum {
2154 		AL_NEW, AL_DRVNAME, AL_DRVNAME_COMMA, AL_ALIAS, AL_ALIAS_COMMA
2155 	} state;
2156 
2157 	struct _buf *file;
2158 	char tokbuf[MAXNAMELEN];
2159 	char drvbuf[MAXNAMELEN];
2160 	token_t token;
2161 	major_t major;
2162 	int done = 0;
2163 	static char dupwarn[] = "!Driver alias \"%s\" conflicts with "
2164 	    "an existing driver name or alias.";
2165 
2166 	if ((file = kobj_open_file(dafile)) == (struct _buf *)-1)
2167 		return;
2168 
2169 	state = AL_NEW;
2170 	major = (major_t)-1;
2171 	while (!done) {
2172 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2173 		switch (token) {
2174 		case POUND:
2175 			/*
2176 			 * Skip comments.
2177 			 */
2178 			kobj_find_eol(file);
2179 			break;
2180 		case NAME:
2181 		case STRING:
2182 			switch (state) {
2183 			case AL_NEW:
2184 				(void) strcpy(drvbuf, tokbuf);
2185 				state = AL_DRVNAME;
2186 				break;
2187 			case AL_DRVNAME_COMMA:
2188 				(void) strcat(drvbuf, tokbuf);
2189 				state = AL_DRVNAME;
2190 				break;
2191 			case AL_ALIAS_COMMA:
2192 				(void) strcat(drvbuf, tokbuf);
2193 				state = AL_ALIAS;
2194 				break;
2195 			case AL_DRVNAME:
2196 				major = mod_name_to_major(drvbuf);
2197 				if (major == (major_t)-1) {
2198 					kobj_find_eol(file);
2199 					state = AL_NEW;
2200 				} else {
2201 					(void) strcpy(drvbuf, tokbuf);
2202 					state = AL_ALIAS;
2203 				}
2204 				break;
2205 			case AL_ALIAS:
2206 				if (make_mbind(drvbuf, major, NULL, bhash)
2207 				    != 0) {
2208 					cmn_err(CE_WARN, dupwarn, drvbuf);
2209 				}
2210 				/*
2211 				 * copy this token just in case that there
2212 				 * are multiple names on the same line.
2213 				 */
2214 				(void) strcpy(drvbuf, tokbuf);
2215 				break;
2216 			}
2217 			break;
2218 		case COMMA:
2219 			(void) strcat(drvbuf, tokbuf);
2220 			switch (state) {
2221 			case AL_DRVNAME:
2222 				state = AL_DRVNAME_COMMA;
2223 				break;
2224 			case AL_ALIAS:
2225 				state = AL_ALIAS_COMMA;
2226 				break;
2227 			default:
2228 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2229 			}
2230 			break;
2231 		case EOF:
2232 			done = 1;
2233 			/*FALLTHROUGH*/
2234 		case NEWLINE:
2235 			if (state == AL_ALIAS) {
2236 				if (make_mbind(drvbuf, major, NULL, bhash)
2237 				    != 0) {
2238 					cmn_err(CE_WARN, dupwarn, drvbuf);
2239 				}
2240 			} else if (state != AL_NEW) {
2241 				kobj_file_err(CE_WARN, file,
2242 				    "Missing alias for %s", drvbuf);
2243 			}
2244 
2245 			kobj_newline(file);
2246 			state = AL_NEW;
2247 			major = (major_t)-1;
2248 			break;
2249 		default:
2250 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2251 		}
2252 	}
2253 
2254 	kobj_close_file(file);
2255 }
2256 
2257 
2258 /*
2259  * It is called for parsing these files:
2260  * - /etc/path_to_inst
2261  * - /etc/name_to_major
2262  * - /etc/name_to_sysnum
2263  * A callback "int (*line_parser)(char *, int, char *, struct bind **)"
2264  * is invoked for each line of the file.
2265  * The callback can inhash the entry into a hashtable by supplying
2266  * a pre-allocated hashtable in "struct bind **hashtab".
2267  */
2268 int
2269 read_binding_file(char *bindfile, struct bind **hashtab,
2270     int (*line_parser)(char *, int, char *, struct bind **))
2271 {
2272 	enum {
2273 		B_NEW, B_NAME, B_VAL, B_BIND_NAME
2274 	} state;
2275 	struct _buf *file;
2276 	char tokbuf[MAXNAMELEN];
2277 	token_t token;
2278 	int maxnum = 0;
2279 	char *bind_name = NULL, *name = NULL, *bn = NULL;
2280 	u_longlong_t val;
2281 	int done = 0;
2282 
2283 	static char num_err[] = "Missing number on preceding line?";
2284 	static char dupwarn[] = "!The binding file entry \"%s %u\" conflicts "
2285 	    "with a previous entry";
2286 
2287 	if (hashtab != NULL) {
2288 		clear_binding_hash(hashtab);
2289 	}
2290 
2291 	if ((file = kobj_open_file(bindfile)) == (struct _buf *)-1)
2292 		panic("read_binding_file: %s file not found", bindfile);
2293 
2294 	state = B_NEW;
2295 
2296 	while (!done) {
2297 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2298 
2299 		switch (token) {
2300 		case POUND:
2301 			/*
2302 			 * Skip comments.
2303 			 */
2304 			kobj_find_eol(file);
2305 			break;
2306 		case NAME:
2307 		case STRING:
2308 			switch (state) {
2309 			case B_NEW:
2310 				/*
2311 				 * This case is for the first name and
2312 				 * possibly only name in an entry.
2313 				 */
2314 				ASSERT(name == NULL);
2315 				name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
2316 				(void) strcpy(name, tokbuf);
2317 				state = B_NAME;
2318 				break;
2319 			case B_VAL:
2320 				/*
2321 				 * This case is for a second name, which
2322 				 * would be the binding name if the first
2323 				 * name was actually a generic name.
2324 				 */
2325 				ASSERT(bind_name == NULL);
2326 				bind_name = kmem_alloc(strlen(tokbuf) + 1,
2327 				    KM_SLEEP);
2328 				(void) strcpy(bind_name, tokbuf);
2329 				state = B_BIND_NAME;
2330 				break;
2331 			default:
2332 				kobj_file_err(CE_WARN, file, num_err);
2333 			}
2334 			break;
2335 		case HEXVAL:
2336 		case DECVAL:
2337 			if (state != B_NAME) {
2338 				kobj_file_err(CE_WARN, file, "Missing name?");
2339 				state = B_NEW;
2340 				continue;
2341 			}
2342 			(void) kobj_getvalue(tokbuf, &val);
2343 			if (val > (u_longlong_t)INT_MAX) {
2344 				kobj_file_err(CE_WARN, file,
2345 				    "value %llu too large", val);
2346 				state = B_NEW;
2347 				continue;
2348 			}
2349 			state = B_VAL;
2350 			break;
2351 		case EOF:
2352 			done = 1;
2353 			/*FALLTHROUGH*/
2354 		case NEWLINE:
2355 			if ((state == B_BIND_NAME) || (state == B_VAL)) {
2356 				if (state == B_BIND_NAME)
2357 					bn = bind_name;
2358 				else
2359 					bn = NULL;
2360 
2361 				if (line_parser != NULL) {
2362 					if ((*line_parser)(name, (int)val, bn,
2363 					    hashtab) == 0)
2364 						maxnum = MAX((int)val, maxnum);
2365 					else
2366 						kobj_file_err(CE_WARN, file,
2367 						    dupwarn, name, (uint_t)val);
2368 				}
2369 			} else if (state != B_NEW)
2370 				kobj_file_err(CE_WARN, file, "Syntax error?");
2371 
2372 			if (name) {
2373 				kmem_free(name, strlen(name) + 1);
2374 				name = NULL;
2375 			}
2376 			if (bind_name) {
2377 				kmem_free(bind_name, strlen(bind_name) + 1);
2378 				bind_name = NULL;
2379 			}
2380 			state = B_NEW;
2381 			kobj_newline(file);
2382 			break;
2383 		default:
2384 			kobj_file_err(CE_WARN, file, "Missing name/number?");
2385 			break;
2386 		}
2387 	}
2388 
2389 	ASSERT(name == NULL);		/* any leaks? */
2390 	ASSERT(bind_name == NULL);
2391 
2392 	kobj_close_file(file);
2393 	return (maxnum);
2394 }
2395 
2396 /*
2397  * read_dacf_binding_file()
2398  * 	Read the /etc/dacf.conf file and build the dacf_rule_t database from it.
2399  *
2400  * The syntax of a line in the dacf.conf file is:
2401  *   dev-spec 	[module:]op-set	operation options 	[config-args];
2402  *
2403  * Where:
2404  *   	1. dev-spec is of the format: name="data"
2405  *   	2. operation is the operation that this rule matches. (i.e. pre-detach)
2406  *   	3. options is a comma delimited list of options (i.e. debug,foobar)
2407  *   	4. config-data is a whitespace delimited list of the format: name="data"
2408  */
2409 int
2410 read_dacf_binding_file(char *filename)
2411 {
2412 	enum {
2413 		DACF_BEGIN,
2414 		/* minor_nodetype="ddi_mouse:serial" */
2415 		DACF_NT_SPEC, DACF_NT_EQUALS, DACF_NT_DATA,
2416 		/* consconfig:mouseconfig */
2417 		DACF_MN_MODNAME, DACF_MN_COLON, DACF_MN_OPSET,
2418 		/* op */
2419 		DACF_OP_NAME,
2420 		/* [ option1, option2, option3... | - ] */
2421 		DACF_OPT_OPTION, DACF_OPT_COMMA, DACF_OPT_END,
2422 		/* argname1="argval1" argname2="argval2" ... */
2423 		DACF_OPARG_SPEC, DACF_OPARG_EQUALS, DACF_OPARG_DATA,
2424 		DACF_ERR, DACF_ERR_NEWLINE, DACF_COMMENT
2425 	} state = DACF_BEGIN;
2426 
2427 	struct _buf *file;
2428 	char *fname;
2429 	token_t token;
2430 
2431 	char tokbuf[MAXNAMELEN];
2432 	char mn_modname_buf[MAXNAMELEN], *mn_modnamep = NULL;
2433 	char mn_opset_buf[MAXNAMELEN], *mn_opsetp = NULL;
2434 	char nt_data_buf[MAXNAMELEN], *nt_datap = NULL;
2435 	char arg_spec_buf[MAXNAMELEN];
2436 
2437 	uint_t opts = 0;
2438 	dacf_devspec_t nt_spec_type = DACF_DS_ERROR;
2439 
2440 	dacf_arg_t *arg_list = NULL;
2441 	dacf_opid_t opid = DACF_OPID_ERROR;
2442 	int done = 0;
2443 
2444 	static char w_syntax[] = "'%s' unexpected";
2445 	static char w_equals[] = "'=' is illegal in the current context";
2446 	static char w_baddevspec[] = "device specification '%s' unrecognized";
2447 	static char w_badop[] = "operation '%s' unrecognized";
2448 	static char w_badopt[] = "option '%s' unrecognized, ignoring";
2449 	static char w_newline[] = "rule is incomplete";
2450 	static char w_insert[] = "failed to register rule";
2451 	static char w_comment[] = "'#' not allowed except at start of line";
2452 	static char w_dupargs[] =
2453 	    "argument '%s' duplicates a previous argument, skipping";
2454 	static char w_nt_empty[] = "empty device specification not allowed";
2455 
2456 	if (filename == NULL) {
2457 		fname = dacffile;	/* default binding file */
2458 	} else {
2459 		fname = filename;	/* user specified */
2460 	}
2461 
2462 	if ((file = kobj_open_file(fname)) == (struct _buf *)-1) {
2463 		return (ENOENT);
2464 	}
2465 
2466 	if (dacfdebug & DACF_DBG_MSGS) {
2467 		printf("dacf debug: clearing rules database\n");
2468 	}
2469 
2470 	mutex_enter(&dacf_lock);
2471 	dacf_clear_rules();
2472 
2473 	if (dacfdebug & DACF_DBG_MSGS) {
2474 		printf("dacf debug: parsing %s\n", fname);
2475 	}
2476 
2477 	while (!done) {
2478 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2479 
2480 		switch (token) {
2481 		case POUND:	/* comment line */
2482 			if (state != DACF_BEGIN) {
2483 				kobj_file_err(CE_WARN, file, w_comment);
2484 				state = DACF_ERR;
2485 				break;
2486 			}
2487 			state = DACF_COMMENT;
2488 			kobj_find_eol(file);
2489 			break;
2490 
2491 		case EQUALS:
2492 			switch (state) {
2493 			case DACF_NT_SPEC:
2494 				state = DACF_NT_EQUALS;
2495 				break;
2496 			case DACF_OPARG_SPEC:
2497 				state = DACF_OPARG_EQUALS;
2498 				break;
2499 			default:
2500 				kobj_file_err(CE_WARN, file, w_equals);
2501 				state = DACF_ERR;
2502 			}
2503 			break;
2504 
2505 		case NAME:
2506 			switch (state) {
2507 			case DACF_BEGIN:
2508 				nt_spec_type = dacf_get_devspec(tokbuf);
2509 				if (nt_spec_type == DACF_DS_ERROR) {
2510 					kobj_file_err(CE_WARN, file,
2511 					    w_baddevspec, tokbuf);
2512 					state = DACF_ERR;
2513 					break;
2514 				}
2515 				state = DACF_NT_SPEC;
2516 				break;
2517 			case DACF_NT_DATA:
2518 				(void) strncpy(mn_modname_buf, tokbuf,
2519 				    sizeof (mn_modname_buf));
2520 				mn_modnamep = mn_modname_buf;
2521 				state = DACF_MN_MODNAME;
2522 				break;
2523 			case DACF_MN_MODNAME:
2524 				/*
2525 				 * This handles the 'optional' modname.
2526 				 * What we thought was the modname is really
2527 				 * the op-set.  So it is copied over.
2528 				 */
2529 				ASSERT(mn_modnamep);
2530 				(void) strncpy(mn_opset_buf, mn_modnamep,
2531 				    sizeof (mn_opset_buf));
2532 				mn_opsetp = mn_opset_buf;
2533 				mn_modnamep = NULL;
2534 				/*
2535 				 * Now, the token we just read is the opset,
2536 				 * so look that up and fill in opid
2537 				 */
2538 				if ((opid = dacf_get_op(tokbuf)) ==
2539 				    DACF_OPID_ERROR) {
2540 					kobj_file_err(CE_WARN, file, w_badop,
2541 					    tokbuf);
2542 					state = DACF_ERR;
2543 					break;
2544 				}
2545 				state = DACF_OP_NAME;
2546 				break;
2547 			case DACF_MN_COLON:
2548 				(void) strncpy(mn_opset_buf, tokbuf,
2549 				    sizeof (mn_opset_buf));
2550 				mn_opsetp = mn_opset_buf;
2551 				state = DACF_MN_OPSET;
2552 				break;
2553 			case DACF_MN_OPSET:
2554 				if ((opid = dacf_get_op(tokbuf)) ==
2555 				    DACF_OPID_ERROR) {
2556 					kobj_file_err(CE_WARN, file, w_badop,
2557 					    tokbuf);
2558 					state = DACF_ERR;
2559 					break;
2560 				}
2561 				state = DACF_OP_NAME;
2562 				break;
2563 			case DACF_OP_NAME:
2564 				/*
2565 				 * This case is just like DACF_OPT_COMMA below,
2566 				 * but we check for the sole '-' argument
2567 				 */
2568 				if (strcmp(tokbuf, "-") == 0) {
2569 					state = DACF_OPT_END;
2570 					break;
2571 				}
2572 				/*FALLTHROUGH*/
2573 			case DACF_OPT_COMMA:
2574 				/*
2575 				 * figure out what option was given, but don't
2576 				 * make a federal case if invalid, just skip it
2577 				 */
2578 				if (dacf_getopt(tokbuf, &opts) != 0) {
2579 					kobj_file_err(CE_WARN, file, w_badopt,
2580 					    tokbuf);
2581 				}
2582 				state = DACF_OPT_OPTION;
2583 				break;
2584 			case DACF_OPT_END:
2585 			case DACF_OPT_OPTION:
2586 			case DACF_OPARG_DATA:
2587 				(void) strncpy(arg_spec_buf, tokbuf,
2588 				    sizeof (arg_spec_buf));
2589 				state = DACF_OPARG_SPEC;
2590 				break;
2591 			case DACF_OPARG_EQUALS:
2592 				/*
2593 				 * Add the arg.  Warn if it's a duplicate
2594 				 */
2595 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
2596 				    tokbuf) != 0) {
2597 					kobj_file_err(CE_WARN, file, w_dupargs,
2598 					    arg_spec_buf);
2599 				}
2600 				state = DACF_OPARG_DATA;
2601 				break;
2602 			default:
2603 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2604 				state = DACF_ERR;
2605 				break;
2606 			}
2607 			break;
2608 
2609 		case STRING:
2610 			/*
2611 			 * We need to check to see if the string has a \n in it.
2612 			 * If so, we had an unmatched " mark error, and lex has
2613 			 * already emitted an error for us, so we need to enter
2614 			 * the error state.  Stupid lex.
2615 			 */
2616 			if (strchr(tokbuf, '\n')) {
2617 				state = DACF_ERR;
2618 				break;
2619 			}
2620 			switch (state) {
2621 			case DACF_NT_EQUALS:
2622 				if (strlen(tokbuf) == 0) {
2623 					kobj_file_err(CE_WARN, file,
2624 					    w_nt_empty);
2625 					state = DACF_ERR;
2626 					break;
2627 				}
2628 				state = DACF_NT_DATA;
2629 				nt_datap = nt_data_buf;
2630 				(void) strncpy(nt_datap, tokbuf,
2631 				    sizeof (nt_data_buf));
2632 				break;
2633 			case DACF_OPARG_EQUALS:
2634 				/*
2635 				 * Add the arg.  Warn if it's a duplicate
2636 				 */
2637 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
2638 				    tokbuf) != 0) {
2639 					kobj_file_err(CE_WARN, file, w_dupargs,
2640 					    arg_spec_buf);
2641 				}
2642 				state = DACF_OPARG_DATA;
2643 				break;
2644 			default:
2645 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2646 				state = DACF_ERR;
2647 				break;
2648 			}
2649 			break;
2650 
2651 		case COMMA:
2652 			switch (state) {
2653 			case DACF_OPT_OPTION:
2654 				state = DACF_OPT_COMMA;
2655 				break;
2656 			default:
2657 				kobj_file_err(CE_WARN, file, w_syntax, ",");
2658 				state = DACF_ERR;
2659 				break;
2660 			}
2661 			break;
2662 
2663 		case COLON:
2664 			if (state == DACF_MN_MODNAME)
2665 				state = DACF_MN_COLON;
2666 			else {
2667 				kobj_file_err(CE_WARN, file, w_syntax, ":");
2668 				state = DACF_ERR;
2669 			}
2670 			break;
2671 
2672 		case EOF:
2673 			done = 1;
2674 			/*FALLTHROUGH*/
2675 		case NEWLINE:
2676 			if (state == DACF_COMMENT || state == DACF_BEGIN) {
2677 				state = DACF_BEGIN;
2678 				kobj_newline(file);
2679 				break;
2680 			}
2681 			if ((state != DACF_OPT_OPTION) &&
2682 			    (state != DACF_OPARG_DATA) &&
2683 			    (state != DACF_OPT_END)) {
2684 				kobj_file_err(CE_WARN, file, w_newline);
2685 				/*
2686 				 * We can't just do DACF_ERR here, since we'll
2687 				 * wind up eating the _next_ newline if so.
2688 				 */
2689 				state = DACF_ERR_NEWLINE;
2690 				kobj_newline(file);
2691 				break;
2692 			}
2693 
2694 			/*
2695 			 * insert the rule.
2696 			 */
2697 			if (dacf_rule_insert(nt_spec_type, nt_datap,
2698 			    mn_modnamep, mn_opsetp, opid, opts, arg_list) < 0) {
2699 				/*
2700 				 * We can't just do DACF_ERR here, since we'll
2701 				 * wind up eating the _next_ newline if so.
2702 				 */
2703 				kobj_file_err(CE_WARN, file, w_insert);
2704 				state = DACF_ERR_NEWLINE;
2705 				kobj_newline(file);
2706 				break;
2707 			}
2708 
2709 			state = DACF_BEGIN;
2710 			kobj_newline(file);
2711 			break;
2712 
2713 		default:
2714 			kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2715 			break;
2716 		} /* switch */
2717 
2718 		/*
2719 		 * Clean up after ourselves, either after a line has terminated
2720 		 * successfully or because of a syntax error; or when we reach
2721 		 * EOF (remember, we may reach EOF without being 'done' with
2722 		 * handling a particular line).
2723 		 */
2724 		if (state == DACF_ERR) {
2725 			kobj_find_eol(file);
2726 		}
2727 		if ((state == DACF_BEGIN) || (state == DACF_ERR) ||
2728 		    (state == DACF_ERR_NEWLINE) || done) {
2729 			nt_datap = NULL;
2730 			mn_modnamep = mn_opsetp = NULL;
2731 			opts = 0;
2732 			opid = DACF_OPID_ERROR;
2733 			nt_spec_type = DACF_DS_ERROR;
2734 			dacf_arglist_delete(&arg_list);
2735 			state = DACF_BEGIN;
2736 		}
2737 	} /* while */
2738 
2739 	if (dacfdebug & DACF_DBG_MSGS) {
2740 		printf("\ndacf debug: done!\n");
2741 	}
2742 
2743 	mutex_exit(&dacf_lock);
2744 
2745 	kobj_close_file(file);
2746 	return (0);
2747 }
2748 
2749 void
2750 lock_hw_class_list()
2751 {
2752 	mutex_enter(&hcl_lock);
2753 }
2754 
2755 void
2756 unlock_hw_class_list()
2757 {
2758 	mutex_exit(&hcl_lock);
2759 }
2760 
2761 void
2762 add_class(char *exporter, char *class)
2763 {
2764 	struct hwc_class *hcl;
2765 
2766 	/*
2767 	 * If exporter's major is not registered in /etc/name_to_major,
2768 	 * don't update hwc_class, but just return here.
2769 	 */
2770 	if (ddi_name_to_major(exporter) >= devcnt) {
2771 		cmn_err(CE_WARN, "No major number for driver %s"
2772 				" in class %s", exporter, class);
2773 		return;
2774 	}
2775 	hcl = kmem_zalloc(sizeof (struct hwc_class), KM_SLEEP);
2776 	hcl->class_exporter = kmem_alloc(strlen(exporter) + 1, KM_SLEEP);
2777 	hcl->class_name = kmem_alloc(strlen(class) + 1, KM_SLEEP);
2778 	(void) strcpy(hcl->class_exporter, exporter);
2779 	(void) strcpy(hcl->class_name, class);
2780 	lock_hw_class_list();
2781 	hcl->class_next = hcl_head;
2782 	hcl_head = hcl;
2783 	unlock_hw_class_list();
2784 }
2785 
2786 /*
2787  * Return the number of classes exported. If buf is not NULL, fill in
2788  * the array of the class names as well.
2789  *
2790  * Caller must hold hcl_lock to ensure the class list unmodified while
2791  * it is accessed. A typical caller will get a count first and then
2792  * allocate buf. The lock should be held by the caller.
2793  */
2794 int
2795 get_class(const char *exporter, char **buf)
2796 {
2797 	int n = 0;
2798 	struct hwc_class *hcl;
2799 
2800 	ASSERT(mutex_owned(&hcl_lock));
2801 	for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
2802 		if (strcmp(exporter, hcl->class_exporter) == 0) {
2803 			if (buf)
2804 				buf[n] = hcl->class_name;
2805 			++n;
2806 		}
2807 	}
2808 
2809 	return (n);
2810 }
2811 
2812 void
2813 read_class_file(void)
2814 {
2815 	struct _buf *file;
2816 	struct hwc_class *hcl, *hcl1;
2817 	char tokbuf[MAXNAMELEN];
2818 	enum {
2819 		C_BEGIN, C_EXPORTER, C_END
2820 	} state;
2821 	token_t token;
2822 	int done = 0;
2823 	char *exporter = NULL, *class = NULL, *name = NULL;
2824 
2825 	if (hcl_head != NULL) {
2826 		hcl = hcl_head;
2827 		while (hcl != NULL) {
2828 			kmem_free(hcl->class_exporter,
2829 			    strlen(hcl->class_exporter) + 1);
2830 			hcl1 = hcl;
2831 			hcl = hcl->class_next;
2832 			kmem_free(hcl1, sizeof (struct hwc_class));
2833 		}
2834 		hcl_head = NULL;
2835 	}
2836 
2837 	if ((file = kobj_open_file(class_file)) == (struct _buf *)-1)
2838 		return;
2839 
2840 	state = C_BEGIN;
2841 	while (!done) {
2842 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2843 
2844 		switch (token) {
2845 		case POUND:
2846 			/*
2847 			 * Skip comments.
2848 			 */
2849 			kobj_find_eol(file);
2850 			break;
2851 		case NAME:
2852 		case STRING:
2853 			name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
2854 			(void) strcpy(name, tokbuf);
2855 			switch (state) {
2856 			case C_BEGIN:
2857 				exporter = name;
2858 				state = C_EXPORTER;
2859 				break;
2860 			case C_EXPORTER:
2861 				class = name;
2862 				add_class(exporter, class);
2863 				state = C_END;
2864 				break;
2865 			case C_END:
2866 				kobj_file_err(CE_WARN, file,
2867 				    "Extra noise after entry");
2868 				kmem_free(name, strlen(name) + 1);
2869 				kobj_find_eol(file);
2870 				break;
2871 			} /* End Switch */
2872 			break;
2873 		case EOF:
2874 			done = 1;
2875 			/*FALLTHROUGH*/
2876 		case NEWLINE:
2877 			kobj_newline(file);
2878 			if (state == C_EXPORTER)
2879 				kobj_file_err(CE_WARN, file,
2880 				    "Partial entry ignored");
2881 			state = C_BEGIN;
2882 			if (exporter)
2883 				kmem_free(exporter, strlen(exporter) + 1);
2884 			if (class)
2885 				kmem_free(class, strlen(class) + 1);
2886 			exporter = NULL;
2887 			class = NULL;
2888 			break;
2889 		default:
2890 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2891 			break;
2892 		}
2893 	}
2894 	kobj_close_file(file);
2895 }
2896 
2897 /*
2898  * Given par_list, get a list of parent major number
2899  */
2900 int
2901 impl_parlist_to_major(struct par_list *pl, char parents[])
2902 {
2903 	struct hwc_spec *hwcp;
2904 	struct hwc_class *hcl;
2905 	major_t major;
2906 	int nmajor = 0;
2907 	extern int devcnt;
2908 
2909 	for (; pl != NULL; pl = pl->par_next) {
2910 		if ((pl->par_major < devcnt) && (parents[pl->par_major] == 0)) {
2911 			parents[pl->par_major] = 1;
2912 			nmajor++;
2913 			continue;
2914 		}
2915 
2916 		/* parent specs cannot be mapped to a driver */
2917 		if (pl->par_major != (major_t)-1)
2918 			continue;
2919 
2920 		/* class spec */
2921 		hwcp = pl->par_specs;
2922 		ASSERT(hwcp->hwc_class_name);
2923 		ASSERT(hwcp->hwc_parent_name == NULL);
2924 
2925 		for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
2926 			if (strcmp(hwcp->hwc_class_name, hcl->class_name) != 0)
2927 				continue;
2928 			major = ddi_name_to_major(hcl->class_exporter);
2929 			ASSERT(major != (major_t)-1);
2930 			if (parents[major] == 0) {
2931 				parents[major] = 1;
2932 				nmajor++;
2933 			}
2934 		}
2935 	}
2936 	return (nmajor);
2937 }
2938 
2939 /*
2940  * delete a parent list and all its hwc specs
2941  */
2942 void
2943 impl_delete_par_list(struct par_list *pl)
2944 {
2945 	struct par_list *saved_pl;
2946 	struct hwc_spec *hp, *hp1;
2947 
2948 	while (pl) {
2949 		hp = pl->par_specs;
2950 		while (hp) {
2951 			hp1 = hp;
2952 			hp = hp->hwc_next;
2953 			hwc_free(hp1);
2954 		}
2955 		saved_pl = pl;
2956 		pl = pl->par_next;
2957 		kmem_free(saved_pl, sizeof (*saved_pl));
2958 	}
2959 }
2960 
2961 #if defined(_PSM_MODULES)
2962 void
2963 open_mach_list(void)
2964 {
2965 	struct _buf *file;
2966 	char tokbuf[MAXNAMELEN];
2967 	token_t token;
2968 	struct psm_mach *machp;
2969 
2970 	if ((file = kobj_open_file(mach_file)) == (struct _buf *)-1)
2971 		return;
2972 
2973 	while ((token = kobj_lex(file, tokbuf, sizeof (tokbuf))) != EOF) {
2974 		switch (token) {
2975 		case POUND:
2976 			/*
2977 			 * Skip comments.
2978 			 */
2979 			kobj_find_eol(file);
2980 			break;
2981 		case NAME:
2982 		case STRING:
2983 			machp = kmem_alloc((sizeof (struct psm_mach) +
2984 			    strlen(tokbuf) + 1), KM_SLEEP);
2985 			machp->m_next = pmach_head;
2986 			machp->m_machname = (char *)(machp + 1);
2987 			(void) strcpy(machp->m_machname, tokbuf);
2988 			pmach_head = machp;
2989 			break;
2990 		case NEWLINE:
2991 			kobj_newline(file);
2992 			break;
2993 		default:
2994 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2995 			break;
2996 		}
2997 	}
2998 	kobj_close_file(file);
2999 }
3000 
3001 void *
3002 get_next_mach(void *handle, char *buf)
3003 {
3004 	struct psm_mach *machp;
3005 
3006 	machp = (struct psm_mach *)handle;
3007 	if (machp)
3008 		machp = machp->m_next;
3009 	else
3010 		machp = pmach_head;
3011 	if (machp)
3012 		(void) strcpy(buf, machp->m_machname);
3013 	return (machp);
3014 }
3015 
3016 void
3017 close_mach_list(void)
3018 {
3019 	struct psm_mach *machp;
3020 
3021 	while (pmach_head) {
3022 		machp = pmach_head;
3023 		pmach_head = machp->m_next;
3024 		kmem_free(machp, sizeof (struct psm_mach) +
3025 			strlen(machp->m_machname) + 1);
3026 	}
3027 }
3028 #endif	/* _PSM_MODULES */
3029 
3030 #if defined(_RTC_CONFIG)
3031 /*
3032  * Read in the 'zone_lag' value from the rtc configuration file,
3033  * and return the value to the caller.  Note that there is other information
3034  * in this file (zone_info), so we ignore unknown values.  We do spit out
3035  * warnings if the line doesn't begin with an identifier, or if we don't find
3036  * exactly "zone_lag=value".  No one should be editing this file by hand
3037  * (use the rtc command instead), but it's better to be careful.
3038  */
3039 long
3040 process_rtc_config_file(void)
3041 {
3042 	enum {
3043 		R_NEW, R_NAME, R_EQUALS, R_VALUE
3044 	} state;
3045 	struct _buf *file;
3046 	char tokbuf[MAXNAMELEN];
3047 	token_t token;
3048 	long zone_lag = 0;
3049 	u_longlong_t tmp;
3050 	int done = 0;
3051 
3052 	if ((file = kobj_open_file(rtc_config_file)) == (struct _buf *)-1)
3053 		return (0);
3054 
3055 	state = R_NEW;
3056 
3057 	while (!done) {
3058 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
3059 
3060 		switch (token) {
3061 		case POUND:
3062 			/*
3063 			 * Skip comments.
3064 			 */
3065 			kobj_find_eol(file);
3066 			break;
3067 		case NAME:
3068 		case STRING:
3069 			if (state == R_NEW) {
3070 				if (strcmp(tokbuf, "zone_lag") == 0)
3071 					state = R_NAME;
3072 				else
3073 					kobj_find_eol(file);   /* Ignore */
3074 			} else
3075 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3076 			break;
3077 		case EQUALS:
3078 			if (state == R_NAME)
3079 				state = R_EQUALS;
3080 			else
3081 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3082 			break;
3083 		case DECVAL:
3084 			if (state == R_EQUALS) {
3085 				if (kobj_getvalue(tokbuf, &tmp) != 0)
3086 					kobj_file_err(CE_WARN, file,
3087 					    "Bad value %s for zone_lag",
3088 					    tokbuf);
3089 				else
3090 					zone_lag = (long)tmp;
3091 				state = R_VALUE;
3092 			} else
3093 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3094 			break;
3095 		case EOF:
3096 			done = 1;
3097 			/*FALLTHROUGH*/
3098 		case NEWLINE:
3099 			if (state != R_NEW && state != R_VALUE)
3100 				kobj_file_err(CE_WARN, file,
3101 				    "Partial zone_lag entry ignored");
3102 			kobj_newline(file);
3103 			state = R_NEW;
3104 			break;
3105 		default:
3106 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3107 			break;
3108 		}
3109 	}
3110 	kobj_close_file(file);
3111 	return (zone_lag);
3112 }
3113 #endif /* _RTC_CONFIG */
3114 
3115 
3116 /*
3117  * Append node spec to the end of par_list
3118  */
3119 static void
3120 append(struct hwc_spec *spec, struct par_list *par)
3121 {
3122 	struct hwc_spec *hwc, *last;
3123 
3124 	ASSERT(par->par_specs);
3125 	for (hwc = par->par_specs; hwc; hwc = hwc->hwc_next)
3126 		last = hwc;
3127 	last->hwc_next = spec;
3128 }
3129 
3130 /*
3131  * Given a parent=/full-pathname, see if the platform
3132  * can resolve the pathname to driver, otherwise, try
3133  * the leaf node name.
3134  */
3135 static major_t
3136 get_major(char *parent)
3137 {
3138 	major_t major = (major_t)-1;
3139 	char *tmp, *driver = NULL;
3140 
3141 	if (*parent == '/')
3142 		major = path_to_major(parent);
3143 
3144 	if (major != (major_t)-1)
3145 		return (major);
3146 
3147 	/* extract the name between '/' and '@' */
3148 	if (*parent == '/')
3149 		driver = strrchr(parent, '/') + 1;
3150 	else
3151 		driver = parent;
3152 	if ((tmp = strchr(driver, '@')) != NULL)
3153 		*tmp = '\0';
3154 	major = ddi_name_to_major(driver);
3155 	if (tmp)
3156 		*tmp = '@';
3157 	return (major);
3158 }
3159 
3160 /*
3161  * Chain together specs whose parent's module name is the same.
3162  */
3163 static void
3164 add_spec(struct hwc_spec *spec, struct par_list **par)
3165 {
3166 	major_t maj;
3167 	struct par_list *pl, *par_last = NULL;
3168 	char *parent = spec->hwc_parent_name;
3169 
3170 	ASSERT(parent || spec->hwc_class_name);
3171 
3172 	/*
3173 	 * If given a parent=/full-pathname, see if the platform
3174 	 * can resolve the pathname to driver, otherwise, try
3175 	 * the leaf node name.
3176 	 *
3177 	 * If parent=/full-pathname doesn't resolve to a driver,
3178 	 * this could be cause by DR removal of the device.
3179 	 * We put it on the major=-2 list in case the device
3180 	 * is brought back into the system by DR.
3181 	 */
3182 	if (parent) {
3183 		maj = get_major(parent);
3184 		if (maj == (major_t)-1) {
3185 			if ((*parent == '/') &&
3186 			    (strncmp(parent, "/pseudo", 7) != 0)) {
3187 				maj = (major_t)-2;
3188 			} else {
3189 				cmn_err(CE_WARN,
3190 				    "add_spec: No major number for %s",
3191 				    parent);
3192 				hwc_free(spec);
3193 				return;
3194 			}
3195 		}
3196 	} else
3197 		maj = (major_t)-1;
3198 
3199 	/*
3200 	 * Scan the list looking for a matching parent.
3201 	 */
3202 	for (pl = *par; pl; pl = pl->par_next) {
3203 		if (maj == pl->par_major) {
3204 			append(spec, pl);
3205 			return;
3206 		}
3207 		par_last = pl;
3208 	}
3209 
3210 	/*
3211 	 * Didn't find a match on the list.  Make a new parent list.
3212 	 */
3213 	pl = kmem_zalloc(sizeof (*pl), KM_SLEEP);
3214 	pl->par_major = maj;
3215 	pl->par_specs = spec;
3216 	if (*par == NULL) {	/* null par list */
3217 		*par = pl;
3218 		return;
3219 	}
3220 	/* put "class=" entries last (lower pri if dups) */
3221 	if (maj == (major_t)-1) {
3222 		par_last->par_next = pl;
3223 		return;
3224 	}
3225 
3226 	/* ensure unresolved "parent=/full-path" goes first */
3227 	if ((maj != (major_t)-2) && ((*par)->par_major == (major_t)-2))
3228 		par = &(*par)->par_next;
3229 	pl->par_next = *par;
3230 	*par = pl;
3231 }
3232 
3233 /*
3234  * Add property spec to property list in original order
3235  */
3236 static void
3237 add_props(struct hwc_spec *spec, ddi_prop_t **props)
3238 {
3239 	ASSERT(spec->hwc_devi_name == NULL);
3240 
3241 	if (spec->hwc_devi_sys_prop_ptr) {
3242 		while (*props)
3243 			props = &(*props)->prop_next;
3244 		*props = spec->hwc_devi_sys_prop_ptr;
3245 
3246 		/* remove these properties from the spec */
3247 		spec->hwc_devi_sys_prop_ptr = NULL;
3248 	}
3249 	hwc_free(spec);
3250 }
3251