Home | History | Annotate | Line # | Download | only in eeprom
main.c revision 1.15
      1 /*	$NetBSD: main.c,v 1.15 2001/02/19 23:22:42 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT(
     42 "@(#) Copyright (c) 1996 The NetBSD Foundation, Inc.  All rights reserved.");
     43 __RCSID("$NetBSD: main.c,v 1.15 2001/02/19 23:22:42 cgd Exp $");
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <err.h>
     48 #include <string.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <unistd.h>
     52 
     53 #include <machine/eeprom.h>
     54 
     55 #include "defs.h"
     56 #include "pathnames.h"
     57 
     58 #if defined(__sparc__)
     59 # define USE_OPENPROM
     60 # if defined(__arch64__)
     61 #  define ee_action(a,b)
     62 #  define ee_dump()
     63 #  define ee_updatechecksums() (void)0
     64 #  define check_for_openprom() 1
     65 # endif
     66 #endif
     67 
     68 int	main (int, char *[]);
     69 static	void action (char *);
     70 static	void dump_prom (void);
     71 static	void usage (void);
     72 
     73 char	*path_eeprom = _PATH_EEPROM;
     74 char	*path_openprom = _PATH_OPENPROM;
     75 int	fix_checksum = 0;
     76 int	ignore_checksum = 0;
     77 int	update_checksums = 0;
     78 int	cksumfail = 0;
     79 u_short	writecount;
     80 int	eval = 0;
     81 #ifdef USE_OPENPROM
     82 int	verbose = 0;
     83 int	use_openprom;
     84 #endif
     85 
     86 int
     87 main(argc, argv)
     88 	int argc;
     89 	char *argv[];
     90 {
     91 	int ch, do_stdin = 0;
     92 	char *cp, line[BUFSIZE];
     93 #ifdef USE_OPENPROM
     94 	char *optstring = "-cf:iv";
     95 #else
     96 	char *optstring = "-cf:i";
     97 #endif /* USE_OPENPROM */
     98 
     99 	while ((ch = getopt(argc, argv, optstring)) != -1)
    100 		switch (ch) {
    101 		case '-':
    102 			do_stdin = 1;
    103 			break;
    104 
    105 		case 'c':
    106 			fix_checksum = 1;
    107 			break;
    108 
    109 		case 'f':
    110 			path_eeprom = path_openprom = optarg;
    111 			break;
    112 
    113 		case 'i':
    114 			ignore_checksum = 1;
    115 			break;
    116 
    117 #ifdef USE_OPENPROM
    118 		case 'v':
    119 			verbose = 1;
    120 			break;
    121 #endif /* USE_OPENPROM */
    122 
    123 		case '?':
    124 		default:
    125 			usage();
    126 		}
    127 	argc -= optind;
    128 	argv += optind;
    129 
    130 #ifdef USE_OPENPROM
    131 	use_openprom = check_for_openprom();
    132 
    133 	if (use_openprom == 0) {
    134 #endif /* USE_OPENPROM */
    135 		ee_verifychecksums();
    136 		if (fix_checksum || cksumfail)
    137 			exit(cksumfail);
    138 #ifdef USE_OPENPROM
    139 	}
    140 #endif /* USE_OPENPROM */
    141 
    142 	if (do_stdin) {
    143 		while (fgets(line, BUFSIZE, stdin) != NULL) {
    144 			if (line[0] == '\n')
    145 				continue;
    146 			if ((cp = strrchr(line, '\n')) != NULL)
    147 				*cp = '\0';
    148 			action(line);
    149 		}
    150 		if (ferror(stdin))
    151 			err(++eval, "stdin");
    152 	} else {
    153 		if (argc == 0) {
    154 			dump_prom();
    155 			exit(eval + cksumfail);
    156 		}
    157 
    158 		while (argc) {
    159 			action(*argv);
    160 			++argv;
    161 			--argc;
    162 		}
    163 	}
    164 
    165 #ifdef USE_OPENPROM
    166 	if (use_openprom == 0)
    167 #endif /* USE_OPENPROM */
    168 		if (update_checksums) {
    169 			++writecount;
    170 			ee_updatechecksums();
    171 		}
    172 
    173 	exit(eval + cksumfail);
    174 }
    175 
    176 /*
    177  * Separate the keyword from the argument (if any), find the keyword in
    178  * the table, and call the corresponding handler function.
    179  */
    180 static void
    181 action(line)
    182 	char *line;
    183 {
    184 	char *keyword, *arg;
    185 
    186 	keyword = strdup(line);
    187 	if ((arg = strrchr(keyword, '=')) != NULL)
    188 		*arg++ = '\0';
    189 
    190 #ifdef USE_OPENPROM
    191 	if (use_openprom)
    192 		op_action(keyword, arg);
    193 	else
    194 #endif /* USE_OPENPROM */
    195 		ee_action(keyword, arg);
    196 }
    197 
    198 /*
    199  * Dump the contents of the prom corresponding to all known keywords.
    200  */
    201 static void
    202 dump_prom()
    203 {
    204 
    205 #ifdef USE_OPENPROM
    206 	if (use_openprom)
    207 		/*
    208 		 * We have a special dump routine for this.
    209 		 */
    210 		op_dump();
    211 	else
    212 #endif /* USE_OPENPROM */
    213 		ee_dump();
    214 }
    215 
    216 static void
    217 usage()
    218 {
    219 
    220 #ifdef USE_OPENPROM
    221 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    222 	    "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
    223 #else
    224 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    225 	    "[-] [-c] [-f device] [-i] [field[=value] ...]");
    226 #endif /* __us */
    227 	exit(1);
    228 }
    229