xref: /onnv-gate/usr/src/cmd/prtconf/pdevinfo.c (revision 7224:19ff3ecd90c2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54453Scth  * Common Development and Distribution License (the "License").
64453Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226640Scth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * For machines that support the openprom, fetch and print the list
300Sstevel@tonic-gate  * of devices that the kernel has fetched from the prom or conjured up.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdarg.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <strings.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stropts.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
430Sstevel@tonic-gate #include <sys/sunddi.h>
440Sstevel@tonic-gate #include <sys/openpromio.h>
450Sstevel@tonic-gate #include <sys/modctl.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <zone.h>
480Sstevel@tonic-gate #include <libnvpair.h>
490Sstevel@tonic-gate #include "prtconf.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate typedef char *(*dump_propname_t)(void *);
530Sstevel@tonic-gate typedef int (*dump_proptype_t)(void *);
540Sstevel@tonic-gate typedef int (*dump_propints_t)(void *, int **);
550Sstevel@tonic-gate typedef int (*dump_propint64_t)(void *, int64_t **);
560Sstevel@tonic-gate typedef int (*dump_propstrings_t)(void *, char **);
570Sstevel@tonic-gate typedef int (*dump_propbytes_t)(void *, uchar_t **);
580Sstevel@tonic-gate typedef int (*dump_proprawdata_t)(void *, uchar_t **);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate typedef struct dumpops_common {
610Sstevel@tonic-gate 	dump_propname_t doc_propname;
620Sstevel@tonic-gate 	dump_proptype_t doc_proptype;
630Sstevel@tonic-gate 	dump_propints_t doc_propints;
640Sstevel@tonic-gate 	dump_propint64_t doc_propint64;
650Sstevel@tonic-gate 	dump_propstrings_t doc_propstrings;
660Sstevel@tonic-gate 	dump_propbytes_t doc_propbytes;
670Sstevel@tonic-gate 	dump_proprawdata_t doc_proprawdata;
680Sstevel@tonic-gate } dumpops_common_t;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static const dumpops_common_t prop_dumpops = {
710Sstevel@tonic-gate 	(dump_propname_t)di_prop_name,
720Sstevel@tonic-gate 	(dump_proptype_t)di_prop_type,
730Sstevel@tonic-gate 	(dump_propints_t)di_prop_ints,
740Sstevel@tonic-gate 	(dump_propint64_t)di_prop_int64,
750Sstevel@tonic-gate 	(dump_propstrings_t)di_prop_strings,
760Sstevel@tonic-gate 	(dump_propbytes_t)di_prop_bytes,
770Sstevel@tonic-gate 	(dump_proprawdata_t)di_prop_rawdata
780Sstevel@tonic-gate }, pathprop_common_dumpops = {
790Sstevel@tonic-gate 	(dump_propname_t)di_path_prop_name,
800Sstevel@tonic-gate 	(dump_proptype_t)di_path_prop_type,
810Sstevel@tonic-gate 	(dump_propints_t)di_path_prop_ints,
820Sstevel@tonic-gate 	(dump_propint64_t)di_path_prop_int64s,
830Sstevel@tonic-gate 	(dump_propstrings_t)di_path_prop_strings,
840Sstevel@tonic-gate 	(dump_propbytes_t)di_path_prop_bytes,
850Sstevel@tonic-gate 	(dump_proprawdata_t)di_path_prop_bytes
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate typedef void *(*dump_nextprop_t)(void *, void *);
890Sstevel@tonic-gate typedef dev_t (*dump_propdevt_t)(void *);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate typedef struct dumpops {
920Sstevel@tonic-gate 	const dumpops_common_t *dop_common;
930Sstevel@tonic-gate 	dump_nextprop_t dop_nextprop;
940Sstevel@tonic-gate 	dump_propdevt_t dop_propdevt;
950Sstevel@tonic-gate } dumpops_t;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static const dumpops_t sysprop_dumpops = {
980Sstevel@tonic-gate 	&prop_dumpops,
990Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_sys_next,
1000Sstevel@tonic-gate 	NULL
1010Sstevel@tonic-gate }, globprop_dumpops = {
1020Sstevel@tonic-gate 	&prop_dumpops,
1030Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_global_next,
1040Sstevel@tonic-gate 	NULL
1050Sstevel@tonic-gate }, drvprop_dumpops = {
1060Sstevel@tonic-gate 	&prop_dumpops,
1070Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_drv_next,
1080Sstevel@tonic-gate 	(dump_propdevt_t)di_prop_devt
1090Sstevel@tonic-gate }, hwprop_dumpops = {
1100Sstevel@tonic-gate 	&prop_dumpops,
1110Sstevel@tonic-gate 	(dump_nextprop_t)di_prop_hw_next,
1120Sstevel@tonic-gate 	NULL
1130Sstevel@tonic-gate }, pathprop_dumpops = {
1140Sstevel@tonic-gate 	&pathprop_common_dumpops,
1150Sstevel@tonic-gate 	(dump_nextprop_t)di_path_prop_next,
1160Sstevel@tonic-gate 	NULL
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #define	PROPNAME(ops) (ops->dop_common->doc_propname)
1200Sstevel@tonic-gate #define	PROPTYPE(ops) (ops->dop_common->doc_proptype)
1210Sstevel@tonic-gate #define	PROPINTS(ops) (ops->dop_common->doc_propints)
1220Sstevel@tonic-gate #define	PROPINT64(ops) (ops->dop_common->doc_propint64)
1230Sstevel@tonic-gate #define	PROPSTRINGS(ops) (ops->dop_common->doc_propstrings)
1240Sstevel@tonic-gate #define	PROPBYTES(ops) (ops->dop_common->doc_propbytes)
1250Sstevel@tonic-gate #define	PROPRAWDATA(ops) (ops->dop_common->doc_proprawdata)
1260Sstevel@tonic-gate #define	NEXTPROP(ops) (ops->dop_nextprop)
1270Sstevel@tonic-gate #define	PROPDEVT(ops) (ops->dop_propdevt)
1280Sstevel@tonic-gate #define	NUM_ELEMENTS(A) (sizeof (A) / sizeof (A[0]))
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static int prop_type_guess(const dumpops_t *, void *, void **, int *);
1310Sstevel@tonic-gate static void walk_driver(di_node_t, di_devlink_handle_t);
1320Sstevel@tonic-gate static int dump_devs(di_node_t, void *);
133*7224Scth static int dump_prop_list(const dumpops_t *, const char *,
134*7224Scth 				int, void *, dev_t);
1350Sstevel@tonic-gate static int _error(const char *, ...);
1360Sstevel@tonic-gate static int is_openprom();
1370Sstevel@tonic-gate static void walk(uchar_t *, uint_t, int);
1380Sstevel@tonic-gate static void dump_node(nvlist_t *, int);
1390Sstevel@tonic-gate static void dump_prodinfo(di_prom_handle_t, di_node_t, const char **,
1400Sstevel@tonic-gate 				char *, int);
1410Sstevel@tonic-gate static di_node_t find_node_by_name(di_prom_handle_t, di_node_t, char *);
1420Sstevel@tonic-gate static int get_propval_by_name(di_prom_handle_t, di_node_t,
1430Sstevel@tonic-gate 				const char *, uchar_t **);
1440Sstevel@tonic-gate static void dump_pathing_data(int, di_node_t);
1450Sstevel@tonic-gate static void dump_minor_data(int, di_node_t, di_devlink_handle_t);
1460Sstevel@tonic-gate static void dump_link_data(int, di_node_t, di_devlink_handle_t);
1470Sstevel@tonic-gate static int print_composite_string(const char *, char *, int);
1480Sstevel@tonic-gate static void print_one(nvpair_t *, int);
1490Sstevel@tonic-gate static int unprintable(char *, int);
1500Sstevel@tonic-gate static int promopen(int);
1510Sstevel@tonic-gate static void promclose();
1520Sstevel@tonic-gate static di_node_t find_target_node(di_node_t);
1530Sstevel@tonic-gate static void node_display_set(di_node_t);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate void
1560Sstevel@tonic-gate prtconf_devinfo(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct di_priv_data	fetch;
1590Sstevel@tonic-gate 	di_devlink_handle_t	devlink_hdl = NULL;
1600Sstevel@tonic-gate 	di_node_t		root_node;
1610Sstevel@tonic-gate 	uint_t			flag;
1620Sstevel@tonic-gate 	char			*rootpath;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	dprintf("verbosemode %s\n", opts.o_verbose ? "on" : "off");
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/* determine what info we need to get from kernel */
1670Sstevel@tonic-gate 	flag = DINFOSUBTREE;
1680Sstevel@tonic-gate 	rootpath = "/";
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (opts.o_target) {
1710Sstevel@tonic-gate 		flag |= (DINFOMINOR | DINFOPATH);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (opts.o_forcecache) {
1754453Scth 		if (dbg.d_forceload) {
1760Sstevel@tonic-gate 			exit(_error(NULL, "option combination not supported"));
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 		if (strcmp(rootpath, "/") != 0) {
1790Sstevel@tonic-gate 			exit(_error(NULL, "invalid root path for option"));
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		flag = DINFOCACHE;
1824453Scth 	} else if (opts.o_verbose) {
1834453Scth 		flag |= (DINFOPROP | DINFOMINOR |
1844453Scth 		    DINFOPRIVDATA | DINFOPATH | DINFOLYR);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (dbg.d_forceload) {
1880Sstevel@tonic-gate 		flag |= DINFOFORCE;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (opts.o_verbose) {
1920Sstevel@tonic-gate 		init_priv_data(&fetch);
1930Sstevel@tonic-gate 		root_node = di_init_impl(rootpath, flag, &fetch);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		/* get devlink (aka aliases) data */
1960Sstevel@tonic-gate 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL)
1970Sstevel@tonic-gate 			exit(_error("di_devlink_init() failed."));
1980Sstevel@tonic-gate 	} else
1990Sstevel@tonic-gate 		root_node = di_init(rootpath, flag);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (root_node == DI_NODE_NIL) {
2020Sstevel@tonic-gate 		(void) _error(NULL, "devinfo facility not available");
2030Sstevel@tonic-gate 		/* not an error if this isn't the global zone */
2040Sstevel@tonic-gate 		if (getzoneid() == GLOBAL_ZONEID)
2050Sstevel@tonic-gate 			exit(-1);
2060Sstevel@tonic-gate 		else
2070Sstevel@tonic-gate 			exit(0);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/*
2110Sstevel@tonic-gate 	 * ...and walk all nodes to report them out...
2120Sstevel@tonic-gate 	 */
2130Sstevel@tonic-gate 	if (dbg.d_bydriver) {
2140Sstevel@tonic-gate 		opts.o_target = 0;
2150Sstevel@tonic-gate 		walk_driver(root_node, devlink_hdl);
2160Sstevel@tonic-gate 		if (devlink_hdl != NULL)
2170Sstevel@tonic-gate 			(void) di_devlink_fini(&devlink_hdl);
2180Sstevel@tonic-gate 		di_fini(root_node);
2190Sstevel@tonic-gate 		return;
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (opts.o_target) {
2230Sstevel@tonic-gate 		di_node_t target_node, node;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		target_node = find_target_node(root_node);
2260Sstevel@tonic-gate 		if (target_node == DI_NODE_NIL) {
2270Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: "
2284453Scth 			    "invalid device path specified\n",
2294453Scth 			    opts.o_progname);
2300Sstevel@tonic-gate 			exit(1);
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		/* mark the target node so we display it */
2340Sstevel@tonic-gate 		node_display_set(target_node);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		if (opts.o_ancestors) {
2370Sstevel@tonic-gate 			/*
2380Sstevel@tonic-gate 			 * mark the ancestors of this node so we display
2390Sstevel@tonic-gate 			 * them as well
2400Sstevel@tonic-gate 			 */
2410Sstevel@tonic-gate 			node = target_node;
2420Sstevel@tonic-gate 			while (node = di_parent_node(node))
2430Sstevel@tonic-gate 				node_display_set(node);
2440Sstevel@tonic-gate 		} else {
2450Sstevel@tonic-gate 			/*
2460Sstevel@tonic-gate 			 * when we display device tree nodes the indentation
2470Sstevel@tonic-gate 			 * level is based off of tree depth.
2480Sstevel@tonic-gate 			 *
2490Sstevel@tonic-gate 			 * here we increment o_target to reflect the
2500Sstevel@tonic-gate 			 * depth of the target node in the tree.  we do
2510Sstevel@tonic-gate 			 * this so that when we calculate the indentation
2520Sstevel@tonic-gate 			 * level we can subtract o_target so that the
2530Sstevel@tonic-gate 			 * target node starts with an indentation of zero.
2540Sstevel@tonic-gate 			 */
2550Sstevel@tonic-gate 			node = target_node;
2560Sstevel@tonic-gate 			while (node = di_parent_node(node))
2570Sstevel@tonic-gate 				opts.o_target++;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		if (opts.o_children) {
2610Sstevel@tonic-gate 			/*
2620Sstevel@tonic-gate 			 * mark the children of this node so we display
2630Sstevel@tonic-gate 			 * them as well
2640Sstevel@tonic-gate 			 */
2650Sstevel@tonic-gate 			(void) di_walk_node(target_node, DI_WALK_CLDFIRST,
2664453Scth 			    (void *)1,
2674453Scth 			    (int (*)(di_node_t, void *))
2684453Scth 			    node_display_set);
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, devlink_hdl,
2734453Scth 	    dump_devs);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (devlink_hdl != NULL)
2760Sstevel@tonic-gate 		(void) di_devlink_fini(&devlink_hdl);
2770Sstevel@tonic-gate 	di_fini(root_node);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * utility routines
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate static int
2840Sstevel@tonic-gate i_find_target_node(di_node_t node, void *arg)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	di_node_t *target = (di_node_t *)arg;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (opts.o_devices_path != NULL) {
2890Sstevel@tonic-gate 		char *path;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		if ((path = di_devfs_path(node)) == NULL)
2920Sstevel@tonic-gate 			exit(_error("failed to allocate memory"));
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		if (strcmp(opts.o_devices_path, path) == 0) {
2950Sstevel@tonic-gate 			di_devfs_path_free(path);
2960Sstevel@tonic-gate 			*target = node;
2970Sstevel@tonic-gate 			return (DI_WALK_TERMINATE);
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		di_devfs_path_free(path);
3010Sstevel@tonic-gate 	} else if (opts.o_devt != DDI_DEV_T_NONE) {
3020Sstevel@tonic-gate 		di_minor_t	minor = DI_MINOR_NIL;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
3050Sstevel@tonic-gate 			if (opts.o_devt == di_minor_devt(minor)) {
3060Sstevel@tonic-gate 				*target = node;
3070Sstevel@tonic-gate 				return (DI_WALK_TERMINATE);
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 	} else {
3110Sstevel@tonic-gate 		/* we should never get here */
3120Sstevel@tonic-gate 		exit(_error(NULL, "internal error"));
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate static di_node_t
3180Sstevel@tonic-gate find_target_node(di_node_t root_node)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	di_node_t target = DI_NODE_NIL;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* special case to allow displaying of the root node */
3230Sstevel@tonic-gate 	if (opts.o_devices_path != NULL) {
3240Sstevel@tonic-gate 		if (strlen(opts.o_devices_path) == 0)
3250Sstevel@tonic-gate 			return (root_node);
3260Sstevel@tonic-gate 		if (strcmp(opts.o_devices_path, ".") == 0)
3270Sstevel@tonic-gate 			return (root_node);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	(void) di_walk_node(root_node, DI_WALK_CLDFIRST, &target,
3314453Scth 	    i_find_target_node);
3320Sstevel@tonic-gate 	return (target);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate #define	NODE_DISPLAY		(1<<0)
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate static long
3380Sstevel@tonic-gate node_display(di_node_t node)
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate 	long data = (long)di_node_private_get(node);
3410Sstevel@tonic-gate 	return (data & NODE_DISPLAY);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate static void
3450Sstevel@tonic-gate node_display_set(di_node_t node)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	long data = (long)di_node_private_get(node);
3480Sstevel@tonic-gate 	data |= NODE_DISPLAY;
3490Sstevel@tonic-gate 	di_node_private_set(node, (void *)data);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate #define	LNODE_DISPLAYED		(1<<0)
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static long
3550Sstevel@tonic-gate lnode_displayed(di_lnode_t lnode)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
3580Sstevel@tonic-gate 	return (data & LNODE_DISPLAYED);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate static void
3620Sstevel@tonic-gate lnode_displayed_set(di_lnode_t lnode)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
3650Sstevel@tonic-gate 	data |= LNODE_DISPLAYED;
3660Sstevel@tonic-gate 	di_lnode_private_set(lnode, (void *)data);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static void
3700Sstevel@tonic-gate lnode_displayed_clear(di_lnode_t lnode)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	long data = (long)di_lnode_private_get(lnode);
3730Sstevel@tonic-gate 	data &= ~LNODE_DISPLAYED;
3740Sstevel@tonic-gate 	di_lnode_private_set(lnode, (void *)data);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate #define	MINOR_DISPLAYED		(1<<0)
3780Sstevel@tonic-gate #define	MINOR_PTR		(~(0x3))
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate static long
3810Sstevel@tonic-gate minor_displayed(di_minor_t minor)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
3840Sstevel@tonic-gate 	return (data & MINOR_DISPLAYED);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate static void
3880Sstevel@tonic-gate minor_displayed_set(di_minor_t minor)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
3910Sstevel@tonic-gate 	data |= MINOR_DISPLAYED;
3920Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate static void
3960Sstevel@tonic-gate minor_displayed_clear(di_minor_t minor)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
3990Sstevel@tonic-gate 	data &= ~MINOR_DISPLAYED;
4000Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate static void *
4040Sstevel@tonic-gate minor_ptr(di_minor_t minor)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
4070Sstevel@tonic-gate 	return ((void *)(data & MINOR_PTR));
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate static void
4110Sstevel@tonic-gate minor_ptr_set(di_minor_t minor, void *ptr)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	long data = (long)di_minor_private_get(minor);
4140Sstevel@tonic-gate 	data = (data & ~MINOR_PTR) | (((long)ptr) & MINOR_PTR);
4150Sstevel@tonic-gate 	di_minor_private_set(minor, (void *)data);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate /*
4190Sstevel@tonic-gate  * In this comment typed properties are those of type DI_PROP_TYPE_UNDEF_IT,
4200Sstevel@tonic-gate  * DI_PROP_TYPE_BOOLEAN, DI_PROP_TYPE_INT, DI_PROP_TYPE_INT64,
4210Sstevel@tonic-gate  * DI_PROP_TYPE_BYTE, and DI_PROP_TYPE_STRING.
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * The guessing algorithm is:
4240Sstevel@tonic-gate  * 1. If the property is typed and the type is consistent with the value of
4250Sstevel@tonic-gate  *    the property, then the property is of that type. If the type is not
4260Sstevel@tonic-gate  *    consistent with value of the property, then the type is treated as
4270Sstevel@tonic-gate  *    alien to prtconf.
4280Sstevel@tonic-gate  * 2. If the property is of type DI_PROP_TYPE_UNKNOWN the following steps
4290Sstevel@tonic-gate  *    are carried out.
4300Sstevel@tonic-gate  *    a. If the value of the property is consistent with a string property,
4310Sstevel@tonic-gate  *       the type of the property is DI_PROP_TYPE_STRING.
4320Sstevel@tonic-gate  *    b. Otherwise, if the value of the property is consistent with an integer
4330Sstevel@tonic-gate  *       property, the type of the property is DI_PROP_TYPE_INT.
4340Sstevel@tonic-gate  *    c. Otherwise, the property type is treated as alien to prtconf.
4350Sstevel@tonic-gate  * 3. If the property type is alien to prtconf, then the property value is
4360Sstevel@tonic-gate  *    read by the appropriate routine for untyped properties and the following
4370Sstevel@tonic-gate  *    steps are carried out.
4380Sstevel@tonic-gate  *    a. If the length that the property routine returned is zero, the
4390Sstevel@tonic-gate  *       property is of type DI_PROP_TYPE_BOOLEAN.
4400Sstevel@tonic-gate  *    b. Otherwise, if the length that the property routine returned is
4410Sstevel@tonic-gate  *       positive, then the property value is treated as raw data of type
4420Sstevel@tonic-gate  *       DI_PROP_TYPE_UNKNOWN.
4430Sstevel@tonic-gate  *    c. Otherwise, if the length that the property routine returned is
4440Sstevel@tonic-gate  *       negative, then there is some internal inconsistency and this is
4450Sstevel@tonic-gate  *       treated as an error and no type is determined.
4460Sstevel@tonic-gate  */
4470Sstevel@tonic-gate static int
4480Sstevel@tonic-gate prop_type_guess(const dumpops_t *propops, void *prop, void **prop_data,
4490Sstevel@tonic-gate     int *prop_type)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	int len, type;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	type = PROPTYPE(propops)(prop);
4540Sstevel@tonic-gate 	switch (type) {
4550Sstevel@tonic-gate 	case DI_PROP_TYPE_UNDEF_IT:
4560Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
4570Sstevel@tonic-gate 		*prop_data = NULL;
4580Sstevel@tonic-gate 		*prop_type = type;
4590Sstevel@tonic-gate 		return (0);
4600Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
4610Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
4620Sstevel@tonic-gate 		break;
4630Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
4640Sstevel@tonic-gate 		len = PROPINT64(propops)(prop, (int64_t **)prop_data);
4650Sstevel@tonic-gate 		break;
4660Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
4670Sstevel@tonic-gate 		len = PROPBYTES(propops)(prop, (uchar_t **)prop_data);
4680Sstevel@tonic-gate 		break;
4690Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
4700Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
4730Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
4740Sstevel@tonic-gate 		if ((len > 0) && ((*(char **)prop_data)[0] != 0)) {
4750Sstevel@tonic-gate 			*prop_type = DI_PROP_TYPE_STRING;
4760Sstevel@tonic-gate 			return (len);
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
4800Sstevel@tonic-gate 		type = DI_PROP_TYPE_INT;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		break;
4830Sstevel@tonic-gate 	default:
4840Sstevel@tonic-gate 		len = -1;
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (len > 0) {
4880Sstevel@tonic-gate 		*prop_type = type;
4890Sstevel@tonic-gate 		return (len);
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	len = PROPRAWDATA(propops)(prop, (uchar_t **)prop_data);
4930Sstevel@tonic-gate 	if (len < 0) {
4940Sstevel@tonic-gate 		return (-1);
4950Sstevel@tonic-gate 	} else if (len == 0) {
4960Sstevel@tonic-gate 		*prop_type = DI_PROP_TYPE_BOOLEAN;
4970Sstevel@tonic-gate 		return (0);
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	*prop_type = DI_PROP_TYPE_UNKNOWN;
5010Sstevel@tonic-gate 	return (len);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
504*7224Scth /*
505*7224Scth  * Returns 0 if nothing is printed, 1 otherwise
506*7224Scth  */
507*7224Scth static int
508*7224Scth dump_prop_list(const dumpops_t *dumpops, const char *name, int ilev,
509*7224Scth     void *node, dev_t dev)
5100Sstevel@tonic-gate {
511*7224Scth 	void		*prop = DI_PROP_NIL, *prop_data;
512*7224Scth 	di_minor_t	minor;
513*7224Scth 	char		*p;
514*7224Scth 	int		i, prop_type, nitems;
515*7224Scth 	dev_t		pdev;
516*7224Scth 	int		nprop = 0;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	while ((prop = NEXTPROP(dumpops)(node, prop)) != DI_PROP_NIL) {
519*7224Scth 
520*7224Scth 		/* Skip properties a dev_t oriented caller is not requesting */
521*7224Scth 		if (PROPDEVT(dumpops)) {
522*7224Scth 			pdev = PROPDEVT(dumpops)(prop);
523*7224Scth 
524*7224Scth 			if (dev == DDI_DEV_T_ANY) {
525*7224Scth 				/*
526*7224Scth 				 * Caller requesting print all properties
527*7224Scth 				 */
528*7224Scth 				goto print;
529*7224Scth 			} else if (dev == DDI_DEV_T_NONE) {
530*7224Scth 				/*
531*7224Scth 				 * Caller requesting print of properties
532*7224Scth 				 * associated with devinfo (not minor).
533*7224Scth 				 */
534*7224Scth 				if ((pdev == DDI_DEV_T_ANY) ||
535*7224Scth 				    (pdev == DDI_DEV_T_NONE))
536*7224Scth 					goto print;
537*7224Scth 
538*7224Scth 				/*
539*7224Scth 				 * Property has a minor association, see if
540*7224Scth 				 * we have a minor with this dev_t. If there
541*7224Scth 				 * is no such minor we print the property now
542*7224Scth 				 * so it gets displayed.
543*7224Scth 				 */
544*7224Scth 				minor = DI_MINOR_NIL;
545*7224Scth 				while ((minor = di_minor_next((di_node_t)node,
546*7224Scth 				    minor)) != DI_MINOR_NIL) {
547*7224Scth 					if (di_minor_devt(minor) == pdev)
548*7224Scth 						break;
549*7224Scth 				}
550*7224Scth 				if (minor == DI_MINOR_NIL)
551*7224Scth 					goto print;
552*7224Scth 			} else if (dev == pdev) {
553*7224Scth 				/*
554*7224Scth 				 * Caller requesting print of properties
555*7224Scth 				 * associated with a specific matching minor
556*7224Scth 				 * node.
557*7224Scth 				 */
558*7224Scth 				goto print;
559*7224Scth 			}
560*7224Scth 
561*7224Scth 			/* otherwise skip print */
562*7224Scth 			continue;
563*7224Scth 		}
564*7224Scth 
565*7224Scth print:		nitems = prop_type_guess(dumpops, prop, &prop_data, &prop_type);
5660Sstevel@tonic-gate 		if (nitems < 0)
5670Sstevel@tonic-gate 			continue;
5680Sstevel@tonic-gate 
569*7224Scth 		if (nprop == 0) {
570*7224Scth 			if (name) {
571*7224Scth 				indent_to_level(ilev);
572*7224Scth 				(void) printf("%s properties:\n", name);
573*7224Scth 			}
574*7224Scth 			ilev++;
575*7224Scth 		}
576*7224Scth 		nprop++;
577*7224Scth 
5780Sstevel@tonic-gate 		indent_to_level(ilev);
5790Sstevel@tonic-gate 		(void) printf("name='%s' type=", PROPNAME(dumpops)(prop));
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		switch (prop_type) {
5820Sstevel@tonic-gate 		case DI_PROP_TYPE_UNDEF_IT:
5830Sstevel@tonic-gate 			(void) printf("undef");
5840Sstevel@tonic-gate 			break;
5850Sstevel@tonic-gate 		case DI_PROP_TYPE_BOOLEAN:
5860Sstevel@tonic-gate 			(void) printf("boolean");
5870Sstevel@tonic-gate 			break;
5880Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
5890Sstevel@tonic-gate 			(void) printf("int");
5900Sstevel@tonic-gate 			break;
5910Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
5920Sstevel@tonic-gate 			(void) printf("int64");
5930Sstevel@tonic-gate 			break;
5940Sstevel@tonic-gate 		case DI_PROP_TYPE_BYTE:
5950Sstevel@tonic-gate 			(void) printf("byte");
5960Sstevel@tonic-gate 			break;
5970Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
5980Sstevel@tonic-gate 			(void) printf("string");
5990Sstevel@tonic-gate 			break;
6000Sstevel@tonic-gate 		case DI_PROP_TYPE_UNKNOWN:
6010Sstevel@tonic-gate 			(void) printf("unknown");
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		default:
6040Sstevel@tonic-gate 			/* Should never be here */
6050Sstevel@tonic-gate 			(void) printf("0x%x", prop_type);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		if (nitems != 0)
6090Sstevel@tonic-gate 			(void) printf(" items=%i", nitems);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		/* print the major and minor numbers for a device property */
612*7224Scth 		if (PROPDEVT(dumpops)) {
613*7224Scth 			if ((pdev == DDI_DEV_T_NONE) ||
614*7224Scth 			    (pdev == DDI_DEV_T_ANY)) {
615*7224Scth 				(void) printf(" dev=none");
616*7224Scth 			} else {
6170Sstevel@tonic-gate 				(void) printf(" dev=(%u,%u)",
618*7224Scth 				    (uint_t)major(pdev), (uint_t)minor(pdev));
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		(void) putchar('\n');
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (nitems == 0)
6250Sstevel@tonic-gate 			continue;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		indent_to_level(ilev);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		(void) printf("    value=");
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		switch (prop_type) {
6320Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
6330Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
6340Sstevel@tonic-gate 				(void) printf("%8.8x.", ((int *)prop_data)[i]);
6350Sstevel@tonic-gate 			(void) printf("%8.8x", ((int *)prop_data)[i]);
6360Sstevel@tonic-gate 			break;
6370Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
6380Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
6390Sstevel@tonic-gate 				(void) printf("%16.16llx.",
6400Sstevel@tonic-gate 				    ((long long *)prop_data)[i]);
6410Sstevel@tonic-gate 			(void) printf("%16.16llx", ((long long *)prop_data)[i]);
6420Sstevel@tonic-gate 			break;
6430Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
6440Sstevel@tonic-gate 			p = (char *)prop_data;
6450Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++) {
6460Sstevel@tonic-gate 				(void) printf("'%s' + ", p);
6470Sstevel@tonic-gate 				p += strlen(p) + 1;
6480Sstevel@tonic-gate 			}
6490Sstevel@tonic-gate 			(void) printf("'%s'", p);
6500Sstevel@tonic-gate 			break;
6510Sstevel@tonic-gate 		default:
6520Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
6530Sstevel@tonic-gate 				(void) printf("%2.2x.",
6540Sstevel@tonic-gate 				    ((uint8_t *)prop_data)[i]);
6550Sstevel@tonic-gate 			(void) printf("%2.2x", ((uint8_t *)prop_data)[i]);
6560Sstevel@tonic-gate 		}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		(void) putchar('\n');
6590Sstevel@tonic-gate 	}
660*7224Scth 
661*7224Scth 	return (nprop ? 1 : 0);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * walk_driver is a debugging facility.
6660Sstevel@tonic-gate  */
6670Sstevel@tonic-gate static void
6680Sstevel@tonic-gate walk_driver(di_node_t root, di_devlink_handle_t devlink_hdl)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	di_node_t node;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	node = di_drv_first_node(dbg.d_drivername, root);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
6750Sstevel@tonic-gate 		(void) dump_devs(node, devlink_hdl);
6760Sstevel@tonic-gate 		node = di_drv_next_node(node);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate  * print out information about this node, returns appropriate code.
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate /*ARGSUSED1*/
6840Sstevel@tonic-gate static int
6850Sstevel@tonic-gate dump_devs(di_node_t node, void *arg)
6860Sstevel@tonic-gate {
6870Sstevel@tonic-gate 	di_devlink_handle_t	devlink_hdl = (di_devlink_handle_t)arg;
6880Sstevel@tonic-gate 	int			ilev = 0;	/* indentation level */
6890Sstevel@tonic-gate 	char			*driver_name;
6900Sstevel@tonic-gate 	di_node_t		root_node, tmp;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (dbg.d_debug) {
6930Sstevel@tonic-gate 		char *path = di_devfs_path(node);
6940Sstevel@tonic-gate 		dprintf("Dump node %s\n", path);
6950Sstevel@tonic-gate 		di_devfs_path_free(path);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if (dbg.d_bydriver) {
6990Sstevel@tonic-gate 		ilev = 1;
7000Sstevel@tonic-gate 	} else {
7010Sstevel@tonic-gate 		/* figure out indentation level */
7020Sstevel@tonic-gate 		tmp = node;
7030Sstevel@tonic-gate 		while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
7040Sstevel@tonic-gate 			ilev++;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		if (opts.o_target && !opts.o_ancestors) {
7070Sstevel@tonic-gate 			ilev -= opts.o_target - 1;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (opts.o_target && !node_display(node)) {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * if we're only displaying certain nodes and this one
7140Sstevel@tonic-gate 		 * isn't flagged, skip it.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	indent_to_level(ilev);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	(void) printf("%s", di_node_name(node));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/*
7240Sstevel@tonic-gate 	 * if this node does not have an instance number or is the
7250Sstevel@tonic-gate 	 * root node (1229946), we don't print an instance number
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	root_node = tmp = node;
7280Sstevel@tonic-gate 	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
7290Sstevel@tonic-gate 		root_node = tmp;
7300Sstevel@tonic-gate 	if ((di_instance(node) >= 0) && (node != root_node))
7310Sstevel@tonic-gate 		(void) printf(", instance #%d", di_instance(node));
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (opts.o_drv_name) {
7340Sstevel@tonic-gate 		driver_name = di_driver_name(node);
7350Sstevel@tonic-gate 		if (driver_name != NULL)
7360Sstevel@tonic-gate 			(void) printf(" (driver name: %s)", driver_name);
7374845Svikram 	} else if (di_retired(node)) {
7384845Svikram 		(void) printf(" (retired)");
7390Sstevel@tonic-gate 	} else if (di_state(node) & DI_DRIVER_DETACHED)
7400Sstevel@tonic-gate 		(void) printf(" (driver not attached)");
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	(void) printf("\n");
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (opts.o_verbose)  {
7450Sstevel@tonic-gate 		if (dump_prop_list(&sysprop_dumpops, "System", ilev + 1,
746*7224Scth 		    node, DDI_DEV_T_ANY)) {
7470Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops, NULL, ilev + 1,
748*7224Scth 			    node, DDI_DEV_T_ANY);
7490Sstevel@tonic-gate 		} else {
7500Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops,
751*7224Scth 			    "System software", ilev + 1, node, DDI_DEV_T_ANY);
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 		(void) dump_prop_list(&drvprop_dumpops, "Driver", ilev + 1,
754*7224Scth 		    node, DDI_DEV_T_NONE);
7550Sstevel@tonic-gate 		(void) dump_prop_list(&hwprop_dumpops, "Hardware", ilev + 1,
756*7224Scth 		    node, DDI_DEV_T_ANY);
7570Sstevel@tonic-gate 		dump_priv_data(ilev + 1, node);
7580Sstevel@tonic-gate 		dump_pathing_data(ilev + 1, node);
7590Sstevel@tonic-gate 		dump_link_data(ilev + 1, node, devlink_hdl);
7600Sstevel@tonic-gate 		dump_minor_data(ilev + 1, node, devlink_hdl);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (opts.o_target)
7640Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (!opts.o_pseudodevs && (strcmp(di_node_name(node), "pseudo") == 0))
7670Sstevel@tonic-gate 		return (DI_WALK_PRUNECHILD);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /* _error([no_perror, ] fmt [, arg ...]) */
7730Sstevel@tonic-gate static int
7740Sstevel@tonic-gate _error(const char *opt_noperror, ...)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	int saved_errno;
7770Sstevel@tonic-gate 	va_list ap;
7780Sstevel@tonic-gate 	int no_perror = 0;
7790Sstevel@tonic-gate 	const char *fmt;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	saved_errno = errno;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts.o_progname);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	va_start(ap, opt_noperror);
7860Sstevel@tonic-gate 	if (opt_noperror == NULL) {
7870Sstevel@tonic-gate 		no_perror = 1;
7880Sstevel@tonic-gate 		fmt = va_arg(ap, char *);
7890Sstevel@tonic-gate 	} else
7900Sstevel@tonic-gate 		fmt = opt_noperror;
7910Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
7920Sstevel@tonic-gate 	va_end(ap);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	if (no_perror)
7950Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
7960Sstevel@tonic-gate 	else {
7970Sstevel@tonic-gate 		(void) fprintf(stderr, ": ");
7980Sstevel@tonic-gate 		errno = saved_errno;
7990Sstevel@tonic-gate 		perror("");
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	return (-1);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate  * The rest of the routines handle printing the raw prom devinfo (-p option).
8080Sstevel@tonic-gate  *
8090Sstevel@tonic-gate  * 128 is the size of the largest (currently) property name
8100Sstevel@tonic-gate  * 16k - MAXNAMESZ - sizeof (int) is the size of the largest
8110Sstevel@tonic-gate  * (currently) property value that is allowed.
8120Sstevel@tonic-gate  * the sizeof (uint_t) is from struct openpromio
8130Sstevel@tonic-gate  */
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate #define	MAXNAMESZ	128
8160Sstevel@tonic-gate #define	MAXVALSIZE	(16384 - MAXNAMESZ - sizeof (uint_t))
8170Sstevel@tonic-gate #define	BUFSIZE		(MAXNAMESZ + MAXVALSIZE + sizeof (uint_t))
8180Sstevel@tonic-gate typedef union {
8190Sstevel@tonic-gate 	char buf[BUFSIZE];
8200Sstevel@tonic-gate 	struct openpromio opp;
8210Sstevel@tonic-gate } Oppbuf;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate static int prom_fd;
8240Sstevel@tonic-gate static uchar_t *prom_snapshot;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate static int
8270Sstevel@tonic-gate is_openprom(void)
8280Sstevel@tonic-gate {
8290Sstevel@tonic-gate 	Oppbuf	oppbuf;
8300Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
8310Sstevel@tonic-gate 	unsigned int i;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
8340Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETCONS, opp) < 0)
8350Sstevel@tonic-gate 		exit(_error("OPROMGETCONS"));
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	i = (unsigned int)((unsigned char)opp->oprom_array[0]);
8380Sstevel@tonic-gate 	return ((i & OPROMCONS_OPENPROM) == OPROMCONS_OPENPROM);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate int
8420Sstevel@tonic-gate do_prominfo(void)
8430Sstevel@tonic-gate {
8440Sstevel@tonic-gate 	uint_t arg = opts.o_verbose;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
8470Sstevel@tonic-gate 		exit(_error("openeepr device open failed"));
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	if (is_openprom() == 0)  {
8510Sstevel@tonic-gate 		(void) fprintf(stderr, "System architecture does not "
8520Sstevel@tonic-gate 		    "support this option of this command.\n");
8530Sstevel@tonic-gate 		return (1);
8540Sstevel@tonic-gate 	}
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/* OPROMSNAPSHOT returns size in arg */
8570Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMSNAPSHOT, &arg) < 0)
8580Sstevel@tonic-gate 		exit(_error("OPROMSNAPSHOT"));
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	if (arg == 0)
8610Sstevel@tonic-gate 		return (1);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if ((prom_snapshot = malloc(arg)) == NULL)
8640Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/* copy out the snapshot for printing */
8670Sstevel@tonic-gate 	/*LINTED*/
8680Sstevel@tonic-gate 	*(uint_t *)prom_snapshot = arg;
8690Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMCOPYOUT, prom_snapshot) < 0)
8700Sstevel@tonic-gate 		exit(_error("OPROMCOPYOUT"));
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	promclose();
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/* print out information */
8750Sstevel@tonic-gate 	walk(prom_snapshot, arg, 0);
8760Sstevel@tonic-gate 	free(prom_snapshot);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	return (0);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate static void
8820Sstevel@tonic-gate walk(uchar_t *buf, uint_t size, int level)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 	int error;
8850Sstevel@tonic-gate 	nvlist_t *nvl, *cnvl;
8860Sstevel@tonic-gate 	nvpair_t *child = NULL;
8870Sstevel@tonic-gate 	uchar_t *cbuf = NULL;
8880Sstevel@tonic-gate 	uint_t csize;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/* Expand to an nvlist */
8910Sstevel@tonic-gate 	if (nvlist_unpack((char *)buf, size, &nvl, 0))
8920Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/* print current node */
8950Sstevel@tonic-gate 	dump_node(nvl, level);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/* print children */
8980Sstevel@tonic-gate 	error = nvlist_lookup_byte_array(nvl, "@child", &cbuf, &csize);
8990Sstevel@tonic-gate 	if ((error == ENOENT) || (cbuf == NULL))
9000Sstevel@tonic-gate 		return;		/* no child exists */
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (error || nvlist_unpack((char *)cbuf, csize, &cnvl, 0))
9030Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	while (child = nvlist_next_nvpair(cnvl, child)) {
9060Sstevel@tonic-gate 		char *name = nvpair_name(child);
9070Sstevel@tonic-gate 		data_type_t type = nvpair_type(child);
9080Sstevel@tonic-gate 		uchar_t *nodebuf;
9090Sstevel@tonic-gate 		uint_t nodesize;
9100Sstevel@tonic-gate 		if (strcmp("node", name) != 0) {
9110Sstevel@tonic-gate 			dprintf("unexpected nvpair name %s != name\n", name);
9120Sstevel@tonic-gate 			continue;
9130Sstevel@tonic-gate 		}
9140Sstevel@tonic-gate 		if (type != DATA_TYPE_BYTE_ARRAY) {
9150Sstevel@tonic-gate 			dprintf("unexpected nvpair type %d, not byte array \n",
9160Sstevel@tonic-gate 			    type);
9170Sstevel@tonic-gate 			continue;
9180Sstevel@tonic-gate 		}
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		(void) nvpair_value_byte_array(child,
9210Sstevel@tonic-gate 		    (uchar_t **)&nodebuf, &nodesize);
9220Sstevel@tonic-gate 		walk(nodebuf, nodesize, level + 1);
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	nvlist_free(nvl);
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate  * Print all properties and values
9300Sstevel@tonic-gate  */
9310Sstevel@tonic-gate static void
9320Sstevel@tonic-gate dump_node(nvlist_t *nvl, int level)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate 	int id = 0;
9350Sstevel@tonic-gate 	char *name = NULL;
9360Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	indent_to_level(level);
9390Sstevel@tonic-gate 	(void) printf("Node");
9400Sstevel@tonic-gate 	if (!opts.o_verbose) {
9410Sstevel@tonic-gate 		if (nvlist_lookup_string(nvl, "name", &name))
9420Sstevel@tonic-gate 			(void) printf("data not available");
9430Sstevel@tonic-gate 		else
9440Sstevel@tonic-gate 			(void) printf(" '%s'", name);
9450Sstevel@tonic-gate 		(void) putchar('\n');
9460Sstevel@tonic-gate 		return;
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 	(void) nvlist_lookup_int32(nvl, "@nodeid", &id);
9490Sstevel@tonic-gate 	(void) printf(" %#08x\n", id);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	while (nvp = nvlist_next_nvpair(nvl, nvp)) {
9520Sstevel@tonic-gate 		name = nvpair_name(nvp);
9530Sstevel@tonic-gate 		if (name[0] == '@')
9540Sstevel@tonic-gate 			continue;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		print_one(nvp, level + 1);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 	(void) putchar('\n');
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate static const char *
9620Sstevel@tonic-gate path_state_name(di_path_state_t st)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	switch (st) {
9650Sstevel@tonic-gate 		case DI_PATH_STATE_ONLINE:
9660Sstevel@tonic-gate 			return ("online");
9670Sstevel@tonic-gate 		case DI_PATH_STATE_STANDBY:
9680Sstevel@tonic-gate 			return ("standby");
9690Sstevel@tonic-gate 		case DI_PATH_STATE_OFFLINE:
9700Sstevel@tonic-gate 			return ("offline");
9710Sstevel@tonic-gate 		case DI_PATH_STATE_FAULT:
9720Sstevel@tonic-gate 			return ("faulted");
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	return ("unknown");
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * Print all phci's each client is connected to.
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate static void
9810Sstevel@tonic-gate dump_pathing_data(int ilev, di_node_t node)
9820Sstevel@tonic-gate {
9836640Scth 	di_path_t	pi = DI_PATH_NIL;
9846640Scth 	di_node_t	phci_node;
9856640Scth 	char		*phci_path;
9866640Scth 	int		path_instance;
9876640Scth 	int		firsttime = 1;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (node == DI_PATH_NIL)
9900Sstevel@tonic-gate 		return;
9910Sstevel@tonic-gate 
9926640Scth 	while ((pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
9930Sstevel@tonic-gate 		if (firsttime) {
9940Sstevel@tonic-gate 			indent_to_level(ilev);
9950Sstevel@tonic-gate 			firsttime = 0;
9960Sstevel@tonic-gate 			ilev++;
9970Sstevel@tonic-gate 			(void) printf("Paths from multipath bus adapters:\n");
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 
10006640Scth 		/*
10016640Scth 		 * Print the path instance and full "pathinfo" path, which is
10026640Scth 		 * the same as the /devices devifo path had the device been
10036640Scth 		 * enumerated under pHCI.
10046640Scth 		 */
10056640Scth 		phci_node = di_path_phci_node(pi);
10066640Scth 		phci_path = di_devfs_path(phci_node);
10076640Scth 		path_instance = di_path_instance(pi);
10086640Scth 		if (phci_path) {
10096640Scth 			if (path_instance > 0) {
10106640Scth 				indent_to_level(ilev);
10116640Scth 				(void) printf("Path %d: %s/%s@%s\n",
10126640Scth 				    path_instance, phci_path,
10136640Scth 				    di_node_name(node),
10146640Scth 				    di_path_bus_addr(pi));
10156640Scth 			}
10166640Scth 			di_devfs_path_free(phci_path);
10176640Scth 		}
10186640Scth 
10196640Scth 		/* print phci driver, instance, and path state information */
10200Sstevel@tonic-gate 		indent_to_level(ilev);
10210Sstevel@tonic-gate 		(void) printf("%s#%d (%s)\n", di_driver_name(phci_node),
10220Sstevel@tonic-gate 		    di_instance(phci_node), path_state_name(di_path_state(pi)));
1023*7224Scth 		(void) dump_prop_list(&pathprop_dumpops, NULL, ilev + 1,
1024*7224Scth 		    pi, DDI_DEV_T_ANY);
10250Sstevel@tonic-gate 	}
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate static int
10290Sstevel@tonic-gate dump_minor_data_links(di_devlink_t devlink, void *arg)
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	int ilev = (intptr_t)arg;
10320Sstevel@tonic-gate 	indent_to_level(ilev);
10330Sstevel@tonic-gate 	(void) printf("dev_link=%s\n", di_devlink_path(devlink));
10340Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate static void
10380Sstevel@tonic-gate dump_minor_data_paths(int ilev, di_minor_t minor,
10390Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	char	*path, *type;
10420Sstevel@tonic-gate 	int	spec_type;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	/* get the path to the device and the minor node name */
10450Sstevel@tonic-gate 	if ((path = di_devfs_minor_path(minor)) == NULL)
10460Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	/* display the path to this minor node */
10490Sstevel@tonic-gate 	indent_to_level(ilev);
10500Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if (devlink_hdl != NULL) {
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		/* get the device minor node information */
10550Sstevel@tonic-gate 		spec_type = di_minor_spectype(minor);
10560Sstevel@tonic-gate 		switch (di_minor_type(minor)) {
10570Sstevel@tonic-gate 			case DDM_MINOR:
10580Sstevel@tonic-gate 				type = "minor";
10590Sstevel@tonic-gate 				break;
10600Sstevel@tonic-gate 			case DDM_ALIAS:
10610Sstevel@tonic-gate 				type = "alias";
10620Sstevel@tonic-gate 				break;
10630Sstevel@tonic-gate 			case DDM_DEFAULT:
10640Sstevel@tonic-gate 				type = "default";
10650Sstevel@tonic-gate 				break;
10660Sstevel@tonic-gate 			case DDM_INTERNAL_PATH:
10670Sstevel@tonic-gate 				type = "internal";
10680Sstevel@tonic-gate 				break;
10690Sstevel@tonic-gate 			default:
10700Sstevel@tonic-gate 				type = "unknown";
10710Sstevel@tonic-gate 				break;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		/* display the device minor node information */
10750Sstevel@tonic-gate 		indent_to_level(ilev + 1);
10760Sstevel@tonic-gate 		(void) printf("spectype=%s type=%s\n",
10774453Scth 		    (spec_type == S_IFBLK) ? "blk" : "chr", type);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		/* display all the devlinks for this device minor node */
10800Sstevel@tonic-gate 		(void) di_devlink_walk(devlink_hdl, NULL, path,
10810Sstevel@tonic-gate 		    0, (void *)(intptr_t)(ilev + 1), dump_minor_data_links);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	di_devfs_path_free(path);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate static void
10880Sstevel@tonic-gate create_minor_list(di_node_t node)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate 	di_minor_t	minor, minor_head, minor_tail, minor_prev, minor_walk;
10910Sstevel@tonic-gate 	int		major;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	/* if there are no minor nodes, bail */
10940Sstevel@tonic-gate 	if (di_minor_next(node, DI_MINOR_NIL) == DI_MINOR_NIL)
10950Sstevel@tonic-gate 		return;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * here we want to create lists of minor nodes with the same
10990Sstevel@tonic-gate 	 * dev_t.  to do this we first sort all the minor nodes by devt.
11000Sstevel@tonic-gate 	 *
11010Sstevel@tonic-gate 	 * the algorithm used here is a bubble sort, so performance sucks.
11020Sstevel@tonic-gate 	 * but it's probably ok here because most device instances don't
11030Sstevel@tonic-gate 	 * have that many minor nodes.  also we're doing this as we're
11040Sstevel@tonic-gate 	 * displaying each node so it doesn't look like we're pausing
11050Sstevel@tonic-gate 	 * output for a long time.
11060Sstevel@tonic-gate 	 */
11070Sstevel@tonic-gate 	major = di_driver_major(node);
11080Sstevel@tonic-gate 	minor_head = minor_tail = minor = DI_MINOR_NIL;
11090Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
11100Sstevel@tonic-gate 		dev_t	dev = di_minor_devt(minor);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		/* skip /pseudo/clone@0 minor nodes */
11130Sstevel@tonic-gate 		if (major != major(dev))
11140Sstevel@tonic-gate 			continue;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 		minor_ptr_set(minor, DI_MINOR_NIL);
11170Sstevel@tonic-gate 		if (minor_head == DI_MINOR_NIL) {
11180Sstevel@tonic-gate 			/* this is the first minor node we're looking at */
11190Sstevel@tonic-gate 			minor_head = minor_tail = minor;
11200Sstevel@tonic-gate 			continue;
11210Sstevel@tonic-gate 		}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 		/*
11240Sstevel@tonic-gate 		 * if the new dev is less than the old dev, update minor_head
11250Sstevel@tonic-gate 		 * so it points to the beginning of the list.  ie it points
11260Sstevel@tonic-gate 		 * to the node with the lowest dev value
11270Sstevel@tonic-gate 		 */
11280Sstevel@tonic-gate 		if (dev <= di_minor_devt(minor_head)) {
11290Sstevel@tonic-gate 			minor_ptr_set(minor, minor_head);
11300Sstevel@tonic-gate 			minor_head = minor;
11310Sstevel@tonic-gate 			continue;
11320Sstevel@tonic-gate 		}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 		minor_prev = minor_head;
11350Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_head);
11360Sstevel@tonic-gate 		while ((minor_walk != DI_MINOR_NIL) &&
11374453Scth 		    (dev > di_minor_devt(minor_walk))) {
11380Sstevel@tonic-gate 			minor_prev = minor_walk;
11390Sstevel@tonic-gate 			minor_walk = minor_ptr(minor_walk);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 		minor_ptr_set(minor, minor_walk);
11420Sstevel@tonic-gate 		minor_ptr_set(minor_prev, minor);
11430Sstevel@tonic-gate 		if (minor_walk == NULL)
11440Sstevel@tonic-gate 			minor_tail = minor;
11450Sstevel@tonic-gate 	}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* check if there were any non /pseudo/clone@0 nodes.  if not, bail */
11480Sstevel@tonic-gate 	if (minor_head == DI_MINOR_NIL)
11490Sstevel@tonic-gate 		return;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * now that we have a list of minor nodes sorted by devt
11530Sstevel@tonic-gate 	 * we walk through the list and break apart the entire list
11540Sstevel@tonic-gate 	 * to create circular lists of minor nodes with matching devts.
11550Sstevel@tonic-gate 	 */
11560Sstevel@tonic-gate 	minor_prev = minor_head;
11570Sstevel@tonic-gate 	minor_walk = minor_ptr(minor_head);
11580Sstevel@tonic-gate 	while (minor_walk != DI_MINOR_NIL) {
11590Sstevel@tonic-gate 		if (di_minor_devt(minor_prev) != di_minor_devt(minor_walk)) {
11600Sstevel@tonic-gate 			minor_ptr_set(minor_prev, minor_head);
11610Sstevel@tonic-gate 			minor_head = minor_walk;
11620Sstevel@tonic-gate 		}
11630Sstevel@tonic-gate 		minor_prev = minor_walk;
11640Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_walk);
11650Sstevel@tonic-gate 	}
11660Sstevel@tonic-gate 	minor_ptr_set(minor_tail, minor_head);
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate static void
11700Sstevel@tonic-gate link_lnode_disp(di_link_t link, uint_t endpoint, int ilev,
11710Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	di_lnode_t	lnode;
11740Sstevel@tonic-gate 	char		*name, *path;
11750Sstevel@tonic-gate 	int		displayed_path, spec_type;
11760Sstevel@tonic-gate 	di_node_t	node = DI_NODE_NIL;
11770Sstevel@tonic-gate 	dev_t		devt = DDI_DEV_T_NONE;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	lnode = di_link_to_lnode(link, endpoint);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	indent_to_level(ilev);
11820Sstevel@tonic-gate 	name = di_lnode_name(lnode);
11830Sstevel@tonic-gate 	spec_type = di_link_spectype(link);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	(void) printf("mod=%s", name);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	/*
11880Sstevel@tonic-gate 	 * if we're displaying the source of a link, we should display
11890Sstevel@tonic-gate 	 * the target access mode.  (either block or char.)
11900Sstevel@tonic-gate 	 */
11910Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC)
11920Sstevel@tonic-gate 		(void) printf(" accesstype=%s",
11930Sstevel@tonic-gate 		    (spec_type == S_IFBLK) ? "blk" : "chr");
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	/*
11960Sstevel@tonic-gate 	 * check if the lnode is bound to a specific device
11970Sstevel@tonic-gate 	 * minor node (i.e.  if it's bound to a dev_t) and
11980Sstevel@tonic-gate 	 * if so display the dev_t value and any possible
11990Sstevel@tonic-gate 	 * minor node pathing information.
12000Sstevel@tonic-gate 	 */
12010Sstevel@tonic-gate 	displayed_path = 0;
12020Sstevel@tonic-gate 	if (di_lnode_devt(lnode, &devt) == 0) {
12030Sstevel@tonic-gate 		di_minor_t	minor = DI_MINOR_NIL;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		(void) printf(" dev=(%u,%u)\n",
12060Sstevel@tonic-gate 		    (uint_t)major(devt), (uint_t)minor(devt));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		/* display paths to the src devt minor node */
12090Sstevel@tonic-gate 		while (minor = di_minor_next(node, minor)) {
12100Sstevel@tonic-gate 			if (devt != di_minor_devt(minor))
12110Sstevel@tonic-gate 				continue;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			if ((endpoint == DI_LINK_TGT) &&
12140Sstevel@tonic-gate 			    (spec_type != di_minor_spectype(minor)))
12150Sstevel@tonic-gate 				continue;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor, devlink_hdl);
12180Sstevel@tonic-gate 			displayed_path = 1;
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 	} else {
12210Sstevel@tonic-gate 		(void) printf("\n");
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (displayed_path)
12250Sstevel@tonic-gate 		return;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	/*
12280Sstevel@tonic-gate 	 * This device lnode is not did not have any minor node
12290Sstevel@tonic-gate 	 * pathing information so display the path to device node.
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	node = di_lnode_devinfo(lnode);
12320Sstevel@tonic-gate 	if ((path = di_devfs_path(node)) == NULL)
12330Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	indent_to_level(ilev + 1);
12360Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
12370Sstevel@tonic-gate 	di_devfs_path_free(path);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate static void
12410Sstevel@tonic-gate dump_minor_link_data(int ilev, di_node_t node, dev_t devt,
12420Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	int		first = 1;
12450Sstevel@tonic-gate 	di_link_t	link;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	link = DI_LINK_NIL;
12480Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
12490Sstevel@tonic-gate 		di_lnode_t	tgt_lnode;
12500Sstevel@tonic-gate 		dev_t		tgt_devt = DDI_DEV_T_NONE;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		tgt_lnode = di_link_to_lnode(link, DI_LINK_TGT);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		if (di_lnode_devt(tgt_lnode, &tgt_devt) != 0)
12550Sstevel@tonic-gate 			continue;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 		if (devt != tgt_devt)
12580Sstevel@tonic-gate 			continue;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		if (first) {
12610Sstevel@tonic-gate 			first = 0;
12620Sstevel@tonic-gate 			indent_to_level(ilev);
12630Sstevel@tonic-gate 			(void) printf("Device Minor Layered Under:\n");
12640Sstevel@tonic-gate 		}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 		/* displayed this lnode */
12670Sstevel@tonic-gate 		lnode_displayed_set(tgt_lnode);
12680Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_SRC, ilev + 1, devlink_hdl);
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	link = DI_LINK_NIL;
12720Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
12730Sstevel@tonic-gate 		di_lnode_t	src_lnode;
12740Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != 0)
12790Sstevel@tonic-gate 			continue;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		if (devt != src_devt)
12820Sstevel@tonic-gate 			continue;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 		if (first) {
12850Sstevel@tonic-gate 			first = 0;
12860Sstevel@tonic-gate 			indent_to_level(ilev);
12870Sstevel@tonic-gate 			(void) printf("Device Minor Layered Over:\n");
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 		/* displayed this lnode */
12910Sstevel@tonic-gate 		lnode_displayed_set(src_lnode);
12920Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate static void
12970Sstevel@tonic-gate dump_minor_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
12980Sstevel@tonic-gate {
12990Sstevel@tonic-gate 	di_minor_t	minor, minor_next;
13000Sstevel@tonic-gate 	di_lnode_t	lnode;
13010Sstevel@tonic-gate 	di_link_t	link;
13020Sstevel@tonic-gate 	int		major, firstminor = 1;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/*
13050Sstevel@tonic-gate 	 * first go through and mark all lnodes and minor nodes for this
13060Sstevel@tonic-gate 	 * node as undisplayed
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	lnode = DI_LNODE_NIL;
13090Sstevel@tonic-gate 	while (lnode = di_lnode_next(node, lnode))
13100Sstevel@tonic-gate 		lnode_displayed_clear(lnode);
13110Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
13120Sstevel@tonic-gate 	while (minor = di_minor_next(node, minor)) {
13130Sstevel@tonic-gate 		minor_displayed_clear(minor);
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13170Sstevel@tonic-gate 	 * when we display the minor nodes we want to coalesce nodes
13180Sstevel@tonic-gate 	 * that have the same dev_t.  we do this by creating circular
13190Sstevel@tonic-gate 	 * lists of minor nodes with the same devt.
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	create_minor_list(node);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	/* now we display the driver defined minor nodes */
13240Sstevel@tonic-gate 	major = di_driver_major(node);
13250Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
13260Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
13270Sstevel@tonic-gate 		dev_t	devt;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		/*
13300Sstevel@tonic-gate 		 * skip /pseudo/clone@0 minor nodes.
13310Sstevel@tonic-gate 		 * these are only created for DLPIv2 network devices.
13320Sstevel@tonic-gate 		 * since these minor nodes are associated with a driver
13330Sstevel@tonic-gate 		 * and are only bound to a device instance after they
13340Sstevel@tonic-gate 		 * are opened and attached we don't print them out
13350Sstevel@tonic-gate 		 * here.
13360Sstevel@tonic-gate 		 */
13370Sstevel@tonic-gate 		devt = di_minor_devt(minor);
13380Sstevel@tonic-gate 		if (major != major(devt))
13390Sstevel@tonic-gate 			continue;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		/* skip nodes that may have already been displayed */
13420Sstevel@tonic-gate 		if (minor_displayed(minor))
13430Sstevel@tonic-gate 			continue;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		if (firstminor) {
13460Sstevel@tonic-gate 			firstminor = 0;
13470Sstevel@tonic-gate 			indent_to_level(ilev++);
13480Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 		/* display the device minor node information */
13520Sstevel@tonic-gate 		indent_to_level(ilev);
13530Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
13544453Scth 		    (uint_t)major(devt), (uint_t)minor(devt));
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		minor_next = minor;
13570Sstevel@tonic-gate 		do {
13580Sstevel@tonic-gate 			/* display device minor node path info */
13590Sstevel@tonic-gate 			minor_displayed_set(minor_next);
13600Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor_next,
13614453Scth 			    devlink_hdl);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 			/* get a pointer to the next node */
13640Sstevel@tonic-gate 			minor_next = minor_ptr(minor_next);
13650Sstevel@tonic-gate 		} while (minor_next != minor);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 		/* display who has this device minor node open */
13680Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
1369*7224Scth 
1370*7224Scth 		/* display properties associated with this devt */
1371*7224Scth 		(void) dump_prop_list(&drvprop_dumpops, "Minor",
1372*7224Scth 		    ilev + 1, node, devt);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	/*
13760Sstevel@tonic-gate 	 * now go through all the target lnodes for this node and
13770Sstevel@tonic-gate 	 * if they haven't yet been displayed, display them now.
13780Sstevel@tonic-gate 	 *
13790Sstevel@tonic-gate 	 * this happens in the case of clone opens when an "official"
13800Sstevel@tonic-gate 	 * minor node does not exist for the opened devt
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	link = DI_LINK_NIL;
13830Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
13840Sstevel@tonic-gate 		dev_t		devt;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		lnode = di_link_to_lnode(link, DI_LINK_TGT);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		/* if we've already displayed this target lnode, skip it */
13890Sstevel@tonic-gate 		if (lnode_displayed(lnode))
13900Sstevel@tonic-gate 			continue;
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 		if (firstminor) {
13930Sstevel@tonic-gate 			firstminor = 0;
13940Sstevel@tonic-gate 			indent_to_level(ilev++);
13950Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 		/* display the device minor node information */
13990Sstevel@tonic-gate 		indent_to_level(ilev);
14000Sstevel@tonic-gate 		(void) di_lnode_devt(lnode, &devt);
14010Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
14024453Scth 		    (uint_t)major(devt), (uint_t)minor(devt));
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 		indent_to_level(ilev + 1);
14050Sstevel@tonic-gate 		(void) printf("dev_path=<clone>\n");
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 		/* display who has this cloned device minor node open */
14080Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 		/* mark node as displayed */
14110Sstevel@tonic-gate 		lnode_displayed_set(lnode);
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate static void
14160Sstevel@tonic-gate dump_link_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	int		first = 1;
14190Sstevel@tonic-gate 	di_link_t	link;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	link = DI_LINK_NIL;
14220Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
14230Sstevel@tonic-gate 		di_lnode_t	src_lnode;
14240Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 		/*
14290Sstevel@tonic-gate 		 * here we only want to print out layering information
14300Sstevel@tonic-gate 		 * if we are the source and our source lnode is not
14310Sstevel@tonic-gate 		 * associated with any particular dev_t.  (which means
14320Sstevel@tonic-gate 		 * we won't display this link while dumping minor node
14330Sstevel@tonic-gate 		 * info.)
14340Sstevel@tonic-gate 		 */
14350Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != -1)
14360Sstevel@tonic-gate 			continue;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 		if (first) {
14390Sstevel@tonic-gate 			first = 0;
14400Sstevel@tonic-gate 			indent_to_level(ilev);
14410Sstevel@tonic-gate 			(void) printf("Device Layered Over:\n");
14420Sstevel@tonic-gate 		}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 		/* displayed this lnode */
14450Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate }
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate /*
14500Sstevel@tonic-gate  * certain 'known' property names may contain 'composite' strings.
14510Sstevel@tonic-gate  * Handle them here, and print them as 'string1' + 'string2' ...
14520Sstevel@tonic-gate  */
14530Sstevel@tonic-gate static int
14540Sstevel@tonic-gate print_composite_string(const char *var, char *value, int size)
14550Sstevel@tonic-gate {
14560Sstevel@tonic-gate 	char *p, *q;
14570Sstevel@tonic-gate 	char *firstp;
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 	if ((strcmp(var, "version") != 0) &&
14600Sstevel@tonic-gate 	    (strcmp(var, "compatible") != 0))
14610Sstevel@tonic-gate 		return (0);	/* Not a known composite string */
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	/*
14640Sstevel@tonic-gate 	 * Verify that each string in the composite string is non-NULL,
14650Sstevel@tonic-gate 	 * is within the bounds of the property length, and contains
14660Sstevel@tonic-gate 	 * printable characters or white space. Otherwise let the
14670Sstevel@tonic-gate 	 * caller deal with it.
14680Sstevel@tonic-gate 	 */
14690Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
14700Sstevel@tonic-gate 		if (strlen(p) == 0)
14710Sstevel@tonic-gate 			return (0);		/* NULL string */
14720Sstevel@tonic-gate 		for (q = p; *q; q++) {
14730Sstevel@tonic-gate 			if (!(isascii(*q) && (isprint(*q) || isspace(*q))))
14740Sstevel@tonic-gate 				return (0);	/* Not printable or space */
14750Sstevel@tonic-gate 		}
14760Sstevel@tonic-gate 		if (q > (firstp + size))
14770Sstevel@tonic-gate 			return (0);		/* Out of bounds */
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
14810Sstevel@tonic-gate 		if (p == firstp)
14820Sstevel@tonic-gate 			(void) printf("'%s'", p);
14830Sstevel@tonic-gate 		else
14840Sstevel@tonic-gate 			(void) printf(" + '%s'", p);
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 	(void) putchar('\n');
14870Sstevel@tonic-gate 	return (1);
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Print one property and its value. Handle the verbose case.
14920Sstevel@tonic-gate  */
14930Sstevel@tonic-gate static void
14940Sstevel@tonic-gate print_one(nvpair_t *nvp, int level)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	int i;
14970Sstevel@tonic-gate 	int endswap = 0;
14980Sstevel@tonic-gate 	uint_t valsize;
14990Sstevel@tonic-gate 	char *value;
15000Sstevel@tonic-gate 	char *var = nvpair_name(nvp);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	indent_to_level(level);
15030Sstevel@tonic-gate 	(void) printf("%s: ", var);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	switch (nvpair_type(nvp)) {
15060Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
15070Sstevel@tonic-gate 		(void) printf(" \n");
15080Sstevel@tonic-gate 		return;
15090Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
15100Sstevel@tonic-gate 		if (nvpair_value_byte_array(nvp, (uchar_t **)&value,
15110Sstevel@tonic-gate 		    &valsize)) {
15120Sstevel@tonic-gate 			(void) printf("data not available.\n");
15130Sstevel@tonic-gate 			return;
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 		valsize--;	/* take out null added by driver */
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 		/*
15180Sstevel@tonic-gate 		 * Do not print valsize > MAXVALSIZE, to be compatible
15190Sstevel@tonic-gate 		 * with old behavior. E.g. intel's eisa-nvram property
15200Sstevel@tonic-gate 		 * has a size of 65 K.
15210Sstevel@tonic-gate 		 */
15220Sstevel@tonic-gate 		if (valsize > MAXVALSIZE) {
15230Sstevel@tonic-gate 			(void) printf(" \n");
15240Sstevel@tonic-gate 			return;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 		break;
15270Sstevel@tonic-gate 	default:
15280Sstevel@tonic-gate 		(void) printf("data type unexpected.\n");
15290Sstevel@tonic-gate 		return;
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Handle printing verbosely
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	if (print_composite_string(var, value, valsize)) {
15360Sstevel@tonic-gate 		return;
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (!unprintable(value, valsize)) {
15400Sstevel@tonic-gate 		(void) printf(" '%s'\n", value);
15410Sstevel@tonic-gate 		return;
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	(void) printf(" ");
15450Sstevel@tonic-gate #ifdef	__x86
15460Sstevel@tonic-gate 	/*
15470Sstevel@tonic-gate 	 * Due to backwards compatibility constraints x86 int
15480Sstevel@tonic-gate 	 * properties are not in big-endian (ieee 1275) byte order.
15490Sstevel@tonic-gate 	 * If we have a property that is a multiple of 4 bytes,
15500Sstevel@tonic-gate 	 * let's assume it is an array of ints and print the bytes
15510Sstevel@tonic-gate 	 * in little endian order to make things look nicer for
15520Sstevel@tonic-gate 	 * the user.
15530Sstevel@tonic-gate 	 */
15540Sstevel@tonic-gate 	endswap = (valsize % 4) == 0;
15550Sstevel@tonic-gate #endif	/* __x86 */
15560Sstevel@tonic-gate 	for (i = 0; i < valsize; i++) {
15570Sstevel@tonic-gate 		int out;
15580Sstevel@tonic-gate 		if (i && (i % 4 == 0))
15590Sstevel@tonic-gate 			(void) putchar('.');
15600Sstevel@tonic-gate 		if (endswap)
15610Sstevel@tonic-gate 			out = value[i + (3 - 2 * (i % 4))] & 0xff;
15620Sstevel@tonic-gate 		else
15630Sstevel@tonic-gate 			out = value[i] & 0xff;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 		(void) printf("%02x", out);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	(void) putchar('\n');
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate static int
15710Sstevel@tonic-gate unprintable(char *value, int size)
15720Sstevel@tonic-gate {
15730Sstevel@tonic-gate 	int i;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Is this just a zero?
15770Sstevel@tonic-gate 	 */
15780Sstevel@tonic-gate 	if (size == 0 || value[0] == '\0')
15790Sstevel@tonic-gate 		return (1);
15800Sstevel@tonic-gate 	/*
15810Sstevel@tonic-gate 	 * If any character is unprintable, or if a null appears
15820Sstevel@tonic-gate 	 * anywhere except at the end of a string, the whole
15830Sstevel@tonic-gate 	 * property is "unprintable".
15840Sstevel@tonic-gate 	 */
15850Sstevel@tonic-gate 	for (i = 0; i < size; ++i) {
15860Sstevel@tonic-gate 		if (value[i] == '\0')
15870Sstevel@tonic-gate 			return (i != (size - 1));
15880Sstevel@tonic-gate 		if (!isascii(value[i]) || iscntrl(value[i]))
15890Sstevel@tonic-gate 			return (1);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 	return (0);
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate static int
15950Sstevel@tonic-gate promopen(int oflag)
15960Sstevel@tonic-gate {
15970Sstevel@tonic-gate 	for (;;)  {
15980Sstevel@tonic-gate 		if ((prom_fd = open(opts.o_promdev, oflag)) < 0)  {
15990Sstevel@tonic-gate 			if (errno == EAGAIN)   {
16000Sstevel@tonic-gate 				(void) sleep(5);
16010Sstevel@tonic-gate 				continue;
16020Sstevel@tonic-gate 			}
16030Sstevel@tonic-gate 			if (errno == ENXIO)
16040Sstevel@tonic-gate 				return (-1);
16050Sstevel@tonic-gate 			if (getzoneid() == GLOBAL_ZONEID) {
16060Sstevel@tonic-gate 				_exit(_error("cannot open %s",
16070Sstevel@tonic-gate 				    opts.o_promdev));
16080Sstevel@tonic-gate 			}
16090Sstevel@tonic-gate 			/* not an error if this isn't the global zone */
16100Sstevel@tonic-gate 			(void) _error(NULL, "openprom facility not available");
16110Sstevel@tonic-gate 			exit(0);
16120Sstevel@tonic-gate 		} else
16130Sstevel@tonic-gate 			return (0);
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate static void
16180Sstevel@tonic-gate promclose(void)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	if (close(prom_fd) < 0)
16210Sstevel@tonic-gate 		exit(_error("close error on %s", opts.o_promdev));
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate /*
16250Sstevel@tonic-gate  * Get and print the name of the frame buffer device.
16260Sstevel@tonic-gate  */
16270Sstevel@tonic-gate int
16280Sstevel@tonic-gate do_fbname(void)
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	int	retval;
16310Sstevel@tonic-gate 	char fbuf_path[MAXPATHLEN];
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	retval =  modctl(MODGETFBNAME, (caddr_t)fbuf_path);
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	if (retval == 0) {
16360Sstevel@tonic-gate 		(void) printf("%s\n", fbuf_path);
16370Sstevel@tonic-gate 	} else {
16380Sstevel@tonic-gate 		if (retval == EFAULT) {
16390Sstevel@tonic-gate 			(void) fprintf(stderr,
16400Sstevel@tonic-gate 			"Error copying fb path to userland\n");
16410Sstevel@tonic-gate 		} else {
16420Sstevel@tonic-gate 			(void) fprintf(stderr,
16430Sstevel@tonic-gate 			"Console output device is not a frame buffer\n");
16440Sstevel@tonic-gate 		}
16450Sstevel@tonic-gate 		return (1);
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 	return (0);
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate /*
16510Sstevel@tonic-gate  * Get and print the PROM version.
16520Sstevel@tonic-gate  */
16530Sstevel@tonic-gate int
16540Sstevel@tonic-gate do_promversion(void)
16550Sstevel@tonic-gate {
16560Sstevel@tonic-gate 	Oppbuf	oppbuf;
16570Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
16600Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
16610Sstevel@tonic-gate 		return (1);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
16650Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETVERSION, opp) < 0)
16660Sstevel@tonic-gate 		exit(_error("OPROMGETVERSION"));
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	(void) printf("%s\n", opp->oprom_array);
16690Sstevel@tonic-gate 	promclose();
16700Sstevel@tonic-gate 	return (0);
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate int
16740Sstevel@tonic-gate do_prom_version64(void)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate #ifdef	sparc
16770Sstevel@tonic-gate 	Oppbuf	oppbuf;
16780Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
16790Sstevel@tonic-gate 	/*LINTED*/
16800Sstevel@tonic-gate 	struct openprom_opr64 *opr = (struct openprom_opr64 *)opp->oprom_array;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	static const char msg[] =
16834453Scth 	    "NOTICE: The firmware on this system does not support the "
16844453Scth 	    "64-bit OS.\n"
16854453Scth 	    "\tPlease upgrade to at least the following version:\n"
16864453Scth 	    "\t\t%s\n\n";
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
16890Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
16900Sstevel@tonic-gate 		return (-1);
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
16940Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMREADY64, opp) < 0)
16950Sstevel@tonic-gate 		exit(_error("OPROMREADY64"));
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	if (opr->return_code == 0)
16980Sstevel@tonic-gate 		return (0);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	(void) printf(msg, opr->message);
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	promclose();
17030Sstevel@tonic-gate 	return (opr->return_code);
17040Sstevel@tonic-gate #else
17050Sstevel@tonic-gate 	return (0);
17060Sstevel@tonic-gate #endif
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate int
17100Sstevel@tonic-gate do_productinfo(void)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	di_node_t root, next_node;
17130Sstevel@tonic-gate 	di_prom_handle_t promh;
17140Sstevel@tonic-gate 	static const char *root_prop[] = { "name", "model", "banner-name",
17150Sstevel@tonic-gate 					"compatible" };
17160Sstevel@tonic-gate 	static const char *root_propv[] = { "name", "model", "banner-name",
17170Sstevel@tonic-gate 					"compatible", "idprom" };
17180Sstevel@tonic-gate 	static const char *oprom_prop[] = { "model", "version" };
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	root = di_init("/", DINFOCPYALL);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
17240Sstevel@tonic-gate 		(void) fprintf(stderr, "di_init() failed\n");
17250Sstevel@tonic-gate 		return (1);
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	promh = di_prom_init();
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	if (promh == DI_PROM_HANDLE_NIL) {
17310Sstevel@tonic-gate 		(void) fprintf(stderr, "di_prom_init() failed\n");
17320Sstevel@tonic-gate 		return (1);
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	if (opts.o_verbose) {
17360Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_propv, "root",
17374453Scth 		    NUM_ELEMENTS(root_propv));
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 		/* Get model and version properties under node "openprom" */
17400Sstevel@tonic-gate 		next_node = find_node_by_name(promh, root, "openprom");
17410Sstevel@tonic-gate 		if (next_node != DI_NODE_NIL)
17420Sstevel@tonic-gate 			dump_prodinfo(promh, next_node, oprom_prop,
17434453Scth 			    "openprom", NUM_ELEMENTS(oprom_prop));
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	} else
17460Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_prop, "root",
17474453Scth 		    NUM_ELEMENTS(root_prop));
17480Sstevel@tonic-gate 	di_prom_fini(promh);
17490Sstevel@tonic-gate 	di_fini(root);
17500Sstevel@tonic-gate 	return (0);
17510Sstevel@tonic-gate }
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate di_node_t
17540Sstevel@tonic-gate find_node_by_name(di_prom_handle_t promh, di_node_t parent,
17550Sstevel@tonic-gate 		char *node_name)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate 	di_node_t next_node;
17580Sstevel@tonic-gate 	uchar_t *prop_valp;
17590Sstevel@tonic-gate 
17604900Svb160487 	for (next_node = di_child_node(parent); next_node != DI_NODE_NIL;
17614900Svb160487 	    next_node = di_sibling_node(next_node)) {
17624900Svb160487 		int len;
17634900Svb160487 
17644900Svb160487 		len = get_propval_by_name(promh, next_node, "name", &prop_valp);
17654900Svb160487 		if ((len != -1) && (strcmp((char *)prop_valp, node_name) == 0))
17660Sstevel@tonic-gate 			return (next_node);
17670Sstevel@tonic-gate 	}
17680Sstevel@tonic-gate 	return (DI_NODE_NIL);
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate int
17730Sstevel@tonic-gate get_propval_by_name(di_prom_handle_t promh, di_node_t node, const char *name,
17740Sstevel@tonic-gate 			uchar_t **valp)
17750Sstevel@tonic-gate {
17760Sstevel@tonic-gate 	int len;
17770Sstevel@tonic-gate 	uchar_t *bufp;
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	len = di_prom_prop_lookup_bytes(promh, node, name,
17804453Scth 	    (uchar_t **)&bufp);
17810Sstevel@tonic-gate 	if (len != -1) {
17820Sstevel@tonic-gate 		*valp = (uchar_t *)malloc(len);
17830Sstevel@tonic-gate 		(void) memcpy(*valp, bufp, len);
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 	return (len);
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate static void
17900Sstevel@tonic-gate dump_prodinfo(di_prom_handle_t promh, di_node_t node, const char **propstr,
17910Sstevel@tonic-gate 		char *node_name, int num)
17920Sstevel@tonic-gate {
17930Sstevel@tonic-gate 	int out, len, index1, index, endswap = 0;
17940Sstevel@tonic-gate 	uchar_t *prop_valp;
17950Sstevel@tonic-gate 
17960Sstevel@tonic-gate 	for (index1 = 0; index1 < num; index1++) {
17970Sstevel@tonic-gate 		len = get_propval_by_name(promh, node, propstr[index1],
17984453Scth 		    &prop_valp);
17990Sstevel@tonic-gate 		if (len != -1) {
18000Sstevel@tonic-gate 			if (strcmp(node_name, "root"))
18010Sstevel@tonic-gate 				(void) printf("%s ", node_name);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 			(void) printf("%s: ", propstr[index1]);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 			if (print_composite_string((const char *)
18064453Scth 			    propstr[index1], (char *)prop_valp, len)) {
18070Sstevel@tonic-gate 				free(prop_valp);
18080Sstevel@tonic-gate 				continue;
18090Sstevel@tonic-gate 			}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 			if (!unprintable((char *)prop_valp, len)) {
18120Sstevel@tonic-gate 				(void) printf(" %s\n", (char *)prop_valp);
18130Sstevel@tonic-gate 				free(prop_valp);
18140Sstevel@tonic-gate 				continue;
18150Sstevel@tonic-gate 			}
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 			(void) printf(" ");
18180Sstevel@tonic-gate #ifdef  __x86
18190Sstevel@tonic-gate 			endswap = (len % 4) == 0;
18200Sstevel@tonic-gate #endif  /* __x86 */
18210Sstevel@tonic-gate 			for (index = 0; index < len; index++) {
18220Sstevel@tonic-gate 				if (index && (index % 4 == 0))
18230Sstevel@tonic-gate 					(void) putchar('.');
18240Sstevel@tonic-gate 				if (endswap)
18250Sstevel@tonic-gate 					out = prop_valp[index +
18264453Scth 					    (3 - 2 * (index % 4))] & 0xff;
18270Sstevel@tonic-gate 				else
18280Sstevel@tonic-gate 					out = prop_valp[index] & 0xff;
18290Sstevel@tonic-gate 				(void) printf("%02x", out);
18300Sstevel@tonic-gate 			}
18310Sstevel@tonic-gate 			(void) putchar('\n');
18320Sstevel@tonic-gate 			free(prop_valp);
18330Sstevel@tonic-gate 		}
18340Sstevel@tonic-gate 	}
18350Sstevel@tonic-gate }
1836