tree: drop last paragraph of GPL copyright header
[coreboot.git] / util / nvramtool / cli / opts.c
blob8b62a47c83a7a737e3f4287448c7b9d03487872d
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.
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
15 * distribution.
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.
25 \*****************************************************************************/
27 #include "common.h"
28 #include "opts.h"
30 nvramtool_op_info_t nvramtool_op;
32 nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
34 static char *handle_optional_arg(int argc, char *argv[]);
35 static void register_op(int *op_found, nvramtool_op_t op, char op_param[]);
36 static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]);
37 static void resolve_op_modifiers(void);
38 static void sanity_check_args(void);
40 static const char getopt_string[] = "-ab:B:c::C:dD:e:hH:iL:l::np:r:tvw:xX:y:Y";
42 /****************************************************************************
43 * parse_nvramtool_args
45 * Parse command line arguments.
46 ****************************************************************************/
47 void parse_nvramtool_args(int argc, char *argv[])
49 nvramtool_op_modifier_info_t *mod_info;
50 int i, op_found, c;
52 for (i = 0, mod_info = nvramtool_op_modifiers;
53 i < NVRAMTOOL_NUM_OP_MODIFIERS; i++, mod_info++) {
54 mod_info->found = FALSE;
55 mod_info->found_seq = 0;
56 mod_info->param = NULL;
59 op_found = FALSE;
60 opterr = 0;
62 do {
63 switch (c = getopt(argc, argv, getopt_string)) {
64 case 'a':
65 register_op(&op_found,
66 NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
67 break;
68 case 'b':
69 register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP,
70 optarg);
71 break;
72 case 'B':
73 register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP,
74 optarg);
75 break;
76 case 'c':
77 register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
78 handle_optional_arg(argc, argv));
79 break;
80 case 'C':
81 register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE,
82 optarg);
83 break;
84 case 'd':
85 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
86 break;
87 case 'D':
88 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE,
89 optarg);
90 break;
91 case 'e':
92 register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES,
93 optarg);
94 break;
95 case 'h':
96 register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
97 break;
98 case 'H':
99 register_op(&op_found, NVRAMTOOL_OP_WRITE_HEADER_FILE, optarg);
100 break;
101 case 'i':
102 register_op(&op_found,
103 NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
104 break;
105 case 'l':
106 register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
107 handle_optional_arg(argc, argv));
108 break;
109 case 'L':
110 register_op(&op_found, NVRAMTOOL_OP_WRITE_BINARY_FILE,
111 optarg);
112 break;
113 case 'n':
114 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY,
115 NULL);
116 break;
117 case 'p':
118 register_op(&op_found,
119 NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
120 break;
121 case 'r':
122 register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM,
123 optarg);
124 break;
125 case 't':
126 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
127 NULL);
128 break;
129 case 'v':
130 register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
131 break;
132 case 'w':
133 register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM,
134 optarg);
135 break;
136 case 'x':
137 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP,
138 NULL);
139 break;
140 case 'X':
141 register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE,
142 optarg);
143 break;
144 case 'y':
145 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
146 optarg);
147 break;
148 case 'Y':
149 register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
150 break;
151 case -1: /* no more command line args */
152 break;
153 case '?': /* unknown option found */
154 case 1: /* nonoption command line arg found */
155 default:
156 usage(stderr);
157 break;
159 } while (c != -1);
161 if (!op_found)
162 usage(stderr);
164 resolve_op_modifiers();
165 sanity_check_args();
168 /****************************************************************************
169 * handle_optional_arg
171 * Handle a command line option with an optional argument.
172 ****************************************************************************/
173 static char *handle_optional_arg(int argc, char *argv[])
175 char *arg;
177 if (optarg != NULL) {
178 /* optional arg is present and arg was specified as
179 * "-zarg" (with no whitespace between "z" and "arg"),
180 * where -z is the option and "arg" is the value of the
181 * optional arg
183 return optarg;
186 if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
187 return NULL;
189 arg = argv[optind]; /* optional arg is present */
191 /* This call to getopt yields the optional arg we just found,
192 * which we want to skip.
194 getopt(argc, argv, getopt_string);
196 return arg;
199 /****************************************************************************
200 * register_op
202 * Store the user's selection of which operation this program should perform.
203 ****************************************************************************/
204 static void register_op(int *op_found, nvramtool_op_t op, char op_param[])
206 if (*op_found && (op != nvramtool_op.op))
207 usage(stderr);
209 *op_found = TRUE;
210 nvramtool_op.op = op;
211 nvramtool_op.param = op_param;
214 /****************************************************************************
215 * register_op_modifier
217 * Store information regarding an optional argument specified in addition to
218 * the user's selection of which operation this program should perform.
219 ****************************************************************************/
220 static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[])
222 static int found_seq = 0;
223 nvramtool_op_modifier_info_t *mod_info;
225 mod_info = &nvramtool_op_modifiers[mod];
226 mod_info->found = TRUE;
227 mod_info->found_seq = ++found_seq;
228 mod_info->param = mod_param;
231 /****************************************************************************
232 * resolve_op_modifiers
234 * If the user specifies multiple arguments that conflict with each other,
235 * the last specified argument overrides previous conflicting arguments.
236 ****************************************************************************/
237 static void resolve_op_modifiers(void)
239 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
240 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
241 if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
242 nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
243 nvramtool_op_modifiers
244 [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
245 else
246 nvramtool_op_modifiers
247 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
251 /****************************************************************************
252 * sanity_check_args
254 * Perform sanity checking on command line arguments.
255 ****************************************************************************/
256 static void sanity_check_args(void)
258 if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
259 (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
260 usage(stderr);