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