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