Restrict ncdcs.debug and man pages to MKBINUTILS=yes builds.
[netbsd-mini2440.git] / usr.sbin / eeprom / main.c
blob6a3ba92343b29932a3da3addf4900d0d732f654a
1 /* $NetBSD: main.c,v 1.20 2009/04/26 01:51:07 lukem Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
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.20 2009/04/26 01:51:07 lukem Exp $");
37 #endif
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>
46 #ifdef __sun__
47 #include <machine/eeprom.h>
48 #endif
50 #include "defs.h"
51 #include "pathnames.h"
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
63 int main (int, char *[]);
64 static void action (char *);
65 static void dump_prom (void);
66 static void usage (void);
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
86 int
87 main(argc, argv)
88 int argc;
89 char *argv[];
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 */
99 while ((ch = getopt(argc, argv, optstring)) != -1)
100 switch (ch) {
101 case '-':
102 do_stdin = 1;
103 break;
105 case 'c':
106 fix_checksum = 1;
107 break;
109 case 'f':
110 path_eeprom = path_openprom = optarg;
111 break;
113 case 'i':
114 ignore_checksum = 1;
115 break;
117 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
118 case 'v':
119 verbose = 1;
120 break;
121 #endif /* USE_OPENPROM */
123 case '?':
124 default:
125 usage();
127 argc -= optind;
128 argv += optind;
130 #ifdef USE_OPENPROM
131 use_openprom = check_for_openprom();
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
142 #endif /* USE_OPENPROM */
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);
152 if (ferror(stdin))
153 err(++eval, "stdin");
154 } else {
155 if (argc == 0) {
156 dump_prom();
157 exit(eval + cksumfail);
160 while (argc) {
161 action(*argv);
162 ++argv;
163 --argc;
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();
176 exit(eval + cksumfail);
177 #endif
178 return 0;
182 * Separate the keyword from the argument (if any), find the keyword in
183 * the table, and call the corresponding handler function.
185 static void
186 action(line)
187 char *line;
189 char *keyword, *arg;
191 keyword = strdup(line);
192 if ((arg = strrchr(keyword, '=')) != NULL)
193 *arg++ = '\0';
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
209 #endif /* USE_OPENPROM */
210 #endif /* USE_OPENFIRM */
211 #endif /* USE_PREPNVRAM */
215 * Dump the contents of the prom corresponding to all known keywords.
217 static void
218 dump_prom()
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)
230 * We have a special dump routine for this.
232 op_dump();
233 else {
234 #endif /* USE_OPENPROM */
235 ee_dump();
236 #ifdef USE_OPENPROM
238 #endif /* USE_OPENPROM */
239 #endif /* USE_OPENFIRM */
240 #endif /* USE_PREPNVRAM */
243 static void
244 usage()
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);