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