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