1 /* $NetBSD: main.c,v 1.17 2007/03/01 16:49:48 garbled 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/cdefs.h> 40 #ifndef lint 41 __COPYRIGHT( 42 "@(#) Copyright (c) 1996 The NetBSD Foundation, Inc. All rights reserved."); 43 __RCSID("$NetBSD: main.c,v 1.17 2007/03/01 16:49:48 garbled Exp $"); 44 #endif 45 46 #include <sys/param.h> 47 #include <err.h> 48 #include <string.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <unistd.h> 52 53 #ifdef __sun__ 54 #include <machine/eeprom.h> 55 #endif 56 57 #include "defs.h" 58 #include "pathnames.h" 59 60 #if defined(__sparc__) 61 # define USE_OPENPROM 62 # if defined(__arch64__) 63 # define ee_action(a,b) 64 # define ee_dump() 65 # define ee_updatechecksums() (void)0 66 # define check_for_openprom() 1 67 # endif 68 #endif 69 70 int main (int, char *[]); 71 static void action (char *); 72 static void dump_prom (void); 73 static void usage (void); 74 75 char *path_eeprom = _PATH_EEPROM; 76 char *path_openprom = _PATH_OPENPROM; 77 char *path_openfirm = _PATH_OPENFIRM; 78 char *path_prepnvram = _PATH_PREPNVRAM; 79 int fix_checksum = 0; 80 int ignore_checksum = 0; 81 int update_checksums = 0; 82 int cksumfail = 0; 83 u_short writecount; 84 int eval = 0; 85 #ifdef USE_OPENPROM 86 int verbose = 0; 87 int use_openprom; 88 #endif 89 #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM) 90 int verbose=0; 91 #endif 92 93 int 94 main(argc, argv) 95 int argc; 96 char *argv[]; 97 { 98 int ch, do_stdin = 0; 99 char *cp, line[BUFSIZE]; 100 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 101 char *optstring = "-cf:iv"; 102 #else 103 char *optstring = "-cf:i"; 104 #endif /* USE_OPENPROM */ 105 106 while ((ch = getopt(argc, argv, optstring)) != -1) 107 switch (ch) { 108 case '-': 109 do_stdin = 1; 110 break; 111 112 case 'c': 113 fix_checksum = 1; 114 break; 115 116 case 'f': 117 path_eeprom = path_openprom = optarg; 118 break; 119 120 case 'i': 121 ignore_checksum = 1; 122 break; 123 124 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 125 case 'v': 126 verbose = 1; 127 break; 128 #endif /* USE_OPENPROM */ 129 130 case '?': 131 default: 132 usage(); 133 } 134 argc -= optind; 135 argv += optind; 136 137 #ifdef USE_OPENPROM 138 use_openprom = check_for_openprom(); 139 140 if (use_openprom == 0) { 141 #endif /* USE_OPENPROM */ 142 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM) 143 ee_verifychecksums(); 144 if (fix_checksum || cksumfail) 145 exit(cksumfail); 146 #endif 147 #ifdef USE_OPENPROM 148 } 149 #endif /* USE_OPENPROM */ 150 151 if (do_stdin) { 152 while (fgets(line, BUFSIZE, stdin) != NULL) { 153 if (line[0] == '\n') 154 continue; 155 if ((cp = strrchr(line, '\n')) != NULL) 156 *cp = '\0'; 157 action(line); 158 } 159 if (ferror(stdin)) 160 err(++eval, "stdin"); 161 } else { 162 if (argc == 0) { 163 dump_prom(); 164 exit(eval + cksumfail); 165 } 166 167 while (argc) { 168 action(*argv); 169 ++argv; 170 --argc; 171 } 172 } 173 174 #ifdef USE_OPENPROM 175 if (use_openprom == 0) 176 #endif /* USE_OPENPROM */ 177 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM) 178 if (update_checksums) { 179 ++writecount; 180 ee_updatechecksums(); 181 } 182 183 exit(eval + cksumfail); 184 #endif 185 return 0; 186 } 187 188 /* 189 * Separate the keyword from the argument (if any), find the keyword in 190 * the table, and call the corresponding handler function. 191 */ 192 static void 193 action(line) 194 char *line; 195 { 196 char *keyword, *arg; 197 198 keyword = strdup(line); 199 if ((arg = strrchr(keyword, '=')) != NULL) 200 *arg++ = '\0'; 201 202 #ifdef USE_PREPNVRAM 203 prep_action(keyword, arg); 204 #else 205 #ifdef USE_OPENFIRM 206 of_action(keyword, arg); 207 #else 208 #ifdef USE_OPENPROM 209 if (use_openprom) 210 op_action(keyword, arg); 211 else 212 #endif /* USE_OPENPROM */ 213 ee_action(keyword, arg); 214 #endif /* USE_OPENFIRM */ 215 #endif /* USE_PREPNVRAM */ 216 } 217 218 /* 219 * Dump the contents of the prom corresponding to all known keywords. 220 */ 221 static void 222 dump_prom() 223 { 224 225 #ifdef USE_PREPNVRAM 226 prep_dump(); 227 #else 228 #ifdef USE_OPENFIRM 229 of_dump(); 230 #else 231 #ifdef USE_OPENPROM 232 if (use_openprom) 233 /* 234 * We have a special dump routine for this. 235 */ 236 op_dump(); 237 else 238 #endif /* USE_OPENPROM */ 239 ee_dump(); 240 #endif /* USE_OPENFIRM */ 241 #endif /* USE_PREPNVRAM */ 242 } 243 244 static void 245 usage() 246 { 247 248 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 249 fprintf(stderr, "usage: %s %s\n", getprogname(), 250 "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]"); 251 #else 252 fprintf(stderr, "usage: %s %s\n", getprogname(), 253 "[-] [-c] [-f device] [-i] [field[=value] ...]"); 254 #endif /* __us */ 255 exit(1); 256 } 257