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