1*ca1a6f97Sjoerg /* $NetBSD: main.c,v 1.23 2013/07/02 11:59:46 joerg Exp $ */
2da9b1f76Sthorpej
3c479d69aSthorpej /*-
4c479d69aSthorpej * Copyright (c) 1996 The NetBSD Foundation, Inc.
5da9b1f76Sthorpej * All rights reserved.
6da9b1f76Sthorpej *
7c479d69aSthorpej * This code is derived from software contributed to The NetBSD Foundation
8c479d69aSthorpej * by Jason R. Thorpe.
9c479d69aSthorpej *
10da9b1f76Sthorpej * Redistribution and use in source and binary forms, with or without
11da9b1f76Sthorpej * modification, are permitted provided that the following conditions
12da9b1f76Sthorpej * are met:
13da9b1f76Sthorpej * 1. Redistributions of source code must retain the above copyright
14da9b1f76Sthorpej * notice, this list of conditions and the following disclaimer.
15da9b1f76Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
16da9b1f76Sthorpej * notice, this list of conditions and the following disclaimer in the
17da9b1f76Sthorpej * documentation and/or other materials provided with the distribution.
18da9b1f76Sthorpej *
19c479d69aSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c479d69aSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c479d69aSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2287f4ccd4Sjtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2387f4ccd4Sjtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c479d69aSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c479d69aSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c479d69aSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c479d69aSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c479d69aSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c479d69aSthorpej * POSSIBILITY OF SUCH DAMAGE.
30da9b1f76Sthorpej */
31da9b1f76Sthorpej
322434f068Sthorpej #include <sys/cdefs.h>
332434f068Sthorpej #ifndef lint
349c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1996\
359c194566Slukem The NetBSD Foundation, Inc. All rights reserved.");
36*ca1a6f97Sjoerg __RCSID("$NetBSD: main.c,v 1.23 2013/07/02 11:59:46 joerg Exp $");
372434f068Sthorpej #endif
382434f068Sthorpej
39da9b1f76Sthorpej #include <sys/param.h>
40da9b1f76Sthorpej #include <err.h>
41da9b1f76Sthorpej #include <string.h>
42da9b1f76Sthorpej #include <stdio.h>
4325bdbb66Scgd #include <stdlib.h>
44da9b1f76Sthorpej #include <unistd.h>
45da9b1f76Sthorpej
4661db04d3Snakayama #if defined(USE_EEPROM) || defined(USE_OPENPROM)
47da9b1f76Sthorpej #include <machine/eeprom.h>
48ad53a0a5Smacallan #endif
49da9b1f76Sthorpej
50da9b1f76Sthorpej #include "defs.h"
51ff579c68Sbjh21 #include "pathnames.h"
52da9b1f76Sthorpej
5361db04d3Snakayama #ifdef USE_OPENPROM
5461db04d3Snakayama # ifndef USE_EEPROM
556ce96c99Smrg # define ee_action(a,b)
566ce96c99Smrg # define ee_dump()
576ce96c99Smrg # define ee_updatechecksums() (void)0
586ce96c99Smrg # define check_for_openprom() 1
596ce96c99Smrg # endif
606ce96c99Smrg #endif
61da9b1f76Sthorpej
627996c8deSmrg static void action(char *);
637996c8deSmrg static void dump_prom(void);
64*ca1a6f97Sjoerg static void usage(void) __dead;
65da9b1f76Sthorpej
667a4c7e7fSlukem const char *path_eeprom = _PATH_EEPROM;
677a4c7e7fSlukem const char *path_openprom = _PATH_OPENPROM;
687a4c7e7fSlukem const char *path_openfirm = _PATH_OPENFIRM;
697a4c7e7fSlukem const char *path_prepnvram = _PATH_PREPNVRAM;
70da9b1f76Sthorpej int fix_checksum = 0;
71da9b1f76Sthorpej int ignore_checksum = 0;
72da9b1f76Sthorpej int update_checksums = 0;
73da9b1f76Sthorpej int cksumfail = 0;
74da9b1f76Sthorpej u_short writecount;
75da9b1f76Sthorpej int eval = 0;
766ce96c99Smrg #ifdef USE_OPENPROM
77da9b1f76Sthorpej int verbose = 0;
786ce96c99Smrg int use_openprom;
796ce96c99Smrg #endif
809cc2c6c4Sgarbled #if defined(USE_OPENFIRM) || defined (USE_PREPNVRAM)
81ad53a0a5Smacallan int verbose=0;
82ad53a0a5Smacallan #endif
83da9b1f76Sthorpej
84da9b1f76Sthorpej int
main(int argc,char * argv[])85*ca1a6f97Sjoerg main(int argc, char *argv[])
86da9b1f76Sthorpej {
87da9b1f76Sthorpej int ch, do_stdin = 0;
88da9b1f76Sthorpej char *cp, line[BUFSIZE];
899cc2c6c4Sgarbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
907a4c7e7fSlukem const char *optstring = "-cf:iv";
91da9b1f76Sthorpej #else
921d09b9bbSnakayama const char *optstring = "-cf:i";
936ce96c99Smrg #endif /* USE_OPENPROM */
94da9b1f76Sthorpej
95da9b1f76Sthorpej while ((ch = getopt(argc, argv, optstring)) != -1)
96da9b1f76Sthorpej switch (ch) {
97da9b1f76Sthorpej case '-':
98da9b1f76Sthorpej do_stdin = 1;
99da9b1f76Sthorpej break;
100da9b1f76Sthorpej
101da9b1f76Sthorpej case 'c':
102da9b1f76Sthorpej fix_checksum = 1;
103da9b1f76Sthorpej break;
104da9b1f76Sthorpej
105da9b1f76Sthorpej case 'f':
106da9b1f76Sthorpej path_eeprom = path_openprom = optarg;
107da9b1f76Sthorpej break;
108da9b1f76Sthorpej
109da9b1f76Sthorpej case 'i':
110da9b1f76Sthorpej ignore_checksum = 1;
111da9b1f76Sthorpej break;
1126ce96c99Smrg
1139cc2c6c4Sgarbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
114da9b1f76Sthorpej case 'v':
115da9b1f76Sthorpej verbose = 1;
116da9b1f76Sthorpej break;
1176ce96c99Smrg #endif /* USE_OPENPROM */
118da9b1f76Sthorpej
119da9b1f76Sthorpej case '?':
120da9b1f76Sthorpej default:
121da9b1f76Sthorpej usage();
122da9b1f76Sthorpej }
123da9b1f76Sthorpej argc -= optind;
124da9b1f76Sthorpej argv += optind;
125da9b1f76Sthorpej
1266ce96c99Smrg #ifdef USE_OPENPROM
1276ce96c99Smrg use_openprom = check_for_openprom();
128da9b1f76Sthorpej
129da9b1f76Sthorpej if (use_openprom == 0) {
1305ec07d55Sjmc #endif /* USE_OPENPROM */
1319cc2c6c4Sgarbled #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
132da9b1f76Sthorpej ee_verifychecksums();
133da9b1f76Sthorpej if (fix_checksum || cksumfail)
134da9b1f76Sthorpej exit(cksumfail);
135ad53a0a5Smacallan #endif
1365ec07d55Sjmc #ifdef USE_OPENPROM
137da9b1f76Sthorpej }
138f8c082d5Sjmc #endif /* USE_OPENPROM */
139da9b1f76Sthorpej
140da9b1f76Sthorpej if (do_stdin) {
141da9b1f76Sthorpej while (fgets(line, BUFSIZE, stdin) != NULL) {
142da9b1f76Sthorpej if (line[0] == '\n')
143da9b1f76Sthorpej continue;
144da9b1f76Sthorpej if ((cp = strrchr(line, '\n')) != NULL)
145da9b1f76Sthorpej *cp = '\0';
146da9b1f76Sthorpej action(line);
147da9b1f76Sthorpej }
148da9b1f76Sthorpej if (ferror(stdin))
149da9b1f76Sthorpej err(++eval, "stdin");
150da9b1f76Sthorpej } else {
151da9b1f76Sthorpej if (argc == 0) {
152da9b1f76Sthorpej dump_prom();
153da9b1f76Sthorpej exit(eval + cksumfail);
154da9b1f76Sthorpej }
155da9b1f76Sthorpej
156da9b1f76Sthorpej while (argc) {
1577356f2c7Sthorpej action(*argv);
158da9b1f76Sthorpej ++argv;
159da9b1f76Sthorpej --argc;
160da9b1f76Sthorpej }
161da9b1f76Sthorpej }
162da9b1f76Sthorpej
1636ce96c99Smrg #ifdef USE_OPENPROM
164da9b1f76Sthorpej if (use_openprom == 0)
1656ce96c99Smrg #endif /* USE_OPENPROM */
1669cc2c6c4Sgarbled #if !defined(USE_OPENFIRM) && !defined(USE_PREPNVRAM)
167da9b1f76Sthorpej if (update_checksums) {
168da9b1f76Sthorpej ++writecount;
169da9b1f76Sthorpej ee_updatechecksums();
170da9b1f76Sthorpej }
171da9b1f76Sthorpej
172da9b1f76Sthorpej exit(eval + cksumfail);
173ad53a0a5Smacallan #endif
174ad53a0a5Smacallan return 0;
175da9b1f76Sthorpej }
176da9b1f76Sthorpej
177da9b1f76Sthorpej /*
178da9b1f76Sthorpej * Separate the keyword from the argument (if any), find the keyword in
179da9b1f76Sthorpej * the table, and call the corresponding handler function.
180da9b1f76Sthorpej */
181da9b1f76Sthorpej static void
action(char * line)182*ca1a6f97Sjoerg action(char *line)
183da9b1f76Sthorpej {
1841f759605Sfair char *keyword, *arg;
185da9b1f76Sthorpej
186da9b1f76Sthorpej keyword = strdup(line);
187da9b1f76Sthorpej if ((arg = strrchr(keyword, '=')) != NULL)
188da9b1f76Sthorpej *arg++ = '\0';
189da9b1f76Sthorpej
1909cc2c6c4Sgarbled #ifdef USE_PREPNVRAM
1919cc2c6c4Sgarbled prep_action(keyword, arg);
1929cc2c6c4Sgarbled #else
193ad53a0a5Smacallan #ifdef USE_OPENFIRM
194ad53a0a5Smacallan of_action(keyword, arg);
195ad53a0a5Smacallan #else
1966ce96c99Smrg #ifdef USE_OPENPROM
1976ce96c99Smrg if (use_openprom)
1986ce96c99Smrg op_action(keyword, arg);
1997a4c7e7fSlukem else {
2006ce96c99Smrg #endif /* USE_OPENPROM */
2016ce96c99Smrg ee_action(keyword, arg);
2027a4c7e7fSlukem #ifdef USE_OPENPROM
2037a4c7e7fSlukem }
2047a4c7e7fSlukem #endif /* USE_OPENPROM */
205ad53a0a5Smacallan #endif /* USE_OPENFIRM */
2069cc2c6c4Sgarbled #endif /* USE_PREPNVRAM */
207da9b1f76Sthorpej }
208da9b1f76Sthorpej
209da9b1f76Sthorpej /*
210da9b1f76Sthorpej * Dump the contents of the prom corresponding to all known keywords.
211da9b1f76Sthorpej */
212da9b1f76Sthorpej static void
dump_prom(void)213*ca1a6f97Sjoerg dump_prom(void)
214da9b1f76Sthorpej {
215da9b1f76Sthorpej
2169cc2c6c4Sgarbled #ifdef USE_PREPNVRAM
2179cc2c6c4Sgarbled prep_dump();
2189cc2c6c4Sgarbled #else
219ad53a0a5Smacallan #ifdef USE_OPENFIRM
220ad53a0a5Smacallan of_dump();
221ad53a0a5Smacallan #else
2226ce96c99Smrg #ifdef USE_OPENPROM
2236ce96c99Smrg if (use_openprom)
224da9b1f76Sthorpej /*
225da9b1f76Sthorpej * We have a special dump routine for this.
226da9b1f76Sthorpej */
227da9b1f76Sthorpej op_dump();
2287a4c7e7fSlukem else {
2296ce96c99Smrg #endif /* USE_OPENPROM */
2306ce96c99Smrg ee_dump();
2317a4c7e7fSlukem #ifdef USE_OPENPROM
2327a4c7e7fSlukem }
2337a4c7e7fSlukem #endif /* USE_OPENPROM */
234ad53a0a5Smacallan #endif /* USE_OPENFIRM */
2359cc2c6c4Sgarbled #endif /* USE_PREPNVRAM */
236da9b1f76Sthorpej }
237da9b1f76Sthorpej
238*ca1a6f97Sjoerg __dead static void
usage(void)239*ca1a6f97Sjoerg usage(void)
240da9b1f76Sthorpej {
241da9b1f76Sthorpej
2429cc2c6c4Sgarbled #if defined(USE_OPENPROM) || defined(USE_OPENFIRM) || defined(USE_PREPNVRAM)
24325bdbb66Scgd fprintf(stderr, "usage: %s %s\n", getprogname(),
2446ce96c99Smrg "[-] [-c] [-f device] [-i] [-v] [field[=value] ...]");
245da9b1f76Sthorpej #else
24625bdbb66Scgd fprintf(stderr, "usage: %s %s\n", getprogname(),
247da9b1f76Sthorpej "[-] [-c] [-f device] [-i] [field[=value] ...]");
2486ce96c99Smrg #endif /* __us */
249da9b1f76Sthorpej exit(1);
250da9b1f76Sthorpej }
251