eehandlers.c revision 1.1 1 1.1 thorpej /* $NetBSD: eehandlers.c,v 1.1 1995/07/13 18:10:29 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/types.h>
36 1.1 thorpej #include <ctype.h>
37 1.1 thorpej #include <err.h>
38 1.1 thorpej #include <errno.h>
39 1.1 thorpej #include <fcntl.h>
40 1.1 thorpej #include <string.h>
41 1.1 thorpej #include <stdio.h>
42 1.1 thorpej #include <stdlib.h>
43 1.1 thorpej #include <string.h>
44 1.1 thorpej #include <time.h>
45 1.1 thorpej #include <unistd.h>
46 1.1 thorpej
47 1.1 thorpej #include <machine/eeprom.h>
48 1.1 thorpej #ifdef __sparc__
49 1.1 thorpej #include <machine/openpromio.h>
50 1.1 thorpej #endif /* __sparc__ */
51 1.1 thorpej
52 1.1 thorpej #include "defs.h"
53 1.1 thorpej
54 1.1 thorpej extern char *path_eeprom;
55 1.1 thorpej extern int eval;
56 1.1 thorpej extern int update_checksums;
57 1.1 thorpej extern int ignore_checksum;
58 1.1 thorpej extern int fix_checksum;
59 1.1 thorpej extern int cksumfail;
60 1.1 thorpej extern u_short writecount;
61 1.1 thorpej
62 1.1 thorpej struct timeb;
63 1.1 thorpej extern time_t get_date __P((char *, struct timeb *));
64 1.1 thorpej
65 1.1 thorpej static char err_str[BUFSIZE];
66 1.1 thorpej
67 1.1 thorpej static void badval __P((struct keytabent *, char *));
68 1.1 thorpej static int doio __P((struct keytabent *, u_char *, ssize_t, int));
69 1.1 thorpej
70 1.1 thorpej #define BARF(kt) { \
71 1.1 thorpej badval((kt), arg); \
72 1.1 thorpej ++eval; \
73 1.1 thorpej return; \
74 1.1 thorpej }
75 1.1 thorpej
76 1.1 thorpej #define FAILEDREAD(kt) { \
77 1.1 thorpej warnx(err_str); \
78 1.1 thorpej warnx("failed to read field `%s'", (kt)->kt_keyword); \
79 1.1 thorpej ++eval; \
80 1.1 thorpej return; \
81 1.1 thorpej }
82 1.1 thorpej
83 1.1 thorpej #define FAILEDWRITE(kt) { \
84 1.1 thorpej warnx(err_str); \
85 1.1 thorpej warnx("failed to update field `%s'", (kt)->kt_keyword); \
86 1.1 thorpej ++eval; \
87 1.1 thorpej return; \
88 1.1 thorpej }
89 1.1 thorpej
90 1.1 thorpej void
91 1.1 thorpej ee_hwupdate(ktent, arg)
92 1.1 thorpej struct keytabent *ktent;
93 1.1 thorpej char *arg;
94 1.1 thorpej {
95 1.1 thorpej time_t t;
96 1.1 thorpej char *cp, *cp2;
97 1.1 thorpej
98 1.1 thorpej if (arg) {
99 1.1 thorpej if ((strcmp(arg, "now") == 0) ||
100 1.1 thorpej (strcmp(arg, "today") == 0)) {
101 1.1 thorpej if ((t = time(NULL)) == (time_t)(-1)) {
102 1.1 thorpej warnx("can't get current time");
103 1.1 thorpej ++eval;
104 1.1 thorpej return;
105 1.1 thorpej }
106 1.1 thorpej } else
107 1.1 thorpej if ((t = get_date(arg, NULL)) == (time_t)(-1))
108 1.1 thorpej BARF(ktent);
109 1.1 thorpej
110 1.1 thorpej if (doio(ktent, (u_char *)&t, sizeof(t), IO_WRITE))
111 1.1 thorpej FAILEDWRITE(ktent);
112 1.1 thorpej } else
113 1.1 thorpej if (doio(ktent, (u_char *)&t, sizeof(t), IO_READ))
114 1.1 thorpej FAILEDREAD(ktent);
115 1.1 thorpej
116 1.1 thorpej cp = ctime(&t);
117 1.1 thorpej if ((cp2 = strrchr(cp, '\n')) != NULL)
118 1.1 thorpej *cp2 = '\0';
119 1.1 thorpej
120 1.1 thorpej printf("%s=%d (%s)\n", ktent->kt_keyword, t, cp);
121 1.1 thorpej }
122 1.1 thorpej
123 1.1 thorpej void
124 1.1 thorpej ee_num8(ktent, arg)
125 1.1 thorpej struct keytabent *ktent;
126 1.1 thorpej char *arg;
127 1.1 thorpej {
128 1.1 thorpej u_char num8 = 0;
129 1.1 thorpej u_int num32;
130 1.1 thorpej int i;
131 1.1 thorpej
132 1.1 thorpej if (arg) {
133 1.1 thorpej for (i = 0; i < (strlen(arg) - 1); ++i)
134 1.1 thorpej if (!isdigit(arg[i]))
135 1.1 thorpej BARF(ktent);
136 1.1 thorpej num32 = atoi(arg);
137 1.1 thorpej if (num32 > 0xff)
138 1.1 thorpej BARF(ktent);
139 1.1 thorpej num8 += num32;
140 1.1 thorpej if (doio(ktent, &num8, sizeof(num8), IO_WRITE))
141 1.1 thorpej FAILEDWRITE(ktent);
142 1.1 thorpej } else
143 1.1 thorpej if (doio(ktent, &num8, sizeof(num8), IO_READ))
144 1.1 thorpej FAILEDREAD(ktent);
145 1.1 thorpej
146 1.1 thorpej printf("%s=%d\n", ktent->kt_keyword, num8);
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej void
150 1.1 thorpej ee_num16(ktent, arg)
151 1.1 thorpej struct keytabent *ktent;
152 1.1 thorpej char *arg;
153 1.1 thorpej {
154 1.1 thorpej u_int16_t num16 = 0;
155 1.1 thorpej u_int num32;
156 1.1 thorpej int i;
157 1.1 thorpej
158 1.1 thorpej if (arg) {
159 1.1 thorpej for (i = 0; i < (strlen(arg) - 1); ++i)
160 1.1 thorpej if (!isdigit(arg[i]))
161 1.1 thorpej BARF(ktent);
162 1.1 thorpej num32 = atoi(arg);
163 1.1 thorpej if (num32 > 0xffff)
164 1.1 thorpej BARF(ktent);
165 1.1 thorpej num16 += num32;
166 1.1 thorpej if (doio(ktent, (u_char *)&num16, sizeof(num16), IO_WRITE))
167 1.1 thorpej FAILEDWRITE(ktent);
168 1.1 thorpej } else
169 1.1 thorpej if (doio(ktent, (u_char *)&num16, sizeof(num16), IO_READ))
170 1.1 thorpej FAILEDREAD(ktent);
171 1.1 thorpej
172 1.1 thorpej printf("%s=%d\n", ktent->kt_keyword, num16);
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej static struct strvaltabent scrsizetab[] = {
176 1.1 thorpej { "1152x900", EE_SCR_1152X900 },
177 1.1 thorpej { "1024x1024", EE_SCR_1024X1024 },
178 1.1 thorpej { "1600x1280", EE_SCR_1600X1280 },
179 1.1 thorpej { "1440x1440", EE_SCR_1440X1440 },
180 1.1 thorpej { NULL, 0 },
181 1.1 thorpej };
182 1.1 thorpej
183 1.1 thorpej void
184 1.1 thorpej ee_screensize(ktent, arg)
185 1.1 thorpej struct keytabent *ktent;
186 1.1 thorpej char *arg;
187 1.1 thorpej {
188 1.1 thorpej struct strvaltabent *svp;
189 1.1 thorpej u_char scsize;
190 1.1 thorpej
191 1.1 thorpej if (arg) {
192 1.1 thorpej for (svp = scrsizetab; svp->sv_str != NULL; ++svp)
193 1.1 thorpej if (strcmp(svp->sv_str, arg) == 0)
194 1.1 thorpej break;
195 1.1 thorpej if (svp->sv_str == NULL)
196 1.1 thorpej BARF(ktent);
197 1.1 thorpej
198 1.1 thorpej scsize = svp->sv_val;
199 1.1 thorpej if (doio(ktent, &scsize, sizeof(scsize), IO_WRITE))
200 1.1 thorpej FAILEDWRITE(ktent);
201 1.1 thorpej } else {
202 1.1 thorpej if (doio(ktent, &scsize, sizeof(scsize), IO_READ))
203 1.1 thorpej FAILEDREAD(ktent);
204 1.1 thorpej
205 1.1 thorpej for (svp = scrsizetab; svp->sv_str != NULL; ++svp)
206 1.1 thorpej if (svp->sv_val == scsize)
207 1.1 thorpej break;
208 1.1 thorpej if (svp->sv_str == NULL) {
209 1.1 thorpej warnx("unknown %s value %d", ktent->kt_keyword,
210 1.1 thorpej scsize);
211 1.1 thorpej return;
212 1.1 thorpej }
213 1.1 thorpej }
214 1.1 thorpej printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);
215 1.1 thorpej }
216 1.1 thorpej
217 1.1 thorpej static struct strvaltabent truthtab[] = {
218 1.1 thorpej { "true", EE_TRUE },
219 1.1 thorpej { "false", EE_FALSE },
220 1.1 thorpej { NULL, 0 },
221 1.1 thorpej };
222 1.1 thorpej
223 1.1 thorpej void
224 1.1 thorpej ee_truefalse(ktent, arg)
225 1.1 thorpej struct keytabent *ktent;
226 1.1 thorpej char *arg;
227 1.1 thorpej {
228 1.1 thorpej struct strvaltabent *svp;
229 1.1 thorpej u_char truth;
230 1.1 thorpej
231 1.1 thorpej if (arg) {
232 1.1 thorpej for (svp = truthtab; svp->sv_str != NULL; ++svp)
233 1.1 thorpej if (strcmp(svp->sv_str, arg) == 0)
234 1.1 thorpej break;
235 1.1 thorpej if (svp->sv_str == NULL)
236 1.1 thorpej BARF(ktent);
237 1.1 thorpej
238 1.1 thorpej truth = svp->sv_val;
239 1.1 thorpej if (doio(ktent, &truth, sizeof(truth), IO_WRITE))
240 1.1 thorpej FAILEDWRITE(ktent);
241 1.1 thorpej } else {
242 1.1 thorpej if (doio(ktent, &truth, sizeof(truth), IO_READ))
243 1.1 thorpej FAILEDREAD(ktent);
244 1.1 thorpej
245 1.1 thorpej for (svp = truthtab; svp->sv_str != NULL; ++svp)
246 1.1 thorpej if (svp->sv_val == truth)
247 1.1 thorpej break;
248 1.1 thorpej if (svp->sv_str == NULL) {
249 1.1 thorpej warnx("unknown truth value 0x%x for %s", truth,
250 1.1 thorpej ktent->kt_keyword);
251 1.1 thorpej return;
252 1.1 thorpej }
253 1.1 thorpej }
254 1.1 thorpej printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);
255 1.1 thorpej }
256 1.1 thorpej
257 1.1 thorpej void
258 1.1 thorpej ee_bootdev(ktent, arg)
259 1.1 thorpej struct keytabent *ktent;
260 1.1 thorpej char *arg;
261 1.1 thorpej {
262 1.1 thorpej u_char dev[5];
263 1.1 thorpej int i;
264 1.1 thorpej size_t arglen;
265 1.1 thorpej char *cp;
266 1.1 thorpej
267 1.1 thorpej if (arg) {
268 1.1 thorpej /*
269 1.1 thorpej * The format of the string we accept is the following:
270 1.1 thorpej * cc(n,n,n)
271 1.1 thorpej * where:
272 1.1 thorpej * c -- an alphabetical character [a-z]
273 1.1 thorpej * n -- a number in hexadecimal, between 0 and ff,
274 1.1 thorpej * with no leading `0x'.
275 1.1 thorpej */
276 1.1 thorpej arglen = strlen(arg);
277 1.1 thorpej if (arglen < 9 || arglen > 12 || arg[2] != '(' ||
278 1.1 thorpej arg[arglen - 1] != ')')
279 1.1 thorpej BARF(ktent);
280 1.1 thorpej
281 1.1 thorpej /* Handle the first 2 letters. */
282 1.1 thorpej for (i = 0; i < 2; ++i) {
283 1.1 thorpej if (arg[i] < 'a' || arg[i] > 'z')
284 1.1 thorpej BARF(ktent);
285 1.1 thorpej dev[i] = (u_char)arg[i];
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej /* Handle the 3 `0x'-less hex values. */
289 1.1 thorpej cp = &arg[3];
290 1.1 thorpej for (i = 2; i < 5; ++i) {
291 1.1 thorpej if (*cp == '\0')
292 1.1 thorpej BARF(ktent);
293 1.1 thorpej
294 1.1 thorpej if (*cp >= '0' && *cp <= '9')
295 1.1 thorpej dev[i] = *cp++ - '0';
296 1.1 thorpej else if (*cp >= 'a' && *cp <= 'f')
297 1.1 thorpej dev[i] = 10 + (*cp++ - 'a');
298 1.1 thorpej else
299 1.1 thorpej BARF(ktent);
300 1.1 thorpej
301 1.1 thorpej /* Deal with a second digit. */
302 1.1 thorpej if (*cp >= '0' && *cp <= '9') {
303 1.1 thorpej dev[i] <<= 4;
304 1.1 thorpej dev[i] &= 0xf0;
305 1.1 thorpej dev[i] += *cp++ - '0';
306 1.1 thorpej } else if (*cp >= 'a' && *cp <= 'f') {
307 1.1 thorpej dev[i] <<= 4;
308 1.1 thorpej dev[i] &= 0xf0;
309 1.1 thorpej dev[i] += 10 + (*cp++ - 'a');
310 1.1 thorpej }
311 1.1 thorpej
312 1.1 thorpej /* Ensure we have the correct delimiter. */
313 1.1 thorpej if ((*cp == ',' && i < 4) || (*cp == ')' && i == 4)) {
314 1.1 thorpej ++cp;
315 1.1 thorpej continue;
316 1.1 thorpej } else
317 1.1 thorpej BARF(ktent);
318 1.1 thorpej }
319 1.1 thorpej if (doio(ktent, (u_char *)&dev[0], sizeof(dev), IO_WRITE))
320 1.1 thorpej FAILEDWRITE(ktent);
321 1.1 thorpej } else
322 1.1 thorpej if (doio(ktent, (u_char *)&dev[0], sizeof(dev), IO_READ))
323 1.1 thorpej FAILEDREAD(ktent);
324 1.1 thorpej
325 1.1 thorpej printf("%s=%c%c(%x,%x,%x)\n", ktent->kt_keyword, dev[0],
326 1.1 thorpej dev[1], dev[2], dev[3], dev[4]);
327 1.1 thorpej }
328 1.1 thorpej
329 1.1 thorpej void
330 1.1 thorpej ee_kbdtype(ktent, arg)
331 1.1 thorpej struct keytabent *ktent;
332 1.1 thorpej char *arg;
333 1.1 thorpej {
334 1.1 thorpej u_char kbd = 0;
335 1.1 thorpej u_int kbd2;
336 1.1 thorpej int i;
337 1.1 thorpej
338 1.1 thorpej if (arg) {
339 1.1 thorpej for (i = 0; i < (strlen(arg) - 1); ++i)
340 1.1 thorpej if (!isdigit(arg[i]))
341 1.1 thorpej BARF(ktent);
342 1.1 thorpej kbd2 = atoi(arg);
343 1.1 thorpej if (kbd2 > 0xff)
344 1.1 thorpej BARF(ktent);
345 1.1 thorpej kbd += kbd2;
346 1.1 thorpej if (doio(ktent, &kbd, sizeof(kbd), IO_WRITE))
347 1.1 thorpej FAILEDWRITE(ktent);
348 1.1 thorpej } else
349 1.1 thorpej if (doio(ktent, &kbd, sizeof(kbd), IO_READ))
350 1.1 thorpej FAILEDREAD(ktent);
351 1.1 thorpej
352 1.1 thorpej printf("%s=%d (%s)\n", ktent->kt_keyword, kbd, kbd ? "other" : "Sun");
353 1.1 thorpej }
354 1.1 thorpej
355 1.1 thorpej static struct strvaltabent constab[] = {
356 1.1 thorpej { "b&w", EE_CONS_BW },
357 1.1 thorpej { "ttya", EE_CONS_TTYA },
358 1.1 thorpej { "ttyb", EE_CONS_TTYB },
359 1.1 thorpej { "color", EE_CONS_COLOR },
360 1.1 thorpej { "p4opt", EE_CONS_P4OPT },
361 1.1 thorpej { NULL, 0 },
362 1.1 thorpej };
363 1.1 thorpej
364 1.1 thorpej void
365 1.1 thorpej ee_constype(ktent, arg)
366 1.1 thorpej struct keytabent *ktent;
367 1.1 thorpej char *arg;
368 1.1 thorpej {
369 1.1 thorpej struct strvaltabent *svp;
370 1.1 thorpej u_char cons;
371 1.1 thorpej
372 1.1 thorpej if (arg) {
373 1.1 thorpej for (svp = constab; svp->sv_str != NULL; ++svp)
374 1.1 thorpej if (strcmp(svp->sv_str, arg) == 0)
375 1.1 thorpej break;
376 1.1 thorpej if (svp->sv_str == NULL)
377 1.1 thorpej BARF(ktent);
378 1.1 thorpej
379 1.1 thorpej cons = svp->sv_val;
380 1.1 thorpej if (doio(ktent, &cons, sizeof(cons), IO_WRITE))
381 1.1 thorpej FAILEDWRITE(ktent);
382 1.1 thorpej } else {
383 1.1 thorpej if (doio(ktent, &cons, sizeof(cons), IO_READ))
384 1.1 thorpej FAILEDREAD(ktent);
385 1.1 thorpej
386 1.1 thorpej for (svp = constab; svp->sv_str != NULL; ++svp)
387 1.1 thorpej if (svp->sv_val == cons)
388 1.1 thorpej break;
389 1.1 thorpej if (svp->sv_str == NULL) {
390 1.1 thorpej warnx("unknown type 0x%x for %s", cons,
391 1.1 thorpej ktent->kt_keyword);
392 1.1 thorpej return;
393 1.1 thorpej }
394 1.1 thorpej }
395 1.1 thorpej printf("%s=%s\n", ktent->kt_keyword, svp->sv_str);
396 1.1 thorpej
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej void
400 1.1 thorpej ee_diagpath(ktent, arg)
401 1.1 thorpej struct keytabent *ktent;
402 1.1 thorpej char *arg;
403 1.1 thorpej {
404 1.1 thorpej char path[40];
405 1.1 thorpej
406 1.1 thorpej bzero(path, sizeof(path));
407 1.1 thorpej if (arg) {
408 1.1 thorpej if (strlen(arg) > sizeof(path))
409 1.1 thorpej BARF(ktent);
410 1.1 thorpej sprintf(path, arg);
411 1.1 thorpej if (doio(ktent, (u_char *)&path[0], sizeof(path), IO_WRITE))
412 1.1 thorpej FAILEDWRITE(ktent);
413 1.1 thorpej } else
414 1.1 thorpej if (doio(ktent, (u_char *)&path[0], sizeof(path), IO_READ))
415 1.1 thorpej FAILEDREAD(ktent);
416 1.1 thorpej
417 1.1 thorpej printf("%s=%s\n", ktent->kt_keyword, path);
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej void
421 1.1 thorpej ee_banner(ktent, arg)
422 1.1 thorpej struct keytabent *ktent;
423 1.1 thorpej char *arg;
424 1.1 thorpej {
425 1.1 thorpej char string[80];
426 1.1 thorpej u_char enable;
427 1.1 thorpej struct keytabent kt;
428 1.1 thorpej
429 1.1 thorpej kt.kt_keyword = "enable_banner";
430 1.1 thorpej kt.kt_offset = EE_BANNER_ENABLE_LOC;
431 1.1 thorpej kt.kt_handler = ee_notsupp;
432 1.1 thorpej
433 1.1 thorpej bzero(string, sizeof(string));
434 1.1 thorpej if (arg) {
435 1.1 thorpej if (strlen(arg) > sizeof(string))
436 1.1 thorpej BARF(ktent);
437 1.1 thorpej if (*arg != '\0') {
438 1.1 thorpej enable = EE_TRUE;
439 1.1 thorpej sprintf(string, arg);
440 1.1 thorpej if (doio(ktent, (u_char *)string,
441 1.1 thorpej sizeof(string), IO_WRITE))
442 1.1 thorpej FAILEDWRITE(ktent);
443 1.1 thorpej } else {
444 1.1 thorpej enable = EE_FALSE;
445 1.1 thorpej if (doio(ktent, (u_char *)string,
446 1.1 thorpej sizeof(string), IO_READ))
447 1.1 thorpej FAILEDREAD(ktent);
448 1.1 thorpej }
449 1.1 thorpej
450 1.1 thorpej if (doio(&kt, &enable, sizeof(enable), IO_WRITE))
451 1.1 thorpej FAILEDWRITE(&kt);
452 1.1 thorpej } else {
453 1.1 thorpej if (doio(ktent, (u_char *)string, sizeof(string), IO_READ))
454 1.1 thorpej FAILEDREAD(ktent);
455 1.1 thorpej if (doio(&kt, &enable, sizeof(enable), IO_READ))
456 1.1 thorpej FAILEDREAD(&kt);
457 1.1 thorpej }
458 1.1 thorpej printf("%s=%s (%s)\n", ktent->kt_keyword, string,
459 1.1 thorpej enable == EE_TRUE ? "enabled" : "disabled");
460 1.1 thorpej }
461 1.1 thorpej
462 1.1 thorpej /* ARGSUSED */
463 1.1 thorpej void
464 1.1 thorpej ee_notsupp(ktent, arg)
465 1.1 thorpej struct keytabent *ktent;
466 1.1 thorpej char *arg;
467 1.1 thorpej {
468 1.1 thorpej
469 1.1 thorpej warnx("field `%s' not yet supported", ktent->kt_keyword);
470 1.1 thorpej }
471 1.1 thorpej
472 1.1 thorpej static void
473 1.1 thorpej badval(ktent, arg)
474 1.1 thorpej struct keytabent *ktent;
475 1.1 thorpej char *arg;
476 1.1 thorpej {
477 1.1 thorpej
478 1.1 thorpej warnx("inappropriate value `%s' for field `%s'", arg,
479 1.1 thorpej ktent->kt_keyword);
480 1.1 thorpej }
481 1.1 thorpej
482 1.1 thorpej static int
483 1.1 thorpej doio(ktent, buf, len, wr)
484 1.1 thorpej struct keytabent *ktent;
485 1.1 thorpej u_char *buf;
486 1.1 thorpej ssize_t len;
487 1.1 thorpej int wr;
488 1.1 thorpej {
489 1.1 thorpej int fd, rval = 0;
490 1.1 thorpej u_char *buf2;
491 1.1 thorpej
492 1.1 thorpej buf2 = (u_char *)calloc(1, len);
493 1.1 thorpej if (buf2 == NULL) {
494 1.1 thorpej sprintf(err_str, "memory allocation failed");
495 1.1 thorpej return (1);
496 1.1 thorpej }
497 1.1 thorpej
498 1.1 thorpej fd = open(path_eeprom, wr == IO_WRITE ? O_RDWR : O_RDONLY, 0640);
499 1.1 thorpej if (fd < 0) {
500 1.1 thorpej sprintf(err_str, "open: %s: %s", path_eeprom,
501 1.1 thorpej strerror(errno));
502 1.1 thorpej free(buf2);
503 1.1 thorpej return (1);
504 1.1 thorpej }
505 1.1 thorpej
506 1.1 thorpej if (lseek(fd, (off_t)ktent->kt_offset, SEEK_SET) < (off_t)0) {
507 1.1 thorpej sprintf(err_str, "lseek: %s:", path_eeprom,
508 1.1 thorpej strerror(errno));
509 1.1 thorpej rval = 1;
510 1.1 thorpej goto done;
511 1.1 thorpej }
512 1.1 thorpej
513 1.1 thorpej if (read(fd, buf2, len) != len) {
514 1.1 thorpej sprintf(err_str, "read: %s: %s", path_eeprom,
515 1.1 thorpej strerror(errno));
516 1.1 thorpej return (1);
517 1.1 thorpej }
518 1.1 thorpej
519 1.1 thorpej if (wr == IO_WRITE) {
520 1.1 thorpej if (bcmp(buf, buf2, len) == 0)
521 1.1 thorpej goto done;
522 1.1 thorpej
523 1.1 thorpej if (lseek(fd, (off_t)ktent->kt_offset, SEEK_SET) < (off_t)0) {
524 1.1 thorpej sprintf(err_str, "lseek: %s: %s", path_eeprom,
525 1.1 thorpej strerror(errno));
526 1.1 thorpej rval = 1;
527 1.1 thorpej goto done;
528 1.1 thorpej }
529 1.1 thorpej
530 1.1 thorpej ++update_checksums;
531 1.1 thorpej if (write(fd, buf, len) < 0) {
532 1.1 thorpej sprintf(err_str, "write: %s: %s", path_eeprom,
533 1.1 thorpej strerror(errno));
534 1.1 thorpej rval = 1;
535 1.1 thorpej goto done;
536 1.1 thorpej }
537 1.1 thorpej } else
538 1.1 thorpej bcopy(buf2, buf, len);
539 1.1 thorpej
540 1.1 thorpej done:
541 1.1 thorpej free(buf2);
542 1.1 thorpej (void)close(fd);
543 1.1 thorpej return (rval);
544 1.1 thorpej }
545 1.1 thorpej
546 1.1 thorpej /*
547 1.1 thorpej * Read from eeLastHwUpdate to just before eeReserved. Calculate
548 1.1 thorpej * a checksum, and deposit 3 copies of it sequentially starting at
549 1.1 thorpej * eeChecksum[0]. Increment the write count, and deposit 3 copies
550 1.1 thorpej * of it sequentially starting at eeWriteCount[0].
551 1.1 thorpej */
552 1.1 thorpej void
553 1.1 thorpej ee_updatechecksums()
554 1.1 thorpej {
555 1.1 thorpej struct keytabent kt;
556 1.1 thorpej u_char checkme[EE_SIZE - EE_HWUPDATE_LOC];
557 1.1 thorpej u_char checksum;
558 1.1 thorpej int i;
559 1.1 thorpej
560 1.1 thorpej kt.kt_keyword = "eeprom contents";
561 1.1 thorpej kt.kt_offset = EE_HWUPDATE_LOC;
562 1.1 thorpej kt.kt_handler = ee_notsupp;
563 1.1 thorpej
564 1.1 thorpej if (doio(&kt, checkme, sizeof(checkme), IO_READ)) {
565 1.1 thorpej cksumfail = 1;
566 1.1 thorpej FAILEDREAD(&kt);
567 1.1 thorpej }
568 1.1 thorpej
569 1.1 thorpej checksum = ee_checksum(checkme, sizeof(checkme));
570 1.1 thorpej
571 1.1 thorpej kt.kt_keyword = "eeprom checksum";
572 1.1 thorpej for (i = 0; i < 4; ++i) {
573 1.1 thorpej kt.kt_offset = EE_CKSUM_LOC + (i * sizeof(checksum));
574 1.1 thorpej if (doio(&kt, &checksum, sizeof(checksum), IO_WRITE)) {
575 1.1 thorpej cksumfail = 1;
576 1.1 thorpej FAILEDWRITE(&kt);
577 1.1 thorpej }
578 1.1 thorpej }
579 1.1 thorpej
580 1.1 thorpej kt.kt_keyword = "eeprom writecount";
581 1.1 thorpej for (i = 0; i < 4; ++i) {
582 1.1 thorpej kt.kt_offset = EE_WC_LOC + (i * sizeof(writecount));
583 1.1 thorpej if (doio(&kt, (u_char *)&writecount, sizeof(writecount),
584 1.1 thorpej IO_WRITE)) {
585 1.1 thorpej cksumfail = 1;
586 1.1 thorpej FAILEDWRITE(&kt);
587 1.1 thorpej }
588 1.1 thorpej }
589 1.1 thorpej }
590 1.1 thorpej
591 1.1 thorpej void
592 1.1 thorpej ee_verifychecksums()
593 1.1 thorpej {
594 1.1 thorpej struct keytabent kt;
595 1.1 thorpej u_char checkme[EE_SIZE - EE_HWUPDATE_LOC];
596 1.1 thorpej u_char checksum, ochecksum[3];
597 1.1 thorpej u_short owritecount[3];
598 1.1 thorpej
599 1.1 thorpej /*
600 1.1 thorpej * Verify that the EEPROM's write counts match, and update the
601 1.1 thorpej * global copy for use later.
602 1.1 thorpej */
603 1.1 thorpej kt.kt_keyword = "eeprom writecount";
604 1.1 thorpej kt.kt_offset = EE_WC_LOC;
605 1.1 thorpej kt.kt_handler = ee_notsupp;
606 1.1 thorpej
607 1.1 thorpej if (doio(&kt, (u_char *)&owritecount, sizeof(owritecount), IO_READ)) {
608 1.1 thorpej cksumfail = 1;
609 1.1 thorpej FAILEDREAD(&kt);
610 1.1 thorpej }
611 1.1 thorpej
612 1.1 thorpej if (owritecount[0] != owritecount[1] ||
613 1.1 thorpej owritecount[0] != owritecount[2]) {
614 1.1 thorpej warnx("eeprom writecount mismatch %s",
615 1.1 thorpej ignore_checksum ? "(ignoring)" :
616 1.1 thorpej (fix_checksum ? "(fixing)" : ""));
617 1.1 thorpej
618 1.1 thorpej if (!ignore_checksum && !fix_checksum) {
619 1.1 thorpej cksumfail = 1;
620 1.1 thorpej return;
621 1.1 thorpej }
622 1.1 thorpej
623 1.1 thorpej writecount = MAXIMUM(owritecount[0], owritecount[1]);
624 1.1 thorpej writecount = MAXIMUM(writecount, owritecount[2]);
625 1.1 thorpej } else
626 1.1 thorpej writecount = owritecount[0];
627 1.1 thorpej
628 1.1 thorpej /*
629 1.1 thorpej * Verify that the EEPROM's checksums match and are correct.
630 1.1 thorpej */
631 1.1 thorpej kt.kt_keyword = "eeprom checksum";
632 1.1 thorpej kt.kt_offset = EE_CKSUM_LOC;
633 1.1 thorpej
634 1.1 thorpej if (doio(&kt, ochecksum, sizeof(ochecksum), IO_READ)) {
635 1.1 thorpej cksumfail = 1;
636 1.1 thorpej FAILEDREAD(&kt);
637 1.1 thorpej }
638 1.1 thorpej
639 1.1 thorpej if (ochecksum[0] != ochecksum[1] ||
640 1.1 thorpej ochecksum[0] != ochecksum[2]) {
641 1.1 thorpej warnx("eeprom checksum mismatch %s",
642 1.1 thorpej ignore_checksum ? "(ignoring)" :
643 1.1 thorpej (fix_checksum ? "(fixing)" : ""));
644 1.1 thorpej
645 1.1 thorpej if (!ignore_checksum && !fix_checksum) {
646 1.1 thorpej cksumfail = 1;
647 1.1 thorpej return;
648 1.1 thorpej }
649 1.1 thorpej }
650 1.1 thorpej
651 1.1 thorpej kt.kt_keyword = "eeprom contents";
652 1.1 thorpej kt.kt_offset = EE_HWUPDATE_LOC;
653 1.1 thorpej
654 1.1 thorpej if (doio(&kt, checkme, sizeof(checkme), IO_READ)) {
655 1.1 thorpej cksumfail = 1;
656 1.1 thorpej FAILEDREAD(&kt);
657 1.1 thorpej }
658 1.1 thorpej
659 1.1 thorpej checksum = ee_checksum(checkme, sizeof(checkme));
660 1.1 thorpej
661 1.1 thorpej if (ochecksum[0] != checksum) {
662 1.1 thorpej warnx("eeprom checksum incorrect %s",
663 1.1 thorpej ignore_checksum ? "(ignoring)" :
664 1.1 thorpej (fix_checksum ? "(fixing)" : ""));
665 1.1 thorpej
666 1.1 thorpej if (!ignore_checksum && !fix_checksum) {
667 1.1 thorpej cksumfail = 1;
668 1.1 thorpej return;
669 1.1 thorpej }
670 1.1 thorpej }
671 1.1 thorpej
672 1.1 thorpej if (fix_checksum)
673 1.1 thorpej ee_updatechecksums();
674 1.1 thorpej }
675 1.1 thorpej
676 1.1 thorpej u_char
677 1.1 thorpej ee_checksum(area, len)
678 1.1 thorpej u_char *area;
679 1.1 thorpej size_t len;
680 1.1 thorpej {
681 1.1 thorpej u_char sum = 0;
682 1.1 thorpej
683 1.1 thorpej while (len--)
684 1.1 thorpej sum += *area++;
685 1.1 thorpej
686 1.1 thorpej return (0x100 - sum);
687 1.1 thorpej }
688