ophandlers.c revision 1.3 1 1.3 mrg /* $NetBSD: ophandlers.c,v 1.3 1997/04/13 13:36:49 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.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 1.2 thorpej * 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.1 thorpej
48 1.1 thorpej #include <machine/eeprom.h>
49 1.1 thorpej #include <machine/openpromio.h>
50 1.1 thorpej
51 1.1 thorpej #include "defs.h"
52 1.1 thorpej
53 1.1 thorpej extern char *path_openprom;
54 1.1 thorpej extern int eval;
55 1.1 thorpej extern int verbose;
56 1.1 thorpej
57 1.1 thorpej static char err_str[BUFSIZE];
58 1.1 thorpej
59 1.1 thorpej static void op_notsupp __P((struct extabent *, struct opiocdesc *, char *));
60 1.1 thorpej
61 1.1 thorpej /*
62 1.1 thorpej * There are several known fields that I either don't know how to
63 1.1 thorpej * deal with or require special treatment.
64 1.1 thorpej */
65 1.1 thorpej static struct extabent opextab[] = {
66 1.1 thorpej { "security-password", op_notsupp },
67 1.1 thorpej { "security-mode", op_notsupp },
68 1.1 thorpej { "oem-logo", op_notsupp },
69 1.1 thorpej { NULL, op_notsupp },
70 1.1 thorpej };
71 1.1 thorpej
72 1.1 thorpej #define BARF(str1, str2) { \
73 1.3 mrg snprintf(err_str, sizeof err_str, "%s: %s", (str1), (str2)); \
74 1.1 thorpej ++eval; \
75 1.1 thorpej return (err_str); \
76 1.1 thorpej };
77 1.1 thorpej
78 1.1 thorpej char *
79 1.1 thorpej op_handler(keyword, arg)
80 1.1 thorpej char *keyword, *arg;
81 1.1 thorpej {
82 1.1 thorpej struct opiocdesc opio;
83 1.1 thorpej struct extabent *ex;
84 1.1 thorpej char opio_buf[BUFSIZE];
85 1.1 thorpej int fd, optnode;
86 1.1 thorpej
87 1.1 thorpej if ((fd = open(path_openprom, arg ? O_RDWR : O_RDONLY, 0640)) < 0)
88 1.1 thorpej BARF(path_openprom, strerror(errno));
89 1.1 thorpej
90 1.1 thorpej /* Check to see if it's a special-case keyword. */
91 1.1 thorpej for (ex = opextab; ex->ex_keyword != NULL; ++ex)
92 1.1 thorpej if (strcmp(ex->ex_keyword, keyword) == 0)
93 1.1 thorpej break;
94 1.1 thorpej
95 1.1 thorpej if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
96 1.1 thorpej BARF("OPIOCGETOPTNODE", strerror(errno));
97 1.1 thorpej
98 1.1 thorpej bzero(&opio_buf[0], sizeof(opio_buf));
99 1.1 thorpej bzero(&opio, sizeof(opio));
100 1.1 thorpej opio.op_nodeid = optnode;
101 1.1 thorpej opio.op_name = keyword;
102 1.1 thorpej opio.op_namelen = strlen(opio.op_name);
103 1.1 thorpej
104 1.1 thorpej if (arg) {
105 1.1 thorpej if (verbose) {
106 1.1 thorpej printf("old: ");
107 1.1 thorpej
108 1.1 thorpej opio.op_buf = &opio_buf[0];
109 1.1 thorpej opio.op_buflen = sizeof(opio_buf);
110 1.1 thorpej if (ioctl(fd, OPIOCGET, (char *)&opio) < 0)
111 1.1 thorpej BARF("OPIOCGET", strerror(errno));
112 1.1 thorpej
113 1.1 thorpej if (opio.op_buflen <= 0) {
114 1.1 thorpej printf("nothing available for %s\n");
115 1.1 thorpej goto out;
116 1.1 thorpej }
117 1.1 thorpej
118 1.1 thorpej if (ex->ex_keyword != NULL)
119 1.1 thorpej (*ex->ex_handler)(ex, &opio, NULL);
120 1.1 thorpej else
121 1.1 thorpej printf("%s\n", opio.op_buf);
122 1.1 thorpej }
123 1.1 thorpej out:
124 1.1 thorpej if (ex->ex_keyword != NULL)
125 1.1 thorpej (*ex->ex_handler)(ex, &opio, arg);
126 1.1 thorpej else {
127 1.1 thorpej opio.op_buf = arg;
128 1.1 thorpej opio.op_buflen = strlen(arg);
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej if (ioctl(fd, OPIOCSET, (char *)&opio) < 0)
132 1.1 thorpej BARF("invalid keyword", keyword);
133 1.1 thorpej
134 1.1 thorpej if (verbose) {
135 1.1 thorpej printf("new: ");
136 1.1 thorpej if (ex->ex_keyword != NULL)
137 1.1 thorpej (*ex->ex_handler)(ex, &opio, NULL);
138 1.1 thorpej else
139 1.1 thorpej printf("%s\n", opio.op_buf);
140 1.1 thorpej }
141 1.1 thorpej } else {
142 1.1 thorpej opio.op_buf = &opio_buf[0];
143 1.1 thorpej opio.op_buflen = sizeof(opio_buf);
144 1.1 thorpej if (ioctl(fd, OPIOCGET, (char *)&opio) < 0)
145 1.1 thorpej BARF("OPIOCGET", strerror(errno));
146 1.1 thorpej
147 1.1 thorpej if (opio.op_buflen <= 0) {
148 1.3 mrg (void)snprintf(err_str, sizeof err_str,
149 1.3 mrg "nothing available for %s", keyword);
150 1.1 thorpej return (err_str);
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej if (ex->ex_keyword != NULL)
154 1.1 thorpej (*ex->ex_handler)(ex, &opio, NULL);
155 1.1 thorpej else
156 1.1 thorpej printf("%s=%s\n", keyword, opio.op_buf);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej (void)close(fd);
160 1.1 thorpej return (NULL);
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej /* ARGSUSED */
164 1.1 thorpej static void
165 1.1 thorpej op_notsupp(exent, opiop, arg)
166 1.1 thorpej struct extabent *exent;
167 1.1 thorpej struct opiocdesc *opiop;
168 1.1 thorpej char *arg;
169 1.1 thorpej {
170 1.1 thorpej
171 1.1 thorpej warnx("property `%s' not yet supported", exent->ex_keyword);
172 1.1 thorpej }
173 1.1 thorpej
174 1.1 thorpej /*
175 1.1 thorpej * XXX: This code is quite ugly. You have been warned.
176 1.1 thorpej * (Really! This is the only way I could get it to work!)
177 1.1 thorpej */
178 1.1 thorpej void
179 1.1 thorpej op_dump()
180 1.1 thorpej {
181 1.1 thorpej struct opiocdesc opio1, opio2;
182 1.1 thorpej struct extabent *ex;
183 1.1 thorpej char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE];
184 1.1 thorpej int fd, optnode;
185 1.1 thorpej
186 1.1 thorpej if ((fd = open(path_openprom, O_RDONLY, 0640)) < 0)
187 1.1 thorpej err(1, "open: %s", path_openprom);
188 1.1 thorpej
189 1.1 thorpej if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0)
190 1.1 thorpej err(1, "OPIOCGETOPTNODE");
191 1.1 thorpej
192 1.1 thorpej bzero(&opio1, sizeof(opio1));
193 1.1 thorpej
194 1.1 thorpej /* This will grab the first property name from OPIOCNEXTPROP. */
195 1.1 thorpej bzero(buf1, sizeof(buf1));
196 1.1 thorpej bzero(buf2, sizeof(buf2));
197 1.1 thorpej
198 1.1 thorpej opio1.op_nodeid = opio2.op_nodeid = optnode;
199 1.1 thorpej
200 1.1 thorpej opio1.op_name = buf1;
201 1.1 thorpej opio1.op_buf = buf2;
202 1.1 thorpej
203 1.1 thorpej opio2.op_name = buf3;
204 1.1 thorpej opio2.op_buf = buf4;
205 1.1 thorpej
206 1.1 thorpej /*
207 1.1 thorpej * For reference: opio1 is for obtaining the name. Pass the
208 1.1 thorpej * name of the last property read in op_name, and the next one
209 1.1 thorpej * will be returned in op_buf. To get the first name, pass
210 1.1 thorpej * an empty string. There are no more properties when an
211 1.1 thorpej * empty string is returned.
212 1.1 thorpej *
213 1.1 thorpej * opio2 is for obtaining the value associated with that name.
214 1.1 thorpej * For some crazy reason, it seems as if we need to do all
215 1.1 thorpej * of that gratuitious zapping and copying. *sigh*
216 1.1 thorpej */
217 1.1 thorpej for (;;) {
218 1.1 thorpej opio1.op_namelen = strlen(opio1.op_name);
219 1.1 thorpej opio1.op_buflen = sizeof(buf2);
220 1.1 thorpej
221 1.1 thorpej if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) < 0)
222 1.1 thorpej err(1, "ioctl: OPIOCNEXTPROP");
223 1.1 thorpej
224 1.1 thorpej /*
225 1.1 thorpej * The name of the property we wish to get the
226 1.1 thorpej * value for has been stored in the value field
227 1.1 thorpej * of opio1. If the length of the name is 0, there
228 1.1 thorpej * are no more properties left.
229 1.1 thorpej */
230 1.3 mrg strcpy(opio2.op_name, opio1.op_buf); /* XXX strcpy is safe */
231 1.1 thorpej opio2.op_namelen = strlen(opio2.op_name);
232 1.1 thorpej
233 1.1 thorpej if (opio2.op_namelen == 0) {
234 1.1 thorpej (void)close(fd);
235 1.1 thorpej return;
236 1.1 thorpej }
237 1.1 thorpej
238 1.1 thorpej bzero(opio2.op_buf, sizeof(buf4));
239 1.1 thorpej opio2.op_buflen = sizeof(buf4);
240 1.1 thorpej
241 1.1 thorpej if (ioctl(fd, OPIOCGET, (char *)&opio2) < 0)
242 1.1 thorpej err(1, "ioctl: OPIOCGET");
243 1.1 thorpej
244 1.1 thorpej for (ex = opextab; ex->ex_keyword != NULL; ++ex)
245 1.1 thorpej if (strcmp(ex->ex_keyword, opio2.op_name) == 0)
246 1.1 thorpej break;
247 1.1 thorpej
248 1.1 thorpej if (ex->ex_keyword != NULL)
249 1.1 thorpej (*ex->ex_handler)(ex, &opio2, NULL);
250 1.1 thorpej else
251 1.1 thorpej printf("%s=%s\n", opio2.op_name, opio2.op_buf);
252 1.1 thorpej
253 1.1 thorpej /*
254 1.1 thorpej * Place the name of the last read value back into
255 1.1 thorpej * opio1 so that we may obtain the next name.
256 1.1 thorpej */
257 1.1 thorpej bzero(opio1.op_name, sizeof(buf1));
258 1.1 thorpej bzero(opio1.op_buf, sizeof(buf2));
259 1.3 mrg strcpy(opio1.op_name, opio2.op_name); /* XXX strcpy is safe */
260 1.1 thorpej }
261 1.1 thorpej /* NOTREACHED */
262 1.1 thorpej }
263