xref: /onnv-gate/usr/src/uts/common/io/openprom.c (revision 7656:2621e50fdf4a)
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
57009Scth  * Common Development and Distribution License (the "License").
67009Scth  * 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 /*
227009Scth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
25*7656SSherry.Moore@Sun.COM 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Ported from 4.1.1_PSRA: "@(#)openprom.c 1.19 91/02/19 SMI";
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * Porting notes:
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * OPROMU2P unsupported after SunOS 4.x.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * Only one of these devices per system is allowed.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Openprom eeprom options/devinfo driver.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/openpromio.h>
460Sstevel@tonic-gate #include <sys/conf.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/autoconf.h>
510Sstevel@tonic-gate #include <sys/ddi.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/promif.h>
540Sstevel@tonic-gate #include <sys/sysmacros.h>	/* offsetof */
550Sstevel@tonic-gate #include <sys/nvpair.h>
560Sstevel@tonic-gate #include <sys/wanboot_impl.h>
570Sstevel@tonic-gate #include <sys/zone.h>
587335SLipeng.Sang@Sun.COM #include <sys/consplat.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	MAX_OPENS	32	/* Up to this many simultaneous opens */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
630Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
640Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
650Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * XXX	Make this dynamic.. or (better still) make the interface stateless
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate static struct oprom_state {
71789Sahrens 	pnode_t	current_id;	/* node we're fetching props from */
720Sstevel@tonic-gate 	int16_t	already_open;	/* if true, this instance is 'active' */
730Sstevel@tonic-gate 	int16_t	ioc_state;	/* snapshot ioctl state */
740Sstevel@tonic-gate 	char	*snapshot;	/* snapshot of all prom nodes */
750Sstevel@tonic-gate 	size_t	size;		/* size of snapshot */
760Sstevel@tonic-gate 	prom_generation_cookie_t tree_gen;
770Sstevel@tonic-gate } oprom_state[MAX_OPENS];
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static kmutex_t oprom_lock;	/* serialize instance assignment */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static int opromopen(dev_t *, int, int, cred_t *);
820Sstevel@tonic-gate static int opromioctl(dev_t, int, intptr_t, int, cred_t *, int *);
830Sstevel@tonic-gate static int opromclose(dev_t, int, int, cred_t *);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
860Sstevel@tonic-gate 		void **result);
870Sstevel@tonic-gate static int opattach(dev_info_t *, ddi_attach_cmd_t cmd);
880Sstevel@tonic-gate static int opdetach(dev_info_t *, ddi_detach_cmd_t cmd);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* help functions */
91789Sahrens static int oprom_checknodeid(pnode_t, pnode_t);
920Sstevel@tonic-gate static int oprom_copyinstr(intptr_t, char *, size_t, size_t);
93789Sahrens static int oprom_copynode(pnode_t, uint_t, char **, size_t *);
940Sstevel@tonic-gate static int oprom_snapshot(struct oprom_state *, intptr_t);
950Sstevel@tonic-gate static int oprom_copyout(struct oprom_state *, intptr_t);
960Sstevel@tonic-gate static int oprom_setstate(struct oprom_state *, int16_t);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static struct cb_ops openeepr_cb_ops = {
990Sstevel@tonic-gate 	opromopen,		/* open */
1000Sstevel@tonic-gate 	opromclose,		/* close */
1010Sstevel@tonic-gate 	nodev,			/* strategy */
1020Sstevel@tonic-gate 	nodev,			/* print */
1030Sstevel@tonic-gate 	nodev,			/* dump */
1040Sstevel@tonic-gate 	nodev,			/* read */
1050Sstevel@tonic-gate 	nodev,			/* write */
1060Sstevel@tonic-gate 	opromioctl,		/* ioctl */
1070Sstevel@tonic-gate 	nodev,			/* devmap */
1080Sstevel@tonic-gate 	nodev,			/* mmap */
1090Sstevel@tonic-gate 	nodev,			/* segmap */
1100Sstevel@tonic-gate 	nochpoll,		/* poll */
1110Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1120Sstevel@tonic-gate 	NULL,			/* streamtab  */
1130Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static struct dev_ops openeepr_ops = {
1170Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1180Sstevel@tonic-gate 	0,			/* refcnt  */
1190Sstevel@tonic-gate 	opinfo,			/* info */
1200Sstevel@tonic-gate 	nulldev,		/* identify */
1210Sstevel@tonic-gate 	nulldev,		/* probe */
1220Sstevel@tonic-gate 	opattach,		/* attach */
1230Sstevel@tonic-gate 	opdetach,		/* detach */
1240Sstevel@tonic-gate 	nodev,			/* reset */
1250Sstevel@tonic-gate 	&openeepr_cb_ops,	/* driver operations */
126*7656SSherry.Moore@Sun.COM 	NULL,			/* bus operations */
127*7656SSherry.Moore@Sun.COM 	NULL,			/* power */
128*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Module linkage information for the kernel.
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static struct modldrv modldrv = {
1350Sstevel@tonic-gate 	&mod_driverops,
1367335SLipeng.Sang@Sun.COM 	"OPENPROM/NVRAM Driver",
1370Sstevel@tonic-gate 	&openeepr_ops
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static struct modlinkage modlinkage = {
1410Sstevel@tonic-gate 	MODREV_1,
1420Sstevel@tonic-gate 	&modldrv,
1430Sstevel@tonic-gate 	NULL
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate int
1470Sstevel@tonic-gate _init(void)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	int	error;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	mutex_init(&oprom_lock, NULL, MUTEX_DRIVER, NULL);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1540Sstevel@tonic-gate 	if (error != 0) {
1550Sstevel@tonic-gate 		mutex_destroy(&oprom_lock);
1560Sstevel@tonic-gate 		return (error);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate int
1630Sstevel@tonic-gate _info(struct modinfo *modinfop)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate int
1690Sstevel@tonic-gate _fini(void)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	int	error;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1740Sstevel@tonic-gate 	if (error != 0)
1750Sstevel@tonic-gate 		return (error);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	mutex_destroy(&oprom_lock);
1780Sstevel@tonic-gate 	return (0);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static dev_info_t *opdip;
182789Sahrens static pnode_t options_nodeid;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*ARGSUSED*/
1850Sstevel@tonic-gate static int
1860Sstevel@tonic-gate opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	int error = DDI_FAILURE;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	switch (infocmd) {
1910Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1920Sstevel@tonic-gate 		*result = (void *)opdip;
1930Sstevel@tonic-gate 		error = DDI_SUCCESS;
1940Sstevel@tonic-gate 		break;
1950Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1960Sstevel@tonic-gate 		/* All dev_t's map to the same, single instance */
1970Sstevel@tonic-gate 		*result = (void *)0;
1980Sstevel@tonic-gate 		error = DDI_SUCCESS;
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 	default:
2010Sstevel@tonic-gate 		break;
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	return (error);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static int
2080Sstevel@tonic-gate opattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	switch (cmd) {
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	case DDI_ATTACH:
2130Sstevel@tonic-gate 		if (prom_is_openprom()) {
2140Sstevel@tonic-gate 			options_nodeid = prom_optionsnode();
2150Sstevel@tonic-gate 		} else {
2160Sstevel@tonic-gate 			options_nodeid = OBP_BADNODE;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		opdip = dip;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "openprom", S_IFCHR,
2220Sstevel@tonic-gate 		    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2230Sstevel@tonic-gate 			return (DDI_FAILURE);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		return (DDI_SUCCESS);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	default:
2290Sstevel@tonic-gate 		return (DDI_FAILURE);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static int
2340Sstevel@tonic-gate opdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2370Sstevel@tonic-gate 		return (DDI_FAILURE);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2400Sstevel@tonic-gate 	opdip = NULL;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (DDI_SUCCESS);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
2470Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
2480Sstevel@tonic-gate  * entry in the oprom_state[] table.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate /*ARGSUSED*/
2510Sstevel@tonic-gate static int
2520Sstevel@tonic-gate opromopen(dev_t *devp, int flag, int otyp, cred_t *credp)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	int m;
2550Sstevel@tonic-gate 	struct oprom_state *st = oprom_state;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (getminor(*devp) != 0)
2580Sstevel@tonic-gate 		return (ENXIO);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
2610Sstevel@tonic-gate 	for (m = 0; m < MAX_OPENS; m++)
2620Sstevel@tonic-gate 		if (st->already_open)
2630Sstevel@tonic-gate 			st++;
2640Sstevel@tonic-gate 		else {
2650Sstevel@tonic-gate 			st->already_open = 1;
2660Sstevel@tonic-gate 			/*
2670Sstevel@tonic-gate 			 * It's ours.
2680Sstevel@tonic-gate 			 */
269789Sahrens 			st->current_id = (pnode_t)0;
2700Sstevel@tonic-gate 			ASSERT(st->snapshot == NULL && st->size == 0);
2710Sstevel@tonic-gate 			ASSERT(st->ioc_state == IOC_IDLE);
2720Sstevel@tonic-gate 			break;
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (m == MAX_OPENS)  {
2770Sstevel@tonic-gate 		/*
2780Sstevel@tonic-gate 		 * "Thank you for calling, but all our lines are
2790Sstevel@tonic-gate 		 * busy at the moment.."
2800Sstevel@tonic-gate 		 *
2810Sstevel@tonic-gate 		 * We could get sophisticated here, and go into a
2820Sstevel@tonic-gate 		 * sleep-retry loop .. but hey, I just can't see
2830Sstevel@tonic-gate 		 * that many processes sitting in this driver.
2840Sstevel@tonic-gate 		 *
2850Sstevel@tonic-gate 		 * (And if it does become possible, then we should
2860Sstevel@tonic-gate 		 * change the interface so that the 'state' is held
2870Sstevel@tonic-gate 		 * external to the driver)
2880Sstevel@tonic-gate 		 */
2890Sstevel@tonic-gate 		return (EAGAIN);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)m);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (0);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*ARGSUSED*/
2980Sstevel@tonic-gate static int
2990Sstevel@tonic-gate opromclose(dev_t dev, int flag, int otype, cred_t *cred_p)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct oprom_state *st;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
3040Sstevel@tonic-gate 	ASSERT(getminor(dev) < MAX_OPENS && st->already_open != 0);
3050Sstevel@tonic-gate 	if (st->snapshot) {
3060Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
3070Sstevel@tonic-gate 		st->snapshot = NULL;
3080Sstevel@tonic-gate 		st->size = 0;
3090Sstevel@tonic-gate 		st->ioc_state = IOC_IDLE;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
3120Sstevel@tonic-gate 	st->already_open = 0;
3130Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	return (0);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate struct opromioctl_args {
3190Sstevel@tonic-gate 	struct oprom_state *st;
3200Sstevel@tonic-gate 	int cmd;
3210Sstevel@tonic-gate 	intptr_t arg;
3220Sstevel@tonic-gate 	int mode;
3230Sstevel@tonic-gate };
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*ARGSUSED*/
3260Sstevel@tonic-gate static int
3270Sstevel@tonic-gate opromioctl_cb(void *avp, int has_changed)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	struct opromioctl_args *argp = avp;
3300Sstevel@tonic-gate 	int cmd;
3310Sstevel@tonic-gate 	intptr_t arg;
3320Sstevel@tonic-gate 	int mode;
3330Sstevel@tonic-gate 	struct oprom_state *st;
3340Sstevel@tonic-gate 	struct openpromio *opp;
3350Sstevel@tonic-gate 	int valsize;
3360Sstevel@tonic-gate 	char *valbuf;
3370Sstevel@tonic-gate 	int error = 0;
3380Sstevel@tonic-gate 	uint_t userbufsize;
339789Sahrens 	pnode_t node_id;
3400Sstevel@tonic-gate 	char propname[OBP_MAXPROPNAME];
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	st = argp->st;
3430Sstevel@tonic-gate 	cmd = argp->cmd;
3440Sstevel@tonic-gate 	arg = argp->arg;
3450Sstevel@tonic-gate 	mode = argp->mode;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (has_changed) {
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * The prom tree has changed since we last used current_id,
3500Sstevel@tonic-gate 		 * so we need to check it.
3510Sstevel@tonic-gate 		 */
3520Sstevel@tonic-gate 		if ((st->current_id != OBP_NONODE) &&
3530Sstevel@tonic-gate 		    (st->current_id != OBP_BADNODE)) {
3540Sstevel@tonic-gate 			if (oprom_checknodeid(st->current_id, OBP_NONODE) == 0)
3550Sstevel@tonic-gate 				st->current_id = OBP_BADNODE;
3560Sstevel@tonic-gate 		}
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Check permissions
3610Sstevel@tonic-gate 	 * and weed out unsupported commands on x86 platform
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	switch (cmd) {
3640Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
3650Sstevel@tonic-gate 	case OPROMLISTKEYSLEN:
3660Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
3670Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
3680Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
3690Sstevel@tonic-gate 		opp->oprom_size = valsize;
3700Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
3717009Scth 			error = EFAULT;
3720Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
3730Sstevel@tonic-gate 		break;
3740Sstevel@tonic-gate 	case OPROMLISTKEYS:
3750Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
3760Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
3777009Scth 			return (EFAULT);
3780Sstevel@tonic-gate 		if (valsize > userbufsize)
3797009Scth 			return (EINVAL);
3800Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
3810Sstevel@tonic-gate 		if (prom_asr_list_keys((caddr_t)valbuf) == -1) {
3820Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
3830Sstevel@tonic-gate 			return (EFAULT);
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
3860Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
3870Sstevel@tonic-gate 		opp->oprom_size = valsize;
3880Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
3890Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
3907009Scth 			error = EFAULT;
3910Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
3920Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
3930Sstevel@tonic-gate 		break;
3940Sstevel@tonic-gate 	case OPROMEXPORT:
3950Sstevel@tonic-gate 		valsize = prom_asr_export_len();
3960Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
3977009Scth 			return (EFAULT);
3980Sstevel@tonic-gate 		if (valsize > userbufsize)
3997009Scth 			return (EINVAL);
4000Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
4010Sstevel@tonic-gate 		if (prom_asr_export((caddr_t)valbuf) == -1) {
4020Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
4030Sstevel@tonic-gate 			return (EFAULT);
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4060Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
4070Sstevel@tonic-gate 		opp->oprom_size = valsize;
4080Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
4090Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
4107009Scth 			error = EFAULT;
4110Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
4120Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
4130Sstevel@tonic-gate 		break;
4140Sstevel@tonic-gate 	case OPROMEXPORTLEN:
4150Sstevel@tonic-gate 		valsize = prom_asr_export_len();
4160Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4170Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
4180Sstevel@tonic-gate 		opp->oprom_size = valsize;
4190Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
4207009Scth 			error = EFAULT;
4210Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate #endif
4240Sstevel@tonic-gate 	case OPROMGETOPT:
4250Sstevel@tonic-gate 	case OPROMNXTOPT:
4260Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4270Sstevel@tonic-gate 			return (EPERM);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		node_id = options_nodeid;
4300Sstevel@tonic-gate 		break;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	case OPROMSETOPT:
4330Sstevel@tonic-gate 	case OPROMSETOPT2:
4340Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4350Sstevel@tonic-gate 		if (mode & FWRITE) {
4360Sstevel@tonic-gate 			node_id = options_nodeid;
4370Sstevel@tonic-gate 			break;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate #endif /* !__i386 && !__amd64 */
4400Sstevel@tonic-gate 		return (EPERM);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	case OPROMNEXT:
4430Sstevel@tonic-gate 	case OPROMCHILD:
4440Sstevel@tonic-gate 	case OPROMGETPROP:
4450Sstevel@tonic-gate 	case OPROMGETPROPLEN:
4460Sstevel@tonic-gate 	case OPROMNXTPROP:
4470Sstevel@tonic-gate 	case OPROMSETNODEID:
4480Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4490Sstevel@tonic-gate 			return (EPERM);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		node_id = st->current_id;
4520Sstevel@tonic-gate 		break;
4530Sstevel@tonic-gate 	case OPROMCOPYOUT:
4540Sstevel@tonic-gate 		if (st->snapshot == NULL)
4550Sstevel@tonic-gate 			return (EINVAL);
4560Sstevel@tonic-gate 		/*FALLTHROUGH*/
4570Sstevel@tonic-gate 	case OPROMSNAPSHOT:
4580Sstevel@tonic-gate 	case OPROMGETCONS:
4590Sstevel@tonic-gate 	case OPROMGETBOOTARGS:
4600Sstevel@tonic-gate 	case OPROMGETVERSION:
4610Sstevel@tonic-gate 	case OPROMPATH2DRV:
4620Sstevel@tonic-gate 	case OPROMPROM2DEVNAME:
4630Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4640Sstevel@tonic-gate 	case OPROMGETFBNAME:
4650Sstevel@tonic-gate 	case OPROMDEV2PROMNAME:
4660Sstevel@tonic-gate 	case OPROMREADY64:
4670Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
4680Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4690Sstevel@tonic-gate 			return (EPERM);
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 		break;
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4740Sstevel@tonic-gate 	case WANBOOT_SETKEY:
4750Sstevel@tonic-gate 		if (!(mode & FWRITE))
4760Sstevel@tonic-gate 			return (EPERM);
4770Sstevel@tonic-gate 		break;
4780Sstevel@tonic-gate #endif	/* !__i386 && !defined(__amd64) */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	default:
4810Sstevel@tonic-gate 		return (EINVAL);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * Deal with SNAPSHOT and COPYOUT ioctls first
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	switch (cmd) {
4880Sstevel@tonic-gate 	case OPROMCOPYOUT:
4890Sstevel@tonic-gate 		return (oprom_copyout(st, arg));
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	case OPROMSNAPSHOT:
4920Sstevel@tonic-gate 		return (oprom_snapshot(st, arg));
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * Copy in user argument length and allocation memory
4970Sstevel@tonic-gate 	 *
4980Sstevel@tonic-gate 	 * NB do not copyin the entire buffer we may not need
4990Sstevel@tonic-gate 	 *	to. userbufsize can be as big as 32 K.
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
5020Sstevel@tonic-gate 		return (EFAULT);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	if (userbufsize == 0 || userbufsize > OPROMMAXPARAM)
5050Sstevel@tonic-gate 		return (EINVAL);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	opp = (struct openpromio *)kmem_zalloc(
5080Sstevel@tonic-gate 	    userbufsize + sizeof (uint_t) + 1, KM_SLEEP);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/*
5110Sstevel@tonic-gate 	 * Execute command
5120Sstevel@tonic-gate 	 */
5130Sstevel@tonic-gate 	switch (cmd) {
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	case OPROMGETOPT:
5160Sstevel@tonic-gate 	case OPROMGETPROP:
5170Sstevel@tonic-gate 	case OPROMGETPROPLEN:
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
5200Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
5210Sstevel@tonic-gate 			error = EINVAL;
5220Sstevel@tonic-gate 			break;
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
5270Sstevel@tonic-gate 		 */
5280Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
5290Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
5300Sstevel@tonic-gate 			break;
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 		(void) strcpy(propname, opp->oprom_array);
5330Sstevel@tonic-gate 		valsize = prom_getproplen(node_id, propname);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		/*
5360Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but not an option.
5370Sstevel@tonic-gate 		 */
5380Sstevel@tonic-gate 		if ((cmd == OPROMGETOPT) && (strcmp("name", propname) == 0))
5390Sstevel@tonic-gate 			valsize = -1;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		if (cmd == OPROMGETPROPLEN)  {
5420Sstevel@tonic-gate 			int proplen = valsize;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 			if (userbufsize < sizeof (int)) {
5450Sstevel@tonic-gate 				error = EINVAL;
5460Sstevel@tonic-gate 				break;
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 			opp->oprom_size = valsize = sizeof (int);
5490Sstevel@tonic-gate 			bcopy(&proplen, opp->oprom_array, valsize);
5500Sstevel@tonic-gate 		} else if (valsize > 0 && valsize <= userbufsize) {
5510Sstevel@tonic-gate 			bzero(opp->oprom_array, valsize + 1);
5520Sstevel@tonic-gate 			(void) prom_getprop(node_id, propname,
5530Sstevel@tonic-gate 			    opp->oprom_array);
5540Sstevel@tonic-gate 			opp->oprom_size = valsize;
5550Sstevel@tonic-gate 			if (valsize < userbufsize)
5560Sstevel@tonic-gate 				++valsize;	/* Forces NULL termination */
5570Sstevel@tonic-gate 						/* If space permits */
5580Sstevel@tonic-gate 		} else {
5590Sstevel@tonic-gate 			/*
5600Sstevel@tonic-gate 			 * XXX: There is no error code if the buf is too small.
5610Sstevel@tonic-gate 			 * which is consistent with the current behavior.
5620Sstevel@tonic-gate 			 *
5630Sstevel@tonic-gate 			 * NB: This clause also handles the non-error
5640Sstevel@tonic-gate 			 * zero length (boolean) property value case.
5650Sstevel@tonic-gate 			 */
5660Sstevel@tonic-gate 			opp->oprom_size = 0;
5670Sstevel@tonic-gate 			(void) strcpy(opp->oprom_array, "");
5680Sstevel@tonic-gate 			valsize = 1;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
5710Sstevel@tonic-gate 			error = EFAULT;
5720Sstevel@tonic-gate 		break;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	case OPROMNXTOPT:
5750Sstevel@tonic-gate 	case OPROMNXTPROP:
5760Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
5770Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
5780Sstevel@tonic-gate 			error = EINVAL;
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 		/*
5830Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
5860Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
5870Sstevel@tonic-gate 			break;
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 		valbuf = (char *)prom_nextprop(node_id, opp->oprom_array,
5900Sstevel@tonic-gate 		    propname);
5910Sstevel@tonic-gate 		valsize = strlen(valbuf);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but it's not an option.
5950Sstevel@tonic-gate 		 */
5960Sstevel@tonic-gate 		if ((cmd == OPROMNXTOPT) && valsize &&
5970Sstevel@tonic-gate 		    (strcmp(valbuf, "name") == 0)) {
5980Sstevel@tonic-gate 			valbuf = (char *)prom_nextprop(node_id, "name",
5990Sstevel@tonic-gate 			    propname);
6000Sstevel@tonic-gate 			valsize = strlen(valbuf);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		if (valsize == 0) {
6040Sstevel@tonic-gate 			opp->oprom_size = 0;
6050Sstevel@tonic-gate 		} else if (++valsize <= userbufsize) {
6060Sstevel@tonic-gate 			opp->oprom_size = valsize;
6070Sstevel@tonic-gate 			bzero((caddr_t)opp->oprom_array, (size_t)valsize);
6080Sstevel@tonic-gate 			bcopy((caddr_t)valbuf, (caddr_t)opp->oprom_array,
6090Sstevel@tonic-gate 			    (size_t)valsize);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
6130Sstevel@tonic-gate 			error = EFAULT;
6140Sstevel@tonic-gate 		break;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	case OPROMNEXT:
6170Sstevel@tonic-gate 	case OPROMCHILD:
6180Sstevel@tonic-gate 	case OPROMSETNODEID:
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		if (prom_is_openprom() == 0 ||
621789Sahrens 		    userbufsize < sizeof (pnode_t)) {
6220Sstevel@tonic-gate 			error = EINVAL;
6230Sstevel@tonic-gate 			break;
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 		/*
627789Sahrens 		 * The argument is a phandle. (aka pnode_t)
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
630789Sahrens 		    opp->oprom_array, sizeof (pnode_t)) != 0) {
6310Sstevel@tonic-gate 			error = EFAULT;
6320Sstevel@tonic-gate 			break;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		/*
636789Sahrens 		 * If pnode_t from userland is garbage, we
6370Sstevel@tonic-gate 		 * could confuse the PROM.
6380Sstevel@tonic-gate 		 */
639789Sahrens 		node_id = *(pnode_t *)opp->oprom_array;
6400Sstevel@tonic-gate 		if (oprom_checknodeid(node_id, st->current_id) == 0) {
6410Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!nodeid 0x%x not found",
6420Sstevel@tonic-gate 			    (int)node_id);
6430Sstevel@tonic-gate 			error = EINVAL;
6440Sstevel@tonic-gate 			break;
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		if (cmd == OPROMNEXT)
6480Sstevel@tonic-gate 			st->current_id = prom_nextnode(node_id);
6490Sstevel@tonic-gate 		else if (cmd == OPROMCHILD)
6500Sstevel@tonic-gate 			st->current_id = prom_childnode(node_id);
6510Sstevel@tonic-gate 		else {
6520Sstevel@tonic-gate 			/* OPROMSETNODEID */
6530Sstevel@tonic-gate 			st->current_id = node_id;
6540Sstevel@tonic-gate 			break;
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
657789Sahrens 		opp->oprom_size = sizeof (pnode_t);
658789Sahrens 		*(pnode_t *)opp->oprom_array = st->current_id;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
661789Sahrens 		    sizeof (pnode_t) + sizeof (uint_t)) != 0)
6620Sstevel@tonic-gate 			error = EFAULT;
6630Sstevel@tonic-gate 		break;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	case OPROMGETCONS:
6660Sstevel@tonic-gate 		/*
6670Sstevel@tonic-gate 		 * Is openboot supported on this machine?
6680Sstevel@tonic-gate 		 * This ioctl used to return the console device,
6690Sstevel@tonic-gate 		 * information; this is now done via modctl()
6700Sstevel@tonic-gate 		 * in libdevinfo.
6710Sstevel@tonic-gate 		 */
6720Sstevel@tonic-gate 		opp->oprom_size = sizeof (char);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		opp->oprom_array[0] |= prom_is_openprom() ?
6750Sstevel@tonic-gate 		    OPROMCONS_OPENPROM : 0;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		/*
6780Sstevel@tonic-gate 		 * The rest of the info is needed by Install to
6790Sstevel@tonic-gate 		 * decide if graphics should be started.
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
6820Sstevel@tonic-gate 		    plat_stdin_is_keyboard()) {
6830Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDIN_IS_KBD;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
6870Sstevel@tonic-gate 		    plat_stdout_is_framebuffer()) {
6880Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDOUT_IS_FB;
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
6920Sstevel@tonic-gate 		    sizeof (char) + sizeof (uint_t)) != 0)
6930Sstevel@tonic-gate 			error = EFAULT;
6940Sstevel@tonic-gate 		break;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	case OPROMGETBOOTARGS: {
6970Sstevel@tonic-gate 		extern char kern_bootargs[];
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		valsize = strlen(kern_bootargs) + 1;
7000Sstevel@tonic-gate 		if (valsize > userbufsize) {
7010Sstevel@tonic-gate 			error = EINVAL;
7020Sstevel@tonic-gate 			break;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, kern_bootargs);
7050Sstevel@tonic-gate 		opp->oprom_size = valsize - 1;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
7080Sstevel@tonic-gate 			error = EFAULT;
7097009Scth 		break;
7107009Scth 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * convert a prom device path to an equivalent devfs path
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate 	case OPROMPROM2DEVNAME: {
7160Sstevel@tonic-gate 		char *dev_name;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
7200Sstevel@tonic-gate 		 */
7210Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
7220Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
7230Sstevel@tonic-gate 			break;
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		dev_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		error = i_promname_to_devname(opp->oprom_array, dev_name);
7290Sstevel@tonic-gate 		if (error != 0) {
7300Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7310Sstevel@tonic-gate 			break;
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 		valsize = opp->oprom_size = strlen(dev_name);
7340Sstevel@tonic-gate 		if (++valsize > userbufsize) {
7350Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7360Sstevel@tonic-gate 			error = EINVAL;
7370Sstevel@tonic-gate 			break;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, dev_name);
7400Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
7410Sstevel@tonic-gate 			error = EFAULT;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		kmem_free(dev_name, MAXPATHLEN);
7447009Scth 		break;
7457009Scth 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * Convert a prom device path name to a driver name
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	case OPROMPATH2DRV: {
7510Sstevel@tonic-gate 		char *drv_name;
7520Sstevel@tonic-gate 		major_t maj;
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
7580Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
7590Sstevel@tonic-gate 			break;
7600Sstevel@tonic-gate 		}
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		/*
7630Sstevel@tonic-gate 		 * convert path to a driver binding name
7640Sstevel@tonic-gate 		 */
7650Sstevel@tonic-gate 		maj = path_to_major((char *)opp->oprom_array);
7667009Scth 		if (maj == DDI_MAJOR_T_NONE) {
7670Sstevel@tonic-gate 			error = EINVAL;
7680Sstevel@tonic-gate 			break;
7690Sstevel@tonic-gate 		}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 		/*
7720Sstevel@tonic-gate 		 * resolve any aliases
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		if ((drv_name = ddi_major_to_name(maj)) == NULL) {
7750Sstevel@tonic-gate 			error = EINVAL;
7760Sstevel@tonic-gate 			break;
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, drv_name);
7800Sstevel@tonic-gate 		opp->oprom_size = strlen(drv_name);
7810Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
7820Sstevel@tonic-gate 		    sizeof (uint_t) + opp->oprom_size + 1) != 0)
7830Sstevel@tonic-gate 			error = EFAULT;
7847009Scth 		break;
7857009Scth 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	case OPROMGETVERSION:
7880Sstevel@tonic-gate 		/*
7890Sstevel@tonic-gate 		 * Get a string representing the running version of the
7900Sstevel@tonic-gate 		 * prom. How to create such a string is platform dependent,
7910Sstevel@tonic-gate 		 * so we just defer to a promif function. If no such
7920Sstevel@tonic-gate 		 * association exists, the promif implementation
7930Sstevel@tonic-gate 		 * may copy the string "unknown" into the given buffer,
7940Sstevel@tonic-gate 		 * and return its length (incl. NULL terminator).
7950Sstevel@tonic-gate 		 *
7960Sstevel@tonic-gate 		 * We expect prom_version_name to return the actual
7970Sstevel@tonic-gate 		 * length of the string, but copy at most userbufsize
7980Sstevel@tonic-gate 		 * bytes into the given buffer, including NULL termination.
7990Sstevel@tonic-gate 		 */
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		valsize = prom_version_name(opp->oprom_array, userbufsize);
8020Sstevel@tonic-gate 		if (valsize < 0) {
8030Sstevel@tonic-gate 			error = EINVAL;
8040Sstevel@tonic-gate 			break;
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		/*
8080Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
8090Sstevel@tonic-gate 		 */
8100Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
8110Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
8120Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
8130Sstevel@tonic-gate 			error = EFAULT;
8140Sstevel@tonic-gate 		break;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
8170Sstevel@tonic-gate 	case OPROMGETFBNAME:
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * Return stdoutpath, if it's a frame buffer.
8200Sstevel@tonic-gate 		 * Yes, we are comparing a possibly longer string against
8210Sstevel@tonic-gate 		 * the size we're really going to copy, but so what?
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
8240Sstevel@tonic-gate 		    (prom_stdout_is_framebuffer() != 0) &&
8250Sstevel@tonic-gate 		    (userbufsize > strlen(prom_stdoutpath()))) {
8260Sstevel@tonic-gate 			prom_strip_options(prom_stdoutpath(),
8270Sstevel@tonic-gate 			    opp->oprom_array);	/* strip options and copy */
8280Sstevel@tonic-gate 			valsize = opp->oprom_size = strlen(opp->oprom_array);
8290Sstevel@tonic-gate 			if (copyout(opp, (void *)arg,
8300Sstevel@tonic-gate 			    valsize + 1 + sizeof (uint_t)) != 0)
8310Sstevel@tonic-gate 				error = EFAULT;
8320Sstevel@tonic-gate 		} else
8330Sstevel@tonic-gate 			error = EINVAL;
8340Sstevel@tonic-gate 		break;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Convert a logical or physical device path to prom device path
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	case OPROMDEV2PROMNAME: {
8400Sstevel@tonic-gate 		char *prom_name;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 		/*
8430Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
8440Sstevel@tonic-gate 		 */
8450Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
8460Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
8470Sstevel@tonic-gate 			break;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		prom_name = kmem_alloc(userbufsize, KM_SLEEP);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		/*
8530Sstevel@tonic-gate 		 * convert the devfs path to an equivalent prom path
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		error = i_devname_to_promname(opp->oprom_array, prom_name,
8560Sstevel@tonic-gate 		    userbufsize);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (error != 0) {
8590Sstevel@tonic-gate 			kmem_free(prom_name, userbufsize);
8600Sstevel@tonic-gate 			break;
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		for (valsize = 0; valsize < userbufsize; valsize++) {
8640Sstevel@tonic-gate 			opp->oprom_array[valsize] = prom_name[valsize];
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 			if ((valsize > 0) && (prom_name[valsize] == '\0') &&
8670Sstevel@tonic-gate 			    (prom_name[valsize-1] == '\0')) {
8680Sstevel@tonic-gate 				break;
8690Sstevel@tonic-gate 			}
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 		opp->oprom_size = valsize;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		kmem_free(prom_name, userbufsize);
8740Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
8750Sstevel@tonic-gate 			error = EFAULT;
8760Sstevel@tonic-gate 
8777009Scth 		break;
8787009Scth 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	case OPROMSETOPT:
8810Sstevel@tonic-gate 	case OPROMSETOPT2: {
8820Sstevel@tonic-gate 		int namebuflen;
8830Sstevel@tonic-gate 		int valbuflen;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
8860Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
8870Sstevel@tonic-gate 			error = EINVAL;
8880Sstevel@tonic-gate 			break;
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 		/*
8920Sstevel@tonic-gate 		 * The arguments are a property name and a value.
8930Sstevel@tonic-gate 		 * Copy in the entire user buffer.
8940Sstevel@tonic-gate 		 */
8950Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
8960Sstevel@tonic-gate 		    opp->oprom_array, userbufsize) != 0) {
8970Sstevel@tonic-gate 			error = EFAULT;
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		/*
9020Sstevel@tonic-gate 		 * The property name is the first string, value second
9030Sstevel@tonic-gate 		 */
9040Sstevel@tonic-gate 		namebuflen = strlen(opp->oprom_array);
9050Sstevel@tonic-gate 		valbuf = opp->oprom_array + namebuflen + 1;
9060Sstevel@tonic-gate 		valbuflen = strlen(valbuf);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (cmd == OPROMSETOPT) {
9090Sstevel@tonic-gate 			valsize = valbuflen + 1;  /* +1 for the '\0' */
9100Sstevel@tonic-gate 		} else {
9110Sstevel@tonic-gate 			if ((namebuflen + 1 + valbuflen + 1) > userbufsize) {
9120Sstevel@tonic-gate 				error = EINVAL;
9130Sstevel@tonic-gate 				break;
9140Sstevel@tonic-gate 			}
9150Sstevel@tonic-gate 			valsize = (opp->oprom_array + userbufsize) - valbuf;
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		/*
9190Sstevel@tonic-gate 		 * 4010173: 'name' is not an option, but it is a property.
9200Sstevel@tonic-gate 		 */
9210Sstevel@tonic-gate 		if (strcmp(opp->oprom_array, "name") == 0)
9220Sstevel@tonic-gate 			error = EINVAL;
9230Sstevel@tonic-gate 		else if (prom_setprop(node_id, opp->oprom_array,
9240Sstevel@tonic-gate 		    valbuf, valsize) < 0)
9250Sstevel@tonic-gate 			error = EINVAL;
9260Sstevel@tonic-gate 
9277009Scth 		break;
9287009Scth 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	case OPROMREADY64: {
9310Sstevel@tonic-gate 		struct openprom_opr64 *opr =
9320Sstevel@tonic-gate 		    (struct openprom_opr64 *)opp->oprom_array;
9330Sstevel@tonic-gate 		int i;
934789Sahrens 		pnode_t id;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		if (userbufsize < sizeof (*opr)) {
9370Sstevel@tonic-gate 			error = EINVAL;
9380Sstevel@tonic-gate 			break;
9390Sstevel@tonic-gate 		}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 		valsize = userbufsize -
9420Sstevel@tonic-gate 		    offsetof(struct openprom_opr64, message);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		i = prom_version_check(opr->message, valsize, &id);
9450Sstevel@tonic-gate 		opr->return_code = i;
9460Sstevel@tonic-gate 		opr->nodeid = (int)id;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		valsize = offsetof(struct openprom_opr64, message);
9490Sstevel@tonic-gate 		valsize += strlen(opr->message) + 1;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 		/*
9520Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
9530Sstevel@tonic-gate 		 */
9540Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
9550Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
9560Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
9570Sstevel@tonic-gate 			error = EFAULT;
9580Sstevel@tonic-gate 		break;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	}	/* case OPROMREADY64 */
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	case WANBOOT_SETKEY: {
9630Sstevel@tonic-gate 		struct wankeyio *wp;
9640Sstevel@tonic-gate 		int reslen;
9650Sstevel@tonic-gate 		int status;
9660Sstevel@tonic-gate 		int rv;
9670Sstevel@tonic-gate 		int i;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 		/*
9700Sstevel@tonic-gate 		 * The argument is a struct wankeyio.  Validate it as best
9710Sstevel@tonic-gate 		 * we can.
9720Sstevel@tonic-gate 		 */
9730Sstevel@tonic-gate 		if (userbufsize != (sizeof (struct wankeyio))) {
9740Sstevel@tonic-gate 			error = EINVAL;
9750Sstevel@tonic-gate 			break;
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
9780Sstevel@tonic-gate 		    opp->oprom_array, sizeof (struct wankeyio)) != 0) {
9790Sstevel@tonic-gate 			error = EFAULT;
9800Sstevel@tonic-gate 			break;
9810Sstevel@tonic-gate 		}
9820Sstevel@tonic-gate 		wp = (struct wankeyio *)opp->oprom_array;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		/* check for key name and key size overflow */
9850Sstevel@tonic-gate 		for (i = 0; i < WANBOOT_MAXKEYNAMELEN; i++)
9860Sstevel@tonic-gate 			if (wp->wk_keyname[i] == '\0')
9870Sstevel@tonic-gate 				break;
9880Sstevel@tonic-gate 		if ((i == WANBOOT_MAXKEYNAMELEN) ||
9890Sstevel@tonic-gate 		    (wp->wk_keysize > WANBOOT_MAXKEYLEN)) {
9900Sstevel@tonic-gate 			error = EINVAL;
9910Sstevel@tonic-gate 			break;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		rv = prom_set_security_key(wp->wk_keyname, wp->wk_u.key,
9950Sstevel@tonic-gate 		    wp->wk_keysize, &reslen, &status);
9960Sstevel@tonic-gate 		if (rv)
9970Sstevel@tonic-gate 			error = EIO;
9980Sstevel@tonic-gate 		else
9990Sstevel@tonic-gate 			switch (status) {
10000Sstevel@tonic-gate 				case 0:
10010Sstevel@tonic-gate 					error = 0;
10020Sstevel@tonic-gate 					break;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 				case -2:	/* out of key storage space */
10050Sstevel@tonic-gate 					error = ENOSPC;
10060Sstevel@tonic-gate 					break;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 				case -3:	/* key name or value too long */
10090Sstevel@tonic-gate 					error = EINVAL;
10100Sstevel@tonic-gate 					break;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 				case -4:	/* can't delete:  no such key */
10130Sstevel@tonic-gate 					error = ENOENT;
10140Sstevel@tonic-gate 					break;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 				case -1:	/* unspecified error */
10170Sstevel@tonic-gate 				default:	/* this should not happen */
10180Sstevel@tonic-gate 					error = EIO;
10190Sstevel@tonic-gate 					break;
10200Sstevel@tonic-gate 			}
10210Sstevel@tonic-gate 		break;
10220Sstevel@tonic-gate 	}	/* case WANBOOT_SETKEY */
10230Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
10240Sstevel@tonic-gate 	}	/* switch (cmd)	*/
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	kmem_free(opp, userbufsize + sizeof (uint_t) + 1);
10270Sstevel@tonic-gate 	return (error);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate /*ARGSUSED*/
10310Sstevel@tonic-gate static int
10320Sstevel@tonic-gate opromioctl(dev_t dev, int cmd, intptr_t arg, int mode,
10330Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	struct oprom_state *st;
10360Sstevel@tonic-gate 	struct opromioctl_args arg_block;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if (getminor(dev) >= MAX_OPENS)
10390Sstevel@tonic-gate 		return (ENXIO);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
10420Sstevel@tonic-gate 	ASSERT(st->already_open);
10430Sstevel@tonic-gate 	arg_block.st = st;
10440Sstevel@tonic-gate 	arg_block.cmd = cmd;
10450Sstevel@tonic-gate 	arg_block.arg = arg;
10460Sstevel@tonic-gate 	arg_block.mode = mode;
10470Sstevel@tonic-gate 	return (prom_tree_access(opromioctl_cb, &arg_block, &st->tree_gen));
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate /*
10510Sstevel@tonic-gate  * Copyin string and verify the actual string length is less than maxsize
10520Sstevel@tonic-gate  * specified by the caller.
10530Sstevel@tonic-gate  *
10540Sstevel@tonic-gate  * Currently, maxsize is either OBP_MAXPROPNAME for property names
10550Sstevel@tonic-gate  * or MAXPATHLEN for device path names. userbufsize is specified
10560Sstevel@tonic-gate  * by the userland caller.
10570Sstevel@tonic-gate  */
10580Sstevel@tonic-gate static int
10590Sstevel@tonic-gate oprom_copyinstr(intptr_t arg, char *buf, size_t bufsize, size_t maxsize)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	int error;
10620Sstevel@tonic-gate 	size_t actual_len;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	if ((error = copyinstr(((caddr_t)arg + sizeof (uint_t)),
10650Sstevel@tonic-gate 	    buf, bufsize, &actual_len)) != 0) {
10660Sstevel@tonic-gate 		return (error);
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 	if ((actual_len == 0) || (actual_len > maxsize)) {
10690Sstevel@tonic-gate 		return (EINVAL);
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	return (0);
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
1076789Sahrens  * Check pnode_t passed in from userland
10770Sstevel@tonic-gate  */
10780Sstevel@tonic-gate static int
1079789Sahrens oprom_checknodeid(pnode_t node_id, pnode_t current_id)
10800Sstevel@tonic-gate {
10810Sstevel@tonic-gate 	int depth;
1082789Sahrens 	pnode_t id[OBP_STACKDEPTH];
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * optimized path
10860Sstevel@tonic-gate 	 */
10870Sstevel@tonic-gate 	if (node_id == 0) {
10880Sstevel@tonic-gate 		return (1);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	if (node_id == OBP_BADNODE) {
10910Sstevel@tonic-gate 		return (0);
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 	if ((current_id != OBP_BADNODE) && ((node_id == current_id) ||
10940Sstevel@tonic-gate 	    (node_id == prom_nextnode(current_id)) ||
10950Sstevel@tonic-gate 	    (node_id == prom_childnode(current_id)))) {
10960Sstevel@tonic-gate 		return (1);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * long path: walk from root till we find node_id
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	depth = 1;
1103789Sahrens 	id[0] = prom_nextnode((pnode_t)0);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	while (depth) {
11060Sstevel@tonic-gate 		if (id[depth - 1] == node_id)
11070Sstevel@tonic-gate 			return (1);	/* node_id found */
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		if (id[depth] = prom_childnode(id[depth - 1])) {
11100Sstevel@tonic-gate 			depth++;
11110Sstevel@tonic-gate 			continue;
11120Sstevel@tonic-gate 		}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 		while (depth &&
11150Sstevel@tonic-gate 		    ((id[depth - 1] = prom_nextnode(id[depth - 1])) == 0))
11160Sstevel@tonic-gate 			depth--;
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	return (0);	/* node_id not found */
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate static int
11220Sstevel@tonic-gate oprom_copytree(struct oprom_state *st, uint_t flag)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate 	ASSERT(st->snapshot == NULL && st->size == 0);
11250Sstevel@tonic-gate 	return (oprom_copynode(
11260Sstevel@tonic-gate 	    prom_nextnode(0), flag, &st->snapshot, &st->size));
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate static int
11300Sstevel@tonic-gate oprom_snapshot(struct oprom_state *st, intptr_t arg)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	uint_t flag;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_SNAP) == -1)
11350Sstevel@tonic-gate 		return (EBUSY);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* copyin flag and create snapshot */
11380Sstevel@tonic-gate 	if ((copyin((void *)arg, &flag, sizeof (uint_t)) != 0) ||
11390Sstevel@tonic-gate 	    (oprom_copytree(st, flag) != 0)) {
11400Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
11410Sstevel@tonic-gate 		return (EFAULT);
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/* copyout the size of the snapshot */
11460Sstevel@tonic-gate 	flag = (uint_t)st->size;
11470Sstevel@tonic-gate 	if (copyout(&flag, (void *)arg, sizeof (uint_t)) != 0) {
11480Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
11490Sstevel@tonic-gate 		st->snapshot = NULL;
11500Sstevel@tonic-gate 		st->size = 0;
11510Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
11520Sstevel@tonic-gate 		return (EFAULT);
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_DONE);
11560Sstevel@tonic-gate 	return (0);
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate static int
11600Sstevel@tonic-gate oprom_copyout(struct oprom_state *st, intptr_t arg)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	int error = 0;
11630Sstevel@tonic-gate 	uint_t size;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_COPY) == -1)
11660Sstevel@tonic-gate 		return (EBUSY);
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	/* copyin size and copyout snapshot */
11690Sstevel@tonic-gate 	if (copyin((void *)arg, &size, sizeof (uint_t)) != 0)
11700Sstevel@tonic-gate 		error = EFAULT;
11710Sstevel@tonic-gate 	else if (size < st->size)
11720Sstevel@tonic-gate 		error = EINVAL;
11730Sstevel@tonic-gate 	else if (copyout(st->snapshot, (void *)arg, st->size) != 0)
11740Sstevel@tonic-gate 		error = EFAULT;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	if (error) {
11770Sstevel@tonic-gate 		/*
11780Sstevel@tonic-gate 		 * on error keep the snapshot until a successful
11790Sstevel@tonic-gate 		 * copyout or when the driver is closed.
11800Sstevel@tonic-gate 		 */
11810Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_DONE);
11820Sstevel@tonic-gate 		return (error);
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	kmem_free(st->snapshot, st->size);
11860Sstevel@tonic-gate 	st->snapshot = NULL;
11870Sstevel@tonic-gate 	st->size = 0;
11880Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_IDLE);
11890Sstevel@tonic-gate 	return (0);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * Copy all properties of nodeid into a single packed nvlist
11940Sstevel@tonic-gate  */
11950Sstevel@tonic-gate static int
1196789Sahrens oprom_copyprop(pnode_t nodeid, uint_t flag, nvlist_t *nvl)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	int proplen;
11990Sstevel@tonic-gate 	char *propname, *propval, *buf1, *buf2;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	ASSERT(nvl != NULL);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	/*
12040Sstevel@tonic-gate 	 * non verbose mode, get the "name" property only
12050Sstevel@tonic-gate 	 */
12060Sstevel@tonic-gate 	if (flag == 0) {
12070Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, "name");
12080Sstevel@tonic-gate 		if (proplen <= 0) {
12090Sstevel@tonic-gate 			cmn_err(CE_WARN,
12100Sstevel@tonic-gate 			    "failed to get the name of openprom node 0x%x",
12110Sstevel@tonic-gate 			    nodeid);
12120Sstevel@tonic-gate 			(void) nvlist_add_string(nvl, "name", "");
12130Sstevel@tonic-gate 			return (0);
12140Sstevel@tonic-gate 		}
12150Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
12160Sstevel@tonic-gate 		(void) prom_getprop(nodeid, "name", propval);
12170Sstevel@tonic-gate 		(void) nvlist_add_string(nvl, "name", propval);
12180Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
12190Sstevel@tonic-gate 		return (0);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	/*
12230Sstevel@tonic-gate 	 * Ask for first property by passing a NULL string
12240Sstevel@tonic-gate 	 */
12250Sstevel@tonic-gate 	buf1 = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
12260Sstevel@tonic-gate 	buf2 = kmem_zalloc(OBP_MAXPROPNAME, KM_SLEEP);
12270Sstevel@tonic-gate 	buf1[0] = '\0';
12280Sstevel@tonic-gate 	while (propname = (char *)prom_nextprop(nodeid, buf1, buf2)) {
12290Sstevel@tonic-gate 		if (strlen(propname) == 0)
12300Sstevel@tonic-gate 			break;		/* end of prop list */
12310Sstevel@tonic-gate 		(void) strcpy(buf1, propname);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, propname);
12340Sstevel@tonic-gate 		if (proplen == 0) {
12350Sstevel@tonic-gate 			/* boolean property */
12360Sstevel@tonic-gate 			(void) nvlist_add_boolean(nvl, propname);
12370Sstevel@tonic-gate 			continue;
12380Sstevel@tonic-gate 		}
12390Sstevel@tonic-gate 		/* add 1 for null termination in case of a string */
12400Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
12410Sstevel@tonic-gate 		(void) prom_getprop(nodeid, propname, propval);
12420Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, propname,
12430Sstevel@tonic-gate 		    (uchar_t *)propval, proplen + 1);
12440Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
12450Sstevel@tonic-gate 		bzero(buf2, OBP_MAXPROPNAME);
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	kmem_free(buf1, OBP_MAXPROPNAME);
12490Sstevel@tonic-gate 	kmem_free(buf2, OBP_MAXPROPNAME);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	return (0);
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate /*
12550Sstevel@tonic-gate  * Copy all children and descendents into a a packed nvlist
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate static int
1258789Sahrens oprom_copychild(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	nvlist_t *nvl;
1261789Sahrens 	pnode_t child = prom_childnode(nodeid);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	if (child == 0)
12640Sstevel@tonic-gate 		return (0);
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, 0, KM_SLEEP);
12670Sstevel@tonic-gate 	while (child != 0) {
12680Sstevel@tonic-gate 		char *nodebuf = NULL;
12690Sstevel@tonic-gate 		size_t nodesize = 0;
12700Sstevel@tonic-gate 		if (oprom_copynode(child, flag, &nodebuf, &nodesize)) {
12710Sstevel@tonic-gate 			nvlist_free(nvl);
12720Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to copy nodeid 0x%x", child);
12730Sstevel@tonic-gate 			return (-1);
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "node",
12760Sstevel@tonic-gate 		    (uchar_t *)nodebuf, nodesize);
12770Sstevel@tonic-gate 		kmem_free(nodebuf, nodesize);
12780Sstevel@tonic-gate 		child = prom_nextnode(child);
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	(void) nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
12820Sstevel@tonic-gate 	nvlist_free(nvl);
12830Sstevel@tonic-gate 	return (0);
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate  * Copy a node into a packed nvlist
12880Sstevel@tonic-gate  */
12890Sstevel@tonic-gate static int
1290789Sahrens oprom_copynode(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	int error = 0;
12930Sstevel@tonic-gate 	nvlist_t *nvl;
12940Sstevel@tonic-gate 	char *childlist = NULL;
12950Sstevel@tonic-gate 	size_t childsize = 0;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
12980Sstevel@tonic-gate 	ASSERT(nvl != NULL);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	/* @nodeid -- @ is not a legal char in a 1275 property name */
13010Sstevel@tonic-gate 	(void) nvlist_add_int32(nvl, "@nodeid", (int32_t)nodeid);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/* properties */
13040Sstevel@tonic-gate 	if (error = oprom_copyprop(nodeid, flag, nvl))
13050Sstevel@tonic-gate 		goto fail;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	/* children */
13080Sstevel@tonic-gate 	error = oprom_copychild(nodeid, flag, &childlist, &childsize);
13090Sstevel@tonic-gate 	if (error != 0)
13100Sstevel@tonic-gate 		goto fail;
13110Sstevel@tonic-gate 	if (childlist != NULL) {
13120Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "@child",
13130Sstevel@tonic-gate 		    (uchar_t *)childlist, (uint_t)childsize);
13140Sstevel@tonic-gate 		kmem_free(childlist, childsize);
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/* pack into contiguous buffer */
13180Sstevel@tonic-gate 	error = nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate fail:
13210Sstevel@tonic-gate 	nvlist_free(nvl);
13220Sstevel@tonic-gate 	return (error);
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate  * The driver is stateful across OPROMSNAPSHOT and OPROMCOPYOUT.
13270Sstevel@tonic-gate  * This function encapsulates the state machine:
13280Sstevel@tonic-gate  *
13290Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
13300Sstevel@tonic-gate  *	|		SNAPSHOT		COPYOUT	 |
13310Sstevel@tonic-gate  *	--------------------------------------------------
13320Sstevel@tonic-gate  *
13330Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
13340Sstevel@tonic-gate  */
13350Sstevel@tonic-gate static int
13360Sstevel@tonic-gate oprom_setstate(struct oprom_state *st, int16_t new_state)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	int ret = 0;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
13410Sstevel@tonic-gate 	switch (new_state) {
13420Sstevel@tonic-gate 	case IOC_IDLE:
13430Sstevel@tonic-gate 	case IOC_DONE:
13440Sstevel@tonic-gate 		break;
13450Sstevel@tonic-gate 	case IOC_SNAP:
13460Sstevel@tonic-gate 		if (st->ioc_state != IOC_IDLE)
13470Sstevel@tonic-gate 			ret = -1;
13480Sstevel@tonic-gate 		break;
13490Sstevel@tonic-gate 	case IOC_COPY:
13500Sstevel@tonic-gate 		if (st->ioc_state != IOC_DONE)
13510Sstevel@tonic-gate 			ret = -1;
13520Sstevel@tonic-gate 		break;
13530Sstevel@tonic-gate 	default:
13540Sstevel@tonic-gate 		ret = -1;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if (ret == 0)
13580Sstevel@tonic-gate 		st->ioc_state = new_state;
13590Sstevel@tonic-gate 	else
13600Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
13610Sstevel@tonic-gate 		    st->ioc_state, new_state);
13620Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
13630Sstevel@tonic-gate 	return (ret);
13640Sstevel@tonic-gate }
1365