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