2007-07-12 18:35:42 +02:00
|
|
|
/*****************************************************************************\
|
|
|
|
* opts.c
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (C) 2002-2005 The Regents of the University of California.
|
|
|
|
* Produced at the Lawrence Livermore National Laboratory.
|
|
|
|
* Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
|
|
|
|
* UCRL-CODE-2003-012
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2008-03-01 20:06:32 +01:00
|
|
|
* This file is part of nvramtool, a utility for reading/writing coreboot
|
2008-01-18 16:33:49 +01:00
|
|
|
* parameters and displaying information from the coreboot table.
|
2008-03-01 20:06:32 +01:00
|
|
|
* For details, see http://coreboot.org/nvramtool.
|
2007-07-12 18:35:42 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2009-09-23 23:53:25 +02:00
|
|
|
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-07-12 18:35:42 +02:00
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
static char * handle_optional_arg (int argc, char *argv[]);
|
2008-03-01 20:06:32 +01:00
|
|
|
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[]);
|
2007-07-12 18:35:42 +02:00
|
|
|
static void resolve_op_modifiers (void);
|
|
|
|
static void sanity_check_args (void);
|
|
|
|
|
|
|
|
static const char getopt_string[] = "-ab:B:c::de:hil::np:r:tvw:xX:y:Y";
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-03-01 20:06:32 +01:00
|
|
|
* parse_nvramtool_args
|
2007-07-12 18:35:42 +02:00
|
|
|
*
|
|
|
|
* Parse command line arguments.
|
|
|
|
****************************************************************************/
|
2008-03-01 20:06:32 +01:00
|
|
|
void parse_nvramtool_args (int argc, char *argv[])
|
|
|
|
{ nvramtool_op_modifier_info_t *mod_info;
|
2007-07-12 18:35:42 +02:00
|
|
|
int i, op_found;
|
|
|
|
char c;
|
|
|
|
|
2008-03-01 20:06:32 +01:00
|
|
|
for (i = 0, mod_info = nvramtool_op_modifiers;
|
|
|
|
i < NVRAMTOOL_NUM_OP_MODIFIERS;
|
2007-07-12 18:35:42 +02:00
|
|
|
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':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'b':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'B':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'c':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
|
2007-07-12 18:35:42 +02:00
|
|
|
handle_optional_arg(argc, argv));
|
|
|
|
break;
|
|
|
|
case 'd':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'e':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'h':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'i':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'l':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO,
|
2007-07-12 18:35:42 +02:00
|
|
|
handle_optional_arg(argc, argv));
|
|
|
|
break;
|
|
|
|
case 'n':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'p':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'r':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'v':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'w':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'x':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'X':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'y':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE, optarg);
|
2007-07-12 18:35:42 +02:00
|
|
|
break;
|
|
|
|
case 'Y':
|
2008-03-01 20:06:32 +01:00
|
|
|
register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL);
|
2007-07-12 18:35:42 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* handle_optional_arg
|
|
|
|
*
|
|
|
|
* Handle a command line option with an optional argument.
|
|
|
|
****************************************************************************/
|
|
|
|
static char * handle_optional_arg (int argc, char *argv[])
|
|
|
|
{ char *arg;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((argv[optind] == NULL) || (argv[optind][0] == '-'))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
arg = argv[optind]; /* optional arg is present */
|
|
|
|
|
|
|
|
/* This call to getopt yields the optional arg we just found, which we want
|
|
|
|
* to skip.
|
|
|
|
*/
|
|
|
|
getopt(argc, argv, getopt_string);
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* register_op
|
|
|
|
*
|
|
|
|
* Store the user's selection of which operation this program should perform.
|
|
|
|
****************************************************************************/
|
2008-03-01 20:06:32 +01:00
|
|
|
static void register_op (int *op_found, nvramtool_op_t op, char op_param[])
|
|
|
|
{ if (*op_found && (op != nvramtool_op.op))
|
2007-07-12 18:35:42 +02:00
|
|
|
usage(stderr);
|
|
|
|
|
|
|
|
*op_found = TRUE;
|
2008-03-01 20:06:32 +01:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2008-03-01 20:06:32 +01:00
|
|
|
static void register_op_modifier (nvramtool_op_modifier_t mod, char mod_param[])
|
2007-07-12 18:35:42 +02:00
|
|
|
{ static int found_seq = 0;
|
2008-03-01 20:06:32 +01:00
|
|
|
nvramtool_op_modifier_info_t *mod_info;
|
2007-07-12 18:35:42 +02:00
|
|
|
|
2008-03-01 20:06:32 +01:00
|
|
|
mod_info = &nvramtool_op_modifiers[mod];
|
2007-07-12 18:35:42 +02:00
|
|
|
mod_info->found = TRUE;
|
|
|
|
mod_info->found_seq = ++found_seq;
|
|
|
|
mod_info->param = mod_param;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* resolve_op_modifiers
|
|
|
|
*
|
|
|
|
* If the user specifies multiple arguments that conflict with each other,
|
|
|
|
* the last specified argument overrides previous conflicting arguments.
|
|
|
|
****************************************************************************/
|
|
|
|
static void resolve_op_modifiers (void)
|
2008-03-01 20:06:32 +01:00
|
|
|
{ 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;
|
2007-07-12 18:35:42 +02:00
|
|
|
else
|
2008-03-01 20:06:32 +01:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
static void sanity_check_args (void)
|
2008-03-01 20:06:32 +01:00
|
|
|
{ if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) &&
|
|
|
|
(nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM))
|
2007-07-12 18:35:42 +02:00
|
|
|
usage(stderr);
|
|
|
|
}
|