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