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