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