1 /* $NetBSD: main.c,v 1.21 2009/04/29 09:13:58 nakayama 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1996\ 35 The NetBSD Foundation, Inc. All rights reserved."); 36 __RCSID("$NetBSD: main.c,v 1.21 2009/04/29 09:13:58 nakayama Exp $"); 37 #endif 38 39 #include <sys/param.h> 40 #include <err.h> 41 #include <string.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 #ifdef __sun__ 47 #include <machine/eeprom.h> 48 #endif 49 50 #include "defs.h" 51 #include "pathnames.h" 52 53 #if defined(__sparc__) 54 # define USE_OPENPROM 55 # if defined(__arch64__) 56 # define ee_action(a,b) 57 # define ee_dump() 58 # define ee_updatechecksums() (void)0 59 # define check_for_openprom() 1 60 # endif 61 #endif 62 63 int main (int, char *[]); 64 static void action (char *); 65 static void dump_prom (void); 66 static void usage (void); 67 68 const char *path_eeprom = _PATH_EEPROM; 69 const char *path_openprom = _PATH_OPENPROM; 70 const char *path_openfirm = _PATH_OPENFIRM; 71 const char *path_prepnvram = _PATH_PREPNVRAM; 72 int fix_checksum = 0; 73 int ignore_checksum = 0; 74 int update_checksums = 0; 75 int cksumfail = 0; 76 u_short writecount; 77 int eval = 0; 78 #ifdef USE_OPENPROM 79 int verbose = 0; 80 int use_openprom; 81 #endif 82 #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM) 83 int verbose=0; 84 #endif 85 86 int 87 main(argc, argv) 88 int argc; 89 char *argv[]; 90 { 91 int ch, do_stdin = 0; 92 char *cp, line[BUFSIZE]; 93 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 94 const char *optstring = "-cf:iv"; 95 #else 96 const char *optstring = "-cf:i"; 97 #endif /* USE_OPENPROM */ 98 99 while ((ch = getopt(argc, argv, optstring)) != -1) 100 switch (ch) { 101 case '-': 102 do_stdin = 1; 103 break; 104 105 case 'c': 106 fix_checksum = 1; 107 break; 108 109 case 'f': 110 path_eeprom = path_openprom = optarg; 111 break; 112 113 case 'i': 114 ignore_checksum = 1; 115 break; 116 117 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 118 case 'v': 119 verbose = 1; 120 break; 121 #endif /* USE_OPENPROM */ 122 123 case '?': 124 default: 125 usage(); 126 } 127 argc -= optind; 128 argv += optind; 129 130 #ifdef USE_OPENPROM 131 use_openprom = check_for_openprom(); 132 133 if (use_openprom == 0) { 134 #endif /* USE_OPENPROM */ 135 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM) 136 ee_verifychecksums(); 137 if (fix_checksum || cksumfail) 138 exit(cksumfail); 139 #endif 140 #ifdef USE_OPENPROM 141 } 142 #endif /* USE_OPENPROM */ 143 144 if (do_stdin) { 145 while (fgets(line, BUFSIZE, stdin) != NULL) { 146 if (line[0] == '\n') 147 continue; 148 if ((cp = strrchr(line, '\n')) != NULL) 149 *cp = '\0'; 150 action(line); 151 } 152 if (ferror(stdin)) 153 err(++eval, "stdin"); 154 } else { 155 if (argc == 0) { 156 dump_prom(); 157 exit(eval + cksumfail); 158 } 159 160 while (argc) { 161 action(*argv); 162 ++argv; 163 --argc; 164 } 165 } 166 167 #ifdef USE_OPENPROM 168 if (use_openprom == 0) 169 #endif /* USE_OPENPROM */ 170 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM) 171 if (update_checksums) { 172 ++writecount; 173 ee_updatechecksums(); 174 } 175 176 exit(eval + cksumfail); 177 #endif 178 return 0; 179 } 180 181 /* 182 * Separate the keyword from the argument (if any), find the keyword in 183 * the table, and call the corresponding handler function. 184 */ 185 static void 186 action(line) 187 char *line; 188 { 189 char *keyword, *arg; 190 191 keyword = strdup(line); 192 if ((arg = strrchr(keyword, '=')) != NULL) 193 *arg++ = '\0'; 194 195 #ifdef USE_PREPNVRAM 196 prep_action(keyword, arg); 197 #else 198 #ifdef USE_OPENFIRM 199 of_action(keyword, arg); 200 #else 201 #ifdef USE_OPENPROM 202 if (use_openprom) 203 op_action(keyword, arg); 204 else { 205 #endif /* USE_OPENPROM */ 206 ee_action(keyword, arg); 207 #ifdef USE_OPENPROM 208 } 209 #endif /* USE_OPENPROM */ 210 #endif /* USE_OPENFIRM */ 211 #endif /* USE_PREPNVRAM */ 212 } 213 214 /* 215 * Dump the contents of the prom corresponding to all known keywords. 216 */ 217 static void 218 dump_prom() 219 { 220 221 #ifdef USE_PREPNVRAM 222 prep_dump(); 223 #else 224 #ifdef USE_OPENFIRM 225 of_dump(); 226 #else 227 #ifdef USE_OPENPROM 228 if (use_openprom) 229 /* 230 * We have a special dump routine for this. 231 */ 232 op_dump(); 233 else { 234 #endif /* USE_OPENPROM */ 235 ee_dump(); 236 #ifdef USE_OPENPROM 237 } 238 #endif /* USE_OPENPROM */ 239 #endif /* USE_OPENFIRM */ 240 #endif /* USE_PREPNVRAM */ 241 } 242 243 static void 244 usage() 245 { 246 247 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM) 248 fprintf(stderr, "usage: %s %s\n", getprogname(), 249 "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]"); 250 #else 251 fprintf(stderr, "usage: %s %s\n", getprogname(), 252 "[-] [-c] [-f device] [-i] [field[=value] ...]"); 253 #endif /* __us */ 254 exit(1); 255 } 256