Home | History | Annotate | Line # | Download | only in ofw
openfirmio.c revision 1.2.4.2
      1 /*	$NetBSD: openfirmio.c,v 1.2.4.2 2002/09/17 21:20:12 nathanw Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)openfirm.c	8.1 (Berkeley) 6/11/93
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: openfirmio.c,v 1.2.4.2 2002/09/17 21:20:12 nathanw Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/errno.h>
     53 #include <sys/fcntl.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/conf.h>
     57 #include <sys/device.h>
     58 
     59 #include <dev/ofw/openfirm.h>
     60 #include <dev/ofw/openfirmio.h>
     61 
     62 static	int lastnode;			/* speed hack */
     63 
     64 static int openfirmcheckid (int, int);
     65 static int openfirmgetstr (int, char *, char **);
     66 
     67 void openfirmattach (int);
     68 
     69 dev_type_ioctl(openfirmioctl);
     70 
     71 const struct cdevsw openfirm_cdevsw = {
     72 	nullopen, nullclose, noread, nowrite, openfirmioctl,
     73 	nostop, notty, nopoll, nommap,
     74 };
     75 
     76 void
     77 openfirmattach(int num)
     78 {
     79 	/* nothing */
     80 }
     81 
     82 /*
     83  * Verify target ID is valid (exists in the OPENPROM tree), as
     84  * listed from node ID sid forward.
     85  */
     86 static int
     87 openfirmcheckid(int sid, int tid)
     88 {
     89 
     90 	for (; sid != 0; sid = OF_peer(sid))
     91 		if (sid == tid || openfirmcheckid(OF_child(sid), tid))
     92 			return (1);
     93 
     94 	return (0);
     95 }
     96 
     97 static int
     98 openfirmgetstr(int len, char *user, char **cpp)
     99 {
    100 	int error;
    101 	char *cp;
    102 
    103 	/* Reject obvious bogus requests */
    104 	if ((u_int)len > (8 * 1024) - 1)
    105 		return (ENAMETOOLONG);
    106 
    107 	*cpp = cp = malloc(len + 1, M_TEMP, M_WAITOK);
    108 	error = copyin(user, cp, len);
    109 	cp[len] = '\0';
    110 	return (error);
    111 }
    112 
    113 int
    114 openfirmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
    115 {
    116 	struct ofiocdesc *of;
    117 	int node, len, ok, error, s;
    118 	char *name, *value;
    119 
    120 	if (cmd == OFIOCGETOPTNODE) {
    121 		s = splhigh();
    122 		*(int *) data = OF_finddevice("/options");
    123 		splx(s);
    124 		return (0);
    125 	}
    126 
    127 	/* Verify node id */
    128 	of = (struct ofiocdesc *)data;
    129 	node = of->of_nodeid;
    130 	if (node != 0 && node != lastnode) {
    131 		/* Not an easy one, must search for it */
    132 		s = splhigh();
    133 		ok = openfirmcheckid(OF_peer(0), node);
    134 		splx(s);
    135 		if (!ok)
    136 			return (EINVAL);
    137 		lastnode = node;
    138 	}
    139 
    140 	name = value = NULL;
    141 	error = 0;
    142 	switch (cmd) {
    143 
    144 	case OFIOCGET:
    145 		if ((flags & FREAD) == 0)
    146 			return (EBADF);
    147 		if (node == 0)
    148 			return (EINVAL);
    149 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    150 		if (error)
    151 			break;
    152 		s = splhigh();
    153 		len = OF_getproplen(node, name);
    154 		splx(s);
    155 		if (len > of->of_buflen) {
    156 			error = ENOMEM;
    157 			break;
    158 		}
    159 		of->of_buflen = len;
    160 		/* -1 means no entry; 0 means no value */
    161 		if (len <= 0)
    162 			break;
    163 		value = malloc(len, M_TEMP, M_WAITOK);
    164 		if (value == NULL) {
    165 			error = ENOMEM;
    166 			break;
    167 		}
    168 		s = splhigh();
    169 		len = OF_getprop(node, name, (void *)value, len);
    170 		splx(s);
    171 		error = copyout(value, of->of_buf, len);
    172 		break;
    173 
    174 #if 0
    175 	case OFIOCSET:
    176 		if ((flags & FWRITE) == 0)
    177 			return (EBADF);
    178 		if (node == 0)
    179 			return (EINVAL);
    180 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    181 		if (error)
    182 			break;
    183 		error = openfirmgetstr(of->of_buflen, of->of_buf, &value);
    184 		if (error)
    185 			break;
    186 		s = splhigh();
    187 		len = OF_setprop(node, name, value, of->of_buflen + 1);
    188 		splx(s);
    189 		if (len != of->of_buflen)
    190 			error = EINVAL;
    191 		break;
    192 #endif
    193 
    194 	case OFIOCNEXTPROP: {
    195 		char newname[32];
    196 		if ((flags & FREAD) == 0)
    197 			return (EBADF);
    198 		if (node == 0)
    199 			return (EINVAL);
    200 		if (of->of_namelen != 0) {
    201 			error = openfirmgetstr(of->of_namelen, of->of_name,
    202 			    &name);
    203 			if (error)
    204 				break;
    205 		}
    206 		s = splhigh();
    207 		ok = OF_nextprop(node, name, newname);
    208 		splx(s);
    209 		if (ok == 0) {
    210 			error = ENOENT;
    211 			break;
    212 		}
    213 		if (ok == -1) {
    214 			error = EINVAL;
    215 			break;
    216 		}
    217 		len = strlen(newname);
    218 		if (len > of->of_buflen)
    219 			len = of->of_buflen;
    220 		else
    221 			of->of_buflen = len;
    222 		error = copyout(newname, of->of_buf, len);
    223 		break;
    224 	}
    225 
    226 	case OFIOCGETNEXT:
    227 		if ((flags & FREAD) == 0)
    228 			return (EBADF);
    229 		s = splhigh();
    230 		node = OF_peer(node);
    231 		splx(s);
    232 		*(int *)data = lastnode = node;
    233 		break;
    234 
    235 	case OFIOCGETCHILD:
    236 		if ((flags & FREAD) == 0)
    237 			return (EBADF);
    238 		if (node == 0)
    239 			return (EINVAL);
    240 		s = splhigh();
    241 		node = OF_child(node);
    242 		splx(s);
    243 		*(int *)data = lastnode = node;
    244 		break;
    245 
    246 	case OFIOCFINDDEVICE:
    247 		if ((flags & FREAD) == 0)
    248 			return (EBADF);
    249 		error = openfirmgetstr(of->of_namelen, of->of_name, &name);
    250 		if (error)
    251 			break;
    252 		node = OF_finddevice(name);
    253 		if (node == 0 || node == -1) {
    254 			error = ENOENT;
    255 			break;
    256 		}
    257 		of->of_nodeid = lastnode = node;
    258 		break;
    259 
    260 	default:
    261 		return (ENOTTY);
    262 	}
    263 
    264 	if (name)
    265 		free(name, M_TEMP);
    266 	if (value)
    267 		free(value, M_TEMP);
    268 
    269 	return (error);
    270 }
    271