main.c revision 1.6 1 1.6 fair /* $NetBSD: main.c,v 1.6 1997/07/28 19:29:54 fair 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.2 thorpej * This product includes software developed by the NetBSD
21 1.2 thorpej * Foundation, Inc. and its contributors.
22 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 thorpej * contributors may be used to endorse or promote products derived
24 1.2 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 1.2 thorpej * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.5 thorpej #include <sys/cdefs.h>
40 1.5 thorpej #ifndef lint
41 1.5 thorpej __COPYRIGHT(
42 1.5 thorpej "@(#) Copyright (c) 1996 The NetBSD Foundation, Inc. All rights reserved.");
43 1.6 fair __RCSID("$NetBSD: main.c,v 1.6 1997/07/28 19:29:54 fair Exp $");
44 1.5 thorpej #endif
45 1.5 thorpej
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <err.h>
48 1.1 thorpej #include <string.h>
49 1.1 thorpej #include <stdio.h>
50 1.1 thorpej #include <unistd.h>
51 1.1 thorpej
52 1.1 thorpej #ifdef __sparc__
53 1.1 thorpej #include <fcntl.h>
54 1.1 thorpej #include <kvm.h>
55 1.1 thorpej #include <limits.h>
56 1.1 thorpej #include <nlist.h>
57 1.1 thorpej
58 1.1 thorpej #include <machine/openpromio.h>
59 1.1 thorpej
60 1.1 thorpej struct nlist nl[] = {
61 1.1 thorpej { "_cputyp" },
62 1.1 thorpej #define SYM_CPUTYP 0
63 1.1 thorpej { NULL },
64 1.1 thorpej };
65 1.1 thorpej
66 1.1 thorpej static char *system = NULL;
67 1.1 thorpej #endif /* __sparc__ */
68 1.1 thorpej
69 1.1 thorpej #include <machine/eeprom.h>
70 1.1 thorpej
71 1.1 thorpej #include "defs.h"
72 1.1 thorpej
73 1.1 thorpej struct keytabent eekeytab[] = {
74 1.1 thorpej { "hwupdate", 0x10, ee_hwupdate },
75 1.1 thorpej { "memsize", 0x14, ee_num8 },
76 1.1 thorpej { "memtest", 0x15, ee_num8 },
77 1.1 thorpej { "scrsize", 0x16, ee_screensize },
78 1.1 thorpej { "watchdog_reboot", 0x17, ee_truefalse },
79 1.1 thorpej { "default_boot", 0x18, ee_truefalse },
80 1.1 thorpej { "bootdev", 0x19, ee_bootdev },
81 1.1 thorpej { "kbdtype", 0x1e, ee_kbdtype },
82 1.1 thorpej { "console", 0x1f, ee_constype },
83 1.1 thorpej { "keyclick", 0x21, ee_truefalse },
84 1.1 thorpej { "diagdev", 0x22, ee_bootdev },
85 1.1 thorpej { "diagpath", 0x28, ee_diagpath },
86 1.1 thorpej { "columns", 0x50, ee_num8 },
87 1.1 thorpej { "rows", 0x51, ee_num8 },
88 1.1 thorpej { "ttya_use_baud", 0x58, ee_truefalse },
89 1.1 thorpej { "ttya_baud", 0x59, ee_num16 },
90 1.1 thorpej { "ttya_no_rtsdtr", 0x5b, ee_truefalse },
91 1.1 thorpej { "ttyb_use_baud", 0x60, ee_truefalse },
92 1.1 thorpej { "ttyb_baud", 0x61, ee_num16 },
93 1.1 thorpej { "ttyb_no_rtsdtr", 0x63, ee_truefalse },
94 1.1 thorpej { "banner", 0x68, ee_banner },
95 1.1 thorpej { "secure", 0, ee_notsupp },
96 1.1 thorpej { "bad_login", 0, ee_notsupp },
97 1.1 thorpej { "password", 0, ee_notsupp },
98 1.1 thorpej { NULL, 0, ee_notsupp },
99 1.1 thorpej };
100 1.1 thorpej
101 1.5 thorpej int main __P((int, char *[]));
102 1.1 thorpej static void action __P((char *));
103 1.1 thorpej static void dump_prom __P((void));
104 1.1 thorpej static void usage __P((void));
105 1.1 thorpej #ifdef __sparc__
106 1.1 thorpej static int getcputype __P((void));
107 1.1 thorpej #endif /* __sparc__ */
108 1.1 thorpej
109 1.1 thorpej char *path_eeprom = "/dev/eeprom";
110 1.1 thorpej char *path_openprom = "/dev/openprom";
111 1.1 thorpej int fix_checksum = 0;
112 1.1 thorpej int ignore_checksum = 0;
113 1.1 thorpej int update_checksums = 0;
114 1.1 thorpej int cksumfail = 0;
115 1.1 thorpej u_short writecount;
116 1.1 thorpej int eval = 0;
117 1.1 thorpej int use_openprom = 0;
118 1.1 thorpej int verbose = 0;
119 1.1 thorpej
120 1.1 thorpej extern char *__progname;
121 1.1 thorpej
122 1.1 thorpej int
123 1.1 thorpej main(argc, argv)
124 1.1 thorpej int argc;
125 1.5 thorpej char *argv[];
126 1.1 thorpej {
127 1.1 thorpej int ch, do_stdin = 0;
128 1.1 thorpej char *cp, line[BUFSIZE];
129 1.1 thorpej #ifdef __sparc__
130 1.1 thorpej char *optstring = "-cf:ivN:";
131 1.1 thorpej #else
132 1.1 thorpej char *optstring = "-cf:i";
133 1.1 thorpej #endif /* __sparc__ */
134 1.1 thorpej
135 1.1 thorpej while ((ch = getopt(argc, argv, optstring)) != -1)
136 1.1 thorpej switch (ch) {
137 1.1 thorpej case '-':
138 1.1 thorpej do_stdin = 1;
139 1.1 thorpej break;
140 1.1 thorpej
141 1.1 thorpej case 'c':
142 1.1 thorpej fix_checksum = 1;
143 1.1 thorpej break;
144 1.1 thorpej
145 1.1 thorpej case 'f':
146 1.1 thorpej path_eeprom = path_openprom = optarg;
147 1.1 thorpej break;
148 1.1 thorpej
149 1.1 thorpej case 'i':
150 1.1 thorpej ignore_checksum = 1;
151 1.1 thorpej break;
152 1.1 thorpej #ifdef __sparc__
153 1.1 thorpej case 'v':
154 1.1 thorpej verbose = 1;
155 1.1 thorpej break;
156 1.1 thorpej
157 1.1 thorpej case 'N':
158 1.1 thorpej system = optarg;
159 1.1 thorpej break;
160 1.1 thorpej
161 1.1 thorpej #endif /* __sparc__ */
162 1.1 thorpej
163 1.1 thorpej case '?':
164 1.1 thorpej default:
165 1.1 thorpej usage();
166 1.1 thorpej }
167 1.1 thorpej argc -= optind;
168 1.1 thorpej argv += optind;
169 1.1 thorpej
170 1.1 thorpej #ifdef __sparc__
171 1.4 thorpej /*
172 1.4 thorpej * Discard setgid setgid privileges if not the running kernel so
173 1.4 thorpej * that bad guys can't print interesting stuff from kernel memory.
174 1.4 thorpej */
175 1.4 thorpej if (system != NULL)
176 1.4 thorpej setgid(getgid());
177 1.1 thorpej if (getcputype() != CPU_SUN4)
178 1.1 thorpej use_openprom = 1;
179 1.1 thorpej #endif /* __sparc__ */
180 1.1 thorpej
181 1.1 thorpej if (use_openprom == 0) {
182 1.1 thorpej ee_verifychecksums();
183 1.1 thorpej if (fix_checksum || cksumfail)
184 1.1 thorpej exit(cksumfail);
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej if (do_stdin) {
188 1.1 thorpej while (fgets(line, BUFSIZE, stdin) != NULL) {
189 1.1 thorpej if (line[0] == '\n')
190 1.1 thorpej continue;
191 1.1 thorpej if ((cp = strrchr(line, '\n')) != NULL)
192 1.1 thorpej *cp = '\0';
193 1.1 thorpej action(line);
194 1.1 thorpej }
195 1.1 thorpej if (ferror(stdin))
196 1.1 thorpej err(++eval, "stdin");
197 1.1 thorpej } else {
198 1.1 thorpej if (argc == 0) {
199 1.1 thorpej dump_prom();
200 1.1 thorpej exit(eval + cksumfail);
201 1.1 thorpej }
202 1.1 thorpej
203 1.1 thorpej while (argc) {
204 1.3 thorpej action(*argv);
205 1.1 thorpej ++argv;
206 1.1 thorpej --argc;
207 1.1 thorpej }
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej if (use_openprom == 0)
211 1.1 thorpej if (update_checksums) {
212 1.1 thorpej ++writecount;
213 1.1 thorpej ee_updatechecksums();
214 1.1 thorpej }
215 1.1 thorpej
216 1.1 thorpej exit(eval + cksumfail);
217 1.1 thorpej }
218 1.1 thorpej
219 1.1 thorpej #ifdef __sparc__
220 1.1 thorpej #define KVM_ABORT(kd, str) { \
221 1.1 thorpej (void)kvm_close((kd)); \
222 1.1 thorpej errx(1, "%s: %s", (str), kvm_geterr((kd))); \
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej static int
226 1.1 thorpej getcputype()
227 1.1 thorpej {
228 1.1 thorpej char errbuf[_POSIX2_LINE_MAX];
229 1.1 thorpej int cputype;
230 1.1 thorpej kvm_t *kd;
231 1.1 thorpej
232 1.1 thorpej bzero(errbuf, sizeof(errbuf));
233 1.1 thorpej
234 1.1 thorpej if ((kd = kvm_openfiles(system, NULL, NULL, O_RDONLY, errbuf)) == NULL)
235 1.1 thorpej errx(1, "can't open kvm: %s", errbuf);
236 1.1 thorpej
237 1.1 thorpej if (kvm_nlist(kd, nl))
238 1.1 thorpej KVM_ABORT(kd, "can't read symbol table");
239 1.1 thorpej
240 1.1 thorpej if (kvm_read(kd, nl[SYM_CPUTYP].n_value, (char *)&cputype,
241 1.1 thorpej sizeof(cputype)) != sizeof(cputype))
242 1.1 thorpej KVM_ABORT(kd, "can't determine cpu type");
243 1.1 thorpej
244 1.1 thorpej (void)kvm_close(kd);
245 1.1 thorpej return (cputype);
246 1.1 thorpej }
247 1.1 thorpej #endif /* __sparc__ */
248 1.1 thorpej
249 1.1 thorpej /*
250 1.1 thorpej * Separate the keyword from the argument (if any), find the keyword in
251 1.1 thorpej * the table, and call the corresponding handler function.
252 1.1 thorpej */
253 1.1 thorpej static void
254 1.1 thorpej action(line)
255 1.1 thorpej char *line;
256 1.1 thorpej {
257 1.6 fair char *keyword, *arg;
258 1.1 thorpej struct keytabent *ktent;
259 1.1 thorpej
260 1.1 thorpej keyword = strdup(line);
261 1.1 thorpej if ((arg = strrchr(keyword, '=')) != NULL)
262 1.1 thorpej *arg++ = '\0';
263 1.1 thorpej
264 1.1 thorpej #ifdef __sparc__
265 1.1 thorpej if (use_openprom) {
266 1.6 fair char *cp;
267 1.1 thorpej /*
268 1.1 thorpej * The whole point of the Openprom is that one
269 1.1 thorpej * isn't required to know the keywords. With this
270 1.1 thorpej * in mind, we just dump the whole thing off to
271 1.1 thorpej * the generic op_handler.
272 1.1 thorpej */
273 1.1 thorpej if ((cp = op_handler(keyword, arg)) != NULL)
274 1.1 thorpej warnx(cp);
275 1.1 thorpej return;
276 1.1 thorpej } else
277 1.1 thorpej #endif /* __sparc__ */
278 1.1 thorpej for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent) {
279 1.1 thorpej if (strcmp(ktent->kt_keyword, keyword) == 0) {
280 1.1 thorpej (*ktent->kt_handler)(ktent, arg);
281 1.1 thorpej return;
282 1.1 thorpej }
283 1.1 thorpej }
284 1.1 thorpej
285 1.1 thorpej warnx("unknown keyword %s", keyword);
286 1.1 thorpej ++eval;
287 1.1 thorpej }
288 1.1 thorpej
289 1.1 thorpej /*
290 1.1 thorpej * Dump the contents of the prom corresponding to all known keywords.
291 1.1 thorpej */
292 1.1 thorpej static void
293 1.1 thorpej dump_prom()
294 1.1 thorpej {
295 1.1 thorpej struct keytabent *ktent;
296 1.1 thorpej
297 1.1 thorpej #ifdef __sparc__
298 1.1 thorpej if (use_openprom) {
299 1.1 thorpej /*
300 1.1 thorpej * We have a special dump routine for this.
301 1.1 thorpej */
302 1.1 thorpej op_dump();
303 1.1 thorpej } else
304 1.1 thorpej #endif /* __sparc__ */
305 1.1 thorpej for (ktent = eekeytab; ktent->kt_keyword != NULL; ++ktent)
306 1.1 thorpej (*ktent->kt_handler)(ktent, NULL);
307 1.1 thorpej }
308 1.1 thorpej
309 1.1 thorpej static void
310 1.1 thorpej usage()
311 1.1 thorpej {
312 1.1 thorpej
313 1.1 thorpej #ifdef __sparc__
314 1.1 thorpej fprintf(stderr, "usage: %s %s %s\n", __progname,
315 1.1 thorpej "[-] [-c] [-f device] [-i] [-v]",
316 1.1 thorpej "[-N system] [field[=value] ...]");
317 1.1 thorpej #else
318 1.1 thorpej fprintf(stderr, "usage: %s %s\n", __progname,
319 1.1 thorpej "[-] [-c] [-f device] [-i] [field[=value] ...]");
320 1.1 thorpej #endif /* __sparc__ */
321 1.1 thorpej exit(1);
322 1.1 thorpej }
323