Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * opts.c |
| 3 | * $Id: opts.c,v 1.2 2005/12/16 22:45:49 dsp_llnl Exp $ |
| 4 | ***************************************************************************** |
| 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 | * |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame^] | 11 | * This file is part of lxbios, a utility for reading/writing coreboot |
| 12 | * parameters and displaying information from the coreboot table. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * For details, see <http://www.llnl.gov/linux/lxbios/>. |
| 14 | * |
| 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 | |
| 35 | lxbios_op_info_t lxbios_op; |
| 36 | |
| 37 | lxbios_op_modifier_info_t lxbios_op_modifiers[LXBIOS_NUM_OP_MODIFIERS]; |
| 38 | |
| 39 | static char * handle_optional_arg (int argc, char *argv[]); |
| 40 | static void register_op (int *op_found, lxbios_op_t op, char op_param[]); |
| 41 | static void register_op_modifier (lxbios_op_modifier_t mod, char mod_param[]); |
| 42 | static void resolve_op_modifiers (void); |
| 43 | static void sanity_check_args (void); |
| 44 | |
| 45 | static const char getopt_string[] = "-ab:B:c::de:hil::np:r:tvw:xX:y:Y"; |
| 46 | |
| 47 | /**************************************************************************** |
| 48 | * parse_lxbios_args |
| 49 | * |
| 50 | * Parse command line arguments. |
| 51 | ****************************************************************************/ |
| 52 | void parse_lxbios_args (int argc, char *argv[]) |
| 53 | { lxbios_op_modifier_info_t *mod_info; |
| 54 | int i, op_found; |
| 55 | char c; |
| 56 | |
| 57 | for (i = 0, mod_info = lxbios_op_modifiers; |
| 58 | i < LXBIOS_NUM_OP_MODIFIERS; |
| 59 | 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': |
| 71 | register_op(&op_found, LXBIOS_OP_CMOS_SHOW_ALL_PARAMS, NULL); |
| 72 | break; |
| 73 | case 'b': |
| 74 | register_op(&op_found, LXBIOS_OP_WRITE_CMOS_DUMP, optarg); |
| 75 | break; |
| 76 | case 'B': |
| 77 | register_op(&op_found, LXBIOS_OP_READ_CMOS_DUMP, optarg); |
| 78 | break; |
| 79 | case 'c': |
| 80 | register_op(&op_found, LXBIOS_OP_CMOS_CHECKSUM, |
| 81 | handle_optional_arg(argc, argv)); |
| 82 | break; |
| 83 | case 'd': |
| 84 | register_op(&op_found, LXBIOS_OP_LBTABLE_DUMP, NULL); |
| 85 | break; |
| 86 | case 'e': |
| 87 | register_op(&op_found, LXBIOS_OP_SHOW_PARAM_VALUES, optarg); |
| 88 | break; |
| 89 | case 'h': |
| 90 | register_op(&op_found, LXBIOS_OP_SHOW_USAGE, NULL); |
| 91 | break; |
| 92 | case 'i': |
| 93 | register_op(&op_found, LXBIOS_OP_CMOS_SET_PARAMS_STDIN, NULL); |
| 94 | break; |
| 95 | case 'l': |
| 96 | register_op(&op_found, LXBIOS_OP_LBTABLE_SHOW_INFO, |
| 97 | handle_optional_arg(argc, argv)); |
| 98 | break; |
| 99 | case 'n': |
| 100 | register_op_modifier(LXBIOS_MOD_SHOW_VALUE_ONLY, NULL); |
| 101 | break; |
| 102 | case 'p': |
| 103 | register_op(&op_found, LXBIOS_OP_CMOS_SET_PARAMS_FILE, optarg); |
| 104 | break; |
| 105 | case 'r': |
| 106 | register_op(&op_found, LXBIOS_OP_CMOS_SHOW_ONE_PARAM, optarg); |
| 107 | break; |
| 108 | case 't': |
| 109 | register_op_modifier(LXBIOS_MOD_USE_CMOS_OPT_TABLE, NULL); |
| 110 | break; |
| 111 | case 'v': |
| 112 | register_op(&op_found, LXBIOS_OP_SHOW_VERSION, NULL); |
| 113 | break; |
| 114 | case 'w': |
| 115 | register_op(&op_found, LXBIOS_OP_CMOS_SET_ONE_PARAM, optarg); |
| 116 | break; |
| 117 | case 'x': |
| 118 | register_op(&op_found, LXBIOS_OP_SHOW_CMOS_HEX_DUMP, NULL); |
| 119 | break; |
| 120 | case 'X': |
| 121 | register_op(&op_found, LXBIOS_OP_SHOW_CMOS_DUMPFILE, optarg); |
| 122 | break; |
| 123 | case 'y': |
| 124 | register_op_modifier(LXBIOS_MOD_USE_CMOS_LAYOUT_FILE, optarg); |
| 125 | break; |
| 126 | case 'Y': |
| 127 | register_op(&op_found, LXBIOS_OP_SHOW_LAYOUT, NULL); |
| 128 | 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 | ****************************************************************************/ |
| 152 | static 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 | ****************************************************************************/ |
| 181 | static void register_op (int *op_found, lxbios_op_t op, char op_param[]) |
| 182 | { if (*op_found && (op != lxbios_op.op)) |
| 183 | usage(stderr); |
| 184 | |
| 185 | *op_found = TRUE; |
| 186 | lxbios_op.op = op; |
| 187 | lxbios_op.param = op_param; |
| 188 | } |
| 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 | ****************************************************************************/ |
| 196 | static void register_op_modifier (lxbios_op_modifier_t mod, char mod_param[]) |
| 197 | { static int found_seq = 0; |
| 198 | lxbios_op_modifier_info_t *mod_info; |
| 199 | |
| 200 | mod_info = &lxbios_op_modifiers[mod]; |
| 201 | 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 | ****************************************************************************/ |
| 212 | static void resolve_op_modifiers (void) |
| 213 | { if (lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_LAYOUT_FILE].found && |
| 214 | lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_OPT_TABLE].found) |
| 215 | { if (lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_LAYOUT_FILE].found_seq > |
| 216 | lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_OPT_TABLE].found_seq) |
| 217 | lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_OPT_TABLE].found = FALSE; |
| 218 | else |
| 219 | lxbios_op_modifiers[LXBIOS_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /**************************************************************************** |
| 224 | * sanity_check_args |
| 225 | * |
| 226 | * Perform sanity checking on command line arguments. |
| 227 | ****************************************************************************/ |
| 228 | static void sanity_check_args (void) |
| 229 | { if ((lxbios_op_modifiers[LXBIOS_MOD_SHOW_VALUE_ONLY].found) && |
| 230 | (lxbios_op.op != LXBIOS_OP_CMOS_SHOW_ONE_PARAM)) |
| 231 | usage(stderr); |
| 232 | } |