1 /*****************************************************************************\
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>.
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.
14 * Please also read the file DISCLAIMER which is included in this software
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.
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.
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 \*****************************************************************************/
34 nvramtool_op_info_t nvramtool_op;
36 nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
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);
44 static const char getopt_string[] = "-ab:B:c::C:dD:e:hil::np:r:tvw:xX:y:Y";
46 /****************************************************************************
47 * parse_nvramtool_args
49 * Parse command line arguments.
50 ****************************************************************************/
51 void parse_nvramtool_args(int argc, char *argv[])
53 nvramtool_op_modifier_info_t *mod_info;
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;
68 switch (c = getopt(argc, argv, getopt_string)) {
70 register_op(&op_found,
71 NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
74 register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP,
78 register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP,
82 register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
83 handle_optional_arg(argc, argv));
86 register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE,
90 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
93 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE,
97 register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES,
101 register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
104 register_op(&op_found,
105 NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
108 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
109 handle_optional_arg(argc, argv));
112 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY,
116 register_op(&op_found,
117 NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
120 register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM,
124 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
128 register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
131 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM,
135 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP,
139 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE,
143 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
147 register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
149 case -1: /* no more command line args */
151 case '?': /* unknown option found */
152 case 1: /* nonoption command line arg found */
162 resolve_op_modifiers();
166 /****************************************************************************
167 * handle_optional_arg
169 * Handle a command line option with an optional argument.
170 ****************************************************************************/
171 static char *handle_optional_arg(int argc, char *argv[])
175 if (optarg != NULL) {
176 /* optional arg is present and arg was specified as
177 * "-zarg" (with no whitespace between "z" and "arg"),
178 * where -z is the option and "arg" is the value of the
184 if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
187 arg = argv[optind]; /* optional arg is present */
189 /* This call to getopt yields the optional arg we just found,
190 * which we want to skip.
192 getopt(argc, argv, getopt_string);
197 /****************************************************************************
200 * Store the user's selection of which operation this program should perform.
201 ****************************************************************************/
202 static void register_op(int *op_found, nvramtool_op_t op, char op_param[])
204 if (*op_found && (op != nvramtool_op.op))
208 nvramtool_op.op = op;
209 nvramtool_op.param = op_param;
212 /****************************************************************************
213 * register_op_modifier
215 * Store information regarding an optional argument specified in addition to
216 * the user's selection of which operation this program should perform.
217 ****************************************************************************/
218 static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[])
220 static int found_seq = 0;
221 nvramtool_op_modifier_info_t *mod_info;
223 mod_info = &nvramtool_op_modifiers[mod];
224 mod_info->found = TRUE;
225 mod_info->found_seq = ++found_seq;
226 mod_info->param = mod_param;
229 /****************************************************************************
230 * resolve_op_modifiers
232 * If the user specifies multiple arguments that conflict with each other,
233 * the last specified argument overrides previous conflicting arguments.
234 ****************************************************************************/
235 static void resolve_op_modifiers(void)
237 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
238 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
239 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
240 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
241 nvramtool_op_modifiers
242 [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
244 nvramtool_op_modifiers
245 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
249 /****************************************************************************
252 * Perform sanity checking on command line arguments.
253 ****************************************************************************/
254 static void sanity_check_args(void)
256 if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
257 (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))