xref: /onnv-gate/usr/src/cmd/prtconf/pdevinfo.c (revision 4845:357e8e7542af)
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 /*
224453Scth  * Copyright 2007 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 dump_prop_list_common(const dumpops_t *, int, void *);
1320Sstevel@tonic-gate static void walk_driver(di_node_t, di_devlink_handle_t);
1330Sstevel@tonic-gate static int dump_devs(di_node_t, void *);
1340Sstevel@tonic-gate static int dump_prop_list(const dumpops_t *, const char *, int, di_node_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 /*
4200Sstevel@tonic-gate  * In this comment typed properties are those of type DI_PROP_TYPE_UNDEF_IT,
4210Sstevel@tonic-gate  * DI_PROP_TYPE_BOOLEAN, DI_PROP_TYPE_INT, DI_PROP_TYPE_INT64,
4220Sstevel@tonic-gate  * DI_PROP_TYPE_BYTE, and DI_PROP_TYPE_STRING.
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * The guessing algorithm is:
4250Sstevel@tonic-gate  * 1. If the property is typed and the type is consistent with the value of
4260Sstevel@tonic-gate  *    the property, then the property is of that type. If the type is not
4270Sstevel@tonic-gate  *    consistent with value of the property, then the type is treated as
4280Sstevel@tonic-gate  *    alien to prtconf.
4290Sstevel@tonic-gate  * 2. If the property is of type DI_PROP_TYPE_UNKNOWN the following steps
4300Sstevel@tonic-gate  *    are carried out.
4310Sstevel@tonic-gate  *    a. If the value of the property is consistent with a string property,
4320Sstevel@tonic-gate  *       the type of the property is DI_PROP_TYPE_STRING.
4330Sstevel@tonic-gate  *    b. Otherwise, if the value of the property is consistent with an integer
4340Sstevel@tonic-gate  *       property, the type of the property is DI_PROP_TYPE_INT.
4350Sstevel@tonic-gate  *    c. Otherwise, the property type is treated as alien to prtconf.
4360Sstevel@tonic-gate  * 3. If the property type is alien to prtconf, then the property value is
4370Sstevel@tonic-gate  *    read by the appropriate routine for untyped properties and the following
4380Sstevel@tonic-gate  *    steps are carried out.
4390Sstevel@tonic-gate  *    a. If the length that the property routine returned is zero, the
4400Sstevel@tonic-gate  *       property is of type DI_PROP_TYPE_BOOLEAN.
4410Sstevel@tonic-gate  *    b. Otherwise, if the length that the property routine returned is
4420Sstevel@tonic-gate  *       positive, then the property value is treated as raw data of type
4430Sstevel@tonic-gate  *       DI_PROP_TYPE_UNKNOWN.
4440Sstevel@tonic-gate  *    c. Otherwise, if the length that the property routine returned is
4450Sstevel@tonic-gate  *       negative, then there is some internal inconsistency and this is
4460Sstevel@tonic-gate  *       treated as an error and no type is determined.
4470Sstevel@tonic-gate  */
4480Sstevel@tonic-gate static int
4490Sstevel@tonic-gate prop_type_guess(const dumpops_t *propops, void *prop, void **prop_data,
4500Sstevel@tonic-gate     int *prop_type)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	int len, type;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	type = PROPTYPE(propops)(prop);
4550Sstevel@tonic-gate 	switch (type) {
4560Sstevel@tonic-gate 	case DI_PROP_TYPE_UNDEF_IT:
4570Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
4580Sstevel@tonic-gate 		*prop_data = NULL;
4590Sstevel@tonic-gate 		*prop_type = type;
4600Sstevel@tonic-gate 		return (0);
4610Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
4620Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
4630Sstevel@tonic-gate 		break;
4640Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
4650Sstevel@tonic-gate 		len = PROPINT64(propops)(prop, (int64_t **)prop_data);
4660Sstevel@tonic-gate 		break;
4670Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
4680Sstevel@tonic-gate 		len = PROPBYTES(propops)(prop, (uchar_t **)prop_data);
4690Sstevel@tonic-gate 		break;
4700Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
4710Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
4720Sstevel@tonic-gate 		break;
4730Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
4740Sstevel@tonic-gate 		len = PROPSTRINGS(propops)(prop, (char **)prop_data);
4750Sstevel@tonic-gate 		if ((len > 0) && ((*(char **)prop_data)[0] != 0)) {
4760Sstevel@tonic-gate 			*prop_type = DI_PROP_TYPE_STRING;
4770Sstevel@tonic-gate 			return (len);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		len = PROPINTS(propops)(prop, (int **)prop_data);
4810Sstevel@tonic-gate 		type = DI_PROP_TYPE_INT;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 		break;
4840Sstevel@tonic-gate 	default:
4850Sstevel@tonic-gate 		len = -1;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if (len > 0) {
4890Sstevel@tonic-gate 		*prop_type = type;
4900Sstevel@tonic-gate 		return (len);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	len = PROPRAWDATA(propops)(prop, (uchar_t **)prop_data);
4940Sstevel@tonic-gate 	if (len < 0) {
4950Sstevel@tonic-gate 		return (-1);
4960Sstevel@tonic-gate 	} else if (len == 0) {
4970Sstevel@tonic-gate 		*prop_type = DI_PROP_TYPE_BOOLEAN;
4980Sstevel@tonic-gate 		return (0);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	*prop_type = DI_PROP_TYPE_UNKNOWN;
5020Sstevel@tonic-gate 	return (len);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate static void
5060Sstevel@tonic-gate dump_prop_list_common(const dumpops_t *dumpops, int ilev, void *node)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	void *prop = DI_PROP_NIL, *prop_data;
5090Sstevel@tonic-gate 	char *p;
5100Sstevel@tonic-gate 	int i, prop_type, nitems;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	while ((prop = NEXTPROP(dumpops)(node, prop)) != DI_PROP_NIL) {
5130Sstevel@tonic-gate 		nitems = prop_type_guess(dumpops, prop, &prop_data, &prop_type);
5140Sstevel@tonic-gate 		if (nitems < 0)
5150Sstevel@tonic-gate 			continue;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		indent_to_level(ilev);
5180Sstevel@tonic-gate 		(void) printf("name='%s' type=", PROPNAME(dumpops)(prop));
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		switch (prop_type) {
5210Sstevel@tonic-gate 		case DI_PROP_TYPE_UNDEF_IT:
5220Sstevel@tonic-gate 			(void) printf("undef");
5230Sstevel@tonic-gate 			break;
5240Sstevel@tonic-gate 		case DI_PROP_TYPE_BOOLEAN:
5250Sstevel@tonic-gate 			(void) printf("boolean");
5260Sstevel@tonic-gate 			break;
5270Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
5280Sstevel@tonic-gate 			(void) printf("int");
5290Sstevel@tonic-gate 			break;
5300Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
5310Sstevel@tonic-gate 			(void) printf("int64");
5320Sstevel@tonic-gate 			break;
5330Sstevel@tonic-gate 		case DI_PROP_TYPE_BYTE:
5340Sstevel@tonic-gate 			(void) printf("byte");
5350Sstevel@tonic-gate 			break;
5360Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
5370Sstevel@tonic-gate 			(void) printf("string");
5380Sstevel@tonic-gate 			break;
5390Sstevel@tonic-gate 		case DI_PROP_TYPE_UNKNOWN:
5400Sstevel@tonic-gate 			(void) printf("unknown");
5410Sstevel@tonic-gate 			break;
5420Sstevel@tonic-gate 		default:
5430Sstevel@tonic-gate 			/* Should never be here */
5440Sstevel@tonic-gate 			(void) printf("0x%x", prop_type);
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		if (nitems != 0)
5480Sstevel@tonic-gate 			(void) printf(" items=%i", nitems);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		/* print the major and minor numbers for a device property */
5510Sstevel@tonic-gate 		if (PROPDEVT(dumpops) != NULL) {
5520Sstevel@tonic-gate 			dev_t dev;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 			dev = PROPDEVT(dumpops)(prop);
5550Sstevel@tonic-gate 			if (dev != DDI_DEV_T_NONE) {
5560Sstevel@tonic-gate 				(void) printf(" dev=(%u,%u)",
5570Sstevel@tonic-gate 				    (uint_t)major(dev), (uint_t)minor(dev));
5580Sstevel@tonic-gate 			} else {
5590Sstevel@tonic-gate 				(void) printf(" dev=none");
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		(void) putchar('\n');
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		if (nitems == 0)
5660Sstevel@tonic-gate 			continue;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 		indent_to_level(ilev);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		(void) printf("    value=");
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		switch (prop_type) {
5730Sstevel@tonic-gate 		case DI_PROP_TYPE_INT:
5740Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
5750Sstevel@tonic-gate 				(void) printf("%8.8x.", ((int *)prop_data)[i]);
5760Sstevel@tonic-gate 			(void) printf("%8.8x", ((int *)prop_data)[i]);
5770Sstevel@tonic-gate 			break;
5780Sstevel@tonic-gate 		case DI_PROP_TYPE_INT64:
5790Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
5800Sstevel@tonic-gate 				(void) printf("%16.16llx.",
5810Sstevel@tonic-gate 				    ((long long *)prop_data)[i]);
5820Sstevel@tonic-gate 			(void) printf("%16.16llx", ((long long *)prop_data)[i]);
5830Sstevel@tonic-gate 			break;
5840Sstevel@tonic-gate 		case DI_PROP_TYPE_STRING:
5850Sstevel@tonic-gate 			p = (char *)prop_data;
5860Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++) {
5870Sstevel@tonic-gate 				(void) printf("'%s' + ", p);
5880Sstevel@tonic-gate 				p += strlen(p) + 1;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 			(void) printf("'%s'", p);
5910Sstevel@tonic-gate 			break;
5920Sstevel@tonic-gate 		default:
5930Sstevel@tonic-gate 			for (i = 0; i < nitems - 1; i++)
5940Sstevel@tonic-gate 				(void) printf("%2.2x.",
5950Sstevel@tonic-gate 				    ((uint8_t *)prop_data)[i]);
5960Sstevel@tonic-gate 			(void) printf("%2.2x", ((uint8_t *)prop_data)[i]);
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		(void) putchar('\n');
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate  * walk_driver is a debugging facility.
6050Sstevel@tonic-gate  */
6060Sstevel@tonic-gate static void
6070Sstevel@tonic-gate walk_driver(di_node_t root, di_devlink_handle_t devlink_hdl)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	di_node_t node;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	node = di_drv_first_node(dbg.d_drivername, root);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
6140Sstevel@tonic-gate 		(void) dump_devs(node, devlink_hdl);
6150Sstevel@tonic-gate 		node = di_drv_next_node(node);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate  * print out information about this node, returns appropriate code.
6210Sstevel@tonic-gate  */
6220Sstevel@tonic-gate /*ARGSUSED1*/
6230Sstevel@tonic-gate static int
6240Sstevel@tonic-gate dump_devs(di_node_t node, void *arg)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	di_devlink_handle_t	devlink_hdl = (di_devlink_handle_t)arg;
6270Sstevel@tonic-gate 	int			ilev = 0;	/* indentation level */
6280Sstevel@tonic-gate 	char			*driver_name;
6290Sstevel@tonic-gate 	di_node_t		root_node, tmp;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (dbg.d_debug) {
6320Sstevel@tonic-gate 		char *path = di_devfs_path(node);
6330Sstevel@tonic-gate 		dprintf("Dump node %s\n", path);
6340Sstevel@tonic-gate 		di_devfs_path_free(path);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (dbg.d_bydriver) {
6380Sstevel@tonic-gate 		ilev = 1;
6390Sstevel@tonic-gate 	} else {
6400Sstevel@tonic-gate 		/* figure out indentation level */
6410Sstevel@tonic-gate 		tmp = node;
6420Sstevel@tonic-gate 		while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
6430Sstevel@tonic-gate 			ilev++;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		if (opts.o_target && !opts.o_ancestors) {
6460Sstevel@tonic-gate 			ilev -= opts.o_target - 1;
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (opts.o_target && !node_display(node)) {
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * if we're only displaying certain nodes and this one
6530Sstevel@tonic-gate 		 * isn't flagged, skip it.
6540Sstevel@tonic-gate 		 */
6550Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	indent_to_level(ilev);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	(void) printf("%s", di_node_name(node));
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	/*
6630Sstevel@tonic-gate 	 * if this node does not have an instance number or is the
6640Sstevel@tonic-gate 	 * root node (1229946), we don't print an instance number
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	root_node = tmp = node;
6670Sstevel@tonic-gate 	while ((tmp = di_parent_node(tmp)) != DI_NODE_NIL)
6680Sstevel@tonic-gate 		root_node = tmp;
6690Sstevel@tonic-gate 	if ((di_instance(node) >= 0) && (node != root_node))
6700Sstevel@tonic-gate 		(void) printf(", instance #%d", di_instance(node));
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (opts.o_drv_name) {
6730Sstevel@tonic-gate 		driver_name = di_driver_name(node);
6740Sstevel@tonic-gate 		if (driver_name != NULL)
6750Sstevel@tonic-gate 			(void) printf(" (driver name: %s)", driver_name);
676*4845Svikram 	} else if (di_retired(node)) {
677*4845Svikram 		(void) printf(" (retired)");
6780Sstevel@tonic-gate 	} else if (di_state(node) & DI_DRIVER_DETACHED)
6790Sstevel@tonic-gate 		(void) printf(" (driver not attached)");
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	(void) printf("\n");
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (opts.o_verbose)  {
6840Sstevel@tonic-gate 		if (dump_prop_list(&sysprop_dumpops, "System", ilev + 1,
6850Sstevel@tonic-gate 		    node)) {
6860Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops, NULL, ilev + 1,
6870Sstevel@tonic-gate 			    node);
6880Sstevel@tonic-gate 		} else {
6890Sstevel@tonic-gate 			(void) dump_prop_list(&globprop_dumpops,
6900Sstevel@tonic-gate 			    "System software", ilev + 1, node);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		(void) dump_prop_list(&drvprop_dumpops, "Driver", ilev + 1,
6930Sstevel@tonic-gate 		    node);
6940Sstevel@tonic-gate 		(void) dump_prop_list(&hwprop_dumpops, "Hardware", ilev + 1,
6950Sstevel@tonic-gate 		    node);
6960Sstevel@tonic-gate 		dump_priv_data(ilev + 1, node);
6970Sstevel@tonic-gate 		dump_pathing_data(ilev + 1, node);
6980Sstevel@tonic-gate 		dump_link_data(ilev + 1, node, devlink_hdl);
6990Sstevel@tonic-gate 		dump_minor_data(ilev + 1, node, devlink_hdl);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (opts.o_target)
7030Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if (!opts.o_pseudodevs && (strcmp(di_node_name(node), "pseudo") == 0))
7060Sstevel@tonic-gate 		return (DI_WALK_PRUNECHILD);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate /*
7120Sstevel@tonic-gate  * Returns 0 if nothing is printed, 1 otherwise
7130Sstevel@tonic-gate  */
7140Sstevel@tonic-gate static int
7150Sstevel@tonic-gate dump_prop_list(const dumpops_t *dumpops, const char *name, int ilev,
7160Sstevel@tonic-gate     di_node_t node)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	if (NEXTPROP(dumpops)(node, DI_PROP_NIL) == DI_PROP_NIL)
7190Sstevel@tonic-gate 		return (0);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (name != NULL)  {
7220Sstevel@tonic-gate 		indent_to_level(ilev);
7230Sstevel@tonic-gate 		(void) printf("%s properties:\n", name);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	dump_prop_list_common(dumpops, ilev + 1, node);
7270Sstevel@tonic-gate 	return (1);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate /* _error([no_perror, ] fmt [, arg ...]) */
7320Sstevel@tonic-gate static int
7330Sstevel@tonic-gate _error(const char *opt_noperror, ...)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	int saved_errno;
7360Sstevel@tonic-gate 	va_list ap;
7370Sstevel@tonic-gate 	int no_perror = 0;
7380Sstevel@tonic-gate 	const char *fmt;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	saved_errno = errno;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts.o_progname);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	va_start(ap, opt_noperror);
7450Sstevel@tonic-gate 	if (opt_noperror == NULL) {
7460Sstevel@tonic-gate 		no_perror = 1;
7470Sstevel@tonic-gate 		fmt = va_arg(ap, char *);
7480Sstevel@tonic-gate 	} else
7490Sstevel@tonic-gate 		fmt = opt_noperror;
7500Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
7510Sstevel@tonic-gate 	va_end(ap);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (no_perror)
7540Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
7550Sstevel@tonic-gate 	else {
7560Sstevel@tonic-gate 		(void) fprintf(stderr, ": ");
7570Sstevel@tonic-gate 		errno = saved_errno;
7580Sstevel@tonic-gate 		perror("");
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	return (-1);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * The rest of the routines handle printing the raw prom devinfo (-p option).
7670Sstevel@tonic-gate  *
7680Sstevel@tonic-gate  * 128 is the size of the largest (currently) property name
7690Sstevel@tonic-gate  * 16k - MAXNAMESZ - sizeof (int) is the size of the largest
7700Sstevel@tonic-gate  * (currently) property value that is allowed.
7710Sstevel@tonic-gate  * the sizeof (uint_t) is from struct openpromio
7720Sstevel@tonic-gate  */
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate #define	MAXNAMESZ	128
7750Sstevel@tonic-gate #define	MAXVALSIZE	(16384 - MAXNAMESZ - sizeof (uint_t))
7760Sstevel@tonic-gate #define	BUFSIZE		(MAXNAMESZ + MAXVALSIZE + sizeof (uint_t))
7770Sstevel@tonic-gate typedef union {
7780Sstevel@tonic-gate 	char buf[BUFSIZE];
7790Sstevel@tonic-gate 	struct openpromio opp;
7800Sstevel@tonic-gate } Oppbuf;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate static int prom_fd;
7830Sstevel@tonic-gate static uchar_t *prom_snapshot;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static int
7860Sstevel@tonic-gate is_openprom(void)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	Oppbuf	oppbuf;
7890Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
7900Sstevel@tonic-gate 	unsigned int i;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
7930Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETCONS, opp) < 0)
7940Sstevel@tonic-gate 		exit(_error("OPROMGETCONS"));
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	i = (unsigned int)((unsigned char)opp->oprom_array[0]);
7970Sstevel@tonic-gate 	return ((i & OPROMCONS_OPENPROM) == OPROMCONS_OPENPROM);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate int
8010Sstevel@tonic-gate do_prominfo(void)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	uint_t arg = opts.o_verbose;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
8060Sstevel@tonic-gate 		exit(_error("openeepr device open failed"));
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (is_openprom() == 0)  {
8100Sstevel@tonic-gate 		(void) fprintf(stderr, "System architecture does not "
8110Sstevel@tonic-gate 		    "support this option of this command.\n");
8120Sstevel@tonic-gate 		return (1);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/* OPROMSNAPSHOT returns size in arg */
8160Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMSNAPSHOT, &arg) < 0)
8170Sstevel@tonic-gate 		exit(_error("OPROMSNAPSHOT"));
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (arg == 0)
8200Sstevel@tonic-gate 		return (1);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if ((prom_snapshot = malloc(arg)) == NULL)
8230Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/* copy out the snapshot for printing */
8260Sstevel@tonic-gate 	/*LINTED*/
8270Sstevel@tonic-gate 	*(uint_t *)prom_snapshot = arg;
8280Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMCOPYOUT, prom_snapshot) < 0)
8290Sstevel@tonic-gate 		exit(_error("OPROMCOPYOUT"));
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	promclose();
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/* print out information */
8340Sstevel@tonic-gate 	walk(prom_snapshot, arg, 0);
8350Sstevel@tonic-gate 	free(prom_snapshot);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	return (0);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate static void
8410Sstevel@tonic-gate walk(uchar_t *buf, uint_t size, int level)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	int error;
8440Sstevel@tonic-gate 	nvlist_t *nvl, *cnvl;
8450Sstevel@tonic-gate 	nvpair_t *child = NULL;
8460Sstevel@tonic-gate 	uchar_t *cbuf = NULL;
8470Sstevel@tonic-gate 	uint_t csize;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/* Expand to an nvlist */
8500Sstevel@tonic-gate 	if (nvlist_unpack((char *)buf, size, &nvl, 0))
8510Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/* print current node */
8540Sstevel@tonic-gate 	dump_node(nvl, level);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/* print children */
8570Sstevel@tonic-gate 	error = nvlist_lookup_byte_array(nvl, "@child", &cbuf, &csize);
8580Sstevel@tonic-gate 	if ((error == ENOENT) || (cbuf == NULL))
8590Sstevel@tonic-gate 		return;		/* no child exists */
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if (error || nvlist_unpack((char *)cbuf, csize, &cnvl, 0))
8620Sstevel@tonic-gate 		exit(_error("error processing snapshot"));
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	while (child = nvlist_next_nvpair(cnvl, child)) {
8650Sstevel@tonic-gate 		char *name = nvpair_name(child);
8660Sstevel@tonic-gate 		data_type_t type = nvpair_type(child);
8670Sstevel@tonic-gate 		uchar_t *nodebuf;
8680Sstevel@tonic-gate 		uint_t nodesize;
8690Sstevel@tonic-gate 		if (strcmp("node", name) != 0) {
8700Sstevel@tonic-gate 			dprintf("unexpected nvpair name %s != name\n", name);
8710Sstevel@tonic-gate 			continue;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		if (type != DATA_TYPE_BYTE_ARRAY) {
8740Sstevel@tonic-gate 			dprintf("unexpected nvpair type %d, not byte array \n",
8750Sstevel@tonic-gate 			    type);
8760Sstevel@tonic-gate 			continue;
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 		(void) nvpair_value_byte_array(child,
8800Sstevel@tonic-gate 		    (uchar_t **)&nodebuf, &nodesize);
8810Sstevel@tonic-gate 		walk(nodebuf, nodesize, level + 1);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	nvlist_free(nvl);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate  * Print all properties and values
8890Sstevel@tonic-gate  */
8900Sstevel@tonic-gate static void
8910Sstevel@tonic-gate dump_node(nvlist_t *nvl, int level)
8920Sstevel@tonic-gate {
8930Sstevel@tonic-gate 	int id = 0;
8940Sstevel@tonic-gate 	char *name = NULL;
8950Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	indent_to_level(level);
8980Sstevel@tonic-gate 	(void) printf("Node");
8990Sstevel@tonic-gate 	if (!opts.o_verbose) {
9000Sstevel@tonic-gate 		if (nvlist_lookup_string(nvl, "name", &name))
9010Sstevel@tonic-gate 			(void) printf("data not available");
9020Sstevel@tonic-gate 		else
9030Sstevel@tonic-gate 			(void) printf(" '%s'", name);
9040Sstevel@tonic-gate 		(void) putchar('\n');
9050Sstevel@tonic-gate 		return;
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 	(void) nvlist_lookup_int32(nvl, "@nodeid", &id);
9080Sstevel@tonic-gate 	(void) printf(" %#08x\n", id);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	while (nvp = nvlist_next_nvpair(nvl, nvp)) {
9110Sstevel@tonic-gate 		name = nvpair_name(nvp);
9120Sstevel@tonic-gate 		if (name[0] == '@')
9130Sstevel@tonic-gate 			continue;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		print_one(nvp, level + 1);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 	(void) putchar('\n');
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate static const char *
9210Sstevel@tonic-gate path_state_name(di_path_state_t st)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	switch (st) {
9240Sstevel@tonic-gate 		case DI_PATH_STATE_ONLINE:
9250Sstevel@tonic-gate 			return ("online");
9260Sstevel@tonic-gate 		case DI_PATH_STATE_STANDBY:
9270Sstevel@tonic-gate 			return ("standby");
9280Sstevel@tonic-gate 		case DI_PATH_STATE_OFFLINE:
9290Sstevel@tonic-gate 			return ("offline");
9300Sstevel@tonic-gate 		case DI_PATH_STATE_FAULT:
9310Sstevel@tonic-gate 			return ("faulted");
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 	return ("unknown");
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate  * Print all phci's each client is connected to.
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate static void
9400Sstevel@tonic-gate dump_pathing_data(int ilev, di_node_t node)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	di_path_t pi = DI_PATH_NIL;
9430Sstevel@tonic-gate 	int firsttime = 1;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (node == DI_PATH_NIL)
9460Sstevel@tonic-gate 		return;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	while ((pi = di_path_next_phci(node, pi)) != DI_PATH_NIL) {
9490Sstevel@tonic-gate 		di_node_t phci_node = di_path_phci_node(pi);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		if (firsttime) {
9520Sstevel@tonic-gate 			indent_to_level(ilev);
9530Sstevel@tonic-gate 			firsttime = 0;
9540Sstevel@tonic-gate 			ilev++;
9550Sstevel@tonic-gate 			(void) printf("Paths from multipath bus adapters:\n");
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		indent_to_level(ilev);
9590Sstevel@tonic-gate 		(void) printf("%s#%d (%s)\n", di_driver_name(phci_node),
9600Sstevel@tonic-gate 		    di_instance(phci_node), path_state_name(di_path_state(pi)));
9610Sstevel@tonic-gate 		dump_prop_list_common(&pathprop_dumpops, ilev + 1, pi);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate static int
9660Sstevel@tonic-gate dump_minor_data_links(di_devlink_t devlink, void *arg)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	int ilev = (intptr_t)arg;
9690Sstevel@tonic-gate 	indent_to_level(ilev);
9700Sstevel@tonic-gate 	(void) printf("dev_link=%s\n", di_devlink_path(devlink));
9710Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate static void
9750Sstevel@tonic-gate dump_minor_data_paths(int ilev, di_minor_t minor,
9760Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	char	*path, *type;
9790Sstevel@tonic-gate 	int	spec_type;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/* get the path to the device and the minor node name */
9820Sstevel@tonic-gate 	if ((path = di_devfs_minor_path(minor)) == NULL)
9830Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/* display the path to this minor node */
9860Sstevel@tonic-gate 	indent_to_level(ilev);
9870Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (devlink_hdl != NULL) {
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		/* get the device minor node information */
9920Sstevel@tonic-gate 		spec_type = di_minor_spectype(minor);
9930Sstevel@tonic-gate 		switch (di_minor_type(minor)) {
9940Sstevel@tonic-gate 			case DDM_MINOR:
9950Sstevel@tonic-gate 				type = "minor";
9960Sstevel@tonic-gate 				break;
9970Sstevel@tonic-gate 			case DDM_ALIAS:
9980Sstevel@tonic-gate 				type = "alias";
9990Sstevel@tonic-gate 				break;
10000Sstevel@tonic-gate 			case DDM_DEFAULT:
10010Sstevel@tonic-gate 				type = "default";
10020Sstevel@tonic-gate 				break;
10030Sstevel@tonic-gate 			case DDM_INTERNAL_PATH:
10040Sstevel@tonic-gate 				type = "internal";
10050Sstevel@tonic-gate 				break;
10060Sstevel@tonic-gate 			default:
10070Sstevel@tonic-gate 				type = "unknown";
10080Sstevel@tonic-gate 				break;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 		/* display the device minor node information */
10120Sstevel@tonic-gate 		indent_to_level(ilev + 1);
10130Sstevel@tonic-gate 		(void) printf("spectype=%s type=%s\n",
10144453Scth 		    (spec_type == S_IFBLK) ? "blk" : "chr", type);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 		/* display all the devlinks for this device minor node */
10170Sstevel@tonic-gate 		(void) di_devlink_walk(devlink_hdl, NULL, path,
10180Sstevel@tonic-gate 		    0, (void *)(intptr_t)(ilev + 1), dump_minor_data_links);
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	di_devfs_path_free(path);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate static void
10250Sstevel@tonic-gate create_minor_list(di_node_t node)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	di_minor_t	minor, minor_head, minor_tail, minor_prev, minor_walk;
10280Sstevel@tonic-gate 	int		major;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/* if there are no minor nodes, bail */
10310Sstevel@tonic-gate 	if (di_minor_next(node, DI_MINOR_NIL) == DI_MINOR_NIL)
10320Sstevel@tonic-gate 		return;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/*
10350Sstevel@tonic-gate 	 * here we want to create lists of minor nodes with the same
10360Sstevel@tonic-gate 	 * dev_t.  to do this we first sort all the minor nodes by devt.
10370Sstevel@tonic-gate 	 *
10380Sstevel@tonic-gate 	 * the algorithm used here is a bubble sort, so performance sucks.
10390Sstevel@tonic-gate 	 * but it's probably ok here because most device instances don't
10400Sstevel@tonic-gate 	 * have that many minor nodes.  also we're doing this as we're
10410Sstevel@tonic-gate 	 * displaying each node so it doesn't look like we're pausing
10420Sstevel@tonic-gate 	 * output for a long time.
10430Sstevel@tonic-gate 	 */
10440Sstevel@tonic-gate 	major = di_driver_major(node);
10450Sstevel@tonic-gate 	minor_head = minor_tail = minor = DI_MINOR_NIL;
10460Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
10470Sstevel@tonic-gate 		dev_t	dev = di_minor_devt(minor);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		/* skip /pseudo/clone@0 minor nodes */
10500Sstevel@tonic-gate 		if (major != major(dev))
10510Sstevel@tonic-gate 			continue;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		minor_ptr_set(minor, DI_MINOR_NIL);
10540Sstevel@tonic-gate 		if (minor_head == DI_MINOR_NIL) {
10550Sstevel@tonic-gate 			/* this is the first minor node we're looking at */
10560Sstevel@tonic-gate 			minor_head = minor_tail = minor;
10570Sstevel@tonic-gate 			continue;
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * if the new dev is less than the old dev, update minor_head
10620Sstevel@tonic-gate 		 * so it points to the beginning of the list.  ie it points
10630Sstevel@tonic-gate 		 * to the node with the lowest dev value
10640Sstevel@tonic-gate 		 */
10650Sstevel@tonic-gate 		if (dev <= di_minor_devt(minor_head)) {
10660Sstevel@tonic-gate 			minor_ptr_set(minor, minor_head);
10670Sstevel@tonic-gate 			minor_head = minor;
10680Sstevel@tonic-gate 			continue;
10690Sstevel@tonic-gate 		}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 		minor_prev = minor_head;
10720Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_head);
10730Sstevel@tonic-gate 		while ((minor_walk != DI_MINOR_NIL) &&
10744453Scth 		    (dev > di_minor_devt(minor_walk))) {
10750Sstevel@tonic-gate 			minor_prev = minor_walk;
10760Sstevel@tonic-gate 			minor_walk = minor_ptr(minor_walk);
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate 		minor_ptr_set(minor, minor_walk);
10790Sstevel@tonic-gate 		minor_ptr_set(minor_prev, minor);
10800Sstevel@tonic-gate 		if (minor_walk == NULL)
10810Sstevel@tonic-gate 			minor_tail = minor;
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/* check if there were any non /pseudo/clone@0 nodes.  if not, bail */
10850Sstevel@tonic-gate 	if (minor_head == DI_MINOR_NIL)
10860Sstevel@tonic-gate 		return;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * now that we have a list of minor nodes sorted by devt
10900Sstevel@tonic-gate 	 * we walk through the list and break apart the entire list
10910Sstevel@tonic-gate 	 * to create circular lists of minor nodes with matching devts.
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	minor_prev = minor_head;
10940Sstevel@tonic-gate 	minor_walk = minor_ptr(minor_head);
10950Sstevel@tonic-gate 	while (minor_walk != DI_MINOR_NIL) {
10960Sstevel@tonic-gate 		if (di_minor_devt(minor_prev) != di_minor_devt(minor_walk)) {
10970Sstevel@tonic-gate 			minor_ptr_set(minor_prev, minor_head);
10980Sstevel@tonic-gate 			minor_head = minor_walk;
10990Sstevel@tonic-gate 		}
11000Sstevel@tonic-gate 		minor_prev = minor_walk;
11010Sstevel@tonic-gate 		minor_walk = minor_ptr(minor_walk);
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	minor_ptr_set(minor_tail, minor_head);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate static void
11070Sstevel@tonic-gate link_lnode_disp(di_link_t link, uint_t endpoint, int ilev,
11080Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	di_lnode_t	lnode;
11110Sstevel@tonic-gate 	char		*name, *path;
11120Sstevel@tonic-gate 	int		displayed_path, spec_type;
11130Sstevel@tonic-gate 	di_node_t	node = DI_NODE_NIL;
11140Sstevel@tonic-gate 	dev_t		devt = DDI_DEV_T_NONE;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	lnode = di_link_to_lnode(link, endpoint);
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	indent_to_level(ilev);
11190Sstevel@tonic-gate 	name = di_lnode_name(lnode);
11200Sstevel@tonic-gate 	spec_type = di_link_spectype(link);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	(void) printf("mod=%s", name);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	/*
11250Sstevel@tonic-gate 	 * if we're displaying the source of a link, we should display
11260Sstevel@tonic-gate 	 * the target access mode.  (either block or char.)
11270Sstevel@tonic-gate 	 */
11280Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC)
11290Sstevel@tonic-gate 		(void) printf(" accesstype=%s",
11300Sstevel@tonic-gate 		    (spec_type == S_IFBLK) ? "blk" : "chr");
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/*
11330Sstevel@tonic-gate 	 * check if the lnode is bound to a specific device
11340Sstevel@tonic-gate 	 * minor node (i.e.  if it's bound to a dev_t) and
11350Sstevel@tonic-gate 	 * if so display the dev_t value and any possible
11360Sstevel@tonic-gate 	 * minor node pathing information.
11370Sstevel@tonic-gate 	 */
11380Sstevel@tonic-gate 	displayed_path = 0;
11390Sstevel@tonic-gate 	if (di_lnode_devt(lnode, &devt) == 0) {
11400Sstevel@tonic-gate 		di_minor_t	minor = DI_MINOR_NIL;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 		(void) printf(" dev=(%u,%u)\n",
11430Sstevel@tonic-gate 		    (uint_t)major(devt), (uint_t)minor(devt));
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		/* display paths to the src devt minor node */
11460Sstevel@tonic-gate 		while (minor = di_minor_next(node, minor)) {
11470Sstevel@tonic-gate 			if (devt != di_minor_devt(minor))
11480Sstevel@tonic-gate 				continue;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			if ((endpoint == DI_LINK_TGT) &&
11510Sstevel@tonic-gate 			    (spec_type != di_minor_spectype(minor)))
11520Sstevel@tonic-gate 				continue;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor, devlink_hdl);
11550Sstevel@tonic-gate 			displayed_path = 1;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 	} else {
11580Sstevel@tonic-gate 		(void) printf("\n");
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (displayed_path)
11620Sstevel@tonic-gate 		return;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/*
11650Sstevel@tonic-gate 	 * This device lnode is not did not have any minor node
11660Sstevel@tonic-gate 	 * pathing information so display the path to device node.
11670Sstevel@tonic-gate 	 */
11680Sstevel@tonic-gate 	node = di_lnode_devinfo(lnode);
11690Sstevel@tonic-gate 	if ((path = di_devfs_path(node)) == NULL)
11700Sstevel@tonic-gate 		exit(_error("failed to allocate memory"));
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	indent_to_level(ilev + 1);
11730Sstevel@tonic-gate 	(void) printf("dev_path=%s\n", path);
11740Sstevel@tonic-gate 	di_devfs_path_free(path);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate static void
11780Sstevel@tonic-gate dump_minor_link_data(int ilev, di_node_t node, dev_t devt,
11790Sstevel@tonic-gate     di_devlink_handle_t devlink_hdl)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate 	int		first = 1;
11820Sstevel@tonic-gate 	di_link_t	link;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	link = DI_LINK_NIL;
11850Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
11860Sstevel@tonic-gate 		di_lnode_t	tgt_lnode;
11870Sstevel@tonic-gate 		dev_t		tgt_devt = DDI_DEV_T_NONE;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 		tgt_lnode = di_link_to_lnode(link, DI_LINK_TGT);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 		if (di_lnode_devt(tgt_lnode, &tgt_devt) != 0)
11920Sstevel@tonic-gate 			continue;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 		if (devt != tgt_devt)
11950Sstevel@tonic-gate 			continue;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 		if (first) {
11980Sstevel@tonic-gate 			first = 0;
11990Sstevel@tonic-gate 			indent_to_level(ilev);
12000Sstevel@tonic-gate 			(void) printf("Device Minor Layered Under:\n");
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 		/* displayed this lnode */
12040Sstevel@tonic-gate 		lnode_displayed_set(tgt_lnode);
12050Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_SRC, ilev + 1, devlink_hdl);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	link = DI_LINK_NIL;
12090Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
12100Sstevel@tonic-gate 		di_lnode_t	src_lnode;
12110Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != 0)
12160Sstevel@tonic-gate 			continue;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		if (devt != src_devt)
12190Sstevel@tonic-gate 			continue;
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		if (first) {
12220Sstevel@tonic-gate 			first = 0;
12230Sstevel@tonic-gate 			indent_to_level(ilev);
12240Sstevel@tonic-gate 			(void) printf("Device Minor Layered Over:\n");
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		/* displayed this lnode */
12280Sstevel@tonic-gate 		lnode_displayed_set(src_lnode);
12290Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate static void
12340Sstevel@tonic-gate dump_minor_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	di_minor_t	minor, minor_next;
12370Sstevel@tonic-gate 	di_lnode_t	lnode;
12380Sstevel@tonic-gate 	di_link_t	link;
12390Sstevel@tonic-gate 	int		major, firstminor = 1;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	/*
12420Sstevel@tonic-gate 	 * first go through and mark all lnodes and minor nodes for this
12430Sstevel@tonic-gate 	 * node as undisplayed
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	lnode = DI_LNODE_NIL;
12460Sstevel@tonic-gate 	while (lnode = di_lnode_next(node, lnode))
12470Sstevel@tonic-gate 		lnode_displayed_clear(lnode);
12480Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
12490Sstevel@tonic-gate 	while (minor = di_minor_next(node, minor)) {
12500Sstevel@tonic-gate 		minor_displayed_clear(minor);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * when we display the minor nodes we want to coalesce nodes
12550Sstevel@tonic-gate 	 * that have the same dev_t.  we do this by creating circular
12560Sstevel@tonic-gate 	 * lists of minor nodes with the same devt.
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	create_minor_list(node);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/* now we display the driver defined minor nodes */
12610Sstevel@tonic-gate 	major = di_driver_major(node);
12620Sstevel@tonic-gate 	minor = DI_MINOR_NIL;
12630Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
12640Sstevel@tonic-gate 		dev_t	devt;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 		/*
12670Sstevel@tonic-gate 		 * skip /pseudo/clone@0 minor nodes.
12680Sstevel@tonic-gate 		 * these are only created for DLPIv2 network devices.
12690Sstevel@tonic-gate 		 * since these minor nodes are associated with a driver
12700Sstevel@tonic-gate 		 * and are only bound to a device instance after they
12710Sstevel@tonic-gate 		 * are opened and attached we don't print them out
12720Sstevel@tonic-gate 		 * here.
12730Sstevel@tonic-gate 		 */
12740Sstevel@tonic-gate 		devt = di_minor_devt(minor);
12750Sstevel@tonic-gate 		if (major != major(devt))
12760Sstevel@tonic-gate 			continue;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 		/* skip nodes that may have already been displayed */
12790Sstevel@tonic-gate 		if (minor_displayed(minor))
12800Sstevel@tonic-gate 			continue;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		if (firstminor) {
12830Sstevel@tonic-gate 			firstminor = 0;
12840Sstevel@tonic-gate 			indent_to_level(ilev++);
12850Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		/* display the device minor node information */
12890Sstevel@tonic-gate 		indent_to_level(ilev);
12900Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
12914453Scth 		    (uint_t)major(devt), (uint_t)minor(devt));
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 		minor_next = minor;
12940Sstevel@tonic-gate 		do {
12950Sstevel@tonic-gate 			/* display device minor node path info */
12960Sstevel@tonic-gate 			minor_displayed_set(minor_next);
12970Sstevel@tonic-gate 			dump_minor_data_paths(ilev + 1, minor_next,
12984453Scth 			    devlink_hdl);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 			/* get a pointer to the next node */
13010Sstevel@tonic-gate 			minor_next = minor_ptr(minor_next);
13020Sstevel@tonic-gate 		} while (minor_next != minor);
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		/* display who has this device minor node open */
13050Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * now go through all the target lnodes for this node and
13100Sstevel@tonic-gate 	 * if they haven't yet been displayed, display them now.
13110Sstevel@tonic-gate 	 *
13120Sstevel@tonic-gate 	 * this happens in the case of clone opens when an "official"
13130Sstevel@tonic-gate 	 * minor node does not exist for the opened devt
13140Sstevel@tonic-gate 	 */
13150Sstevel@tonic-gate 	link = DI_LINK_NIL;
13160Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_TGT)) {
13170Sstevel@tonic-gate 		dev_t		devt;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 		lnode = di_link_to_lnode(link, DI_LINK_TGT);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 		/* if we've already displayed this target lnode, skip it */
13220Sstevel@tonic-gate 		if (lnode_displayed(lnode))
13230Sstevel@tonic-gate 			continue;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		if (firstminor) {
13260Sstevel@tonic-gate 			firstminor = 0;
13270Sstevel@tonic-gate 			indent_to_level(ilev++);
13280Sstevel@tonic-gate 			(void) printf("Device Minor Nodes:\n");
13290Sstevel@tonic-gate 		}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 		/* display the device minor node information */
13320Sstevel@tonic-gate 		indent_to_level(ilev);
13330Sstevel@tonic-gate 		(void) di_lnode_devt(lnode, &devt);
13340Sstevel@tonic-gate 		(void) printf("dev=(%u,%u)\n",
13354453Scth 		    (uint_t)major(devt), (uint_t)minor(devt));
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		indent_to_level(ilev + 1);
13380Sstevel@tonic-gate 		(void) printf("dev_path=<clone>\n");
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		/* display who has this cloned device minor node open */
13410Sstevel@tonic-gate 		dump_minor_link_data(ilev + 1, node, devt, devlink_hdl);
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 		/* mark node as displayed */
13440Sstevel@tonic-gate 		lnode_displayed_set(lnode);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate static void
13490Sstevel@tonic-gate dump_link_data(int ilev, di_node_t node, di_devlink_handle_t devlink_hdl)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	int		first = 1;
13520Sstevel@tonic-gate 	di_link_t	link;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	link = DI_LINK_NIL;
13550Sstevel@tonic-gate 	while (link = di_link_next_by_node(node, link, DI_LINK_SRC)) {
13560Sstevel@tonic-gate 		di_lnode_t	src_lnode;
13570Sstevel@tonic-gate 		dev_t		src_devt = DDI_DEV_T_NONE;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		src_lnode = di_link_to_lnode(link, DI_LINK_SRC);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		/*
13620Sstevel@tonic-gate 		 * here we only want to print out layering information
13630Sstevel@tonic-gate 		 * if we are the source and our source lnode is not
13640Sstevel@tonic-gate 		 * associated with any particular dev_t.  (which means
13650Sstevel@tonic-gate 		 * we won't display this link while dumping minor node
13660Sstevel@tonic-gate 		 * info.)
13670Sstevel@tonic-gate 		 */
13680Sstevel@tonic-gate 		if (di_lnode_devt(src_lnode, &src_devt) != -1)
13690Sstevel@tonic-gate 			continue;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		if (first) {
13720Sstevel@tonic-gate 			first = 0;
13730Sstevel@tonic-gate 			indent_to_level(ilev);
13740Sstevel@tonic-gate 			(void) printf("Device Layered Over:\n");
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		/* displayed this lnode */
13780Sstevel@tonic-gate 		link_lnode_disp(link, DI_LINK_TGT, ilev + 1, devlink_hdl);
13790Sstevel@tonic-gate 	}
13800Sstevel@tonic-gate }
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate /*
13830Sstevel@tonic-gate  * certain 'known' property names may contain 'composite' strings.
13840Sstevel@tonic-gate  * Handle them here, and print them as 'string1' + 'string2' ...
13850Sstevel@tonic-gate  */
13860Sstevel@tonic-gate static int
13870Sstevel@tonic-gate print_composite_string(const char *var, char *value, int size)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	char *p, *q;
13900Sstevel@tonic-gate 	char *firstp;
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	if ((strcmp(var, "version") != 0) &&
13930Sstevel@tonic-gate 	    (strcmp(var, "compatible") != 0))
13940Sstevel@tonic-gate 		return (0);	/* Not a known composite string */
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	/*
13970Sstevel@tonic-gate 	 * Verify that each string in the composite string is non-NULL,
13980Sstevel@tonic-gate 	 * is within the bounds of the property length, and contains
13990Sstevel@tonic-gate 	 * printable characters or white space. Otherwise let the
14000Sstevel@tonic-gate 	 * caller deal with it.
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
14030Sstevel@tonic-gate 		if (strlen(p) == 0)
14040Sstevel@tonic-gate 			return (0);		/* NULL string */
14050Sstevel@tonic-gate 		for (q = p; *q; q++) {
14060Sstevel@tonic-gate 			if (!(isascii(*q) && (isprint(*q) || isspace(*q))))
14070Sstevel@tonic-gate 				return (0);	/* Not printable or space */
14080Sstevel@tonic-gate 		}
14090Sstevel@tonic-gate 		if (q > (firstp + size))
14100Sstevel@tonic-gate 			return (0);		/* Out of bounds */
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	for (firstp = p = value; p < (value + size); p += strlen(p) + 1) {
14140Sstevel@tonic-gate 		if (p == firstp)
14150Sstevel@tonic-gate 			(void) printf("'%s'", p);
14160Sstevel@tonic-gate 		else
14170Sstevel@tonic-gate 			(void) printf(" + '%s'", p);
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 	(void) putchar('\n');
14200Sstevel@tonic-gate 	return (1);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * Print one property and its value. Handle the verbose case.
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate static void
14270Sstevel@tonic-gate print_one(nvpair_t *nvp, int level)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	int i;
14300Sstevel@tonic-gate 	int endswap = 0;
14310Sstevel@tonic-gate 	uint_t valsize;
14320Sstevel@tonic-gate 	char *value;
14330Sstevel@tonic-gate 	char *var = nvpair_name(nvp);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	indent_to_level(level);
14360Sstevel@tonic-gate 	(void) printf("%s: ", var);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	switch (nvpair_type(nvp)) {
14390Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
14400Sstevel@tonic-gate 		(void) printf(" \n");
14410Sstevel@tonic-gate 		return;
14420Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
14430Sstevel@tonic-gate 		if (nvpair_value_byte_array(nvp, (uchar_t **)&value,
14440Sstevel@tonic-gate 		    &valsize)) {
14450Sstevel@tonic-gate 			(void) printf("data not available.\n");
14460Sstevel@tonic-gate 			return;
14470Sstevel@tonic-gate 		}
14480Sstevel@tonic-gate 		valsize--;	/* take out null added by driver */
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		/*
14510Sstevel@tonic-gate 		 * Do not print valsize > MAXVALSIZE, to be compatible
14520Sstevel@tonic-gate 		 * with old behavior. E.g. intel's eisa-nvram property
14530Sstevel@tonic-gate 		 * has a size of 65 K.
14540Sstevel@tonic-gate 		 */
14550Sstevel@tonic-gate 		if (valsize > MAXVALSIZE) {
14560Sstevel@tonic-gate 			(void) printf(" \n");
14570Sstevel@tonic-gate 			return;
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 		break;
14600Sstevel@tonic-gate 	default:
14610Sstevel@tonic-gate 		(void) printf("data type unexpected.\n");
14620Sstevel@tonic-gate 		return;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Handle printing verbosely
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	if (print_composite_string(var, value, valsize)) {
14690Sstevel@tonic-gate 		return;
14700Sstevel@tonic-gate 	}
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	if (!unprintable(value, valsize)) {
14730Sstevel@tonic-gate 		(void) printf(" '%s'\n", value);
14740Sstevel@tonic-gate 		return;
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	(void) printf(" ");
14780Sstevel@tonic-gate #ifdef	__x86
14790Sstevel@tonic-gate 	/*
14800Sstevel@tonic-gate 	 * Due to backwards compatibility constraints x86 int
14810Sstevel@tonic-gate 	 * properties are not in big-endian (ieee 1275) byte order.
14820Sstevel@tonic-gate 	 * If we have a property that is a multiple of 4 bytes,
14830Sstevel@tonic-gate 	 * let's assume it is an array of ints and print the bytes
14840Sstevel@tonic-gate 	 * in little endian order to make things look nicer for
14850Sstevel@tonic-gate 	 * the user.
14860Sstevel@tonic-gate 	 */
14870Sstevel@tonic-gate 	endswap = (valsize % 4) == 0;
14880Sstevel@tonic-gate #endif	/* __x86 */
14890Sstevel@tonic-gate 	for (i = 0; i < valsize; i++) {
14900Sstevel@tonic-gate 		int out;
14910Sstevel@tonic-gate 		if (i && (i % 4 == 0))
14920Sstevel@tonic-gate 			(void) putchar('.');
14930Sstevel@tonic-gate 		if (endswap)
14940Sstevel@tonic-gate 			out = value[i + (3 - 2 * (i % 4))] & 0xff;
14950Sstevel@tonic-gate 		else
14960Sstevel@tonic-gate 			out = value[i] & 0xff;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 		(void) printf("%02x", out);
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate 	(void) putchar('\n');
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate static int
15040Sstevel@tonic-gate unprintable(char *value, int size)
15050Sstevel@tonic-gate {
15060Sstevel@tonic-gate 	int i;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	/*
15090Sstevel@tonic-gate 	 * Is this just a zero?
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 	if (size == 0 || value[0] == '\0')
15120Sstevel@tonic-gate 		return (1);
15130Sstevel@tonic-gate 	/*
15140Sstevel@tonic-gate 	 * If any character is unprintable, or if a null appears
15150Sstevel@tonic-gate 	 * anywhere except at the end of a string, the whole
15160Sstevel@tonic-gate 	 * property is "unprintable".
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	for (i = 0; i < size; ++i) {
15190Sstevel@tonic-gate 		if (value[i] == '\0')
15200Sstevel@tonic-gate 			return (i != (size - 1));
15210Sstevel@tonic-gate 		if (!isascii(value[i]) || iscntrl(value[i]))
15220Sstevel@tonic-gate 			return (1);
15230Sstevel@tonic-gate 	}
15240Sstevel@tonic-gate 	return (0);
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate static int
15280Sstevel@tonic-gate promopen(int oflag)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate 	for (;;)  {
15310Sstevel@tonic-gate 		if ((prom_fd = open(opts.o_promdev, oflag)) < 0)  {
15320Sstevel@tonic-gate 			if (errno == EAGAIN)   {
15330Sstevel@tonic-gate 				(void) sleep(5);
15340Sstevel@tonic-gate 				continue;
15350Sstevel@tonic-gate 			}
15360Sstevel@tonic-gate 			if (errno == ENXIO)
15370Sstevel@tonic-gate 				return (-1);
15380Sstevel@tonic-gate 			if (getzoneid() == GLOBAL_ZONEID) {
15390Sstevel@tonic-gate 				_exit(_error("cannot open %s",
15400Sstevel@tonic-gate 				    opts.o_promdev));
15410Sstevel@tonic-gate 			}
15420Sstevel@tonic-gate 			/* not an error if this isn't the global zone */
15430Sstevel@tonic-gate 			(void) _error(NULL, "openprom facility not available");
15440Sstevel@tonic-gate 			exit(0);
15450Sstevel@tonic-gate 		} else
15460Sstevel@tonic-gate 			return (0);
15470Sstevel@tonic-gate 	}
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate static void
15510Sstevel@tonic-gate promclose(void)
15520Sstevel@tonic-gate {
15530Sstevel@tonic-gate 	if (close(prom_fd) < 0)
15540Sstevel@tonic-gate 		exit(_error("close error on %s", opts.o_promdev));
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * Get and print the name of the frame buffer device.
15590Sstevel@tonic-gate  */
15600Sstevel@tonic-gate int
15610Sstevel@tonic-gate do_fbname(void)
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	int	retval;
15640Sstevel@tonic-gate 	char fbuf_path[MAXPATHLEN];
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	retval =  modctl(MODGETFBNAME, (caddr_t)fbuf_path);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	if (retval == 0) {
15690Sstevel@tonic-gate 		(void) printf("%s\n", fbuf_path);
15700Sstevel@tonic-gate 	} else {
15710Sstevel@tonic-gate 		if (retval == EFAULT) {
15720Sstevel@tonic-gate 			(void) fprintf(stderr,
15730Sstevel@tonic-gate 			"Error copying fb path to userland\n");
15740Sstevel@tonic-gate 		} else {
15750Sstevel@tonic-gate 			(void) fprintf(stderr,
15760Sstevel@tonic-gate 			"Console output device is not a frame buffer\n");
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 		return (1);
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate 	return (0);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * Get and print the PROM version.
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate int
15870Sstevel@tonic-gate do_promversion(void)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	Oppbuf	oppbuf;
15900Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
15930Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
15940Sstevel@tonic-gate 		return (1);
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
15980Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMGETVERSION, opp) < 0)
15990Sstevel@tonic-gate 		exit(_error("OPROMGETVERSION"));
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	(void) printf("%s\n", opp->oprom_array);
16020Sstevel@tonic-gate 	promclose();
16030Sstevel@tonic-gate 	return (0);
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate int
16070Sstevel@tonic-gate do_prom_version64(void)
16080Sstevel@tonic-gate {
16090Sstevel@tonic-gate #ifdef	sparc
16100Sstevel@tonic-gate 	Oppbuf	oppbuf;
16110Sstevel@tonic-gate 	struct openpromio *opp = &(oppbuf.opp);
16120Sstevel@tonic-gate 	/*LINTED*/
16130Sstevel@tonic-gate 	struct openprom_opr64 *opr = (struct openprom_opr64 *)opp->oprom_array;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	static const char msg[] =
16164453Scth 	    "NOTICE: The firmware on this system does not support the "
16174453Scth 	    "64-bit OS.\n"
16184453Scth 	    "\tPlease upgrade to at least the following version:\n"
16194453Scth 	    "\t\t%s\n\n";
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	if (promopen(O_RDONLY))  {
16220Sstevel@tonic-gate 		(void) fprintf(stderr, "Cannot open openprom device\n");
16230Sstevel@tonic-gate 		return (-1);
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	opp->oprom_size = MAXVALSIZE;
16270Sstevel@tonic-gate 	if (ioctl(prom_fd, OPROMREADY64, opp) < 0)
16280Sstevel@tonic-gate 		exit(_error("OPROMREADY64"));
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (opr->return_code == 0)
16310Sstevel@tonic-gate 		return (0);
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	(void) printf(msg, opr->message);
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	promclose();
16360Sstevel@tonic-gate 	return (opr->return_code);
16370Sstevel@tonic-gate #else
16380Sstevel@tonic-gate 	return (0);
16390Sstevel@tonic-gate #endif
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate int
16430Sstevel@tonic-gate do_productinfo(void)
16440Sstevel@tonic-gate {
16450Sstevel@tonic-gate 	di_node_t root, next_node;
16460Sstevel@tonic-gate 	di_prom_handle_t promh;
16470Sstevel@tonic-gate 	static const char *root_prop[] = { "name", "model", "banner-name",
16480Sstevel@tonic-gate 					"compatible" };
16490Sstevel@tonic-gate 	static const char *root_propv[] = { "name", "model", "banner-name",
16500Sstevel@tonic-gate 					"compatible", "idprom" };
16510Sstevel@tonic-gate 	static const char *oprom_prop[] = { "model", "version" };
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	root = di_init("/", DINFOCPYALL);
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
16570Sstevel@tonic-gate 		(void) fprintf(stderr, "di_init() failed\n");
16580Sstevel@tonic-gate 		return (1);
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	promh = di_prom_init();
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	if (promh == DI_PROM_HANDLE_NIL) {
16640Sstevel@tonic-gate 		(void) fprintf(stderr, "di_prom_init() failed\n");
16650Sstevel@tonic-gate 		return (1);
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	if (opts.o_verbose) {
16690Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_propv, "root",
16704453Scth 		    NUM_ELEMENTS(root_propv));
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 		/* Get model and version properties under node "openprom" */
16730Sstevel@tonic-gate 		next_node = find_node_by_name(promh, root, "openprom");
16740Sstevel@tonic-gate 		if (next_node != DI_NODE_NIL)
16750Sstevel@tonic-gate 			dump_prodinfo(promh, next_node, oprom_prop,
16764453Scth 			    "openprom", NUM_ELEMENTS(oprom_prop));
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	} else
16790Sstevel@tonic-gate 		dump_prodinfo(promh, root, root_prop, "root",
16804453Scth 		    NUM_ELEMENTS(root_prop));
16810Sstevel@tonic-gate 	di_prom_fini(promh);
16820Sstevel@tonic-gate 	di_fini(root);
16830Sstevel@tonic-gate 	return (0);
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate di_node_t
16870Sstevel@tonic-gate find_node_by_name(di_prom_handle_t promh, di_node_t parent,
16880Sstevel@tonic-gate 		char *node_name)
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate 	di_node_t next_node;
16910Sstevel@tonic-gate 	uchar_t *prop_valp;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	next_node = di_child_node(parent);
16940Sstevel@tonic-gate 	while (next_node != DI_NODE_NIL) {
16950Sstevel@tonic-gate 		next_node = di_sibling_node(next_node);
16960Sstevel@tonic-gate 		(void) get_propval_by_name(promh, next_node, "name",
16974453Scth 		    &prop_valp);
16980Sstevel@tonic-gate 		if (strcmp((char *)prop_valp, node_name) == 0)
16990Sstevel@tonic-gate 			return (next_node);
17000Sstevel@tonic-gate 	}
17010Sstevel@tonic-gate 	return (DI_NODE_NIL);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate int
17060Sstevel@tonic-gate get_propval_by_name(di_prom_handle_t promh, di_node_t node, const char *name,
17070Sstevel@tonic-gate 			uchar_t **valp)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate 	int len;
17100Sstevel@tonic-gate 	uchar_t *bufp;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	len = di_prom_prop_lookup_bytes(promh, node, name,
17134453Scth 	    (uchar_t **)&bufp);
17140Sstevel@tonic-gate 	if (len != -1) {
17150Sstevel@tonic-gate 		*valp = (uchar_t *)malloc(len);
17160Sstevel@tonic-gate 		(void) memcpy(*valp, bufp, len);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	return (len);
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate static void
17230Sstevel@tonic-gate dump_prodinfo(di_prom_handle_t promh, di_node_t node, const char **propstr,
17240Sstevel@tonic-gate 		char *node_name, int num)
17250Sstevel@tonic-gate {
17260Sstevel@tonic-gate 	int out, len, index1, index, endswap = 0;
17270Sstevel@tonic-gate 	uchar_t *prop_valp;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	for (index1 = 0; index1 < num; index1++) {
17300Sstevel@tonic-gate 		len = get_propval_by_name(promh, node, propstr[index1],
17314453Scth 		    &prop_valp);
17320Sstevel@tonic-gate 		if (len != -1) {
17330Sstevel@tonic-gate 			if (strcmp(node_name, "root"))
17340Sstevel@tonic-gate 				(void) printf("%s ", node_name);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 			(void) printf("%s: ", propstr[index1]);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 			if (print_composite_string((const char *)
17394453Scth 			    propstr[index1], (char *)prop_valp, len)) {
17400Sstevel@tonic-gate 				free(prop_valp);
17410Sstevel@tonic-gate 				continue;
17420Sstevel@tonic-gate 			}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			if (!unprintable((char *)prop_valp, len)) {
17450Sstevel@tonic-gate 				(void) printf(" %s\n", (char *)prop_valp);
17460Sstevel@tonic-gate 				free(prop_valp);
17470Sstevel@tonic-gate 				continue;
17480Sstevel@tonic-gate 			}
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 			(void) printf(" ");
17510Sstevel@tonic-gate #ifdef  __x86
17520Sstevel@tonic-gate 			endswap = (len % 4) == 0;
17530Sstevel@tonic-gate #endif  /* __x86 */
17540Sstevel@tonic-gate 			for (index = 0; index < len; index++) {
17550Sstevel@tonic-gate 				if (index && (index % 4 == 0))
17560Sstevel@tonic-gate 					(void) putchar('.');
17570Sstevel@tonic-gate 				if (endswap)
17580Sstevel@tonic-gate 					out = prop_valp[index +
17594453Scth 					    (3 - 2 * (index % 4))] & 0xff;
17600Sstevel@tonic-gate 				else
17610Sstevel@tonic-gate 					out = prop_valp[index] & 0xff;
17620Sstevel@tonic-gate 				(void) printf("%02x", out);
17630Sstevel@tonic-gate 			}
17640Sstevel@tonic-gate 			(void) putchar('\n');
17650Sstevel@tonic-gate 			free(prop_valp);
17660Sstevel@tonic-gate 		}
17670Sstevel@tonic-gate 	}
17680Sstevel@tonic-gate }
1769