1 /* $OpenBSD: main.c,v 1.7 2001/09/20 20:42:25 jason Exp $ */ 2 /* $NetBSD: main.c,v 1.3 1996/05/16 16:00:55 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/param.h> 41 #include <err.h> 42 #include <string.h> 43 #include <stdio.h> 44 #include <unistd.h> 45 46 #ifdef __sparc__ 47 #include <fcntl.h> 48 #include <kvm.h> 49 #include <limits.h> 50 #include <nlist.h> 51 52 #include <machine/openpromio.h> 53 54 struct nlist nl[] = { 55 { "_cputyp" }, 56 #define SYM_CPUTYP 0 57 { NULL }, 58 }; 59 60 static char *system = NULL; 61 #endif /* __sparc__ */ 62 63 #include <machine/eeprom.h> 64 65 #include "defs.h" 66 67 struct keytabent eekeytab[] = { 68 { "hwupdate", 0x10, ee_hwupdate }, 69 { "memsize", 0x14, ee_num8 }, 70 { "memtest", 0x15, ee_num8 }, 71 #ifdef __sparc64__ 72 { "scrsize", 0x16, ee_notsupp }, 73 #else 74 { "scrsize", 0x16, ee_screensize }, 75 #endif 76 { "watchdog_reboot", 0x17, ee_truefalse }, 77 { "default_boot", 0x18, ee_truefalse }, 78 { "bootdev", 0x19, ee_bootdev }, 79 { "kbdtype", 0x1e, ee_kbdtype }, 80 #ifdef __sparc64__ 81 { "console", 0x1f, ee_notsupp }, 82 #else 83 { "console", 0x1f, ee_constype }, 84 #endif 85 { "keyclick", 0x21, ee_truefalse }, 86 { "diagdev", 0x22, ee_bootdev }, 87 { "diagpath", 0x28, ee_diagpath }, 88 { "columns", 0x50, ee_num8 }, 89 { "rows", 0x51, ee_num8 }, 90 { "ttya_use_baud", 0x58, ee_truefalse }, 91 { "ttya_baud", 0x59, ee_num16 }, 92 { "ttya_no_rtsdtr", 0x5b, ee_truefalse }, 93 { "ttyb_use_baud", 0x60, ee_truefalse }, 94 { "ttyb_baud", 0x61, ee_num16 }, 95 { "ttyb_no_rtsdtr", 0x63, ee_truefalse }, 96 { "banner", 0x68, ee_banner }, 97 { "secure", 0, ee_notsupp }, 98 { "bad_login", 0, ee_notsupp }, 99 { "password", 0, ee_notsupp }, 100 { NULL, 0, ee_notsupp }, 101 }; 102 103 static void action __P((char *)); 104 static void dump_prom __P((void)); 105 static void usage __P((void)); 106 #ifdef __sparc__ 107 static int getcputype __P((void)); 108 #endif /* __sparc__ */ 109 110 char *path_eeprom = "/dev/eeprom"; 111 char *path_openprom = "/dev/openprom"; 112 int fix_checksum = 0; 113 int ignore_checksum = 0; 114 int update_checksums = 0; 115 int cksumfail = 0; 116 u_short writecount; 117 int eval = 0; 118 int use_openprom = 0; 119 int verbose = 0; 120 121 extern char *__progname; 122 123 int 124 main(argc, argv) 125 int argc; 126 char **argv; 127 { 128 int ch, do_stdin = 0; 129 char *cp, line[BUFSIZE]; 130 #ifdef __sparc__ 131 char *optstring = "-cf:ivN:"; 132 #else 133 char *optstring = "-cf:i"; 134 #endif /* __sparc__ */ 135 136 while ((ch = getopt(argc, argv, optstring)) != -1) 137 switch (ch) { 138 case '-': 139 do_stdin = 1; 140 break; 141 142 case 'c': 143 fix_checksum = 1; 144 break; 145 146 case 'f': 147 path_eeprom = path_openprom = optarg; 148 break; 149 150 case 'i': 151 ignore_checksum = 1; 152 break; 153 #ifdef __sparc__ 154 case 'v': 155 verbose = 1; 156 break; 157 158 case 'N': 159 system = optarg; 160 break; 161 162 #endif /* __sparc__ */ 163 164 case '?': 165 default: 166 usage(); 167 } 168 argc -= optind; 169 argv += optind; 170 171 #ifdef __sparc__ 172 if (system != NULL) { 173 setegid(getgid()); 174 setgid(getgid()); 175 } 176 if (getcputype() != CPU_SUN4) 177 use_openprom = 1; 178 #endif /* __sparc__ */ 179 180 if (use_openprom == 0) { 181 ee_verifychecksums(); 182 if (fix_checksum || cksumfail) 183 exit(cksumfail); 184 } 185 186 if (do_stdin) { 187 while (fgets(line, BUFSIZE, stdin) != NULL) { 188 if (line[0] == '\n') 189 continue; 190 if ((cp = strrchr(line, '\n')) != NULL) 191 *cp = '\0'; 192 action(line); 193 } 194 if (ferror(stdin)) 195 err(++eval, "stdin"); 196 } else { 197 if (argc == 0) { 198 dump_prom(); 199 exit(eval + cksumfail); 200 } 201 202 while (argc) { 203 action(*argv); 204 ++argv; 205 --argc; 206 } 207 } 208 209 if (use_openprom == 0) 210 if (update_checksums) { 211 ++writecount; 212 ee_updatechecksums(); 213 } 214 215 exit(eval + cksumfail); 216 } 217 218 #ifdef __sparc__ 219 #define KVM_ABORT(kd, str) { \ 220 (void)kvm_close((kd)); \ 221 errx(1, "%s: %s", (str), kvm_geterr((kd))); \ 222 } 223 224 static int 225 getcputype() 226 { 227 char errbuf[_POSIX2_LINE_MAX]; 228 int cputype; 229 kvm_t *kd; 230 231 bzero(errbuf, sizeof(errbuf)); 232 233 if ((kd = kvm_openfiles(system, NULL, NULL, O_RDONLY, errbuf)) == NULL) 234 errx(1, "can't open kvm: %s", errbuf); 235 236 setegid(getgid()); 237 setgid(getgid()); 238 239 if (kvm_nlist(kd, nl)) 240 KVM_ABORT(kd, "can't read symbol table"); 241 242 if (kvm_read(kd, nl[SYM_CPUTYP].n_value, (char *)&cputype, 243 sizeof(cputype)) != sizeof(cputype)) 244 KVM_ABORT(kd, "can't determine cpu type"); 245 246 (void)kvm_close(kd); 247 return (cputype); 248 } 249 #endif /* __sparc__ */ 250 251 /* 252 * Separate the keyword from the argument (if any), find the keyword in 253 * the table, and call the corresponding handler function. 254 */ 255 static void 256 action(line) 257 char *line; 258 { 259 char *keyword, *arg, *cp; 260 struct keytabent *ktent; 261 262 keyword = strdup(line); 263 if ((arg = strrchr(keyword, '=')) != NULL) 264 *arg++ = '\0'; 265 266 #ifdef __sparc__ 267 if (use_openprom) { 268 /* 269 * The whole point of the Openprom is that one 270 * isn't required to know the keywords. With this 271 * in mind, we just dump the whole thing off to 272 * the generic op_handler. 273 */ 274 if ((cp = op_handler(keyword, arg)) != NULL) 275 warnx("%s", cp); 276 return; 277 } else 278 #endif /* __sparc__ */ 279 for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent) { 280 if (strcmp(ktent->kt_keyword, keyword) == 0) { 281 (*ktent->kt_handler)(ktent, arg); 282 return; 283 } 284 } 285 286 warnx("unknown keyword %s", keyword); 287 ++eval; 288 } 289 290 /* 291 * Dump the contents of the prom corresponding to all known keywords. 292 */ 293 static void 294 dump_prom() 295 { 296 struct keytabent *ktent; 297 298 #ifdef __sparc__ 299 if (use_openprom) { 300 /* 301 * We have a special dump routine for this. 302 */ 303 op_dump(); 304 } else 305 #endif /* __sparc__ */ 306 for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent) 307 (*ktent->kt_handler)(ktent, NULL); 308 } 309 310 static void 311 usage() 312 { 313 314 #ifdef __sparc__ 315 fprintf(stderr, "usage: %s %s %s\n", __progname, 316 "[-] [-c] [-f device] [-i] [-v]", 317 "[-N system] [field[=value] ...]"); 318 #else 319 fprintf(stderr, "usage: %s %s\n", __progname, 320 "[-] [-c] [-f device] [-i] [field[=value] ...]"); 321 #endif /* __sparc__ */ 322 exit(1); 323 } 324