main.c revision 1.18 1 /* $NetBSD: main.c,v 1.18 2008/04/28 20:24:15 martin 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(
35 "@(#) Copyright (c) 1996 The NetBSD Foundation, Inc. All rights reserved.");
36 __RCSID("$NetBSD: main.c,v 1.18 2008/04/28 20:24:15 martin 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 char *path_eeprom = _PATH_EEPROM;
69 char *path_openprom = _PATH_OPENPROM;
70 char *path_openfirm = _PATH_OPENFIRM;
71 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 char *optstring = "-cf:iv";
95 #else
96 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 #endif /* USE_OPENFIRM */
208 #endif /* USE_PREPNVRAM */
209 }
210
211 /*
212 * Dump the contents of the prom corresponding to all known keywords.
213 */
214 static void
215 dump_prom()
216 {
217
218 #ifdef USE_PREPNVRAM
219 prep_dump();
220 #else
221 #ifdef USE_OPENFIRM
222 of_dump();
223 #else
224 #ifdef USE_OPENPROM
225 if (use_openprom)
226 /*
227 * We have a special dump routine for this.
228 */
229 op_dump();
230 else
231 #endif /* USE_OPENPROM */
232 ee_dump();
233 #endif /* USE_OPENFIRM */
234 #endif /* USE_PREPNVRAM */
235 }
236
237 static void
238 usage()
239 {
240
241 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
242 fprintf(stderr, "usage: %s %s\n", getprogname(),
243 "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
244 #else
245 fprintf(stderr, "usage: %s %s\n", getprogname(),
246 "[-] [-c] [-f device] [-i] [field[=value] ...]");
247 #endif /* __us */
248 exit(1);
249 }
250