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