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.
25 \*****************************************************************************/
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
;
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
;
63 switch (c
= getopt(argc
, argv
, getopt_string
)) {
65 register_op(&op_found
,
66 NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS
, NULL
);
69 register_op(&op_found
, NVRAMTOOL_OP_WRITE_CMOS_DUMP
,
73 register_op(&op_found
, NVRAMTOOL_OP_READ_CMOS_DUMP
,
77 register_op(&op_found
, NVRAMTOOL_OP_CMOS_CHECKSUM
,
78 handle_optional_arg(argc
, argv
));
81 register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE
,
85 register_op(&op_found
, NVRAMTOOL_OP_LBTABLE_DUMP
, NULL
);
88 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE
,
92 register_op(&op_found
, NVRAMTOOL_OP_SHOW_PARAM_VALUES
,
96 register_op(&op_found
, NVRAMTOOL_OP_SHOW_USAGE
, NULL
);
99 register_op(&op_found
, NVRAMTOOL_OP_WRITE_HEADER_FILE
, optarg
);
102 register_op(&op_found
,
103 NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN
, NULL
);
106 register_op(&op_found
, NVRAMTOOL_OP_LBTABLE_SHOW_INFO
,
107 handle_optional_arg(argc
, argv
));
110 register_op(&op_found
, NVRAMTOOL_OP_WRITE_BINARY_FILE
,
114 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY
,
118 register_op(&op_found
,
119 NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE
, optarg
);
122 register_op(&op_found
, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM
,
126 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE
,
130 register_op(&op_found
, NVRAMTOOL_OP_SHOW_VERSION
, NULL
);
133 register_op(&op_found
, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM
,
137 register_op(&op_found
, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP
,
141 register_op(&op_found
, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE
,
145 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
,
149 register_op(&op_found
, NVRAMTOOL_OP_SHOW_LAYOUT
, NULL
);
151 case -1: /* no more command line args */
153 case '?': /* unknown option found */
154 case 1: /* nonoption command line arg found */
164 resolve_op_modifiers();
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
[])
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
186 if ((argv
[optind
] == NULL
) || (argv
[optind
][0] == '-'))
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
);
199 /****************************************************************************
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
))
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
;
246 nvramtool_op_modifiers
247 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
].found
= FALSE
;
251 /****************************************************************************
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
))