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