xref: /netbsd-src/usr.sbin/eeprom/main.c (revision 5aefcfdc06931dd97e76246d2fe0302f7b3fe094)
1 /*	$NetBSD: main.c,v 1.12 2000/11/28 22:31:37 mrg 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.12 2000/11/28 22:31:37 mrg 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 #endif /* USE_OPENPROM */
134 
135 	if (use_openprom == 0) {
136 		ee_verifychecksums();
137 		if (fix_checksum || cksumfail)
138 			exit(cksumfail);
139 	}
140 
141 	if (do_stdin) {
142 		while (fgets(line, BUFSIZE, stdin) != NULL) {
143 			if (line[0] == '\n')
144 				continue;
145 			if ((cp = strrchr(line, '\n')) != NULL)
146 				*cp = '\0';
147 			action(line);
148 		}
149 		if (ferror(stdin))
150 			err(++eval, "stdin");
151 	} else {
152 		if (argc == 0) {
153 			dump_prom();
154 			exit(eval + cksumfail);
155 		}
156 
157 		while (argc) {
158 			action(*argv);
159 			++argv;
160 			--argc;
161 		}
162 	}
163 
164 #ifdef USE_OPENPROM
165 	if (use_openprom == 0)
166 #endif /* USE_OPENPROM */
167 		if (update_checksums) {
168 			++writecount;
169 			ee_updatechecksums();
170 		}
171 
172 	exit(eval + cksumfail);
173 }
174 
175 /*
176  * Separate the keyword from the argument (if any), find the keyword in
177  * the table, and call the corresponding handler function.
178  */
179 static void
180 action(line)
181 	char *line;
182 {
183 	char *keyword, *arg;
184 
185 	keyword = strdup(line);
186 	if ((arg = strrchr(keyword, '=')) != NULL)
187 		*arg++ = '\0';
188 
189 #ifdef USE_OPENPROM
190 	if (use_openprom)
191 		op_action(keyword, arg);
192 	else
193 #endif /* USE_OPENPROM */
194 		ee_action(keyword, arg);
195 }
196 
197 /*
198  * Dump the contents of the prom corresponding to all known keywords.
199  */
200 static void
201 dump_prom()
202 {
203 
204 #ifdef USE_OPENPROM
205 	if (use_openprom)
206 		/*
207 		 * We have a special dump routine for this.
208 		 */
209 		op_dump();
210 	else
211 #endif /* USE_OPENPROM */
212 		ee_dump();
213 }
214 
215 static void
216 usage()
217 {
218 
219 #ifdef USE_OPENPROM
220 	fprintf(stderr, "usage: %s %s\n", __progname,
221 	    "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
222 #else
223 	fprintf(stderr, "usage: %s %s\n", __progname,
224 	    "[-] [-c] [-f device] [-i] [field[=value] ...]");
225 #endif /* __us */
226 	exit(1);
227 }
228