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