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