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