xref: /netbsd/usr.sbin/eeprom/ophandlers.c (revision 0aad27fe)
1 /*	$NetBSD: ophandlers.c,v 1.13 2013/07/02 11:59:46 joerg 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 
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	const 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
op_action(char * keyword,char * arg)72 op_action(char *keyword, char *arg)
73 {
74 	char	*cp;
75 
76 	if ((cp = op_handler(keyword, arg)) != NULL)
77 		warnx("%s", cp);
78 	return;
79 }
80 
81 int
check_for_openprom(void)82 check_for_openprom(void)
83 {
84 	int fd, rv, optnode;
85 
86 	/* if we can't open it, obviously we can't use it. */
87 	if ((fd = open(path_openprom, O_RDONLY)) < 0)
88 		return (0);
89 
90 	/* check for the presence of OpenFirmware with OPIOCGETOPTNODE */
91 	rv = ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode);
92 	close (fd);
93 
94 	return (rv == 0);
95 }
96 
97 char *
op_handler(char * keyword,char * arg)98 op_handler(char *keyword, char *arg)
99 {
100 	struct opiocdesc opio;
101 	const struct extabent *ex;
102 	char opio_buf[BUFSIZE];
103 	int fd, optnode;
104 
105 	if ((fd = open(path_openprom, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
106 		BARF(path_openprom, strerror(errno));
107 
108 	/* Check to see if it's a special-case keyword. */
109 	for (ex = opextab; ex->ex_keyword != NULL; ++ex)
110 		if (strcmp(ex->ex_keyword, keyword) == 0)
111 			break;
112 
113 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0) {
114 		(void)close(fd);
115 		BARF("OPIOCGETOPTNODE", strerror(errno));
116 	}
117 
118 	memset(&opio_buf[0], 0, sizeof(opio_buf));
119 	memset(&opio, 0, sizeof(opio));
120 	opio.op_nodeid = optnode;
121 	opio.op_name = keyword;
122 	opio.op_namelen = strlen(opio.op_name);
123 
124 	if (arg) {
125 		if (verbose) {
126 			printf("old: ");
127 
128 			opio.op_buf = &opio_buf[0];
129 			opio.op_buflen = sizeof(opio_buf);
130 			if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
131 				(void)close(fd);
132 				BARF("OPIOCGET", strerror(errno));
133 			}
134 
135 			if (opio.op_buflen <= 0) {
136 				printf("nothing available for %s\n", keyword);
137 				goto out;
138 			}
139 
140 			if (ex->ex_keyword != NULL)
141 				(*ex->ex_handler)(ex, &opio, NULL);
142 			else
143 				printf("%s\n", opio.op_buf);
144 		}
145  out:
146 		if (ex->ex_keyword != NULL)
147 			(*ex->ex_handler)(ex, &opio, arg);
148 		else {
149 			opio.op_buf = arg;
150 			opio.op_buflen = strlen(arg);
151 		}
152 
153 		if (ioctl(fd, OPIOCSET, (char *)&opio) < 0) {
154 			(void)close(fd);
155 			BARF("invalid keyword", keyword);
156 		}
157 
158 		if (verbose) {
159 			printf("new: ");
160 			if (ex->ex_keyword != NULL)
161 				(*ex->ex_handler)(ex, &opio, NULL);
162 			else
163 				printf("%s\n", opio.op_buf);
164 		}
165 	} else {
166 		opio.op_buf = &opio_buf[0];
167 		opio.op_buflen = sizeof(opio_buf);
168 		if (ioctl(fd, OPIOCGET, (char *)&opio) < 0) {
169 			(void)close(fd);
170 			BARF("OPIOCGET", strerror(errno));
171 		}
172 
173 		if (opio.op_buflen <= 0) {
174 			(void)snprintf(err_str, sizeof err_str,
175 			    "nothing available for %s", keyword);
176 			return (err_str);
177 		}
178 
179 		if (ex->ex_keyword != NULL)
180 			(*ex->ex_handler)(ex, &opio, NULL);
181 		else
182 			printf("%s=%s\n", keyword, opio.op_buf);
183 	}
184 
185 	(void)close(fd);
186 	return (NULL);
187 }
188 
189 /* ARGSUSED */
190 static void
op_notsupp(const struct extabent * exent,struct opiocdesc * opiop,char * arg)191 op_notsupp(const struct extabent *exent, struct opiocdesc *opiop, char *arg)
192 {
193 
194 	warnx("property `%s' not yet supported", exent->ex_keyword);
195 }
196 
197 /*
198  * XXX: This code is quite ugly.  You have been warned.
199  * (Really!  This is the only way I could get it to work!)
200  */
201 void
op_dump(void)202 op_dump(void)
203 {
204 	struct opiocdesc opio1, opio2;
205 	const struct extabent *ex;
206 	char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
207 	int fd, optnode;
208 
209 	if ((fd = open(path_openprom, O_RDONLY, 0640)) < 0)
210 		err(1, "open: %s", path_openprom);
211 
212 	if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
213 		err(1, "OPIOCGETOPTNODE");
214 
215 	memset(&opio1, 0, sizeof(opio1));
216 
217 	/* This will grab the first property name from OPIOCNEXTPROP. */
218 	memset(buf1, 0, sizeof(buf1));
219 	memset(buf2, 0, sizeof(buf2));
220 
221 	opio1.op_nodeid = opio2.op_nodeid = optnode;
222 
223 	opio1.op_name = buf1;
224 	opio1.op_buf = buf2;
225 
226 	opio2.op_name = buf3;
227 	opio2.op_buf = buf4;
228 
229 	/*
230 	 * For reference: opio1 is for obtaining the name.  Pass the
231 	 * name of the last property read in op_name, and the next one
232 	 * will be returned in op_buf.  To get the first name, pass
233 	 * an empty string.  There are no more properties when an
234 	 * empty string is returned.
235 	 *
236 	 * opio2 is for obtaining the value associated with that name.
237 	 * For some crazy reason, it seems as if we need to do all
238 	 * of that gratuitious zapping and copying.  *sigh*
239 	 */
240 	for (;;) {
241 		opio1.op_namelen = strlen(opio1.op_name);
242 		opio1.op_buflen = sizeof(buf2);
243 
244 		if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) < 0)
245 			err(1, "ioctl: OPIOCNEXTPROP");
246 
247 		/*
248 		 * The name of the property we wish to get the
249 		 * value for has been stored in the value field
250 		 * of opio1.  If the length of the name is 0, there
251 		 * are no more properties left.
252 		 */
253 		strcpy(opio2.op_name, opio1.op_buf);	/* XXX strcpy is safe */
254 		opio2.op_namelen = strlen(opio2.op_name);
255 
256 		if (opio2.op_namelen == 0) {
257 			(void)close(fd);
258 			return;
259 		}
260 
261 		memset(opio2.op_buf, 0, sizeof(buf4));
262 		opio2.op_buflen = sizeof(buf4);
263 
264 		if (ioctl(fd, OPIOCGET, (char *)&opio2) < 0)
265 			err(1, "ioctl: OPIOCGET");
266 
267 		for (ex = opextab; ex->ex_keyword != NULL; ++ex)
268 			if (strcmp(ex->ex_keyword, opio2.op_name) == 0)
269 				break;
270 
271 		if (ex->ex_keyword != NULL)
272 			(*ex->ex_handler)(ex, &opio2, NULL);
273 		else
274 			printf("%s=%s\n", opio2.op_name, opio2.op_buf);
275 
276 		/*
277 		 * Place the name of the last read value back into
278 		 * opio1 so that we may obtain the next name.
279 		 */
280 		memset(opio1.op_name, 0, sizeof(buf1));
281 		memset(opio1.op_buf, 0, sizeof(buf2));
282 		strcpy(opio1.op_name, opio2.op_name);	/* XXX strcpy is safe */
283 	}
284 	/* NOTREACHED */
285 }
286