Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * opts.c |
Uwe Hermann | 1a6177b | 2008-01-25 15:08:37 +0000 | [diff] [blame] | 3 | * $Id$ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 11 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 12 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 13 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 35 | nvramtool_op_info_t nvramtool_op; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 36 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 37 | nvramtool_op_modifier_info_t nvramtool_op_modifiers[NVRAMTOOL_NUM_OP_MODIFIERS]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 38 | |
| 39 | static char * handle_optional_arg (int argc, char *argv[]); |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 40 | static void register_op (int *op_found, nvramtool_op_t op, char op_param[]); |
| 41 | static void register_op_modifier (nvramtool_op_modifier_t mod, char mod_param[]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | /**************************************************************************** |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 48 | * parse_nvramtool_args |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 49 | * |
| 50 | * Parse command line arguments. |
| 51 | ****************************************************************************/ |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 52 | void parse_nvramtool_args (int argc, char *argv[]) |
| 53 | { nvramtool_op_modifier_info_t *mod_info; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 54 | int i, op_found; |
| 55 | char c; |
| 56 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 57 | for (i = 0, mod_info = nvramtool_op_modifiers; |
| 58 | i < NVRAMTOOL_NUM_OP_MODIFIERS; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 71 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 72 | break; |
| 73 | case 'b': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 74 | register_op(&op_found, NVRAMTOOL_OP_WRITE_CMOS_DUMP, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 75 | break; |
| 76 | case 'B': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 77 | register_op(&op_found, NVRAMTOOL_OP_READ_CMOS_DUMP, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 78 | break; |
| 79 | case 'c': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 80 | register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM, |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 81 | handle_optional_arg(argc, argv)); |
| 82 | break; |
| 83 | case 'd': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 84 | register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 85 | break; |
| 86 | case 'e': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 87 | register_op(&op_found, NVRAMTOOL_OP_SHOW_PARAM_VALUES, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 88 | break; |
| 89 | case 'h': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 90 | register_op(&op_found, NVRAMTOOL_OP_SHOW_USAGE, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 91 | break; |
| 92 | case 'i': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 93 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 94 | break; |
| 95 | case 'l': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 96 | register_op(&op_found, NVRAMTOOL_OP_LBTABLE_SHOW_INFO, |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 97 | handle_optional_arg(argc, argv)); |
| 98 | break; |
| 99 | case 'n': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 100 | register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 101 | break; |
| 102 | case 'p': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 103 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 104 | break; |
| 105 | case 'r': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 106 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 107 | break; |
| 108 | case 't': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 109 | register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 110 | break; |
| 111 | case 'v': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 112 | register_op(&op_found, NVRAMTOOL_OP_SHOW_VERSION, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 113 | break; |
| 114 | case 'w': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 115 | register_op(&op_found, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 116 | break; |
| 117 | case 'x': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 118 | register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 119 | break; |
| 120 | case 'X': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 121 | register_op(&op_found, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 122 | break; |
| 123 | case 'y': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 124 | register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE, optarg); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 125 | break; |
| 126 | case 'Y': |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 127 | register_op(&op_found, NVRAMTOOL_OP_SHOW_LAYOUT, NULL); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | ****************************************************************************/ |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 181 | static void register_op (int *op_found, nvramtool_op_t op, char op_param[]) |
| 182 | { if (*op_found && (op != nvramtool_op.op)) |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 183 | usage(stderr); |
| 184 | |
| 185 | *op_found = TRUE; |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 186 | nvramtool_op.op = op; |
| 187 | nvramtool_op.param = op_param; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | ****************************************************************************/ |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 196 | static void register_op_modifier (nvramtool_op_modifier_t mod, char mod_param[]) |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 197 | { static int found_seq = 0; |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 198 | nvramtool_op_modifier_info_t *mod_info; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 199 | |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 200 | mod_info = &nvramtool_op_modifiers[mod]; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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) |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 213 | { if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found && |
| 214 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) |
| 215 | { if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found_seq > |
| 216 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found_seq) |
| 217 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found = FALSE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 218 | else |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 219 | nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found = FALSE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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) |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame^] | 229 | { if ((nvramtool_op_modifiers[NVRAMTOOL_MOD_SHOW_VALUE_ONLY].found) && |
| 230 | (nvramtool_op.op != NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM)) |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 231 | usage(stderr); |
| 232 | } |