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