blob: 9aaeedfbfdad171448c5ff76b7b1e17c400d1d0a [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * opts.c
Stefan Reinauer6540ae52007-07-12 16:35:42 +00003 *****************************************************************************
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 *
Uwe Hermann6e565942008-03-01 19:06:32 +000010 * This file is part of nvramtool, a utility for reading/writing coreboot
Stefan Reinauerf527e702008-01-18 15:33:49 +000011 * parameters and displaying information from the coreboot table.
Uwe Hermann6e565942008-03-01 19:06:32 +000012 * For details, see http://coreboot.org/nvramtool.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000013 *
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.,
Stefan Reinauerac7a2d22009-09-23 21:53:25 +000028 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000029\*****************************************************************************/
30
31#include "common.h"
32#include "opts.h"
33
Uwe Hermann6e565942008-03-01 19:06:32 +000034nvramtool_op_info_t nvramtool_op;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000035
Uwe Hermann6e565942008-03-01 19:06:32 +000036nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
Stefan Reinauer6540ae52007-07-12 16:35:42 +000037
Stefan Reinauer90b96b62010-01-13 21:00:23 +000038static char *handle_optional_arg(int argc, char *argv[]);
39static void register_op(int *op_found, nvramtool_op_t op, char op_param[]);
40static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]);
41static void resolve_op_modifiers(void);
42static void sanity_check_args(void);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000043
Patrick Georgi269e9322011-01-21 07:24:08 +000044static const char getopt_string[] = "-ab:B:c::C:de:hil::np:r:tvw:xX:y:Y";
Stefan Reinauer6540ae52007-07-12 16:35:42 +000045
46/****************************************************************************
Uwe Hermann6e565942008-03-01 19:06:32 +000047 * parse_nvramtool_args
Stefan Reinauer6540ae52007-07-12 16:35:42 +000048 *
49 * Parse command line arguments.
50 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000051void parse_nvramtool_args(int argc, char *argv[])
52{
53 nvramtool_op_modifier_info_t *mod_info;
54 int i, op_found;
55 char c;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000056
Stefan Reinauer90b96b62010-01-13 21:00:23 +000057 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 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000063
Stefan Reinauer90b96b62010-01-13 21:00:23 +000064 op_found = FALSE;
65 opterr = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000066
Stefan Reinauer90b96b62010-01-13 21:00:23 +000067 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;
Patrick Georgi269e9322011-01-21 07:24:08 +000085 case 'C':
86 register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE,
87 optarg);
88 break;
Stefan Reinauer90b96b62010-01-13 21:00:23 +000089 case 'd':
90 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
91 break;
92 case 'e':
93 register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES,
94 optarg);
95 break;
96 case 'h':
97 register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
98 break;
99 case 'i':
100 register_op(&op_found,
101 NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
102 break;
103 case 'l':
104 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
105 handle_optional_arg(argc, argv));
106 break;
107 case 'n':
108 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY,
109 NULL);
110 break;
111 case 'p':
112 register_op(&op_found,
113 NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
114 break;
115 case 'r':
116 register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM,
117 optarg);
118 break;
119 case 't':
120 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
121 NULL);
122 break;
123 case 'v':
124 register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
125 break;
126 case 'w':
127 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM,
128 optarg);
129 break;
130 case 'x':
131 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP,
132 NULL);
133 break;
134 case 'X':
135 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE,
136 optarg);
137 break;
138 case 'y':
139 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
140 optarg);
141 break;
142 case 'Y':
143 register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
144 break;
145 case -1: /* no more command line args */
146 break;
147 case '?': /* unknown option found */
148 case 1: /* nonoption command line arg found */
149 default:
150 usage(stderr);
151 break;
152 }
153 } while (c != -1);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000154
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000155 if (!op_found)
156 usage(stderr);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000157
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000158 resolve_op_modifiers();
159 sanity_check_args();
160}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000161
162/****************************************************************************
163 * handle_optional_arg
164 *
165 * Handle a command line option with an optional argument.
166 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000167static char *handle_optional_arg(int argc, char *argv[])
168{
169 char *arg;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000170
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000171 if (optarg != NULL) {
172 /* optional arg is present and arg was specified as
173 * "-zarg" (with no whitespace between "z" and "arg"),
174 * where -z is the option and "arg" is the value of the
175 * optional arg
176 */
177 return optarg;
178 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000179
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000180 if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
181 return NULL;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000182
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000183 arg = argv[optind]; /* optional arg is present */
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000184
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000185 /* This call to getopt yields the optional arg we just found,
186 * which we want to skip.
187 */
188 getopt(argc, argv, getopt_string);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000189
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000190 return arg;
191}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000192
193/****************************************************************************
194 * register_op
195 *
196 * Store the user's selection of which operation this program should perform.
197 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000198static void register_op(int *op_found, nvramtool_op_t op, char op_param[])
199{
200 if (*op_found && (op != nvramtool_op.op))
201 usage(stderr);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000202
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000203 *op_found = TRUE;
204 nvramtool_op.op = op;
205 nvramtool_op.param = op_param;
206}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000207
208/****************************************************************************
209 * register_op_modifier
210 *
211 * Store information regarding an optional argument specified in addition to
212 * the user's selection of which operation this program should perform.
213 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000214static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[])
215{
216 static int found_seq = 0;
217 nvramtool_op_modifier_info_t *mod_info;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000218
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000219 mod_info = &nvramtool_op_modifiers[mod];
220 mod_info->found = TRUE;
221 mod_info->found_seq = ++found_seq;
222 mod_info->param = mod_param;
223}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000224
225/****************************************************************************
226 * resolve_op_modifiers
227 *
228 * If the user specifies multiple arguments that conflict with each other,
229 * the last specified argument overrides previous conflicting arguments.
230 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000231static void resolve_op_modifiers(void)
232{
233 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
234 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
235 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
236 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
237 nvramtool_op_modifiers
238 [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
239 else
240 nvramtool_op_modifiers
241 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
242 }
243}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000244
245/****************************************************************************
246 * sanity_check_args
247 *
248 * Perform sanity checking on command line arguments.
249 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000250static void sanity_check_args(void)
251{
252 if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
253 (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
254 usage(stderr);
255}