1 /* SPDX-License-Identifier: GPL-2.0-only */
6 nvramtool_op_info_t nvramtool_op
;
8 nvramtool_op_modifier_info_t nvramtool_op_modifiers
[NVRAMTOOL_NUM_OP_MODIFIERS
];
10 static char *handle_optional_arg(int argc
, char *argv
[]);
11 static void register_op(int *op_found
, nvramtool_op_t op
, char op_param
[]);
12 static void register_op_modifier(nvramtool_op_modifier_t mod
, char mod_param
[]);
13 static void resolve_op_modifiers(void);
14 static void sanity_check_args(void);
16 static const char getopt_string
[] = "-ab:B:c::C:dD:e:hH:iL:l::np:r:tvw:xX:y:Y";
18 /****************************************************************************
19 * parse_nvramtool_args
21 * Parse command line arguments.
22 ****************************************************************************/
23 void parse_nvramtool_args(int argc
, char *argv
[])
25 nvramtool_op_modifier_info_t
*mod_info
;
28 for (i
= 0, mod_info
= nvramtool_op_modifiers
;
29 i
< NVRAMTOOL_NUM_OP_MODIFIERS
; i
++, mod_info
++) {
30 mod_info
->found
= FALSE
;
31 mod_info
->found_seq
= 0;
32 mod_info
->param
= NULL
;
39 switch (c
= getopt(argc
, argv
, getopt_string
)) {
41 register_op(&op_found
,
42 NVRAMTOOL_OP_CMOS_SHOW_ALL_PARAMS
, NULL
);
45 register_op(&op_found
, NVRAMTOOL_OP_WRITE_CMOS_DUMP
,
49 register_op(&op_found
, NVRAMTOOL_OP_READ_CMOS_DUMP
,
53 register_op(&op_found
, NVRAMTOOL_OP_CMOS_CHECKSUM
,
54 handle_optional_arg(argc
, argv
));
57 register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE
,
61 register_op(&op_found
, NVRAMTOOL_OP_LBTABLE_DUMP
, NULL
);
64 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_FILE
,
68 register_op(&op_found
, NVRAMTOOL_OP_SHOW_PARAM_VALUES
,
72 register_op(&op_found
, NVRAMTOOL_OP_SHOW_USAGE
, NULL
);
75 register_op(&op_found
, NVRAMTOOL_OP_WRITE_HEADER_FILE
, optarg
);
78 register_op(&op_found
,
79 NVRAMTOOL_OP_CMOS_SET_PARAMS_STDIN
, NULL
);
82 register_op(&op_found
, NVRAMTOOL_OP_LBTABLE_SHOW_INFO
,
83 handle_optional_arg(argc
, argv
));
86 register_op(&op_found
, NVRAMTOOL_OP_WRITE_BINARY_FILE
,
90 register_op_modifier(NVRAMTOOL_MOD_SHOW_VALUE_ONLY
,
94 register_op(&op_found
,
95 NVRAMTOOL_OP_CMOS_SET_PARAMS_FILE
, optarg
);
98 register_op(&op_found
, NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM
,
102 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE
,
106 register_op(&op_found
, NVRAMTOOL_OP_SHOW_VERSION
, NULL
);
109 register_op(&op_found
, NVRAMTOOL_OP_CMOS_SET_ONE_PARAM
,
113 register_op(&op_found
, NVRAMTOOL_OP_SHOW_CMOS_HEX_DUMP
,
117 register_op(&op_found
, NVRAMTOOL_OP_SHOW_CMOS_DUMPFILE
,
121 register_op_modifier(NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
,
125 register_op(&op_found
, NVRAMTOOL_OP_SHOW_LAYOUT
, NULL
);
127 case -1: /* no more command line args */
129 case '?': /* unknown option found */
130 case 1: /* nonoption command line arg found */
140 resolve_op_modifiers();
144 /****************************************************************************
145 * handle_optional_arg
147 * Handle a command line option with an optional argument.
148 ****************************************************************************/
149 static char *handle_optional_arg(int argc
, char *argv
[])
153 if (optarg
!= NULL
) {
154 /* optional arg is present and arg was specified as
155 * "-zarg" (with no whitespace between "z" and "arg"),
156 * where -z is the option and "arg" is the value of the
162 if ((argv
[optind
] == NULL
) || (argv
[optind
][0] == '-'))
165 arg
= argv
[optind
]; /* optional arg is present */
167 /* This call to getopt yields the optional arg we just found,
168 * which we want to skip.
170 getopt(argc
, argv
, getopt_string
);
175 /****************************************************************************
178 * Store the user's selection of which operation this program should perform.
179 ****************************************************************************/
180 static void register_op(int *op_found
, nvramtool_op_t op
, char op_param
[])
182 if (*op_found
&& (op
!= nvramtool_op
.op
))
186 nvramtool_op
.op
= op
;
187 nvramtool_op
.param
= op_param
;
190 /****************************************************************************
191 * register_op_modifier
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(nvramtool_op_modifier_t mod
, char mod_param
[])
198 static int found_seq
= 0;
199 nvramtool_op_modifier_info_t
*mod_info
;
201 mod_info
= &nvramtool_op_modifiers
[mod
];
202 mod_info
->found
= TRUE
;
203 mod_info
->found_seq
= ++found_seq
;
204 mod_info
->param
= mod_param
;
207 /****************************************************************************
208 * resolve_op_modifiers
210 * If the user specifies multiple arguments that conflict with each other,
211 * the last specified argument overrides previous conflicting arguments.
212 ****************************************************************************/
213 static void resolve_op_modifiers(void)
215 if (nvramtool_op_modifiers
[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
].found
&&
216 nvramtool_op_modifiers
[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE
].found
) {
217 if (nvramtool_op_modifiers
[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
].found_seq
>
218 nvramtool_op_modifiers
[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE
].found_seq
)
219 nvramtool_op_modifiers
220 [NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE
].found
= FALSE
;
222 nvramtool_op_modifiers
223 [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE
].found
= FALSE
;
227 /****************************************************************************
230 * Perform sanity checking on command line arguments.
231 ****************************************************************************/
232 static void sanity_check_args(void)
234 if ((nvramtool_op_modifiers
[NVRAMTOOL_MOD_SHOW_VALUE_ONLY
].found
) &&
235 (nvramtool_op
.op
!= NVRAMTOOL_OP_CMOS_SHOW_ONE_PARAM
))