xref: /openbsd-src/usr.sbin/eeprom/ophandlers.c (revision 5b133f3f277e80f096764111e64f3a1284acb179)
1*5b133f3fSguenther /*	$OpenBSD: ophandlers.c,v 1.18 2023/03/08 04:43:13 guenther Exp $	*/
2608f9123Sniklas /*	$NetBSD: ophandlers.c,v 1.2 1996/02/28 01:13:30 thorpej Exp $	*/
3df930be7Sderaadt 
4608f9123Sniklas /*-
5608f9123Sniklas  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6df930be7Sderaadt  * All rights reserved.
7df930be7Sderaadt  *
8608f9123Sniklas  * This code is derived from software contributed to The NetBSD Foundation
9608f9123Sniklas  * by Jason R. Thorpe.
10608f9123Sniklas  *
11df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt  * modification, are permitted provided that the following conditions
13df930be7Sderaadt  * are met:
14df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
19df930be7Sderaadt  *
20608f9123Sniklas  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21608f9123Sniklas  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22608f9123Sniklas  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23608f9123Sniklas  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24608f9123Sniklas  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25608f9123Sniklas  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26608f9123Sniklas  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27608f9123Sniklas  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28608f9123Sniklas  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29608f9123Sniklas  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30608f9123Sniklas  * POSSIBILITY OF SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33df930be7Sderaadt #include <sys/types.h>
34df930be7Sderaadt #include <sys/ioctl.h>
35df930be7Sderaadt #include <err.h>
36df930be7Sderaadt #include <errno.h>
37df930be7Sderaadt #include <fcntl.h>
38df930be7Sderaadt #include <string.h>
39df930be7Sderaadt #include <stdio.h>
40963a1fd6Smiod #include <stdlib.h>
41ab95fc2fSfgsch #include <unistd.h>
42963a1fd6Smiod #include <vis.h>
43df930be7Sderaadt 
44df930be7Sderaadt #include <machine/openpromio.h>
45df930be7Sderaadt 
46df930be7Sderaadt #include "defs.h"
47df930be7Sderaadt 
48df930be7Sderaadt extern	char *path_openprom;
49df930be7Sderaadt extern	int eval;
50df930be7Sderaadt extern	int verbose;
51df930be7Sderaadt 
52df930be7Sderaadt static	char err_str[BUFSIZE];
53df930be7Sderaadt 
54c72b5b24Smillert static	void op_notsupp(struct extabent *, struct opiocdesc *, char *);
55963a1fd6Smiod static	void op_print(char *);
56df930be7Sderaadt 
57df930be7Sderaadt /*
58df930be7Sderaadt  * There are several known fields that I either don't know how to
59df930be7Sderaadt  * deal with or require special treatment.
60df930be7Sderaadt  */
61df930be7Sderaadt static	struct extabent opextab[] = {
62df930be7Sderaadt 	{ "security-password",		op_notsupp },
63df930be7Sderaadt 	{ "security-mode",		op_notsupp },
64df930be7Sderaadt 	{ "oem-logo",			op_notsupp },
65df930be7Sderaadt 	{ NULL,				op_notsupp },
66df930be7Sderaadt };
67df930be7Sderaadt 
68df930be7Sderaadt #define BARF(str1, str2) {						\
696066580eSderaadt 	snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2));	\
70df930be7Sderaadt 	++eval;								\
71df930be7Sderaadt 	return (err_str);						\
72df930be7Sderaadt };
73df930be7Sderaadt 
74df930be7Sderaadt char *
op_handler(char * keyword,char * arg)753dad9bb3Sderaadt op_handler(char *keyword, char *arg)
76df930be7Sderaadt {
77df930be7Sderaadt 	struct opiocdesc opio;
78df930be7Sderaadt 	struct extabent *ex;
79df930be7Sderaadt 	char opio_buf[BUFSIZE];
80df930be7Sderaadt 	int fd, optnode;
81df930be7Sderaadt 
82b7041c07Sderaadt 	if ((fd = open(path_openprom, arg ? O_RDWR : O_RDONLY)) == -1)
83df930be7Sderaadt 		BARF(path_openprom, strerror(errno));
84df930be7Sderaadt 
85df930be7Sderaadt 	/* Check to see if it's a special-case keyword. */
86df930be7Sderaadt 	for (ex = opextab; ex->ex_keyword != NULL; ++ex)
87df930be7Sderaadt 		if (strcmp(ex->ex_keyword, keyword) == 0)
88df930be7Sderaadt 			break;
89df930be7Sderaadt 
90df69c215Sderaadt 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) == -1)
91df930be7Sderaadt 		BARF("OPIOCGETOPTNODE", strerror(errno));
92df930be7Sderaadt 
93df930be7Sderaadt 	bzero(&opio_buf[0], sizeof(opio_buf));
94df930be7Sderaadt 	bzero(&opio, sizeof(opio));
95df930be7Sderaadt 	opio.op_nodeid = optnode;
96df930be7Sderaadt 	opio.op_name = keyword;
97df930be7Sderaadt 	opio.op_namelen = strlen(opio.op_name);
98df930be7Sderaadt 
99df930be7Sderaadt 	if (arg) {
100df930be7Sderaadt 		if (verbose) {
101df930be7Sderaadt 			printf("old: ");
102df930be7Sderaadt 
103df930be7Sderaadt 			opio.op_buf = &opio_buf[0];
104df930be7Sderaadt 			opio.op_buflen = sizeof(opio_buf);
105df69c215Sderaadt 			if (ioctl(fd, OPIOCGET, (char *)&opio) == -1)
106df930be7Sderaadt 				BARF("OPIOCGET", strerror(errno));
107df930be7Sderaadt 
108df930be7Sderaadt 			if (opio.op_buflen <= 0) {
10958bfddefShenning 				printf("nothing available for %s\n", keyword);
110df930be7Sderaadt 				goto out;
111df930be7Sderaadt 			}
112df930be7Sderaadt 
113df930be7Sderaadt 			if (ex->ex_keyword != NULL)
114df930be7Sderaadt 				(*ex->ex_handler)(ex, &opio, NULL);
115df930be7Sderaadt 			else
116963a1fd6Smiod 				op_print(opio.op_buf);
117df930be7Sderaadt 		}
118df930be7Sderaadt  out:
119df930be7Sderaadt 		if (ex->ex_keyword != NULL)
120df930be7Sderaadt 			(*ex->ex_handler)(ex, &opio, arg);
121df930be7Sderaadt 		else {
122df930be7Sderaadt 			opio.op_buf = arg;
123df930be7Sderaadt 			opio.op_buflen = strlen(arg);
124df930be7Sderaadt 		}
125df930be7Sderaadt 
126df69c215Sderaadt 		if (ioctl(fd, OPIOCSET, (char *)&opio) == -1)
127df930be7Sderaadt 			BARF("invalid keyword", keyword);
128df930be7Sderaadt 
129df930be7Sderaadt 		if (verbose) {
130df930be7Sderaadt 			printf("new: ");
131df930be7Sderaadt 			if (ex->ex_keyword != NULL)
132df930be7Sderaadt 				(*ex->ex_handler)(ex, &opio, NULL);
133df930be7Sderaadt 			else
134963a1fd6Smiod 				op_print(opio.op_buf);
135df930be7Sderaadt 		}
136df930be7Sderaadt 	} else {
137df930be7Sderaadt 		opio.op_buf = &opio_buf[0];
138df930be7Sderaadt 		opio.op_buflen = sizeof(opio_buf);
139df69c215Sderaadt 		if (ioctl(fd, OPIOCGET, (char *)&opio) == -1)
140df930be7Sderaadt 			BARF("OPIOCGET", strerror(errno));
141df930be7Sderaadt 
142df930be7Sderaadt 		if (opio.op_buflen <= 0) {
1434593f4baSderaadt 			snprintf(err_str, sizeof err_str,
1444593f4baSderaadt 			    "nothing available for %s",
145df930be7Sderaadt 			    keyword);
146df930be7Sderaadt 			return (err_str);
147df930be7Sderaadt 		}
148df930be7Sderaadt 
149df930be7Sderaadt 		if (ex->ex_keyword != NULL)
150df930be7Sderaadt 			(*ex->ex_handler)(ex, &opio, NULL);
151963a1fd6Smiod 		else {
152963a1fd6Smiod 			printf("%s=", keyword);
153963a1fd6Smiod 			op_print(opio.op_buf);
154963a1fd6Smiod 		}
155df930be7Sderaadt 	}
156df930be7Sderaadt 
157df930be7Sderaadt 	(void)close(fd);
158df930be7Sderaadt 	return (NULL);
159df930be7Sderaadt }
160df930be7Sderaadt 
161df930be7Sderaadt static void
op_notsupp(struct extabent * exent,struct opiocdesc * opiop,char * arg)1623dad9bb3Sderaadt op_notsupp(struct extabent *exent, struct opiocdesc *opiop, char *arg)
163df930be7Sderaadt {
164df930be7Sderaadt 
165df930be7Sderaadt 	warnx("property `%s' not yet supported", exent->ex_keyword);
166df930be7Sderaadt }
167df930be7Sderaadt 
168df930be7Sderaadt /*
169df930be7Sderaadt  * XXX: This code is quite ugly.  You have been warned.
170df930be7Sderaadt  * (Really!  This is the only way I could get it to work!)
171df930be7Sderaadt  */
172df930be7Sderaadt void
op_dump(void)1733dad9bb3Sderaadt op_dump(void)
174df930be7Sderaadt {
175df930be7Sderaadt 	struct opiocdesc opio1, opio2;
176df930be7Sderaadt 	struct extabent *ex;
177df930be7Sderaadt 	char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
178df930be7Sderaadt 	int fd, optnode;
179df930be7Sderaadt 
180b7041c07Sderaadt 	if ((fd = open(path_openprom, O_RDONLY)) == -1)
181df930be7Sderaadt 		err(1, "open: %s", path_openprom);
182df930be7Sderaadt 
183df69c215Sderaadt 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) == -1)
184df930be7Sderaadt 		err(1, "OPIOCGETOPTNODE");
185df930be7Sderaadt 
186df930be7Sderaadt 	bzero(&opio1, sizeof(opio1));
187df930be7Sderaadt 
188df930be7Sderaadt 	/* This will grab the first property name from OPIOCNEXTPROP. */
189df930be7Sderaadt 	bzero(buf1, sizeof(buf1));
190df930be7Sderaadt 	bzero(buf2, sizeof(buf2));
191df930be7Sderaadt 
192df930be7Sderaadt 	opio1.op_nodeid = opio2.op_nodeid = optnode;
193df930be7Sderaadt 
194df930be7Sderaadt 	opio1.op_name = buf1;
195df930be7Sderaadt 	opio1.op_buf = buf2;
196df930be7Sderaadt 
197df930be7Sderaadt 	opio2.op_name = buf3;
198df930be7Sderaadt 	opio2.op_buf = buf4;
199df930be7Sderaadt 
200df930be7Sderaadt 	/*
201df930be7Sderaadt 	 * For reference: opio1 is for obtaining the name.  Pass the
202df930be7Sderaadt 	 * name of the last property read in op_name, and the next one
203df930be7Sderaadt 	 * will be returned in op_buf.  To get the first name, pass
204df930be7Sderaadt 	 * an empty string.  There are no more properties when an
205df930be7Sderaadt 	 * empty string is returned.
206df930be7Sderaadt 	 *
207df930be7Sderaadt 	 * opio2 is for obtaining the value associated with that name.
208df930be7Sderaadt 	 * For some crazy reason, it seems as if we need to do all
20951b27d62Stb 	 * of that gratuitous zapping and copying.  *sigh*
210df930be7Sderaadt 	 */
211df930be7Sderaadt 	for (;;) {
212df930be7Sderaadt 		opio1.op_namelen = strlen(opio1.op_name);
213df930be7Sderaadt 		opio1.op_buflen = sizeof(buf2);
214df930be7Sderaadt 
215df69c215Sderaadt 		if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) == -1)
216df930be7Sderaadt 			err(1, "ioctl: OPIOCNEXTPROP");
217df930be7Sderaadt 
218df930be7Sderaadt 		/*
219df930be7Sderaadt 		 * The name of the property we wish to get the
220df930be7Sderaadt 		 * value for has been stored in the value field
221df930be7Sderaadt 		 * of opio1.  If the length of the name is 0, there
222df930be7Sderaadt 		 * are no more properties left.
223df930be7Sderaadt 		 */
224cf839266Smiod 		strlcpy(opio2.op_name, opio1.op_buf, sizeof(buf3));
225df930be7Sderaadt 		opio2.op_namelen = strlen(opio2.op_name);
226df930be7Sderaadt 
227df930be7Sderaadt 		if (opio2.op_namelen == 0) {
228df930be7Sderaadt 			(void)close(fd);
229df930be7Sderaadt 			return;
230df930be7Sderaadt 		}
231df930be7Sderaadt 
232df930be7Sderaadt 		bzero(opio2.op_buf, sizeof(buf4));
233df930be7Sderaadt 		opio2.op_buflen = sizeof(buf4);
234df930be7Sderaadt 
235df69c215Sderaadt 		if (ioctl(fd, OPIOCGET, (char *)&opio2) == -1)
236df930be7Sderaadt 			err(1, "ioctl: OPIOCGET");
237df930be7Sderaadt 
238df930be7Sderaadt 		for (ex = opextab; ex->ex_keyword != NULL; ++ex)
239df930be7Sderaadt 			if (strcmp(ex->ex_keyword, opio2.op_name) == 0)
240df930be7Sderaadt 				break;
241df930be7Sderaadt 
242df930be7Sderaadt 		if (ex->ex_keyword != NULL)
243df930be7Sderaadt 			(*ex->ex_handler)(ex, &opio2, NULL);
244963a1fd6Smiod 		else {
245963a1fd6Smiod 			printf("%s=", opio2.op_name);
246963a1fd6Smiod 			op_print(opio2.op_buf);
247963a1fd6Smiod 		}
248df930be7Sderaadt 
249df930be7Sderaadt 		/*
250df930be7Sderaadt 		 * Place the name of the last read value back into
251df930be7Sderaadt 		 * opio1 so that we may obtain the next name.
252df930be7Sderaadt 		 */
253df930be7Sderaadt 		bzero(opio1.op_name, sizeof(buf1));
254df930be7Sderaadt 		bzero(opio1.op_buf, sizeof(buf2));
255cf839266Smiod 		strlcpy(opio1.op_name, opio2.op_name, sizeof(buf1));
256df930be7Sderaadt 	}
257df930be7Sderaadt 	/* NOTREACHED */
258df930be7Sderaadt }
259963a1fd6Smiod 
260963a1fd6Smiod static void
op_print(char * op_buf)261963a1fd6Smiod op_print(char *op_buf)
262963a1fd6Smiod {
263963a1fd6Smiod 	char *vistr;
264963a1fd6Smiod 	size_t size;
265963a1fd6Smiod 
266963a1fd6Smiod 	size = 1 + 4 * strlen(op_buf);
26735de856eSderaadt 	vistr = malloc(size);
268963a1fd6Smiod 	if (vistr == NULL)
269963a1fd6Smiod 		printf("(out of memory)\n");
270963a1fd6Smiod 	else {
271963a1fd6Smiod 		strnvis(vistr, op_buf, size, VIS_NL | VIS_TAB | VIS_OCTAL);
272963a1fd6Smiod 		printf("%s\n", vistr);
273963a1fd6Smiod 		free(vistr);
274963a1fd6Smiod 	}
275963a1fd6Smiod }
276