Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * opts.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 12 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Please also read the file DISCLAIMER which is included in this software |
| 15 | * distribution. |
| 16 | * |
| 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. |
| 20 | * |
| 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 | * |
| 26 | * You should have received a copy of the GNU General Public License along |
| 27 | * with this program; if not, write to the Free Software Foundation, Inc., |
Stefan Reinauer | ac7a2d2 | 2009-09-23 21:53:25 +0000 | [diff] [blame] | 28 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 29 | \*****************************************************************************/ |
| 30 | |
| 31 | #include "common.h" |
| 32 | #include "opts.h" |
| 33 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 34 | nvramtool_op_info_t nvramtool_op; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 35 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 36 | nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 37 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 38 | static char *handle_optional_arg(int argc, char *argv[]); |
| 39 | static void register_op(int *op_found, nvramtool_op_t op, char op_param[]); |
| 40 | static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]); |
| 41 | static void resolve_op_modifiers(void); |
| 42 | static void sanity_check_args(void); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 43 | |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 44 | static const char getopt_string[] = "-ab:B:c::C:dD:e:hH:iL:l::np:r:tvw:xX:y:Y"; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 45 | |
| 46 | /**************************************************************************** |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 47 | * parse_nvramtool_args |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 48 | * |
| 49 | * Parse command line arguments. |
| 50 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 51 | void parse_nvramtool_args(int argc, char *argv[]) |
| 52 | { |
| 53 | nvramtool_op_modifier_info_t *mod_info; |
| 54 | int i, op_found; |
| 55 | char c; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 56 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 57 | for (i = 0, mod_info = nvramtool_op_modifiers; |
| 58 | i < NVRAMTOOL_NUM_OP_MODIFIERS; i++, mod_info++) { |
| 59 | mod_info->found = FALSE; |
| 60 | mod_info->found_seq = 0; |
| 61 | mod_info->param = NULL; |
| 62 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 63 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 64 | op_found = FALSE; |
| 65 | opterr = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 66 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 67 | do { |
| 68 | switch (c = getopt(argc, argv, getopt_string)) { |
| 69 | case 'a': |
| 70 | register_op(&op_found, |
| 71 | NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL); |
| 72 | break; |
| 73 | case 'b': |
| 74 | register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP, |
| 75 | optarg); |
| 76 | break; |
| 77 | case 'B': |
| 78 | register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP, |
| 79 | optarg); |
| 80 | break; |
| 81 | case 'c': |
| 82 | register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM, |
| 83 | handle_optional_arg(argc, argv)); |
| 84 | break; |
Patrick Georgi | 269e932 | 2011-01-21 07:24:08 +0000 | [diff] [blame] | 85 | case 'C': |
| 86 | register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE, |
| 87 | optarg); |
| 88 | break; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 89 | case 'd': |
| 90 | register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL); |
| 91 | break; |
Patrick Georgi | 202be7b | 2011-01-21 07:29:40 +0000 | [diff] [blame] | 92 | case 'D': |
| 93 | register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE, |
| 94 | optarg); |
| 95 | break; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 96 | case 'e': |
| 97 | register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES, |
| 98 | optarg); |
| 99 | break; |
| 100 | case 'h': |
| 101 | register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL); |
| 102 | break; |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 103 | case 'H': |
| 104 | register_op(&op_found, NVRAMTOOL_OP_WRITE_HEADER_FILE, optarg); |
| 105 | break; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 106 | case 'i': |
| 107 | register_op(&op_found, |
| 108 | NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL); |
| 109 | break; |
| 110 | case 'l': |
| 111 | register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO, |
| 112 | handle_optional_arg(argc, argv)); |
| 113 | break; |
Vikram Narayanan | a8111cf | 2012-04-14 15:25:13 +0530 | [diff] [blame] | 114 | case 'L': |
| 115 | register_op(&op_found, NVRAMTOOL_OP_WRITE_BINARY_FILE, |
| 116 | optarg); |
| 117 | break; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 118 | case 'n': |
| 119 | register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY, |
| 120 | NULL); |
| 121 | break; |
| 122 | case 'p': |
| 123 | register_op(&op_found, |
| 124 | NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg); |
| 125 | break; |
| 126 | case 'r': |
| 127 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM, |
| 128 | optarg); |
| 129 | break; |
| 130 | case 't': |
| 131 | register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE, |
| 132 | NULL); |
| 133 | break; |
| 134 | case 'v': |
| 135 | register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL); |
| 136 | break; |
| 137 | case 'w': |
| 138 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM, |
| 139 | optarg); |
| 140 | break; |
| 141 | case 'x': |
| 142 | register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP, |
| 143 | NULL); |
| 144 | break; |
| 145 | case 'X': |
| 146 | register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE, |
| 147 | optarg); |
| 148 | break; |
| 149 | case 'y': |
| 150 | register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE, |
| 151 | optarg); |
| 152 | break; |
| 153 | case 'Y': |
| 154 | register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL); |
| 155 | break; |
| 156 | case -1: /* no more command line args */ |
| 157 | break; |
| 158 | case '?': /* unknown option found */ |
| 159 | case 1: /* nonoption command line arg found */ |
| 160 | default: |
| 161 | usage(stderr); |
| 162 | break; |
| 163 | } |
| 164 | } while (c != -1); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 165 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 166 | if (!op_found) |
| 167 | usage(stderr); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 168 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 169 | resolve_op_modifiers(); |
| 170 | sanity_check_args(); |
| 171 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 172 | |
| 173 | /**************************************************************************** |
| 174 | * handle_optional_arg |
| 175 | * |
| 176 | * Handle a command line option with an optional argument. |
| 177 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 178 | static char *handle_optional_arg(int argc, char *argv[]) |
| 179 | { |
| 180 | char *arg; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 181 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 182 | if (optarg != NULL) { |
| 183 | /* optional arg is present and arg was specified as |
| 184 | * "-zarg" (with no whitespace between "z" and "arg"), |
| 185 | * where -z is the option and "arg" is the value of the |
| 186 | * optional arg |
| 187 | */ |
| 188 | return optarg; |
| 189 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 190 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 191 | if ((argv[optind] == NULL) || (argv[optind][0] == '-')) |
| 192 | return NULL; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 193 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 194 | arg = argv[optind]; /* optional arg is present */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 195 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 196 | /* This call to getopt yields the optional arg we just found, |
| 197 | * which we want to skip. |
| 198 | */ |
| 199 | getopt(argc, argv, getopt_string); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 200 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 201 | return arg; |
| 202 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 203 | |
| 204 | /**************************************************************************** |
| 205 | * register_op |
| 206 | * |
| 207 | * Store the user's selection of which operation this program should perform. |
| 208 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 209 | static void register_op(int *op_found, nvramtool_op_t op, char op_param[]) |
| 210 | { |
| 211 | if (*op_found && (op != nvramtool_op.op)) |
| 212 | usage(stderr); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 213 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 214 | *op_found = TRUE; |
| 215 | nvramtool_op.op = op; |
| 216 | nvramtool_op.param = op_param; |
| 217 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 218 | |
| 219 | /**************************************************************************** |
| 220 | * register_op_modifier |
| 221 | * |
| 222 | * Store information regarding an optional argument specified in addition to |
| 223 | * the user's selection of which operation this program should perform. |
| 224 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 225 | static void register_op_modifier(nvramtool_op_modifier_t mod, char mod_param[]) |
| 226 | { |
| 227 | static int found_seq = 0; |
| 228 | nvramtool_op_modifier_info_t *mod_info; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 229 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 230 | mod_info = &nvramtool_op_modifiers[mod]; |
| 231 | mod_info->found = TRUE; |
| 232 | mod_info->found_seq = ++found_seq; |
| 233 | mod_info->param = mod_param; |
| 234 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 235 | |
| 236 | /**************************************************************************** |
| 237 | * resolve_op_modifiers |
| 238 | * |
| 239 | * If the user specifies multiple arguments that conflict with each other, |
| 240 | * the last specified argument overrides previous conflicting arguments. |
| 241 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 242 | static void resolve_op_modifiers(void) |
| 243 | { |
| 244 | if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found && |
| 245 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) { |
| 246 | if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq > |
| 247 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq) |
| 248 | nvramtool_op_modifiers |
| 249 | [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE; |
| 250 | else |
| 251 | nvramtool_op_modifiers |
| 252 | [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE; |
| 253 | } |
| 254 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 255 | |
| 256 | /**************************************************************************** |
| 257 | * sanity_check_args |
| 258 | * |
| 259 | * Perform sanity checking on command line arguments. |
| 260 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 261 | static void sanity_check_args(void) |
| 262 | { |
| 263 | if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) && |
| 264 | (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM)) |
| 265 | usage(stderr); |
| 266 | } |