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