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 
105 /** "set", "clear", and "read" option list */
106 static struct option_descriptor set_core_opts[] = {};
107 
108 /** "set" command descriptor */
111  "<setting> <value>" );
112 
113 /** "clear" and "read" command descriptor */
116  "<setting>" );
117 
118 /**
119  * "set", "clear", and "read" command
120  *
121  * @v argc Argument count
122  * @v argv Argument list
123  * @v cmd Command descriptor
124  * @v get_value Method to obtain setting value
125  * @ret rc Return status code
126  */
127 static int set_core_exec ( int argc, char **argv,
128  struct command_descriptor *cmd,
129  int ( * get_value ) ( struct named_setting *setting,
130  char **args, char **value ) ) {
131  struct set_core_options opts;
132  struct named_setting setting;
133  char *value;
134  int rc;
135 
136  /* Parse options */
137  if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
138  goto err_parse_options;
139 
140  /* Parse setting name */
141  if ( ( rc = parse_autovivified_setting ( argv[optind],
142  &setting ) ) != 0 )
143  goto err_parse_setting;
144 
145  /* Parse setting value */
146  if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
147  goto err_get_value;
148 
149  /* Apply default type if necessary */
150  if ( ! setting.setting.type )
151  setting.setting.type = &setting_type_string;
152 
153  /* Store setting */
154  if ( ( rc = storef_setting ( setting.settings, &setting.setting,
155  value ) ) != 0 ) {
156  printf ( "Could not store \"%s\": %s\n",
157  setting.setting.name, strerror ( rc ) );
158  goto err_store;
159  }
160 
161  err_store:
162  free ( value );
163  err_get_value:
164  err_parse_setting:
165  err_parse_options:
166  return rc;
167 }
168 
169 /**
170  * Get setting value for "set" command
171  *
172  * @v setting Named setting
173  * @v args Remaining arguments
174  * @ret value Setting value
175  * @ret rc Return status code
176  */
178  char **args, char **value ) {
179 
180  *value = concat_args ( args );
181  if ( ! *value )
182  return -ENOMEM;
183 
184  return 0;
185 }
186 
187 /**
188  * "set" command
189  *
190  * @v argc Argument count
191  * @v argv Argument list
192  * @ret rc Return status code
193  */
194 static int set_exec ( int argc, char **argv ) {
195  return set_core_exec ( argc, argv, &set_cmd, set_value );
196 }
197 
198 /**
199  * Get setting value for "clear" command
200  *
201  * @v setting Named setting
202  * @v args Remaining arguments
203  * @ret value Setting value
204  * @ret rc Return status code
205  */
207  char **args __unused, char **value ) {
208 
209  *value = NULL;
210  return 0;
211 }
212 
213 /**
214  * "clear" command
215  *
216  * @v argc Argument count
217  * @v argv Argument list
218  * @ret rc Return status code
219  */
220 static int clear_exec ( int argc, char **argv ) {
221  return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
222 }
223 
224 /**
225  * Get setting value for "read" command
226  *
227  * @v setting Named setting
228  * @v args Remaining arguments
229  * @ret value Setting value
230  * @ret rc Return status code
231  */
232 static int read_value ( struct named_setting *setting, char **args __unused,
233  char **value ) {
234  char *existing;
235  int rc;
236 
237  /* Read existing value, treating errors as equivalent to an
238  * empty initial setting.
239  */
240  fetchf_setting_copy ( setting->settings, &setting->setting,
241  NULL, &setting->setting, &existing );
242 
243  /* Read new value */
244  if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
245  goto err_readline;
246 
247  err_readline:
248  free ( existing );
249  return rc;
250 }
251 
252 /**
253  * "read" command
254  *
255  * @v argc Argument count
256  * @v argv Argument list
257  * @ret rc Return status code
258  */
259 static int read_exec ( int argc, char **argv ) {
260  return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
261 }
262 
263 /** "inc" options */
264 struct inc_options {};
265 
266 /** "inc" option list */
267 static struct option_descriptor inc_opts[] = {};
268 
269 /** "inc" command descriptor */
271  COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
272  "<setting> [<increment>]" );
273 
274 /**
275  * "inc" command
276  *
277  * @v argc Argument count
278  * @v argv Argument list
279  * @ret rc Return status code
280  */
281 static int inc_exec ( int argc, char **argv ) {
282  struct inc_options opts;
283  struct named_setting setting;
284  unsigned int increment = 1;
285  unsigned long value;
286  int rc;
287 
288  /* Parse options */
289  if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
290  goto err_parse_options;
291 
292  /* Parse setting name */
293  if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
294  goto err_parse_setting;
295 
296  /* Parse increment (if present) */
297  if ( ( ( optind + 1 ) < argc ) &&
298  ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
299  goto err_parse_increment;
300 
301  /* Read existing value, treating errors as equivalent to a
302  * zero-valued :int32 initial setting.
303  */
304  if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
305  NULL, &setting.setting, &value ) ) != 0 ) {
306  value = 0;
307  if ( ! setting.setting.type )
308  setting.setting.type = &setting_type_int32;
309  }
310 
311  /* Increment value */
312  value += increment;
313 
314  /* Store updated setting value */
315  if ( ( rc = storen_setting ( setting.settings, &setting.setting,
316  value ) ) != 0 ) {
317  printf ( "Could not store \"%s\": %s\n",
318  setting.setting.name, strerror ( rc ) );
319  goto err_store;
320  }
321 
322  err_store:
323  err_parse_increment:
324  err_parse_setting:
325  err_parse_options:
326  return rc;
327 }
328 
329 /** Non-volatile option commands */
330 struct command nvo_commands[] __command = {
331  {
332  .name = "show",
333  .exec = show_exec,
334  },
335  {
336  .name = "set",
337  .exec = set_exec,
338  },
339  {
340  .name = "clear",
341  .exec = clear_exec,
342  },
343  {
344  .name = "read",
345  .exec = read_exec,
346  },
347  {
348  .name = "inc",
349  .exec = inc_exec,
350  },
351 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:88
"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:109
int optind
Current option index.
Definition: getopt.c:51
static int inc_exec(int argc, char **argv)
"inc" command
Definition: nvo_cmd.c:281
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:1280
static int clear_exec(int argc, char **argv)
"clear" command
Definition: nvo_cmd.c:220
Error codes.
static struct command_descriptor clear_read_cmd
"clear" and "read" command descriptor
Definition: nvo_cmd.c:114
A command-line command.
Definition: command.h:9
static struct option_descriptor show_opts[]
"show" option list
Definition: nvo_cmd.c:48
int storen_setting(struct settings *settings, const struct setting *setting, unsigned long value)
Store numeric value of setting.
Definition: settings.c:1418
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
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:267
static int set_exec(int argc, char **argv)
"set" command
Definition: nvo_cmd.c:194
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:264
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Parse command-line options.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
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:1376
static union @423 opts
"cert<xxx>" option list
Configuration settings.
int storef_setting(struct settings *settings, const struct setting *setting, const char *value)
Store formatted value of setting.
Definition: settings.c:1323
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.
"show" options
Definition: nvo_cmd.c:45
static int clear_value(struct named_setting *setting __unused, char **args __unused, char **value)
Get setting value for "clear" command.
Definition: nvo_cmd.c:206
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
static int read_value(struct named_setting *setting, char **args __unused, char **value)
Get setting value for "read" command.
Definition: nvo_cmd.c:232
uint32_t len
Length.
Definition: ena.h:14
static struct option_descriptor set_core_opts[]
"set", "clear", and "read" option list
Definition: nvo_cmd.c:106
static int read_exec(int argc, char **argv)
"read" command
Definition: nvo_cmd.c:259
struct command nvo_commands [] __command
Non-volatile option commands.
Definition: nvo_cmd.c:330
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
static int set_value(struct named_setting *setting __unused, char **args, char **value)
Get setting value for "set" command.
Definition: nvo_cmd.c:177
static struct command_descriptor inc_cmd
"inc" command descriptor
Definition: nvo_cmd.c:270
int setting_name(struct settings *settings, const struct setting *setting, char *buf, size_t len)
Return full setting name.
Definition: settings.c:1610
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
String functions.
static int set_core_exec(int argc, char **argv, struct command_descriptor *cmd, int(*get_value)(struct named_setting *setting, char **args, char **value))
"set", "clear", and "read" command
Definition: nvo_cmd.c:127