1 /* $OpenBSD: main.c,v 1.25 2020/01/09 19:33:19 kn Exp $ */ 2 /* $NetBSD: main.c,v 1.3 1996/05/16 16:00:55 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1996 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <err.h> 34 #include <string.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 39 40 #include <machine/openpromio.h> 41 42 #include "defs.h" 43 44 static void action(char *); 45 static void dump_prom(void); 46 static void usage(void); 47 48 char *path_eeprom = "/dev/eeprom"; 49 char *path_openprom = "/dev/openprom"; 50 int fix_checksum = 0; 51 int ignore_checksum = 0; 52 int update_checksums = 0; 53 int cksumfail = 0; 54 u_short writecount; 55 int eval = 0; 56 int print_tree = 0; 57 int verbose = 0; 58 59 extern char *__progname; 60 61 int 62 main(int argc, char *argv[]) 63 { 64 int ch, do_stdin = 0; 65 char *cp, line[BUFSIZE]; 66 char *optstring = "cf:ipvN:-"; 67 68 while ((ch = getopt(argc, argv, optstring)) != -1) 69 switch (ch) { 70 case '-': 71 do_stdin = 1; 72 break; 73 74 case 'c': 75 fix_checksum = 1; 76 break; 77 78 case 'f': 79 path_eeprom = path_openprom = optarg; 80 break; 81 82 case 'i': 83 ignore_checksum = 1; 84 break; 85 86 case 'p': 87 print_tree = 1; 88 break; 89 90 case 'v': 91 verbose = 1; 92 break; 93 94 case '?': 95 default: 96 usage(); 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (print_tree) { 102 op_tree(); 103 exit(0); 104 } 105 106 if (do_stdin) { 107 while (fgets(line, BUFSIZE, stdin) != NULL) { 108 if (line[0] == '\n') 109 continue; 110 if ((cp = strrchr(line, '\n')) != NULL) 111 *cp = '\0'; 112 action(line); 113 } 114 if (ferror(stdin)) 115 err(++eval, "stdin"); 116 } else { 117 if (argc == 0) { 118 dump_prom(); 119 exit(eval + cksumfail); 120 } 121 122 while (argc) { 123 action(*argv); 124 ++argv; 125 --argc; 126 } 127 } 128 129 exit(eval + cksumfail); 130 } 131 132 /* 133 * Separate the keyword from the argument (if any), find the keyword in 134 * the table, and call the corresponding handler function. 135 */ 136 static void 137 action(char *line) 138 { 139 char *keyword, *arg, *cp; 140 141 keyword = strdup(line); 142 if (!keyword) 143 errx(1, "out of memory"); 144 if ((arg = strrchr(keyword, '=')) != NULL) 145 *arg++ = '\0'; 146 147 /* 148 * The whole point of the Openprom is that one 149 * isn't required to know the keywords. With this 150 * in mind, we just dump the whole thing off to 151 * the generic op_handler. 152 */ 153 if ((cp = op_handler(keyword, arg)) != NULL) 154 warnx("%s", cp); 155 } 156 157 /* 158 * Dump the contents of the prom corresponding to all known keywords. 159 */ 160 static void 161 dump_prom(void) 162 { 163 /* 164 * We have a special dump routine for this. 165 */ 166 op_dump(); 167 } 168 169 static void 170 usage(void) 171 { 172 173 fprintf(stderr, 174 "usage: %s [-cipv] [-f device] [field[=value] ...]\n", 175 __progname); 176 exit(1); 177 } 178