xref: /onnv-gate/usr/src/uts/common/io/openprom.c (revision 11906:6743277598ac)
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 /*
22*11906SGangadhar.M@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
257656SSherry.Moore@Sun.COM 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Ported from 4.1.1_PSRA: "@(#)openprom.c 1.19 91/02/19 SMI";
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * Porting notes:
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * OPROMU2P unsupported after SunOS 4.x.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * Only one of these devices per system is allowed.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Openprom eeprom options/devinfo driver.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/openpromio.h>
460Sstevel@tonic-gate #include <sys/conf.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/autoconf.h>
510Sstevel@tonic-gate #include <sys/ddi.h>
520Sstevel@tonic-gate #include <sys/sunddi.h>
530Sstevel@tonic-gate #include <sys/promif.h>
540Sstevel@tonic-gate #include <sys/sysmacros.h>	/* offsetof */
550Sstevel@tonic-gate #include <sys/nvpair.h>
560Sstevel@tonic-gate #include <sys/wanboot_impl.h>
570Sstevel@tonic-gate #include <sys/zone.h>
587335SLipeng.Sang@Sun.COM #include <sys/consplat.h>
59*11906SGangadhar.M@Sun.COM #include <sys/bootconf.h>
60*11906SGangadhar.M@Sun.COM #include <sys/systm.h>
61*11906SGangadhar.M@Sun.COM #include <sys/bootprops.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	MAX_OPENS	32	/* Up to this many simultaneous opens */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
660Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
670Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
680Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * XXX	Make this dynamic.. or (better still) make the interface stateless
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate static struct oprom_state {
74789Sahrens 	pnode_t	current_id;	/* node we're fetching props from */
750Sstevel@tonic-gate 	int16_t	already_open;	/* if true, this instance is 'active' */
760Sstevel@tonic-gate 	int16_t	ioc_state;	/* snapshot ioctl state */
770Sstevel@tonic-gate 	char	*snapshot;	/* snapshot of all prom nodes */
780Sstevel@tonic-gate 	size_t	size;		/* size of snapshot */
790Sstevel@tonic-gate 	prom_generation_cookie_t tree_gen;
800Sstevel@tonic-gate } oprom_state[MAX_OPENS];
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static kmutex_t oprom_lock;	/* serialize instance assignment */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int opromopen(dev_t *, int, int, cred_t *);
850Sstevel@tonic-gate static int opromioctl(dev_t, int, intptr_t, int, cred_t *, int *);
860Sstevel@tonic-gate static int opromclose(dev_t, int, int, cred_t *);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static int opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
890Sstevel@tonic-gate 		void **result);
900Sstevel@tonic-gate static int opattach(dev_info_t *, ddi_attach_cmd_t cmd);
910Sstevel@tonic-gate static int opdetach(dev_info_t *, ddi_detach_cmd_t cmd);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* help functions */
94789Sahrens static int oprom_checknodeid(pnode_t, pnode_t);
950Sstevel@tonic-gate static int oprom_copyinstr(intptr_t, char *, size_t, size_t);
96789Sahrens static int oprom_copynode(pnode_t, uint_t, char **, size_t *);
970Sstevel@tonic-gate static int oprom_snapshot(struct oprom_state *, intptr_t);
980Sstevel@tonic-gate static int oprom_copyout(struct oprom_state *, intptr_t);
990Sstevel@tonic-gate static int oprom_setstate(struct oprom_state *, int16_t);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct cb_ops openeepr_cb_ops = {
1020Sstevel@tonic-gate 	opromopen,		/* open */
1030Sstevel@tonic-gate 	opromclose,		/* close */
1040Sstevel@tonic-gate 	nodev,			/* strategy */
1050Sstevel@tonic-gate 	nodev,			/* print */
1060Sstevel@tonic-gate 	nodev,			/* dump */
1070Sstevel@tonic-gate 	nodev,			/* read */
1080Sstevel@tonic-gate 	nodev,			/* write */
1090Sstevel@tonic-gate 	opromioctl,		/* ioctl */
1100Sstevel@tonic-gate 	nodev,			/* devmap */
1110Sstevel@tonic-gate 	nodev,			/* mmap */
1120Sstevel@tonic-gate 	nodev,			/* segmap */
1130Sstevel@tonic-gate 	nochpoll,		/* poll */
1140Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1150Sstevel@tonic-gate 	NULL,			/* streamtab  */
1160Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static struct dev_ops openeepr_ops = {
1200Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1210Sstevel@tonic-gate 	0,			/* refcnt  */
1220Sstevel@tonic-gate 	opinfo,			/* info */
1230Sstevel@tonic-gate 	nulldev,		/* identify */
1240Sstevel@tonic-gate 	nulldev,		/* probe */
1250Sstevel@tonic-gate 	opattach,		/* attach */
1260Sstevel@tonic-gate 	opdetach,		/* detach */
1270Sstevel@tonic-gate 	nodev,			/* reset */
1280Sstevel@tonic-gate 	&openeepr_cb_ops,	/* driver operations */
1297656SSherry.Moore@Sun.COM 	NULL,			/* bus operations */
1307656SSherry.Moore@Sun.COM 	NULL,			/* power */
1317656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
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,
1397335SLipeng.Sang@Sun.COM 	"OPENPROM/NVRAM Driver",
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
_init(void)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
_info(struct modinfo * modinfop)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
_fini(void)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;
185789Sahrens static pnode_t options_nodeid;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*ARGSUSED*/
1880Sstevel@tonic-gate static int
opinfo(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)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
opattach(dev_info_t * dip,ddi_attach_cmd_t cmd)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
opdetach(dev_info_t * dip,ddi_detach_cmd_t cmd)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
opromopen(dev_t * devp,int flag,int otyp,cred_t * credp)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 			 */
272789Sahrens 			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
opromclose(dev_t dev,int flag,int otype,cred_t * cred_p)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 
321*11906SGangadhar.M@Sun.COM #ifdef __sparc
322*11906SGangadhar.M@Sun.COM static int
get_bootpath_prop(char * bootpath)323*11906SGangadhar.M@Sun.COM get_bootpath_prop(char *bootpath)
324*11906SGangadhar.M@Sun.COM {
325*11906SGangadhar.M@Sun.COM 	if (root_is_ramdisk) {
326*11906SGangadhar.M@Sun.COM 		if (BOP_GETPROP(bootops, "bootarchive", bootpath) == -1)
327*11906SGangadhar.M@Sun.COM 			return (-1);
328*11906SGangadhar.M@Sun.COM 		(void) strlcat(bootpath, ":a", BO_MAXOBJNAME);
329*11906SGangadhar.M@Sun.COM 	} else {
330*11906SGangadhar.M@Sun.COM 		if ((BOP_GETPROP(bootops, "bootpath", bootpath) == -1) ||
331*11906SGangadhar.M@Sun.COM 		    strlen(bootpath) == 0) {
332*11906SGangadhar.M@Sun.COM 			if (BOP_GETPROP(bootops,
333*11906SGangadhar.M@Sun.COM 			    "boot-path", bootpath) == -1)
334*11906SGangadhar.M@Sun.COM 				return (-1);
335*11906SGangadhar.M@Sun.COM 		}
336*11906SGangadhar.M@Sun.COM 		if (memcmp(bootpath, BP_ISCSI_DISK,
337*11906SGangadhar.M@Sun.COM 		    strlen(BP_ISCSI_DISK)) == 0) {
338*11906SGangadhar.M@Sun.COM 			get_iscsi_bootpath_vhci(bootpath);
339*11906SGangadhar.M@Sun.COM 		}
340*11906SGangadhar.M@Sun.COM 	}
341*11906SGangadhar.M@Sun.COM 	return (0);
342*11906SGangadhar.M@Sun.COM }
343*11906SGangadhar.M@Sun.COM #endif
344*11906SGangadhar.M@Sun.COM 
3450Sstevel@tonic-gate struct opromioctl_args {
3460Sstevel@tonic-gate 	struct oprom_state *st;
3470Sstevel@tonic-gate 	int cmd;
3480Sstevel@tonic-gate 	intptr_t arg;
3490Sstevel@tonic-gate 	int mode;
3500Sstevel@tonic-gate };
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*ARGSUSED*/
3530Sstevel@tonic-gate static int
opromioctl_cb(void * avp,int has_changed)3540Sstevel@tonic-gate opromioctl_cb(void *avp, int has_changed)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	struct opromioctl_args *argp = avp;
3570Sstevel@tonic-gate 	int cmd;
3580Sstevel@tonic-gate 	intptr_t arg;
3590Sstevel@tonic-gate 	int mode;
3600Sstevel@tonic-gate 	struct oprom_state *st;
3610Sstevel@tonic-gate 	struct openpromio *opp;
3620Sstevel@tonic-gate 	int valsize;
3630Sstevel@tonic-gate 	char *valbuf;
3640Sstevel@tonic-gate 	int error = 0;
3650Sstevel@tonic-gate 	uint_t userbufsize;
366789Sahrens 	pnode_t node_id;
3670Sstevel@tonic-gate 	char propname[OBP_MAXPROPNAME];
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	st = argp->st;
3700Sstevel@tonic-gate 	cmd = argp->cmd;
3710Sstevel@tonic-gate 	arg = argp->arg;
3720Sstevel@tonic-gate 	mode = argp->mode;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (has_changed) {
3750Sstevel@tonic-gate 		/*
3760Sstevel@tonic-gate 		 * The prom tree has changed since we last used current_id,
3770Sstevel@tonic-gate 		 * so we need to check it.
3780Sstevel@tonic-gate 		 */
3790Sstevel@tonic-gate 		if ((st->current_id != OBP_NONODE) &&
3800Sstevel@tonic-gate 		    (st->current_id != OBP_BADNODE)) {
3810Sstevel@tonic-gate 			if (oprom_checknodeid(st->current_id, OBP_NONODE) == 0)
3820Sstevel@tonic-gate 				st->current_id = OBP_BADNODE;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * Check permissions
3880Sstevel@tonic-gate 	 * and weed out unsupported commands on x86 platform
3890Sstevel@tonic-gate 	 */
3900Sstevel@tonic-gate 	switch (cmd) {
3910Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
3920Sstevel@tonic-gate 	case OPROMLISTKEYSLEN:
3930Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
3940Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
3950Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
3960Sstevel@tonic-gate 		opp->oprom_size = valsize;
3970Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
3987009Scth 			error = EFAULT;
3990Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
4000Sstevel@tonic-gate 		break;
4010Sstevel@tonic-gate 	case OPROMLISTKEYS:
4020Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
4030Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
4047009Scth 			return (EFAULT);
4050Sstevel@tonic-gate 		if (valsize > userbufsize)
4067009Scth 			return (EINVAL);
4070Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
4080Sstevel@tonic-gate 		if (prom_asr_list_keys((caddr_t)valbuf) == -1) {
4090Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
4100Sstevel@tonic-gate 			return (EFAULT);
4110Sstevel@tonic-gate 		}
4120Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4130Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
4140Sstevel@tonic-gate 		opp->oprom_size = valsize;
4150Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
4160Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
4177009Scth 			error = EFAULT;
4180Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
4190Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
4200Sstevel@tonic-gate 		break;
4210Sstevel@tonic-gate 	case OPROMEXPORT:
4220Sstevel@tonic-gate 		valsize = prom_asr_export_len();
4230Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
4247009Scth 			return (EFAULT);
4250Sstevel@tonic-gate 		if (valsize > userbufsize)
4267009Scth 			return (EINVAL);
4270Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
4280Sstevel@tonic-gate 		if (prom_asr_export((caddr_t)valbuf) == -1) {
4290Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
4300Sstevel@tonic-gate 			return (EFAULT);
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4330Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
4340Sstevel@tonic-gate 		opp->oprom_size = valsize;
4350Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
4360Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
4377009Scth 			error = EFAULT;
4380Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
4390Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
4400Sstevel@tonic-gate 		break;
4410Sstevel@tonic-gate 	case OPROMEXPORTLEN:
4420Sstevel@tonic-gate 		valsize = prom_asr_export_len();
4430Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4440Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
4450Sstevel@tonic-gate 		opp->oprom_size = valsize;
4460Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
4477009Scth 			error = EFAULT;
4480Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
4490Sstevel@tonic-gate 		break;
4500Sstevel@tonic-gate #endif
4510Sstevel@tonic-gate 	case OPROMGETOPT:
4520Sstevel@tonic-gate 	case OPROMNXTOPT:
4530Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4540Sstevel@tonic-gate 			return (EPERM);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 		node_id = options_nodeid;
4570Sstevel@tonic-gate 		break;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	case OPROMSETOPT:
4600Sstevel@tonic-gate 	case OPROMSETOPT2:
4610Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4620Sstevel@tonic-gate 		if (mode & FWRITE) {
4630Sstevel@tonic-gate 			node_id = options_nodeid;
4640Sstevel@tonic-gate 			break;
4650Sstevel@tonic-gate 		}
4660Sstevel@tonic-gate #endif /* !__i386 && !__amd64 */
4670Sstevel@tonic-gate 		return (EPERM);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	case OPROMNEXT:
4700Sstevel@tonic-gate 	case OPROMCHILD:
4710Sstevel@tonic-gate 	case OPROMGETPROP:
4720Sstevel@tonic-gate 	case OPROMGETPROPLEN:
4730Sstevel@tonic-gate 	case OPROMNXTPROP:
4740Sstevel@tonic-gate 	case OPROMSETNODEID:
4750Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4760Sstevel@tonic-gate 			return (EPERM);
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 		node_id = st->current_id;
4790Sstevel@tonic-gate 		break;
4800Sstevel@tonic-gate 	case OPROMCOPYOUT:
4810Sstevel@tonic-gate 		if (st->snapshot == NULL)
4820Sstevel@tonic-gate 			return (EINVAL);
4830Sstevel@tonic-gate 		/*FALLTHROUGH*/
4840Sstevel@tonic-gate 	case OPROMSNAPSHOT:
4850Sstevel@tonic-gate 	case OPROMGETCONS:
4860Sstevel@tonic-gate 	case OPROMGETBOOTARGS:
487*11906SGangadhar.M@Sun.COM 	case OPROMGETBOOTPATH:
4880Sstevel@tonic-gate 	case OPROMGETVERSION:
4890Sstevel@tonic-gate 	case OPROMPATH2DRV:
4900Sstevel@tonic-gate 	case OPROMPROM2DEVNAME:
4910Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4920Sstevel@tonic-gate 	case OPROMGETFBNAME:
4930Sstevel@tonic-gate 	case OPROMDEV2PROMNAME:
4940Sstevel@tonic-gate 	case OPROMREADY64:
4950Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
4960Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4970Sstevel@tonic-gate 			return (EPERM);
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 		break;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
5020Sstevel@tonic-gate 	case WANBOOT_SETKEY:
5030Sstevel@tonic-gate 		if (!(mode & FWRITE))
5040Sstevel@tonic-gate 			return (EPERM);
5050Sstevel@tonic-gate 		break;
5060Sstevel@tonic-gate #endif	/* !__i386 && !defined(__amd64) */
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	default:
5090Sstevel@tonic-gate 		return (EINVAL);
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Deal with SNAPSHOT and COPYOUT ioctls first
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	switch (cmd) {
5160Sstevel@tonic-gate 	case OPROMCOPYOUT:
5170Sstevel@tonic-gate 		return (oprom_copyout(st, arg));
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	case OPROMSNAPSHOT:
5200Sstevel@tonic-gate 		return (oprom_snapshot(st, arg));
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * Copy in user argument length and allocation memory
5250Sstevel@tonic-gate 	 *
5260Sstevel@tonic-gate 	 * NB do not copyin the entire buffer we may not need
5270Sstevel@tonic-gate 	 *	to. userbufsize can be as big as 32 K.
5280Sstevel@tonic-gate 	 */
5290Sstevel@tonic-gate 	if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
5300Sstevel@tonic-gate 		return (EFAULT);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (userbufsize == 0 || userbufsize > OPROMMAXPARAM)
5330Sstevel@tonic-gate 		return (EINVAL);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	opp = (struct openpromio *)kmem_zalloc(
5360Sstevel@tonic-gate 	    userbufsize + sizeof (uint_t) + 1, KM_SLEEP);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	/*
5390Sstevel@tonic-gate 	 * Execute command
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	switch (cmd) {
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	case OPROMGETOPT:
5440Sstevel@tonic-gate 	case OPROMGETPROP:
5450Sstevel@tonic-gate 	case OPROMGETPROPLEN:
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
5480Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
5490Sstevel@tonic-gate 			error = EINVAL;
5500Sstevel@tonic-gate 			break;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 		/*
5540Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
5570Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
5580Sstevel@tonic-gate 			break;
5590Sstevel@tonic-gate 		}
5600Sstevel@tonic-gate 		(void) strcpy(propname, opp->oprom_array);
5610Sstevel@tonic-gate 		valsize = prom_getproplen(node_id, propname);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but not an option.
5650Sstevel@tonic-gate 		 */
5660Sstevel@tonic-gate 		if ((cmd == OPROMGETOPT) && (strcmp("name", propname) == 0))
5670Sstevel@tonic-gate 			valsize = -1;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (cmd == OPROMGETPROPLEN)  {
5700Sstevel@tonic-gate 			int proplen = valsize;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 			if (userbufsize < sizeof (int)) {
5730Sstevel@tonic-gate 				error = EINVAL;
5740Sstevel@tonic-gate 				break;
5750Sstevel@tonic-gate 			}
5760Sstevel@tonic-gate 			opp->oprom_size = valsize = sizeof (int);
5770Sstevel@tonic-gate 			bcopy(&proplen, opp->oprom_array, valsize);
5780Sstevel@tonic-gate 		} else if (valsize > 0 && valsize <= userbufsize) {
5790Sstevel@tonic-gate 			bzero(opp->oprom_array, valsize + 1);
5800Sstevel@tonic-gate 			(void) prom_getprop(node_id, propname,
5810Sstevel@tonic-gate 			    opp->oprom_array);
5820Sstevel@tonic-gate 			opp->oprom_size = valsize;
5830Sstevel@tonic-gate 			if (valsize < userbufsize)
5840Sstevel@tonic-gate 				++valsize;	/* Forces NULL termination */
5850Sstevel@tonic-gate 						/* If space permits */
5860Sstevel@tonic-gate 		} else {
5870Sstevel@tonic-gate 			/*
5880Sstevel@tonic-gate 			 * XXX: There is no error code if the buf is too small.
5890Sstevel@tonic-gate 			 * which is consistent with the current behavior.
5900Sstevel@tonic-gate 			 *
5910Sstevel@tonic-gate 			 * NB: This clause also handles the non-error
5920Sstevel@tonic-gate 			 * zero length (boolean) property value case.
5930Sstevel@tonic-gate 			 */
5940Sstevel@tonic-gate 			opp->oprom_size = 0;
5950Sstevel@tonic-gate 			(void) strcpy(opp->oprom_array, "");
5960Sstevel@tonic-gate 			valsize = 1;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
5990Sstevel@tonic-gate 			error = EFAULT;
6000Sstevel@tonic-gate 		break;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	case OPROMNXTOPT:
6030Sstevel@tonic-gate 	case OPROMNXTPROP:
6040Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
6050Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
6060Sstevel@tonic-gate 			error = EINVAL;
6070Sstevel@tonic-gate 			break;
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
6120Sstevel@tonic-gate 		 */
6130Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
6140Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
6150Sstevel@tonic-gate 			break;
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 		valbuf = (char *)prom_nextprop(node_id, opp->oprom_array,
6180Sstevel@tonic-gate 		    propname);
6190Sstevel@tonic-gate 		valsize = strlen(valbuf);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 		/*
6220Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but it's not an option.
6230Sstevel@tonic-gate 		 */
6240Sstevel@tonic-gate 		if ((cmd == OPROMNXTOPT) && valsize &&
6250Sstevel@tonic-gate 		    (strcmp(valbuf, "name") == 0)) {
6260Sstevel@tonic-gate 			valbuf = (char *)prom_nextprop(node_id, "name",
6270Sstevel@tonic-gate 			    propname);
6280Sstevel@tonic-gate 			valsize = strlen(valbuf);
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		if (valsize == 0) {
6320Sstevel@tonic-gate 			opp->oprom_size = 0;
6330Sstevel@tonic-gate 		} else if (++valsize <= userbufsize) {
6340Sstevel@tonic-gate 			opp->oprom_size = valsize;
6350Sstevel@tonic-gate 			bzero((caddr_t)opp->oprom_array, (size_t)valsize);
6360Sstevel@tonic-gate 			bcopy((caddr_t)valbuf, (caddr_t)opp->oprom_array,
6370Sstevel@tonic-gate 			    (size_t)valsize);
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
6410Sstevel@tonic-gate 			error = EFAULT;
6420Sstevel@tonic-gate 		break;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	case OPROMNEXT:
6450Sstevel@tonic-gate 	case OPROMCHILD:
6460Sstevel@tonic-gate 	case OPROMSETNODEID:
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if (prom_is_openprom() == 0 ||
649789Sahrens 		    userbufsize < sizeof (pnode_t)) {
6500Sstevel@tonic-gate 			error = EINVAL;
6510Sstevel@tonic-gate 			break;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		/*
655789Sahrens 		 * The argument is a phandle. (aka pnode_t)
6560Sstevel@tonic-gate 		 */
6570Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
658789Sahrens 		    opp->oprom_array, sizeof (pnode_t)) != 0) {
6590Sstevel@tonic-gate 			error = EFAULT;
6600Sstevel@tonic-gate 			break;
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		/*
664789Sahrens 		 * If pnode_t from userland is garbage, we
6650Sstevel@tonic-gate 		 * could confuse the PROM.
6660Sstevel@tonic-gate 		 */
667789Sahrens 		node_id = *(pnode_t *)opp->oprom_array;
6680Sstevel@tonic-gate 		if (oprom_checknodeid(node_id, st->current_id) == 0) {
6690Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!nodeid 0x%x not found",
6700Sstevel@tonic-gate 			    (int)node_id);
6710Sstevel@tonic-gate 			error = EINVAL;
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		if (cmd == OPROMNEXT)
6760Sstevel@tonic-gate 			st->current_id = prom_nextnode(node_id);
6770Sstevel@tonic-gate 		else if (cmd == OPROMCHILD)
6780Sstevel@tonic-gate 			st->current_id = prom_childnode(node_id);
6790Sstevel@tonic-gate 		else {
6800Sstevel@tonic-gate 			/* OPROMSETNODEID */
6810Sstevel@tonic-gate 			st->current_id = node_id;
6820Sstevel@tonic-gate 			break;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 
685789Sahrens 		opp->oprom_size = sizeof (pnode_t);
686789Sahrens 		*(pnode_t *)opp->oprom_array = st->current_id;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
689789Sahrens 		    sizeof (pnode_t) + sizeof (uint_t)) != 0)
6900Sstevel@tonic-gate 			error = EFAULT;
6910Sstevel@tonic-gate 		break;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	case OPROMGETCONS:
6940Sstevel@tonic-gate 		/*
6950Sstevel@tonic-gate 		 * Is openboot supported on this machine?
6960Sstevel@tonic-gate 		 * This ioctl used to return the console device,
6970Sstevel@tonic-gate 		 * information; this is now done via modctl()
6980Sstevel@tonic-gate 		 * in libdevinfo.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		opp->oprom_size = sizeof (char);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		opp->oprom_array[0] |= prom_is_openprom() ?
7030Sstevel@tonic-gate 		    OPROMCONS_OPENPROM : 0;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * The rest of the info is needed by Install to
7070Sstevel@tonic-gate 		 * decide if graphics should be started.
7080Sstevel@tonic-gate 		 */
7090Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
7100Sstevel@tonic-gate 		    plat_stdin_is_keyboard()) {
7110Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDIN_IS_KBD;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
7150Sstevel@tonic-gate 		    plat_stdout_is_framebuffer()) {
7160Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDOUT_IS_FB;
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
7200Sstevel@tonic-gate 		    sizeof (char) + sizeof (uint_t)) != 0)
7210Sstevel@tonic-gate 			error = EFAULT;
7220Sstevel@tonic-gate 		break;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	case OPROMGETBOOTARGS: {
7250Sstevel@tonic-gate 		extern char kern_bootargs[];
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		valsize = strlen(kern_bootargs) + 1;
7280Sstevel@tonic-gate 		if (valsize > userbufsize) {
7290Sstevel@tonic-gate 			error = EINVAL;
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, kern_bootargs);
7330Sstevel@tonic-gate 		opp->oprom_size = valsize - 1;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
7360Sstevel@tonic-gate 			error = EFAULT;
7377009Scth 		break;
7387009Scth 	}
7390Sstevel@tonic-gate 
740*11906SGangadhar.M@Sun.COM 	case OPROMGETBOOTPATH: {
741*11906SGangadhar.M@Sun.COM #if defined(__sparc) && defined(_OBP)
742*11906SGangadhar.M@Sun.COM 
743*11906SGangadhar.M@Sun.COM 		char bpath[OBP_MAXPATHLEN];
744*11906SGangadhar.M@Sun.COM 		if (get_bootpath_prop(bpath) != 0) {
745*11906SGangadhar.M@Sun.COM 			error = EINVAL;
746*11906SGangadhar.M@Sun.COM 			break;
747*11906SGangadhar.M@Sun.COM 		}
748*11906SGangadhar.M@Sun.COM 		valsize = strlen(bpath) + 1;
749*11906SGangadhar.M@Sun.COM 		if (valsize > userbufsize) {
750*11906SGangadhar.M@Sun.COM 			error = EINVAL;
751*11906SGangadhar.M@Sun.COM 			break;
752*11906SGangadhar.M@Sun.COM 		}
753*11906SGangadhar.M@Sun.COM 		(void) strcpy(opp->oprom_array, bpath);
754*11906SGangadhar.M@Sun.COM 
755*11906SGangadhar.M@Sun.COM #elif defined(__i386) || defined(__amd64)
756*11906SGangadhar.M@Sun.COM 
757*11906SGangadhar.M@Sun.COM 		extern char saved_cmdline[];
758*11906SGangadhar.M@Sun.COM 		valsize = strlen(saved_cmdline) + 1;
759*11906SGangadhar.M@Sun.COM 		if (valsize > userbufsize) {
760*11906SGangadhar.M@Sun.COM 			error = EINVAL;
761*11906SGangadhar.M@Sun.COM 			break;
762*11906SGangadhar.M@Sun.COM 		}
763*11906SGangadhar.M@Sun.COM 		(void) strcpy(opp->oprom_array, saved_cmdline);
764*11906SGangadhar.M@Sun.COM #endif
765*11906SGangadhar.M@Sun.COM 		opp->oprom_size = valsize - 1;
766*11906SGangadhar.M@Sun.COM 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
767*11906SGangadhar.M@Sun.COM 			error = EFAULT;
768*11906SGangadhar.M@Sun.COM 		break;
769*11906SGangadhar.M@Sun.COM 	}
770*11906SGangadhar.M@Sun.COM 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * convert a prom device path to an equivalent devfs path
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	case OPROMPROM2DEVNAME: {
7750Sstevel@tonic-gate 		char *dev_name;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 		/*
7780Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
7790Sstevel@tonic-gate 		 */
7800Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
7810Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
7820Sstevel@tonic-gate 			break;
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		dev_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		error = i_promname_to_devname(opp->oprom_array, dev_name);
7880Sstevel@tonic-gate 		if (error != 0) {
7890Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7900Sstevel@tonic-gate 			break;
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 		valsize = opp->oprom_size = strlen(dev_name);
7930Sstevel@tonic-gate 		if (++valsize > userbufsize) {
7940Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7950Sstevel@tonic-gate 			error = EINVAL;
7960Sstevel@tonic-gate 			break;
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, dev_name);
7990Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
8000Sstevel@tonic-gate 			error = EFAULT;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		kmem_free(dev_name, MAXPATHLEN);
8037009Scth 		break;
8047009Scth 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * Convert a prom device path name to a driver name
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	case OPROMPATH2DRV: {
8100Sstevel@tonic-gate 		char *drv_name;
8110Sstevel@tonic-gate 		major_t maj;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
8150Sstevel@tonic-gate 		 */
8160Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
8170Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
8180Sstevel@tonic-gate 			break;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		/*
8220Sstevel@tonic-gate 		 * convert path to a driver binding name
8230Sstevel@tonic-gate 		 */
8240Sstevel@tonic-gate 		maj = path_to_major((char *)opp->oprom_array);
8257009Scth 		if (maj == DDI_MAJOR_T_NONE) {
8260Sstevel@tonic-gate 			error = EINVAL;
8270Sstevel@tonic-gate 			break;
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		/*
8310Sstevel@tonic-gate 		 * resolve any aliases
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		if ((drv_name = ddi_major_to_name(maj)) == NULL) {
8340Sstevel@tonic-gate 			error = EINVAL;
8350Sstevel@tonic-gate 			break;
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, drv_name);
8390Sstevel@tonic-gate 		opp->oprom_size = strlen(drv_name);
8400Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
8410Sstevel@tonic-gate 		    sizeof (uint_t) + opp->oprom_size + 1) != 0)
8420Sstevel@tonic-gate 			error = EFAULT;
8437009Scth 		break;
8447009Scth 	}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	case OPROMGETVERSION:
8470Sstevel@tonic-gate 		/*
8480Sstevel@tonic-gate 		 * Get a string representing the running version of the
8490Sstevel@tonic-gate 		 * prom. How to create such a string is platform dependent,
8500Sstevel@tonic-gate 		 * so we just defer to a promif function. If no such
8510Sstevel@tonic-gate 		 * association exists, the promif implementation
8520Sstevel@tonic-gate 		 * may copy the string "unknown" into the given buffer,
8530Sstevel@tonic-gate 		 * and return its length (incl. NULL terminator).
8540Sstevel@tonic-gate 		 *
8550Sstevel@tonic-gate 		 * We expect prom_version_name to return the actual
8560Sstevel@tonic-gate 		 * length of the string, but copy at most userbufsize
8570Sstevel@tonic-gate 		 * bytes into the given buffer, including NULL termination.
8580Sstevel@tonic-gate 		 */
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		valsize = prom_version_name(opp->oprom_array, userbufsize);
8610Sstevel@tonic-gate 		if (valsize < 0) {
8620Sstevel@tonic-gate 			error = EINVAL;
8630Sstevel@tonic-gate 			break;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		/*
8670Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
8680Sstevel@tonic-gate 		 */
8690Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
8700Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
8710Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
8720Sstevel@tonic-gate 			error = EFAULT;
8730Sstevel@tonic-gate 		break;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
8760Sstevel@tonic-gate 	case OPROMGETFBNAME:
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * Return stdoutpath, if it's a frame buffer.
8790Sstevel@tonic-gate 		 * Yes, we are comparing a possibly longer string against
8800Sstevel@tonic-gate 		 * the size we're really going to copy, but so what?
8810Sstevel@tonic-gate 		 */
8820Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
8830Sstevel@tonic-gate 		    (prom_stdout_is_framebuffer() != 0) &&
8840Sstevel@tonic-gate 		    (userbufsize > strlen(prom_stdoutpath()))) {
8850Sstevel@tonic-gate 			prom_strip_options(prom_stdoutpath(),
8860Sstevel@tonic-gate 			    opp->oprom_array);	/* strip options and copy */
8870Sstevel@tonic-gate 			valsize = opp->oprom_size = strlen(opp->oprom_array);
8880Sstevel@tonic-gate 			if (copyout(opp, (void *)arg,
8890Sstevel@tonic-gate 			    valsize + 1 + sizeof (uint_t)) != 0)
8900Sstevel@tonic-gate 				error = EFAULT;
8910Sstevel@tonic-gate 		} else
8920Sstevel@tonic-gate 			error = EINVAL;
8930Sstevel@tonic-gate 		break;
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Convert a logical or physical device path to prom device path
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	case OPROMDEV2PROMNAME: {
8990Sstevel@tonic-gate 		char *prom_name;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		/*
9020Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
9030Sstevel@tonic-gate 		 */
9040Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
9050Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
9060Sstevel@tonic-gate 			break;
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 		prom_name = kmem_alloc(userbufsize, KM_SLEEP);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		/*
9120Sstevel@tonic-gate 		 * convert the devfs path to an equivalent prom path
9130Sstevel@tonic-gate 		 */
9140Sstevel@tonic-gate 		error = i_devname_to_promname(opp->oprom_array, prom_name,
9150Sstevel@tonic-gate 		    userbufsize);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		if (error != 0) {
9180Sstevel@tonic-gate 			kmem_free(prom_name, userbufsize);
9190Sstevel@tonic-gate 			break;
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		for (valsize = 0; valsize < userbufsize; valsize++) {
9230Sstevel@tonic-gate 			opp->oprom_array[valsize] = prom_name[valsize];
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 			if ((valsize > 0) && (prom_name[valsize] == '\0') &&
9260Sstevel@tonic-gate 			    (prom_name[valsize-1] == '\0')) {
9270Sstevel@tonic-gate 				break;
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		opp->oprom_size = valsize;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		kmem_free(prom_name, userbufsize);
9330Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
9340Sstevel@tonic-gate 			error = EFAULT;
9350Sstevel@tonic-gate 
9367009Scth 		break;
9377009Scth 	}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	case OPROMSETOPT:
9400Sstevel@tonic-gate 	case OPROMSETOPT2: {
9410Sstevel@tonic-gate 		int namebuflen;
9420Sstevel@tonic-gate 		int valbuflen;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
9450Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
9460Sstevel@tonic-gate 			error = EINVAL;
9470Sstevel@tonic-gate 			break;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		/*
9510Sstevel@tonic-gate 		 * The arguments are a property name and a value.
9520Sstevel@tonic-gate 		 * Copy in the entire user buffer.
9530Sstevel@tonic-gate 		 */
9540Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
9550Sstevel@tonic-gate 		    opp->oprom_array, userbufsize) != 0) {
9560Sstevel@tonic-gate 			error = EFAULT;
9570Sstevel@tonic-gate 			break;
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 		/*
9610Sstevel@tonic-gate 		 * The property name is the first string, value second
9620Sstevel@tonic-gate 		 */
9630Sstevel@tonic-gate 		namebuflen = strlen(opp->oprom_array);
9640Sstevel@tonic-gate 		valbuf = opp->oprom_array + namebuflen + 1;
9650Sstevel@tonic-gate 		valbuflen = strlen(valbuf);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		if (cmd == OPROMSETOPT) {
9680Sstevel@tonic-gate 			valsize = valbuflen + 1;  /* +1 for the '\0' */
9690Sstevel@tonic-gate 		} else {
9700Sstevel@tonic-gate 			if ((namebuflen + 1 + valbuflen + 1) > userbufsize) {
9710Sstevel@tonic-gate 				error = EINVAL;
9720Sstevel@tonic-gate 				break;
9730Sstevel@tonic-gate 			}
9740Sstevel@tonic-gate 			valsize = (opp->oprom_array + userbufsize) - valbuf;
9750Sstevel@tonic-gate 		}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		/*
9780Sstevel@tonic-gate 		 * 4010173: 'name' is not an option, but it is a property.
9790Sstevel@tonic-gate 		 */
9800Sstevel@tonic-gate 		if (strcmp(opp->oprom_array, "name") == 0)
9810Sstevel@tonic-gate 			error = EINVAL;
9820Sstevel@tonic-gate 		else if (prom_setprop(node_id, opp->oprom_array,
9830Sstevel@tonic-gate 		    valbuf, valsize) < 0)
9840Sstevel@tonic-gate 			error = EINVAL;
9850Sstevel@tonic-gate 
9867009Scth 		break;
9877009Scth 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	case OPROMREADY64: {
9900Sstevel@tonic-gate 		struct openprom_opr64 *opr =
9910Sstevel@tonic-gate 		    (struct openprom_opr64 *)opp->oprom_array;
9920Sstevel@tonic-gate 		int i;
993789Sahrens 		pnode_t id;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		if (userbufsize < sizeof (*opr)) {
9960Sstevel@tonic-gate 			error = EINVAL;
9970Sstevel@tonic-gate 			break;
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 		valsize = userbufsize -
10010Sstevel@tonic-gate 		    offsetof(struct openprom_opr64, message);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		i = prom_version_check(opr->message, valsize, &id);
10040Sstevel@tonic-gate 		opr->return_code = i;
10050Sstevel@tonic-gate 		opr->nodeid = (int)id;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		valsize = offsetof(struct openprom_opr64, message);
10080Sstevel@tonic-gate 		valsize += strlen(opr->message) + 1;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 		/*
10110Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
10120Sstevel@tonic-gate 		 */
10130Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
10140Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
10150Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
10160Sstevel@tonic-gate 			error = EFAULT;
10170Sstevel@tonic-gate 		break;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	}	/* case OPROMREADY64 */
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	case WANBOOT_SETKEY: {
10220Sstevel@tonic-gate 		struct wankeyio *wp;
10230Sstevel@tonic-gate 		int reslen;
10240Sstevel@tonic-gate 		int status;
10250Sstevel@tonic-gate 		int rv;
10260Sstevel@tonic-gate 		int i;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		/*
10290Sstevel@tonic-gate 		 * The argument is a struct wankeyio.  Validate it as best
10300Sstevel@tonic-gate 		 * we can.
10310Sstevel@tonic-gate 		 */
10320Sstevel@tonic-gate 		if (userbufsize != (sizeof (struct wankeyio))) {
10330Sstevel@tonic-gate 			error = EINVAL;
10340Sstevel@tonic-gate 			break;
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
10370Sstevel@tonic-gate 		    opp->oprom_array, sizeof (struct wankeyio)) != 0) {
10380Sstevel@tonic-gate 			error = EFAULT;
10390Sstevel@tonic-gate 			break;
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 		wp = (struct wankeyio *)opp->oprom_array;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 		/* check for key name and key size overflow */
10440Sstevel@tonic-gate 		for (i = 0; i < WANBOOT_MAXKEYNAMELEN; i++)
10450Sstevel@tonic-gate 			if (wp->wk_keyname[i] == '\0')
10460Sstevel@tonic-gate 				break;
10470Sstevel@tonic-gate 		if ((i == WANBOOT_MAXKEYNAMELEN) ||
10480Sstevel@tonic-gate 		    (wp->wk_keysize > WANBOOT_MAXKEYLEN)) {
10490Sstevel@tonic-gate 			error = EINVAL;
10500Sstevel@tonic-gate 			break;
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		rv = prom_set_security_key(wp->wk_keyname, wp->wk_u.key,
10540Sstevel@tonic-gate 		    wp->wk_keysize, &reslen, &status);
10550Sstevel@tonic-gate 		if (rv)
10560Sstevel@tonic-gate 			error = EIO;
10570Sstevel@tonic-gate 		else
10580Sstevel@tonic-gate 			switch (status) {
10590Sstevel@tonic-gate 				case 0:
10600Sstevel@tonic-gate 					error = 0;
10610Sstevel@tonic-gate 					break;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 				case -2:	/* out of key storage space */
10640Sstevel@tonic-gate 					error = ENOSPC;
10650Sstevel@tonic-gate 					break;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 				case -3:	/* key name or value too long */
10680Sstevel@tonic-gate 					error = EINVAL;
10690Sstevel@tonic-gate 					break;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 				case -4:	/* can't delete:  no such key */
10720Sstevel@tonic-gate 					error = ENOENT;
10730Sstevel@tonic-gate 					break;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 				case -1:	/* unspecified error */
10760Sstevel@tonic-gate 				default:	/* this should not happen */
10770Sstevel@tonic-gate 					error = EIO;
10780Sstevel@tonic-gate 					break;
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 		break;
10810Sstevel@tonic-gate 	}	/* case WANBOOT_SETKEY */
10820Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
10830Sstevel@tonic-gate 	}	/* switch (cmd)	*/
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	kmem_free(opp, userbufsize + sizeof (uint_t) + 1);
10860Sstevel@tonic-gate 	return (error);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*ARGSUSED*/
10900Sstevel@tonic-gate static int
opromioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)10910Sstevel@tonic-gate opromioctl(dev_t dev, int cmd, intptr_t arg, int mode,
10920Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
10930Sstevel@tonic-gate {
10940Sstevel@tonic-gate 	struct oprom_state *st;
10950Sstevel@tonic-gate 	struct opromioctl_args arg_block;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	if (getminor(dev) >= MAX_OPENS)
10980Sstevel@tonic-gate 		return (ENXIO);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
11010Sstevel@tonic-gate 	ASSERT(st->already_open);
11020Sstevel@tonic-gate 	arg_block.st = st;
11030Sstevel@tonic-gate 	arg_block.cmd = cmd;
11040Sstevel@tonic-gate 	arg_block.arg = arg;
11050Sstevel@tonic-gate 	arg_block.mode = mode;
11060Sstevel@tonic-gate 	return (prom_tree_access(opromioctl_cb, &arg_block, &st->tree_gen));
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate  * Copyin string and verify the actual string length is less than maxsize
11110Sstevel@tonic-gate  * specified by the caller.
11120Sstevel@tonic-gate  *
11130Sstevel@tonic-gate  * Currently, maxsize is either OBP_MAXPROPNAME for property names
11140Sstevel@tonic-gate  * or MAXPATHLEN for device path names. userbufsize is specified
11150Sstevel@tonic-gate  * by the userland caller.
11160Sstevel@tonic-gate  */
11170Sstevel@tonic-gate static int
oprom_copyinstr(intptr_t arg,char * buf,size_t bufsize,size_t maxsize)11180Sstevel@tonic-gate oprom_copyinstr(intptr_t arg, char *buf, size_t bufsize, size_t maxsize)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	int error;
11210Sstevel@tonic-gate 	size_t actual_len;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if ((error = copyinstr(((caddr_t)arg + sizeof (uint_t)),
11240Sstevel@tonic-gate 	    buf, bufsize, &actual_len)) != 0) {
11250Sstevel@tonic-gate 		return (error);
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 	if ((actual_len == 0) || (actual_len > maxsize)) {
11280Sstevel@tonic-gate 		return (EINVAL);
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	return (0);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
1135789Sahrens  * Check pnode_t passed in from userland
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate static int
oprom_checknodeid(pnode_t node_id,pnode_t current_id)1138789Sahrens oprom_checknodeid(pnode_t node_id, pnode_t current_id)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	int depth;
1141789Sahrens 	pnode_t id[OBP_STACKDEPTH];
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/*
11440Sstevel@tonic-gate 	 * optimized path
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate 	if (node_id == 0) {
11470Sstevel@tonic-gate 		return (1);
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 	if (node_id == OBP_BADNODE) {
11500Sstevel@tonic-gate 		return (0);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 	if ((current_id != OBP_BADNODE) && ((node_id == current_id) ||
11530Sstevel@tonic-gate 	    (node_id == prom_nextnode(current_id)) ||
11540Sstevel@tonic-gate 	    (node_id == prom_childnode(current_id)))) {
11550Sstevel@tonic-gate 		return (1);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * long path: walk from root till we find node_id
11600Sstevel@tonic-gate 	 */
11610Sstevel@tonic-gate 	depth = 1;
1162789Sahrens 	id[0] = prom_nextnode((pnode_t)0);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	while (depth) {
11650Sstevel@tonic-gate 		if (id[depth - 1] == node_id)
11660Sstevel@tonic-gate 			return (1);	/* node_id found */
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 		if (id[depth] = prom_childnode(id[depth - 1])) {
11690Sstevel@tonic-gate 			depth++;
11700Sstevel@tonic-gate 			continue;
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 		while (depth &&
11740Sstevel@tonic-gate 		    ((id[depth - 1] = prom_nextnode(id[depth - 1])) == 0))
11750Sstevel@tonic-gate 			depth--;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	return (0);	/* node_id not found */
11780Sstevel@tonic-gate }
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate static int
oprom_copytree(struct oprom_state * st,uint_t flag)11810Sstevel@tonic-gate oprom_copytree(struct oprom_state *st, uint_t flag)
11820Sstevel@tonic-gate {
11830Sstevel@tonic-gate 	ASSERT(st->snapshot == NULL && st->size == 0);
11840Sstevel@tonic-gate 	return (oprom_copynode(
11850Sstevel@tonic-gate 	    prom_nextnode(0), flag, &st->snapshot, &st->size));
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate static int
oprom_snapshot(struct oprom_state * st,intptr_t arg)11890Sstevel@tonic-gate oprom_snapshot(struct oprom_state *st, intptr_t arg)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	uint_t flag;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_SNAP) == -1)
11940Sstevel@tonic-gate 		return (EBUSY);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/* copyin flag and create snapshot */
11970Sstevel@tonic-gate 	if ((copyin((void *)arg, &flag, sizeof (uint_t)) != 0) ||
11980Sstevel@tonic-gate 	    (oprom_copytree(st, flag) != 0)) {
11990Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
12000Sstevel@tonic-gate 		return (EFAULT);
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	/* copyout the size of the snapshot */
12050Sstevel@tonic-gate 	flag = (uint_t)st->size;
12060Sstevel@tonic-gate 	if (copyout(&flag, (void *)arg, sizeof (uint_t)) != 0) {
12070Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
12080Sstevel@tonic-gate 		st->snapshot = NULL;
12090Sstevel@tonic-gate 		st->size = 0;
12100Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
12110Sstevel@tonic-gate 		return (EFAULT);
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_DONE);
12150Sstevel@tonic-gate 	return (0);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate static int
oprom_copyout(struct oprom_state * st,intptr_t arg)12190Sstevel@tonic-gate oprom_copyout(struct oprom_state *st, intptr_t arg)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	int error = 0;
12220Sstevel@tonic-gate 	uint_t size;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_COPY) == -1)
12250Sstevel@tonic-gate 		return (EBUSY);
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	/* copyin size and copyout snapshot */
12280Sstevel@tonic-gate 	if (copyin((void *)arg, &size, sizeof (uint_t)) != 0)
12290Sstevel@tonic-gate 		error = EFAULT;
12300Sstevel@tonic-gate 	else if (size < st->size)
12310Sstevel@tonic-gate 		error = EINVAL;
12320Sstevel@tonic-gate 	else if (copyout(st->snapshot, (void *)arg, st->size) != 0)
12330Sstevel@tonic-gate 		error = EFAULT;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if (error) {
12360Sstevel@tonic-gate 		/*
12370Sstevel@tonic-gate 		 * on error keep the snapshot until a successful
12380Sstevel@tonic-gate 		 * copyout or when the driver is closed.
12390Sstevel@tonic-gate 		 */
12400Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_DONE);
12410Sstevel@tonic-gate 		return (error);
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	kmem_free(st->snapshot, st->size);
12450Sstevel@tonic-gate 	st->snapshot = NULL;
12460Sstevel@tonic-gate 	st->size = 0;
12470Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_IDLE);
12480Sstevel@tonic-gate 	return (0);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*
12520Sstevel@tonic-gate  * Copy all properties of nodeid into a single packed nvlist
12530Sstevel@tonic-gate  */
12540Sstevel@tonic-gate static int
oprom_copyprop(pnode_t nodeid,uint_t flag,nvlist_t * nvl)1255789Sahrens oprom_copyprop(pnode_t nodeid, uint_t flag, nvlist_t *nvl)
12560Sstevel@tonic-gate {
12570Sstevel@tonic-gate 	int proplen;
12580Sstevel@tonic-gate 	char *propname, *propval, *buf1, *buf2;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	ASSERT(nvl != NULL);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * non verbose mode, get the "name" property only
12640Sstevel@tonic-gate 	 */
12650Sstevel@tonic-gate 	if (flag == 0) {
12660Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, "name");
12670Sstevel@tonic-gate 		if (proplen <= 0) {
12680Sstevel@tonic-gate 			cmn_err(CE_WARN,
12690Sstevel@tonic-gate 			    "failed to get the name of openprom node 0x%x",
12700Sstevel@tonic-gate 			    nodeid);
12710Sstevel@tonic-gate 			(void) nvlist_add_string(nvl, "name", "");
12720Sstevel@tonic-gate 			return (0);
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
12750Sstevel@tonic-gate 		(void) prom_getprop(nodeid, "name", propval);
12760Sstevel@tonic-gate 		(void) nvlist_add_string(nvl, "name", propval);
12770Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
12780Sstevel@tonic-gate 		return (0);
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	/*
12820Sstevel@tonic-gate 	 * Ask for first property by passing a NULL string
12830Sstevel@tonic-gate 	 */
12840Sstevel@tonic-gate 	buf1 = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
12850Sstevel@tonic-gate 	buf2 = kmem_zalloc(OBP_MAXPROPNAME, KM_SLEEP);
12860Sstevel@tonic-gate 	buf1[0] = '\0';
12870Sstevel@tonic-gate 	while (propname = (char *)prom_nextprop(nodeid, buf1, buf2)) {
12880Sstevel@tonic-gate 		if (strlen(propname) == 0)
12890Sstevel@tonic-gate 			break;		/* end of prop list */
12900Sstevel@tonic-gate 		(void) strcpy(buf1, propname);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, propname);
12930Sstevel@tonic-gate 		if (proplen == 0) {
12940Sstevel@tonic-gate 			/* boolean property */
12950Sstevel@tonic-gate 			(void) nvlist_add_boolean(nvl, propname);
12960Sstevel@tonic-gate 			continue;
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 		/* add 1 for null termination in case of a string */
12990Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
13000Sstevel@tonic-gate 		(void) prom_getprop(nodeid, propname, propval);
13010Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, propname,
13020Sstevel@tonic-gate 		    (uchar_t *)propval, proplen + 1);
13030Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
13040Sstevel@tonic-gate 		bzero(buf2, OBP_MAXPROPNAME);
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	kmem_free(buf1, OBP_MAXPROPNAME);
13080Sstevel@tonic-gate 	kmem_free(buf2, OBP_MAXPROPNAME);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	return (0);
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate /*
13140Sstevel@tonic-gate  * Copy all children and descendents into a a packed nvlist
13150Sstevel@tonic-gate  */
13160Sstevel@tonic-gate static int
oprom_copychild(pnode_t nodeid,uint_t flag,char ** buf,size_t * size)1317789Sahrens oprom_copychild(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
13180Sstevel@tonic-gate {
13190Sstevel@tonic-gate 	nvlist_t *nvl;
1320789Sahrens 	pnode_t child = prom_childnode(nodeid);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	if (child == 0)
13230Sstevel@tonic-gate 		return (0);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, 0, KM_SLEEP);
13260Sstevel@tonic-gate 	while (child != 0) {
13270Sstevel@tonic-gate 		char *nodebuf = NULL;
13280Sstevel@tonic-gate 		size_t nodesize = 0;
13290Sstevel@tonic-gate 		if (oprom_copynode(child, flag, &nodebuf, &nodesize)) {
13300Sstevel@tonic-gate 			nvlist_free(nvl);
13310Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to copy nodeid 0x%x", child);
13320Sstevel@tonic-gate 			return (-1);
13330Sstevel@tonic-gate 		}
13340Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "node",
13350Sstevel@tonic-gate 		    (uchar_t *)nodebuf, nodesize);
13360Sstevel@tonic-gate 		kmem_free(nodebuf, nodesize);
13370Sstevel@tonic-gate 		child = prom_nextnode(child);
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	(void) nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
13410Sstevel@tonic-gate 	nvlist_free(nvl);
13420Sstevel@tonic-gate 	return (0);
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate  * Copy a node into a packed nvlist
13470Sstevel@tonic-gate  */
13480Sstevel@tonic-gate static int
oprom_copynode(pnode_t nodeid,uint_t flag,char ** buf,size_t * size)1349789Sahrens oprom_copynode(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	int error = 0;
13520Sstevel@tonic-gate 	nvlist_t *nvl;
13530Sstevel@tonic-gate 	char *childlist = NULL;
13540Sstevel@tonic-gate 	size_t childsize = 0;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
13570Sstevel@tonic-gate 	ASSERT(nvl != NULL);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	/* @nodeid -- @ is not a legal char in a 1275 property name */
13600Sstevel@tonic-gate 	(void) nvlist_add_int32(nvl, "@nodeid", (int32_t)nodeid);
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/* properties */
13630Sstevel@tonic-gate 	if (error = oprom_copyprop(nodeid, flag, nvl))
13640Sstevel@tonic-gate 		goto fail;
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	/* children */
13670Sstevel@tonic-gate 	error = oprom_copychild(nodeid, flag, &childlist, &childsize);
13680Sstevel@tonic-gate 	if (error != 0)
13690Sstevel@tonic-gate 		goto fail;
13700Sstevel@tonic-gate 	if (childlist != NULL) {
13710Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "@child",
13720Sstevel@tonic-gate 		    (uchar_t *)childlist, (uint_t)childsize);
13730Sstevel@tonic-gate 		kmem_free(childlist, childsize);
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/* pack into contiguous buffer */
13770Sstevel@tonic-gate 	error = nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate fail:
13800Sstevel@tonic-gate 	nvlist_free(nvl);
13810Sstevel@tonic-gate 	return (error);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate /*
13850Sstevel@tonic-gate  * The driver is stateful across OPROMSNAPSHOT and OPROMCOPYOUT.
13860Sstevel@tonic-gate  * This function encapsulates the state machine:
13870Sstevel@tonic-gate  *
13880Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
13890Sstevel@tonic-gate  *	|		SNAPSHOT		COPYOUT	 |
13900Sstevel@tonic-gate  *	--------------------------------------------------
13910Sstevel@tonic-gate  *
13920Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
13930Sstevel@tonic-gate  */
13940Sstevel@tonic-gate static int
oprom_setstate(struct oprom_state * st,int16_t new_state)13950Sstevel@tonic-gate oprom_setstate(struct oprom_state *st, int16_t new_state)
13960Sstevel@tonic-gate {
13970Sstevel@tonic-gate 	int ret = 0;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
14000Sstevel@tonic-gate 	switch (new_state) {
14010Sstevel@tonic-gate 	case IOC_IDLE:
14020Sstevel@tonic-gate 	case IOC_DONE:
14030Sstevel@tonic-gate 		break;
14040Sstevel@tonic-gate 	case IOC_SNAP:
14050Sstevel@tonic-gate 		if (st->ioc_state != IOC_IDLE)
14060Sstevel@tonic-gate 			ret = -1;
14070Sstevel@tonic-gate 		break;
14080Sstevel@tonic-gate 	case IOC_COPY:
14090Sstevel@tonic-gate 		if (st->ioc_state != IOC_DONE)
14100Sstevel@tonic-gate 			ret = -1;
14110Sstevel@tonic-gate 		break;
14120Sstevel@tonic-gate 	default:
14130Sstevel@tonic-gate 		ret = -1;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if (ret == 0)
14170Sstevel@tonic-gate 		st->ioc_state = new_state;
14180Sstevel@tonic-gate 	else
14190Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
14200Sstevel@tonic-gate 		    st->ioc_state, new_state);
14210Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
14220Sstevel@tonic-gate 	return (ret);
14230Sstevel@tonic-gate }
1424