iPXE
nvo_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24#include <stdint.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <getopt.h>
30#include <byteswap.h>
31#include <ipxe/settings.h>
32#include <ipxe/command.h>
33#include <ipxe/parseopt.h>
34#include <readline/readline.h>
35
36FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37FILE_SECBOOT ( PERMITTED );
38
39/** @file
40 *
41 * Non-volatile option commands
42 *
43 */
44
45/** "show" options */
46struct show_options {};
47
48/** "show" option list */
49static struct option_descriptor show_opts[] = {};
50
51/** "show" command descriptor */
53 COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
54
55/**
56 * "show" command
57 *
58 * @v argc Argument count
59 * @v argv Argument list
60 * @ret rc Return status code
61 */
62static int show_exec ( int argc, char **argv ) {
63 struct show_options opts;
65 struct settings *origin;
66 struct setting fetched;
67 char name_buf[32];
68 char *value;
69 int len;
70 int rc;
71
72 /* Parse options */
73 if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
74 goto err_parse_options;
75
76 /* Parse setting name */
77 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
78 goto err_parse_setting;
79
80 /* Fetch formatted setting value */
81 if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
82 &origin, &fetched, &value ) ) < 0 ) {
83 rc = len;
84 printf ( "Could not find \"%s\": %s\n",
85 setting.setting.name, strerror ( rc ) );
86 goto err_fetchf;
87 }
88
89 /* Print setting value */
90 setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
91 printf ( "%s = %s\n", name_buf, value );
92
93 /* Success */
94 rc = 0;
95
96 free ( value );
97 err_fetchf:
98 err_parse_setting:
99 err_parse_options:
100 return rc;
101}
102
103/** "set", "clear", and "read" options */
105 /** Timeout */
106 unsigned long timeout;
107};
108
109/** "set", "clear", and "read" option list */
110static union {
111 /* "set" takes no options */
113 /* "clear" takes no options */
115 /* "read" takes --timeout option */
117} set_core_opts = {
118 .read = {
119 OPTION_DESC ( "timeout", 't', required_argument,
121 },
123
124/** "set" command descriptor */
127 1, MAX_ARGUMENTS, "<setting> <value>" );
128
129/** "clear" command descriptor */
132 1, 1, "<setting>" );
133
134/** "read" command descriptor */
137 1, 1, "<setting>" );
138
139/**
140 * "set", "clear", and "read" command
141 *
142 * @v argc Argument count
143 * @v argv Argument list
144 * @v cmd Command descriptor
145 * @v get_value Method to obtain setting value
146 * @ret rc Return status code
147 */
148static int set_core_exec ( int argc, char **argv,
149 struct command_descriptor *cmd,
150 int ( * get_value ) ( struct named_setting *setting,
151 struct set_core_options *opts,
152 char **args, char **value ) ) {
153 struct set_core_options opts;
154 struct named_setting setting;
155 char *value;
156 int rc;
157
158 /* Parse options */
159 if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
160 goto err_parse_options;
161
162 /* Parse setting name */
163 if ( ( rc = parse_autovivified_setting ( argv[optind],
164 &setting ) ) != 0 )
165 goto err_parse_setting;
166
167 /* Parse setting value */
168 if ( ( rc = get_value ( &setting, &opts, &argv[ optind + 1 ],
169 &value ) ) != 0 )
170 goto err_get_value;
171
172 /* Apply default type if necessary */
173 if ( ! setting.setting.type )
174 setting.setting.type = &setting_type_string;
175
176 /* Store setting */
177 if ( ( rc = storef_setting ( setting.settings, &setting.setting,
178 value ) ) != 0 ) {
179 printf ( "Could not store \"%s\": %s\n",
180 setting.setting.name, strerror ( rc ) );
181 goto err_store;
182 }
183
184 err_store:
185 free ( value );
186 err_get_value:
187 err_parse_setting:
188 err_parse_options:
189 return rc;
190}
191
192/**
193 * Get setting value for "set" command
194 *
195 * @v setting Named setting
196 * @v opts Options list
197 * @v args Remaining arguments
198 * @ret value Setting value
199 * @ret rc Return status code
200 */
203 char **args, char **value ) {
204
205 *value = concat_args ( args );
206 if ( ! *value )
207 return -ENOMEM;
208
209 return 0;
210}
211
212/**
213 * "set" command
214 *
215 * @v argc Argument count
216 * @v argv Argument list
217 * @ret rc Return status code
218 */
219static int set_exec ( int argc, char **argv ) {
220 return set_core_exec ( argc, argv, &set_cmd, set_value );
221}
222
223/**
224 * Get setting value for "clear" command
225 *
226 * @v setting Named setting
227 * @v args Remaining arguments
228 * @v opts Options list
229 * @ret value Setting value
230 * @ret rc Return status code
231 */
234 char **args __unused, char **value ) {
235
236 *value = NULL;
237 return 0;
238}
239
240/**
241 * "clear" command
242 *
243 * @v argc Argument count
244 * @v argv Argument list
245 * @ret rc Return status code
246 */
247static int clear_exec ( int argc, char **argv ) {
248 return set_core_exec ( argc, argv, &clear_cmd, clear_value );
249}
250
251/**
252 * Get setting value for "read" command
253 *
254 * @v setting Named setting
255 * @v args Remaining arguments
256 * @v opts Options list
257 * @ret value Setting value
258 * @ret rc Return status code
259 */
260static int read_value ( struct named_setting *setting,
261 struct set_core_options *opts,
262 char **args __unused, char **value ) {
263 char *existing;
264 int rc;
265
266 /* Read existing value, treating errors as equivalent to an
267 * empty initial setting.
268 */
269 fetchf_setting_copy ( setting->settings, &setting->setting,
270 NULL, &setting->setting, &existing );
271
272 /* Read new value */
273 if ( ( rc = readline_history ( NULL, existing, NULL, opts->timeout,
274 value ) ) != 0 )
275 goto err_readline;
276
277 err_readline:
278 free ( existing );
279 return rc;
280}
281
282/**
283 * "read" command
284 *
285 * @v argc Argument count
286 * @v argv Argument list
287 * @ret rc Return status code
288 */
289static int read_exec ( int argc, char **argv ) {
290 return set_core_exec ( argc, argv, &read_cmd, read_value );
291}
292
293/** "inc" options */
294struct inc_options {};
295
296/** "inc" option list */
297static struct option_descriptor inc_opts[] = {};
298
299/** "inc" command descriptor */
301 COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
302 "<setting> [<increment>]" );
303
304/**
305 * "inc" command
306 *
307 * @v argc Argument count
308 * @v argv Argument list
309 * @ret rc Return status code
310 */
311static int inc_exec ( int argc, char **argv ) {
312 struct inc_options opts;
313 struct named_setting setting;
314 unsigned int increment = 1;
315 unsigned long value;
316 int rc;
317
318 /* Parse options */
319 if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
320 goto err_parse_options;
321
322 /* Parse setting name */
323 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
324 goto err_parse_setting;
325
326 /* Parse increment (if present) */
327 if ( ( ( optind + 1 ) < argc ) &&
328 ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
329 goto err_parse_increment;
330
331 /* Read existing value, treating errors as equivalent to a
332 * zero-valued :int32 initial setting.
333 */
334 if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
335 NULL, &setting.setting, &value ) ) != 0 ) {
336 value = 0;
337 if ( ! setting.setting.type )
338 setting.setting.type = &setting_type_int32;
339 }
340
341 /* Increment value */
342 value += increment;
343
344 /* Store updated setting value */
345 if ( ( rc = storen_setting ( setting.settings, &setting.setting,
346 value ) ) != 0 ) {
347 printf ( "Could not store \"%s\": %s\n",
348 setting.setting.name, strerror ( rc ) );
349 goto err_store;
350 }
351
352 err_store:
353 err_parse_increment:
354 err_parse_setting:
355 err_parse_options:
356 return rc;
357}
358
359/** Non-volatile option commands */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct golan_eqe_cmd cmd
Definition CIB_PRM.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
void timeout(int)
ring len
Length.
Definition dwmac.h:226
Error codes.
char * concat_args(char **args)
Concatenate arguments.
Definition exec.c:359
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
uint64_t origin
Origin.
Definition hyperv.h:9
Configuration settings.
String functions.
static union @042366366224015251152162264074251001343161101060 set_core_opts
"set", "clear", and "read" option list
struct option_descriptor set[0]
Definition nvo_cmd.c:112
struct option_descriptor clear[0]
Definition nvo_cmd.c:114
static int read_exec(int argc, char **argv)
"read" command
Definition nvo_cmd.c:289
static int read_value(struct named_setting *setting, struct set_core_options *opts, char **args __unused, char **value)
Get setting value for "read" command.
Definition nvo_cmd.c:260
struct option_descriptor read[1]
Definition nvo_cmd.c:116
static struct command_descriptor set_cmd
"set" command descriptor
Definition nvo_cmd.c:125
static int set_core_exec(int argc, char **argv, struct command_descriptor *cmd, int(*get_value)(struct named_setting *setting, struct set_core_options *opts, char **args, char **value))
"set", "clear", and "read" command
Definition nvo_cmd.c:148
static struct option_descriptor inc_opts[]
"inc" option list
Definition nvo_cmd.c:297
static int clear_exec(int argc, char **argv)
"clear" command
Definition nvo_cmd.c:247
static int clear_value(struct named_setting *setting __unused, struct set_core_options *opts __unused, char **args __unused, char **value)
Get setting value for "clear" command.
Definition nvo_cmd.c:232
static struct command_descriptor clear_cmd
"clear" command descriptor
Definition nvo_cmd.c:130
static int set_value(struct named_setting *setting __unused, struct set_core_options *opts __unused, char **args, char **value)
Get setting value for "set" command.
Definition nvo_cmd.c:201
static struct command_descriptor read_cmd
"read" command descriptor
Definition nvo_cmd.c:135
static struct option_descriptor show_opts[]
"show" option list
Definition nvo_cmd.c:49
static int set_exec(int argc, char **argv)
"set" command
Definition nvo_cmd.c:219
static struct command_descriptor show_cmd
"show" command descriptor
Definition nvo_cmd.c:52
static int inc_exec(int argc, char **argv)
"inc" command
Definition nvo_cmd.c:311
static int show_exec(int argc, char **argv)
"show" command
Definition nvo_cmd.c:62
static struct command_descriptor inc_cmd
"inc" command descriptor
Definition nvo_cmd.c:300
int parse_existing_setting(char *text, struct named_setting *setting)
Parse existing setting name.
Definition parseopt.c:323
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition parseopt.c:337
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition parseopt.c:92
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition parseopt.c:115
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition parseopt.h:98
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition parseopt.h:109
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
Minmal readline.
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition settings.c:1607
int fetchn_setting(struct settings *settings, const struct setting *setting, struct settings **origin, struct setting *fetched, unsigned long *value)
Fetch numeric value of setting.
Definition settings.c:1373
int fetchf_setting_copy(struct settings *settings, const struct setting *setting, struct settings **origin, struct setting *fetched, char **value)
Fetch copy of formatted value of setting.
Definition settings.c:1277
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition settings.c:1320
int storen_setting(struct settings *settings, const struct setting *setting, unsigned long value)
Store numeric value of setting.
Definition settings.c:1415
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A command descriptor.
Definition parseopt.h:78
"inc" options
Definition nvo_cmd.c:294
A parsed named setting.
Definition parseopt.h:123
A command-line option descriptor.
Definition parseopt.h:24
A readline history buffer.
Definition readline.h:32
"set", "clear", and "read" options
Definition nvo_cmd.c:104
unsigned long timeout
Timeout.
Definition nvo_cmd.c:106
A setting.
Definition settings.h:24
const char * name
Name.
Definition settings.h:29
const struct setting_type * type
Setting type.
Definition settings.h:37
A settings block.
Definition settings.h:133
"show" options
Definition nvo_cmd.c:46
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465