blob: 34311531799ef1377b9b0866f3bf7ccd27e65e2a [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * opts.c
Uwe Hermann1a6177b2008-01-25 15:08:37 +00003 * $Id$
Stefan Reinauer6540ae52007-07-12 16:35:42 +00004 *****************************************************************************
5 * Copyright (C) 2002-2005 The Regents of the University of California.
6 * Produced at the Lawrence Livermore National Laboratory.
7 * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
8 * UCRL-CODE-2003-012
9 * All rights reserved.
10 *
Uwe Hermann6e565942008-03-01 19:06:32 +000011 * This file is part of nvramtool, a utility for reading/writing coreboot
Stefan Reinauerf527e702008-01-18 15:33:49 +000012 * parameters and displaying information from the coreboot table.
Uwe Hermann6e565942008-03-01 19:06:32 +000013 * For details, see http://coreboot.org/nvramtool.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000014 *
15 * Please also read the file DISCLAIMER which is included in this software
16 * distribution.
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License (as published by the
20 * Free Software Foundation) version 2, dated June 1991.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
25 * conditions of the GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30\*****************************************************************************/
31
32#include "common.h"
33#include "opts.h"
34
Uwe Hermann6e565942008-03-01 19:06:32 +000035nvramtool_op_info_t nvramtool_op;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000036
Uwe Hermann6e565942008-03-01 19:06:32 +000037nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
Stefan Reinauer6540ae52007-07-12 16:35:42 +000038
39static char * handle_optional_arg (int argc, char *argv[]);
Uwe Hermann6e565942008-03-01 19:06:32 +000040static void register_op (int *op_found, nvramtool_op_t op, char op_param[]);
41static void register_op_modifier (nvramtool_op_modifier_t mod, char mod_param[]);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000042static void resolve_op_modifiers (void);
43static void sanity_check_args (void);
44
45static const char getopt_string[] = "-ab:B:c::de:hil::np:r:tvw:xX:y:Y";
46
47/****************************************************************************
Uwe Hermann6e565942008-03-01 19:06:32 +000048 * parse_nvramtool_args
Stefan Reinauer6540ae52007-07-12 16:35:42 +000049 *
50 * Parse command line arguments.
51 ****************************************************************************/
Uwe Hermann6e565942008-03-01 19:06:32 +000052void parse_nvramtool_args (int argc, char *argv[])
53 { nvramtool_op_modifier_info_t *mod_info;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000054 int i, op_found;
55 char c;
56
Uwe Hermann6e565942008-03-01 19:06:32 +000057 for (i = 0, mod_info = nvramtool_op_modifiers;
58 i < NVRAMTOOL_NUM_OP_MODIFIERS;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000059 i++, mod_info++)
60 { mod_info->found = FALSE;
61 mod_info->found_seq = 0;
62 mod_info->param = NULL;
63 }
64
65 op_found = FALSE;
66 opterr = 0;
67
68 do
69 { switch (c = getopt(argc, argv, getopt_string))
70 { case 'a':
Uwe Hermann6e565942008-03-01 19:06:32 +000071 register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000072 break;
73 case 'b':
Uwe Hermann6e565942008-03-01 19:06:32 +000074 register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000075 break;
76 case 'B':
Uwe Hermann6e565942008-03-01 19:06:32 +000077 register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000078 break;
79 case 'c':
Uwe Hermann6e565942008-03-01 19:06:32 +000080 register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
Stefan Reinauer6540ae52007-07-12 16:35:42 +000081 handle_optional_arg(argc, argv));
82 break;
83 case 'd':
Uwe Hermann6e565942008-03-01 19:06:32 +000084 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000085 break;
86 case 'e':
Uwe Hermann6e565942008-03-01 19:06:32 +000087 register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000088 break;
89 case 'h':
Uwe Hermann6e565942008-03-01 19:06:32 +000090 register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000091 break;
92 case 'i':
Uwe Hermann6e565942008-03-01 19:06:32 +000093 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000094 break;
95 case 'l':
Uwe Hermann6e565942008-03-01 19:06:32 +000096 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
Stefan Reinauer6540ae52007-07-12 16:35:42 +000097 handle_optional_arg(argc, argv));
98 break;
99 case 'n':
Uwe Hermann6e565942008-03-01 19:06:32 +0000100 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000101 break;
102 case 'p':
Uwe Hermann6e565942008-03-01 19:06:32 +0000103 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000104 break;
105 case 'r':
Uwe Hermann6e565942008-03-01 19:06:32 +0000106 register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000107 break;
108 case 't':
Uwe Hermann6e565942008-03-01 19:06:32 +0000109 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000110 break;
111 case 'v':
Uwe Hermann6e565942008-03-01 19:06:32 +0000112 register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000113 break;
114 case 'w':
Uwe Hermann6e565942008-03-01 19:06:32 +0000115 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000116 break;
117 case 'x':
Uwe Hermann6e565942008-03-01 19:06:32 +0000118 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000119 break;
120 case 'X':
Uwe Hermann6e565942008-03-01 19:06:32 +0000121 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000122 break;
123 case 'y':
Uwe Hermann6e565942008-03-01 19:06:32 +0000124 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE, optarg);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000125 break;
126 case 'Y':
Uwe Hermann6e565942008-03-01 19:06:32 +0000127 register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000128 break;
129 case -1: /* no more command line args */
130 break;
131 case '?': /* unknown option found */
132 case 1: /* nonoption command line arg found */
133 default:
134 usage(stderr);
135 break;
136 }
137 }
138 while (c != -1);
139
140 if (!op_found)
141 usage(stderr);
142
143 resolve_op_modifiers();
144 sanity_check_args();
145 }
146
147/****************************************************************************
148 * handle_optional_arg
149 *
150 * Handle a command line option with an optional argument.
151 ****************************************************************************/
152static char * handle_optional_arg (int argc, char *argv[])
153 { char *arg;
154
155 if (optarg != NULL)
156 { /* optional arg is present and arg was specified as "-zarg" (with no
157 * whitespace between "z" and "arg"), where -z is the option and "arg"
158 * is the value of the optional arg
159 */
160 return optarg;
161 }
162
163 if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
164 return NULL;
165
166 arg = argv[optind]; /* optional arg is present */
167
168 /* This call to getopt yields the optional arg we just found, which we want
169 * to skip.
170 */
171 getopt(argc, argv, getopt_string);
172
173 return arg;
174 }
175
176/****************************************************************************
177 * register_op
178 *
179 * Store the user's selection of which operation this program should perform.
180 ****************************************************************************/
Uwe Hermann6e565942008-03-01 19:06:32 +0000181static void register_op (int *op_found, nvramtool_op_t op, char op_param[])
182 { if (*op_found && (op != nvramtool_op.op))
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000183 usage(stderr);
184
185 *op_found = TRUE;
Uwe Hermann6e565942008-03-01 19:06:32 +0000186 nvramtool_op.op = op;
187 nvramtool_op.param = op_param;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000188 }
189
190/****************************************************************************
191 * register_op_modifier
192 *
193 * Store information regarding an optional argument specified in addition to
194 * the user's selection of which operation this program should perform.
195 ****************************************************************************/
Uwe Hermann6e565942008-03-01 19:06:32 +0000196static void register_op_modifier (nvramtool_op_modifier_t mod, char mod_param[])
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000197 { static int found_seq = 0;
Uwe Hermann6e565942008-03-01 19:06:32 +0000198 nvramtool_op_modifier_info_t *mod_info;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000199
Uwe Hermann6e565942008-03-01 19:06:32 +0000200 mod_info = &nvramtool_op_modifiers[mod];
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000201 mod_info->found = TRUE;
202 mod_info->found_seq = ++found_seq;
203 mod_info->param = mod_param;
204 }
205
206/****************************************************************************
207 * resolve_op_modifiers
208 *
209 * If the user specifies multiple arguments that conflict with each other,
210 * the last specified argument overrides previous conflicting arguments.
211 ****************************************************************************/
212static void resolve_op_modifiers (void)
Uwe Hermann6e565942008-03-01 19:06:32 +0000213 { if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
214 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found)
215 { if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
216 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
217 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000218 else
Uwe Hermann6e565942008-03-01 19:06:32 +0000219 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000220 }
221 }
222
223/****************************************************************************
224 * sanity_check_args
225 *
226 * Perform sanity checking on command line arguments.
227 ****************************************************************************/
228static void sanity_check_args (void)
Uwe Hermann6e565942008-03-01 19:06:32 +0000229 { if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
230 (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000231 usage(stderr);
232 }