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