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 
36 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 
38 /** @file
39  *
40  * Non-volatile option commands
41  *
42  */
43 
44 /** "show" options */
45 struct show_options {};
46 
47 /** "show" option list */
48 static struct option_descriptor show_opts[] = {};
49 
50 /** "show" command descriptor */
52  COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
53 
54 /**
55  * "show" command
56  *
57  * @v argc Argument count
58  * @v argv Argument list
59  * @ret rc Return status code
60  */
61 static int show_exec ( int argc, char **argv ) {
62  struct show_options opts;
63  struct named_setting setting;
64  struct settings *origin;
65  struct setting fetched;
66  char name_buf[32];
67  char *value;
68  int len;
69  int rc;
70 
71  /* Parse options */
72  if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
73  goto err_parse_options;
74 
75  /* Parse setting name */
76  if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
77  goto err_parse_setting;
78 
79  /* Fetch formatted setting value */
80  if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
81  &origin, &fetched, &value ) ) < 0 ) {
82  rc = len;
83  printf ( "Could not find \"%s\": %s\n",
84  setting.setting.name, strerror ( rc ) );
85  goto err_fetchf;
86  }
87 
88  /* Print setting value */
89  setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
90  printf ( "%s = %s\n", name_buf, value );
91 
92  /* Success */
93  rc = 0;
94 
95  free ( value );
96  err_fetchf:
97  err_parse_setting:
98  err_parse_options:
99  return rc;
100 }
101 
102 /** "set", "clear", and "read" options */
104  /** Timeout */
105  unsigned long timeout;
106 };
107 
108 /** "set", "clear", and "read" option list */
109 static union {
110  /* "set" takes no options */
112  /* "clear" takes no options */
114  /* "read" takes --timeout option */
116 } set_core_opts = {
117  .read = {
118  OPTION_DESC ( "timeout", 't', required_argument,
120  },
121 };
122 
123 /** "set" command descriptor */
126  1, MAX_ARGUMENTS, "<setting> <value>" );
127 
128 /** "clear" command descriptor */
131  1, 1, "<setting>" );
132 
133 /** "read" command descriptor */
136  1, 1, "<setting>" );
137 
138 /**
139  * "set", "clear", and "read" command
140  *
141  * @v argc Argument count
142  * @v argv Argument list
143  * @v cmd Command descriptor
144  * @v get_value Method to obtain setting value
145  * @ret rc Return status code
146  */
147 static int set_core_exec ( int argc, char **argv,
148  struct command_descriptor *cmd,
149  int ( * get_value ) ( struct named_setting *setting,
150  struct set_core_options *opts,
151  char **args, char **value ) ) {
152  struct set_core_options opts;
153  struct named_setting setting;
154  char *value;
155  int rc;
156 
157  /* Parse options */
158  if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
159  goto err_parse_options;
160 
161  /* Parse setting name */
162  if ( ( rc = parse_autovivified_setting ( argv[optind],
163  &setting ) ) != 0 )
164  goto err_parse_setting;
165 
166  /* Parse setting value */
167  if ( ( rc = get_value ( &setting, &opts, &argv[ optind + 1 ],
168  &value ) ) != 0 )
169  goto err_get_value;
170 
171  /* Apply default type if necessary */
172  if ( ! setting.setting.type )
173  setting.setting.type = &setting_type_string;
174 
175  /* Store setting */
176  if ( ( rc = storef_setting ( setting.settings, &setting.setting,
177  value ) ) != 0 ) {
178  printf ( "Could not store \"%s\": %s\n",
179  setting.setting.name, strerror ( rc ) );
180  goto err_store;
181  }
182 
183  err_store:
184  free ( value );
185  err_get_value:
186  err_parse_setting:
187  err_parse_options:
188  return rc;
189 }
190 
191 /**
192  * Get setting value for "set" command
193  *
194  * @v setting Named setting
195  * @v opts Options list
196  * @v args Remaining arguments
197  * @ret value Setting value
198  * @ret rc Return status code
199  */
202  char **args, char **value ) {
203 
204  *value = concat_args ( args );
205  if ( ! *value )
206  return -ENOMEM;
207 
208  return 0;
209 }
210 
211 /**
212  * "set" command
213  *
214  * @v argc Argument count
215  * @v argv Argument list
216  * @ret rc Return status code
217  */
218 static int set_exec ( int argc, char **argv ) {
219  return set_core_exec ( argc, argv, &set_cmd, set_value );
220 }
221 
222 /**
223  * Get setting value for "clear" command
224  *
225  * @v setting Named setting
226  * @v args Remaining arguments
227  * @v opts Options list
228  * @ret value Setting value
229  * @ret rc Return status code
230  */
233  char **args __unused, char **value ) {
234 
235  *value = NULL;
236  return 0;
237 }
238 
239 /**
240  * "clear" command
241  *
242  * @v argc Argument count
243  * @v argv Argument list
244  * @ret rc Return status code
245  */
246 static int clear_exec ( int argc, char **argv ) {
247  return set_core_exec ( argc, argv, &clear_cmd, clear_value );
248 }
249 
250 /**
251  * Get setting value for "read" command
252  *
253  * @v setting Named setting
254  * @v args Remaining arguments
255  * @v opts Options list
256  * @ret value Setting value
257  * @ret rc Return status code
258  */
259 static int read_value ( struct named_setting *setting,
260  struct set_core_options *opts,
261  char **args __unused, char **value ) {
262  char *existing;
263  int rc;
264 
265  /* Read existing value, treating errors as equivalent to an
266  * empty initial setting.
267  */
268  fetchf_setting_copy ( setting->settings, &setting->setting,
269  NULL, &setting->setting, &existing );
270 
271  /* Read new value */
272  if ( ( rc = readline_history ( NULL, existing, NULL, opts->timeout,
273  value ) ) != 0 )
274  goto err_readline;
275 
276  err_readline:
277  free ( existing );
278  return rc;
279 }
280 
281 /**
282  * "read" command
283  *
284  * @v argc Argument count
285  * @v argv Argument list
286  * @ret rc Return status code
287  */
288 static int read_exec ( int argc, char **argv ) {
289  return set_core_exec ( argc, argv, &read_cmd, read_value );
290 }
291 
292 /** "inc" options */
293 struct inc_options {};
294 
295 /** "inc" option list */
296 static struct option_descriptor inc_opts[] = {};
297 
298 /** "inc" command descriptor */
300  COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
301  "<setting> [<increment>]" );
302 
303 /**
304  * "inc" command
305  *
306  * @v argc Argument count
307  * @v argv Argument list
308  * @ret rc Return status code
309  */
310 static int inc_exec ( int argc, char **argv ) {
311  struct inc_options opts;
312  struct named_setting setting;
313  unsigned int increment = 1;
314  unsigned long value;
315  int rc;
316 
317  /* Parse options */
318  if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
319  goto err_parse_options;
320 
321  /* Parse setting name */
322  if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
323  goto err_parse_setting;
324 
325  /* Parse increment (if present) */
326  if ( ( ( optind + 1 ) < argc ) &&
327  ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
328  goto err_parse_increment;
329 
330  /* Read existing value, treating errors as equivalent to a
331  * zero-valued :int32 initial setting.
332  */
333  if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
334  NULL, &setting.setting, &value ) ) != 0 ) {
335  value = 0;
336  if ( ! setting.setting.type )
337  setting.setting.type = &setting_type_int32;
338  }
339 
340  /* Increment value */
341  value += increment;
342 
343  /* Store updated setting value */
344  if ( ( rc = storen_setting ( setting.settings, &setting.setting,
345  value ) ) != 0 ) {
346  printf ( "Could not store \"%s\": %s\n",
347  setting.setting.name, strerror ( rc ) );
348  goto err_store;
349  }
350 
351  err_store:
352  err_parse_increment:
353  err_parse_setting:
354  err_parse_options:
355  return rc;
356 }
357 
358 /** Non-volatile option commands */
359 struct command nvo_commands[] __command = {
360  {
361  .name = "show",
362  .exec = show_exec,
363  },
364  {
365  .name = "set",
366  .exec = set_exec,
367  },
368  {
369  .name = "clear",
370  .exec = clear_exec,
371  },
372  {
373  .name = "read",
374  .exec = read_exec,
375  },
376  {
377  .name = "inc",
378  .exec = inc_exec,
379  },
380 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:88
struct option_descriptor read[1]
Definition: nvo_cmd.c:115
"set", "clear", and "read" options
Definition: nvo_cmd.c:103
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t origin
Origin.
Definition: hyperv.h:20
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
A parsed named setting.
Definition: parseopt.h:121
static struct command_descriptor set_cmd
"set" command descriptor
Definition: nvo_cmd.c:124
int optind
Current option index.
Definition: getopt.c:51
struct option_descriptor set[0]
Definition: nvo_cmd.c:111
static int inc_exec(int argc, char **argv)
"inc" command
Definition: nvo_cmd.c:310
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:1276
static int clear_exec(int argc, char **argv)
"clear" command
Definition: nvo_cmd.c:246
Error codes.
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:231
A command-line command.
Definition: command.h:9
static struct option_descriptor show_opts[]
"show" option list
Definition: nvo_cmd.c:48
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:111
int storen_setting(struct settings *settings, const struct setting *setting, unsigned long value)
Store numeric value of setting.
Definition: settings.c:1414
int parse_autovivified_setting(char *text, struct named_setting *setting)
Parse and autovivify setting name.
Definition: parseopt.c:299
static int show_exec(int argc, char **argv)
"show" command
Definition: nvo_cmd.c:61
static union @425 opts
"cert<xxx>" option list
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:200
static struct command_descriptor clear_cmd
"clear" command descriptor
Definition: nvo_cmd.c:129
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
A command descriptor.
Definition: parseopt.h:76
static struct option_descriptor inc_opts[]
"inc" option list
Definition: nvo_cmd.c:296
static int set_exec(int argc, char **argv)
"set" command
Definition: nvo_cmd.c:218
const char * name
Name.
Definition: settings.h:28
Minmal readline.
#define ENOMEM
Not enough space.
Definition: errno.h:534
"inc" options
Definition: nvo_cmd.c:293
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Parse command-line options.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
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:147
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
const struct setting_type * type
Setting type.
Definition: settings.h:36
int parse_existing_setting(char *text, struct named_setting *setting)
Parse existing setting name.
Definition: parseopt.c:285
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
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:1372
Configuration settings.
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition: settings.c:1319
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A readline history buffer.
Definition: readline.h:31
static struct command_descriptor show_cmd
"show" command descriptor
Definition: nvo_cmd.c:51
Command line option parsing.
static union @427 set_core_opts
"set", "clear", and "read" option list
"show" options
Definition: nvo_cmd.c:45
A settings block.
Definition: settings.h:132
A setting.
Definition: settings.h:23
const char * name
Name of the command.
Definition: command.h:11
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
unsigned long timeout
Timeout.
Definition: nvo_cmd.c:105
struct option_descriptor clear[0]
Definition: nvo_cmd.c:113
uint32_t len
Length.
Definition: ena.h:14
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:259
static int read_exec(int argc, char **argv)
"read" command
Definition: nvo_cmd.c:288
struct command nvo_commands [] __command
Non-volatile option commands.
Definition: nvo_cmd.c:359
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:66
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:22
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
void timeout(int)
static struct command_descriptor inc_cmd
"inc" command descriptor
Definition: nvo_cmd.c:299
static struct command_descriptor read_cmd
"read" command descriptor
Definition: nvo_cmd.c:134
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition: settings.c:1606
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
String functions.