main.c revision 1.10 1 /* $NetBSD: main.c,v 1.10 2000/09/24 09:45:55 bjh21 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.10 2000/09/24 09:45:55 bjh21 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 #ifdef __sparc__
53 #include <fcntl.h>
54 #include <kvm.h>
55 #include <limits.h>
56 #include <nlist.h>
57
58 #include <machine/openpromio.h>
59
60 struct nlist nl[] = {
61 { "_cputyp" },
62 #define SYM_CPUTYP 0
63 { NULL },
64 };
65
66 static char *system = NULL;
67 #endif /* __sparc__ */
68
69 #include <machine/eeprom.h>
70
71 #include "defs.h"
72 #include "pathnames.h"
73
74 struct keytabent eekeytab[] = {
75 { "hwupdate", 0x10, ee_hwupdate },
76 { "memsize", 0x14, ee_num8 },
77 { "memtest", 0x15, ee_num8 },
78 { "scrsize", 0x16, ee_screensize },
79 { "watchdog_reboot", 0x17, ee_truefalse },
80 { "default_boot", 0x18, ee_truefalse },
81 { "bootdev", 0x19, ee_bootdev },
82 { "kbdtype", 0x1e, ee_kbdtype },
83 { "console", 0x1f, ee_constype },
84 { "keyclick", 0x21, ee_truefalse },
85 { "diagdev", 0x22, ee_bootdev },
86 { "diagpath", 0x28, ee_diagpath },
87 { "columns", 0x50, ee_num8 },
88 { "rows", 0x51, ee_num8 },
89 { "ttya_use_baud", 0x58, ee_truefalse },
90 { "ttya_baud", 0x59, ee_num16 },
91 { "ttya_no_rtsdtr", 0x5b, ee_truefalse },
92 { "ttyb_use_baud", 0x60, ee_truefalse },
93 { "ttyb_baud", 0x61, ee_num16 },
94 { "ttyb_no_rtsdtr", 0x63, ee_truefalse },
95 { "banner", 0x68, ee_banner },
96 { "secure", 0, ee_notsupp },
97 { "bad_login", 0, ee_notsupp },
98 { "password", 0, ee_notsupp },
99 { NULL, 0, ee_notsupp },
100 };
101
102 int main __P((int, char *[]));
103 static void action __P((char *));
104 static void dump_prom __P((void));
105 static void usage __P((void));
106 #ifdef __sparc__
107 static int getcputype __P((void));
108 #endif /* __sparc__ */
109
110 char *path_eeprom = _PATH_EEPROM;
111 char *path_openprom = _PATH_OPENPROM;
112 int fix_checksum = 0;
113 int ignore_checksum = 0;
114 int update_checksums = 0;
115 int cksumfail = 0;
116 u_short writecount;
117 int eval = 0;
118 int use_openprom = 0;
119 int verbose = 0;
120
121 extern char *__progname;
122
123 int
124 main(argc, argv)
125 int argc;
126 char *argv[];
127 {
128 int ch, do_stdin = 0;
129 char *cp, line[BUFSIZE];
130 #ifdef __sparc__
131 char *optstring = "-cf:ivN:";
132 #else
133 char *optstring = "-cf:i";
134 #endif /* __sparc__ */
135
136 while ((ch = getopt(argc, argv, optstring)) != -1)
137 switch (ch) {
138 case '-':
139 do_stdin = 1;
140 break;
141
142 case 'c':
143 fix_checksum = 1;
144 break;
145
146 case 'f':
147 path_eeprom = path_openprom = optarg;
148 break;
149
150 case 'i':
151 ignore_checksum = 1;
152 break;
153 #ifdef __sparc__
154 case 'v':
155 verbose = 1;
156 break;
157
158 case 'N':
159 system = optarg;
160 break;
161
162 #endif /* __sparc__ */
163
164 case '?':
165 default:
166 usage();
167 }
168 argc -= optind;
169 argv += optind;
170
171 #ifdef __sparc__
172 /*
173 * Discard setgid setgid privileges if not the running kernel so
174 * that bad guys can't print interesting stuff from kernel memory.
175 */
176 if (system != NULL)
177 setgid(getgid());
178 if (getcputype() != CPU_SUN4)
179 use_openprom = 1;
180 #endif /* __sparc__ */
181
182 if (use_openprom == 0) {
183 ee_verifychecksums();
184 if (fix_checksum || cksumfail)
185 exit(cksumfail);
186 }
187
188 if (do_stdin) {
189 while (fgets(line, BUFSIZE, stdin) != NULL) {
190 if (line[0] == '\n')
191 continue;
192 if ((cp = strrchr(line, '\n')) != NULL)
193 *cp = '\0';
194 action(line);
195 }
196 if (ferror(stdin))
197 err(++eval, "stdin");
198 } else {
199 if (argc == 0) {
200 dump_prom();
201 exit(eval + cksumfail);
202 }
203
204 while (argc) {
205 action(*argv);
206 ++argv;
207 --argc;
208 }
209 }
210
211 if (use_openprom == 0)
212 if (update_checksums) {
213 ++writecount;
214 ee_updatechecksums();
215 }
216
217 exit(eval + cksumfail);
218 }
219
220 #ifdef __sparc__
221 #define KVM_ABORT(kd, str) { \
222 (void)kvm_close((kd)); \
223 errx(1, "%s: %s", (str), kvm_geterr((kd))); \
224 }
225
226 static int
227 getcputype()
228 {
229 char errbuf[_POSIX2_LINE_MAX];
230 int cputype;
231 kvm_t *kd;
232
233 memset(errbuf, 0, sizeof(errbuf));
234
235 if ((kd = kvm_openfiles(system, NULL, NULL, O_RDONLY, errbuf)) == NULL)
236 errx(1, "can't open kvm: %s", errbuf);
237
238 if (kvm_nlist(kd, nl))
239 KVM_ABORT(kd, "can't read symbol table");
240
241 if (kvm_read(kd, nl[SYM_CPUTYP].n_value, (char *)&cputype,
242 sizeof(cputype)) != sizeof(cputype))
243 KVM_ABORT(kd, "can't determine cpu type");
244
245 (void)kvm_close(kd);
246 return (cputype);
247 }
248 #endif /* __sparc__ */
249
250 /*
251 * Separate the keyword from the argument (if any), find the keyword in
252 * the table, and call the corresponding handler function.
253 */
254 static void
255 action(line)
256 char *line;
257 {
258 char *keyword, *arg;
259 struct keytabent *ktent;
260
261 keyword = strdup(line);
262 if ((arg = strrchr(keyword, '=')) != NULL)
263 *arg++ = '\0';
264
265 #ifdef __sparc__
266 if (use_openprom) {
267 char *cp;
268 /*
269 * The whole point of the Openprom is that one
270 * isn't required to know the keywords. With this
271 * in mind, we just dump the whole thing off to
272 * the generic op_handler.
273 */
274 if ((cp = op_handler(keyword, arg)) != NULL)
275 warnx("%s", cp);
276 return;
277 } else
278 #endif /* __sparc__ */
279 for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent) {
280 if (strcmp(ktent->kt_keyword, keyword) == 0) {
281 (*ktent->kt_handler)(ktent, arg);
282 return;
283 }
284 }
285
286 warnx("unknown keyword %s", keyword);
287 ++eval;
288 }
289
290 /*
291 * Dump the contents of the prom corresponding to all known keywords.
292 */
293 static void
294 dump_prom()
295 {
296 struct keytabent *ktent;
297
298 #ifdef __sparc__
299 if (use_openprom) {
300 /*
301 * We have a special dump routine for this.
302 */
303 op_dump();
304 } else
305 #endif /* __sparc__ */
306 for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent)
307 (*ktent->kt_handler)(ktent, NULL);
308 }
309
310 static void
311 usage()
312 {
313
314 #ifdef __sparc__
315 fprintf(stderr, "usage: %s %s %s\n", __progname,
316 "[-] [-c] [-f device] [-i] [-v]",
317 "[-N system] [field[=value] ...]");
318 #else
319 fprintf(stderr, "usage: %s %s\n", __progname,
320 "[-] [-c] [-f device] [-i] [field[=value] ...]");
321 #endif /* __sparc__ */
322 exit(1);
323 }
324