Home | History | Annotate | Line # | Download | only in eeprom
      1 /*	$NetBSD: ophandlers.c,v 1.14 2026/09/21 09:37:48 macallan 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 (const struct extabent *, struct opiocdesc *, char *);
     53 static	void op_data(const struct extabent *, struct opiocdesc *, char *);
     54 static	void op_bitmap(const struct extabent *, struct opiocdesc *, char *);
     55 
     56 /*
     57  * There are several known fields that I either don't know how to
     58  * deal with or require special treatment.
     59  */
     60 static	const struct extabent opextab[] = {
     61 	{ "security-password",		op_notsupp },
     62 	{ "security-mode",		op_notsupp },
     63 	{ "hardware-revision",		op_data },
     64 	{ "oem-logo",			op_bitmap },
     65 	{ NULL,				op_notsupp },
     66 };
     67 
     68 #define BARF(str1, str2) {						\
     69 	snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2));	\
     70 	++eval;								\
     71 	return (err_str);						\
     72 };
     73 
     74 void
     75 op_action(char *keyword, char *arg)
     76 {
     77 	char	*cp;
     78 
     79 	if ((cp = op_handler(keyword, arg)) != NULL)
     80 		warnx("%s", cp);
     81 	return;
     82 }
     83 
     84 int
     85 check_for_openprom(void)
     86 {
     87 	int fd, rv, optnode;
     88 
     89 	/* if we can't open it, obviously we can't use it. */
     90 	if ((fd = open(path_openprom, O_RDONLY)) < 0)
     91 		return (0);
     92 
     93 	/* check for the presence of OpenFirmware with OPIOCGETOPTNODE */
     94 	rv = ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode);
     95 	close (fd);
     96 
     97 	return (rv == 0);
     98 }
     99 
    100 char *
    101 op_handler(char *keyword, char *arg)
    102 {
    103 	struct opiocdesc opio;
    104 	const struct extabent *ex;
    105 	char opio_buf[BUFSIZE];
    106 	int fd, optnode;
    107 
    108 	if ((fd = open(path_openprom, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
    109 		BARF(path_openprom, strerror(errno));
    110 
    111 	/* Check to see if it's a special-case keyword. */
    112 	for (ex = opextab; ex->ex_keyword != NULL; ++ex)
    113 		if (strcmp(ex->ex_keyword, keyword) == 0)
    114 			break;
    115 
    116 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0) {
    117 		(void)close(fd);
    118 		BARF("OPIOCGETOPTNODE", strerror(errno));
    119 	}
    120 
    121 	memset(&opio_buf[0], 0, sizeof(opio_buf));
    122 	memset(&opio, 0, sizeof(opio));
    123 	opio.op_nodeid = optnode;
    124 	opio.op_name = keyword;
    125 	opio.op_namelen = strlen(opio.op_name);
    126 
    127 	if (arg) {
    128 		if (verbose) {
    129 			printf("old: ");
    130 
    131 			opio.op_buf = &opio_buf[0];
    132 			opio.op_buflen = sizeof(opio_buf);
    133 			if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
    134 				(void)close(fd);
    135 				BARF("OPIOCGET", strerror(errno));
    136 			}
    137 
    138 			if (opio.op_buflen <= 0) {
    139 				printf("nothing available for %s\n", keyword);
    140 				goto out;
    141 			}
    142 
    143 			if (ex->ex_keyword != NULL)
    144 				(*ex->ex_handler)(ex, &opio, NULL);
    145 			else
    146 				printf("%s\n", opio.op_buf);
    147 		}
    148  out:
    149 		if (ex->ex_keyword != NULL)
    150 			(*ex->ex_handler)(ex, &opio, arg);
    151 		else {
    152 			opio.op_buf = arg;
    153 			opio.op_buflen = strlen(arg);
    154 		}
    155 
    156 		if (ioctl(fd, OPIOCSET, (char *)&opio) < 0) {
    157 			(void)close(fd);
    158 			BARF("invalid keyword", keyword);
    159 		}
    160 
    161 		if (verbose) {
    162 			printf("new: ");
    163 			if (ex->ex_keyword != NULL)
    164 				(*ex->ex_handler)(ex, &opio, NULL);
    165 			else
    166 				printf("%s\n", opio.op_buf);
    167 		}
    168 	} else {
    169 		opio.op_buf = &opio_buf[0];
    170 		opio.op_buflen = sizeof(opio_buf);
    171 		if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
    172 			(void)close(fd);
    173 			BARF("OPIOCGET", strerror(errno));
    174 		}
    175 
    176 		if (opio.op_buflen <= 0) {
    177 			(void)snprintf(err_str, sizeof err_str,
    178 			    "nothing available for %s", keyword);
    179 			return (err_str);
    180 		}
    181 
    182 		if (ex->ex_keyword != NULL)
    183 			(*ex->ex_handler)(ex, &opio, NULL);
    184 		else
    185 			printf("%s=%s\n", keyword, opio.op_buf);
    186 	}
    187 
    188 	(void)close(fd);
    189 	return (NULL);
    190 }
    191 
    192 /* ARGSUSED */
    193 static void
    194 op_notsupp(const struct extabent *exent, struct opiocdesc *opiop, char *arg)
    195 {
    196 
    197 	warnx("property `%s' not yet supported", exent->ex_keyword);
    198 }
    199 
    200 #define MIN(a, b) ((a) < (b) ? (a) : (b))
    201 
    202 static void
    203 op_data(const struct extabent *exent, struct opiocdesc *opiop, char *arg)
    204 {
    205 	int i, j;
    206 
    207 	printf("%s:\n", exent->ex_keyword);
    208 	for (i = 0; i < opiop->op_buflen; i += 16) {
    209 		printf("%04x:", i);
    210 		for (j = 0; j < MIN(16, opiop->op_buflen - i); j++) {
    211 			printf(" %02x", opiop->op_buf[i + j] & 0xff);
    212 		}
    213 		printf("\n");
    214 	}
    215 }
    216 
    217 static void
    218 op_bitmap(const struct extabent *exent, struct opiocdesc *opiop, char *arg)
    219 {
    220 	int i, j, k, b;
    221 
    222 	printf("%s:\n", exent->ex_keyword);
    223 	for (i = 0; i < opiop->op_buflen; i += 8) {
    224 		for (j = 0; j < MIN(8, opiop->op_buflen - i); j++) {
    225 			b = opiop->op_buf[i + j];
    226 			for (k = 0; k < 8; k++) {
    227 				printf("%c", b & 0x80 ? '#' : ' ');
    228 				b <<= 1;
    229 			}
    230 		}
    231 		printf("\n");
    232 	}
    233 }
    234 
    235 /*
    236  * XXX: This code is quite ugly.  You have been warned.
    237  * (Really!  This is the only way I could get it to work!)
    238  */
    239 void
    240 op_dump(void)
    241 {
    242 	struct opiocdesc opio1, opio2;
    243 	const struct extabent *ex;
    244 	char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
    245 	int fd, optnode;
    246 
    247 	if ((fd = open(path_openprom, O_RDONLY, 0640)) < 0)
    248 		err(1, "open: %s", path_openprom);
    249 
    250 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
    251 		err(1, "OPIOCGETOPTNODE");
    252 
    253 	memset(&opio1, 0, sizeof(opio1));
    254 
    255 	/* This will grab the first property name from OPIOCNEXTPROP. */
    256 	memset(buf1, 0, sizeof(buf1));
    257 	memset(buf2, 0, sizeof(buf2));
    258 
    259 	opio1.op_nodeid = opio2.op_nodeid = optnode;
    260 
    261 	opio1.op_name = buf1;
    262 	opio1.op_buf = buf2;
    263 
    264 	opio2.op_name = buf3;
    265 	opio2.op_buf = buf4;
    266 
    267 	/*
    268 	 * For reference: opio1 is for obtaining the name.  Pass the
    269 	 * name of the last property read in op_name, and the next one
    270 	 * will be returned in op_buf.  To get the first name, pass
    271 	 * an empty string.  There are no more properties when an
    272 	 * empty string is returned.
    273 	 *
    274 	 * opio2 is for obtaining the value associated with that name.
    275 	 * For some crazy reason, it seems as if we need to do all
    276 	 * of that gratuitious zapping and copying.  *sigh*
    277 	 */
    278 	for (;;) {
    279 		opio1.op_namelen = strlen(opio1.op_name);
    280 		opio1.op_buflen = sizeof(buf2);
    281 
    282 		if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) < 0)
    283 			err(1, "ioctl: OPIOCNEXTPROP");
    284 
    285 		/*
    286 		 * The name of the property we wish to get the
    287 		 * value for has been stored in the value field
    288 		 * of opio1.  If the length of the name is 0, there
    289 		 * are no more properties left.
    290 		 */
    291 		strcpy(opio2.op_name, opio1.op_buf);	/* XXX strcpy is safe */
    292 		opio2.op_namelen = strlen(opio2.op_name);
    293 
    294 		if (opio2.op_namelen == 0) {
    295 			(void)close(fd);
    296 			return;
    297 		}
    298 
    299 		memset(opio2.op_buf, 0, sizeof(buf4));
    300 		opio2.op_buflen = sizeof(buf4);
    301 
    302 		if (ioctl(fd, OPIOCGET, (char *)&opio2) < 0)
    303 			err(1, "ioctl: OPIOCGET");
    304 
    305 		for (ex = opextab; ex->ex_keyword != NULL; ++ex)
    306 			if (strcmp(ex->ex_keyword, opio2.op_name) == 0)
    307 				break;
    308 
    309 		if (ex->ex_keyword != NULL)
    310 			(*ex->ex_handler)(ex, &opio2, NULL);
    311 		else
    312 			printf("%s=%s\n", opio2.op_name, opio2.op_buf);
    313 
    314 		/*
    315 		 * Place the name of the last read value back into
    316 		 * opio1 so that we may obtain the next name.
    317 		 */
    318 		memset(opio1.op_name, 0, sizeof(buf1));
    319 		memset(opio1.op_buf, 0, sizeof(buf2));
    320 		strcpy(opio1.op_name, opio2.op_name);	/* XXX strcpy is safe */
    321 	}
    322 	/* NOTREACHED */
    323 }
    324