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