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