44a19c9953201535fd1dd3431a3d18500e8bd354
[coreboot.git] / util / nvramtool / opts.c
1 /*****************************************************************************\
2  * opts.c
3  *****************************************************************************
4  *  Copyright (C) 2002-2005 The Regents of the University of California.
5  *  Produced at the Lawrence Livermore National Laboratory.
6  *  Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
7  *  UCRL-CODE-2003-012
8  *  All rights reserved.
9  *
10  *  This file is part of nvramtool, a utility for reading/writing coreboot
11  *  parameters and displaying information from the coreboot table.
12  *  For details, see http://coreboot.org/nvramtool.
13  *
14  *  Please also read the file DISCLAIMER which is included in this software
15  *  distribution.
16  *
17  *  This program is free software; you can redistribute it and/or modify it
18  *  under the terms of the GNU General Public License (as published by the
19  *  Free Software Foundation) version 2, dated June 1991.
20  *
21  *  This program is distributed in the hope that it will be useful, but
22  *  WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the terms and
24  *  conditions of the GNU General Public License for more details.
25  *
26  *  You should have received a copy of the GNU General Public License along
27  *  with this program; if not, write to the Free Software Foundation, Inc.,
28  *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 \*****************************************************************************/
30
31 #include "common.h"
32 #include "opts.h"
33
34 nvramtool_op_info_t nvramtool_op;
35
36 nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
37
38 static char *handle_optional_arg(int argc, char *argv[]);
39 static void register_op(int *op_found, nvramtool_op_t op, char op_param[]);
40 static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]);
41 static void resolve_op_modifiers(void);
42 static void sanity_check_args(void);
43
44 static const char getopt_string[] = "-ab:B:c::de:hil::np:r:tvw:xX:y:Y";
45
46 /****************************************************************************
47  * parse_nvramtool_args
48  *
49  * Parse command line arguments.
50  ****************************************************************************/
51 void parse_nvramtool_args(int argc, char *argv[])
52 {
53         nvramtool_op_modifier_info_t *mod_info;
54         int i, op_found;
55         char c;
56
57         for (i = 0, mod_info = nvramtool_op_modifiers;
58              i < NVRAMTOOL_NUM_OP_MODIFIERS; i++, mod_info++) {
59                 mod_info->found = FALSE;
60                 mod_info->found_seq = 0;
61                 mod_info->param = NULL;
62         }
63
64         op_found = FALSE;
65         opterr = 0;
66
67         do {
68                 switch (c = getopt(argc, argv, getopt_string)) {
69                 case 'a':
70                         register_op(&op_found,
71                                     NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
72                         break;
73                 case 'b':
74                         register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP,
75                                     optarg);
76                         break;
77                 case 'B':
78                         register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP,
79                                     optarg);
80                         break;
81                 case 'c':
82                         register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
83                                     handle_optional_arg(argc, argv));
84                         break;
85                 case 'd':
86                         register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
87                         break;
88                 case 'e':
89                         register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES,
90                                     optarg);
91                         break;
92                 case 'h':
93                         register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
94                         break;
95                 case 'i':
96                         register_op(&op_found,
97                                     NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
98                         break;
99                 case 'l':
100                         register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
101                                     handle_optional_arg(argc, argv));
102                         break;
103                 case 'n':
104                         register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY,
105                                              NULL);
106                         break;
107                 case 'p':
108                         register_op(&op_found,
109                                     NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
110                         break;
111                 case 'r':
112                         register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM,
113                                     optarg);
114                         break;
115                 case 't':
116                         register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
117                                              NULL);
118                         break;
119                 case 'v':
120                         register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
121                         break;
122                 case 'w':
123                         register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM,
124                                     optarg);
125                         break;
126                 case 'x':
127                         register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP,
128                                     NULL);
129                         break;
130                 case 'X':
131                         register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE,
132                                     optarg);
133                         break;
134                 case 'y':
135                         register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
136                                              optarg);
137                         break;
138                 case 'Y':
139                         register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
140                         break;
141                 case -1:        /* no more command line args */
142                         break;
143                 case '?':       /* unknown option found */
144                 case 1: /* nonoption command line arg found */
145                 default:
146                         usage(stderr);
147                         break;
148                 }
149         } while (c != -1);
150
151         if (!op_found)
152                 usage(stderr);
153
154         resolve_op_modifiers();
155         sanity_check_args();
156 }
157
158 /****************************************************************************
159  * handle_optional_arg
160  *
161  * Handle a command line option with an optional argument.
162  ****************************************************************************/
163 static char *handle_optional_arg(int argc, char *argv[])
164 {
165         char *arg;
166
167         if (optarg != NULL) {
168                 /* optional arg is present and arg was specified as
169                  * "-zarg" (with no whitespace between "z" and "arg"),
170                  * where -z is the option and "arg" is the value of the
171                  * optional arg
172                  */
173                 return optarg;
174         }
175
176         if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
177                 return NULL;
178
179         arg = argv[optind];     /* optional arg is present */
180
181         /* This call to getopt yields the optional arg we just found,
182          * which we want to skip.
183          */
184         getopt(argc, argv, getopt_string);
185
186         return arg;
187 }
188
189 /****************************************************************************
190  * register_op
191  *
192  * Store the user's selection of which operation this program should perform.
193  ****************************************************************************/
194 static void register_op(int *op_found, nvramtool_op_t op, char op_param[])
195 {
196         if (*op_found && (op != nvramtool_op.op))
197                 usage(stderr);
198
199         *op_found = TRUE;
200         nvramtool_op.op = op;
201         nvramtool_op.param = op_param;
202 }
203
204 /****************************************************************************
205  * register_op_modifier
206  *
207  * Store information regarding an optional argument specified in addition to
208  * the user's selection of which operation this program should perform.
209  ****************************************************************************/
210 static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[])
211 {
212         static int found_seq = 0;
213         nvramtool_op_modifier_info_t *mod_info;
214
215         mod_info = &nvramtool_op_modifiers[mod];
216         mod_info->found = TRUE;
217         mod_info->found_seq = ++found_seq;
218         mod_info->param = mod_param;
219 }
220
221 /****************************************************************************
222  * resolve_op_modifiers
223  *
224  * If the user specifies multiple arguments that conflict with each other,
225  * the last specified argument overrides previous conflicting arguments.
226  ****************************************************************************/
227 static void resolve_op_modifiers(void)
228 {
229         if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
230             nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
231                 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
232                     nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
233                         nvramtool_op_modifiers
234                             [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
235                 else
236                         nvramtool_op_modifiers
237                             [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
238         }
239 }
240
241 /****************************************************************************
242  * sanity_check_args
243  *
244  * Perform sanity checking on command line arguments.
245  ****************************************************************************/
246 static void sanity_check_args(void)
247 {
248         if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
249             (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
250                 usage(stderr);
251 }