2007-07-12 18:35:42 +02:00
|
|
|
/*****************************************************************************\
|
|
|
|
* opts.c
|
|
|
|
*****************************************************************************
|
|
|
|
* Please also read the file DISCLAIMER which is included in this software
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License (as published by the
|
|
|
|
* Free Software Foundation) version 2, dated June 1991.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
|
|
|
|
* conditions of the GNU General Public License for more details.
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "opts.h"
|
|
|
|
|
2008-03-01 20:06:32 +01:00
|
|
|
nvramtool_op_info_t nvramtool_op;
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2008-03-01 20:06:32 +01:00
|
|
|
nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS];
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
static char *handle_optional_arg(int argc, char *argv[]);
|
|
|
|
static void register_op(int *op_found, nvramtool_op_t op, char op_param[]);
|
|
|
|
static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]);
|
|
|
|
static void resolve_op_modifiers(void);
|
|
|
|
static void sanity_check_args(void);
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2012-04-14 11:55:13 +02:00
|
|
|
static const char getopt_string[] = "-ab:B:c::C:dD:e:hH:iL:l::np:r:tvw:xX:y:Y";
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2008-03-01 20:06:32 +01:00
|
|
|
* parse_nvramtool_args
|
2007-07-12 18:35:42 +02:00
|
|
|
*
|
|
|
|
* Parse command line arguments.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
void parse_nvramtool_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
nvramtool_op_modifier_info_t *mod_info;
|
2015-04-05 23:13:53 +02:00
|
|
|
int i, op_found, c;
|
2010-01-13 22:00:23 +01:00
|
|
|
|
|
|
|
for (i = 0, mod_info = nvramtool_op_modifiers;
|
|
|
|
i < NVRAMTOOL_NUM_OP_MODIFIERS; i++, mod_info++) {
|
|
|
|
mod_info->found = FALSE;
|
|
|
|
mod_info->found_seq = 0;
|
|
|
|
mod_info->param = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
op_found = FALSE;
|
|
|
|
opterr = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch (c = getopt(argc, argv, getopt_string)) {
|
|
|
|
case 'a':
|
|
|
|
register_op(&op_found,
|
|
|
|
NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
|
|
|
|
handle_optional_arg(argc, argv));
|
|
|
|
break;
|
2011-01-21 08:24:08 +01:00
|
|
|
case 'C':
|
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE,
|
|
|
|
optarg);
|
|
|
|
break;
|
2010-01-13 22:00:23 +01:00
|
|
|
case 'd':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
|
|
|
|
break;
|
2011-01-21 08:29:40 +01:00
|
|
|
case 'D':
|
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE,
|
|
|
|
optarg);
|
|
|
|
break;
|
2010-01-13 22:00:23 +01:00
|
|
|
case 'e':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
|
|
|
|
break;
|
2012-04-14 11:55:13 +02:00
|
|
|
case 'H':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_WRITE_HEADER_FILE, optarg);
|
|
|
|
break;
|
2010-01-13 22:00:23 +01:00
|
|
|
case 'i':
|
|
|
|
register_op(&op_found,
|
|
|
|
NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
|
|
|
|
handle_optional_arg(argc, argv));
|
|
|
|
break;
|
2012-04-14 11:55:13 +02:00
|
|
|
case 'L':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_WRITE_BINARY_FILE,
|
|
|
|
optarg);
|
|
|
|
break;
|
2010-01-13 22:00:23 +01:00
|
|
|
case 'n':
|
|
|
|
register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
register_op(&op_found,
|
|
|
|
NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
|
|
|
|
break;
|
|
|
|
case -1: /* no more command line args */
|
|
|
|
break;
|
|
|
|
case '?': /* unknown option found */
|
|
|
|
case 1: /* nonoption command line arg found */
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (c != -1);
|
|
|
|
|
|
|
|
if (!op_found)
|
|
|
|
usage(stderr);
|
|
|
|
|
|
|
|
resolve_op_modifiers();
|
|
|
|
sanity_check_args();
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* handle_optional_arg
|
|
|
|
*
|
|
|
|
* Handle a command line option with an optional argument.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
static char *handle_optional_arg(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *arg;
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
if (optarg != NULL) {
|
|
|
|
/* optional arg is present and arg was specified as
|
|
|
|
* "-zarg" (with no whitespace between "z" and "arg"),
|
|
|
|
* where -z is the option and "arg" is the value of the
|
|
|
|
* optional arg
|
|
|
|
*/
|
|
|
|
return optarg;
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
|
|
|
|
return NULL;
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
arg = argv[optind]; /* optional arg is present */
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
/* This call to getopt yields the optional arg we just found,
|
|
|
|
* which we want to skip.
|
|
|
|
*/
|
|
|
|
getopt(argc, argv, getopt_string);
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
return arg;
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* register_op
|
|
|
|
*
|
|
|
|
* Store the user's selection of which operation this program should perform.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
static void register_op(int *op_found, nvramtool_op_t op, char op_param[])
|
|
|
|
{
|
|
|
|
if (*op_found && (op != nvramtool_op.op))
|
|
|
|
usage(stderr);
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
*op_found = TRUE;
|
|
|
|
nvramtool_op.op = op;
|
|
|
|
nvramtool_op.param = op_param;
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* register_op_modifier
|
|
|
|
*
|
|
|
|
* Store information regarding an optional argument specified in addition to
|
|
|
|
* the user's selection of which operation this program should perform.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[])
|
|
|
|
{
|
|
|
|
static int found_seq = 0;
|
|
|
|
nvramtool_op_modifier_info_t *mod_info;
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2010-01-13 22:00:23 +01:00
|
|
|
mod_info = &nvramtool_op_modifiers[mod];
|
|
|
|
mod_info->found = TRUE;
|
|
|
|
mod_info->found_seq = ++found_seq;
|
|
|
|
mod_info->param = mod_param;
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* resolve_op_modifiers
|
|
|
|
*
|
|
|
|
* If the user specifies multiple arguments that conflict with each other,
|
|
|
|
* the last specified argument overrides previous conflicting arguments.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
static void resolve_op_modifiers(void)
|
|
|
|
{
|
|
|
|
if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found &&
|
|
|
|
nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
|
|
|
|
if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq >
|
|
|
|
nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq)
|
|
|
|
nvramtool_op_modifiers
|
|
|
|
[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE;
|
|
|
|
else
|
|
|
|
nvramtool_op_modifiers
|
|
|
|
[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-07-12 18:35:42 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* sanity_check_args
|
|
|
|
*
|
|
|
|
* Perform sanity checking on command line arguments.
|
|
|
|
****************************************************************************/
|
2010-01-13 22:00:23 +01:00
|
|
|
static void sanity_check_args(void)
|
|
|
|
{
|
|
|
|
if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
|
|
|
|
(nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
|
|
|
|
usage(stderr);
|
|
|
|
}
|