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