Home | History | Annotate | Line # | Download | only in eeprom
ofhandlers.c revision 1.1
      1  1.1  macallan /*	$NetBSD: ofhandlers.c,v 1.1 2006/08/16 03:24:57 macallan Exp $	*/
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  macallan  * by Jason R. Thorpe.
      9  1.1  macallan  *
     10  1.1  macallan  * Redistribution and use in source and binary forms, with or without
     11  1.1  macallan  * modification, are permitted provided that the following conditions
     12  1.1  macallan  * are met:
     13  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     14  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     15  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     18  1.1  macallan  * 3. All advertising materials mentioning features or use of this software
     19  1.1  macallan  *    must display the following acknowledgement:
     20  1.1  macallan  *        This product includes software developed by the NetBSD
     21  1.1  macallan  *        Foundation, Inc. and its contributors.
     22  1.1  macallan  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  macallan  *    contributors may be used to endorse or promote products derived
     24  1.1  macallan  *    from this software without specific prior written permission.
     25  1.1  macallan  *
     26  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  macallan  */
     38  1.1  macallan 
     39  1.1  macallan #include <sys/types.h>
     40  1.1  macallan #include <sys/ioctl.h>
     41  1.1  macallan #include <err.h>
     42  1.1  macallan #include <errno.h>
     43  1.1  macallan #include <fcntl.h>
     44  1.1  macallan #include <string.h>
     45  1.1  macallan #include <stdio.h>
     46  1.1  macallan #include <string.h>
     47  1.1  macallan #include <unistd.h>
     48  1.1  macallan 
     49  1.1  macallan #include <dev/ofw/openfirmio.h>
     50  1.1  macallan 
     51  1.1  macallan #include "defs.h"
     52  1.1  macallan 
     53  1.1  macallan extern	char *path_openfirm;
     54  1.1  macallan extern	int eval;
     55  1.1  macallan extern	int verbose;
     56  1.1  macallan 
     57  1.1  macallan static	char err_str[BUFSIZE];
     58  1.1  macallan 
     59  1.1  macallan static	void of_notsupp (struct extabent *, struct ofiocdesc *, char *);
     60  1.1  macallan 
     61  1.1  macallan /*
     62  1.1  macallan  * There are several known fields that I either don't know how to
     63  1.1  macallan  * deal with or require special treatment.
     64  1.1  macallan  */
     65  1.1  macallan static	struct extabent ofextab[] = {
     66  1.1  macallan 	{ "security-password",		of_notsupp },
     67  1.1  macallan 	{ "security-mode",		of_notsupp },
     68  1.1  macallan 	{ "oem-logo",			of_notsupp },
     69  1.1  macallan 	{ NULL,				of_notsupp },
     70  1.1  macallan };
     71  1.1  macallan 
     72  1.1  macallan #define BARF(str1, str2) {						\
     73  1.1  macallan 	snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2));	\
     74  1.1  macallan 	++eval;								\
     75  1.1  macallan 	return (err_str);						\
     76  1.1  macallan };
     77  1.1  macallan 
     78  1.1  macallan void
     79  1.1  macallan of_action(keyword, arg)
     80  1.1  macallan 	char *keyword, *arg;
     81  1.1  macallan {
     82  1.1  macallan 	char	*cp;
     83  1.1  macallan 
     84  1.1  macallan 	if ((cp = of_handler(keyword, arg)) != NULL)
     85  1.1  macallan 		warnx("%s", cp);
     86  1.1  macallan 	return;
     87  1.1  macallan }
     88  1.1  macallan 
     89  1.1  macallan char *
     90  1.1  macallan of_handler(keyword, arg)
     91  1.1  macallan 	char *keyword, *arg;
     92  1.1  macallan {
     93  1.1  macallan 	struct ofiocdesc ofio;
     94  1.1  macallan 	struct extabent *ex;
     95  1.1  macallan 	char ofio_buf[BUFSIZE];
     96  1.1  macallan 	int fd, optnode;
     97  1.1  macallan 
     98  1.1  macallan 	if ((fd = open(path_openfirm, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
     99  1.1  macallan 		BARF(path_openfirm, strerror(errno));
    100  1.1  macallan 
    101  1.1  macallan 	/* Check to see if it's a special-case keyword. */
    102  1.1  macallan 	for (ex = ofextab; ex->ex_keyword != NULL; ++ex)
    103  1.1  macallan 		if (strcmp(ex->ex_keyword, keyword) == 0)
    104  1.1  macallan 			break;
    105  1.1  macallan 
    106  1.1  macallan 	if (ioctl(fd, OFIOCGETOPTNODE, (char *)&optnode) < 0)
    107  1.1  macallan 		BARF("OFIOCGETOPTNODE", strerror(errno));
    108  1.1  macallan 
    109  1.1  macallan 	memset(&ofio_buf[0], 0, sizeof(ofio_buf));
    110  1.1  macallan 	memset(&ofio, 0, sizeof(ofio));
    111  1.1  macallan 	ofio.of_nodeid = optnode;
    112  1.1  macallan 	ofio.of_name = keyword;
    113  1.1  macallan 	ofio.of_namelen = strlen(ofio.of_name);
    114  1.1  macallan 
    115  1.1  macallan 	if (arg) {
    116  1.1  macallan 		if (verbose) {
    117  1.1  macallan 			printf("old: ");
    118  1.1  macallan 
    119  1.1  macallan 			ofio.of_buf = &ofio_buf[0];
    120  1.1  macallan 			ofio.of_buflen = sizeof(ofio_buf);
    121  1.1  macallan 			if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0)
    122  1.1  macallan 				BARF("OFIOCGET", strerror(errno));
    123  1.1  macallan 
    124  1.1  macallan 			if (ofio.of_buflen <= 0) {
    125  1.1  macallan 				printf("nothing available for %s\n", keyword);
    126  1.1  macallan 				goto out;
    127  1.1  macallan 			}
    128  1.1  macallan 
    129  1.1  macallan 			if (ex->ex_keyword != NULL)
    130  1.1  macallan 				(*ex->ex_handler)(ex, &ofio, NULL);
    131  1.1  macallan 			else
    132  1.1  macallan 				printf("%s\n", ofio.of_buf);
    133  1.1  macallan 		}
    134  1.1  macallan  out:
    135  1.1  macallan 		if (ex->ex_keyword != NULL)
    136  1.1  macallan 			(*ex->ex_handler)(ex, &ofio, arg);
    137  1.1  macallan 		else {
    138  1.1  macallan 			ofio.of_buf = arg;
    139  1.1  macallan 			ofio.of_buflen = strlen(arg);
    140  1.1  macallan 		}
    141  1.1  macallan 
    142  1.1  macallan 		if (ioctl(fd, OFIOCSET, (char *)&ofio) < 0)
    143  1.1  macallan 			BARF("invalid keyword", keyword);
    144  1.1  macallan 
    145  1.1  macallan 		if (verbose) {
    146  1.1  macallan 			printf("new: ");
    147  1.1  macallan 			if (ex->ex_keyword != NULL)
    148  1.1  macallan 				(*ex->ex_handler)(ex, &ofio, NULL);
    149  1.1  macallan 			else
    150  1.1  macallan 				printf("%s\n", ofio.of_buf);
    151  1.1  macallan 		}
    152  1.1  macallan 	} else {
    153  1.1  macallan 		ofio.of_buf = &ofio_buf[0];
    154  1.1  macallan 		ofio.of_buflen = sizeof(ofio_buf);
    155  1.1  macallan 		if (ioctl(fd, OFIOCGET, (char *)&ofio) < 0)
    156  1.1  macallan 			BARF("OFIOCGET", strerror(errno));
    157  1.1  macallan 
    158  1.1  macallan 		if (ofio.of_buflen <= 0) {
    159  1.1  macallan 			(void)snprintf(err_str, sizeof err_str,
    160  1.1  macallan 			    "nothing available for %s", keyword);
    161  1.1  macallan 			return (err_str);
    162  1.1  macallan 		}
    163  1.1  macallan 
    164  1.1  macallan 		if (ex->ex_keyword != NULL)
    165  1.1  macallan 			(*ex->ex_handler)(ex, &ofio, NULL);
    166  1.1  macallan 		else
    167  1.1  macallan 			printf("%s=%s\n", keyword, ofio.of_buf);
    168  1.1  macallan 	}
    169  1.1  macallan 
    170  1.1  macallan 	(void)close(fd);
    171  1.1  macallan 	return (NULL);
    172  1.1  macallan }
    173  1.1  macallan 
    174  1.1  macallan /* ARGSUSED */
    175  1.1  macallan static void
    176  1.1  macallan of_notsupp(exent, ofiop, arg)
    177  1.1  macallan 	struct extabent *exent;
    178  1.1  macallan 	struct ofiocdesc *ofiop;
    179  1.1  macallan 	char *arg;
    180  1.1  macallan {
    181  1.1  macallan 
    182  1.1  macallan 	warnx("property `%s' not yet supported", exent->ex_keyword);
    183  1.1  macallan }
    184  1.1  macallan 
    185  1.1  macallan /*
    186  1.1  macallan  * XXX: This code is quite ugly.  You have been warned.
    187  1.1  macallan  * (Really!  This is the only way I could get it to work!)
    188  1.1  macallan  */
    189  1.1  macallan void
    190  1.1  macallan of_dump()
    191  1.1  macallan {
    192  1.1  macallan 	struct ofiocdesc ofio1, ofio2;
    193  1.1  macallan 	struct extabent *ex;
    194  1.1  macallan 	char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
    195  1.1  macallan 	int fd, optnode;
    196  1.1  macallan 
    197  1.1  macallan 	if ((fd = open(path_openfirm, O_RDONLY, 0640)) < 0)
    198  1.1  macallan 		err(1, "open: %s", path_openfirm);
    199  1.1  macallan 
    200  1.1  macallan 	if (ioctl(fd, OFIOCGETOPTNODE, (char *)&optnode) < 0)
    201  1.1  macallan 		err(1, "OFIOCGETOPTNODE");
    202  1.1  macallan 
    203  1.1  macallan 	memset(&ofio1, 0, sizeof(ofio1));
    204  1.1  macallan 
    205  1.1  macallan 	/* This will grab the first property name from OPIOCNEXTPROP. */
    206  1.1  macallan 	memset(buf1, 0, sizeof(buf1));
    207  1.1  macallan 	memset(buf2, 0, sizeof(buf2));
    208  1.1  macallan 
    209  1.1  macallan 	ofio1.of_nodeid = ofio2.of_nodeid = optnode;
    210  1.1  macallan 
    211  1.1  macallan 	ofio1.of_name = buf1;
    212  1.1  macallan 	ofio1.of_buf = buf2;
    213  1.1  macallan 
    214  1.1  macallan 	ofio2.of_name = buf3;
    215  1.1  macallan 	ofio2.of_buf = buf4;
    216  1.1  macallan 
    217  1.1  macallan 	/*
    218  1.1  macallan 	 * For reference: ofio1 is for obtaining the name.  Pass the
    219  1.1  macallan 	 * name of the last property read in of_name, and the next one
    220  1.1  macallan 	 * will be returned in of_buf.  To get the first name, pass
    221  1.1  macallan 	 * an empty string.  There are no more properties when an
    222  1.1  macallan 	 * empty string is returned.
    223  1.1  macallan 	 *
    224  1.1  macallan 	 * ofio2 is for obtaining the value associated with that name.
    225  1.1  macallan 	 * For some crazy reason, it seems as if we need to do all
    226  1.1  macallan 	 * of that gratuitious zapping and copying.  *sigh*
    227  1.1  macallan 	 */
    228  1.1  macallan 	for (;;) {
    229  1.1  macallan 		ofio1.of_namelen = strlen(ofio1.of_name);
    230  1.1  macallan 		ofio1.of_buflen = sizeof(buf2);
    231  1.1  macallan 
    232  1.1  macallan 		if (ioctl(fd, OFIOCNEXTPROP, (char *)&ofio1) < 0) {
    233  1.1  macallan 			close(fd);
    234  1.1  macallan 			return;
    235  1.1  macallan 			/* err(1, "ioctl: OFIOCNEXTPROP"); */
    236  1.1  macallan 		}
    237  1.1  macallan 
    238  1.1  macallan 		/*
    239  1.1  macallan 		 * The name of the property we wish to get the
    240  1.1  macallan 		 * value for has been stored in the value field
    241  1.1  macallan 		 * of ofio1.  If the length of the name is 0, there
    242  1.1  macallan 		 * are no more properties left.
    243  1.1  macallan 		 */
    244  1.1  macallan 		strcpy(ofio2.of_name, ofio1.of_buf);	/* XXX strcpy is safe */
    245  1.1  macallan 		ofio2.of_namelen = strlen(ofio2.of_name);
    246  1.1  macallan 
    247  1.1  macallan 		if (ofio2.of_namelen == 0) {
    248  1.1  macallan 			(void)close(fd);
    249  1.1  macallan 			return;
    250  1.1  macallan 		}
    251  1.1  macallan 
    252  1.1  macallan 		memset(ofio2.of_buf, 0, sizeof(buf4));
    253  1.1  macallan 		ofio2.of_buflen = sizeof(buf4);
    254  1.1  macallan 
    255  1.1  macallan 		if (ioctl(fd, OFIOCGET, (char *)&ofio2) < 0)
    256  1.1  macallan 			err(1, "ioctl: OFIOCGET");
    257  1.1  macallan 
    258  1.1  macallan 		for (ex = ofextab; ex->ex_keyword != NULL; ++ex)
    259  1.1  macallan 			if (strcmp(ex->ex_keyword, ofio2.of_name) == 0)
    260  1.1  macallan 				break;
    261  1.1  macallan 
    262  1.1  macallan 		if (ex->ex_keyword != NULL)
    263  1.1  macallan 			(*ex->ex_handler)(ex, &ofio2, NULL);
    264  1.1  macallan 		else
    265  1.1  macallan 			printf("%s=%s\n", ofio2.of_name, ofio2.of_buf);
    266  1.1  macallan 
    267  1.1  macallan 		/*
    268  1.1  macallan 		 * Place the name of the last read value back into
    269  1.1  macallan 		 * ofio1 so that we may obtain the next name.
    270  1.1  macallan 		 */
    271  1.1  macallan 		memset(ofio1.of_name, 0, sizeof(buf1));
    272  1.1  macallan 		memset(ofio1.of_buf, 0, sizeof(buf2));
    273  1.1  macallan 		strcpy(ofio1.of_name, ofio2.of_name);	/* XXX strcpy is safe */
    274  1.1  macallan 	}
    275  1.1  macallan 	/* NOTREACHED */
    276  1.1  macallan }
    277