xref: /titanic_41/usr/src/cmd/prtconf/pdevinfo.c (revision f127cb91970601c2695eead0ee127f1cac48bb7a)
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 2007 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 /*
29  * For machines that support the openprom, fetch and print the list
30  * of devices that the kernel has fetched from the prom or conjured up.
31  */
32 
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <ctype.h>
38 #include <strings.h>
39 #include <unistd.h>
40 #include <stropts.h>
41 #include <sys/types.h>
42 #include <sys/mkdev.h>
43 #include <sys/sunddi.h>
44 #include <sys/openpromio.h>
45 #include <sys/modctl.h>
46 #include <sys/stat.h>
47 #include <zone.h>
48 #include <libnvpair.h>
49 #include "prtconf.h"
50 
51 
52 typedef char *(*dump_propname_t)(void *);
53 typedef int (*dump_proptype_t)(void *);
54 typedef int (*dump_propints_t)(void *, int **);
55 typedef int (*dump_propint64_t)(void *, int64_t **);
56 typedef int (*dump_propstrings_t)(void *, char **);
57 typedef int (*dump_propbytes_t)(void *, uchar_t **);
58 typedef int (*dump_proprawdata_t)(void *, uchar_t **);
59 
60 typedef struct dumpops_common {
61 	dump_propname_t doc_propname;
62 	dump_proptype_t doc_proptype;
63 	dump_propints_t doc_propints;
64 	dump_propint64_t doc_propint64;
65 	dump_propstrings_t doc_propstrings;
66 	dump_propbytes_t doc_propbytes;
67 	dump_proprawdata_t doc_proprawdata;
68 } dumpops_common_t;
69 
70 static const dumpops_common_t prop_dumpops = {
71 	(dump_propname_t)di_prop_name,
72 	(dump_proptype_t)di_prop_type,
73 	(dump_propints_t)di_prop_ints,
74 	(dump_propint64_t)di_prop_int64,
75 	(dump_propstrings_t)di_prop_strings,
76 	(dump_propbytes_t)di_prop_bytes,
77 	(dump_proprawdata_t)di_prop_rawdata
78 }, pathprop_common_dumpops = {
79 	(dump_propname_t)di_path_prop_name,
80 	(dump_proptype_t)di_path_prop_type,
81 	(dump_propints_t)di_path_prop_ints,
82 	(dump_propint64_t)di_path_prop_int64s,
83 	(dump_propstrings_t)di_path_prop_strings,
84 	(dump_propbytes_t)di_path_prop_bytes,
85 	(dump_proprawdata_t)di_path_prop_bytes
86 };
87 
88 typedef void *(*dump_nextprop_t)(void *, void *);
89 typedef dev_t (*dump_propdevt_t)(void *);
90 
91 typedef struct dumpops {
92 	const dumpops_common_t *dop_common;
93 	dump_nextprop_t dop_nextprop;
94 	dump_propdevt_t dop_propdevt;
95 } dumpops_t;
96 
97 static const dumpops_t sysprop_dumpops = {
98 	&prop_dumpops,
99 	(dump_nextprop_t)di_prop_sys_next,
100 	NULL
101 }, globprop_dumpops = {
102 	&prop_dumpops,
103 	(dump_nextprop_t)di_prop_global_next,
104 	NULL
105 }, drvprop_dumpops = {
106 	&prop_dumpops,
107 	(dump_nextprop_t)di_prop_drv_next,
108 	(dump_propdevt_t)di_prop_devt
109 }, hwprop_dumpops = {
110 	&prop_dumpops,
111 	(dump_nextprop_t)di_prop_hw_next,
112 	NULL
113 }, pathprop_dumpops = {
114 	&pathprop_common_dumpops,
115 	(dump_nextprop_t)di_path_prop_next,
116 	NULL
117 };
118 
119 #define	PROPNAME(ops) (ops->dop_common->doc_propname)
120 #define	PROPTYPE(ops) (ops->dop_common->doc_proptype)
121 #define	PROPINTS(ops) (ops->dop_common->doc_propints)
122 #define	PROPINT64(ops) (ops->dop_common->doc_propint64)
123 #define	PROPSTRINGS(ops) (ops->dop_common->doc_propstrings)
124 #define	PROPBYTES(ops) (ops->dop_common->doc_propbytes)
125 #define	PROPRAWDATA(ops) (ops->dop_common->doc_proprawdata)
126 #define	NEXTPROP(ops) (ops->dop_nextprop)
127 #define	PROPDEVT(ops) (ops->dop_propdevt)
128 #define	NUM_ELEMENTS(A) (sizeof (A) / sizeof (A[0]))
129 
130 static int prop_type_guess(const dumpops_t *, void *, void **, int *);
131 static void dump_prop_list_common(const dumpops_t *, int, void *);
132 static void walk_driver(di_node_t, di_devlink_handle_t);
133 static int dump_devs(di_node_t, void *);
134 static int dump_prop_list(const dumpops_t *, const char *, int, di_node_t);
135 static int _error(const char *, ...);
136 static int is_openprom();
137 static void walk(uchar_t *, uint_t, int);
138 static void dump_node(nvlist_t *, int);
139 static void dump_prodinfo(di_prom_handle_t, di_node_t, const char **,
140 				char *, int);
141 static di_node_t find_node_by_name(di_prom_handle_t, di_node_t, char *);
142 static int get_propval_by_name(di_prom_handle_t, di_node_t,
143 				const char *, uchar_t **);
144 static void dump_pathing_data(int, di_node_t);
145 static void dump_minor_data(int, di_node_t, di_devlink_handle_t);
146 static void dump_link_data(int, di_node_t, di_devlink_handle_t);
147 static int print_composite_string(const char *, char *, int);
148 static void print_one(nvpair_t *, int);
149 static int unprintable(char *, int);
150 static int promopen(int);
151 static void promclose();
152 static di_node_t find_target_node(di_node_t);
153 static void node_display_set(di_node_t);
154 
155 void
156 prtconf_devinfo(void)
157 {
158 	struct di_priv_data	fetch;
159 	di_devlink_handle_t	devlink_hdl = NULL;
160 	di_node_t		root_node;
161 	uint_t			flag;
162 	char			*rootpath;
163 
164 	dprintf("verbosemode %s\n", opts.o_verbose ? "on" : "off");
165 
166 	/* determine what info we need to get from kernel */
167 	flag = DINFOSUBTREE;
168 	rootpath = "/";
169 
170 	if (opts.o_target) {
171 		flag |= (DINFOMINOR | DINFOPATH);
172 	}
173 
174 	if (opts.o_forcecache) {
175 		if (dbg.d_forceload) {
176 			exit(_error(NULL, "option combination not supported"));
177 		}
178 		if (strcmp(rootpath, "/") != 0) {
179 			exit(_error(NULL, "invalid root path for option"));
180 		}
181 		flag = DINFOCACHE;
182 	} else if (opts.o_verbose) {
183 		flag |= (DINFOPROP | DINFOMINOR |
184 		    DINFOPRIVDATA | DINFOPATH | DINFOLYR);
185 	}
186 
187 	if (dbg.d_forceload) {
188 		flag |= DINFOFORCE;
189 	}
190 
191 	if (opts.o_verbose) {
192 		init_priv_data(&fetch);
193 		root_node = di_init_impl(rootpath, flag, &fetch);
194 
195 		/* get devlink (aka aliases) data */
196 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL)
197 			exit(_error("di_devlink_init() failed."));
198 	} else
199 		root_node = di_init(rootpath, flag);
200 
201 	if (root_node == DI_NODE_NIL) {
202 		(void) _error(NULL, "devinfo facility not available");
203 		/* not an error if this isn't the global zone */
204 		if (getzoneid() == GLOBAL_ZONEID)
205 			exit(-1);
206 		else
207 			exit(0);
208 	}
209 
210 	/*
211 	 * ...and walk all nodes to report them out...
212 	 */
213 	if (dbg.d_bydriver) {
214 		opts.o_target = 0;
215 		walk_driver(root_node, devlink_hdl);
216 		if (devlink_hdl != NULL)
217 			(void) di_devlink_fini(&devlink_hdl);
218 		di_fini(root_node);
219 		return;
220 	}
221 
222 	if (opts.o_target) {
223 		di_node_t target_node, node;
224 
225 		target_node = find_target_node(root_node);
226 		if (target_node == DI_NODE_NIL) {
227 			(void) fprintf(stderr, "%s: "
228 			    "invalid device path specified\n",
229 			    opts.o_progname);
230 			exit(1);
231 		}
232 
233 		/* mark the target node so we display it */
234 		node_display_set(target_node);
235 
236 		if (opts.o_ancestors) {
237 			/*
238 			 * mark the ancestors of this node so we display
239 			 * them as well
240 			 */
241 			node = target_node;
242 			while (node = di_parent_node(node))
243 				node_display_set(node);
244 		} else {
245 			/*
246 			 * when we display device tree nodes the indentation
247 			 * level is based off of tree depth.
248 			 *
249 			 * here we increment o_target to reflect the
250 			 * depth of the target node in the tree.  we do
251 			 * this so that when we calculate the indentation
252 			 * level we can subtract o_target so that the
253 			 * target node starts with an indentation of zero.
254 			 */
255 			node = target_node;
256 			while (node = di_parent_node(node))
257 				opts.o_target++;
258 		}
259 
260 		if (opts.o_children) {
261 			/*
262 			 * mark the children of this node so we display
263 			 * them as well
264 			 */
265 			(void) di_walk_node(target_node, DI_WALK_CLDFIRST,
266 			    (void *)1,
267 			    (int (*)(di_node_t, void *))
268 			    node_display_set);
269 		}
270 	}
271 
272 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, devlink_hdl,
273 	    dump_devs);
274 
275 	if (devlink_hdl != NULL)
276 		(void) di_devlink_fini(&devlink_hdl);
277 	di_fini(root_node);
278 }
279 
280 /*
281  * utility routines
282  */
283 static int
284 i_find_target_node(di_node_t node, void *arg)
285 {
286 	di_node_t *target = (di_node_t *)arg;
287 
288 	if (opts.o_devices_path != NULL) {
289 		char *path;
290 
291 		if ((path = di_devfs_path(node)) == NULL)
292 			exit(_error("failed to allocate memory"));
293 
294 		if (strcmp(opts.o_devices_path, path) == 0) {
295 			di_devfs_path_free(path);
296 			*target = node;
297 			return (DI_WALK_TERMINATE);
298 		}
299 
300 		di_devfs_path_free(path);
301 	} else if (opts.o_devt != DDI_DEV_T_NONE) {
302 		di_minor_t	minor = DI_MINOR_NIL;
303 
304 		while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
305 			if (opts.o_devt == di_minor_devt(minor)) {
306 				*target = node;
307 				return (DI_WALK_TERMINATE);
308 			}
309 		}
310 	} else {
311 		/* we should never get here */
312 		exit(_error(NULL, "internal error"));
313 	}
314 	return (DI_WALK_CONTINUE);
315 }
316 
317 static di_node_t
318 find_target_node(di_node_t root_node)
319 {
320 	di_node_t target = DI_NODE_NIL;
321 
322 	/* special case to allow displaying of the root node */
323 	if (opts.o_devices_path != NULL) {
324 		if (strlen(opts.o_devices_path) == 0)
325 			return (root_node);
326 		if (strcmp(opts.o_devices_path, ".") == 0)
327 			return (root_node);
328 	}
329 
330 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, &target,
331 	    i_find_target_node);
332 	return (target);
333 }
334 
335 #define	NODE_DISPLAY		(1<<0)
336 
337 static long
338 node_display(di_node_t node)
339 {
340 	long data = (long)di_node_private_get(node);
341 	return (data & NODE_DISPLAY);
342 }
343 
344 static void
345 node_display_set(di_node_t node)
346 {
347 	long data = (long)di_node_private_get(node);
348 	data |= NODE_DISPLAY;
349 	di_node_private_set(node, (void *)data);
350 }
351 
352 #define	LNODE_DISPLAYED		(1<<0)
353 
354 static long
355 lnode_displayed(di_lnode_t lnode)
356 {
357 	long data = (long)di_lnode_private_get(lnode);
358 	return (data & LNODE_DISPLAYED);
359 }
360 
361 static void
362 lnode_displayed_set(di_lnode_t lnode)
363 {
364 	long data = (long)di_lnode_private_get(lnode);
365 	data |= LNODE_DISPLAYED;
366 	di_lnode_private_set(lnode, (void *)data);
367 }
368 
369 static void
370 lnode_displayed_clear(di_lnode_t lnode)
371 {
372 	long data = (long)di_lnode_private_get(lnode);
373 	data &= ~LNODE_DISPLAYED;
374 	di_lnode_private_set(lnode, (void *)data);
375 }
376 
377 #define	MINOR_DISPLAYED		(1<<0)
378 #define	MINOR_PTR		(~(0x3))
379 
380 static long
381 minor_displayed(di_minor_t minor)
382 {
383 	long data = (long)di_minor_private_get(minor);
384 	return (data & MINOR_DISPLAYED);
385 }
386 
387 static void
388 minor_displayed_set(di_minor_t minor)
389 {
390 	long data = (long)di_minor_private_get(minor);
391 	data |= MINOR_DISPLAYED;
392 	di_minor_private_set(minor, (void *)data);
393 }
394 
395 static void
396 minor_displayed_clear(di_minor_t minor)
397 {
398 	long data = (long)di_minor_private_get(minor);
399 	data &= ~MINOR_DISPLAYED;
400 	di_minor_private_set(minor, (void *)data);
401 }
402 
403 static void *
404 minor_ptr(di_minor_t minor)
405 {
406 	long data = (long)di_minor_private_get(minor);
407 	return ((void *)(data & MINOR_PTR));
408 }
409 
410 static void
411 minor_ptr_set(di_minor_t minor, void *ptr)
412 {
413 	long data = (long)di_minor_private_get(minor);
414 	data = (data & ~MINOR_PTR) | (((long)ptr) & MINOR_PTR);
415 	di_minor_private_set(minor, (void *)data);
416 }
417 
418 
419 /*
420  * In this comment typed properties are those of type DI_PROP_TYPE_UNDEF_IT,
421  * DI_PROP_TYPE_BOOLEAN, DI_PROP_TYPE_INT, DI_PROP_TYPE_INT64,
422  * DI_PROP_TYPE_BYTE, and DI_PROP_TYPE_STRING.
423  *
424  * The guessing algorithm is:
425  * 1. If the property is typed and the type is consistent with the value of
426  *    the property, then the property is of that type. If the type is not
427  *    consistent with value of the property, then the type is treated as
428  *    alien to prtconf.
429  * 2. If the property is of type DI_PROP_TYPE_UNKNOWN the following steps
430  *    are carried out.
431  *    a. If the value of the property is consistent with a string property,
432  *       the type of the property is DI_PROP_TYPE_STRING.
433  *    b. Otherwise, if the value of the property is consistent with an integer
434  *       property, the type of the property is DI_PROP_TYPE_INT.
435  *    c. Otherwise, the property type is treated as alien to prtconf.
436  * 3. If the property type is alien to prtconf, then the property value is
437  *    read by the appropriate routine for untyped properties and the following
438  *    steps are carried out.
439  *    a. If the length that the property routine returned is zero, the
440  *       property is of type DI_PROP_TYPE_BOOLEAN.
441  *    b. Otherwise, if the length that the property routine returned is
442  *       positive, then the property value is treated as raw data of type
443  *       DI_PROP_TYPE_UNKNOWN.
444  *    c. Otherwise, if the length that the property routine returned is
445  *       negative, then there is some internal inconsistency and this is
446  *       treated as an error and no type is determined.
447  */
448 static int
449 prop_type_guess(const dumpops_t *propops, void *prop, void **prop_data,
450     int *prop_type)
451 {
452 	int len, type;
453 
454 	type = PROPTYPE(propops)(prop);
455 	switch (type) {
456 	case DI_PROP_TYPE_UNDEF_IT:
457 	case DI_PROP_TYPE_BOOLEAN:
458 		*prop_data = NULL;
459 		*prop_type = type;
460 		return (0);
461 	case DI_PROP_TYPE_INT:
462 		len = PROPINTS(propops)(prop, (int **)prop_data);
463 		break;
464 	case DI_PROP_TYPE_INT64:
465 		len = PROPINT64(propops)(prop, (int64_t **)prop_data);
466 		break;
467 	case DI_PROP_TYPE_BYTE:
468 		len = PROPBYTES(propops)(prop, (uchar_t **)prop_data);
469 		break;
470 	case DI_PROP_TYPE_STRING:
471 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
472 		break;
473 	case DI_PROP_TYPE_UNKNOWN:
474 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
475 		if ((len > 0) && ((*(char **)prop_data)[0] != 0)) {
476 			*prop_type = DI_PROP_TYPE_STRING;
477 			return (len);
478 		}
479 
480 		len = PROPINTS(propops)(prop, (int **)prop_data);
481 		type = DI_PROP_TYPE_INT;
482 
483 		break;
484 	default:
485 		len = -1;
486 	}
487 
488 	if (len > 0) {
489 		*prop_type = type;
490 		return (len);
491 	}
492 
493 	len = PROPRAWDATA(propops)(prop, (uchar_t **)prop_data);
494 	if (len < 0) {
495 		return (-1);
496 	} else if (len == 0) {
497 		*prop_type = DI_PROP_TYPE_BOOLEAN;
498 		return (0);
499 	}
500 
501 	*prop_type = DI_PROP_TYPE_UNKNOWN;
502 	return (len);
503 }
504 
505 static void
506 dump_prop_list_common(const dumpops_t *dumpops, int ilev, void *node)
507 {
508 	void *prop = DI_PROP_NIL, *prop_data;
509 	char *p;
510 	int i, prop_type, nitems;
511 
512 	while ((prop = NEXTPROP(dumpops)(node, prop)) != DI_PROP_NIL) {
513 		nitems = prop_type_guess(dumpops, prop, &prop_data, &prop_type);
514 		if (nitems < 0)
515 			continue;
516 
517 		indent_to_level(ilev);
518 		(void) printf("name='%s' type=", PROPNAME(dumpops)(prop));
519 
520 		switch (prop_type) {
521 		case DI_PROP_TYPE_UNDEF_IT:
522 			(void) printf("undef");
523 			break;
524 		case DI_PROP_TYPE_BOOLEAN:
525 			(void) printf("boolean");
526 			break;
527 		case DI_PROP_TYPE_INT:
528 			(void) printf("int");
529 			break;
530 		case DI_PROP_TYPE_INT64:
531 			(void) printf("int64");
532 			break;
533 		case DI_PROP_TYPE_BYTE:
534 			(void) printf("byte");
535 			break;
536 		case DI_PROP_TYPE_STRING:
537 			(void) printf("string");
538 			break;
539 		case DI_PROP_TYPE_UNKNOWN:
540 			(void) printf("unknown");
541 			break;
542 		default:
543 			/* Should never be here */
544 			(void) printf("0x%x", prop_type);
545 		}
546 
547 		if (nitems != 0)
548 			(void) printf(" items=%i", nitems);
549 
550 		/* print the major and minor numbers for a device property */
551 		if (PROPDEVT(dumpops) != NULL) {
552 			dev_t dev;
553 
554 			dev = PROPDEVT(dumpops)(prop);
555 			if (dev != DDI_DEV_T_NONE) {
556 				(void) printf(" dev=(%u,%u)",
557 				    (uint_t)major(dev), (uint_t)minor(dev));
558 			} else {
559 				(void) printf(" dev=none");
560 			}
561 		}
562 
563 		(void) putchar('\n');
564 
565 		if (nitems == 0)
566 			continue;
567 
568 		indent_to_level(ilev);
569 
570 		(void) printf("    value=");
571 
572 		switch (prop_type) {
573 		case DI_PROP_TYPE_INT:
574 			for (i = 0; i < nitems - 1; i++)
575 				(void) printf("%8.8x.", ((int *)prop_data)[i]);
576 			(void) printf("%8.8x", ((int *)prop_data)[i]);
577 			break;
578 		case DI_PROP_TYPE_INT64:
579 			for (i = 0; i < nitems - 1; i++)
580 				(void) printf("%16.16llx.",
581 				    ((long long *)prop_data)[i]);
582 			(void) printf("%16.16llx", ((long long *)prop_data)[i]);
583 			break;
584 		case DI_PROP_TYPE_STRING:
585 			p = (char *)prop_data;
586 			for (i = 0; i < nitems - 1; i++) {
587 				(void) printf("'%s' + ", p);
588 				p += strlen(p) + 1;
589 			}
590 			(void) printf("'%s'", p);
591 			break;
592 		default:
593 			for (i = 0; i < nitems - 1; i++)
594 				(void) printf("%2.2x.",
595 				    ((uint8_t *)prop_data)[i]);
596 			(void) printf("%2.2x", ((uint8_t *)prop_data)[i]);
597 		}
598 
599 		(void) putchar('\n');
600 	}
601 }
602 
603 /*
604  * walk_driver is a debugging facility.
605  */
606 static void
607 walk_driver(di_node_t root, di_devlink_handle_t devlink_hdl)
608 {
609 	di_node_t node;
610 
611 	node = di_drv_first_node(dbg.d_drivername, root);
612 
613 	while (node != DI_NODE_NIL) {
614 		(void) dump_devs(node, devlink_hdl);
615 		node = di_drv_next_node(node);
616 	}
617 }
618 
619 /*
620  * print out information about this node, returns appropriate code.
621  */
622 /*ARGSUSED1*/
623 static int
624 dump_devs(di_node_t node, void *arg)
625 {
626 	di_devlink_handle_t	devlink_hdl = (di_devlink_handle_t)arg;
627 	int			ilev = 0;	/* indentation level */
628 	char			*driver_name;
629 	di_node_t		root_node, tmp;
630 
631 	if (dbg.d_debug) {
632 		char *path = di_devfs_path(node);
633 		dprintf("Dump node %s\n", path);
634 		di_devfs_path_free(path);
635 	}
636 
637 	if (dbg.d_bydriver) {
638 		ilev = 1;
639 	} else {
640 		/* figure out indentation level */
641 		tmp = node;
642 		while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
643 			ilev++;
644 
645 		if (opts.o_target && !opts.o_ancestors) {
646 			ilev -= opts.o_target - 1;
647 		}
648 	}
649 
650 	if (opts.o_target && !node_display(node)) {
651 		/*
652 		 * if we're only displaying certain nodes and this one
653 		 * isn't flagged, skip it.
654 		 */
655 		return (DI_WALK_CONTINUE);
656 	}
657 
658 	indent_to_level(ilev);
659 
660 	(void) printf("%s", di_node_name(node));
661 
662 	/*
663 	 * if this node does not have an instance number or is the
664 	 * root node (1229946), we don't print an instance number
665 	 */
666 	root_node = tmp = node;
667 	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
668 		root_node = tmp;
669 	if ((di_instance(node) >= 0) && (node != root_node))
670 		(void) printf(", instance #%d", di_instance(node));
671 
672 	if (opts.o_drv_name) {
673 		driver_name = di_driver_name(node);
674 		if (driver_name != NULL)
675 			(void) printf(" (driver name: %s)", driver_name);
676 	} else if (di_retired(node)) {
677 		(void) printf(" (retired)");
678 	} else if (di_state(node) & DI_DRIVER_DETACHED)
679 		(void) printf(" (driver not attached)");
680 
681 	(void) printf("\n");
682 
683 	if (opts.o_verbose)  {
684 		if (dump_prop_list(&sysprop_dumpops, "System", ilev + 1,
685 		    node)) {
686 			(void) dump_prop_list(&globprop_dumpops, NULL, ilev + 1,
687 			    node);
688 		} else {
689 			(void) dump_prop_list(&globprop_dumpops,
690 			    "System software", ilev + 1, node);
691 		}
692 		(void) dump_prop_list(&drvprop_dumpops, "Driver", ilev + 1,
693 		    node);
694 		(void) dump_prop_list(&hwprop_dumpops, "Hardware", ilev + 1,
695 		    node);
696 		dump_priv_data(ilev + 1, node);
697 		dump_pathing_data(ilev + 1, node);
698 		dump_link_data(ilev + 1, node, devlink_hdl);
699 		dump_minor_data(ilev + 1, node, devlink_hdl);
700 	}
701 
702 	if (opts.o_target)
703 		return (DI_WALK_CONTINUE);
704 
705 	if (!opts.o_pseudodevs && (strcmp(di_node_name(node), "pseudo") == 0))
706 		return (DI_WALK_PRUNECHILD);
707 
708 	return (DI_WALK_CONTINUE);
709 }
710 
711 /*
712  * Returns 0 if nothing is printed, 1 otherwise
713  */
714 static int
715 dump_prop_list(const dumpops_t *dumpops, const char *name, int ilev,
716     di_node_t node)
717 {
718 	if (NEXTPROP(dumpops)(node, DI_PROP_NIL) == DI_PROP_NIL)
719 		return (0);
720 
721 	if (name != NULL)  {
722 		indent_to_level(ilev);
723 		(void) printf("%s properties:\n", name);
724 	}
725 
726 	dump_prop_list_common(dumpops, ilev + 1, node);
727 	return (1);
728 }
729 
730 
731 /* _error([no_perror, ] fmt [, arg ...]) */
732 static int
733 _error(const char *opt_noperror, ...)
734 {
735 	int saved_errno;
736 	va_list ap;
737 	int no_perror = 0;
738 	const char *fmt;
739 
740 	saved_errno = errno;
741 
742 	(void) fprintf(stderr, "%s: ", opts.o_progname);
743 
744 	va_start(ap, opt_noperror);
745 	if (opt_noperror == NULL) {
746 		no_perror = 1;
747 		fmt = va_arg(ap, char *);
748 	} else
749 		fmt = opt_noperror;
750 	(void) vfprintf(stderr, fmt, ap);
751 	va_end(ap);
752 
753 	if (no_perror)
754 		(void) fprintf(stderr, "\n");
755 	else {
756 		(void) fprintf(stderr, ": ");
757 		errno = saved_errno;
758 		perror("");
759 	}
760 
761 	return (-1);
762 }
763 
764 
765 /*
766  * The rest of the routines handle printing the raw prom devinfo (-p option).
767  *
768  * 128 is the size of the largest (currently) property name
769  * 16k - MAXNAMESZ - sizeof (int) is the size of the largest
770  * (currently) property value that is allowed.
771  * the sizeof (uint_t) is from struct openpromio
772  */
773 
774 #define	MAXNAMESZ	128
775 #define	MAXVALSIZE	(16384 - MAXNAMESZ - sizeof (uint_t))
776 #define	BUFSIZE		(MAXNAMESZ + MAXVALSIZE + sizeof (uint_t))
777 typedef union {
778 	char buf[BUFSIZE];
779 	struct openpromio opp;
780 } Oppbuf;
781 
782 static int prom_fd;
783 static uchar_t *prom_snapshot;
784 
785 static int
786 is_openprom(void)
787 {
788 	Oppbuf	oppbuf;
789 	struct openpromio *opp = &(oppbuf.opp);
790 	unsigned int i;
791 
792 	opp->oprom_size = MAXVALSIZE;
793 	if (ioctl(prom_fd, OPROMGETCONS, opp) < 0)
794 		exit(_error("OPROMGETCONS"));
795 
796 	i = (unsigned int)((unsigned char)opp->oprom_array[0]);
797 	return ((i & OPROMCONS_OPENPROM) == OPROMCONS_OPENPROM);
798 }
799 
800 int
801 do_prominfo(void)
802 {
803 	uint_t arg = opts.o_verbose;
804 
805 	if (promopen(O_RDONLY))  {
806 		exit(_error("openeepr device open failed"));
807 	}
808 
809 	if (is_openprom() == 0)  {
810 		(void) fprintf(stderr, "System architecture does not "
811 		    "support this option of this command.\n");
812 		return (1);
813 	}
814 
815 	/* OPROMSNAPSHOT returns size in arg */
816 	if (ioctl(prom_fd, OPROMSNAPSHOT, &arg) < 0)
817 		exit(_error("OPROMSNAPSHOT"));
818 
819 	if (arg == 0)
820 		return (1);
821 
822 	if ((prom_snapshot = malloc(arg)) == NULL)
823 		exit(_error("failed to allocate memory"));
824 
825 	/* copy out the snapshot for printing */
826 	/*LINTED*/
827 	*(uint_t *)prom_snapshot = arg;
828 	if (ioctl(prom_fd, OPROMCOPYOUT, prom_snapshot) < 0)
829 		exit(_error("OPROMCOPYOUT"));
830 
831 	promclose();
832 
833 	/* print out information */
834 	walk(prom_snapshot, arg, 0);
835 	free(prom_snapshot);
836 
837 	return (0);
838 }
839 
840 static void
841 walk(uchar_t *buf, uint_t size, int level)
842 {
843 	int error;
844 	nvlist_t *nvl, *cnvl;
845 	nvpair_t *child = NULL;
846 	uchar_t *cbuf = NULL;
847 	uint_t csize;
848 
849 	/* Expand to an nvlist */
850 	if (nvlist_unpack((char *)buf, size, &nvl, 0))
851 		exit(_error("error processing snapshot"));
852 
853 	/* print current node */
854 	dump_node(nvl, level);
855 
856 	/* print children */
857 	error = nvlist_lookup_byte_array(nvl, "@child", &cbuf, &csize);
858 	if ((error == ENOENT) || (cbuf == NULL))
859 		return;		/* no child exists */
860 
861 	if (error || nvlist_unpack((char *)cbuf, csize, &cnvl, 0))
862 		exit(_error("error processing snapshot"));
863 
864 	while (child = nvlist_next_nvpair(cnvl, child)) {
865 		char *name = nvpair_name(child);
866 		data_type_t type = nvpair_type(child);
867 		uchar_t *nodebuf;
868 		uint_t nodesize;
869 		if (strcmp("node", name) != 0) {
870 			dprintf("unexpected nvpair name %s != name\n", name);
871 			continue;
872 		}
873 		if (type != DATA_TYPE_BYTE_ARRAY) {
874 			dprintf("unexpected nvpair type %d, not byte array \n",
875 			    type);
876 			continue;
877 		}
878 
879 		(void) nvpair_value_byte_array(child,
880 		    (uchar_t **)&nodebuf, &nodesize);
881 		walk(nodebuf, nodesize, level + 1);
882 	}
883 
884 	nvlist_free(nvl);
885 }
886 
887 /*
888  * Print all properties and values
889  */
890 static void
891 dump_node(nvlist_t *nvl, int level)
892 {
893 	int id = 0;
894 	char *name = NULL;
895 	nvpair_t *nvp = NULL;
896 
897 	indent_to_level(level);
898 	(void) printf("Node");
899 	if (!opts.o_verbose) {
900 		if (nvlist_lookup_string(nvl, "name", &name))
901 			(void) printf("data not available");
902 		else
903 			(void) printf(" '%s'", name);
904 		(void) putchar('\n');
905 		return;
906 	}
907 	(void) nvlist_lookup_int32(nvl, "@nodeid", &id);
908 	(void) printf(" %#08x\n", id);
909 
910 	while (nvp = nvlist_next_nvpair(nvl, nvp)) {
911 		name = nvpair_name(nvp);
912 		if (name[0] == '@')
913 			continue;
914 
915 		print_one(nvp, level + 1);
916 	}
917 	(void) putchar('\n');
918 }
919 
920 static const char *
921 path_state_name(di_path_state_t st)
922 {
923 	switch (st) {
924 		case DI_PATH_STATE_ONLINE:
925 			return ("online");
926 		case DI_PATH_STATE_STANDBY:
927 			return ("standby");
928 		case DI_PATH_STATE_OFFLINE:
929 			return ("offline");
930 		case DI_PATH_STATE_FAULT:
931 			return ("faulted");
932 	}
933 	return ("unknown");
934 }
935 
936 /*
937  * Print all phci's each client is connected to.
938  */
939 static void
940 dump_pathing_data(int ilev, di_node_t node)
941 {
942 	di_path_t pi = DI_PATH_NIL;
943 	int firsttime = 1;
944 
945 	if (node == DI_PATH_NIL)
946 		return;
947 
948 	while ((pi = di_path_next_phci(node, pi)) != DI_PATH_NIL) {
949 		di_node_t phci_node = di_path_phci_node(pi);
950 
951 		if (firsttime) {
952 			indent_to_level(ilev);
953 			firsttime = 0;
954 			ilev++;
955 			(void) printf("Paths from multipath bus adapters:\n");
956 		}
957 
958 		indent_to_level(ilev);
959 		(void) printf("%s#%d (%s)\n", di_driver_name(phci_node),
960 		    di_instance(phci_node), path_state_name(di_path_state(pi)));
961 		dump_prop_list_common(&pathprop_dumpops, ilev + 1, pi);
962 	}
963 }
964 
965 static int
966 dump_minor_data_links(di_devlink_t devlink, void *arg)
967 {
968 	int ilev = (intptr_t)arg;
969 	indent_to_level(ilev);
970 	(void) printf("dev_link=%s\n", di_devlink_path(devlink));
971 	return (DI_WALK_CONTINUE);
972 }
973 
974 static void
975 dump_minor_data_paths(int ilev, di_minor_t minor,
976     di_devlink_handle_t devlink_hdl)
977 {
978 	char	*path, *type;
979 	int	spec_type;
980 
981 	/* get the path to the device and the minor node name */
982 	if ((path = di_devfs_minor_path(minor)) == NULL)
983 		exit(_error("failed to allocate memory"));
984 
985 	/* display the path to this minor node */
986 	indent_to_level(ilev);
987 	(void) printf("dev_path=%s\n", path);
988 
989 	if (devlink_hdl != NULL) {
990 
991 		/* get the device minor node information */
992 		spec_type = di_minor_spectype(minor);
993 		switch (di_minor_type(minor)) {
994 			case DDM_MINOR:
995 				type = "minor";
996 				break;
997 			case DDM_ALIAS:
998 				type = "alias";
999 				break;
1000 			case DDM_DEFAULT:
1001 				type = "default";
1002 				break;
1003 			case DDM_INTERNAL_PATH:
1004 				type = "internal";
1005 				break;
1006 			default:
1007 				type = "unknown";
1008 				break;
1009 		}
1010 
1011 		/* display the device minor node information */
1012 		indent_to_level(ilev + 1);
1013 		(void) printf("spectype=%s type=%s\n",
1014 		    (spec_type == S_IFBLK) ? "blk" : "chr", type);
1015 
1016 		/* display all the devlinks for this device minor node */
1017 		(void) di_devlink_walk(devlink_hdl, NULL, path,
1018 		    0, (void *)(intptr_t)(ilev + 1), dump_minor_data_links);
1019 	}
1020 
1021 	di_devfs_path_free(path);
1022 }
1023 
1024 static void
1025 create_minor_list(di_node_t node)
1026 {
1027 	di_minor_t	minor, minor_head, minor_tail, minor_prev, minor_walk;
1028 	int		major;
1029 
1030 	/* if there are no minor nodes, bail */
1031 	if (di_minor_next(node, DI_MINOR_NIL) == DI_MINOR_NIL)
1032 		return;
1033 
1034 	/*
1035 	 * here we want to create lists of minor nodes with the same
1036 	 * dev_t.  to do this we first sort all the minor nodes by devt.
1037 	 *
1038 	 * the algorithm used here is a bubble sort, so performance sucks.
1039 	 * but it's probably ok here because most device instances don't
1040 	 * have that many minor nodes.  also we're doing this as we're
1041 	 * displaying each node so it doesn't look like we're pausing
1042 	 * output for a long time.
1043 	 */
1044 	major = di_driver_major(node);
1045 	minor_head = minor_tail = minor = DI_MINOR_NIL;
1046 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
1047 		dev_t	dev = di_minor_devt(minor);
1048 
1049 		/* skip /pseudo/clone@0 minor nodes */
1050 		if (major != major(dev))
1051 			continue;
1052 
1053 		minor_ptr_set(minor, DI_MINOR_NIL);
1054 		if (minor_head == DI_MINOR_NIL) {
1055 			/* this is the first minor node we're looking at */
1056 			minor_head = minor_tail = minor;
1057 			continue;
1058 		}
1059 
1060 		/*
1061 		 * if the new dev is less than the old dev, update minor_head
1062 		 * so it points to the beginning of the list.  ie it points
1063 		 * to the node with the lowest dev value
1064 		 */
1065 		if (dev <= di_minor_devt(minor_head)) {
1066 			minor_ptr_set(minor, minor_head);
1067 			minor_head = minor;
1068 			continue;
1069 		}
1070 
1071 		minor_prev = minor_head;
1072 		minor_walk = minor_ptr(minor_head);
1073 		while ((minor_walk != DI_MINOR_NIL) &&
1074 		    (dev > di_minor_devt(minor_walk))) {
1075 			minor_prev = minor_walk;
1076 			minor_walk = minor_ptr(minor_walk);
1077 		}
1078 		minor_ptr_set(minor, minor_walk);
1079 		minor_ptr_set(minor_prev, minor);
1080 		if (minor_walk == NULL)
1081 			minor_tail = minor;
1082 	}
1083 
1084 	/* check if there were any non /pseudo/clone@0 nodes.  if not, bail */
1085 	if (minor_head == DI_MINOR_NIL)
1086 		return;
1087 
1088 	/*
1089 	 * now that we have a list of minor nodes sorted by devt
1090 	 * we walk through the list and break apart the entire list
1091 	 * to create circular lists of minor nodes with matching devts.
1092 	 */
1093 	minor_prev = minor_head;
1094 	minor_walk = minor_ptr(minor_head);
1095 	while (minor_walk != DI_MINOR_NIL) {
1096 		if (di_minor_devt(minor_prev) != di_minor_devt(minor_walk)) {
1097 			minor_ptr_set(minor_prev, minor_head);
1098 			minor_head = minor_walk;
1099 		}
1100 		minor_prev = minor_walk;
1101 		minor_walk = minor_ptr(minor_walk);
1102 	}
1103 	minor_ptr_set(minor_tail, minor_head);
1104 }
1105 
1106 static void
1107 link_lnode_disp(di_link_t link, uint_t endpoint, int ilev,
1108     di_devlink_handle_t devlink_hdl)
1109 {
1110 	di_lnode_t	lnode;
1111 	char		*name, *path;
1112 	int		displayed_path, spec_type;
1113 	di_node_t	node = DI_NODE_NIL;
1114 	dev_t		devt = DDI_DEV_T_NONE;
1115 
1116 	lnode = di_link_to_lnode(link, endpoint);
1117 
1118 	indent_to_level(ilev);
1119 	name = di_lnode_name(lnode);
1120 	spec_type = di_link_spectype(link);
1121 
1122 	(void) printf("mod=%s", name);
1123 
1124 	/*
1125 	 * if we're displaying the source of a link, we should display
1126 	 * the target access mode.  (either block or char.)
1127 	 */
1128 	if (endpoint == DI_LINK_SRC)
1129 		(void) printf(" accesstype=%s",
1130 		    (spec_type == S_IFBLK) ? "blk" : "chr");
1131 
1132 	/*
1133 	 * check if the lnode is bound to a specific device
1134 	 * minor node (i.e.  if it's bound to a dev_t) and
1135 	 * if so display the dev_t value and any possible
1136 	 * minor node pathing information.
1137 	 */
1138 	displayed_path = 0;
1139 	if (di_lnode_devt(lnode, &devt) == 0) {
1140 		di_minor_t	minor = DI_MINOR_NIL;
1141 
1142 		(void) printf(" dev=(%u,%u)\n",
1143 		    (uint_t)major(devt), (uint_t)minor(devt));
1144 
1145 		/* display paths to the src devt minor node */
1146 		while (minor = di_minor_next(node, minor)) {
1147 			if (devt != di_minor_devt(minor))
1148 				continue;
1149 
1150 			if ((endpoint == DI_LINK_TGT) &&
1151 			    (spec_type != di_minor_spectype(minor)))
1152 				continue;
1153 
1154 			dump_minor_data_paths(ilev + 1, minor, devlink_hdl);
1155 			displayed_path = 1;
1156 		}
1157 	} else {
1158 		(void) printf("\n");
1159 	}
1160 
1161 	if (displayed_path)
1162 		return;
1163 
1164 	/*
1165 	 * This device lnode is not did not have any minor node
1166 	 * pathing information so display the path to device node.
1167 	 */
1168 	node = di_lnode_devinfo(lnode);
1169 	if ((path = di_devfs_path(node)) == NULL)
1170 		exit(_error("failed to allocate memory"));
1171 
1172 	indent_to_level(ilev + 1);
1173 	(void) printf("dev_path=%s\n", path);
1174 	di_devfs_path_free(path);
1175 }
1176 
1177 static void
1178 dump_minor_link_data(int ilev, di_node_t node, dev_t devt,
1179     di_devlink_handle_t devlink_hdl)
1180 {
1181 	int		first = 1;
1182 	di_link_t	link;
1183 
1184 	link = DI_LINK_NIL;
1185 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
1186 		di_lnode_t	tgt_lnode;
1187 		dev_t		tgt_devt = DDI_DEV_T_NONE;
1188 
1189 		tgt_lnode = di_link_to_lnode(link, DI_LINK_TGT);
1190 
1191 		if (di_lnode_devt(tgt_lnode, &tgt_devt) != 0)
1192 			continue;
1193 
1194 		if (devt != tgt_devt)
1195 			continue;
1196 
1197 		if (first) {
1198 			first = 0;
1199 			indent_to_level(ilev);
1200 			(void) printf("Device Minor Layered Under:\n");
1201 		}
1202 
1203 		/* displayed this lnode */
1204 		lnode_displayed_set(tgt_lnode);
1205 		link_lnode_disp(link, DI_LINK_SRC, ilev + 1, devlink_hdl);
1206 	}
1207 
1208 	link = DI_LINK_NIL;
1209 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
1210 		di_lnode_t	src_lnode;
1211 		dev_t		src_devt = DDI_DEV_T_NONE;
1212 
1213 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
1214 
1215 		if (di_lnode_devt(src_lnode, &src_devt) != 0)
1216 			continue;
1217 
1218 		if (devt != src_devt)
1219 			continue;
1220 
1221 		if (first) {
1222 			first = 0;
1223 			indent_to_level(ilev);
1224 			(void) printf("Device Minor Layered Over:\n");
1225 		}
1226 
1227 		/* displayed this lnode */
1228 		lnode_displayed_set(src_lnode);
1229 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
1230 	}
1231 }
1232 
1233 static void
1234 dump_minor_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
1235 {
1236 	di_minor_t	minor, minor_next;
1237 	di_lnode_t	lnode;
1238 	di_link_t	link;
1239 	int		major, firstminor = 1;
1240 
1241 	/*
1242 	 * first go through and mark all lnodes and minor nodes for this
1243 	 * node as undisplayed
1244 	 */
1245 	lnode = DI_LNODE_NIL;
1246 	while (lnode = di_lnode_next(node, lnode))
1247 		lnode_displayed_clear(lnode);
1248 	minor = DI_MINOR_NIL;
1249 	while (minor = di_minor_next(node, minor)) {
1250 		minor_displayed_clear(minor);
1251 	}
1252 
1253 	/*
1254 	 * when we display the minor nodes we want to coalesce nodes
1255 	 * that have the same dev_t.  we do this by creating circular
1256 	 * lists of minor nodes with the same devt.
1257 	 */
1258 	create_minor_list(node);
1259 
1260 	/* now we display the driver defined minor nodes */
1261 	major = di_driver_major(node);
1262 	minor = DI_MINOR_NIL;
1263 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
1264 		dev_t	devt;
1265 
1266 		/*
1267 		 * skip /pseudo/clone@0 minor nodes.
1268 		 * these are only created for DLPIv2 network devices.
1269 		 * since these minor nodes are associated with a driver
1270 		 * and are only bound to a device instance after they
1271 		 * are opened and attached we don't print them out
1272 		 * here.
1273 		 */
1274 		devt = di_minor_devt(minor);
1275 		if (major != major(devt))
1276 			continue;
1277 
1278 		/* skip nodes that may have already been displayed */
1279 		if (minor_displayed(minor))
1280 			continue;
1281 
1282 		if (firstminor) {
1283 			firstminor = 0;
1284 			indent_to_level(ilev++);
1285 			(void) printf("Device Minor Nodes:\n");
1286 		}
1287 
1288 		/* display the device minor node information */
1289 		indent_to_level(ilev);
1290 		(void) printf("dev=(%u,%u)\n",
1291 		    (uint_t)major(devt), (uint_t)minor(devt));
1292 
1293 		minor_next = minor;
1294 		do {
1295 			/* display device minor node path info */
1296 			minor_displayed_set(minor_next);
1297 			dump_minor_data_paths(ilev + 1, minor_next,
1298 			    devlink_hdl);
1299 
1300 			/* get a pointer to the next node */
1301 			minor_next = minor_ptr(minor_next);
1302 		} while (minor_next != minor);
1303 
1304 		/* display who has this device minor node open */
1305 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
1306 	}
1307 
1308 	/*
1309 	 * now go through all the target lnodes for this node and
1310 	 * if they haven't yet been displayed, display them now.
1311 	 *
1312 	 * this happens in the case of clone opens when an "official"
1313 	 * minor node does not exist for the opened devt
1314 	 */
1315 	link = DI_LINK_NIL;
1316 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
1317 		dev_t		devt;
1318 
1319 		lnode = di_link_to_lnode(link, DI_LINK_TGT);
1320 
1321 		/* if we've already displayed this target lnode, skip it */
1322 		if (lnode_displayed(lnode))
1323 			continue;
1324 
1325 		if (firstminor) {
1326 			firstminor = 0;
1327 			indent_to_level(ilev++);
1328 			(void) printf("Device Minor Nodes:\n");
1329 		}
1330 
1331 		/* display the device minor node information */
1332 		indent_to_level(ilev);
1333 		(void) di_lnode_devt(lnode, &devt);
1334 		(void) printf("dev=(%u,%u)\n",
1335 		    (uint_t)major(devt), (uint_t)minor(devt));
1336 
1337 		indent_to_level(ilev + 1);
1338 		(void) printf("dev_path=<clone>\n");
1339 
1340 		/* display who has this cloned device minor node open */
1341 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
1342 
1343 		/* mark node as displayed */
1344 		lnode_displayed_set(lnode);
1345 	}
1346 }
1347 
1348 static void
1349 dump_link_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
1350 {
1351 	int		first = 1;
1352 	di_link_t	link;
1353 
1354 	link = DI_LINK_NIL;
1355 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
1356 		di_lnode_t	src_lnode;
1357 		dev_t		src_devt = DDI_DEV_T_NONE;
1358 
1359 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
1360 
1361 		/*
1362 		 * here we only want to print out layering information
1363 		 * if we are the source and our source lnode is not
1364 		 * associated with any particular dev_t.  (which means
1365 		 * we won't display this link while dumping minor node
1366 		 * info.)
1367 		 */
1368 		if (di_lnode_devt(src_lnode, &src_devt) != -1)
1369 			continue;
1370 
1371 		if (first) {
1372 			first = 0;
1373 			indent_to_level(ilev);
1374 			(void) printf("Device Layered Over:\n");
1375 		}
1376 
1377 		/* displayed this lnode */
1378 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
1379 	}
1380 }
1381 
1382 /*
1383  * certain 'known' property names may contain 'composite' strings.
1384  * Handle them here, and print them as 'string1' + 'string2' ...
1385  */
1386 static int
1387 print_composite_string(const char *var, char *value, int size)
1388 {
1389 	char *p, *q;
1390 	char *firstp;
1391 
1392 	if ((strcmp(var, "version") != 0) &&
1393 	    (strcmp(var, "compatible") != 0))
1394 		return (0);	/* Not a known composite string */
1395 
1396 	/*
1397 	 * Verify that each string in the composite string is non-NULL,
1398 	 * is within the bounds of the property length, and contains
1399 	 * printable characters or white space. Otherwise let the
1400 	 * caller deal with it.
1401 	 */
1402 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
1403 		if (strlen(p) == 0)
1404 			return (0);		/* NULL string */
1405 		for (q = p; *q; q++) {
1406 			if (!(isascii(*q) && (isprint(*q) || isspace(*q))))
1407 				return (0);	/* Not printable or space */
1408 		}
1409 		if (q > (firstp + size))
1410 			return (0);		/* Out of bounds */
1411 	}
1412 
1413 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
1414 		if (p == firstp)
1415 			(void) printf("'%s'", p);
1416 		else
1417 			(void) printf(" + '%s'", p);
1418 	}
1419 	(void) putchar('\n');
1420 	return (1);
1421 }
1422 
1423 /*
1424  * Print one property and its value. Handle the verbose case.
1425  */
1426 static void
1427 print_one(nvpair_t *nvp, int level)
1428 {
1429 	int i;
1430 	int endswap = 0;
1431 	uint_t valsize;
1432 	char *value;
1433 	char *var = nvpair_name(nvp);
1434 
1435 	indent_to_level(level);
1436 	(void) printf("%s: ", var);
1437 
1438 	switch (nvpair_type(nvp)) {
1439 	case DATA_TYPE_BOOLEAN:
1440 		(void) printf(" \n");
1441 		return;
1442 	case DATA_TYPE_BYTE_ARRAY:
1443 		if (nvpair_value_byte_array(nvp, (uchar_t **)&value,
1444 		    &valsize)) {
1445 			(void) printf("data not available.\n");
1446 			return;
1447 		}
1448 		valsize--;	/* take out null added by driver */
1449 
1450 		/*
1451 		 * Do not print valsize > MAXVALSIZE, to be compatible
1452 		 * with old behavior. E.g. intel's eisa-nvram property
1453 		 * has a size of 65 K.
1454 		 */
1455 		if (valsize > MAXVALSIZE) {
1456 			(void) printf(" \n");
1457 			return;
1458 		}
1459 		break;
1460 	default:
1461 		(void) printf("data type unexpected.\n");
1462 		return;
1463 	}
1464 
1465 	/*
1466 	 * Handle printing verbosely
1467 	 */
1468 	if (print_composite_string(var, value, valsize)) {
1469 		return;
1470 	}
1471 
1472 	if (!unprintable(value, valsize)) {
1473 		(void) printf(" '%s'\n", value);
1474 		return;
1475 	}
1476 
1477 	(void) printf(" ");
1478 #ifdef	__x86
1479 	/*
1480 	 * Due to backwards compatibility constraints x86 int
1481 	 * properties are not in big-endian (ieee 1275) byte order.
1482 	 * If we have a property that is a multiple of 4 bytes,
1483 	 * let's assume it is an array of ints and print the bytes
1484 	 * in little endian order to make things look nicer for
1485 	 * the user.
1486 	 */
1487 	endswap = (valsize % 4) == 0;
1488 #endif	/* __x86 */
1489 	for (i = 0; i < valsize; i++) {
1490 		int out;
1491 		if (i && (i % 4 == 0))
1492 			(void) putchar('.');
1493 		if (endswap)
1494 			out = value[i + (3 - 2 * (i % 4))] & 0xff;
1495 		else
1496 			out = value[i] & 0xff;
1497 
1498 		(void) printf("%02x", out);
1499 	}
1500 	(void) putchar('\n');
1501 }
1502 
1503 static int
1504 unprintable(char *value, int size)
1505 {
1506 	int i;
1507 
1508 	/*
1509 	 * Is this just a zero?
1510 	 */
1511 	if (size == 0 || value[0] == '\0')
1512 		return (1);
1513 	/*
1514 	 * If any character is unprintable, or if a null appears
1515 	 * anywhere except at the end of a string, the whole
1516 	 * property is "unprintable".
1517 	 */
1518 	for (i = 0; i < size; ++i) {
1519 		if (value[i] == '\0')
1520 			return (i != (size - 1));
1521 		if (!isascii(value[i]) || iscntrl(value[i]))
1522 			return (1);
1523 	}
1524 	return (0);
1525 }
1526 
1527 static int
1528 promopen(int oflag)
1529 {
1530 	for (;;)  {
1531 		if ((prom_fd = open(opts.o_promdev, oflag)) < 0)  {
1532 			if (errno == EAGAIN)   {
1533 				(void) sleep(5);
1534 				continue;
1535 			}
1536 			if (errno == ENXIO)
1537 				return (-1);
1538 			if (getzoneid() == GLOBAL_ZONEID) {
1539 				_exit(_error("cannot open %s",
1540 				    opts.o_promdev));
1541 			}
1542 			/* not an error if this isn't the global zone */
1543 			(void) _error(NULL, "openprom facility not available");
1544 			exit(0);
1545 		} else
1546 			return (0);
1547 	}
1548 }
1549 
1550 static void
1551 promclose(void)
1552 {
1553 	if (close(prom_fd) < 0)
1554 		exit(_error("close error on %s", opts.o_promdev));
1555 }
1556 
1557 /*
1558  * Get and print the name of the frame buffer device.
1559  */
1560 int
1561 do_fbname(void)
1562 {
1563 	int	retval;
1564 	char fbuf_path[MAXPATHLEN];
1565 
1566 	retval =  modctl(MODGETFBNAME, (caddr_t)fbuf_path);
1567 
1568 	if (retval == 0) {
1569 		(void) printf("%s\n", fbuf_path);
1570 	} else {
1571 		if (retval == EFAULT) {
1572 			(void) fprintf(stderr,
1573 			"Error copying fb path to userland\n");
1574 		} else {
1575 			(void) fprintf(stderr,
1576 			"Console output device is not a frame buffer\n");
1577 		}
1578 		return (1);
1579 	}
1580 	return (0);
1581 }
1582 
1583 /*
1584  * Get and print the PROM version.
1585  */
1586 int
1587 do_promversion(void)
1588 {
1589 	Oppbuf	oppbuf;
1590 	struct openpromio *opp = &(oppbuf.opp);
1591 
1592 	if (promopen(O_RDONLY))  {
1593 		(void) fprintf(stderr, "Cannot open openprom device\n");
1594 		return (1);
1595 	}
1596 
1597 	opp->oprom_size = MAXVALSIZE;
1598 	if (ioctl(prom_fd, OPROMGETVERSION, opp) < 0)
1599 		exit(_error("OPROMGETVERSION"));
1600 
1601 	(void) printf("%s\n", opp->oprom_array);
1602 	promclose();
1603 	return (0);
1604 }
1605 
1606 int
1607 do_prom_version64(void)
1608 {
1609 #ifdef	sparc
1610 	Oppbuf	oppbuf;
1611 	struct openpromio *opp = &(oppbuf.opp);
1612 	/*LINTED*/
1613 	struct openprom_opr64 *opr = (struct openprom_opr64 *)opp->oprom_array;
1614 
1615 	static const char msg[] =
1616 	    "NOTICE: The firmware on this system does not support the "
1617 	    "64-bit OS.\n"
1618 	    "\tPlease upgrade to at least the following version:\n"
1619 	    "\t\t%s\n\n";
1620 
1621 	if (promopen(O_RDONLY))  {
1622 		(void) fprintf(stderr, "Cannot open openprom device\n");
1623 		return (-1);
1624 	}
1625 
1626 	opp->oprom_size = MAXVALSIZE;
1627 	if (ioctl(prom_fd, OPROMREADY64, opp) < 0)
1628 		exit(_error("OPROMREADY64"));
1629 
1630 	if (opr->return_code == 0)
1631 		return (0);
1632 
1633 	(void) printf(msg, opr->message);
1634 
1635 	promclose();
1636 	return (opr->return_code);
1637 #else
1638 	return (0);
1639 #endif
1640 }
1641 
1642 int
1643 do_productinfo(void)
1644 {
1645 	di_node_t root, next_node;
1646 	di_prom_handle_t promh;
1647 	static const char *root_prop[] = { "name", "model", "banner-name",
1648 					"compatible" };
1649 	static const char *root_propv[] = { "name", "model", "banner-name",
1650 					"compatible", "idprom" };
1651 	static const char *oprom_prop[] = { "model", "version" };
1652 
1653 
1654 	root = di_init("/", DINFOCPYALL);
1655 
1656 	if (root == DI_NODE_NIL) {
1657 		(void) fprintf(stderr, "di_init() failed\n");
1658 		return (1);
1659 	}
1660 
1661 	promh = di_prom_init();
1662 
1663 	if (promh == DI_PROM_HANDLE_NIL) {
1664 		(void) fprintf(stderr, "di_prom_init() failed\n");
1665 		return (1);
1666 	}
1667 
1668 	if (opts.o_verbose) {
1669 		dump_prodinfo(promh, root, root_propv, "root",
1670 		    NUM_ELEMENTS(root_propv));
1671 
1672 		/* Get model and version properties under node "openprom" */
1673 		next_node = find_node_by_name(promh, root, "openprom");
1674 		if (next_node != DI_NODE_NIL)
1675 			dump_prodinfo(promh, next_node, oprom_prop,
1676 			    "openprom", NUM_ELEMENTS(oprom_prop));
1677 
1678 	} else
1679 		dump_prodinfo(promh, root, root_prop, "root",
1680 		    NUM_ELEMENTS(root_prop));
1681 	di_prom_fini(promh);
1682 	di_fini(root);
1683 	return (0);
1684 }
1685 
1686 di_node_t
1687 find_node_by_name(di_prom_handle_t promh, di_node_t parent,
1688 		char *node_name)
1689 {
1690 	di_node_t next_node;
1691 	uchar_t *prop_valp;
1692 
1693 	for (next_node = di_child_node(parent); next_node != DI_NODE_NIL;
1694 	    next_node = di_sibling_node(next_node)) {
1695 		int len;
1696 
1697 		len = get_propval_by_name(promh, next_node, "name", &prop_valp);
1698 		if ((len != -1) && (strcmp((char *)prop_valp, node_name) == 0))
1699 			return (next_node);
1700 	}
1701 	return (DI_NODE_NIL);
1702 }
1703 
1704 
1705 int
1706 get_propval_by_name(di_prom_handle_t promh, di_node_t node, const char *name,
1707 			uchar_t **valp)
1708 {
1709 	int len;
1710 	uchar_t *bufp;
1711 
1712 	len = di_prom_prop_lookup_bytes(promh, node, name,
1713 	    (uchar_t **)&bufp);
1714 	if (len != -1) {
1715 		*valp = (uchar_t *)malloc(len);
1716 		(void) memcpy(*valp, bufp, len);
1717 	}
1718 	return (len);
1719 }
1720 
1721 
1722 static void
1723 dump_prodinfo(di_prom_handle_t promh, di_node_t node, const char **propstr,
1724 		char *node_name, int num)
1725 {
1726 	int out, len, index1, index, endswap = 0;
1727 	uchar_t *prop_valp;
1728 
1729 	for (index1 = 0; index1 < num; index1++) {
1730 		len = get_propval_by_name(promh, node, propstr[index1],
1731 		    &prop_valp);
1732 		if (len != -1) {
1733 			if (strcmp(node_name, "root"))
1734 				(void) printf("%s ", node_name);
1735 
1736 			(void) printf("%s: ", propstr[index1]);
1737 
1738 			if (print_composite_string((const char *)
1739 			    propstr[index1], (char *)prop_valp, len)) {
1740 				free(prop_valp);
1741 				continue;
1742 			}
1743 
1744 			if (!unprintable((char *)prop_valp, len)) {
1745 				(void) printf(" %s\n", (char *)prop_valp);
1746 				free(prop_valp);
1747 				continue;
1748 			}
1749 
1750 			(void) printf(" ");
1751 #ifdef  __x86
1752 			endswap = (len % 4) == 0;
1753 #endif  /* __x86 */
1754 			for (index = 0; index < len; index++) {
1755 				if (index && (index % 4 == 0))
1756 					(void) putchar('.');
1757 				if (endswap)
1758 					out = prop_valp[index +
1759 					    (3 - 2 * (index % 4))] & 0xff;
1760 				else
1761 					out = prop_valp[index] & 0xff;
1762 				(void) printf("%02x", out);
1763 			}
1764 			(void) putchar('\n');
1765 			free(prop_valp);
1766 		}
1767 	}
1768 }
1769