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