xref: /titanic_41/usr/src/uts/common/os/modsysfile.c (revision 5aefb6555731130ca4fd295960123d71f2d21fe8)
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 
827 	if (ask == 0)
828 		setparams();
829 }
830 
831 /*
832  * Search for a specific module variable assignment in /etc/system.  If
833  * successful, 1 is returned and the value is stored in '*value'.
834  * Otherwise 0 is returned and '*value' isn't modified.  If 'module' is
835  * NULL we look for global definitions.
836  *
837  * This is useful if the value of an assignment is needed before a
838  * module is loaded (e.g. to obtain a default privileged rctl limit).
839  */
840 int
841 mod_sysvar(const char *module, const char *name, u_longlong_t *value)
842 {
843 	struct sysparam	*sysp;
844 	int cnt = 0; /* dummy */
845 
846 	ASSERT(name != NULL);
847 	ASSERT(value != NULL);
848 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
849 
850 		if ((sysp->sys_type == MOD_SET) &&
851 		    (((module == NULL) && (sysp->sys_modnam == NULL)) ||
852 		    ((module != NULL) && (sysp->sys_modnam != NULL) &&
853 		    (strcmp(module, sysp->sys_modnam) == 0)))) {
854 
855 			ASSERT(sysp->sys_ptr != NULL);
856 
857 			if (strcmp(name, sysp->sys_ptr) == 0) {
858 				sysparam_count_entry(sysp, &cnt, value);
859 				if ((sysp->sys_flags & SYSPARAM_TERM) != 0)
860 					return (1);
861 				continue;
862 			}
863 		}
864 	}
865 	ASSERT(cnt == 0);
866 	return (0);
867 }
868 
869 /*
870  * This function scans sysparam records, which are created from the
871  * contents of /etc/system, for entries which are logical duplicates,
872  * and prints warning messages as appropriate.  When multiple "set"
873  * commands are encountered, the pileup of values with "&", "|"
874  * and "=" operators results in the final value.
875  */
876 static void
877 check_system_file(void)
878 {
879 	struct sysparam	*sysp;
880 
881 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
882 		struct sysparam *entry, *final;
883 		u_longlong_t value = 0;
884 		int cnt = 1;
885 		/*
886 		 * If the entry is already checked, skip it.
887 		 */
888 		if ((sysp->sys_flags & SYSPARAM_DUP) != 0)
889 			continue;
890 		/*
891 		 * Check if there is a duplicate entry by doing a linear
892 		 * search.
893 		 */
894 		final = sysp;
895 		for (entry = sysp->sys_next; entry != NULL;
896 		    entry = entry->sys_next) {
897 			/*
898 			 * Check the entry. if it's different, skip this.
899 			 */
900 			if (sysparam_compare_entry(sysp, entry) != 0)
901 				continue;
902 			/*
903 			 * Count the entry and put the mark.
904 			 */
905 			sysparam_count_entry(entry, &cnt, &value);
906 			entry->sys_flags |= SYSPARAM_DUP;
907 			final = entry;
908 		}
909 		final->sys_flags |= SYSPARAM_TERM;
910 		/*
911 		 * Print the warning if it's duplicated.
912 		 */
913 		if (cnt >= 2)
914 			sysparam_print_warning(final, value);
915 	}
916 }
917 
918 /*
919  * Compare the sysparam records.
920  * Return 0 if they are the same, return 1 if not.
921  */
922 static int
923 sysparam_compare_entry(struct sysparam *sysp, struct sysparam *entry)
924 {
925 	ASSERT(sysp->sys_ptr != NULL && entry->sys_ptr != NULL);
926 
927 	/*
928 	 * If the command is rootdev, rootfs, swapdev, swapfs or moddir,
929 	 * the record with the same type is treated as a duplicate record.
930 	 * In other cases, the record is treated as a duplicate record when
931 	 * its type, its module name (if it exists), and its variable name
932 	 * are the same.
933 	 */
934 	switch (sysp->sys_type) {
935 	case MOD_ROOTDEV:
936 	case MOD_ROOTFS:
937 	case MOD_SWAPDEV:
938 	case MOD_SWAPFS:
939 	case MOD_MODDIR:
940 		return (sysp->sys_type == entry->sys_type ? 0 : 1);
941 	default: /* In other cases, just go through it. */
942 		break;
943 	}
944 
945 	if (sysp->sys_type != entry->sys_type)
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 		return (1);
953 
954 	if (sysp->sys_modnam != NULL && entry->sys_modnam != NULL &&
955 	    strcmp(sysp->sys_modnam, entry->sys_modnam) != 0)
956 		return (1);
957 
958 	return (strcmp(sysp->sys_ptr, entry->sys_ptr));
959 }
960 
961 /*
962  * Translate a sysparam type value to a string.
963  */
964 static char *
965 sysparam_type_to_str(int type)
966 {
967 	struct modcmd *mcp;
968 
969 	for (mcp = modcmd; mcp->mc_cmdname != NULL; mcp++) {
970 		if (mcp->mc_type == type)
971 			break;
972 	}
973 	ASSERT(mcp->mc_type == type);
974 
975 	if (type != MOD_UNKNOWN)
976 		return ((++mcp)->mc_cmdname); /* lower case */
977 	else
978 		return ("");	/* MOD_UNKNOWN */
979 }
980 
981 /*
982  * Check the entry and accumulate the number of entries.
983  */
984 static void
985 sysparam_count_entry(struct sysparam *sysp, int *cnt, u_longlong_t *value)
986 {
987 	u_longlong_t ul = sysp->sys_info;
988 
989 	switch (sysp->sys_op) {
990 	case SETOP_ASSIGN:
991 		*value = ul;
992 		(*cnt)++;
993 		return;
994 	case SETOP_AND:
995 		*value &= ul;
996 		return;
997 	case SETOP_OR:
998 		*value |= ul;
999 		return;
1000 	default: /* Not MOD_SET */
1001 		(*cnt)++;
1002 		return;
1003 	}
1004 }
1005 
1006 /*
1007  * Print out the warning if multiple entries are found in the system file.
1008  */
1009 static void
1010 sysparam_print_warning(struct sysparam *sysp, u_longlong_t value)
1011 {
1012 	char *modnam = sysp->sys_modnam;
1013 	char *varnam = sysp->sys_ptr;
1014 	int type = sysp->sys_type;
1015 	char *typenam = sysparam_type_to_str(type);
1016 	boolean_t str_token = ((sysp->sys_flags & SYSPARAM_STR_TOKEN) != 0);
1017 	boolean_t hex_number = ((sysp->sys_flags & SYSPARAM_HEX_TOKEN) != 0);
1018 #define	warn_format1 " is set more than once in /%s. "
1019 #define	warn_format2 " applied as the current setting.\n"
1020 
1021 	ASSERT(varnam != NULL);
1022 
1023 	if (type == MOD_SET) {
1024 		/*
1025 		 * If a string token is set, print out the string
1026 		 * instead of its pointer value. In other cases,
1027 		 * print out the value with the appropriate format
1028 		 * for a hexadecimal number or a decimal number.
1029 		 */
1030 		if (modnam == NULL) {
1031 			if (str_token == B_TRUE) {
1032 				cmn_err(CE_WARN, "%s" warn_format1
1033 				    "\"%s %s = %s\"" warn_format2,
1034 				    varnam, systemfile, typenam,
1035 				    varnam, (char *)(uintptr_t)value);
1036 			} else if (hex_number == B_TRUE) {
1037 				cmn_err(CE_WARN, "%s" warn_format1
1038 				    "\"%s %s = 0x%llx\"" warn_format2,
1039 				    varnam, systemfile, typenam,
1040 				    varnam, value);
1041 			} else {
1042 				cmn_err(CE_WARN, "%s" warn_format1
1043 				    "\"%s %s = %lld\"" warn_format2,
1044 				    varnam, systemfile, typenam,
1045 				    varnam, value);
1046 			}
1047 		} else {
1048 			if (str_token == B_TRUE) {
1049 				cmn_err(CE_WARN, "%s:%s" warn_format1
1050 				    "\"%s %s:%s = %s\"" warn_format2,
1051 				    modnam, varnam, systemfile,
1052 				    typenam, modnam, varnam,
1053 				    (char *)(uintptr_t)value);
1054 			} else if (hex_number == B_TRUE) {
1055 				cmn_err(CE_WARN, "%s:%s" warn_format1
1056 				    "\"%s %s:%s = 0x%llx\"" warn_format2,
1057 				    modnam, varnam, systemfile,
1058 				    typenam, modnam, varnam, value);
1059 			} else {
1060 				cmn_err(CE_WARN, "%s:%s" warn_format1
1061 				    "\"%s %s:%s = %lld\"" warn_format2,
1062 				    modnam, varnam, systemfile,
1063 				    typenam, modnam, varnam, value);
1064 			}
1065 		}
1066 	} else {
1067 		/*
1068 		 * If the type is MOD_ROOTDEV, MOD_ROOTFS, MOD_SWAPDEV,
1069 		 * MOD_SWAPFS or MOD_MODDIR, the entry is treated as
1070 		 * a duplicate one if it has the same type regardless
1071 		 * of its variable name.
1072 		 */
1073 		switch (type) {
1074 		case MOD_ROOTDEV:
1075 		case MOD_ROOTFS:
1076 		case MOD_SWAPDEV:
1077 		case MOD_SWAPFS:
1078 		case MOD_MODDIR:
1079 			cmn_err(CE_WARN, "\"%s\" appears more than once "
1080 			    "in /%s.", typenam, systemfile);
1081 			break;
1082 		default:
1083 			cmn_err(CE_NOTE, "\"%s: %s\" appears more than once "
1084 			    "in /%s.", typenam, varnam, systemfile);
1085 			break;
1086 		}
1087 	}
1088 }
1089 
1090 /*
1091  * Process the system file commands.
1092  */
1093 int
1094 mod_sysctl(int fcn, void *p)
1095 {
1096 	static char wmesg[] = "forceload of %s failed";
1097 	struct sysparam *sysp;
1098 	char *name;
1099 	struct modctl *modp;
1100 
1101 	if (sysparam_hd == NULL)
1102 		return (0);
1103 
1104 	for (sysp = sysparam_hd; sysp != NULL; sysp = sysp->sys_next) {
1105 
1106 		switch (fcn) {
1107 
1108 		case SYS_FORCELOAD:
1109 		if (sysp->sys_type == MOD_FORCELOAD) {
1110 			name = sysp->sys_ptr;
1111 			if (modload(NULL, name) == -1)
1112 				cmn_err(CE_WARN, wmesg, name);
1113 			/*
1114 			 * The following works because it
1115 			 * runs before autounloading is started!!
1116 			 */
1117 			modp = mod_find_by_filename(NULL, name);
1118 			if (modp != NULL)
1119 				modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
1120 			/*
1121 			 * For drivers, attempt to install it.
1122 			 */
1123 			if (strncmp(sysp->sys_ptr, "drv", 3) == 0) {
1124 				(void) ddi_install_driver(name + 4);
1125 			}
1126 		}
1127 		break;
1128 
1129 		case SYS_SET_KVAR:
1130 		case SYS_SET_MVAR:
1131 			if (sysp->sys_type == MOD_SET)
1132 				sys_set_var(fcn, sysp, p);
1133 			break;
1134 
1135 		case SYS_CHECK_EXCLUDE:
1136 			if (sysp->sys_type == MOD_EXCLUDE) {
1137 				if (p == NULL || sysp->sys_ptr == NULL)
1138 					return (0);
1139 				if (strcmp((char *)p, sysp->sys_ptr) == 0)
1140 					return (1);
1141 			}
1142 		}
1143 	}
1144 	param_check();
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 	int		val_type;
1570 	int		val_size;
1571 	union {
1572 		char *string;
1573 		int integer;
1574 	} val;
1575 };
1576 
1577 static struct val_list *
1578 add_val(struct val_list **val_listp, struct val_list *tail,
1579     int val_type, caddr_t val)
1580 {
1581 	struct val_list *new_val, *listp = *val_listp;
1582 
1583 	new_val = kmem_alloc(sizeof (struct val_list), KM_SLEEP);
1584 	new_val->val_next = NULL;
1585 	if ((new_val->val_type = val_type) == 0) {
1586 		new_val->val_size = strlen((char *)val) + 1;
1587 		new_val->val.string = (char *)val;
1588 	} else {
1589 		new_val->val_size = sizeof (int);
1590 		new_val->val.integer = (int)(uintptr_t)val;
1591 	}
1592 
1593 	ASSERT((listp == NULL && tail == NULL) ||
1594 	    (listp != NULL && tail != NULL));
1595 
1596 	if (tail != NULL) {
1597 		ASSERT(tail->val_next == NULL);
1598 		tail->val_next = new_val;
1599 	} else {
1600 		*val_listp = new_val;
1601 	}
1602 
1603 	return (new_val);
1604 }
1605 
1606 /*
1607  * make sure there are no reserved IEEE 1275 characters (except
1608  * for uppercase characters).
1609  */
1610 static int
1611 valid_prop_name(char *name)
1612 {
1613 	int i;
1614 	int len = strlen(name);
1615 
1616 	for (i = 0; i < len; i++) {
1617 		if (name[i] < 0x21 ||
1618 			name[i] == '/' ||
1619 			name[i] == '\\' ||
1620 			name[i] == ':' ||
1621 			name[i] == '[' ||
1622 			name[i] == ']' ||
1623 			name[i] == '@')
1624 				return (0);
1625 	}
1626 	return (1);
1627 }
1628 
1629 static void
1630 make_prop(struct _buf *file, dev_info_t *devi, char *name, struct val_list *val)
1631 {
1632 	int propcnt = 0, val_type;
1633 	struct val_list *vl, *tvl;
1634 	caddr_t valbuf = NULL;
1635 	char **valsp;
1636 	int *valip;
1637 
1638 	if (name == NULL)
1639 		return;
1640 
1641 #ifdef DEBUG
1642 	parse_debug(NULL, "%s", name);
1643 #endif
1644 	if (!valid_prop_name(name)) {
1645 		cmn_err(CE_WARN, "invalid property name '%s'", name);
1646 		kmem_free(name, strlen(name) + 1);
1647 		return;
1648 	}
1649 	if (val) {
1650 		for (vl = val, val_type = vl->val_type; vl; vl = vl->val_next) {
1651 			if (val_type != vl->val_type) {
1652 				cmn_err(CE_WARN, "Mixed types in value list");
1653 				return;
1654 			}
1655 			propcnt++;
1656 		}
1657 
1658 		vl = val;
1659 
1660 		if (val_type == 1) {
1661 			valip = (int *)kmem_alloc(
1662 			    (propcnt * sizeof (int)), KM_SLEEP);
1663 			valbuf = (caddr_t)valip;
1664 			while (vl) {
1665 				tvl = vl;
1666 				vl = vl->val_next;
1667 #ifdef DEBUG
1668 				parse_debug(NULL, " %x",  tvl->val.integer);
1669 #endif
1670 				*valip = tvl->val.integer;
1671 				valip++;
1672 				kmem_free(tvl, sizeof (struct val_list));
1673 			}
1674 			/* restore valip */
1675 			valip = (int *)valbuf;
1676 
1677 			/* create the property */
1678 			if (e_ddi_prop_update_int_array(DDI_DEV_T_NONE, devi,
1679 			    name, valip, propcnt) != DDI_PROP_SUCCESS) {
1680 				kobj_file_err(CE_WARN, file,
1681 				    "cannot create property %s", name);
1682 			}
1683 			/* cleanup */
1684 			kmem_free(valip, (propcnt * sizeof (int)));
1685 		} else if (val_type == 0) {
1686 			valsp = (char **)kmem_alloc(
1687 			    ((propcnt + 1) * sizeof (char *)), KM_SLEEP);
1688 			valbuf = (caddr_t)valsp;
1689 			while (vl) {
1690 				tvl = vl;
1691 				vl = vl->val_next;
1692 #ifdef DEBUG
1693 				parse_debug(NULL, " %s", tvl->val.string);
1694 #endif
1695 				*valsp = tvl->val.string;
1696 				valsp++;
1697 			}
1698 			/* terminate array with NULL */
1699 			*valsp = NULL;
1700 
1701 			/* restore valsp */
1702 			valsp = (char **)valbuf;
1703 
1704 			/* create the property */
1705 			if (e_ddi_prop_update_string_array(DDI_DEV_T_NONE,
1706 			    devi, name, valsp, propcnt)
1707 			    != DDI_PROP_SUCCESS) {
1708 				kobj_file_err(CE_WARN, file,
1709 				    "cannot create property %s", name);
1710 			}
1711 			/* Clean up */
1712 			vl = val;
1713 			while (vl) {
1714 				tvl = vl;
1715 				vl = vl->val_next;
1716 				kmem_free(tvl->val.string, tvl->val_size);
1717 				kmem_free(tvl, sizeof (struct val_list));
1718 			}
1719 			kmem_free(valsp, ((propcnt + 1) * sizeof (char *)));
1720 		} else {
1721 			cmn_err(CE_WARN, "Invalid property type");
1722 			return;
1723 		}
1724 	} else {
1725 		/*
1726 		 * No value was passed in with property so we will assume
1727 		 * it is a "boolean" property and create an integer
1728 		 * property with 0 value.
1729 		 */
1730 #ifdef DEBUG
1731 		parse_debug(NULL, "\n");
1732 #endif
1733 		if (e_ddi_prop_update_int(DDI_DEV_T_NONE, devi, name, 0)
1734 		    != DDI_PROP_SUCCESS) {
1735 			kobj_file_err(CE_WARN, file,
1736 			    "cannot create property %s", name);
1737 		}
1738 	}
1739 	kmem_free(name, strlen(name) + 1);
1740 }
1741 
1742 static char omit_err[] = "(the ';' may have been omitted on previous spec!)";
1743 static char prnt_err[] = "'parent' property already specified";
1744 static char nm_err[] = "'name' property already specified";
1745 static char class_err[] = "'class' property already specified";
1746 
1747 typedef enum {
1748 	hwc_begin, parent, drvname, drvclass, prop,
1749 	parent_equals, name_equals, drvclass_equals,
1750 	parent_equals_string, name_equals_string,
1751 	drvclass_equals_string,
1752 	prop_equals, prop_equals_string, prop_equals_integer,
1753 	prop_equals_string_comma, prop_equals_integer_comma
1754 } hwc_state_t;
1755 
1756 static struct hwc_spec *
1757 get_hwc_spec(struct _buf *file, char *tokbuf, size_t linesize)
1758 {
1759 	char *prop_name, *string, *class_string;
1760 	token_t token;
1761 	struct hwc_spec *hwcp;
1762 	struct dev_info *devi;
1763 	struct val_list *val_list, *tail;
1764 	hwc_state_t state;
1765 	u_longlong_t ival;
1766 
1767 	hwcp = kmem_zalloc(sizeof (*hwcp), KM_SLEEP);
1768 	devi = kmem_zalloc(sizeof (*devi), KM_SLEEP);
1769 
1770 	state = hwc_begin;
1771 	token = NAME;
1772 	prop_name = NULL;
1773 	val_list = NULL;
1774 	tail = NULL;
1775 	string = NULL;
1776 	do {
1777 #ifdef DEBUG
1778 		parse_debug(NULL, "state 0x%x\n", state);
1779 #endif
1780 		switch (token) {
1781 		case NAME:
1782 			switch (state) {
1783 			case prop:
1784 			case prop_equals_string:
1785 			case prop_equals_integer:
1786 				make_prop(file, (dev_info_t *)devi,
1787 				    prop_name, val_list);
1788 				prop_name = NULL;
1789 				val_list = NULL;
1790 				tail = NULL;
1791 				/*FALLTHROUGH*/
1792 			case hwc_begin:
1793 				if (strcmp(tokbuf, "PARENT") == 0 ||
1794 				    strcmp(tokbuf, "parent") == 0) {
1795 					state = parent;
1796 				} else if (strcmp(tokbuf, "NAME") == 0 ||
1797 				    strcmp(tokbuf, "name") == 0) {
1798 					state = drvname;
1799 				} else if (strcmp(tokbuf, "CLASS") == 0 ||
1800 				    strcmp(tokbuf, "class") == 0) {
1801 					state = drvclass;
1802 					prop_name = kmem_alloc(strlen(tokbuf) +
1803 						1, KM_SLEEP);
1804 					(void) strcpy(prop_name, tokbuf);
1805 				} else {
1806 					state = prop;
1807 					prop_name = kmem_alloc(strlen(tokbuf) +
1808 						1, KM_SLEEP);
1809 					(void) strcpy(prop_name, tokbuf);
1810 				}
1811 				break;
1812 			default:
1813 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1814 			}
1815 			break;
1816 		case EQUALS:
1817 			switch (state) {
1818 			case drvname:
1819 				state = name_equals;
1820 				break;
1821 			case parent:
1822 				state = parent_equals;
1823 				break;
1824 			case drvclass:
1825 				state = drvclass_equals;
1826 				break;
1827 			case prop:
1828 				state = prop_equals;
1829 				break;
1830 			default:
1831 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1832 			}
1833 			break;
1834 		case STRING:
1835 			string = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
1836 			(void) strcpy(string, tokbuf);
1837 			switch (state) {
1838 			case name_equals:
1839 				if (ddi_get_name((dev_info_t *)devi)) {
1840 					kobj_file_err(CE_WARN, file, "%s %s",
1841 						nm_err, omit_err);
1842 					goto bad;
1843 				}
1844 				devi->devi_name = string;
1845 				string = NULL;
1846 				state = hwc_begin;
1847 				break;
1848 			case parent_equals:
1849 				if (hwcp->hwc_parent_name) {
1850 					kobj_file_err(CE_WARN, file, "%s %s",
1851 						prnt_err, omit_err);
1852 					goto bad;
1853 				}
1854 				hwcp->hwc_parent_name = string;
1855 				string = NULL;
1856 				state = hwc_begin;
1857 				break;
1858 			case drvclass_equals:
1859 				if (hwcp->hwc_class_name) {
1860 					kobj_file_err(CE_WARN, file, class_err);
1861 					goto bad;
1862 				}
1863 				class_string = kmem_alloc(strlen(string) + 1,
1864 					KM_SLEEP);
1865 				(void) strcpy(class_string, string);
1866 				hwcp->hwc_class_name = class_string;
1867 				/*FALLTHROUGH*/
1868 			case prop_equals:
1869 			case prop_equals_string_comma:
1870 				tail = add_val(&val_list, tail, 0, string);
1871 				string = NULL;
1872 				state = prop_equals_string;
1873 				break;
1874 			default:
1875 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1876 			}
1877 			break;
1878 		case HEXVAL:
1879 		case DECVAL:
1880 			switch (state) {
1881 			case prop_equals:
1882 			case prop_equals_integer_comma:
1883 				(void) kobj_getvalue(tokbuf, &ival);
1884 				tail = add_val(&val_list, tail,
1885 				    1, (caddr_t)(uintptr_t)ival);
1886 				state = prop_equals_integer;
1887 				break;
1888 			default:
1889 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1890 			}
1891 			break;
1892 		case COMMA:
1893 			switch (state) {
1894 			case prop_equals_string:
1895 				state = prop_equals_string_comma;
1896 				break;
1897 			case prop_equals_integer:
1898 				state = prop_equals_integer_comma;
1899 				break;
1900 			default:
1901 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1902 			}
1903 			break;
1904 		case NEWLINE:
1905 			kobj_newline(file);
1906 			break;
1907 		case POUND:
1908 			kobj_find_eol(file);
1909 			break;
1910 		case EOF:
1911 			kobj_file_err(CE_WARN, file, "Unexpected EOF");
1912 			goto bad;
1913 		default:
1914 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
1915 			goto bad;
1916 		}
1917 	} while ((token = kobj_lex(file, tokbuf, linesize)) != SEMICOLON);
1918 
1919 	switch (state) {
1920 	case prop:
1921 	case prop_equals_string:
1922 	case prop_equals_integer:
1923 		make_prop(file, (dev_info_t *)devi,
1924 			prop_name, val_list);
1925 		break;
1926 
1927 	case hwc_begin:
1928 		break;
1929 	default:
1930 		kobj_file_err(CE_WARN, file, "Unexpected end of line");
1931 		break;
1932 	}
1933 
1934 	/* copy 2 relevant members of devi to hwcp */
1935 	hwcp->hwc_devi_sys_prop_ptr = devi->devi_sys_prop_ptr;
1936 	hwcp->hwc_devi_name = devi->devi_name;
1937 
1938 	kmem_free(devi, sizeof (struct dev_info));
1939 
1940 	return (hwcp);
1941 
1942 bad:
1943 	if (string) {
1944 		kmem_free(string, strlen(string) + 1);
1945 	}
1946 	if (hwcp) {
1947 		hwc_free(hwcp);
1948 	}
1949 	if (devi) {
1950 		if (devi->devi_name)
1951 			kmem_free(devi->devi_name,
1952 			    strlen(devi->devi_name) + 1);
1953 		kmem_free(devi, sizeof (struct dev_info));
1954 	}
1955 	return (NULL);
1956 }
1957 
1958 /*
1959  * This is the primary kernel interface to parse driver.conf files.
1960  *
1961  * Yet another bigstk thread handoff due to deep kernel stacks when booting
1962  * cache-only-clients.
1963  */
1964 int
1965 hwc_parse(char *fname, struct par_list **pl, ddi_prop_t **props)
1966 {
1967 	int ret;
1968 	struct hwc_parse_mt *pltp = hwc_parse_mtalloc(fname, pl, props);
1969 
1970 	if (curthread != &t0) {
1971 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
1972 		    hwc_parse_thread, pltp, 0, &p0, TS_RUN, maxclsyspri);
1973 		sema_p(&pltp->sema);
1974 	} else {
1975 		pltp->rv = hwc_parse_now(fname, pl, props);
1976 	}
1977 	ret = pltp->rv;
1978 	hwc_parse_mtfree(pltp);
1979 	return (ret);
1980 }
1981 
1982 /*
1983  * Calls to hwc_parse() are handled off to this routine in a separate
1984  * thread.
1985  */
1986 static void
1987 hwc_parse_thread(struct hwc_parse_mt *pltp)
1988 {
1989 	kmutex_t	cpr_lk;
1990 	callb_cpr_t	cpr_i;
1991 
1992 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
1993 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "hwc_parse");
1994 
1995 	/*
1996 	 * load and parse the .conf file
1997 	 * return the hwc_spec list (if any) to the creator of this thread
1998 	 */
1999 	pltp->rv = hwc_parse_now(pltp->name, pltp->pl, pltp->props);
2000 	sema_v(&pltp->sema);
2001 	mutex_enter(&cpr_lk);
2002 	CALLB_CPR_EXIT(&cpr_i);
2003 	mutex_destroy(&cpr_lk);
2004 	thread_exit();
2005 }
2006 
2007 /*
2008  * allocate and initialize a hwc_parse thread control structure
2009  */
2010 static struct hwc_parse_mt *
2011 hwc_parse_mtalloc(char *name, struct par_list **pl, ddi_prop_t **props)
2012 {
2013 	struct hwc_parse_mt *pltp = kmem_zalloc(sizeof (*pltp), KM_SLEEP);
2014 
2015 	ASSERT(name != NULL);
2016 
2017 	pltp->name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
2018 	bcopy(name, pltp->name, strlen(name) + 1);
2019 	pltp->pl = pl;
2020 	pltp->props = props;
2021 
2022 	sema_init(&pltp->sema, 0, NULL, SEMA_DEFAULT, NULL);
2023 	return (pltp);
2024 }
2025 
2026 /*
2027  * free a hwc_parse thread control structure
2028  */
2029 static void
2030 hwc_parse_mtfree(struct hwc_parse_mt *pltp)
2031 {
2032 	sema_destroy(&pltp->sema);
2033 
2034 	kmem_free(pltp->name, strlen(pltp->name) + 1);
2035 	kmem_free(pltp, sizeof (*pltp));
2036 }
2037 
2038 /*
2039  * hwc_parse -- parse an hwconf file.  Ignore error lines and parse
2040  * as much as possible.
2041  */
2042 static int
2043 hwc_parse_now(char *fname, struct par_list **pl, ddi_prop_t **props)
2044 {
2045 	struct _buf *file;
2046 	struct hwc_spec *hwcp;
2047 	char *tokval;
2048 	token_t token;
2049 
2050 	/*
2051 	 * Don't use kobj_open_path's use_moddir_suffix option, we only
2052 	 * expect to find conf files in the base module directory, not
2053 	 * an ISA-specific subdirectory.
2054 	 */
2055 	if ((file = kobj_open_path(fname, 1, 0)) == (struct _buf *)-1) {
2056 		if (moddebug & MODDEBUG_ERRMSG)
2057 			cmn_err(CE_WARN, "Cannot open %s", fname);
2058 		return (-1);
2059 	}
2060 
2061 	/*
2062 	 * Initialize variables
2063 	 */
2064 	tokval = kmem_alloc(MAX_HWC_LINESIZE, KM_SLEEP);
2065 
2066 	while ((token = kobj_lex(file, tokval, MAX_HWC_LINESIZE)) != EOF) {
2067 		switch (token) {
2068 		case POUND:
2069 			/*
2070 			 * Skip comments.
2071 			 */
2072 			kobj_find_eol(file);
2073 			break;
2074 		case NAME:
2075 			hwcp = get_hwc_spec(file, tokval, MAX_HWC_LINESIZE);
2076 			if (hwcp == NULL)
2077 				break;
2078 			/*
2079 			 * No devi_name indicates global property.
2080 			 * Make sure parent and class not NULL.
2081 			 */
2082 			if (hwcp->hwc_devi_name == NULL) {
2083 				if (hwcp->hwc_parent_name ||
2084 				    hwcp->hwc_class_name) {
2085 					kobj_file_err(CE_WARN, file,
2086 					    "missing name attribute");
2087 					hwc_free(hwcp);
2088 					continue;
2089 				}
2090 				/* Add to global property list */
2091 				add_props(hwcp, props);
2092 				break;
2093 			}
2094 
2095 			/*
2096 			 * This is a node spec, either parent or class
2097 			 * must be specified.
2098 			 */
2099 			if ((hwcp->hwc_parent_name == NULL) &&
2100 			    (hwcp->hwc_class_name == NULL)) {
2101 				kobj_file_err(CE_WARN, file,
2102 				    "missing parent or class attribute");
2103 				hwc_free(hwcp);
2104 				continue;
2105 			}
2106 
2107 			/* add to node spec list */
2108 			add_spec(hwcp, pl);
2109 			break;
2110 		case NEWLINE:
2111 			kobj_newline(file);
2112 			break;
2113 		default:
2114 			kobj_file_err(CE_WARN, file, tok_err, tokval);
2115 			break;
2116 		}
2117 	}
2118 	/*
2119 	 * XXX - Check for clean termination.
2120 	 */
2121 	kmem_free(tokval, MAX_HWC_LINESIZE);
2122 	kobj_close_file(file);
2123 	return (0);	/* always return success */
2124 }
2125 
2126 void
2127 make_aliases(struct bind **bhash)
2128 {
2129 	enum {
2130 		AL_NEW, AL_DRVNAME, AL_DRVNAME_COMMA, AL_ALIAS, AL_ALIAS_COMMA
2131 	} state;
2132 
2133 	struct _buf *file;
2134 	char tokbuf[MAXNAMELEN];
2135 	char drvbuf[MAXNAMELEN];
2136 	token_t token;
2137 	major_t major;
2138 	int done = 0;
2139 	static char dupwarn[] = "!Driver alias \"%s\" conflicts with "
2140 	    "an existing driver name or alias.";
2141 
2142 	if ((file = kobj_open_file(dafile)) == (struct _buf *)-1)
2143 		return;
2144 
2145 	state = AL_NEW;
2146 	major = (major_t)-1;
2147 	while (!done) {
2148 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2149 		switch (token) {
2150 		case POUND:
2151 			state = AL_NEW;
2152 			kobj_find_eol(file);
2153 			break;
2154 		case NAME:
2155 		case STRING:
2156 			switch (state) {
2157 			case AL_NEW:
2158 				(void) strcpy(drvbuf, tokbuf);
2159 				state = AL_DRVNAME;
2160 				break;
2161 			case AL_DRVNAME_COMMA:
2162 				(void) strcat(drvbuf, tokbuf);
2163 				state = AL_DRVNAME;
2164 				break;
2165 			case AL_ALIAS_COMMA:
2166 				(void) strcat(drvbuf, tokbuf);
2167 				state = AL_ALIAS;
2168 				break;
2169 			case AL_DRVNAME:
2170 				major = mod_name_to_major(drvbuf);
2171 				if (major == (major_t)-1) {
2172 					kobj_find_eol(file);
2173 					state = AL_NEW;
2174 				} else {
2175 					(void) strcpy(drvbuf, tokbuf);
2176 					state = AL_ALIAS;
2177 				}
2178 				break;
2179 			case AL_ALIAS:
2180 				if (make_mbind(drvbuf, major, NULL, bhash)
2181 				    != 0) {
2182 					cmn_err(CE_WARN, dupwarn, drvbuf);
2183 				}
2184 				break;
2185 			}
2186 			break;
2187 		case COMMA:
2188 			(void) strcat(drvbuf, tokbuf);
2189 			switch (state) {
2190 			case AL_DRVNAME:
2191 				state = AL_DRVNAME_COMMA;
2192 				break;
2193 			case AL_ALIAS:
2194 				state = AL_ALIAS_COMMA;
2195 				break;
2196 			default:
2197 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2198 			}
2199 			break;
2200 		case EOF:
2201 			done = 1;
2202 			/*FALLTHROUGH*/
2203 		case NEWLINE:
2204 			if (state == AL_ALIAS) {
2205 				if (make_mbind(drvbuf, major, NULL, bhash)
2206 				    != 0) {
2207 					cmn_err(CE_WARN, dupwarn, drvbuf);
2208 				}
2209 			} else if (state != AL_NEW) {
2210 				kobj_file_err(CE_WARN, file,
2211 				    "Missing alias for %s", drvbuf);
2212 			}
2213 
2214 			kobj_newline(file);
2215 			state = AL_NEW;
2216 			major = (major_t)-1;
2217 			break;
2218 		default:
2219 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2220 		}
2221 	}
2222 
2223 	kobj_close_file(file);
2224 }
2225 
2226 
2227 /*
2228  * It is called for parsing these files:
2229  * - /etc/path_to_inst
2230  * - /etc/name_to_major
2231  * - /etc/name_to_sysnum
2232  * A callback "int (*line_parser)(char *, int, char *, struct bind **)"
2233  * is invoked for each line of the file.
2234  * The callback can inhash the entry into a hashtable by supplying
2235  * a pre-allocated hashtable in "struct bind **hashtab".
2236  */
2237 int
2238 read_binding_file(char *bindfile, struct bind **hashtab,
2239     int (*line_parser)(char *, int, char *, struct bind **))
2240 {
2241 	enum {
2242 		B_NEW, B_NAME, B_VAL, B_BIND_NAME
2243 	} state;
2244 	struct _buf *file;
2245 	char tokbuf[MAXNAMELEN];
2246 	token_t token;
2247 	int maxnum = 0;
2248 	char *bind_name = NULL, *name = NULL, *bn = NULL;
2249 	u_longlong_t val;
2250 	int done = 0;
2251 
2252 	static char num_err[] = "Missing number on preceding line?";
2253 	static char dupwarn[] = "!The binding file entry \"%s %u\" conflicts "
2254 	    "with a previous entry";
2255 
2256 	if (hashtab != NULL) {
2257 		clear_binding_hash(hashtab);
2258 	}
2259 
2260 	if ((file = kobj_open_file(bindfile)) == (struct _buf *)-1)
2261 		panic("read_binding_file: %s file not found", bindfile);
2262 
2263 	state = B_NEW;
2264 
2265 	while (!done) {
2266 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2267 
2268 		switch (token) {
2269 		case POUND:
2270 			state = B_NEW;
2271 			kobj_find_eol(file);
2272 			break;
2273 		case NAME:
2274 		case STRING:
2275 			switch (state) {
2276 			case B_NEW:
2277 				/*
2278 				 * This case is for the first name and
2279 				 * possibly only name in an entry.
2280 				 */
2281 				ASSERT(name == NULL);
2282 				name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
2283 				(void) strcpy(name, tokbuf);
2284 				state = B_NAME;
2285 				break;
2286 			case B_VAL:
2287 				/*
2288 				 * This case is for a second name, which
2289 				 * would be the binding name if the first
2290 				 * name was actually a generic name.
2291 				 */
2292 				ASSERT(bind_name == NULL);
2293 				bind_name = kmem_alloc(strlen(tokbuf) + 1,
2294 				    KM_SLEEP);
2295 				(void) strcpy(bind_name, tokbuf);
2296 				state = B_BIND_NAME;
2297 				break;
2298 			default:
2299 				kobj_file_err(CE_WARN, file, num_err);
2300 			}
2301 			break;
2302 		case HEXVAL:
2303 		case DECVAL:
2304 			if (state != B_NAME) {
2305 				kobj_file_err(CE_WARN, file, "Missing name?");
2306 				state = B_NEW;
2307 				continue;
2308 			}
2309 			(void) kobj_getvalue(tokbuf, &val);
2310 			if (val > (u_longlong_t)INT_MAX) {
2311 				kobj_file_err(CE_WARN, file,
2312 				    "value %llu too large", val);
2313 				state = B_NEW;
2314 				continue;
2315 			}
2316 			state = B_VAL;
2317 			break;
2318 		case EOF:
2319 			done = 1;
2320 			/*FALLTHROUGH*/
2321 		case NEWLINE:
2322 			if ((state == B_BIND_NAME) || (state == B_VAL)) {
2323 				if (state == B_BIND_NAME)
2324 					bn = bind_name;
2325 				else
2326 					bn = NULL;
2327 
2328 				if (line_parser != NULL) {
2329 					if ((*line_parser)(name, (int)val, bn,
2330 					    hashtab) == 0)
2331 						maxnum = MAX((int)val, maxnum);
2332 					else
2333 						kobj_file_err(CE_WARN, file,
2334 						    dupwarn, name, (uint_t)val);
2335 				}
2336 			} else if (state != B_NEW)
2337 				kobj_file_err(CE_WARN, file, "Syntax error?");
2338 
2339 			if (name) {
2340 				kmem_free(name, strlen(name) + 1);
2341 				name = NULL;
2342 			}
2343 			if (bind_name) {
2344 				kmem_free(bind_name, strlen(bind_name) + 1);
2345 				bind_name = NULL;
2346 			}
2347 			state = B_NEW;
2348 			kobj_newline(file);
2349 			break;
2350 		default:
2351 			kobj_file_err(CE_WARN, file, "Missing name/number?");
2352 			break;
2353 		}
2354 	}
2355 
2356 	ASSERT(name == NULL);		/* any leaks? */
2357 	ASSERT(bind_name == NULL);
2358 
2359 	kobj_close_file(file);
2360 	return (maxnum);
2361 }
2362 
2363 /*
2364  * read_dacf_binding_file()
2365  * 	Read the /etc/dacf.conf file and build the dacf_rule_t database from it.
2366  *
2367  * The syntax of a line in the dacf.conf file is:
2368  *   dev-spec 	[module:]op-set	operation options 	[config-args];
2369  *
2370  * Where:
2371  *   	1. dev-spec is of the format: name="data"
2372  *   	2. operation is the operation that this rule matches. (i.e. pre-detach)
2373  *   	3. options is a comma delimited list of options (i.e. debug,foobar)
2374  *   	4. config-data is a whitespace delimited list of the format: name="data"
2375  */
2376 int
2377 read_dacf_binding_file(char *filename)
2378 {
2379 	enum {
2380 		DACF_BEGIN,
2381 		/* minor_nodetype="ddi_mouse:serial" */
2382 		DACF_NT_SPEC, DACF_NT_EQUALS, DACF_NT_DATA,
2383 		/* consconfig:mouseconfig */
2384 		DACF_MN_MODNAME, DACF_MN_COLON, DACF_MN_OPSET,
2385 		/* op */
2386 		DACF_OP_NAME,
2387 		/* [ option1, option2, option3... | - ] */
2388 		DACF_OPT_OPTION, DACF_OPT_COMMA, DACF_OPT_END,
2389 		/* argname1="argval1" argname2="argval2" ... */
2390 		DACF_OPARG_SPEC, DACF_OPARG_EQUALS, DACF_OPARG_DATA,
2391 		DACF_ERR, DACF_ERR_NEWLINE, DACF_COMMENT
2392 	} state = DACF_BEGIN;
2393 
2394 	struct _buf *file;
2395 	char *fname;
2396 	token_t token;
2397 
2398 	char tokbuf[MAXNAMELEN];
2399 	char mn_modname_buf[MAXNAMELEN], *mn_modnamep = NULL;
2400 	char mn_opset_buf[MAXNAMELEN], *mn_opsetp = NULL;
2401 	char nt_data_buf[MAXNAMELEN], *nt_datap = NULL;
2402 	char arg_spec_buf[MAXNAMELEN];
2403 
2404 	uint_t opts = 0;
2405 	dacf_devspec_t nt_spec_type = DACF_DS_ERROR;
2406 
2407 	dacf_arg_t *arg_list = NULL;
2408 	dacf_opid_t opid = DACF_OPID_ERROR;
2409 	int done = 0;
2410 
2411 	static char w_syntax[] = "'%s' unexpected";
2412 	static char w_equals[] = "'=' is illegal in the current context";
2413 	static char w_baddevspec[] = "device specification '%s' unrecognized";
2414 	static char w_badop[] = "operation '%s' unrecognized";
2415 	static char w_badopt[] = "option '%s' unrecognized, ignoring";
2416 	static char w_newline[] = "rule is incomplete";
2417 	static char w_insert[] = "failed to register rule";
2418 	static char w_comment[] = "'#' not allowed except at start of line";
2419 	static char w_dupargs[] =
2420 	    "argument '%s' duplicates a previous argument, skipping";
2421 	static char w_nt_empty[] = "empty device specification not allowed";
2422 
2423 	if (filename == NULL) {
2424 		fname = dacffile;	/* default binding file */
2425 	} else {
2426 		fname = filename;	/* user specified */
2427 	}
2428 
2429 	if ((file = kobj_open_file(fname)) == (struct _buf *)-1) {
2430 		return (ENOENT);
2431 	}
2432 
2433 	if (dacfdebug & DACF_DBG_MSGS) {
2434 		printf("dacf debug: clearing rules database\n");
2435 	}
2436 
2437 	mutex_enter(&dacf_lock);
2438 	dacf_clear_rules();
2439 
2440 	if (dacfdebug & DACF_DBG_MSGS) {
2441 		printf("dacf debug: parsing %s\n", fname);
2442 	}
2443 
2444 	while (!done) {
2445 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2446 
2447 		switch (token) {
2448 		case POUND:	/* comment line */
2449 			if (state != DACF_BEGIN) {
2450 				kobj_file_err(CE_WARN, file, w_comment);
2451 				state = DACF_ERR;
2452 				break;
2453 			}
2454 			state = DACF_COMMENT;
2455 			kobj_find_eol(file);
2456 			break;
2457 
2458 		case EQUALS:
2459 			switch (state) {
2460 			case DACF_NT_SPEC:
2461 				state = DACF_NT_EQUALS;
2462 				break;
2463 			case DACF_OPARG_SPEC:
2464 				state = DACF_OPARG_EQUALS;
2465 				break;
2466 			default:
2467 				kobj_file_err(CE_WARN, file, w_equals);
2468 				state = DACF_ERR;
2469 			}
2470 			break;
2471 
2472 		case NAME:
2473 			switch (state) {
2474 			case DACF_BEGIN:
2475 				nt_spec_type = dacf_get_devspec(tokbuf);
2476 				if (nt_spec_type == DACF_DS_ERROR) {
2477 					kobj_file_err(CE_WARN, file,
2478 					    w_baddevspec, tokbuf);
2479 					state = DACF_ERR;
2480 					break;
2481 				}
2482 				state = DACF_NT_SPEC;
2483 				break;
2484 			case DACF_NT_DATA:
2485 				(void) strncpy(mn_modname_buf, tokbuf,
2486 				    sizeof (mn_modname_buf));
2487 				mn_modnamep = mn_modname_buf;
2488 				state = DACF_MN_MODNAME;
2489 				break;
2490 			case DACF_MN_MODNAME:
2491 				/*
2492 				 * This handles the 'optional' modname.
2493 				 * What we thought was the modname is really
2494 				 * the op-set.  So it is copied over.
2495 				 */
2496 				ASSERT(mn_modnamep);
2497 				(void) strncpy(mn_opset_buf, mn_modnamep,
2498 				    sizeof (mn_opset_buf));
2499 				mn_opsetp = mn_opset_buf;
2500 				mn_modnamep = NULL;
2501 				/*
2502 				 * Now, the token we just read is the opset,
2503 				 * so look that up and fill in opid
2504 				 */
2505 				if ((opid = dacf_get_op(tokbuf)) ==
2506 				    DACF_OPID_ERROR) {
2507 					kobj_file_err(CE_WARN, file, w_badop,
2508 					    tokbuf);
2509 					state = DACF_ERR;
2510 					break;
2511 				}
2512 				state = DACF_OP_NAME;
2513 				break;
2514 			case DACF_MN_COLON:
2515 				(void) strncpy(mn_opset_buf, tokbuf,
2516 				    sizeof (mn_opset_buf));
2517 				mn_opsetp = mn_opset_buf;
2518 				state = DACF_MN_OPSET;
2519 				break;
2520 			case DACF_MN_OPSET:
2521 				if ((opid = dacf_get_op(tokbuf)) ==
2522 				    DACF_OPID_ERROR) {
2523 					kobj_file_err(CE_WARN, file, w_badop,
2524 					    tokbuf);
2525 					state = DACF_ERR;
2526 					break;
2527 				}
2528 				state = DACF_OP_NAME;
2529 				break;
2530 			case DACF_OP_NAME:
2531 				/*
2532 				 * This case is just like DACF_OPT_COMMA below,
2533 				 * but we check for the sole '-' argument
2534 				 */
2535 				if (strcmp(tokbuf, "-") == 0) {
2536 					state = DACF_OPT_END;
2537 					break;
2538 				}
2539 				/*FALLTHROUGH*/
2540 			case DACF_OPT_COMMA:
2541 				/*
2542 				 * figure out what option was given, but don't
2543 				 * make a federal case if invalid, just skip it
2544 				 */
2545 				if (dacf_getopt(tokbuf, &opts) != 0) {
2546 					kobj_file_err(CE_WARN, file, w_badopt,
2547 					    tokbuf);
2548 				}
2549 				state = DACF_OPT_OPTION;
2550 				break;
2551 			case DACF_OPT_END:
2552 			case DACF_OPT_OPTION:
2553 			case DACF_OPARG_DATA:
2554 				(void) strncpy(arg_spec_buf, tokbuf,
2555 				    sizeof (arg_spec_buf));
2556 				state = DACF_OPARG_SPEC;
2557 				break;
2558 			case DACF_OPARG_EQUALS:
2559 				/*
2560 				 * Add the arg.  Warn if it's a duplicate
2561 				 */
2562 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
2563 				    tokbuf) != 0) {
2564 					kobj_file_err(CE_WARN, file, w_dupargs,
2565 					    arg_spec_buf);
2566 				}
2567 				state = DACF_OPARG_DATA;
2568 				break;
2569 			default:
2570 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2571 				state = DACF_ERR;
2572 				break;
2573 			}
2574 			break;
2575 
2576 		case STRING:
2577 			/*
2578 			 * We need to check to see if the string has a \n in it.
2579 			 * If so, we had an unmatched " mark error, and lex has
2580 			 * already emitted an error for us, so we need to enter
2581 			 * the error state.  Stupid lex.
2582 			 */
2583 			if (strchr(tokbuf, '\n')) {
2584 				state = DACF_ERR;
2585 				break;
2586 			}
2587 			switch (state) {
2588 			case DACF_NT_EQUALS:
2589 				if (strlen(tokbuf) == 0) {
2590 					kobj_file_err(CE_WARN, file,
2591 					    w_nt_empty);
2592 					state = DACF_ERR;
2593 					break;
2594 				}
2595 				state = DACF_NT_DATA;
2596 				nt_datap = nt_data_buf;
2597 				(void) strncpy(nt_datap, tokbuf,
2598 				    sizeof (nt_data_buf));
2599 				break;
2600 			case DACF_OPARG_EQUALS:
2601 				/*
2602 				 * Add the arg.  Warn if it's a duplicate
2603 				 */
2604 				if (dacf_arg_insert(&arg_list, arg_spec_buf,
2605 				    tokbuf) != 0) {
2606 					kobj_file_err(CE_WARN, file, w_dupargs,
2607 					    arg_spec_buf);
2608 				}
2609 				state = DACF_OPARG_DATA;
2610 				break;
2611 			default:
2612 				kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2613 				state = DACF_ERR;
2614 				break;
2615 			}
2616 			break;
2617 
2618 		case COMMA:
2619 			switch (state) {
2620 			case DACF_OPT_OPTION:
2621 				state = DACF_OPT_COMMA;
2622 				break;
2623 			default:
2624 				kobj_file_err(CE_WARN, file, w_syntax, ",");
2625 				state = DACF_ERR;
2626 				break;
2627 			}
2628 			break;
2629 
2630 		case COLON:
2631 			if (state == DACF_MN_MODNAME)
2632 				state = DACF_MN_COLON;
2633 			else {
2634 				kobj_file_err(CE_WARN, file, w_syntax, ":");
2635 				state = DACF_ERR;
2636 			}
2637 			break;
2638 
2639 		case EOF:
2640 			done = 1;
2641 			/*FALLTHROUGH*/
2642 		case NEWLINE:
2643 			if (state == DACF_COMMENT || state == DACF_BEGIN) {
2644 				state = DACF_BEGIN;
2645 				kobj_newline(file);
2646 				break;
2647 			}
2648 			if ((state != DACF_OPT_OPTION) &&
2649 			    (state != DACF_OPARG_DATA) &&
2650 			    (state != DACF_OPT_END)) {
2651 				kobj_file_err(CE_WARN, file, w_newline);
2652 				/*
2653 				 * We can't just do DACF_ERR here, since we'll
2654 				 * wind up eating the _next_ newline if so.
2655 				 */
2656 				state = DACF_ERR_NEWLINE;
2657 				kobj_newline(file);
2658 				break;
2659 			}
2660 
2661 			/*
2662 			 * insert the rule.
2663 			 */
2664 			if (dacf_rule_insert(nt_spec_type, nt_datap,
2665 			    mn_modnamep, mn_opsetp, opid, opts, arg_list) < 0) {
2666 				/*
2667 				 * We can't just do DACF_ERR here, since we'll
2668 				 * wind up eating the _next_ newline if so.
2669 				 */
2670 				kobj_file_err(CE_WARN, file, w_insert);
2671 				state = DACF_ERR_NEWLINE;
2672 				kobj_newline(file);
2673 				break;
2674 			}
2675 
2676 			state = DACF_BEGIN;
2677 			kobj_newline(file);
2678 			break;
2679 
2680 		default:
2681 			kobj_file_err(CE_WARN, file, w_syntax, tokbuf);
2682 			break;
2683 		} /* switch */
2684 
2685 		/*
2686 		 * Clean up after ourselves, either after a line has terminated
2687 		 * successfully or because of a syntax error; or when we reach
2688 		 * EOF (remember, we may reach EOF without being 'done' with
2689 		 * handling a particular line).
2690 		 */
2691 		if (state == DACF_ERR) {
2692 			kobj_find_eol(file);
2693 		}
2694 		if ((state == DACF_BEGIN) || (state == DACF_ERR) ||
2695 		    (state == DACF_ERR_NEWLINE) || done) {
2696 			nt_datap = NULL;
2697 			mn_modnamep = mn_opsetp = NULL;
2698 			opts = 0;
2699 			opid = DACF_OPID_ERROR;
2700 			nt_spec_type = DACF_DS_ERROR;
2701 			dacf_arglist_delete(&arg_list);
2702 			state = DACF_BEGIN;
2703 		}
2704 	} /* while */
2705 
2706 	if (dacfdebug & DACF_DBG_MSGS) {
2707 		printf("\ndacf debug: done!\n");
2708 	}
2709 
2710 	mutex_exit(&dacf_lock);
2711 
2712 	kobj_close_file(file);
2713 	return (0);
2714 }
2715 
2716 void
2717 lock_hw_class_list()
2718 {
2719 	mutex_enter(&hcl_lock);
2720 }
2721 
2722 void
2723 unlock_hw_class_list()
2724 {
2725 	mutex_exit(&hcl_lock);
2726 }
2727 
2728 void
2729 add_class(char *exporter, char *class)
2730 {
2731 	struct hwc_class *hcl;
2732 
2733 	/*
2734 	 * If exporter's major is not registered in /etc/name_to_major,
2735 	 * don't update hwc_class, but just return here.
2736 	 */
2737 	if (ddi_name_to_major(exporter) >= devcnt) {
2738 		cmn_err(CE_WARN, "No major number for driver %s"
2739 				" in class %s", exporter, class);
2740 		return;
2741 	}
2742 	hcl = kmem_zalloc(sizeof (struct hwc_class), KM_SLEEP);
2743 	hcl->class_exporter = kmem_alloc(strlen(exporter) + 1, KM_SLEEP);
2744 	hcl->class_name = kmem_alloc(strlen(class) + 1, KM_SLEEP);
2745 	(void) strcpy(hcl->class_exporter, exporter);
2746 	(void) strcpy(hcl->class_name, class);
2747 	lock_hw_class_list();
2748 	hcl->class_next = hcl_head;
2749 	hcl_head = hcl;
2750 	unlock_hw_class_list();
2751 }
2752 
2753 /*
2754  * Return the number of classes exported. If buf is not NULL, fill in
2755  * the array of the class names as well.
2756  *
2757  * Caller must hold hcl_lock to ensure the class list unmodified while
2758  * it is accessed. A typical caller will get a count first and then
2759  * allocate buf. The lock should be held by the caller.
2760  */
2761 int
2762 get_class(const char *exporter, char **buf)
2763 {
2764 	int n = 0;
2765 	struct hwc_class *hcl;
2766 
2767 	ASSERT(mutex_owned(&hcl_lock));
2768 	for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
2769 		if (strcmp(exporter, hcl->class_exporter) == 0) {
2770 			if (buf)
2771 				buf[n] = hcl->class_name;
2772 			++n;
2773 		}
2774 	}
2775 
2776 	return (n);
2777 }
2778 
2779 void
2780 read_class_file(void)
2781 {
2782 	struct _buf *file;
2783 	struct hwc_class *hcl, *hcl1;
2784 	char tokbuf[MAXNAMELEN];
2785 	enum {
2786 		C_BEGIN, C_EXPORTER, C_END
2787 	} state;
2788 	token_t token;
2789 	int done = 0;
2790 	char *exporter = NULL, *class = NULL, *name = NULL;
2791 
2792 	if (hcl_head != NULL) {
2793 		hcl = hcl_head;
2794 		while (hcl != NULL) {
2795 			kmem_free(hcl->class_exporter,
2796 			    strlen(hcl->class_exporter) + 1);
2797 			hcl1 = hcl;
2798 			hcl = hcl->class_next;
2799 			kmem_free(hcl1, sizeof (struct hwc_class));
2800 		}
2801 		hcl_head = NULL;
2802 	}
2803 
2804 	if ((file = kobj_open_file(class_file)) == (struct _buf *)-1)
2805 		return;
2806 
2807 	state = C_BEGIN;
2808 	while (!done) {
2809 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
2810 
2811 		switch (token) {
2812 		case POUND:
2813 			kobj_find_eol(file);
2814 			break;
2815 		case NAME:
2816 		case STRING:
2817 			name = kmem_alloc(strlen(tokbuf) + 1, KM_SLEEP);
2818 			(void) strcpy(name, tokbuf);
2819 			switch (state) {
2820 			case C_BEGIN:
2821 				exporter = name;
2822 				state = C_EXPORTER;
2823 				break;
2824 			case C_EXPORTER:
2825 				class = name;
2826 				add_class(exporter, class);
2827 				state = C_END;
2828 				break;
2829 			case C_END:
2830 				kobj_file_err(CE_WARN, file,
2831 				    "Extra noise after entry");
2832 				kmem_free(name, strlen(name) + 1);
2833 				kobj_find_eol(file);
2834 				break;
2835 			} /* End Switch */
2836 			break;
2837 		case EOF:
2838 			done = 1;
2839 			/*FALLTHROUGH*/
2840 		case NEWLINE:
2841 			kobj_newline(file);
2842 			if (state == C_EXPORTER)
2843 				kobj_file_err(CE_WARN, file,
2844 				    "Partial entry ignored");
2845 			state = C_BEGIN;
2846 			if (exporter)
2847 				kmem_free(exporter, strlen(exporter) + 1);
2848 			if (class)
2849 				kmem_free(class, strlen(class) + 1);
2850 			exporter = NULL;
2851 			class = NULL;
2852 			break;
2853 		default:
2854 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2855 			break;
2856 		}
2857 	}
2858 	kobj_close_file(file);
2859 }
2860 
2861 /*
2862  * Given par_list, get a list of parent major number
2863  */
2864 int
2865 impl_parlist_to_major(struct par_list *pl, char parents[])
2866 {
2867 	struct hwc_spec *hwcp;
2868 	struct hwc_class *hcl;
2869 	major_t major;
2870 	int nmajor = 0;
2871 	extern int devcnt;
2872 
2873 	for (; pl != NULL; pl = pl->par_next) {
2874 		if ((pl->par_major < devcnt) && (parents[pl->par_major] == 0)) {
2875 			parents[pl->par_major] = 1;
2876 			nmajor++;
2877 			continue;
2878 		}
2879 
2880 		/* parent specs cannot be mapped to a driver */
2881 		if (pl->par_major != (major_t)-1)
2882 			continue;
2883 
2884 		/* class spec */
2885 		hwcp = pl->par_specs;
2886 		ASSERT(hwcp->hwc_class_name);
2887 		ASSERT(hwcp->hwc_parent_name == NULL);
2888 
2889 		for (hcl = hcl_head; hcl != NULL; hcl = hcl->class_next) {
2890 			if (strcmp(hwcp->hwc_class_name, hcl->class_name) != 0)
2891 				continue;
2892 			major = ddi_name_to_major(hcl->class_exporter);
2893 			ASSERT(major != (major_t)-1);
2894 			if (parents[major] == 0) {
2895 				parents[major] = 1;
2896 				nmajor++;
2897 			}
2898 		}
2899 	}
2900 	return (nmajor);
2901 }
2902 
2903 /*
2904  * delete a parent list and all its hwc specs
2905  */
2906 void
2907 impl_delete_par_list(struct par_list *pl)
2908 {
2909 	struct par_list *saved_pl;
2910 	struct hwc_spec *hp, *hp1;
2911 
2912 	while (pl) {
2913 		hp = pl->par_specs;
2914 		while (hp) {
2915 			hp1 = hp;
2916 			hp = hp->hwc_next;
2917 			hwc_free(hp1);
2918 		}
2919 		saved_pl = pl;
2920 		pl = pl->par_next;
2921 		kmem_free(saved_pl, sizeof (*saved_pl));
2922 	}
2923 }
2924 
2925 #if defined(_PSM_MODULES)
2926 void
2927 open_mach_list(void)
2928 {
2929 	struct _buf *file;
2930 	char tokbuf[MAXNAMELEN];
2931 	token_t token;
2932 	struct psm_mach *machp;
2933 
2934 	if ((file = kobj_open_file(mach_file)) == (struct _buf *)-1)
2935 		return;
2936 
2937 	while ((token = kobj_lex(file, tokbuf, sizeof (tokbuf))) != EOF) {
2938 		switch (token) {
2939 		case POUND:
2940 			kobj_find_eol(file);
2941 			break;
2942 		case NAME:
2943 		case STRING:
2944 			machp = kmem_alloc((sizeof (struct psm_mach) +
2945 			    strlen(tokbuf) + 1), KM_SLEEP);
2946 			machp->m_next = pmach_head;
2947 			machp->m_machname = (char *)(machp + 1);
2948 			(void) strcpy(machp->m_machname, tokbuf);
2949 			pmach_head = machp;
2950 			break;
2951 		case NEWLINE:
2952 			kobj_newline(file);
2953 			break;
2954 		default:
2955 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
2956 			break;
2957 		}
2958 	}
2959 	kobj_close_file(file);
2960 }
2961 
2962 void *
2963 get_next_mach(void *handle, char *buf)
2964 {
2965 	struct psm_mach *machp;
2966 
2967 	machp = (struct psm_mach *)handle;
2968 	if (machp)
2969 		machp = machp->m_next;
2970 	else
2971 		machp = pmach_head;
2972 	if (machp)
2973 		(void) strcpy(buf, machp->m_machname);
2974 	return (machp);
2975 }
2976 
2977 void
2978 close_mach_list(void)
2979 {
2980 	struct psm_mach *machp;
2981 
2982 	while (pmach_head) {
2983 		machp = pmach_head;
2984 		pmach_head = machp->m_next;
2985 		kmem_free(machp, sizeof (struct psm_mach) +
2986 			strlen(machp->m_machname) + 1);
2987 	}
2988 }
2989 #endif	/* _PSM_MODULES */
2990 
2991 #if defined(_RTC_CONFIG)
2992 /*
2993  * Read in the 'zone_lag' value from the rtc configuration file,
2994  * and return the value to the caller.  Note that there is other information
2995  * in this file (zone_info), so we ignore unknown values.  We do spit out
2996  * warnings if the line doesn't begin with an identifier, or if we don't find
2997  * exactly "zone_lag=value".  No one should be editing this file by hand
2998  * (use the rtc command instead), but it's better to be careful.
2999  */
3000 long
3001 process_rtc_config_file(void)
3002 {
3003 	enum {
3004 		R_NEW, R_NAME, R_EQUALS, R_VALUE
3005 	} state;
3006 	struct _buf *file;
3007 	char tokbuf[MAXNAMELEN];
3008 	token_t token;
3009 	long zone_lag = 0;
3010 	u_longlong_t tmp;
3011 	int done = 0;
3012 
3013 	if ((file = kobj_open_file(rtc_config_file)) == (struct _buf *)-1)
3014 		return (0);
3015 
3016 	state = R_NEW;
3017 
3018 	while (!done) {
3019 		token = kobj_lex(file, tokbuf, sizeof (tokbuf));
3020 
3021 		switch (token) {
3022 		case POUND:
3023 			kobj_find_eol(file);
3024 			break;
3025 		case NAME:
3026 		case STRING:
3027 			if (state == R_NEW) {
3028 				if (strcmp(tokbuf, "zone_lag") == 0)
3029 					state = R_NAME;
3030 				else
3031 					kobj_find_eol(file);   /* Ignore */
3032 			} else
3033 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3034 			break;
3035 		case EQUALS:
3036 			if (state == R_NAME)
3037 				state = R_EQUALS;
3038 			else
3039 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3040 			break;
3041 		case DECVAL:
3042 			if (state == R_EQUALS) {
3043 				if (kobj_getvalue(tokbuf, &tmp) != 0)
3044 					kobj_file_err(CE_WARN, file,
3045 					    "Bad value %s for zone_lag",
3046 					    tokbuf);
3047 				else
3048 					zone_lag = (long)tmp;
3049 				state = R_VALUE;
3050 			} else
3051 				kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3052 			break;
3053 		case EOF:
3054 			done = 1;
3055 			/*FALLTHROUGH*/
3056 		case NEWLINE:
3057 			if (state != R_NEW && state != R_VALUE)
3058 				kobj_file_err(CE_WARN, file,
3059 				    "Partial zone_lag entry ignored");
3060 			kobj_newline(file);
3061 			state = R_NEW;
3062 			break;
3063 		default:
3064 			kobj_file_err(CE_WARN, file, tok_err, tokbuf);
3065 			break;
3066 		}
3067 	}
3068 	kobj_close_file(file);
3069 	return (zone_lag);
3070 }
3071 #endif /* _RTC_CONFIG */
3072 
3073 
3074 /*
3075  * Append node spec to the end of par_list
3076  */
3077 static void
3078 append(struct hwc_spec *spec, struct par_list *par)
3079 {
3080 	struct hwc_spec *hwc, *last;
3081 
3082 	ASSERT(par->par_specs);
3083 	for (hwc = par->par_specs; hwc; hwc = hwc->hwc_next)
3084 		last = hwc;
3085 	last->hwc_next = spec;
3086 }
3087 
3088 /*
3089  * Given a parent=/full-pathname, see if the platform
3090  * can resolve the pathname to driver, otherwise, try
3091  * the leaf node name.
3092  */
3093 static major_t
3094 get_major(char *parent)
3095 {
3096 	major_t major = (major_t)-1;
3097 	char *tmp, *driver = NULL;
3098 
3099 	if (*parent == '/')
3100 		major = path_to_major(parent);
3101 
3102 	if (major != (major_t)-1)
3103 		return (major);
3104 
3105 	/* extract the name between '/' and '@' */
3106 	if (*parent == '/')
3107 		driver = strrchr(parent, '/') + 1;
3108 	else
3109 		driver = parent;
3110 	if ((tmp = strchr(driver, '@')) != NULL)
3111 		*tmp = '\0';
3112 	major = ddi_name_to_major(driver);
3113 	if (tmp)
3114 		*tmp = '@';
3115 	return (major);
3116 }
3117 
3118 /*
3119  * Chain together specs whose parent's module name is the same.
3120  */
3121 static void
3122 add_spec(struct hwc_spec *spec, struct par_list **par)
3123 {
3124 	major_t maj;
3125 	struct par_list *pl, *par_last = NULL;
3126 	char *parent = spec->hwc_parent_name;
3127 
3128 	ASSERT(parent || spec->hwc_class_name);
3129 
3130 	/*
3131 	 * If 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 	 * If parent=/full-pathname doesn't resolve to a driver,
3136 	 * this could be cause by DR removal of the device.
3137 	 * We put it on the major=-2 list in case the device
3138 	 * is brought back into the system by DR.
3139 	 */
3140 	if (parent) {
3141 		maj = get_major(parent);
3142 		if (maj == (major_t)-1) {
3143 			if ((*parent == '/') &&
3144 			    (strncmp(parent, "/pseudo", 7) != 0)) {
3145 				maj = (major_t)-2;
3146 			} else {
3147 				cmn_err(CE_WARN,
3148 				    "add_spec: No major number for %s",
3149 				    parent);
3150 				hwc_free(spec);
3151 				return;
3152 			}
3153 		}
3154 	} else
3155 		maj = (major_t)-1;
3156 
3157 	/*
3158 	 * Scan the list looking for a matching parent.
3159 	 */
3160 	for (pl = *par; pl; pl = pl->par_next) {
3161 		if (maj == pl->par_major) {
3162 			append(spec, pl);
3163 			return;
3164 		}
3165 		par_last = pl;
3166 	}
3167 
3168 	/*
3169 	 * Didn't find a match on the list.  Make a new parent list.
3170 	 */
3171 	pl = kmem_zalloc(sizeof (*pl), KM_SLEEP);
3172 	pl->par_major = maj;
3173 	pl->par_specs = spec;
3174 	if (*par == NULL) {	/* null par list */
3175 		*par = pl;
3176 		return;
3177 	}
3178 	/* put "class=" entries last (lower pri if dups) */
3179 	if (maj == (major_t)-1) {
3180 		par_last->par_next = pl;
3181 		return;
3182 	}
3183 
3184 	/* ensure unresolved "parent=/full-path" goes first */
3185 	if ((maj != (major_t)-2) && ((*par)->par_major == (major_t)-2))
3186 		par = &(*par)->par_next;
3187 	pl->par_next = *par;
3188 	*par = pl;
3189 }
3190 
3191 /*
3192  * Add property spec to property list in original order
3193  */
3194 static void
3195 add_props(struct hwc_spec *spec, ddi_prop_t **props)
3196 {
3197 	ASSERT(spec->hwc_devi_name == NULL);
3198 
3199 	if (spec->hwc_devi_sys_prop_ptr) {
3200 		while (*props)
3201 			props = &(*props)->prop_next;
3202 		*props = spec->hwc_devi_sys_prop_ptr;
3203 
3204 		/* remove these properties from the spec */
3205 		spec->hwc_devi_sys_prop_ptr = NULL;
3206 	}
3207 	hwc_free(spec);
3208 }
3209