Home | History | Annotate | Line # | Download | only in eeprom
main.c revision 1.17
      1  1.17   garbled /*	$NetBSD: main.c,v 1.17 2007/03/01 16:49:48 garbled 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.7       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.7       jtc  * BE 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.5   thorpej #include <sys/cdefs.h>
     40   1.5   thorpej #ifndef lint
     41   1.5   thorpej __COPYRIGHT(
     42   1.5   thorpej "@(#) Copyright (c) 1996 The NetBSD Foundation, Inc.  All rights reserved.");
     43  1.17   garbled __RCSID("$NetBSD: main.c,v 1.17 2007/03/01 16:49:48 garbled Exp $");
     44   1.5   thorpej #endif
     45   1.5   thorpej 
     46   1.1   thorpej #include <sys/param.h>
     47   1.1   thorpej #include <err.h>
     48   1.1   thorpej #include <string.h>
     49   1.1   thorpej #include <stdio.h>
     50  1.15       cgd #include <stdlib.h>
     51   1.1   thorpej #include <unistd.h>
     52   1.1   thorpej 
     53  1.16  macallan #ifdef __sun__
     54   1.1   thorpej #include <machine/eeprom.h>
     55  1.16  macallan #endif
     56   1.1   thorpej 
     57   1.1   thorpej #include "defs.h"
     58  1.10     bjh21 #include "pathnames.h"
     59   1.1   thorpej 
     60  1.12       mrg #if defined(__sparc__)
     61  1.12       mrg # define USE_OPENPROM
     62  1.12       mrg # if defined(__arch64__)
     63  1.12       mrg #  define ee_action(a,b)
     64  1.12       mrg #  define ee_dump()
     65  1.12       mrg #  define ee_updatechecksums() (void)0
     66  1.12       mrg #  define check_for_openprom() 1
     67  1.12       mrg # endif
     68  1.12       mrg #endif
     69   1.1   thorpej 
     70  1.11       mrg int	main (int, char *[]);
     71  1.11       mrg static	void action (char *);
     72  1.11       mrg static	void dump_prom (void);
     73  1.11       mrg static	void usage (void);
     74   1.1   thorpej 
     75  1.10     bjh21 char	*path_eeprom = _PATH_EEPROM;
     76  1.10     bjh21 char	*path_openprom = _PATH_OPENPROM;
     77  1.16  macallan char	*path_openfirm = _PATH_OPENFIRM;
     78  1.17   garbled char	*path_prepnvram = _PATH_PREPNVRAM;
     79   1.1   thorpej int	fix_checksum = 0;
     80   1.1   thorpej int	ignore_checksum = 0;
     81   1.1   thorpej int	update_checksums = 0;
     82   1.1   thorpej int	cksumfail = 0;
     83   1.1   thorpej u_short	writecount;
     84   1.1   thorpej int	eval = 0;
     85  1.12       mrg #ifdef USE_OPENPROM
     86   1.1   thorpej int	verbose = 0;
     87  1.12       mrg int	use_openprom;
     88  1.12       mrg #endif
     89  1.17   garbled #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM)
     90  1.16  macallan int	verbose=0;
     91  1.16  macallan #endif
     92   1.1   thorpej 
     93   1.1   thorpej int
     94   1.1   thorpej main(argc, argv)
     95   1.1   thorpej 	int argc;
     96   1.5   thorpej 	char *argv[];
     97   1.1   thorpej {
     98   1.1   thorpej 	int ch, do_stdin = 0;
     99   1.1   thorpej 	char *cp, line[BUFSIZE];
    100  1.17   garbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
    101  1.12       mrg 	char *optstring = "-cf:iv";
    102   1.1   thorpej #else
    103   1.1   thorpej 	char *optstring = "-cf:i";
    104  1.12       mrg #endif /* USE_OPENPROM */
    105   1.1   thorpej 
    106   1.1   thorpej 	while ((ch = getopt(argc, argv, optstring)) != -1)
    107   1.1   thorpej 		switch (ch) {
    108   1.1   thorpej 		case '-':
    109   1.1   thorpej 			do_stdin = 1;
    110   1.1   thorpej 			break;
    111   1.1   thorpej 
    112   1.1   thorpej 		case 'c':
    113   1.1   thorpej 			fix_checksum = 1;
    114   1.1   thorpej 			break;
    115   1.1   thorpej 
    116   1.1   thorpej 		case 'f':
    117   1.1   thorpej 			path_eeprom = path_openprom = optarg;
    118   1.1   thorpej 			break;
    119   1.1   thorpej 
    120   1.1   thorpej 		case 'i':
    121   1.1   thorpej 			ignore_checksum = 1;
    122   1.1   thorpej 			break;
    123  1.12       mrg 
    124  1.17   garbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
    125   1.1   thorpej 		case 'v':
    126   1.1   thorpej 			verbose = 1;
    127   1.1   thorpej 			break;
    128  1.12       mrg #endif /* USE_OPENPROM */
    129   1.1   thorpej 
    130   1.1   thorpej 		case '?':
    131   1.1   thorpej 		default:
    132   1.1   thorpej 			usage();
    133   1.1   thorpej 		}
    134   1.1   thorpej 	argc -= optind;
    135   1.1   thorpej 	argv += optind;
    136   1.1   thorpej 
    137  1.12       mrg #ifdef USE_OPENPROM
    138  1.12       mrg 	use_openprom = check_for_openprom();
    139   1.1   thorpej 
    140   1.1   thorpej 	if (use_openprom == 0) {
    141  1.14       jmc #endif /* USE_OPENPROM */
    142  1.17   garbled #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
    143   1.1   thorpej 		ee_verifychecksums();
    144   1.1   thorpej 		if (fix_checksum || cksumfail)
    145   1.1   thorpej 			exit(cksumfail);
    146  1.16  macallan #endif
    147  1.14       jmc #ifdef USE_OPENPROM
    148   1.1   thorpej 	}
    149  1.13       jmc #endif /* USE_OPENPROM */
    150   1.1   thorpej 
    151   1.1   thorpej 	if (do_stdin) {
    152   1.1   thorpej 		while (fgets(line, BUFSIZE, stdin) != NULL) {
    153   1.1   thorpej 			if (line[0] == '\n')
    154   1.1   thorpej 				continue;
    155   1.1   thorpej 			if ((cp = strrchr(line, '\n')) != NULL)
    156   1.1   thorpej 				*cp = '\0';
    157   1.1   thorpej 			action(line);
    158   1.1   thorpej 		}
    159   1.1   thorpej 		if (ferror(stdin))
    160   1.1   thorpej 			err(++eval, "stdin");
    161   1.1   thorpej 	} else {
    162   1.1   thorpej 		if (argc == 0) {
    163   1.1   thorpej 			dump_prom();
    164   1.1   thorpej 			exit(eval + cksumfail);
    165   1.1   thorpej 		}
    166   1.1   thorpej 
    167   1.1   thorpej 		while (argc) {
    168   1.3   thorpej 			action(*argv);
    169   1.1   thorpej 			++argv;
    170   1.1   thorpej 			--argc;
    171   1.1   thorpej 		}
    172   1.1   thorpej 	}
    173   1.1   thorpej 
    174  1.12       mrg #ifdef USE_OPENPROM
    175   1.1   thorpej 	if (use_openprom == 0)
    176  1.12       mrg #endif /* USE_OPENPROM */
    177  1.17   garbled #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
    178   1.1   thorpej 		if (update_checksums) {
    179   1.1   thorpej 			++writecount;
    180   1.1   thorpej 			ee_updatechecksums();
    181   1.1   thorpej 		}
    182   1.1   thorpej 
    183   1.1   thorpej 	exit(eval + cksumfail);
    184  1.16  macallan #endif
    185  1.16  macallan 	return 0;
    186   1.1   thorpej }
    187   1.1   thorpej 
    188   1.1   thorpej /*
    189   1.1   thorpej  * Separate the keyword from the argument (if any), find the keyword in
    190   1.1   thorpej  * the table, and call the corresponding handler function.
    191   1.1   thorpej  */
    192   1.1   thorpej static void
    193   1.1   thorpej action(line)
    194   1.1   thorpej 	char *line;
    195   1.1   thorpej {
    196   1.6      fair 	char *keyword, *arg;
    197   1.1   thorpej 
    198   1.1   thorpej 	keyword = strdup(line);
    199   1.1   thorpej 	if ((arg = strrchr(keyword, '=')) != NULL)
    200   1.1   thorpej 		*arg++ = '\0';
    201   1.1   thorpej 
    202  1.17   garbled #ifdef USE_PREPNVRAM
    203  1.17   garbled 	prep_action(keyword, arg);
    204  1.17   garbled #else
    205  1.16  macallan #ifdef USE_OPENFIRM
    206  1.16  macallan 	of_action(keyword, arg);
    207  1.16  macallan #else
    208  1.12       mrg #ifdef USE_OPENPROM
    209  1.12       mrg 	if (use_openprom)
    210  1.12       mrg 		op_action(keyword, arg);
    211  1.12       mrg 	else
    212  1.12       mrg #endif /* USE_OPENPROM */
    213  1.12       mrg 		ee_action(keyword, arg);
    214  1.16  macallan #endif /* USE_OPENFIRM */
    215  1.17   garbled #endif /* USE_PREPNVRAM */
    216   1.1   thorpej }
    217   1.1   thorpej 
    218   1.1   thorpej /*
    219   1.1   thorpej  * Dump the contents of the prom corresponding to all known keywords.
    220   1.1   thorpej  */
    221   1.1   thorpej static void
    222   1.1   thorpej dump_prom()
    223   1.1   thorpej {
    224   1.1   thorpej 
    225  1.17   garbled #ifdef USE_PREPNVRAM
    226  1.17   garbled 	prep_dump();
    227  1.17   garbled #else
    228  1.16  macallan #ifdef USE_OPENFIRM
    229  1.16  macallan 	of_dump();
    230  1.16  macallan #else
    231  1.12       mrg #ifdef USE_OPENPROM
    232  1.12       mrg 	if (use_openprom)
    233   1.1   thorpej 		/*
    234   1.1   thorpej 		 * We have a special dump routine for this.
    235   1.1   thorpej 		 */
    236   1.1   thorpej 		op_dump();
    237  1.12       mrg 	else
    238  1.12       mrg #endif /* USE_OPENPROM */
    239  1.12       mrg 		ee_dump();
    240  1.16  macallan #endif /* USE_OPENFIRM */
    241  1.17   garbled #endif /* USE_PREPNVRAM */
    242   1.1   thorpej }
    243   1.1   thorpej 
    244   1.1   thorpej static void
    245   1.1   thorpej usage()
    246   1.1   thorpej {
    247   1.1   thorpej 
    248  1.17   garbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
    249  1.15       cgd 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    250  1.12       mrg 	    "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
    251   1.1   thorpej #else
    252  1.15       cgd 	fprintf(stderr, "usage: %s %s\n", getprogname(),
    253   1.1   thorpej 	    "[-] [-c] [-f device] [-i] [field[=value] ...]");
    254  1.12       mrg #endif /* __us */
    255   1.1   thorpej 	exit(1);
    256   1.1   thorpej }
    257