1 /* $OpenBSD: ophandlers.c,v 1.13 2014/11/18 20:54:28 krw 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 * 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, 0640)) < 0) 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) < 0) 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) < 0) 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) < 0) 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) < 0) 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 /* ARGSUSED */ 162 static void 163 op_notsupp(struct extabent *exent, struct opiocdesc *opiop, char *arg) 164 { 165 166 warnx("property `%s' not yet supported", exent->ex_keyword); 167 } 168 169 /* 170 * XXX: This code is quite ugly. You have been warned. 171 * (Really! This is the only way I could get it to work!) 172 */ 173 void 174 op_dump(void) 175 { 176 struct opiocdesc opio1, opio2; 177 struct extabent *ex; 178 char buf1[BUFSIZE], buf2[BUFSIZE], buf3[BUFSIZE], buf4[BUFSIZE]; 179 int fd, optnode; 180 181 if ((fd = open(path_openprom, O_RDONLY, 0640)) < 0) 182 err(1, "open: %s", path_openprom); 183 184 if (ioctl(fd, OPIOCGETOPTNODE, (char *)&optnode) < 0) 185 err(1, "OPIOCGETOPTNODE"); 186 187 bzero(&opio1, sizeof(opio1)); 188 189 /* This will grab the first property name from OPIOCNEXTPROP. */ 190 bzero(buf1, sizeof(buf1)); 191 bzero(buf2, sizeof(buf2)); 192 193 opio1.op_nodeid = opio2.op_nodeid = optnode; 194 195 opio1.op_name = buf1; 196 opio1.op_buf = buf2; 197 198 opio2.op_name = buf3; 199 opio2.op_buf = buf4; 200 201 /* 202 * For reference: opio1 is for obtaining the name. Pass the 203 * name of the last property read in op_name, and the next one 204 * will be returned in op_buf. To get the first name, pass 205 * an empty string. There are no more properties when an 206 * empty string is returned. 207 * 208 * opio2 is for obtaining the value associated with that name. 209 * For some crazy reason, it seems as if we need to do all 210 * of that gratuitious zapping and copying. *sigh* 211 */ 212 for (;;) { 213 opio1.op_namelen = strlen(opio1.op_name); 214 opio1.op_buflen = sizeof(buf2); 215 216 if (ioctl(fd, OPIOCNEXTPROP, (char *)&opio1) < 0) 217 err(1, "ioctl: OPIOCNEXTPROP"); 218 219 /* 220 * The name of the property we wish to get the 221 * value for has been stored in the value field 222 * of opio1. If the length of the name is 0, there 223 * are no more properties left. 224 */ 225 strlcpy(opio2.op_name, opio1.op_buf, sizeof(buf3)); 226 opio2.op_namelen = strlen(opio2.op_name); 227 228 if (opio2.op_namelen == 0) { 229 (void)close(fd); 230 return; 231 } 232 233 bzero(opio2.op_buf, sizeof(buf4)); 234 opio2.op_buflen = sizeof(buf4); 235 236 if (ioctl(fd, OPIOCGET, (char *)&opio2) < 0) 237 err(1, "ioctl: OPIOCGET"); 238 239 for (ex = opextab; ex->ex_keyword != NULL; ++ex) 240 if (strcmp(ex->ex_keyword, opio2.op_name) == 0) 241 break; 242 243 if (ex->ex_keyword != NULL) 244 (*ex->ex_handler)(ex, &opio2, NULL); 245 else { 246 printf("%s=", opio2.op_name); 247 op_print(opio2.op_buf); 248 } 249 250 /* 251 * Place the name of the last read value back into 252 * opio1 so that we may obtain the next name. 253 */ 254 bzero(opio1.op_name, sizeof(buf1)); 255 bzero(opio1.op_buf, sizeof(buf2)); 256 strlcpy(opio1.op_name, opio2.op_name, sizeof(buf1)); 257 } 258 /* NOTREACHED */ 259 } 260 261 static void 262 op_print(char *op_buf) 263 { 264 char *vistr; 265 size_t size; 266 267 size = 1 + 4 * strlen(op_buf); 268 vistr = (char *)malloc(size); 269 if (vistr == NULL) 270 printf("(out of memory)\n"); 271 else { 272 strnvis(vistr, op_buf, size, VIS_NL | VIS_TAB | VIS_OCTAL); 273 printf("%s\n", vistr); 274 free(vistr); 275 } 276 } 277