xref: /titanic_54/usr/src/uts/common/io/openprom.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4 */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Ported from 4.1.1_PSRA: "@(#)openprom.c 1.19 91/02/19 SMI";
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Porting notes:
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * OPROMU2P unsupported after SunOS 4.x.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Only one of these devices per system is allowed.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Openprom eeprom options/devinfo driver.
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>	/* offsetof */
58*7c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/wanboot_impl.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #define	MAX_OPENS	32	/* Up to this many simultaneous opens */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #define	IOC_IDLE	0	/* snapshot ioctl states */
65*7c478bd9Sstevel@tonic-gate #define	IOC_SNAP	1	/* snapshot in progress */
66*7c478bd9Sstevel@tonic-gate #define	IOC_DONE	2	/* snapshot done, but not copied out */
67*7c478bd9Sstevel@tonic-gate #define	IOC_COPY	3	/* copyout in progress */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate extern int plat_stdout_is_framebuffer(void);
70*7c478bd9Sstevel@tonic-gate extern int plat_stdin_is_keyboard(void);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * XXX	Make this dynamic.. or (better still) make the interface stateless
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate static struct oprom_state {
76*7c478bd9Sstevel@tonic-gate 	dnode_t	current_id;	/* node we're fetching props from */
77*7c478bd9Sstevel@tonic-gate 	int16_t	already_open;	/* if true, this instance is 'active' */
78*7c478bd9Sstevel@tonic-gate 	int16_t	ioc_state;	/* snapshot ioctl state */
79*7c478bd9Sstevel@tonic-gate 	char	*snapshot;	/* snapshot of all prom nodes */
80*7c478bd9Sstevel@tonic-gate 	size_t	size;		/* size of snapshot */
81*7c478bd9Sstevel@tonic-gate 	prom_generation_cookie_t tree_gen;
82*7c478bd9Sstevel@tonic-gate } oprom_state[MAX_OPENS];
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate static kmutex_t oprom_lock;	/* serialize instance assignment */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static int opromopen(dev_t *, int, int, cred_t *);
87*7c478bd9Sstevel@tonic-gate static int opromioctl(dev_t, int, intptr_t, int, cred_t *, int *);
88*7c478bd9Sstevel@tonic-gate static int opromclose(dev_t, int, int, cred_t *);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate static int opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
91*7c478bd9Sstevel@tonic-gate 		void **result);
92*7c478bd9Sstevel@tonic-gate static int opattach(dev_info_t *, ddi_attach_cmd_t cmd);
93*7c478bd9Sstevel@tonic-gate static int opdetach(dev_info_t *, ddi_detach_cmd_t cmd);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /* help functions */
96*7c478bd9Sstevel@tonic-gate static int oprom_checknodeid(dnode_t, dnode_t);
97*7c478bd9Sstevel@tonic-gate static int oprom_copyinstr(intptr_t, char *, size_t, size_t);
98*7c478bd9Sstevel@tonic-gate static int oprom_copynode(dnode_t, uint_t, char **, size_t *);
99*7c478bd9Sstevel@tonic-gate static int oprom_snapshot(struct oprom_state *, intptr_t);
100*7c478bd9Sstevel@tonic-gate static int oprom_copyout(struct oprom_state *, intptr_t);
101*7c478bd9Sstevel@tonic-gate static int oprom_setstate(struct oprom_state *, int16_t);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate static struct cb_ops openeepr_cb_ops = {
104*7c478bd9Sstevel@tonic-gate 	opromopen,		/* open */
105*7c478bd9Sstevel@tonic-gate 	opromclose,		/* close */
106*7c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
107*7c478bd9Sstevel@tonic-gate 	nodev,			/* print */
108*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
109*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
110*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
111*7c478bd9Sstevel@tonic-gate 	opromioctl,		/* ioctl */
112*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
113*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
114*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
115*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
116*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
117*7c478bd9Sstevel@tonic-gate 	NULL,			/* streamtab  */
118*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
119*7c478bd9Sstevel@tonic-gate };
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate static struct dev_ops openeepr_ops = {
122*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
123*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
124*7c478bd9Sstevel@tonic-gate 	opinfo,			/* info */
125*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
126*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
127*7c478bd9Sstevel@tonic-gate 	opattach,		/* attach */
128*7c478bd9Sstevel@tonic-gate 	opdetach,		/* detach */
129*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
130*7c478bd9Sstevel@tonic-gate 	&openeepr_cb_ops,	/* driver operations */
131*7c478bd9Sstevel@tonic-gate 	NULL			/* bus operations */
132*7c478bd9Sstevel@tonic-gate };
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
138*7c478bd9Sstevel@tonic-gate 	&mod_driverops,
139*7c478bd9Sstevel@tonic-gate 	"OPENPROM/NVRAM Driver v%I%",
140*7c478bd9Sstevel@tonic-gate 	&openeepr_ops
141*7c478bd9Sstevel@tonic-gate };
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
144*7c478bd9Sstevel@tonic-gate 	MODREV_1,
145*7c478bd9Sstevel@tonic-gate 	&modldrv,
146*7c478bd9Sstevel@tonic-gate 	NULL
147*7c478bd9Sstevel@tonic-gate };
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate int
150*7c478bd9Sstevel@tonic-gate _init(void)
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 	int	error;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	mutex_init(&oprom_lock, NULL, MUTEX_DRIVER, NULL);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	error = mod_install(&modlinkage);
157*7c478bd9Sstevel@tonic-gate 	if (error != 0) {
158*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&oprom_lock);
159*7c478bd9Sstevel@tonic-gate 		return (error);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	return (0);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate int
166*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate int
172*7c478bd9Sstevel@tonic-gate _fini(void)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	int	error;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	error = mod_remove(&modlinkage);
177*7c478bd9Sstevel@tonic-gate 	if (error != 0)
178*7c478bd9Sstevel@tonic-gate 		return (error);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&oprom_lock);
181*7c478bd9Sstevel@tonic-gate 	return (0);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate static dev_info_t *opdip;
185*7c478bd9Sstevel@tonic-gate static dnode_t options_nodeid;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
188*7c478bd9Sstevel@tonic-gate static int
189*7c478bd9Sstevel@tonic-gate opinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	int error = DDI_FAILURE;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
194*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
195*7c478bd9Sstevel@tonic-gate 		*result = (void *)opdip;
196*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
197*7c478bd9Sstevel@tonic-gate 		break;
198*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
199*7c478bd9Sstevel@tonic-gate 		/* All dev_t's map to the same, single instance */
200*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
201*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
202*7c478bd9Sstevel@tonic-gate 		break;
203*7c478bd9Sstevel@tonic-gate 	default:
204*7c478bd9Sstevel@tonic-gate 		break;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	return (error);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate static int
211*7c478bd9Sstevel@tonic-gate opattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
216*7c478bd9Sstevel@tonic-gate 		if (prom_is_openprom()) {
217*7c478bd9Sstevel@tonic-gate 			options_nodeid = prom_optionsnode();
218*7c478bd9Sstevel@tonic-gate 		} else {
219*7c478bd9Sstevel@tonic-gate 			options_nodeid = OBP_BADNODE;
220*7c478bd9Sstevel@tonic-gate 		}
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		opdip = dip;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "openprom", S_IFCHR,
225*7c478bd9Sstevel@tonic-gate 		    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
226*7c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	default:
232*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate static int
237*7c478bd9Sstevel@tonic-gate opdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
240*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
243*7c478bd9Sstevel@tonic-gate 	opdip = NULL;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate  * Allow multiple opens by tweaking the dev_t such that it looks like each
250*7c478bd9Sstevel@tonic-gate  * open is getting a different minor device.  Each minor gets a separate
251*7c478bd9Sstevel@tonic-gate  * entry in the oprom_state[] table.
252*7c478bd9Sstevel@tonic-gate  */
253*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
254*7c478bd9Sstevel@tonic-gate static int
255*7c478bd9Sstevel@tonic-gate opromopen(dev_t *devp, int flag, int otyp, cred_t *credp)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	int m;
258*7c478bd9Sstevel@tonic-gate 	struct oprom_state *st = oprom_state;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	if (getminor(*devp) != 0)
261*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
264*7c478bd9Sstevel@tonic-gate 	for (m = 0; m < MAX_OPENS; m++)
265*7c478bd9Sstevel@tonic-gate 		if (st->already_open)
266*7c478bd9Sstevel@tonic-gate 			st++;
267*7c478bd9Sstevel@tonic-gate 		else {
268*7c478bd9Sstevel@tonic-gate 			st->already_open = 1;
269*7c478bd9Sstevel@tonic-gate 			/*
270*7c478bd9Sstevel@tonic-gate 			 * It's ours.
271*7c478bd9Sstevel@tonic-gate 			 */
272*7c478bd9Sstevel@tonic-gate 			st->current_id = (dnode_t)0;
273*7c478bd9Sstevel@tonic-gate 			ASSERT(st->snapshot == NULL && st->size == 0);
274*7c478bd9Sstevel@tonic-gate 			ASSERT(st->ioc_state == IOC_IDLE);
275*7c478bd9Sstevel@tonic-gate 			break;
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	if (m == MAX_OPENS)  {
280*7c478bd9Sstevel@tonic-gate 		/*
281*7c478bd9Sstevel@tonic-gate 		 * "Thank you for calling, but all our lines are
282*7c478bd9Sstevel@tonic-gate 		 * busy at the moment.."
283*7c478bd9Sstevel@tonic-gate 		 *
284*7c478bd9Sstevel@tonic-gate 		 * We could get sophisticated here, and go into a
285*7c478bd9Sstevel@tonic-gate 		 * sleep-retry loop .. but hey, I just can't see
286*7c478bd9Sstevel@tonic-gate 		 * that many processes sitting in this driver.
287*7c478bd9Sstevel@tonic-gate 		 *
288*7c478bd9Sstevel@tonic-gate 		 * (And if it does become possible, then we should
289*7c478bd9Sstevel@tonic-gate 		 * change the interface so that the 'state' is held
290*7c478bd9Sstevel@tonic-gate 		 * external to the driver)
291*7c478bd9Sstevel@tonic-gate 		 */
292*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), (minor_t)m);
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	return (0);
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
301*7c478bd9Sstevel@tonic-gate static int
302*7c478bd9Sstevel@tonic-gate opromclose(dev_t dev, int flag, int otype, cred_t *cred_p)
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
307*7c478bd9Sstevel@tonic-gate 	ASSERT(getminor(dev) < MAX_OPENS && st->already_open != 0);
308*7c478bd9Sstevel@tonic-gate 	if (st->snapshot) {
309*7c478bd9Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
310*7c478bd9Sstevel@tonic-gate 		st->snapshot = NULL;
311*7c478bd9Sstevel@tonic-gate 		st->size = 0;
312*7c478bd9Sstevel@tonic-gate 		st->ioc_state = IOC_IDLE;
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
315*7c478bd9Sstevel@tonic-gate 	st->already_open = 0;
316*7c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	return (0);
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate struct opromioctl_args {
322*7c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
323*7c478bd9Sstevel@tonic-gate 	int cmd;
324*7c478bd9Sstevel@tonic-gate 	intptr_t arg;
325*7c478bd9Sstevel@tonic-gate 	int mode;
326*7c478bd9Sstevel@tonic-gate };
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
329*7c478bd9Sstevel@tonic-gate static int
330*7c478bd9Sstevel@tonic-gate opromioctl_cb(void *avp, int has_changed)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	struct opromioctl_args *argp = avp;
333*7c478bd9Sstevel@tonic-gate 	int cmd;
334*7c478bd9Sstevel@tonic-gate 	intptr_t arg;
335*7c478bd9Sstevel@tonic-gate 	int mode;
336*7c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
337*7c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
338*7c478bd9Sstevel@tonic-gate 	int valsize;
339*7c478bd9Sstevel@tonic-gate 	char *valbuf;
340*7c478bd9Sstevel@tonic-gate 	int error = 0;
341*7c478bd9Sstevel@tonic-gate 	uint_t userbufsize;
342*7c478bd9Sstevel@tonic-gate 	dnode_t node_id;
343*7c478bd9Sstevel@tonic-gate 	char propname[OBP_MAXPROPNAME];
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	st = argp->st;
346*7c478bd9Sstevel@tonic-gate 	cmd = argp->cmd;
347*7c478bd9Sstevel@tonic-gate 	arg = argp->arg;
348*7c478bd9Sstevel@tonic-gate 	mode = argp->mode;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	if (has_changed) {
351*7c478bd9Sstevel@tonic-gate 		/*
352*7c478bd9Sstevel@tonic-gate 		 * The prom tree has changed since we last used current_id,
353*7c478bd9Sstevel@tonic-gate 		 * so we need to check it.
354*7c478bd9Sstevel@tonic-gate 		 */
355*7c478bd9Sstevel@tonic-gate 		if ((st->current_id != OBP_NONODE) &&
356*7c478bd9Sstevel@tonic-gate 		    (st->current_id != OBP_BADNODE)) {
357*7c478bd9Sstevel@tonic-gate 			if (oprom_checknodeid(st->current_id, OBP_NONODE) == 0)
358*7c478bd9Sstevel@tonic-gate 				st->current_id = OBP_BADNODE;
359*7c478bd9Sstevel@tonic-gate 		}
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	/*
363*7c478bd9Sstevel@tonic-gate 	 * Check permissions
364*7c478bd9Sstevel@tonic-gate 	 * and weed out unsupported commands on x86 platform
365*7c478bd9Sstevel@tonic-gate 	 */
366*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
367*7c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
368*7c478bd9Sstevel@tonic-gate 	case OPROMLISTKEYSLEN:
369*7c478bd9Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
370*7c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
371*7c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
372*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
373*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
374*7c478bd9Sstevel@tonic-gate 		    error = EFAULT;
375*7c478bd9Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
376*7c478bd9Sstevel@tonic-gate 		break;
377*7c478bd9Sstevel@tonic-gate 	case OPROMLISTKEYS:
378*7c478bd9Sstevel@tonic-gate 		valsize = prom_asr_list_keys_len();
379*7c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
380*7c478bd9Sstevel@tonic-gate 		    return (EFAULT);
381*7c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize)
382*7c478bd9Sstevel@tonic-gate 		    return (EINVAL);
383*7c478bd9Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
384*7c478bd9Sstevel@tonic-gate 		if (prom_asr_list_keys((caddr_t)valbuf) == -1) {
385*7c478bd9Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
386*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
389*7c478bd9Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
390*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
391*7c478bd9Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
392*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
393*7c478bd9Sstevel@tonic-gate 		    error = EFAULT;
394*7c478bd9Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
395*7c478bd9Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
396*7c478bd9Sstevel@tonic-gate 		break;
397*7c478bd9Sstevel@tonic-gate 	case OPROMEXPORT:
398*7c478bd9Sstevel@tonic-gate 		valsize = prom_asr_export_len();
399*7c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
400*7c478bd9Sstevel@tonic-gate 		    return (EFAULT);
401*7c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize)
402*7c478bd9Sstevel@tonic-gate 		    return (EINVAL);
403*7c478bd9Sstevel@tonic-gate 		valbuf = (char *)kmem_zalloc(valsize + 1, KM_SLEEP);
404*7c478bd9Sstevel@tonic-gate 		if (prom_asr_export((caddr_t)valbuf) == -1) {
405*7c478bd9Sstevel@tonic-gate 			kmem_free(valbuf, valsize + 1);
406*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
407*7c478bd9Sstevel@tonic-gate 		}
408*7c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
409*7c478bd9Sstevel@tonic-gate 		    valsize + sizeof (uint_t) + 1, KM_SLEEP);
410*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
411*7c478bd9Sstevel@tonic-gate 		bcopy(valbuf, opp->oprom_array, valsize);
412*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
413*7c478bd9Sstevel@tonic-gate 		    error = EFAULT;
414*7c478bd9Sstevel@tonic-gate 		kmem_free(valbuf, valsize + 1);
415*7c478bd9Sstevel@tonic-gate 		kmem_free(opp, valsize + sizeof (uint_t) + 1);
416*7c478bd9Sstevel@tonic-gate 		break;
417*7c478bd9Sstevel@tonic-gate 	case OPROMEXPORTLEN:
418*7c478bd9Sstevel@tonic-gate 		valsize = prom_asr_export_len();
419*7c478bd9Sstevel@tonic-gate 		opp = (struct openpromio *)kmem_zalloc(
420*7c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + 1, KM_SLEEP);
421*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
422*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (sizeof (uint_t))) != 0)
423*7c478bd9Sstevel@tonic-gate 		    error = EFAULT;
424*7c478bd9Sstevel@tonic-gate 		kmem_free(opp, sizeof (uint_t) + 1);
425*7c478bd9Sstevel@tonic-gate 		break;
426*7c478bd9Sstevel@tonic-gate #endif
427*7c478bd9Sstevel@tonic-gate 	case OPROMGETOPT:
428*7c478bd9Sstevel@tonic-gate 	case OPROMNXTOPT:
429*7c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
430*7c478bd9Sstevel@tonic-gate 			return (EPERM);
431*7c478bd9Sstevel@tonic-gate 		}
432*7c478bd9Sstevel@tonic-gate 		node_id = options_nodeid;
433*7c478bd9Sstevel@tonic-gate 		break;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	case OPROMSETOPT:
436*7c478bd9Sstevel@tonic-gate 	case OPROMSETOPT2:
437*7c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
438*7c478bd9Sstevel@tonic-gate 		if (mode & FWRITE) {
439*7c478bd9Sstevel@tonic-gate 			node_id = options_nodeid;
440*7c478bd9Sstevel@tonic-gate 			break;
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate #endif /* !__i386 && !__amd64 */
443*7c478bd9Sstevel@tonic-gate 		return (EPERM);
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	case OPROMNEXT:
446*7c478bd9Sstevel@tonic-gate 	case OPROMCHILD:
447*7c478bd9Sstevel@tonic-gate 	case OPROMGETPROP:
448*7c478bd9Sstevel@tonic-gate 	case OPROMGETPROPLEN:
449*7c478bd9Sstevel@tonic-gate 	case OPROMNXTPROP:
450*7c478bd9Sstevel@tonic-gate 	case OPROMSETNODEID:
451*7c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
452*7c478bd9Sstevel@tonic-gate 			return (EPERM);
453*7c478bd9Sstevel@tonic-gate 		}
454*7c478bd9Sstevel@tonic-gate 		node_id = st->current_id;
455*7c478bd9Sstevel@tonic-gate 		break;
456*7c478bd9Sstevel@tonic-gate 	case OPROMCOPYOUT:
457*7c478bd9Sstevel@tonic-gate 		if (st->snapshot == NULL)
458*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
459*7c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
460*7c478bd9Sstevel@tonic-gate 	case OPROMSNAPSHOT:
461*7c478bd9Sstevel@tonic-gate 	case OPROMGETCONS:
462*7c478bd9Sstevel@tonic-gate 	case OPROMGETBOOTARGS:
463*7c478bd9Sstevel@tonic-gate 	case OPROMGETVERSION:
464*7c478bd9Sstevel@tonic-gate 	case OPROMPATH2DRV:
465*7c478bd9Sstevel@tonic-gate 	case OPROMPROM2DEVNAME:
466*7c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
467*7c478bd9Sstevel@tonic-gate 	case OPROMGETFBNAME:
468*7c478bd9Sstevel@tonic-gate 	case OPROMDEV2PROMNAME:
469*7c478bd9Sstevel@tonic-gate 	case OPROMREADY64:
470*7c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
471*7c478bd9Sstevel@tonic-gate 		if ((mode & FREAD) == 0) {
472*7c478bd9Sstevel@tonic-gate 			return (EPERM);
473*7c478bd9Sstevel@tonic-gate 		}
474*7c478bd9Sstevel@tonic-gate 		break;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
477*7c478bd9Sstevel@tonic-gate 	case WANBOOT_SETKEY:
478*7c478bd9Sstevel@tonic-gate 		if (!(mode & FWRITE))
479*7c478bd9Sstevel@tonic-gate 			return (EPERM);
480*7c478bd9Sstevel@tonic-gate 		break;
481*7c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !defined(__amd64) */
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	default:
484*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
485*7c478bd9Sstevel@tonic-gate 	}
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 	/*
488*7c478bd9Sstevel@tonic-gate 	 * Deal with SNAPSHOT and COPYOUT ioctls first
489*7c478bd9Sstevel@tonic-gate 	 */
490*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
491*7c478bd9Sstevel@tonic-gate 	case OPROMCOPYOUT:
492*7c478bd9Sstevel@tonic-gate 		return (oprom_copyout(st, arg));
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	case OPROMSNAPSHOT:
495*7c478bd9Sstevel@tonic-gate 		return (oprom_snapshot(st, arg));
496*7c478bd9Sstevel@tonic-gate 	}
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	/*
499*7c478bd9Sstevel@tonic-gate 	 * Copy in user argument length and allocation memory
500*7c478bd9Sstevel@tonic-gate 	 *
501*7c478bd9Sstevel@tonic-gate 	 * NB do not copyin the entire buffer we may not need
502*7c478bd9Sstevel@tonic-gate 	 *	to. userbufsize can be as big as 32 K.
503*7c478bd9Sstevel@tonic-gate 	 */
504*7c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &userbufsize, sizeof (uint_t)) != 0)
505*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	if (userbufsize == 0 || userbufsize > OPROMMAXPARAM)
508*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate 	opp = (struct openpromio *)kmem_zalloc(
511*7c478bd9Sstevel@tonic-gate 	    userbufsize + sizeof (uint_t) + 1, KM_SLEEP);
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	/*
514*7c478bd9Sstevel@tonic-gate 	 * Execute command
515*7c478bd9Sstevel@tonic-gate 	 */
516*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	case OPROMGETOPT:
519*7c478bd9Sstevel@tonic-gate 	case OPROMGETPROP:
520*7c478bd9Sstevel@tonic-gate 	case OPROMGETPROPLEN:
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
523*7c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
524*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
525*7c478bd9Sstevel@tonic-gate 			break;
526*7c478bd9Sstevel@tonic-gate 		}
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 		/*
529*7c478bd9Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
530*7c478bd9Sstevel@tonic-gate 		 */
531*7c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
532*7c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
533*7c478bd9Sstevel@tonic-gate 			break;
534*7c478bd9Sstevel@tonic-gate 		}
535*7c478bd9Sstevel@tonic-gate 		(void) strcpy(propname, opp->oprom_array);
536*7c478bd9Sstevel@tonic-gate 		valsize = prom_getproplen(node_id, propname);
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 		/*
539*7c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but not an option.
540*7c478bd9Sstevel@tonic-gate 		 */
541*7c478bd9Sstevel@tonic-gate 		if ((cmd == OPROMGETOPT) && (strcmp("name", propname) == 0))
542*7c478bd9Sstevel@tonic-gate 			valsize = -1;
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 		if (cmd == OPROMGETPROPLEN)  {
545*7c478bd9Sstevel@tonic-gate 			int proplen = valsize;
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 			if (userbufsize < sizeof (int)) {
548*7c478bd9Sstevel@tonic-gate 				error = EINVAL;
549*7c478bd9Sstevel@tonic-gate 				break;
550*7c478bd9Sstevel@tonic-gate 			}
551*7c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize = sizeof (int);
552*7c478bd9Sstevel@tonic-gate 			bcopy(&proplen, opp->oprom_array, valsize);
553*7c478bd9Sstevel@tonic-gate 		} else if (valsize > 0 && valsize <= userbufsize) {
554*7c478bd9Sstevel@tonic-gate 			bzero(opp->oprom_array, valsize + 1);
555*7c478bd9Sstevel@tonic-gate 			(void) prom_getprop(node_id, propname,
556*7c478bd9Sstevel@tonic-gate 			    opp->oprom_array);
557*7c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize;
558*7c478bd9Sstevel@tonic-gate 			if (valsize < userbufsize)
559*7c478bd9Sstevel@tonic-gate 				++valsize;	/* Forces NULL termination */
560*7c478bd9Sstevel@tonic-gate 						/* If space permits */
561*7c478bd9Sstevel@tonic-gate 		} else {
562*7c478bd9Sstevel@tonic-gate 			/*
563*7c478bd9Sstevel@tonic-gate 			 * XXX: There is no error code if the buf is too small.
564*7c478bd9Sstevel@tonic-gate 			 * which is consistent with the current behavior.
565*7c478bd9Sstevel@tonic-gate 			 *
566*7c478bd9Sstevel@tonic-gate 			 * NB: This clause also handles the non-error
567*7c478bd9Sstevel@tonic-gate 			 * zero length (boolean) property value case.
568*7c478bd9Sstevel@tonic-gate 			 */
569*7c478bd9Sstevel@tonic-gate 			opp->oprom_size = 0;
570*7c478bd9Sstevel@tonic-gate 			(void) strcpy(opp->oprom_array, "");
571*7c478bd9Sstevel@tonic-gate 			valsize = 1;
572*7c478bd9Sstevel@tonic-gate 		}
573*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, (valsize + sizeof (uint_t))) != 0)
574*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
575*7c478bd9Sstevel@tonic-gate 		break;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	case OPROMNXTOPT:
578*7c478bd9Sstevel@tonic-gate 	case OPROMNXTPROP:
579*7c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
580*7c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
581*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
582*7c478bd9Sstevel@tonic-gate 			break;
583*7c478bd9Sstevel@tonic-gate 		}
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 		/*
586*7c478bd9Sstevel@tonic-gate 		 * The argument, a NULL terminated string, is a prop name.
587*7c478bd9Sstevel@tonic-gate 		 */
588*7c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
589*7c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, OBP_MAXPROPNAME)) != 0) {
590*7c478bd9Sstevel@tonic-gate 			break;
591*7c478bd9Sstevel@tonic-gate 		}
592*7c478bd9Sstevel@tonic-gate 		valbuf = (char *)prom_nextprop(node_id, opp->oprom_array,
593*7c478bd9Sstevel@tonic-gate 		    propname);
594*7c478bd9Sstevel@tonic-gate 		valsize = strlen(valbuf);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 		/*
597*7c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is a property, but it's not an option.
598*7c478bd9Sstevel@tonic-gate 		 */
599*7c478bd9Sstevel@tonic-gate 		if ((cmd == OPROMNXTOPT) && valsize &&
600*7c478bd9Sstevel@tonic-gate 		    (strcmp(valbuf, "name") == 0)) {
601*7c478bd9Sstevel@tonic-gate 			valbuf = (char *)prom_nextprop(node_id, "name",
602*7c478bd9Sstevel@tonic-gate 			    propname);
603*7c478bd9Sstevel@tonic-gate 			valsize = strlen(valbuf);
604*7c478bd9Sstevel@tonic-gate 		}
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 		if (valsize == 0) {
607*7c478bd9Sstevel@tonic-gate 			opp->oprom_size = 0;
608*7c478bd9Sstevel@tonic-gate 		} else if (++valsize <= userbufsize) {
609*7c478bd9Sstevel@tonic-gate 			opp->oprom_size = valsize;
610*7c478bd9Sstevel@tonic-gate 			bzero((caddr_t)opp->oprom_array, (size_t)valsize);
611*7c478bd9Sstevel@tonic-gate 			bcopy((caddr_t)valbuf, (caddr_t)opp->oprom_array,
612*7c478bd9Sstevel@tonic-gate 			    (size_t)valsize);
613*7c478bd9Sstevel@tonic-gate 		}
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
616*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
617*7c478bd9Sstevel@tonic-gate 		break;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	case OPROMNEXT:
620*7c478bd9Sstevel@tonic-gate 	case OPROMCHILD:
621*7c478bd9Sstevel@tonic-gate 	case OPROMSETNODEID:
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate 		if (prom_is_openprom() == 0 ||
624*7c478bd9Sstevel@tonic-gate 		    userbufsize < sizeof (dnode_t)) {
625*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
626*7c478bd9Sstevel@tonic-gate 			break;
627*7c478bd9Sstevel@tonic-gate 		}
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 		/*
630*7c478bd9Sstevel@tonic-gate 		 * The argument is a phandle. (aka dnode_t)
631*7c478bd9Sstevel@tonic-gate 		 */
632*7c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
633*7c478bd9Sstevel@tonic-gate 		    opp->oprom_array, sizeof (dnode_t)) != 0) {
634*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
635*7c478bd9Sstevel@tonic-gate 			break;
636*7c478bd9Sstevel@tonic-gate 		}
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate 		/*
639*7c478bd9Sstevel@tonic-gate 		 * If dnode_t from userland is garbage, we
640*7c478bd9Sstevel@tonic-gate 		 * could confuse the PROM.
641*7c478bd9Sstevel@tonic-gate 		 */
642*7c478bd9Sstevel@tonic-gate 		node_id = *(dnode_t *)opp->oprom_array;
643*7c478bd9Sstevel@tonic-gate 		if (oprom_checknodeid(node_id, st->current_id) == 0) {
644*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!nodeid 0x%x not found",
645*7c478bd9Sstevel@tonic-gate 			    (int)node_id);
646*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
647*7c478bd9Sstevel@tonic-gate 			break;
648*7c478bd9Sstevel@tonic-gate 		}
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 		if (cmd == OPROMNEXT)
651*7c478bd9Sstevel@tonic-gate 			st->current_id = prom_nextnode(node_id);
652*7c478bd9Sstevel@tonic-gate 		else if (cmd == OPROMCHILD)
653*7c478bd9Sstevel@tonic-gate 			st->current_id = prom_childnode(node_id);
654*7c478bd9Sstevel@tonic-gate 		else {
655*7c478bd9Sstevel@tonic-gate 			/* OPROMSETNODEID */
656*7c478bd9Sstevel@tonic-gate 			st->current_id = node_id;
657*7c478bd9Sstevel@tonic-gate 			break;
658*7c478bd9Sstevel@tonic-gate 		}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = sizeof (dnode_t);
661*7c478bd9Sstevel@tonic-gate 		*(dnode_t *)opp->oprom_array = st->current_id;
662*7c478bd9Sstevel@tonic-gate 
663*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
664*7c478bd9Sstevel@tonic-gate 		    sizeof (dnode_t) + sizeof (uint_t)) != 0)
665*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
666*7c478bd9Sstevel@tonic-gate 		break;
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 	case OPROMGETCONS:
669*7c478bd9Sstevel@tonic-gate 		/*
670*7c478bd9Sstevel@tonic-gate 		 * Is openboot supported on this machine?
671*7c478bd9Sstevel@tonic-gate 		 * This ioctl used to return the console device,
672*7c478bd9Sstevel@tonic-gate 		 * information; this is now done via modctl()
673*7c478bd9Sstevel@tonic-gate 		 * in libdevinfo.
674*7c478bd9Sstevel@tonic-gate 		 */
675*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = sizeof (char);
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 		opp->oprom_array[0] |= prom_is_openprom() ?
678*7c478bd9Sstevel@tonic-gate 		    OPROMCONS_OPENPROM : 0;
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 		/*
681*7c478bd9Sstevel@tonic-gate 		 * The rest of the info is needed by Install to
682*7c478bd9Sstevel@tonic-gate 		 * decide if graphics should be started.
683*7c478bd9Sstevel@tonic-gate 		 */
684*7c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
685*7c478bd9Sstevel@tonic-gate 		    plat_stdin_is_keyboard()) {
686*7c478bd9Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDIN_IS_KBD;
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
690*7c478bd9Sstevel@tonic-gate 		    plat_stdout_is_framebuffer()) {
691*7c478bd9Sstevel@tonic-gate 			opp->oprom_array[0] |= OPROMCONS_STDOUT_IS_FB;
692*7c478bd9Sstevel@tonic-gate 		}
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
695*7c478bd9Sstevel@tonic-gate 		    sizeof (char) + sizeof (uint_t)) != 0)
696*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
697*7c478bd9Sstevel@tonic-gate 		break;
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 	case OPROMGETBOOTARGS: {
700*7c478bd9Sstevel@tonic-gate 		extern char kern_bootargs[];
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 		valsize = strlen(kern_bootargs) + 1;
703*7c478bd9Sstevel@tonic-gate 		if (valsize > userbufsize) {
704*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
705*7c478bd9Sstevel@tonic-gate 			break;
706*7c478bd9Sstevel@tonic-gate 		}
707*7c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, kern_bootargs);
708*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize - 1;
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, valsize + sizeof (uint_t)) != 0)
711*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
712*7c478bd9Sstevel@tonic-gate 	}	break;
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/*
715*7c478bd9Sstevel@tonic-gate 	 * convert a prom device path to an equivalent devfs path
716*7c478bd9Sstevel@tonic-gate 	 */
717*7c478bd9Sstevel@tonic-gate 	case OPROMPROM2DEVNAME: {
718*7c478bd9Sstevel@tonic-gate 		char *dev_name;
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 		/*
721*7c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
722*7c478bd9Sstevel@tonic-gate 		 */
723*7c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
724*7c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
725*7c478bd9Sstevel@tonic-gate 			break;
726*7c478bd9Sstevel@tonic-gate 		}
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 		dev_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 		error = i_promname_to_devname(opp->oprom_array, dev_name);
731*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
732*7c478bd9Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
733*7c478bd9Sstevel@tonic-gate 			break;
734*7c478bd9Sstevel@tonic-gate 		}
735*7c478bd9Sstevel@tonic-gate 		valsize = opp->oprom_size = strlen(dev_name);
736*7c478bd9Sstevel@tonic-gate 		if (++valsize > userbufsize) {
737*7c478bd9Sstevel@tonic-gate 			kmem_free(dev_name, MAXPATHLEN);
738*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
739*7c478bd9Sstevel@tonic-gate 			break;
740*7c478bd9Sstevel@tonic-gate 		}
741*7c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, dev_name);
742*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
743*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 		kmem_free(dev_name, MAXPATHLEN);
746*7c478bd9Sstevel@tonic-gate 	}	break;
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 	/*
749*7c478bd9Sstevel@tonic-gate 	 * Convert a prom device path name to a driver name
750*7c478bd9Sstevel@tonic-gate 	 */
751*7c478bd9Sstevel@tonic-gate 	case OPROMPATH2DRV: {
752*7c478bd9Sstevel@tonic-gate 		char *drv_name;
753*7c478bd9Sstevel@tonic-gate 		major_t maj;
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate 		/*
756*7c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
757*7c478bd9Sstevel@tonic-gate 		 */
758*7c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
759*7c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
760*7c478bd9Sstevel@tonic-gate 			break;
761*7c478bd9Sstevel@tonic-gate 		}
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 		/*
764*7c478bd9Sstevel@tonic-gate 		 * convert path to a driver binding name
765*7c478bd9Sstevel@tonic-gate 		 */
766*7c478bd9Sstevel@tonic-gate 		maj = path_to_major((char *)opp->oprom_array);
767*7c478bd9Sstevel@tonic-gate 		if (maj == (major_t)-1) {
768*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
769*7c478bd9Sstevel@tonic-gate 			break;
770*7c478bd9Sstevel@tonic-gate 		}
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 		/*
773*7c478bd9Sstevel@tonic-gate 		 * resolve any aliases
774*7c478bd9Sstevel@tonic-gate 		 */
775*7c478bd9Sstevel@tonic-gate 		if ((drv_name = ddi_major_to_name(maj)) == NULL) {
776*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
777*7c478bd9Sstevel@tonic-gate 			break;
778*7c478bd9Sstevel@tonic-gate 		}
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, drv_name);
781*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = strlen(drv_name);
782*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
783*7c478bd9Sstevel@tonic-gate 		    sizeof (uint_t) + opp->oprom_size + 1) != 0)
784*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
785*7c478bd9Sstevel@tonic-gate 	}	break;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	case OPROMGETVERSION:
788*7c478bd9Sstevel@tonic-gate 		/*
789*7c478bd9Sstevel@tonic-gate 		 * Get a string representing the running version of the
790*7c478bd9Sstevel@tonic-gate 		 * prom. How to create such a string is platform dependent,
791*7c478bd9Sstevel@tonic-gate 		 * so we just defer to a promif function. If no such
792*7c478bd9Sstevel@tonic-gate 		 * association exists, the promif implementation
793*7c478bd9Sstevel@tonic-gate 		 * may copy the string "unknown" into the given buffer,
794*7c478bd9Sstevel@tonic-gate 		 * and return its length (incl. NULL terminator).
795*7c478bd9Sstevel@tonic-gate 		 *
796*7c478bd9Sstevel@tonic-gate 		 * We expect prom_version_name to return the actual
797*7c478bd9Sstevel@tonic-gate 		 * length of the string, but copy at most userbufsize
798*7c478bd9Sstevel@tonic-gate 		 * bytes into the given buffer, including NULL termination.
799*7c478bd9Sstevel@tonic-gate 		 */
800*7c478bd9Sstevel@tonic-gate 
801*7c478bd9Sstevel@tonic-gate 		valsize = prom_version_name(opp->oprom_array, userbufsize);
802*7c478bd9Sstevel@tonic-gate 		if (valsize < 0) {
803*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
804*7c478bd9Sstevel@tonic-gate 			break;
805*7c478bd9Sstevel@tonic-gate 		}
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 		/*
808*7c478bd9Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
809*7c478bd9Sstevel@tonic-gate 		 */
810*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
811*7c478bd9Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
812*7c478bd9Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
813*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
814*7c478bd9Sstevel@tonic-gate 		break;
815*7c478bd9Sstevel@tonic-gate 
816*7c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
817*7c478bd9Sstevel@tonic-gate 	case OPROMGETFBNAME:
818*7c478bd9Sstevel@tonic-gate 		/*
819*7c478bd9Sstevel@tonic-gate 		 * Return stdoutpath, if it's a frame buffer.
820*7c478bd9Sstevel@tonic-gate 		 * Yes, we are comparing a possibly longer string against
821*7c478bd9Sstevel@tonic-gate 		 * the size we're really going to copy, but so what?
822*7c478bd9Sstevel@tonic-gate 		 */
823*7c478bd9Sstevel@tonic-gate 		if ((getzoneid() == GLOBAL_ZONEID) &&
824*7c478bd9Sstevel@tonic-gate 		    (prom_stdout_is_framebuffer() != 0) &&
825*7c478bd9Sstevel@tonic-gate 		    (userbufsize > strlen(prom_stdoutpath()))) {
826*7c478bd9Sstevel@tonic-gate 			prom_strip_options(prom_stdoutpath(),
827*7c478bd9Sstevel@tonic-gate 			    opp->oprom_array);	/* strip options and copy */
828*7c478bd9Sstevel@tonic-gate 			valsize = opp->oprom_size = strlen(opp->oprom_array);
829*7c478bd9Sstevel@tonic-gate 			if (copyout(opp, (void *)arg,
830*7c478bd9Sstevel@tonic-gate 			    valsize + 1 + sizeof (uint_t)) != 0)
831*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
832*7c478bd9Sstevel@tonic-gate 		} else
833*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
834*7c478bd9Sstevel@tonic-gate 		break;
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	/*
837*7c478bd9Sstevel@tonic-gate 	 * Convert a logical or physical device path to prom device path
838*7c478bd9Sstevel@tonic-gate 	 */
839*7c478bd9Sstevel@tonic-gate 	case OPROMDEV2PROMNAME: {
840*7c478bd9Sstevel@tonic-gate 		char *prom_name;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 		/*
843*7c478bd9Sstevel@tonic-gate 		 * The input argument, a pathname, is a NULL terminated string.
844*7c478bd9Sstevel@tonic-gate 		 */
845*7c478bd9Sstevel@tonic-gate 		if ((error = oprom_copyinstr(arg, opp->oprom_array,
846*7c478bd9Sstevel@tonic-gate 		    (size_t)userbufsize, MAXPATHLEN)) != 0) {
847*7c478bd9Sstevel@tonic-gate 			break;
848*7c478bd9Sstevel@tonic-gate 		}
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate 		prom_name = kmem_alloc(userbufsize, KM_SLEEP);
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 		/*
853*7c478bd9Sstevel@tonic-gate 		 * convert the devfs path to an equivalent prom path
854*7c478bd9Sstevel@tonic-gate 		 */
855*7c478bd9Sstevel@tonic-gate 		error = i_devname_to_promname(opp->oprom_array, prom_name,
856*7c478bd9Sstevel@tonic-gate 		    userbufsize);
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate 		if (error != 0) {
859*7c478bd9Sstevel@tonic-gate 			kmem_free(prom_name, userbufsize);
860*7c478bd9Sstevel@tonic-gate 			break;
861*7c478bd9Sstevel@tonic-gate 		}
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate 		for (valsize = 0; valsize < userbufsize; valsize++) {
864*7c478bd9Sstevel@tonic-gate 			opp->oprom_array[valsize] = prom_name[valsize];
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 			if ((valsize > 0) && (prom_name[valsize] == '\0') &&
867*7c478bd9Sstevel@tonic-gate 			    (prom_name[valsize-1] == '\0')) {
868*7c478bd9Sstevel@tonic-gate 				break;
869*7c478bd9Sstevel@tonic-gate 			}
870*7c478bd9Sstevel@tonic-gate 		}
871*7c478bd9Sstevel@tonic-gate 		opp->oprom_size = valsize;
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate 		kmem_free(prom_name, userbufsize);
874*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg, sizeof (uint_t) + valsize) != 0)
875*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 	}	break;
878*7c478bd9Sstevel@tonic-gate 
879*7c478bd9Sstevel@tonic-gate 	case OPROMSETOPT:
880*7c478bd9Sstevel@tonic-gate 	case OPROMSETOPT2: {
881*7c478bd9Sstevel@tonic-gate 		int namebuflen;
882*7c478bd9Sstevel@tonic-gate 		int valbuflen;
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate 		if ((prom_is_openprom() == 0) ||
885*7c478bd9Sstevel@tonic-gate 		    (node_id == OBP_NONODE) || (node_id == OBP_BADNODE)) {
886*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
887*7c478bd9Sstevel@tonic-gate 			break;
888*7c478bd9Sstevel@tonic-gate 		}
889*7c478bd9Sstevel@tonic-gate 
890*7c478bd9Sstevel@tonic-gate 		/*
891*7c478bd9Sstevel@tonic-gate 		 * The arguments are a property name and a value.
892*7c478bd9Sstevel@tonic-gate 		 * Copy in the entire user buffer.
893*7c478bd9Sstevel@tonic-gate 		 */
894*7c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
895*7c478bd9Sstevel@tonic-gate 		    opp->oprom_array, userbufsize) != 0) {
896*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
897*7c478bd9Sstevel@tonic-gate 			break;
898*7c478bd9Sstevel@tonic-gate 		}
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 		/*
901*7c478bd9Sstevel@tonic-gate 		 * The property name is the first string, value second
902*7c478bd9Sstevel@tonic-gate 		 */
903*7c478bd9Sstevel@tonic-gate 		namebuflen = strlen(opp->oprom_array);
904*7c478bd9Sstevel@tonic-gate 		valbuf = opp->oprom_array + namebuflen + 1;
905*7c478bd9Sstevel@tonic-gate 		valbuflen = strlen(valbuf);
906*7c478bd9Sstevel@tonic-gate 
907*7c478bd9Sstevel@tonic-gate 		if (cmd == OPROMSETOPT) {
908*7c478bd9Sstevel@tonic-gate 			valsize = valbuflen + 1;  /* +1 for the '\0' */
909*7c478bd9Sstevel@tonic-gate 		} else {
910*7c478bd9Sstevel@tonic-gate 			if ((namebuflen + 1 + valbuflen + 1) > userbufsize) {
911*7c478bd9Sstevel@tonic-gate 				error = EINVAL;
912*7c478bd9Sstevel@tonic-gate 				break;
913*7c478bd9Sstevel@tonic-gate 			}
914*7c478bd9Sstevel@tonic-gate 			valsize = (opp->oprom_array + userbufsize) - valbuf;
915*7c478bd9Sstevel@tonic-gate 		}
916*7c478bd9Sstevel@tonic-gate 
917*7c478bd9Sstevel@tonic-gate 		/*
918*7c478bd9Sstevel@tonic-gate 		 * 4010173: 'name' is not an option, but it is a property.
919*7c478bd9Sstevel@tonic-gate 		 */
920*7c478bd9Sstevel@tonic-gate 		if (strcmp(opp->oprom_array, "name") == 0)
921*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
922*7c478bd9Sstevel@tonic-gate 		else if (prom_setprop(node_id, opp->oprom_array,
923*7c478bd9Sstevel@tonic-gate 		    valbuf, valsize) < 0)
924*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
925*7c478bd9Sstevel@tonic-gate 
926*7c478bd9Sstevel@tonic-gate 	}	break;
927*7c478bd9Sstevel@tonic-gate 
928*7c478bd9Sstevel@tonic-gate 	case OPROMREADY64: {
929*7c478bd9Sstevel@tonic-gate 		struct openprom_opr64 *opr =
930*7c478bd9Sstevel@tonic-gate 		    (struct openprom_opr64 *)opp->oprom_array;
931*7c478bd9Sstevel@tonic-gate 		int i;
932*7c478bd9Sstevel@tonic-gate 		dnode_t id;
933*7c478bd9Sstevel@tonic-gate 
934*7c478bd9Sstevel@tonic-gate 		if (userbufsize < sizeof (*opr)) {
935*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
936*7c478bd9Sstevel@tonic-gate 			break;
937*7c478bd9Sstevel@tonic-gate 		}
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 		valsize = userbufsize -
940*7c478bd9Sstevel@tonic-gate 		    offsetof(struct openprom_opr64, message);
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate 		i = prom_version_check(opr->message, valsize, &id);
943*7c478bd9Sstevel@tonic-gate 		opr->return_code = i;
944*7c478bd9Sstevel@tonic-gate 		opr->nodeid = (int)id;
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate 		valsize = offsetof(struct openprom_opr64, message);
947*7c478bd9Sstevel@tonic-gate 		valsize += strlen(opr->message) + 1;
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate 		/*
950*7c478bd9Sstevel@tonic-gate 		 * copyout only the part of the user buffer we need to.
951*7c478bd9Sstevel@tonic-gate 		 */
952*7c478bd9Sstevel@tonic-gate 		if (copyout(opp, (void *)arg,
953*7c478bd9Sstevel@tonic-gate 		    (size_t)(min((uint_t)valsize, userbufsize) +
954*7c478bd9Sstevel@tonic-gate 		    sizeof (uint_t))) != 0)
955*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
956*7c478bd9Sstevel@tonic-gate 		break;
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate 	}	/* case OPROMREADY64 */
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate 	case WANBOOT_SETKEY: {
961*7c478bd9Sstevel@tonic-gate 		struct wankeyio *wp;
962*7c478bd9Sstevel@tonic-gate 		int reslen;
963*7c478bd9Sstevel@tonic-gate 		int status;
964*7c478bd9Sstevel@tonic-gate 		int rv;
965*7c478bd9Sstevel@tonic-gate 		int i;
966*7c478bd9Sstevel@tonic-gate 
967*7c478bd9Sstevel@tonic-gate 		/*
968*7c478bd9Sstevel@tonic-gate 		 * The argument is a struct wankeyio.  Validate it as best
969*7c478bd9Sstevel@tonic-gate 		 * we can.
970*7c478bd9Sstevel@tonic-gate 		 */
971*7c478bd9Sstevel@tonic-gate 		if (userbufsize != (sizeof (struct wankeyio))) {
972*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
973*7c478bd9Sstevel@tonic-gate 			break;
974*7c478bd9Sstevel@tonic-gate 		}
975*7c478bd9Sstevel@tonic-gate 		if (copyin(((caddr_t)arg + sizeof (uint_t)),
976*7c478bd9Sstevel@tonic-gate 		    opp->oprom_array, sizeof (struct wankeyio)) != 0) {
977*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
978*7c478bd9Sstevel@tonic-gate 			break;
979*7c478bd9Sstevel@tonic-gate 		}
980*7c478bd9Sstevel@tonic-gate 		wp = (struct wankeyio *)opp->oprom_array;
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate 		/* check for key name and key size overflow */
983*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < WANBOOT_MAXKEYNAMELEN; i++)
984*7c478bd9Sstevel@tonic-gate 			if (wp->wk_keyname[i] == '\0')
985*7c478bd9Sstevel@tonic-gate 				break;
986*7c478bd9Sstevel@tonic-gate 		if ((i == WANBOOT_MAXKEYNAMELEN) ||
987*7c478bd9Sstevel@tonic-gate 		    (wp->wk_keysize > WANBOOT_MAXKEYLEN)) {
988*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
989*7c478bd9Sstevel@tonic-gate 			break;
990*7c478bd9Sstevel@tonic-gate 		}
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 		rv = prom_set_security_key(wp->wk_keyname, wp->wk_u.key,
993*7c478bd9Sstevel@tonic-gate 		    wp->wk_keysize, &reslen, &status);
994*7c478bd9Sstevel@tonic-gate 		if (rv)
995*7c478bd9Sstevel@tonic-gate 			error = EIO;
996*7c478bd9Sstevel@tonic-gate 		else
997*7c478bd9Sstevel@tonic-gate 			switch (status) {
998*7c478bd9Sstevel@tonic-gate 				case 0:
999*7c478bd9Sstevel@tonic-gate 					error = 0;
1000*7c478bd9Sstevel@tonic-gate 					break;
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate 				case -2:	/* out of key storage space */
1003*7c478bd9Sstevel@tonic-gate 					error = ENOSPC;
1004*7c478bd9Sstevel@tonic-gate 					break;
1005*7c478bd9Sstevel@tonic-gate 
1006*7c478bd9Sstevel@tonic-gate 				case -3:	/* key name or value too long */
1007*7c478bd9Sstevel@tonic-gate 					error = EINVAL;
1008*7c478bd9Sstevel@tonic-gate 					break;
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 				case -4:	/* can't delete:  no such key */
1011*7c478bd9Sstevel@tonic-gate 					error = ENOENT;
1012*7c478bd9Sstevel@tonic-gate 					break;
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate 				case -1:	/* unspecified error */
1015*7c478bd9Sstevel@tonic-gate 				default:	/* this should not happen */
1016*7c478bd9Sstevel@tonic-gate 					error = EIO;
1017*7c478bd9Sstevel@tonic-gate 					break;
1018*7c478bd9Sstevel@tonic-gate 			}
1019*7c478bd9Sstevel@tonic-gate 		break;
1020*7c478bd9Sstevel@tonic-gate 	}	/* case WANBOOT_SETKEY */
1021*7c478bd9Sstevel@tonic-gate #endif	/* !__i386 && !__amd64 */
1022*7c478bd9Sstevel@tonic-gate 	}	/* switch (cmd)	*/
1023*7c478bd9Sstevel@tonic-gate 
1024*7c478bd9Sstevel@tonic-gate 	kmem_free(opp, userbufsize + sizeof (uint_t) + 1);
1025*7c478bd9Sstevel@tonic-gate 	return (error);
1026*7c478bd9Sstevel@tonic-gate }
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1029*7c478bd9Sstevel@tonic-gate static int
1030*7c478bd9Sstevel@tonic-gate opromioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1031*7c478bd9Sstevel@tonic-gate 	cred_t *credp, int *rvalp)
1032*7c478bd9Sstevel@tonic-gate {
1033*7c478bd9Sstevel@tonic-gate 	struct oprom_state *st;
1034*7c478bd9Sstevel@tonic-gate 	struct opromioctl_args arg_block;
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	if (getminor(dev) >= MAX_OPENS)
1037*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate 	st = &oprom_state[getminor(dev)];
1040*7c478bd9Sstevel@tonic-gate 	ASSERT(st->already_open);
1041*7c478bd9Sstevel@tonic-gate 	arg_block.st = st;
1042*7c478bd9Sstevel@tonic-gate 	arg_block.cmd = cmd;
1043*7c478bd9Sstevel@tonic-gate 	arg_block.arg = arg;
1044*7c478bd9Sstevel@tonic-gate 	arg_block.mode = mode;
1045*7c478bd9Sstevel@tonic-gate 	return (prom_tree_access(opromioctl_cb, &arg_block, &st->tree_gen));
1046*7c478bd9Sstevel@tonic-gate }
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate /*
1049*7c478bd9Sstevel@tonic-gate  * Copyin string and verify the actual string length is less than maxsize
1050*7c478bd9Sstevel@tonic-gate  * specified by the caller.
1051*7c478bd9Sstevel@tonic-gate  *
1052*7c478bd9Sstevel@tonic-gate  * Currently, maxsize is either OBP_MAXPROPNAME for property names
1053*7c478bd9Sstevel@tonic-gate  * or MAXPATHLEN for device path names. userbufsize is specified
1054*7c478bd9Sstevel@tonic-gate  * by the userland caller.
1055*7c478bd9Sstevel@tonic-gate  */
1056*7c478bd9Sstevel@tonic-gate static int
1057*7c478bd9Sstevel@tonic-gate oprom_copyinstr(intptr_t arg, char *buf, size_t bufsize, size_t maxsize)
1058*7c478bd9Sstevel@tonic-gate {
1059*7c478bd9Sstevel@tonic-gate 	int error;
1060*7c478bd9Sstevel@tonic-gate 	size_t actual_len;
1061*7c478bd9Sstevel@tonic-gate 
1062*7c478bd9Sstevel@tonic-gate 	if ((error = copyinstr(((caddr_t)arg + sizeof (uint_t)),
1063*7c478bd9Sstevel@tonic-gate 	    buf, bufsize, &actual_len)) != 0) {
1064*7c478bd9Sstevel@tonic-gate 		return (error);
1065*7c478bd9Sstevel@tonic-gate 	}
1066*7c478bd9Sstevel@tonic-gate 	if ((actual_len == 0) || (actual_len > maxsize)) {
1067*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1068*7c478bd9Sstevel@tonic-gate 	}
1069*7c478bd9Sstevel@tonic-gate 
1070*7c478bd9Sstevel@tonic-gate 	return (0);
1071*7c478bd9Sstevel@tonic-gate }
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate /*
1074*7c478bd9Sstevel@tonic-gate  * Check dnode_t passed in from userland
1075*7c478bd9Sstevel@tonic-gate  */
1076*7c478bd9Sstevel@tonic-gate static int
1077*7c478bd9Sstevel@tonic-gate oprom_checknodeid(dnode_t node_id, dnode_t current_id)
1078*7c478bd9Sstevel@tonic-gate {
1079*7c478bd9Sstevel@tonic-gate 	int depth;
1080*7c478bd9Sstevel@tonic-gate 	dnode_t id[OBP_STACKDEPTH];
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 	/*
1083*7c478bd9Sstevel@tonic-gate 	 * optimized path
1084*7c478bd9Sstevel@tonic-gate 	 */
1085*7c478bd9Sstevel@tonic-gate 	if (node_id == 0) {
1086*7c478bd9Sstevel@tonic-gate 		return (1);
1087*7c478bd9Sstevel@tonic-gate 	}
1088*7c478bd9Sstevel@tonic-gate 	if (node_id == OBP_BADNODE) {
1089*7c478bd9Sstevel@tonic-gate 		return (0);
1090*7c478bd9Sstevel@tonic-gate 	}
1091*7c478bd9Sstevel@tonic-gate 	if ((current_id != OBP_BADNODE) && ((node_id == current_id) ||
1092*7c478bd9Sstevel@tonic-gate 	    (node_id == prom_nextnode(current_id)) ||
1093*7c478bd9Sstevel@tonic-gate 	    (node_id == prom_childnode(current_id)))) {
1094*7c478bd9Sstevel@tonic-gate 		return (1);
1095*7c478bd9Sstevel@tonic-gate 	}
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 	/*
1098*7c478bd9Sstevel@tonic-gate 	 * long path: walk from root till we find node_id
1099*7c478bd9Sstevel@tonic-gate 	 */
1100*7c478bd9Sstevel@tonic-gate 	depth = 1;
1101*7c478bd9Sstevel@tonic-gate 	id[0] = prom_nextnode((dnode_t)0);
1102*7c478bd9Sstevel@tonic-gate 
1103*7c478bd9Sstevel@tonic-gate 	while (depth) {
1104*7c478bd9Sstevel@tonic-gate 		if (id[depth - 1] == node_id)
1105*7c478bd9Sstevel@tonic-gate 			return (1);	/* node_id found */
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate 		if (id[depth] = prom_childnode(id[depth - 1])) {
1108*7c478bd9Sstevel@tonic-gate 			depth++;
1109*7c478bd9Sstevel@tonic-gate 			continue;
1110*7c478bd9Sstevel@tonic-gate 		}
1111*7c478bd9Sstevel@tonic-gate 
1112*7c478bd9Sstevel@tonic-gate 		while (depth &&
1113*7c478bd9Sstevel@tonic-gate 		    ((id[depth - 1] = prom_nextnode(id[depth - 1])) == 0))
1114*7c478bd9Sstevel@tonic-gate 			depth--;
1115*7c478bd9Sstevel@tonic-gate 	}
1116*7c478bd9Sstevel@tonic-gate 	return (0);	/* node_id not found */
1117*7c478bd9Sstevel@tonic-gate }
1118*7c478bd9Sstevel@tonic-gate 
1119*7c478bd9Sstevel@tonic-gate static int
1120*7c478bd9Sstevel@tonic-gate oprom_copytree(struct oprom_state *st, uint_t flag)
1121*7c478bd9Sstevel@tonic-gate {
1122*7c478bd9Sstevel@tonic-gate 	ASSERT(st->snapshot == NULL && st->size == 0);
1123*7c478bd9Sstevel@tonic-gate 	return (oprom_copynode(
1124*7c478bd9Sstevel@tonic-gate 	    prom_nextnode(0), flag, &st->snapshot, &st->size));
1125*7c478bd9Sstevel@tonic-gate }
1126*7c478bd9Sstevel@tonic-gate 
1127*7c478bd9Sstevel@tonic-gate static int
1128*7c478bd9Sstevel@tonic-gate oprom_snapshot(struct oprom_state *st, intptr_t arg)
1129*7c478bd9Sstevel@tonic-gate {
1130*7c478bd9Sstevel@tonic-gate 	uint_t flag;
1131*7c478bd9Sstevel@tonic-gate 
1132*7c478bd9Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_SNAP) == -1)
1133*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
1134*7c478bd9Sstevel@tonic-gate 
1135*7c478bd9Sstevel@tonic-gate 	/* copyin flag and create snapshot */
1136*7c478bd9Sstevel@tonic-gate 	if ((copyin((void *)arg, &flag, sizeof (uint_t)) != 0) ||
1137*7c478bd9Sstevel@tonic-gate 	    (oprom_copytree(st, flag) != 0)) {
1138*7c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
1139*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
1140*7c478bd9Sstevel@tonic-gate 	}
1141*7c478bd9Sstevel@tonic-gate 
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 	/* copyout the size of the snapshot */
1144*7c478bd9Sstevel@tonic-gate 	flag = (uint_t)st->size;
1145*7c478bd9Sstevel@tonic-gate 	if (copyout(&flag, (void *)arg, sizeof (uint_t)) != 0) {
1146*7c478bd9Sstevel@tonic-gate 		kmem_free(st->snapshot, st->size);
1147*7c478bd9Sstevel@tonic-gate 		st->snapshot = NULL;
1148*7c478bd9Sstevel@tonic-gate 		st->size = 0;
1149*7c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_IDLE);
1150*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
1151*7c478bd9Sstevel@tonic-gate 	}
1152*7c478bd9Sstevel@tonic-gate 
1153*7c478bd9Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_DONE);
1154*7c478bd9Sstevel@tonic-gate 	return (0);
1155*7c478bd9Sstevel@tonic-gate }
1156*7c478bd9Sstevel@tonic-gate 
1157*7c478bd9Sstevel@tonic-gate static int
1158*7c478bd9Sstevel@tonic-gate oprom_copyout(struct oprom_state *st, intptr_t arg)
1159*7c478bd9Sstevel@tonic-gate {
1160*7c478bd9Sstevel@tonic-gate 	int error = 0;
1161*7c478bd9Sstevel@tonic-gate 	uint_t size;
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 	if (oprom_setstate(st, IOC_COPY) == -1)
1164*7c478bd9Sstevel@tonic-gate 		return (EBUSY);
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate 	/* copyin size and copyout snapshot */
1167*7c478bd9Sstevel@tonic-gate 	if (copyin((void *)arg, &size, sizeof (uint_t)) != 0)
1168*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
1169*7c478bd9Sstevel@tonic-gate 	else if (size < st->size)
1170*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
1171*7c478bd9Sstevel@tonic-gate 	else if (copyout(st->snapshot, (void *)arg, st->size) != 0)
1172*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	if (error) {
1175*7c478bd9Sstevel@tonic-gate 		/*
1176*7c478bd9Sstevel@tonic-gate 		 * on error keep the snapshot until a successful
1177*7c478bd9Sstevel@tonic-gate 		 * copyout or when the driver is closed.
1178*7c478bd9Sstevel@tonic-gate 		 */
1179*7c478bd9Sstevel@tonic-gate 		(void) oprom_setstate(st, IOC_DONE);
1180*7c478bd9Sstevel@tonic-gate 		return (error);
1181*7c478bd9Sstevel@tonic-gate 	}
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate 	kmem_free(st->snapshot, st->size);
1184*7c478bd9Sstevel@tonic-gate 	st->snapshot = NULL;
1185*7c478bd9Sstevel@tonic-gate 	st->size = 0;
1186*7c478bd9Sstevel@tonic-gate 	(void) oprom_setstate(st, IOC_IDLE);
1187*7c478bd9Sstevel@tonic-gate 	return (0);
1188*7c478bd9Sstevel@tonic-gate }
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate /*
1191*7c478bd9Sstevel@tonic-gate  * Copy all properties of nodeid into a single packed nvlist
1192*7c478bd9Sstevel@tonic-gate  */
1193*7c478bd9Sstevel@tonic-gate static int
1194*7c478bd9Sstevel@tonic-gate oprom_copyprop(dnode_t nodeid, uint_t flag, nvlist_t *nvl)
1195*7c478bd9Sstevel@tonic-gate {
1196*7c478bd9Sstevel@tonic-gate 	int proplen;
1197*7c478bd9Sstevel@tonic-gate 	char *propname, *propval, *buf1, *buf2;
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate 	ASSERT(nvl != NULL);
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate 	/*
1202*7c478bd9Sstevel@tonic-gate 	 * non verbose mode, get the "name" property only
1203*7c478bd9Sstevel@tonic-gate 	 */
1204*7c478bd9Sstevel@tonic-gate 	if (flag == 0) {
1205*7c478bd9Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, "name");
1206*7c478bd9Sstevel@tonic-gate 		if (proplen <= 0) {
1207*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
1208*7c478bd9Sstevel@tonic-gate 			    "failed to get the name of openprom node 0x%x",
1209*7c478bd9Sstevel@tonic-gate 			    nodeid);
1210*7c478bd9Sstevel@tonic-gate 			(void) nvlist_add_string(nvl, "name", "");
1211*7c478bd9Sstevel@tonic-gate 			return (0);
1212*7c478bd9Sstevel@tonic-gate 		}
1213*7c478bd9Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
1214*7c478bd9Sstevel@tonic-gate 		(void) prom_getprop(nodeid, "name", propval);
1215*7c478bd9Sstevel@tonic-gate 		(void) nvlist_add_string(nvl, "name", propval);
1216*7c478bd9Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
1217*7c478bd9Sstevel@tonic-gate 		return (0);
1218*7c478bd9Sstevel@tonic-gate 	}
1219*7c478bd9Sstevel@tonic-gate 
1220*7c478bd9Sstevel@tonic-gate 	/*
1221*7c478bd9Sstevel@tonic-gate 	 * Ask for first property by passing a NULL string
1222*7c478bd9Sstevel@tonic-gate 	 */
1223*7c478bd9Sstevel@tonic-gate 	buf1 = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
1224*7c478bd9Sstevel@tonic-gate 	buf2 = kmem_zalloc(OBP_MAXPROPNAME, KM_SLEEP);
1225*7c478bd9Sstevel@tonic-gate 	buf1[0] = '\0';
1226*7c478bd9Sstevel@tonic-gate 	while (propname = (char *)prom_nextprop(nodeid, buf1, buf2)) {
1227*7c478bd9Sstevel@tonic-gate 		if (strlen(propname) == 0)
1228*7c478bd9Sstevel@tonic-gate 			break;		/* end of prop list */
1229*7c478bd9Sstevel@tonic-gate 		(void) strcpy(buf1, propname);
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 		proplen = prom_getproplen(nodeid, propname);
1232*7c478bd9Sstevel@tonic-gate 		if (proplen == 0) {
1233*7c478bd9Sstevel@tonic-gate 			/* boolean property */
1234*7c478bd9Sstevel@tonic-gate 			(void) nvlist_add_boolean(nvl, propname);
1235*7c478bd9Sstevel@tonic-gate 			continue;
1236*7c478bd9Sstevel@tonic-gate 		}
1237*7c478bd9Sstevel@tonic-gate 		/* add 1 for null termination in case of a string */
1238*7c478bd9Sstevel@tonic-gate 		propval = kmem_zalloc(proplen + 1, KM_SLEEP);
1239*7c478bd9Sstevel@tonic-gate 		(void) prom_getprop(nodeid, propname, propval);
1240*7c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, propname,
1241*7c478bd9Sstevel@tonic-gate 		    (uchar_t *)propval, proplen + 1);
1242*7c478bd9Sstevel@tonic-gate 		kmem_free(propval, proplen + 1);
1243*7c478bd9Sstevel@tonic-gate 		bzero(buf2, OBP_MAXPROPNAME);
1244*7c478bd9Sstevel@tonic-gate 	}
1245*7c478bd9Sstevel@tonic-gate 
1246*7c478bd9Sstevel@tonic-gate 	kmem_free(buf1, OBP_MAXPROPNAME);
1247*7c478bd9Sstevel@tonic-gate 	kmem_free(buf2, OBP_MAXPROPNAME);
1248*7c478bd9Sstevel@tonic-gate 
1249*7c478bd9Sstevel@tonic-gate 	return (0);
1250*7c478bd9Sstevel@tonic-gate }
1251*7c478bd9Sstevel@tonic-gate 
1252*7c478bd9Sstevel@tonic-gate /*
1253*7c478bd9Sstevel@tonic-gate  * Copy all children and descendents into a a packed nvlist
1254*7c478bd9Sstevel@tonic-gate  */
1255*7c478bd9Sstevel@tonic-gate static int
1256*7c478bd9Sstevel@tonic-gate oprom_copychild(dnode_t nodeid, uint_t flag, char **buf, size_t *size)
1257*7c478bd9Sstevel@tonic-gate {
1258*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
1259*7c478bd9Sstevel@tonic-gate 	dnode_t child = prom_childnode(nodeid);
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate 	if (child == 0)
1262*7c478bd9Sstevel@tonic-gate 		return (0);
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, 0, KM_SLEEP);
1265*7c478bd9Sstevel@tonic-gate 	while (child != 0) {
1266*7c478bd9Sstevel@tonic-gate 		char *nodebuf = NULL;
1267*7c478bd9Sstevel@tonic-gate 		size_t nodesize = 0;
1268*7c478bd9Sstevel@tonic-gate 		if (oprom_copynode(child, flag, &nodebuf, &nodesize)) {
1269*7c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
1270*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "failed to copy nodeid 0x%x", child);
1271*7c478bd9Sstevel@tonic-gate 			return (-1);
1272*7c478bd9Sstevel@tonic-gate 		}
1273*7c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "node",
1274*7c478bd9Sstevel@tonic-gate 		    (uchar_t *)nodebuf, nodesize);
1275*7c478bd9Sstevel@tonic-gate 		kmem_free(nodebuf, nodesize);
1276*7c478bd9Sstevel@tonic-gate 		child = prom_nextnode(child);
1277*7c478bd9Sstevel@tonic-gate 	}
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate 	(void) nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
1280*7c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
1281*7c478bd9Sstevel@tonic-gate 	return (0);
1282*7c478bd9Sstevel@tonic-gate }
1283*7c478bd9Sstevel@tonic-gate 
1284*7c478bd9Sstevel@tonic-gate /*
1285*7c478bd9Sstevel@tonic-gate  * Copy a node into a packed nvlist
1286*7c478bd9Sstevel@tonic-gate  */
1287*7c478bd9Sstevel@tonic-gate static int
1288*7c478bd9Sstevel@tonic-gate oprom_copynode(dnode_t nodeid, uint_t flag, char **buf, size_t *size)
1289*7c478bd9Sstevel@tonic-gate {
1290*7c478bd9Sstevel@tonic-gate 	int error = 0;
1291*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
1292*7c478bd9Sstevel@tonic-gate 	char *childlist = NULL;
1293*7c478bd9Sstevel@tonic-gate 	size_t childsize = 0;
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
1296*7c478bd9Sstevel@tonic-gate 	ASSERT(nvl != NULL);
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate 	/* @nodeid -- @ is not a legal char in a 1275 property name */
1299*7c478bd9Sstevel@tonic-gate 	(void) nvlist_add_int32(nvl, "@nodeid", (int32_t)nodeid);
1300*7c478bd9Sstevel@tonic-gate 
1301*7c478bd9Sstevel@tonic-gate 	/* properties */
1302*7c478bd9Sstevel@tonic-gate 	if (error = oprom_copyprop(nodeid, flag, nvl))
1303*7c478bd9Sstevel@tonic-gate 		goto fail;
1304*7c478bd9Sstevel@tonic-gate 
1305*7c478bd9Sstevel@tonic-gate 	/* children */
1306*7c478bd9Sstevel@tonic-gate 	error = oprom_copychild(nodeid, flag, &childlist, &childsize);
1307*7c478bd9Sstevel@tonic-gate 	if (error != 0)
1308*7c478bd9Sstevel@tonic-gate 		goto fail;
1309*7c478bd9Sstevel@tonic-gate 	if (childlist != NULL) {
1310*7c478bd9Sstevel@tonic-gate 		(void) nvlist_add_byte_array(nvl, "@child",
1311*7c478bd9Sstevel@tonic-gate 		    (uchar_t *)childlist, (uint_t)childsize);
1312*7c478bd9Sstevel@tonic-gate 		kmem_free(childlist, childsize);
1313*7c478bd9Sstevel@tonic-gate 	}
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 	/* pack into contiguous buffer */
1316*7c478bd9Sstevel@tonic-gate 	error = nvlist_pack(nvl, buf, size, NV_ENCODE_NATIVE, KM_SLEEP);
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate fail:
1319*7c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
1320*7c478bd9Sstevel@tonic-gate 	return (error);
1321*7c478bd9Sstevel@tonic-gate }
1322*7c478bd9Sstevel@tonic-gate 
1323*7c478bd9Sstevel@tonic-gate /*
1324*7c478bd9Sstevel@tonic-gate  * The driver is stateful across OPROMSNAPSHOT and OPROMCOPYOUT.
1325*7c478bd9Sstevel@tonic-gate  * This function encapsulates the state machine:
1326*7c478bd9Sstevel@tonic-gate  *
1327*7c478bd9Sstevel@tonic-gate  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
1328*7c478bd9Sstevel@tonic-gate  *	|		SNAPSHOT		COPYOUT	 |
1329*7c478bd9Sstevel@tonic-gate  *	--------------------------------------------------
1330*7c478bd9Sstevel@tonic-gate  *
1331*7c478bd9Sstevel@tonic-gate  * Returns 0 on success and -1 on failure
1332*7c478bd9Sstevel@tonic-gate  */
1333*7c478bd9Sstevel@tonic-gate static int
1334*7c478bd9Sstevel@tonic-gate oprom_setstate(struct oprom_state *st, int16_t new_state)
1335*7c478bd9Sstevel@tonic-gate {
1336*7c478bd9Sstevel@tonic-gate 	int ret = 0;
1337*7c478bd9Sstevel@tonic-gate 
1338*7c478bd9Sstevel@tonic-gate 	mutex_enter(&oprom_lock);
1339*7c478bd9Sstevel@tonic-gate 	switch (new_state) {
1340*7c478bd9Sstevel@tonic-gate 	case IOC_IDLE:
1341*7c478bd9Sstevel@tonic-gate 	case IOC_DONE:
1342*7c478bd9Sstevel@tonic-gate 		break;
1343*7c478bd9Sstevel@tonic-gate 	case IOC_SNAP:
1344*7c478bd9Sstevel@tonic-gate 		if (st->ioc_state != IOC_IDLE)
1345*7c478bd9Sstevel@tonic-gate 			ret = -1;
1346*7c478bd9Sstevel@tonic-gate 		break;
1347*7c478bd9Sstevel@tonic-gate 	case IOC_COPY:
1348*7c478bd9Sstevel@tonic-gate 		if (st->ioc_state != IOC_DONE)
1349*7c478bd9Sstevel@tonic-gate 			ret = -1;
1350*7c478bd9Sstevel@tonic-gate 		break;
1351*7c478bd9Sstevel@tonic-gate 	default:
1352*7c478bd9Sstevel@tonic-gate 		ret = -1;
1353*7c478bd9Sstevel@tonic-gate 	}
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate 	if (ret == 0)
1356*7c478bd9Sstevel@tonic-gate 		st->ioc_state = new_state;
1357*7c478bd9Sstevel@tonic-gate 	else
1358*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
1359*7c478bd9Sstevel@tonic-gate 		    st->ioc_state, new_state);
1360*7c478bd9Sstevel@tonic-gate 	mutex_exit(&oprom_lock);
1361*7c478bd9Sstevel@tonic-gate 	return (ret);
1362*7c478bd9Sstevel@tonic-gate }
1363