xref: /titanic_52/usr/src/uts/common/io/openprom.c (revision 3b133bec939f5230f040960ee1503dadd3dff343)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a204de77Scth  * Common Development and Distribution License (the "License").
6a204de77Scth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3b133becSGangadhar Mylapuram  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
2519397407SSherry Moore 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Ported from 4.1.1_PSRA: "@(#)openprom.c 1.19 91/02/19 SMI";
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Porting notes:
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * OPROMU2P unsupported after SunOS 4.x.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Only one of these devices per system is allowed.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Openprom eeprom options/devinfo driver.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
457c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
467c478bd9Sstevel@tonic-gate #include <sys/conf.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/promif.h>
547c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>	/* offsetof */
557c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
567c478bd9Sstevel@tonic-gate #include <sys/wanboot_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/zone.h>
58b52fe415Slipeng sang - Sun Microsystems - Beijing China #include <sys/consplat.h>
59*3b133becSGangadhar Mylapuram #include <sys/bootconf.h>
60*3b133becSGangadhar Mylapuram #include <sys/systm.h>
61*3b133becSGangadhar Mylapuram #include <sys/bootprops.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	MAX_OPENS	32	/* Up to this many simultaneous opens */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
667c478bd9Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
677c478bd9Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
687c478bd9Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * XXX	Make this dynamic.. or (better still) make the interface stateless
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate static struct oprom_state {
74fa9e4066Sahrens 	pnode_t	current_id;	/* node we're fetching props from */
757c478bd9Sstevel@tonic-gate 	int16_t	already_open;	/* if true, this instance is 'active' */
767c478bd9Sstevel@tonic-gate 	int16_t	ioc_state;	/* snapshot ioctl state */
777c478bd9Sstevel@tonic-gate 	char	*snapshot;	/* snapshot of all prom nodes */
787c478bd9Sstevel@tonic-gate 	size_t	size;		/* size of snapshot */
797c478bd9Sstevel@tonic-gate 	prom_generation_cookie_t tree_gen;
807c478bd9Sstevel@tonic-gate } oprom_state[MAX_OPENS];
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static kmutex_t oprom_lock;	/* serialize instance assignment */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int opromopen(dev_t *, int, int, cred_t *);
857c478bd9Sstevel@tonic-gate static int opromioctl(dev_t, int, intptr_t, int, cred_t *, int *);
867c478bd9Sstevel@tonic-gate static int opromclose(dev_t, int, int, cred_t *);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
897c478bd9Sstevel@tonic-gate 		void **result);
907c478bd9Sstevel@tonic-gate static int opattach(dev_info_t *, ddi_attach_cmd_t cmd);
917c478bd9Sstevel@tonic-gate static int opdetach(dev_info_t *, ddi_detach_cmd_t cmd);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* help functions */
94fa9e4066Sahrens static int oprom_checknodeid(pnode_t, pnode_t);
957c478bd9Sstevel@tonic-gate static int oprom_copyinstr(intptr_t, char *, size_t, size_t);
96fa9e4066Sahrens static int oprom_copynode(pnode_t, uint_t, char **, size_t *);
977c478bd9Sstevel@tonic-gate static int oprom_snapshot(struct oprom_state *, intptr_t);
987c478bd9Sstevel@tonic-gate static int oprom_copyout(struct oprom_state *, intptr_t);
997c478bd9Sstevel@tonic-gate static int oprom_setstate(struct oprom_state *, int16_t);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct cb_ops openeepr_cb_ops = {
1027c478bd9Sstevel@tonic-gate 	opromopen,		/* open */
1037c478bd9Sstevel@tonic-gate 	opromclose,		/* close */
1047c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
1057c478bd9Sstevel@tonic-gate 	nodev,			/* print */
1067c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
1077c478bd9Sstevel@tonic-gate 	nodev,			/* read */
1087c478bd9Sstevel@tonic-gate 	nodev,			/* write */
1097c478bd9Sstevel@tonic-gate 	opromioctl,		/* ioctl */
1107c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
1117c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
1127c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
1137c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
1147c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
1157c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab  */
1167c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static struct dev_ops openeepr_ops = {
1207c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
1217c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
1227c478bd9Sstevel@tonic-gate 	opinfo,			/* info */
1237c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1247c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
1257c478bd9Sstevel@tonic-gate 	opattach,		/* attach */
1267c478bd9Sstevel@tonic-gate 	opdetach,		/* detach */
1277c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
1287c478bd9Sstevel@tonic-gate 	&openeepr_cb_ops,	/* driver operations */
12919397407SSherry Moore 	NULL,			/* bus operations */
13019397407SSherry Moore 	NULL,			/* power */
13119397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1387c478bd9Sstevel@tonic-gate 	&mod_driverops,
139b52fe415Slipeng sang - Sun Microsystems - Beijing China 	"OPENPROM/NVRAM Driver",
1407c478bd9Sstevel@tonic-gate 	&openeepr_ops
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1447c478bd9Sstevel@tonic-gate 	MODREV_1,
1457c478bd9Sstevel@tonic-gate 	&modldrv,
1467c478bd9Sstevel@tonic-gate 	NULL
1477c478bd9Sstevel@tonic-gate };
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int
1507c478bd9Sstevel@tonic-gate _init(void)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int	error;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	mutex_init(&oprom_lock, NULL, MUTEX_DRIVER, NULL);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
1577c478bd9Sstevel@tonic-gate 	if (error != 0) {
1587c478bd9Sstevel@tonic-gate 		mutex_destroy(&oprom_lock);
1597c478bd9Sstevel@tonic-gate 		return (error);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	return (0);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate int
1667c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate int
1727c478bd9Sstevel@tonic-gate _fini(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	int	error;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
1777c478bd9Sstevel@tonic-gate 	if (error != 0)
1787c478bd9Sstevel@tonic-gate 		return (error);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	mutex_destroy(&oprom_lock);
1817c478bd9Sstevel@tonic-gate 	return (0);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate static dev_info_t *opdip;
185fa9e4066Sahrens static pnode_t options_nodeid;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1887c478bd9Sstevel@tonic-gate static int
1897c478bd9Sstevel@tonic-gate opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	switch (infocmd) {
1947c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1957c478bd9Sstevel@tonic-gate 		*result = (void *)opdip;
1967c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1977c478bd9Sstevel@tonic-gate 		break;
1987c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1997c478bd9Sstevel@tonic-gate 		/* All dev_t's map to the same, single instance */
2007c478bd9Sstevel@tonic-gate 		*result = (void *)0;
2017c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	default:
2047c478bd9Sstevel@tonic-gate 		break;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (error);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static int
2117c478bd9Sstevel@tonic-gate opattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	switch (cmd) {
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2167c478bd9Sstevel@tonic-gate 		if (prom_is_openprom()) {
2177c478bd9Sstevel@tonic-gate 			options_nodeid = prom_optionsnode();
2187c478bd9Sstevel@tonic-gate 		} else {
2197c478bd9Sstevel@tonic-gate 			options_nodeid = OBP_BADNODE;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		opdip = dip;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "openprom", S_IFCHR,
2257c478bd9Sstevel@tonic-gate 		    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
2267c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	default:
2327c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static int
2377c478bd9Sstevel@tonic-gate opdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2407c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2437c478bd9Sstevel@tonic-gate 	opdip = NULL;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
2507c478bd9Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
2517c478bd9Sstevel@tonic-gate  * entry in the oprom_state[] table.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2547c478bd9Sstevel@tonic-gate static int
2557c478bd9Sstevel@tonic-gate opromopen(dev_t *devp, int flag, int otyp, cred_t *credp)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int m;
2587c478bd9Sstevel@tonic-gate 	struct oprom_state *st = oprom_state;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (getminor(*devp) != 0)
2617c478bd9Sstevel@tonic-gate 		return (ENXIO);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
2647c478bd9Sstevel@tonic-gate 	for (m = 0; m < MAX_OPENS; m++)
2657c478bd9Sstevel@tonic-gate 		if (st->already_open)
2667c478bd9Sstevel@tonic-gate 			st++;
2677c478bd9Sstevel@tonic-gate 		else {
2687c478bd9Sstevel@tonic-gate 			st->already_open = 1;
2697c478bd9Sstevel@tonic-gate 			/*
2707c478bd9Sstevel@tonic-gate 			 * It's ours.
2717c478bd9Sstevel@tonic-gate 			 */
272fa9e4066Sahrens 			st->current_id = (pnode_t)0;
2737c478bd9Sstevel@tonic-gate 			ASSERT(st->snapshot == NULL && st->size == 0);
2747c478bd9Sstevel@tonic-gate 			ASSERT(st->ioc_state == IOC_IDLE);
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (m == MAX_OPENS)  {
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * "Thank you for calling, but all our lines are
2827c478bd9Sstevel@tonic-gate 		 * busy at the moment.."
2837c478bd9Sstevel@tonic-gate 		 *
2847c478bd9Sstevel@tonic-gate 		 * We could get sophisticated here, and go into a
2857c478bd9Sstevel@tonic-gate 		 * sleep-retry loop .. but hey, I just can't see
2867c478bd9Sstevel@tonic-gate 		 * that many processes sitting in this driver.
2877c478bd9Sstevel@tonic-gate 		 *
2887c478bd9Sstevel@tonic-gate 		 * (And if it does become possible, then we should
2897c478bd9Sstevel@tonic-gate 		 * change the interface so that the 'state' is held
2907c478bd9Sstevel@tonic-gate 		 * external to the driver)
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		return (EAGAIN);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)m);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	return (0);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3017c478bd9Sstevel@tonic-gate static int
3027c478bd9Sstevel@tonic-gate opromclose(dev_t dev, int flag, int otype, cred_t *cred_p)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
3077c478bd9Sstevel@tonic-gate 	ASSERT(getminor(dev) < MAX_OPENS && st->already_open != 0);
3087c478bd9Sstevel@tonic-gate 	if (st->snapshot) {
3097c478bd9Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
3107c478bd9Sstevel@tonic-gate 		st->snapshot = NULL;
3117c478bd9Sstevel@tonic-gate 		st->size = 0;
3127c478bd9Sstevel@tonic-gate 		st->ioc_state = IOC_IDLE;
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
3157c478bd9Sstevel@tonic-gate 	st->already_open = 0;
3167c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (0);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
321*3b133becSGangadhar Mylapuram #ifdef __sparc
322*3b133becSGangadhar Mylapuram static int
323*3b133becSGangadhar Mylapuram get_bootpath_prop(char *bootpath)
324*3b133becSGangadhar Mylapuram {
325*3b133becSGangadhar Mylapuram 	if (root_is_ramdisk) {
326*3b133becSGangadhar Mylapuram 		if (BOP_GETPROP(bootops, "bootarchive", bootpath) == -1)
327*3b133becSGangadhar Mylapuram 			return (-1);
328*3b133becSGangadhar Mylapuram 		(void) strlcat(bootpath, ":a", BO_MAXOBJNAME);
329*3b133becSGangadhar Mylapuram 	} else {
330*3b133becSGangadhar Mylapuram 		if ((BOP_GETPROP(bootops, "bootpath", bootpath) == -1) ||
331*3b133becSGangadhar Mylapuram 		    strlen(bootpath) == 0) {
332*3b133becSGangadhar Mylapuram 			if (BOP_GETPROP(bootops,
333*3b133becSGangadhar Mylapuram 			    "boot-path", bootpath) == -1)
334*3b133becSGangadhar Mylapuram 				return (-1);
335*3b133becSGangadhar Mylapuram 		}
336*3b133becSGangadhar Mylapuram 		if (memcmp(bootpath, BP_ISCSI_DISK,
337*3b133becSGangadhar Mylapuram 		    strlen(BP_ISCSI_DISK)) == 0) {
338*3b133becSGangadhar Mylapuram 			get_iscsi_bootpath_vhci(bootpath);
339*3b133becSGangadhar Mylapuram 		}
340*3b133becSGangadhar Mylapuram 	}
341*3b133becSGangadhar Mylapuram 	return (0);
342*3b133becSGangadhar Mylapuram }
343*3b133becSGangadhar Mylapuram #endif
344*3b133becSGangadhar Mylapuram 
3457c478bd9Sstevel@tonic-gate struct opromioctl_args {
3467c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
3477c478bd9Sstevel@tonic-gate 	int cmd;
3487c478bd9Sstevel@tonic-gate 	intptr_t arg;
3497c478bd9Sstevel@tonic-gate 	int mode;
3507c478bd9Sstevel@tonic-gate };
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3537c478bd9Sstevel@tonic-gate static int
3547c478bd9Sstevel@tonic-gate opromioctl_cb(void *avp, int has_changed)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	struct opromioctl_args *argp = avp;
3577c478bd9Sstevel@tonic-gate 	int cmd;
3587c478bd9Sstevel@tonic-gate 	intptr_t arg;
3597c478bd9Sstevel@tonic-gate 	int mode;
3607c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
3617c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
3627c478bd9Sstevel@tonic-gate 	int valsize;
3637c478bd9Sstevel@tonic-gate 	char *valbuf;
3647c478bd9Sstevel@tonic-gate 	int error = 0;
3657c478bd9Sstevel@tonic-gate 	uint_t userbufsize;
366fa9e4066Sahrens 	pnode_t node_id;
3677c478bd9Sstevel@tonic-gate 	char propname[OBP_MAXPROPNAME];
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	st = argp->st;
3707c478bd9Sstevel@tonic-gate 	cmd = argp->cmd;
3717c478bd9Sstevel@tonic-gate 	arg = argp->arg;
3727c478bd9Sstevel@tonic-gate 	mode = argp->mode;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (has_changed) {
3757c478bd9Sstevel@tonic-gate 		/*
3767c478bd9Sstevel@tonic-gate 		 * The prom tree has changed since we last used current_id,
3777c478bd9Sstevel@tonic-gate 		 * so we need to check it.
3787c478bd9Sstevel@tonic-gate 		 */
3797c478bd9Sstevel@tonic-gate 		if ((st->current_id != OBP_NONODE) &&
3807c478bd9Sstevel@tonic-gate 		    (st->current_id != OBP_BADNODE)) {
3817c478bd9Sstevel@tonic-gate 			if (oprom_checknodeid(st->current_id, OBP_NONODE) == 0)
3827c478bd9Sstevel@tonic-gate 				st->current_id = OBP_BADNODE;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * Check permissions
3887c478bd9Sstevel@tonic-gate 	 * and weed out unsupported commands on x86 platform
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	switch (cmd) {
3917c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
3927c478bd9Sstevel@tonic-gate 	case OPROMLISTKEYSLEN:
3937c478bd9Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
3947c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
3957c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
3967c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
3977c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
3987c478bd9Sstevel@tonic-gate 			error = EFAULT;
3997c478bd9Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
4007c478bd9Sstevel@tonic-gate 		break;
4017c478bd9Sstevel@tonic-gate 	case OPROMLISTKEYS:
4027c478bd9Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
4037c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
4047c478bd9Sstevel@tonic-gate 			return (EFAULT);
4057c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize)
4067c478bd9Sstevel@tonic-gate 			return (EINVAL);
4077c478bd9Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
4087c478bd9Sstevel@tonic-gate 		if (prom_asr_list_keys((caddr_t)valbuf) == -1) {
4097c478bd9Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
4107c478bd9Sstevel@tonic-gate 			return (EFAULT);
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4137c478bd9Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
4147c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
4157c478bd9Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
4167c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
4177c478bd9Sstevel@tonic-gate 			error = EFAULT;
4187c478bd9Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
4197c478bd9Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 	case OPROMEXPORT:
4227c478bd9Sstevel@tonic-gate 		valsize = prom_asr_export_len();
4237c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
4247c478bd9Sstevel@tonic-gate 			return (EFAULT);
4257c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize)
4267c478bd9Sstevel@tonic-gate 			return (EINVAL);
4277c478bd9Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
4287c478bd9Sstevel@tonic-gate 		if (prom_asr_export((caddr_t)valbuf) == -1) {
4297c478bd9Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
4307c478bd9Sstevel@tonic-gate 			return (EFAULT);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4337c478bd9Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
4347c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
4357c478bd9Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
4367c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
4377c478bd9Sstevel@tonic-gate 			error = EFAULT;
4387c478bd9Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
4397c478bd9Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
4407c478bd9Sstevel@tonic-gate 		break;
4417c478bd9Sstevel@tonic-gate 	case OPROMEXPORTLEN:
4427c478bd9Sstevel@tonic-gate 		valsize = prom_asr_export_len();
4437c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
4447c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
4457c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
4467c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
4477c478bd9Sstevel@tonic-gate 			error = EFAULT;
4487c478bd9Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate #endif
4517c478bd9Sstevel@tonic-gate 	case OPROMGETOPT:
4527c478bd9Sstevel@tonic-gate 	case OPROMNXTOPT:
4537c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4547c478bd9Sstevel@tonic-gate 			return (EPERM);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		node_id = options_nodeid;
4577c478bd9Sstevel@tonic-gate 		break;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	case OPROMSETOPT:
4607c478bd9Sstevel@tonic-gate 	case OPROMSETOPT2:
4617c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4627c478bd9Sstevel@tonic-gate 		if (mode & FWRITE) {
4637c478bd9Sstevel@tonic-gate 			node_id = options_nodeid;
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate #endif /* !__i386 && !__amd64 */
4677c478bd9Sstevel@tonic-gate 		return (EPERM);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	case OPROMNEXT:
4707c478bd9Sstevel@tonic-gate 	case OPROMCHILD:
4717c478bd9Sstevel@tonic-gate 	case OPROMGETPROP:
4727c478bd9Sstevel@tonic-gate 	case OPROMGETPROPLEN:
4737c478bd9Sstevel@tonic-gate 	case OPROMNXTPROP:
4747c478bd9Sstevel@tonic-gate 	case OPROMSETNODEID:
4757c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4767c478bd9Sstevel@tonic-gate 			return (EPERM);
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		node_id = st->current_id;
4797c478bd9Sstevel@tonic-gate 		break;
4807c478bd9Sstevel@tonic-gate 	case OPROMCOPYOUT:
4817c478bd9Sstevel@tonic-gate 		if (st->snapshot == NULL)
4827c478bd9Sstevel@tonic-gate 			return (EINVAL);
4837c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
4847c478bd9Sstevel@tonic-gate 	case OPROMSNAPSHOT:
4857c478bd9Sstevel@tonic-gate 	case OPROMGETCONS:
4867c478bd9Sstevel@tonic-gate 	case OPROMGETBOOTARGS:
487*3b133becSGangadhar Mylapuram 	case OPROMGETBOOTPATH:
4887c478bd9Sstevel@tonic-gate 	case OPROMGETVERSION:
4897c478bd9Sstevel@tonic-gate 	case OPROMPATH2DRV:
4907c478bd9Sstevel@tonic-gate 	case OPROMPROM2DEVNAME:
4917c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
4927c478bd9Sstevel@tonic-gate 	case OPROMGETFBNAME:
4937c478bd9Sstevel@tonic-gate 	case OPROMDEV2PROMNAME:
4947c478bd9Sstevel@tonic-gate 	case OPROMREADY64:
4957c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
4967c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
4977c478bd9Sstevel@tonic-gate 			return (EPERM);
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		break;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
5027c478bd9Sstevel@tonic-gate 	case WANBOOT_SETKEY:
5037c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
5047c478bd9Sstevel@tonic-gate 			return (EPERM);
5057c478bd9Sstevel@tonic-gate 		break;
5067c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !defined(__amd64) */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	default:
5097c478bd9Sstevel@tonic-gate 		return (EINVAL);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Deal with SNAPSHOT and COPYOUT ioctls first
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	switch (cmd) {
5167c478bd9Sstevel@tonic-gate 	case OPROMCOPYOUT:
5177c478bd9Sstevel@tonic-gate 		return (oprom_copyout(st, arg));
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	case OPROMSNAPSHOT:
5207c478bd9Sstevel@tonic-gate 		return (oprom_snapshot(st, arg));
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Copy in user argument length and allocation memory
5257c478bd9Sstevel@tonic-gate 	 *
5267c478bd9Sstevel@tonic-gate 	 * NB do not copyin the entire buffer we may not need
5277c478bd9Sstevel@tonic-gate 	 *	to. userbufsize can be as big as 32 K.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
5307c478bd9Sstevel@tonic-gate 		return (EFAULT);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (userbufsize == 0 || userbufsize > OPROMMAXPARAM)
5337c478bd9Sstevel@tonic-gate 		return (EINVAL);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	opp = (struct openpromio *)kmem_zalloc(
5367c478bd9Sstevel@tonic-gate 	    userbufsize + sizeof (uint_t) + 1, KM_SLEEP);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Execute command
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	switch (cmd) {
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	case OPROMGETOPT:
5447c478bd9Sstevel@tonic-gate 	case OPROMGETPROP:
5457c478bd9Sstevel@tonic-gate 	case OPROMGETPROPLEN:
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
5487c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
5497c478bd9Sstevel@tonic-gate 			error = EINVAL;
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		/*
5547c478bd9Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
5577c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
5587c478bd9Sstevel@tonic-gate 			break;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		(void) strcpy(propname, opp->oprom_array);
5617c478bd9Sstevel@tonic-gate 		valsize = prom_getproplen(node_id, propname);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but not an option.
5657c478bd9Sstevel@tonic-gate 		 */
5667c478bd9Sstevel@tonic-gate 		if ((cmd == OPROMGETOPT) && (strcmp("name", propname) == 0))
5677c478bd9Sstevel@tonic-gate 			valsize = -1;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		if (cmd == OPROMGETPROPLEN)  {
5707c478bd9Sstevel@tonic-gate 			int proplen = valsize;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 			if (userbufsize < sizeof (int)) {
5737c478bd9Sstevel@tonic-gate 				error = EINVAL;
5747c478bd9Sstevel@tonic-gate 				break;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize = sizeof (int);
5777c478bd9Sstevel@tonic-gate 			bcopy(&proplen, opp->oprom_array, valsize);
5787c478bd9Sstevel@tonic-gate 		} else if (valsize > 0 && valsize <= userbufsize) {
5797c478bd9Sstevel@tonic-gate 			bzero(opp->oprom_array, valsize + 1);
5807c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node_id, propname,
5817c478bd9Sstevel@tonic-gate 			    opp->oprom_array);
5827c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize;
5837c478bd9Sstevel@tonic-gate 			if (valsize < userbufsize)
5847c478bd9Sstevel@tonic-gate 				++valsize;	/* Forces NULL termination */
5857c478bd9Sstevel@tonic-gate 						/* If space permits */
5867c478bd9Sstevel@tonic-gate 		} else {
5877c478bd9Sstevel@tonic-gate 			/*
5887c478bd9Sstevel@tonic-gate 			 * XXX: There is no error code if the buf is too small.
5897c478bd9Sstevel@tonic-gate 			 * which is consistent with the current behavior.
5907c478bd9Sstevel@tonic-gate 			 *
5917c478bd9Sstevel@tonic-gate 			 * NB: This clause also handles the non-error
5927c478bd9Sstevel@tonic-gate 			 * zero length (boolean) property value case.
5937c478bd9Sstevel@tonic-gate 			 */
5947c478bd9Sstevel@tonic-gate 			opp->oprom_size = 0;
5957c478bd9Sstevel@tonic-gate 			(void) strcpy(opp->oprom_array, "");
5967c478bd9Sstevel@tonic-gate 			valsize = 1;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
5997c478bd9Sstevel@tonic-gate 			error = EFAULT;
6007c478bd9Sstevel@tonic-gate 		break;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	case OPROMNXTOPT:
6037c478bd9Sstevel@tonic-gate 	case OPROMNXTPROP:
6047c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
6057c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
6067c478bd9Sstevel@tonic-gate 			error = EINVAL;
6077c478bd9Sstevel@tonic-gate 			break;
6087c478bd9Sstevel@tonic-gate 		}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
6127c478bd9Sstevel@tonic-gate 		 */
6137c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
6147c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
6157c478bd9Sstevel@tonic-gate 			break;
6167c478bd9Sstevel@tonic-gate 		}
6177c478bd9Sstevel@tonic-gate 		valbuf = (char *)prom_nextprop(node_id, opp->oprom_array,
6187c478bd9Sstevel@tonic-gate 		    propname);
6197c478bd9Sstevel@tonic-gate 		valsize = strlen(valbuf);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but it's not an option.
6237c478bd9Sstevel@tonic-gate 		 */
6247c478bd9Sstevel@tonic-gate 		if ((cmd == OPROMNXTOPT) && valsize &&
6257c478bd9Sstevel@tonic-gate 		    (strcmp(valbuf, "name") == 0)) {
6267c478bd9Sstevel@tonic-gate 			valbuf = (char *)prom_nextprop(node_id, "name",
6277c478bd9Sstevel@tonic-gate 			    propname);
6287c478bd9Sstevel@tonic-gate 			valsize = strlen(valbuf);
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		if (valsize == 0) {
6327c478bd9Sstevel@tonic-gate 			opp->oprom_size = 0;
6337c478bd9Sstevel@tonic-gate 		} else if (++valsize <= userbufsize) {
6347c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize;
6357c478bd9Sstevel@tonic-gate 			bzero((caddr_t)opp->oprom_array, (size_t)valsize);
6367c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)valbuf, (caddr_t)opp->oprom_array,
6377c478bd9Sstevel@tonic-gate 			    (size_t)valsize);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
6417c478bd9Sstevel@tonic-gate 			error = EFAULT;
6427c478bd9Sstevel@tonic-gate 		break;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	case OPROMNEXT:
6457c478bd9Sstevel@tonic-gate 	case OPROMCHILD:
6467c478bd9Sstevel@tonic-gate 	case OPROMSETNODEID:
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		if (prom_is_openprom() == 0 ||
649fa9e4066Sahrens 		    userbufsize < sizeof (pnode_t)) {
6507c478bd9Sstevel@tonic-gate 			error = EINVAL;
6517c478bd9Sstevel@tonic-gate 			break;
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/*
655fa9e4066Sahrens 		 * The argument is a phandle. (aka pnode_t)
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
658fa9e4066Sahrens 		    opp->oprom_array, sizeof (pnode_t)) != 0) {
6597c478bd9Sstevel@tonic-gate 			error = EFAULT;
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		/*
664fa9e4066Sahrens 		 * If pnode_t from userland is garbage, we
6657c478bd9Sstevel@tonic-gate 		 * could confuse the PROM.
6667c478bd9Sstevel@tonic-gate 		 */
667fa9e4066Sahrens 		node_id = *(pnode_t *)opp->oprom_array;
6687c478bd9Sstevel@tonic-gate 		if (oprom_checknodeid(node_id, st->current_id) == 0) {
6697c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!nodeid 0x%x not found",
6707c478bd9Sstevel@tonic-gate 			    (int)node_id);
6717c478bd9Sstevel@tonic-gate 			error = EINVAL;
6727c478bd9Sstevel@tonic-gate 			break;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		if (cmd == OPROMNEXT)
6767c478bd9Sstevel@tonic-gate 			st->current_id = prom_nextnode(node_id);
6777c478bd9Sstevel@tonic-gate 		else if (cmd == OPROMCHILD)
6787c478bd9Sstevel@tonic-gate 			st->current_id = prom_childnode(node_id);
6797c478bd9Sstevel@tonic-gate 		else {
6807c478bd9Sstevel@tonic-gate 			/* OPROMSETNODEID */
6817c478bd9Sstevel@tonic-gate 			st->current_id = node_id;
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 
685fa9e4066Sahrens 		opp->oprom_size = sizeof (pnode_t);
686fa9e4066Sahrens 		*(pnode_t *)opp->oprom_array = st->current_id;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
689fa9e4066Sahrens 		    sizeof (pnode_t) + sizeof (uint_t)) != 0)
6907c478bd9Sstevel@tonic-gate 			error = EFAULT;
6917c478bd9Sstevel@tonic-gate 		break;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	case OPROMGETCONS:
6947c478bd9Sstevel@tonic-gate 		/*
6957c478bd9Sstevel@tonic-gate 		 * Is openboot supported on this machine?
6967c478bd9Sstevel@tonic-gate 		 * This ioctl used to return the console device,
6977c478bd9Sstevel@tonic-gate 		 * information; this is now done via modctl()
6987c478bd9Sstevel@tonic-gate 		 * in libdevinfo.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		opp->oprom_size = sizeof (char);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		opp->oprom_array[0] |= prom_is_openprom() ?
7037c478bd9Sstevel@tonic-gate 		    OPROMCONS_OPENPROM : 0;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * The rest of the info is needed by Install to
7077c478bd9Sstevel@tonic-gate 		 * decide if graphics should be started.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
7107c478bd9Sstevel@tonic-gate 		    plat_stdin_is_keyboard()) {
7117c478bd9Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDIN_IS_KBD;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
7157c478bd9Sstevel@tonic-gate 		    plat_stdout_is_framebuffer()) {
7167c478bd9Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDOUT_IS_FB;
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
7207c478bd9Sstevel@tonic-gate 		    sizeof (char) + sizeof (uint_t)) != 0)
7217c478bd9Sstevel@tonic-gate 			error = EFAULT;
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	case OPROMGETBOOTARGS: {
7257c478bd9Sstevel@tonic-gate 		extern char kern_bootargs[];
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		valsize = strlen(kern_bootargs) + 1;
7287c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize) {
7297c478bd9Sstevel@tonic-gate 			error = EINVAL;
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, kern_bootargs);
7337c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize - 1;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
7367c478bd9Sstevel@tonic-gate 			error = EFAULT;
737a204de77Scth 		break;
738a204de77Scth 	}
7397c478bd9Sstevel@tonic-gate 
740*3b133becSGangadhar Mylapuram 	case OPROMGETBOOTPATH: {
741*3b133becSGangadhar Mylapuram #if defined(__sparc) && defined(_OBP)
742*3b133becSGangadhar Mylapuram 
743*3b133becSGangadhar Mylapuram 		char bpath[OBP_MAXPATHLEN];
744*3b133becSGangadhar Mylapuram 		if (get_bootpath_prop(bpath) != 0) {
745*3b133becSGangadhar Mylapuram 			error = EINVAL;
746*3b133becSGangadhar Mylapuram 			break;
747*3b133becSGangadhar Mylapuram 		}
748*3b133becSGangadhar Mylapuram 		valsize = strlen(bpath) + 1;
749*3b133becSGangadhar Mylapuram 		if (valsize > userbufsize) {
750*3b133becSGangadhar Mylapuram 			error = EINVAL;
751*3b133becSGangadhar Mylapuram 			break;
752*3b133becSGangadhar Mylapuram 		}
753*3b133becSGangadhar Mylapuram 		(void) strcpy(opp->oprom_array, bpath);
754*3b133becSGangadhar Mylapuram 
755*3b133becSGangadhar Mylapuram #elif defined(__i386) || defined(__amd64)
756*3b133becSGangadhar Mylapuram 
757*3b133becSGangadhar Mylapuram 		extern char saved_cmdline[];
758*3b133becSGangadhar Mylapuram 		valsize = strlen(saved_cmdline) + 1;
759*3b133becSGangadhar Mylapuram 		if (valsize > userbufsize) {
760*3b133becSGangadhar Mylapuram 			error = EINVAL;
761*3b133becSGangadhar Mylapuram 			break;
762*3b133becSGangadhar Mylapuram 		}
763*3b133becSGangadhar Mylapuram 		(void) strcpy(opp->oprom_array, saved_cmdline);
764*3b133becSGangadhar Mylapuram #endif
765*3b133becSGangadhar Mylapuram 		opp->oprom_size = valsize - 1;
766*3b133becSGangadhar Mylapuram 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
767*3b133becSGangadhar Mylapuram 			error = EFAULT;
768*3b133becSGangadhar Mylapuram 		break;
769*3b133becSGangadhar Mylapuram 	}
770*3b133becSGangadhar Mylapuram 
7717c478bd9Sstevel@tonic-gate 	/*
7727c478bd9Sstevel@tonic-gate 	 * convert a prom device path to an equivalent devfs path
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 	case OPROMPROM2DEVNAME: {
7757c478bd9Sstevel@tonic-gate 		char *dev_name;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		/*
7787c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
7797c478bd9Sstevel@tonic-gate 		 */
7807c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
7817c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
7827c478bd9Sstevel@tonic-gate 			break;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		dev_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		error = i_promname_to_devname(opp->oprom_array, dev_name);
7887c478bd9Sstevel@tonic-gate 		if (error != 0) {
7897c478bd9Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7907c478bd9Sstevel@tonic-gate 			break;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 		valsize = opp->oprom_size = strlen(dev_name);
7937c478bd9Sstevel@tonic-gate 		if (++valsize > userbufsize) {
7947c478bd9Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
7957c478bd9Sstevel@tonic-gate 			error = EINVAL;
7967c478bd9Sstevel@tonic-gate 			break;
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, dev_name);
7997c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
8007c478bd9Sstevel@tonic-gate 			error = EFAULT;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		kmem_free(dev_name, MAXPATHLEN);
803a204de77Scth 		break;
804a204de77Scth 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * Convert a prom device path name to a driver name
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 	case OPROMPATH2DRV: {
8107c478bd9Sstevel@tonic-gate 		char *drv_name;
8117c478bd9Sstevel@tonic-gate 		major_t maj;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
8157c478bd9Sstevel@tonic-gate 		 */
8167c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
8177c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * convert path to a driver binding name
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		maj = path_to_major((char *)opp->oprom_array);
825a204de77Scth 		if (maj == DDI_MAJOR_T_NONE) {
8267c478bd9Sstevel@tonic-gate 			error = EINVAL;
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * resolve any aliases
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		if ((drv_name = ddi_major_to_name(maj)) == NULL) {
8347c478bd9Sstevel@tonic-gate 			error = EINVAL;
8357c478bd9Sstevel@tonic-gate 			break;
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, drv_name);
8397c478bd9Sstevel@tonic-gate 		opp->oprom_size = strlen(drv_name);
8407c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
8417c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + opp->oprom_size + 1) != 0)
8427c478bd9Sstevel@tonic-gate 			error = EFAULT;
843a204de77Scth 		break;
844a204de77Scth 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	case OPROMGETVERSION:
8477c478bd9Sstevel@tonic-gate 		/*
8487c478bd9Sstevel@tonic-gate 		 * Get a string representing the running version of the
8497c478bd9Sstevel@tonic-gate 		 * prom. How to create such a string is platform dependent,
8507c478bd9Sstevel@tonic-gate 		 * so we just defer to a promif function. If no such
8517c478bd9Sstevel@tonic-gate 		 * association exists, the promif implementation
8527c478bd9Sstevel@tonic-gate 		 * may copy the string "unknown" into the given buffer,
8537c478bd9Sstevel@tonic-gate 		 * and return its length (incl. NULL terminator).
8547c478bd9Sstevel@tonic-gate 		 *
8557c478bd9Sstevel@tonic-gate 		 * We expect prom_version_name to return the actual
8567c478bd9Sstevel@tonic-gate 		 * length of the string, but copy at most userbufsize
8577c478bd9Sstevel@tonic-gate 		 * bytes into the given buffer, including NULL termination.
8587c478bd9Sstevel@tonic-gate 		 */
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		valsize = prom_version_name(opp->oprom_array, userbufsize);
8617c478bd9Sstevel@tonic-gate 		if (valsize < 0) {
8627c478bd9Sstevel@tonic-gate 			error = EINVAL;
8637c478bd9Sstevel@tonic-gate 			break;
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		/*
8677c478bd9Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
8687c478bd9Sstevel@tonic-gate 		 */
8697c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
8707c478bd9Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
8717c478bd9Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
8727c478bd9Sstevel@tonic-gate 			error = EFAULT;
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
8767c478bd9Sstevel@tonic-gate 	case OPROMGETFBNAME:
8777c478bd9Sstevel@tonic-gate 		/*
8787c478bd9Sstevel@tonic-gate 		 * Return stdoutpath, if it's a frame buffer.
8797c478bd9Sstevel@tonic-gate 		 * Yes, we are comparing a possibly longer string against
8807c478bd9Sstevel@tonic-gate 		 * the size we're really going to copy, but so what?
8817c478bd9Sstevel@tonic-gate 		 */
8827c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
8837c478bd9Sstevel@tonic-gate 		    (prom_stdout_is_framebuffer() != 0) &&
8847c478bd9Sstevel@tonic-gate 		    (userbufsize > strlen(prom_stdoutpath()))) {
8857c478bd9Sstevel@tonic-gate 			prom_strip_options(prom_stdoutpath(),
8867c478bd9Sstevel@tonic-gate 			    opp->oprom_array);	/* strip options and copy */
8877c478bd9Sstevel@tonic-gate 			valsize = opp->oprom_size = strlen(opp->oprom_array);
8887c478bd9Sstevel@tonic-gate 			if (copyout(opp, (void *)arg,
8897c478bd9Sstevel@tonic-gate 			    valsize + 1 + sizeof (uint_t)) != 0)
8907c478bd9Sstevel@tonic-gate 				error = EFAULT;
8917c478bd9Sstevel@tonic-gate 		} else
8927c478bd9Sstevel@tonic-gate 			error = EINVAL;
8937c478bd9Sstevel@tonic-gate 		break;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	/*
8967c478bd9Sstevel@tonic-gate 	 * Convert a logical or physical device path to prom device path
8977c478bd9Sstevel@tonic-gate 	 */
8987c478bd9Sstevel@tonic-gate 	case OPROMDEV2PROMNAME: {
8997c478bd9Sstevel@tonic-gate 		char *prom_name;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		/*
9027c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
9037c478bd9Sstevel@tonic-gate 		 */
9047c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
9057c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
9067c478bd9Sstevel@tonic-gate 			break;
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		prom_name = kmem_alloc(userbufsize, KM_SLEEP);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		 * convert the devfs path to an equivalent prom path
9137c478bd9Sstevel@tonic-gate 		 */
9147c478bd9Sstevel@tonic-gate 		error = i_devname_to_promname(opp->oprom_array, prom_name,
9157c478bd9Sstevel@tonic-gate 		    userbufsize);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		if (error != 0) {
9187c478bd9Sstevel@tonic-gate 			kmem_free(prom_name, userbufsize);
9197c478bd9Sstevel@tonic-gate 			break;
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		for (valsize = 0; valsize < userbufsize; valsize++) {
9237c478bd9Sstevel@tonic-gate 			opp->oprom_array[valsize] = prom_name[valsize];
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 			if ((valsize > 0) && (prom_name[valsize] == '\0') &&
9267c478bd9Sstevel@tonic-gate 			    (prom_name[valsize-1] == '\0')) {
9277c478bd9Sstevel@tonic-gate 				break;
9287c478bd9Sstevel@tonic-gate 			}
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		kmem_free(prom_name, userbufsize);
9337c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
9347c478bd9Sstevel@tonic-gate 			error = EFAULT;
9357c478bd9Sstevel@tonic-gate 
936a204de77Scth 		break;
937a204de77Scth 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	case OPROMSETOPT:
9407c478bd9Sstevel@tonic-gate 	case OPROMSETOPT2: {
9417c478bd9Sstevel@tonic-gate 		int namebuflen;
9427c478bd9Sstevel@tonic-gate 		int valbuflen;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
9457c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
9467c478bd9Sstevel@tonic-gate 			error = EINVAL;
9477c478bd9Sstevel@tonic-gate 			break;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		/*
9517c478bd9Sstevel@tonic-gate 		 * The arguments are a property name and a value.
9527c478bd9Sstevel@tonic-gate 		 * Copy in the entire user buffer.
9537c478bd9Sstevel@tonic-gate 		 */
9547c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
9557c478bd9Sstevel@tonic-gate 		    opp->oprom_array, userbufsize) != 0) {
9567c478bd9Sstevel@tonic-gate 			error = EFAULT;
9577c478bd9Sstevel@tonic-gate 			break;
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		/*
9617c478bd9Sstevel@tonic-gate 		 * The property name is the first string, value second
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		namebuflen = strlen(opp->oprom_array);
9647c478bd9Sstevel@tonic-gate 		valbuf = opp->oprom_array + namebuflen + 1;
9657c478bd9Sstevel@tonic-gate 		valbuflen = strlen(valbuf);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		if (cmd == OPROMSETOPT) {
9687c478bd9Sstevel@tonic-gate 			valsize = valbuflen + 1;  /* +1 for the '\0' */
9697c478bd9Sstevel@tonic-gate 		} else {
9707c478bd9Sstevel@tonic-gate 			if ((namebuflen + 1 + valbuflen + 1) > userbufsize) {
9717c478bd9Sstevel@tonic-gate 				error = EINVAL;
9727c478bd9Sstevel@tonic-gate 				break;
9737c478bd9Sstevel@tonic-gate 			}
9747c478bd9Sstevel@tonic-gate 			valsize = (opp->oprom_array + userbufsize) - valbuf;
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		/*
9787c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is not an option, but it is a property.
9797c478bd9Sstevel@tonic-gate 		 */
9807c478bd9Sstevel@tonic-gate 		if (strcmp(opp->oprom_array, "name") == 0)
9817c478bd9Sstevel@tonic-gate 			error = EINVAL;
9827c478bd9Sstevel@tonic-gate 		else if (prom_setprop(node_id, opp->oprom_array,
9837c478bd9Sstevel@tonic-gate 		    valbuf, valsize) < 0)
9847c478bd9Sstevel@tonic-gate 			error = EINVAL;
9857c478bd9Sstevel@tonic-gate 
986a204de77Scth 		break;
987a204de77Scth 	}
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	case OPROMREADY64: {
9907c478bd9Sstevel@tonic-gate 		struct openprom_opr64 *opr =
9917c478bd9Sstevel@tonic-gate 		    (struct openprom_opr64 *)opp->oprom_array;
9927c478bd9Sstevel@tonic-gate 		int i;
993fa9e4066Sahrens 		pnode_t id;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		if (userbufsize < sizeof (*opr)) {
9967c478bd9Sstevel@tonic-gate 			error = EINVAL;
9977c478bd9Sstevel@tonic-gate 			break;
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		valsize = userbufsize -
10017c478bd9Sstevel@tonic-gate 		    offsetof(struct openprom_opr64, message);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		i = prom_version_check(opr->message, valsize, &id);
10047c478bd9Sstevel@tonic-gate 		opr->return_code = i;
10057c478bd9Sstevel@tonic-gate 		opr->nodeid = (int)id;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 		valsize = offsetof(struct openprom_opr64, message);
10087c478bd9Sstevel@tonic-gate 		valsize += strlen(opr->message) + 1;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
10127c478bd9Sstevel@tonic-gate 		 */
10137c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
10147c478bd9Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
10157c478bd9Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
10167c478bd9Sstevel@tonic-gate 			error = EFAULT;
10177c478bd9Sstevel@tonic-gate 		break;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	}	/* case OPROMREADY64 */
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	case WANBOOT_SETKEY: {
10227c478bd9Sstevel@tonic-gate 		struct wankeyio *wp;
10237c478bd9Sstevel@tonic-gate 		int reslen;
10247c478bd9Sstevel@tonic-gate 		int status;
10257c478bd9Sstevel@tonic-gate 		int rv;
10267c478bd9Sstevel@tonic-gate 		int i;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		/*
10297c478bd9Sstevel@tonic-gate 		 * The argument is a struct wankeyio.  Validate it as best
10307c478bd9Sstevel@tonic-gate 		 * we can.
10317c478bd9Sstevel@tonic-gate 		 */
10327c478bd9Sstevel@tonic-gate 		if (userbufsize != (sizeof (struct wankeyio))) {
10337c478bd9Sstevel@tonic-gate 			error = EINVAL;
10347c478bd9Sstevel@tonic-gate 			break;
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
10377c478bd9Sstevel@tonic-gate 		    opp->oprom_array, sizeof (struct wankeyio)) != 0) {
10387c478bd9Sstevel@tonic-gate 			error = EFAULT;
10397c478bd9Sstevel@tonic-gate 			break;
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 		wp = (struct wankeyio *)opp->oprom_array;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		/* check for key name and key size overflow */
10447c478bd9Sstevel@tonic-gate 		for (i = 0; i < WANBOOT_MAXKEYNAMELEN; i++)
10457c478bd9Sstevel@tonic-gate 			if (wp->wk_keyname[i] == '\0')
10467c478bd9Sstevel@tonic-gate 				break;
10477c478bd9Sstevel@tonic-gate 		if ((i == WANBOOT_MAXKEYNAMELEN) ||
10487c478bd9Sstevel@tonic-gate 		    (wp->wk_keysize > WANBOOT_MAXKEYLEN)) {
10497c478bd9Sstevel@tonic-gate 			error = EINVAL;
10507c478bd9Sstevel@tonic-gate 			break;
10517c478bd9Sstevel@tonic-gate 		}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		rv = prom_set_security_key(wp->wk_keyname, wp->wk_u.key,
10547c478bd9Sstevel@tonic-gate 		    wp->wk_keysize, &reslen, &status);
10557c478bd9Sstevel@tonic-gate 		if (rv)
10567c478bd9Sstevel@tonic-gate 			error = EIO;
10577c478bd9Sstevel@tonic-gate 		else
10587c478bd9Sstevel@tonic-gate 			switch (status) {
10597c478bd9Sstevel@tonic-gate 				case 0:
10607c478bd9Sstevel@tonic-gate 					error = 0;
10617c478bd9Sstevel@tonic-gate 					break;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 				case -2:	/* out of key storage space */
10647c478bd9Sstevel@tonic-gate 					error = ENOSPC;
10657c478bd9Sstevel@tonic-gate 					break;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 				case -3:	/* key name or value too long */
10687c478bd9Sstevel@tonic-gate 					error = EINVAL;
10697c478bd9Sstevel@tonic-gate 					break;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 				case -4:	/* can't delete:  no such key */
10727c478bd9Sstevel@tonic-gate 					error = ENOENT;
10737c478bd9Sstevel@tonic-gate 					break;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 				case -1:	/* unspecified error */
10767c478bd9Sstevel@tonic-gate 				default:	/* this should not happen */
10777c478bd9Sstevel@tonic-gate 					error = EIO;
10787c478bd9Sstevel@tonic-gate 					break;
10797c478bd9Sstevel@tonic-gate 			}
10807c478bd9Sstevel@tonic-gate 		break;
10817c478bd9Sstevel@tonic-gate 	}	/* case WANBOOT_SETKEY */
10827c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
10837c478bd9Sstevel@tonic-gate 	}	/* switch (cmd)	*/
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	kmem_free(opp, userbufsize + sizeof (uint_t) + 1);
10867c478bd9Sstevel@tonic-gate 	return (error);
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10907c478bd9Sstevel@tonic-gate static int
10917c478bd9Sstevel@tonic-gate opromioctl(dev_t dev, int cmd, intptr_t arg, int mode,
10927c478bd9Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
10957c478bd9Sstevel@tonic-gate 	struct opromioctl_args arg_block;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (getminor(dev) >= MAX_OPENS)
10987c478bd9Sstevel@tonic-gate 		return (ENXIO);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
11017c478bd9Sstevel@tonic-gate 	ASSERT(st->already_open);
11027c478bd9Sstevel@tonic-gate 	arg_block.st = st;
11037c478bd9Sstevel@tonic-gate 	arg_block.cmd = cmd;
11047c478bd9Sstevel@tonic-gate 	arg_block.arg = arg;
11057c478bd9Sstevel@tonic-gate 	arg_block.mode = mode;
11067c478bd9Sstevel@tonic-gate 	return (prom_tree_access(opromioctl_cb, &arg_block, &st->tree_gen));
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Copyin string and verify the actual string length is less than maxsize
11117c478bd9Sstevel@tonic-gate  * specified by the caller.
11127c478bd9Sstevel@tonic-gate  *
11137c478bd9Sstevel@tonic-gate  * Currently, maxsize is either OBP_MAXPROPNAME for property names
11147c478bd9Sstevel@tonic-gate  * or MAXPATHLEN for device path names. userbufsize is specified
11157c478bd9Sstevel@tonic-gate  * by the userland caller.
11167c478bd9Sstevel@tonic-gate  */
11177c478bd9Sstevel@tonic-gate static int
11187c478bd9Sstevel@tonic-gate oprom_copyinstr(intptr_t arg, char *buf, size_t bufsize, size_t maxsize)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	int error;
11217c478bd9Sstevel@tonic-gate 	size_t actual_len;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if ((error = copyinstr(((caddr_t)arg + sizeof (uint_t)),
11247c478bd9Sstevel@tonic-gate 	    buf, bufsize, &actual_len)) != 0) {
11257c478bd9Sstevel@tonic-gate 		return (error);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	if ((actual_len == 0) || (actual_len > maxsize)) {
11287c478bd9Sstevel@tonic-gate 		return (EINVAL);
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	return (0);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*
1135fa9e4066Sahrens  * Check pnode_t passed in from userland
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate static int
1138fa9e4066Sahrens oprom_checknodeid(pnode_t node_id, pnode_t current_id)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	int depth;
1141fa9e4066Sahrens 	pnode_t id[OBP_STACKDEPTH];
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * optimized path
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	if (node_id == 0) {
11477c478bd9Sstevel@tonic-gate 		return (1);
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 	if (node_id == OBP_BADNODE) {
11507c478bd9Sstevel@tonic-gate 		return (0);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	if ((current_id != OBP_BADNODE) && ((node_id == current_id) ||
11537c478bd9Sstevel@tonic-gate 	    (node_id == prom_nextnode(current_id)) ||
11547c478bd9Sstevel@tonic-gate 	    (node_id == prom_childnode(current_id)))) {
11557c478bd9Sstevel@tonic-gate 		return (1);
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/*
11597c478bd9Sstevel@tonic-gate 	 * long path: walk from root till we find node_id
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	depth = 1;
1162fa9e4066Sahrens 	id[0] = prom_nextnode((pnode_t)0);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	while (depth) {
11657c478bd9Sstevel@tonic-gate 		if (id[depth - 1] == node_id)
11667c478bd9Sstevel@tonic-gate 			return (1);	/* node_id found */
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if (id[depth] = prom_childnode(id[depth - 1])) {
11697c478bd9Sstevel@tonic-gate 			depth++;
11707c478bd9Sstevel@tonic-gate 			continue;
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		while (depth &&
11747c478bd9Sstevel@tonic-gate 		    ((id[depth - 1] = prom_nextnode(id[depth - 1])) == 0))
11757c478bd9Sstevel@tonic-gate 			depth--;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	return (0);	/* node_id not found */
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate static int
11817c478bd9Sstevel@tonic-gate oprom_copytree(struct oprom_state *st, uint_t flag)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	ASSERT(st->snapshot == NULL && st->size == 0);
11847c478bd9Sstevel@tonic-gate 	return (oprom_copynode(
11857c478bd9Sstevel@tonic-gate 	    prom_nextnode(0), flag, &st->snapshot, &st->size));
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate static int
11897c478bd9Sstevel@tonic-gate oprom_snapshot(struct oprom_state *st, intptr_t arg)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	uint_t flag;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_SNAP) == -1)
11947c478bd9Sstevel@tonic-gate 		return (EBUSY);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/* copyin flag and create snapshot */
11977c478bd9Sstevel@tonic-gate 	if ((copyin((void *)arg, &flag, sizeof (uint_t)) != 0) ||
11987c478bd9Sstevel@tonic-gate 	    (oprom_copytree(st, flag) != 0)) {
11997c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
12007c478bd9Sstevel@tonic-gate 		return (EFAULT);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	/* copyout the size of the snapshot */
12057c478bd9Sstevel@tonic-gate 	flag = (uint_t)st->size;
12067c478bd9Sstevel@tonic-gate 	if (copyout(&flag, (void *)arg, sizeof (uint_t)) != 0) {
12077c478bd9Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
12087c478bd9Sstevel@tonic-gate 		st->snapshot = NULL;
12097c478bd9Sstevel@tonic-gate 		st->size = 0;
12107c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
12117c478bd9Sstevel@tonic-gate 		return (EFAULT);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_DONE);
12157c478bd9Sstevel@tonic-gate 	return (0);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate static int
12197c478bd9Sstevel@tonic-gate oprom_copyout(struct oprom_state *st, intptr_t arg)
12207c478bd9Sstevel@tonic-gate {
12217c478bd9Sstevel@tonic-gate 	int error = 0;
12227c478bd9Sstevel@tonic-gate 	uint_t size;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_COPY) == -1)
12257c478bd9Sstevel@tonic-gate 		return (EBUSY);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	/* copyin size and copyout snapshot */
12287c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &size, sizeof (uint_t)) != 0)
12297c478bd9Sstevel@tonic-gate 		error = EFAULT;
12307c478bd9Sstevel@tonic-gate 	else if (size < st->size)
12317c478bd9Sstevel@tonic-gate 		error = EINVAL;
12327c478bd9Sstevel@tonic-gate 	else if (copyout(st->snapshot, (void *)arg, st->size) != 0)
12337c478bd9Sstevel@tonic-gate 		error = EFAULT;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	if (error) {
12367c478bd9Sstevel@tonic-gate 		/*
12377c478bd9Sstevel@tonic-gate 		 * on error keep the snapshot until a successful
12387c478bd9Sstevel@tonic-gate 		 * copyout or when the driver is closed.
12397c478bd9Sstevel@tonic-gate 		 */
12407c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_DONE);
12417c478bd9Sstevel@tonic-gate 		return (error);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	kmem_free(st->snapshot, st->size);
12457c478bd9Sstevel@tonic-gate 	st->snapshot = NULL;
12467c478bd9Sstevel@tonic-gate 	st->size = 0;
12477c478bd9Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_IDLE);
12487c478bd9Sstevel@tonic-gate 	return (0);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * Copy all properties of nodeid into a single packed nvlist
12537c478bd9Sstevel@tonic-gate  */
12547c478bd9Sstevel@tonic-gate static int
1255fa9e4066Sahrens oprom_copyprop(pnode_t nodeid, uint_t flag, nvlist_t *nvl)
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	int proplen;
12587c478bd9Sstevel@tonic-gate 	char *propname, *propval, *buf1, *buf2;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	ASSERT(nvl != NULL);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
12637c478bd9Sstevel@tonic-gate 	 * non verbose mode, get the "name" property only
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	if (flag == 0) {
12667c478bd9Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, "name");
12677c478bd9Sstevel@tonic-gate 		if (proplen <= 0) {
12687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
12697c478bd9Sstevel@tonic-gate 			    "failed to get the name of openprom node 0x%x",
12707c478bd9Sstevel@tonic-gate 			    nodeid);
12717c478bd9Sstevel@tonic-gate 			(void) nvlist_add_string(nvl, "name", "");
12727c478bd9Sstevel@tonic-gate 			return (0);
12737c478bd9Sstevel@tonic-gate 		}
12747c478bd9Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
12757c478bd9Sstevel@tonic-gate 		(void) prom_getprop(nodeid, "name", propval);
12767c478bd9Sstevel@tonic-gate 		(void) nvlist_add_string(nvl, "name", propval);
12777c478bd9Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
12787c478bd9Sstevel@tonic-gate 		return (0);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	/*
12827c478bd9Sstevel@tonic-gate 	 * Ask for first property by passing a NULL string
12837c478bd9Sstevel@tonic-gate 	 */
12847c478bd9Sstevel@tonic-gate 	buf1 = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
12857c478bd9Sstevel@tonic-gate 	buf2 = kmem_zalloc(OBP_MAXPROPNAME, KM_SLEEP);
12867c478bd9Sstevel@tonic-gate 	buf1[0] = '\0';
12877c478bd9Sstevel@tonic-gate 	while (propname = (char *)prom_nextprop(nodeid, buf1, buf2)) {
12887c478bd9Sstevel@tonic-gate 		if (strlen(propname) == 0)
12897c478bd9Sstevel@tonic-gate 			break;		/* end of prop list */
12907c478bd9Sstevel@tonic-gate 		(void) strcpy(buf1, propname);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, propname);
12937c478bd9Sstevel@tonic-gate 		if (proplen == 0) {
12947c478bd9Sstevel@tonic-gate 			/* boolean property */
12957c478bd9Sstevel@tonic-gate 			(void) nvlist_add_boolean(nvl, propname);
12967c478bd9Sstevel@tonic-gate 			continue;
12977c478bd9Sstevel@tonic-gate 		}
12987c478bd9Sstevel@tonic-gate 		/* add 1 for null termination in case of a string */
12997c478bd9Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
13007c478bd9Sstevel@tonic-gate 		(void) prom_getprop(nodeid, propname, propval);
13017c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, propname,
13027c478bd9Sstevel@tonic-gate 		    (uchar_t *)propval, proplen + 1);
13037c478bd9Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
13047c478bd9Sstevel@tonic-gate 		bzero(buf2, OBP_MAXPROPNAME);
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	kmem_free(buf1, OBP_MAXPROPNAME);
13087c478bd9Sstevel@tonic-gate 	kmem_free(buf2, OBP_MAXPROPNAME);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	return (0);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /*
13147c478bd9Sstevel@tonic-gate  * Copy all children and descendents into a a packed nvlist
13157c478bd9Sstevel@tonic-gate  */
13167c478bd9Sstevel@tonic-gate static int
1317fa9e4066Sahrens oprom_copychild(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
1320fa9e4066Sahrens 	pnode_t child = prom_childnode(nodeid);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	if (child == 0)
13237c478bd9Sstevel@tonic-gate 		return (0);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, 0, KM_SLEEP);
13267c478bd9Sstevel@tonic-gate 	while (child != 0) {
13277c478bd9Sstevel@tonic-gate 		char *nodebuf = NULL;
13287c478bd9Sstevel@tonic-gate 		size_t nodesize = 0;
13297c478bd9Sstevel@tonic-gate 		if (oprom_copynode(child, flag, &nodebuf, &nodesize)) {
13307c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
13317c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to copy nodeid 0x%x", child);
13327c478bd9Sstevel@tonic-gate 			return (-1);
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "node",
13357c478bd9Sstevel@tonic-gate 		    (uchar_t *)nodebuf, nodesize);
13367c478bd9Sstevel@tonic-gate 		kmem_free(nodebuf, nodesize);
13377c478bd9Sstevel@tonic-gate 		child = prom_nextnode(child);
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	(void) nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
13417c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
13427c478bd9Sstevel@tonic-gate 	return (0);
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate /*
13467c478bd9Sstevel@tonic-gate  * Copy a node into a packed nvlist
13477c478bd9Sstevel@tonic-gate  */
13487c478bd9Sstevel@tonic-gate static int
1349fa9e4066Sahrens oprom_copynode(pnode_t nodeid, uint_t flag, char **buf, size_t *size)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	int error = 0;
13527c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
13537c478bd9Sstevel@tonic-gate 	char *childlist = NULL;
13547c478bd9Sstevel@tonic-gate 	size_t childsize = 0;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
13577c478bd9Sstevel@tonic-gate 	ASSERT(nvl != NULL);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/* @nodeid -- @ is not a legal char in a 1275 property name */
13607c478bd9Sstevel@tonic-gate 	(void) nvlist_add_int32(nvl, "@nodeid", (int32_t)nodeid);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	/* properties */
13637c478bd9Sstevel@tonic-gate 	if (error = oprom_copyprop(nodeid, flag, nvl))
13647c478bd9Sstevel@tonic-gate 		goto fail;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/* children */
13677c478bd9Sstevel@tonic-gate 	error = oprom_copychild(nodeid, flag, &childlist, &childsize);
13687c478bd9Sstevel@tonic-gate 	if (error != 0)
13697c478bd9Sstevel@tonic-gate 		goto fail;
13707c478bd9Sstevel@tonic-gate 	if (childlist != NULL) {
13717c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "@child",
13727c478bd9Sstevel@tonic-gate 		    (uchar_t *)childlist, (uint_t)childsize);
13737c478bd9Sstevel@tonic-gate 		kmem_free(childlist, childsize);
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/* pack into contiguous buffer */
13777c478bd9Sstevel@tonic-gate 	error = nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate fail:
13807c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
13817c478bd9Sstevel@tonic-gate 	return (error);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate  * The driver is stateful across OPROMSNAPSHOT and OPROMCOPYOUT.
13867c478bd9Sstevel@tonic-gate  * This function encapsulates the state machine:
13877c478bd9Sstevel@tonic-gate  *
13887c478bd9Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
13897c478bd9Sstevel@tonic-gate  *	|		SNAPSHOT		COPYOUT	 |
13907c478bd9Sstevel@tonic-gate  *	--------------------------------------------------
13917c478bd9Sstevel@tonic-gate  *
13927c478bd9Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
13937c478bd9Sstevel@tonic-gate  */
13947c478bd9Sstevel@tonic-gate static int
13957c478bd9Sstevel@tonic-gate oprom_setstate(struct oprom_state *st, int16_t new_state)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	int ret = 0;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
14007c478bd9Sstevel@tonic-gate 	switch (new_state) {
14017c478bd9Sstevel@tonic-gate 	case IOC_IDLE:
14027c478bd9Sstevel@tonic-gate 	case IOC_DONE:
14037c478bd9Sstevel@tonic-gate 		break;
14047c478bd9Sstevel@tonic-gate 	case IOC_SNAP:
14057c478bd9Sstevel@tonic-gate 		if (st->ioc_state != IOC_IDLE)
14067c478bd9Sstevel@tonic-gate 			ret = -1;
14077c478bd9Sstevel@tonic-gate 		break;
14087c478bd9Sstevel@tonic-gate 	case IOC_COPY:
14097c478bd9Sstevel@tonic-gate 		if (st->ioc_state != IOC_DONE)
14107c478bd9Sstevel@tonic-gate 			ret = -1;
14117c478bd9Sstevel@tonic-gate 		break;
14127c478bd9Sstevel@tonic-gate 	default:
14137c478bd9Sstevel@tonic-gate 		ret = -1;
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (ret == 0)
14177c478bd9Sstevel@tonic-gate 		st->ioc_state = new_state;
14187c478bd9Sstevel@tonic-gate 	else
14197c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
14207c478bd9Sstevel@tonic-gate 		    st->ioc_state, new_state);
14217c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
14227c478bd9Sstevel@tonic-gate 	return (ret);
14237c478bd9Sstevel@tonic-gate }
1424