xref: /netbsd-src/usr.sbin/eeprom/main.c (revision 62f324d0121177eaf2e0384f92fd9ca2a751c795)
1 /*	$NetBSD: main.c,v 1.22 2013/03/15 20:22:44 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.22 2013/03/15 20:22:44 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 #if defined(USE_EEPROM) || defined(USE_OPENPROM)
47 #include <machine/eeprom.h>
48 #endif
49 
50 #include "defs.h"
51 #include "pathnames.h"
52 
53 #ifdef USE_OPENPROM
54 # ifndef USE_EEPROM
55 #  define ee_action(a,b)
56 #  define ee_dump()
57 #  define ee_updatechecksums() (void)0
58 #  define check_for_openprom() 1
59 # endif
60 #endif
61 
62 int	main (int, char *[]);
63 static	void action (char *);
64 static	void dump_prom (void);
65 static	void usage (void);
66 
67 const char *path_eeprom = _PATH_EEPROM;
68 const char *path_openprom = _PATH_OPENPROM;
69 const char *path_openfirm = _PATH_OPENFIRM;
70 const char *path_prepnvram = _PATH_PREPNVRAM;
71 int	fix_checksum = 0;
72 int	ignore_checksum = 0;
73 int	update_checksums = 0;
74 int	cksumfail = 0;
75 u_short	writecount;
76 int	eval = 0;
77 #ifdef USE_OPENPROM
78 int	verbose = 0;
79 int	use_openprom;
80 #endif
81 #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM)
82 int	verbose=0;
83 #endif
84 
85 int
86 main(argc, argv)
87 	int argc;
88 	char *argv[];
89 {
90 	int ch, do_stdin = 0;
91 	char *cp, line[BUFSIZE];
92 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
93 	const char *optstring = "-cf:iv";
94 #else
95 	const char *optstring = "-cf:i";
96 #endif /* USE_OPENPROM */
97 
98 	while ((ch = getopt(argc, argv, optstring)) != -1)
99 		switch (ch) {
100 		case '-':
101 			do_stdin = 1;
102 			break;
103 
104 		case 'c':
105 			fix_checksum = 1;
106 			break;
107 
108 		case 'f':
109 			path_eeprom = path_openprom = optarg;
110 			break;
111 
112 		case 'i':
113 			ignore_checksum = 1;
114 			break;
115 
116 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
117 		case 'v':
118 			verbose = 1;
119 			break;
120 #endif /* USE_OPENPROM */
121 
122 		case '?':
123 		default:
124 			usage();
125 		}
126 	argc -= optind;
127 	argv += optind;
128 
129 #ifdef USE_OPENPROM
130 	use_openprom = check_for_openprom();
131 
132 	if (use_openprom == 0) {
133 #endif /* USE_OPENPROM */
134 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
135 		ee_verifychecksums();
136 		if (fix_checksum || cksumfail)
137 			exit(cksumfail);
138 #endif
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 !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
170 		if (update_checksums) {
171 			++writecount;
172 			ee_updatechecksums();
173 		}
174 
175 	exit(eval + cksumfail);
176 #endif
177 	return 0;
178 }
179 
180 /*
181  * Separate the keyword from the argument (if any), find the keyword in
182  * the table, and call the corresponding handler function.
183  */
184 static void
185 action(line)
186 	char *line;
187 {
188 	char *keyword, *arg;
189 
190 	keyword = strdup(line);
191 	if ((arg = strrchr(keyword, '=')) != NULL)
192 		*arg++ = '\0';
193 
194 #ifdef USE_PREPNVRAM
195 	prep_action(keyword, arg);
196 #else
197 #ifdef USE_OPENFIRM
198 	of_action(keyword, arg);
199 #else
200 #ifdef USE_OPENPROM
201 	if (use_openprom)
202 		op_action(keyword, arg);
203 	else {
204 #endif /* USE_OPENPROM */
205 		ee_action(keyword, arg);
206 #ifdef USE_OPENPROM
207 	}
208 #endif /* USE_OPENPROM */
209 #endif /* USE_OPENFIRM */
210 #endif /* USE_PREPNVRAM */
211 }
212 
213 /*
214  * Dump the contents of the prom corresponding to all known keywords.
215  */
216 static void
217 dump_prom()
218 {
219 
220 #ifdef USE_PREPNVRAM
221 	prep_dump();
222 #else
223 #ifdef USE_OPENFIRM
224 	of_dump();
225 #else
226 #ifdef USE_OPENPROM
227 	if (use_openprom)
228 		/*
229 		 * We have a special dump routine for this.
230 		 */
231 		op_dump();
232 	else {
233 #endif /* USE_OPENPROM */
234 		ee_dump();
235 #ifdef USE_OPENPROM
236 	}
237 #endif /* USE_OPENPROM */
238 #endif /* USE_OPENFIRM */
239 #endif /* USE_PREPNVRAM */
240 }
241 
242 static void
243 usage()
244 {
245 
246 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
247 	fprintf(stderr, "usage: %s %s\n", getprogname(),
248 	    "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
249 #else
250 	fprintf(stderr, "usage: %s %s\n", getprogname(),
251 	    "[-] [-c] [-f device] [-i] [field[=value] ...]");
252 #endif /* __us */
253 	exit(1);
254 }
255