1 /* $NetBSD: main.c,v 1.16 2006/08/16 03:24:57 macallan 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.16 2006/08/16 03:24:57 macallan 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 int fix_checksum = 0; 79 int ignore_checksum = 0; 80 int update_checksums = 0; 81 int cksumfail = 0; 82 u_short writecount; 83 int eval = 0; 84 #ifdef USE_OPENPROM 85 int verbose = 0; 86 int use_openprom; 87 #endif 88 #ifdef USE_OPENFIRM 89 int verbose=0; 90 #endif 91 92 int 93 main(argc, argv) 94 int argc; 95 char *argv[]; 96 { 97 int ch, do_stdin = 0; 98 char *cp, line[BUFSIZE]; 99 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) 100 char *optstring = "-cf:iv"; 101 #else 102 char *optstring = "-cf:i"; 103 #endif /* USE_OPENPROM */ 104 105 while ((ch = getopt(argc, argv, optstring)) != -1) 106 switch (ch) { 107 case '-': 108 do_stdin = 1; 109 break; 110 111 case 'c': 112 fix_checksum = 1; 113 break; 114 115 case 'f': 116 path_eeprom = path_openprom = optarg; 117 break; 118 119 case 'i': 120 ignore_checksum = 1; 121 break; 122 123 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) 124 case 'v': 125 verbose = 1; 126 break; 127 #endif /* USE_OPENPROM */ 128 129 case '?': 130 default: 131 usage(); 132 } 133 argc -= optind; 134 argv += optind; 135 136 #ifdef USE_OPENPROM 137 use_openprom = check_for_openprom(); 138 139 if (use_openprom == 0) { 140 #endif /* USE_OPENPROM */ 141 #ifndef USE_OPENFIRM 142 ee_verifychecksums(); 143 if (fix_checksum || cksumfail) 144 exit(cksumfail); 145 #endif 146 #ifdef USE_OPENPROM 147 } 148 #endif /* USE_OPENPROM */ 149 150 if (do_stdin) { 151 while (fgets(line, BUFSIZE, stdin) != NULL) { 152 if (line[0] == '\n') 153 continue; 154 if ((cp = strrchr(line, '\n')) != NULL) 155 *cp = '\0'; 156 action(line); 157 } 158 if (ferror(stdin)) 159 err(++eval, "stdin"); 160 } else { 161 if (argc == 0) { 162 dump_prom(); 163 exit(eval + cksumfail); 164 } 165 166 while (argc) { 167 action(*argv); 168 ++argv; 169 --argc; 170 } 171 } 172 173 #ifdef USE_OPENPROM 174 if (use_openprom == 0) 175 #endif /* USE_OPENPROM */ 176 #ifndef USE_OPENFIRM 177 if (update_checksums) { 178 ++writecount; 179 ee_updatechecksums(); 180 } 181 182 exit(eval + cksumfail); 183 #endif 184 return 0; 185 } 186 187 /* 188 * Separate the keyword from the argument (if any), find the keyword in 189 * the table, and call the corresponding handler function. 190 */ 191 static void 192 action(line) 193 char *line; 194 { 195 char *keyword, *arg; 196 197 keyword = strdup(line); 198 if ((arg = strrchr(keyword, '=')) != NULL) 199 *arg++ = '\0'; 200 201 #ifdef USE_OPENFIRM 202 of_action(keyword, arg); 203 #else 204 #ifdef USE_OPENPROM 205 if (use_openprom) 206 op_action(keyword, arg); 207 else 208 #endif /* USE_OPENPROM */ 209 ee_action(keyword, arg); 210 #endif /* USE_OPENFIRM */ 211 } 212 213 /* 214 * Dump the contents of the prom corresponding to all known keywords. 215 */ 216 static void 217 dump_prom() 218 { 219 220 #ifdef USE_OPENFIRM 221 of_dump(); 222 #else 223 #ifdef USE_OPENPROM 224 if (use_openprom) 225 /* 226 * We have a special dump routine for this. 227 */ 228 op_dump(); 229 else 230 #endif /* USE_OPENPROM */ 231 ee_dump(); 232 #endif /* USE_OPENFIRM */ 233 } 234 235 static void 236 usage() 237 { 238 239 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) 240 fprintf(stderr, "usage: %s %s\n", getprogname(), 241 "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]"); 242 #else 243 fprintf(stderr, "usage: %s %s\n", getprogname(), 244 "[-] [-c] [-f device] [-i] [field[=value] ...]"); 245 #endif /* __us */ 246 exit(1); 247 } 248