Home | History | Annotate | Line # | Download | only in eeprom
      1 /*	$NetBSD: main.c,v 1.23 2013/07/02 11:59:46 joerg 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1996\
     35  The NetBSD Foundation, Inc.  All rights reserved.");
     36 __RCSID("$NetBSD: main.c,v 1.23 2013/07/02 11:59:46 joerg Exp $");
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <err.h>
     41 #include <string.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <unistd.h>
     45 
     46 #if defined(USE_EEPROM) || defined(USE_OPENPROM)
     47 #include <machine/eeprom.h>
     48 #endif
     49 
     50 #include "defs.h"
     51 #include "pathnames.h"
     52 
     53 #ifdef USE_OPENPROM
     54 # ifndef USE_EEPROM
     55 #  define ee_action(a,b)
     56 #  define ee_dump()
     57 #  define ee_updatechecksums() (void)0
     58 #  define check_for_openprom() 1
     59 # endif
     60 #endif
     61 
     62 static	void action(char *);
     63 static	void dump_prom(void);
     64 static	void usage(void) __dead;
     65 
     66 const char *path_eeprom = _PATH_EEPROM;
     67 const char *path_openprom = _PATH_OPENPROM;
     68 const char *path_openfirm = _PATH_OPENFIRM;
     69 const char *path_prepnvram = _PATH_PREPNVRAM;
     70 int	fix_checksum = 0;
     71 int	ignore_checksum = 0;
     72 int	update_checksums = 0;
     73 int	cksumfail = 0;
     74 u_short	writecount;
     75 int	eval = 0;
     76 #ifdef USE_OPENPROM
     77 int	verbose = 0;
     78 int	use_openprom;
     79 #endif
     80 #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM)
     81 int	verbose=0;
     82 #endif
     83 
     84 int
     85 main(int argc, char *argv[])
     86 {
     87 	int ch, do_stdin = 0;
     88 	char *cp, line[BUFSIZE];
     89 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
     90 	const char *optstring = "-cf:iv";
     91 #else
     92 	const char *optstring = "-cf:i";
     93 #endif /* USE_OPENPROM */
     94 
     95 	while ((ch = getopt(argc, argv, optstring)) != -1)
     96 		switch (ch) {
     97 		case '-':
     98 			do_stdin = 1;
     99 			break;
    100 
    101 		case 'c':
    102 			fix_checksum = 1;
    103 			break;
    104 
    105 		case 'f':
    106 			path_eeprom = path_openprom = optarg;
    107 			break;
    108 
    109 		case 'i':
    110 			ignore_checksum = 1;
    111 			break;
    112 
    113 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
    114 		case 'v':
    115 			verbose = 1;
    116 			break;
    117 #endif /* USE_OPENPROM */
    118 
    119 		case '?':
    120 		default:
    121 			usage();
    122 		}
    123 	argc -= optind;
    124 	argv += optind;
    125 
    126 #ifdef USE_OPENPROM
    127 	use_openprom = check_for_openprom();
    128 
    129 	if (use_openprom == 0) {
    130 #endif /* USE_OPENPROM */
    131 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
    132 		ee_verifychecksums();
    133 		if (fix_checksum || cksumfail)
    134 			exit(cksumfail);
    135 #endif
    136 #ifdef USE_OPENPROM
    137 	}
    138 #endif /* USE_OPENPROM */
    139 
    140 	if (do_stdin) {
    141 		while (fgets(line, BUFSIZE, stdin) != NULL) {
    142 			if (line[0] == '\n')
    143 				continue;
    144 			if ((cp = strrchr(line, '\n')) != NULL)
    145 				*cp = '\0';
    146 			action(line);
    147 		}
    148 		if (ferror(stdin))
    149 			err(++eval, "stdin");
    150 	} else {
    151 		if (argc == 0) {
    152 			dump_prom();
    153 			exit(eval + cksumfail);
    154 		}
    155 
    156 		while (argc) {
    157 			action(*argv);
    158 			++argv;
    159 			--argc;
    160 		}
    161 	}
    162 
    163 #ifdef USE_OPENPROM
    164 	if (use_openprom == 0)
    165 #endif /* USE_OPENPROM */
    166 #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
    167 		if (update_checksums) {
    168 			++writecount;
    169 			ee_updatechecksums();
    170 		}
    171 
    172 	exit(eval + cksumfail);
    173 #endif
    174 	return 0;
    175 }
    176 
    177 /*
    178  * Separate the keyword from the argument (if any), find the keyword in
    179  * the table, and call the corresponding handler function.
    180  */
    181 static void
    182 action(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_PREPNVRAM
    191 	prep_action(keyword, arg);
    192 #else
    193 #ifdef USE_OPENFIRM
    194 	of_action(keyword, arg);
    195 #else
    196 #ifdef USE_OPENPROM
    197 	if (use_openprom)
    198 		op_action(keyword, arg);
    199 	else {
    200 #endif /* USE_OPENPROM */
    201 		ee_action(keyword, arg);
    202 #ifdef USE_OPENPROM
    203 	}
    204 #endif /* USE_OPENPROM */
    205 #endif /* USE_OPENFIRM */
    206 #endif /* USE_PREPNVRAM */
    207 }
    208 
    209 /*
    210  * Dump the contents of the prom corresponding to all known keywords.
    211  */
    212 static void
    213 dump_prom(void)
    214 {
    215 
    216 #ifdef USE_PREPNVRAM
    217 	prep_dump();
    218 #else
    219 #ifdef USE_OPENFIRM
    220 	of_dump();
    221 #else
    222 #ifdef USE_OPENPROM
    223 	if (use_openprom)
    224 		/*
    225 		 * We have a special dump routine for this.
    226 		 */
    227 		op_dump();
    228 	else {
    229 #endif /* USE_OPENPROM */
    230 		ee_dump();
    231 #ifdef USE_OPENPROM
    232 	}
    233 #endif /* USE_OPENPROM */
    234 #endif /* USE_OPENFIRM */
    235 #endif /* USE_PREPNVRAM */
    236 }
    237 
    238 __dead static void
    239 usage(void)
    240 {
    241 
    242 #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
    243 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    244 	    "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
    245 #else
    246 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    247 	    "[-] [-c] [-f device] [-i] [field[=value] ...]");
    248 #endif /* __us */
    249 	exit(1);
    250 }
    251