Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / nvo_cmd.c
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
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <byteswap.h>
27 #include <ipxe/settings.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <readline/readline.h>
31
32 FILE_LICENCE ( GPL2_OR_LATER );
33
34 /** @file
35  *
36  * Non-volatile option commands
37  *
38  */
39
40 /** "show" options */
41 struct show_options {};
42
43 /** "show" option list */
44 static struct option_descriptor show_opts[] = {};
45
46 /** "show" command descriptor */
47 static struct command_descriptor show_cmd =
48         COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
49
50 /**
51  * "show" command
52  *
53  * @v argc              Argument count
54  * @v argv              Argument list
55  * @ret rc              Return status code
56  */
57 static int show_exec ( int argc, char **argv ) {
58         struct show_options opts;
59         struct named_setting setting;
60         struct settings *origin;
61         struct setting fetched;
62         char name_buf[32];
63         char *value;
64         int len;
65         int rc;
66
67         /* Parse options */
68         if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
69                 goto err_parse_options;
70
71         /* Parse setting name */
72         if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
73                 goto err_parse_setting;
74
75         /* Fetch formatted setting value */
76         if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
77                                            &origin, &fetched, &value ) ) < 0 ) {
78                 rc = len;
79                 printf ( "Could not find \"%s\": %s\n",
80                          setting.setting.name, strerror ( rc ) );
81                 goto err_fetchf;
82         }
83
84         /* Print setting value */
85         setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
86         printf ( "%s = %s\n", name_buf, value );
87
88         /* Success */
89         rc = 0;
90
91         free ( value );
92  err_fetchf:
93  err_parse_setting:
94  err_parse_options:
95         return rc;
96 }
97
98 /** "set", "clear", and "read" options */
99 struct set_core_options {};
100
101 /** "set", "clear", and "read" option list */
102 static struct option_descriptor set_core_opts[] = {};
103
104 /** "set" command descriptor */
105 static struct command_descriptor set_cmd =
106         COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
107                        "<setting> <value>" );
108
109 /** "clear" and "read" command descriptor */
110 static struct command_descriptor clear_read_cmd =
111         COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
112                        "<setting>" );
113
114 /**
115  * "set", "clear", and "read" command
116  *
117  * @v argc              Argument count
118  * @v argv              Argument list
119  * @v cmd               Command descriptor
120  * @v get_value         Method to obtain setting value
121  * @ret rc              Return status code
122  */
123 static int set_core_exec ( int argc, char **argv,
124                            struct command_descriptor *cmd,
125                            int ( * get_value ) ( struct named_setting *setting,
126                                                  char **args, char **value ) ) {
127         struct set_core_options opts;
128         struct named_setting setting;
129         char *value;
130         int rc;
131
132         /* Parse options */
133         if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
134                 goto err_parse_options;
135
136         /* Parse setting name */
137         if ( ( rc = parse_autovivified_setting ( argv[optind],
138                                                  &setting ) ) != 0 )
139                 goto err_parse_setting;
140
141         /* Parse setting value */
142         if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
143                 goto err_get_value;
144
145         /* Apply default type if necessary */
146         if ( ! setting.setting.type )
147                 setting.setting.type = &setting_type_string;
148
149         /* Store setting */
150         if ( ( rc = storef_setting ( setting.settings, &setting.setting,
151                                      value ) ) != 0 ) {
152                 printf ( "Could not store \"%s\": %s\n",
153                          setting.setting.name, strerror ( rc ) );
154                 goto err_store;
155         }
156
157  err_store:
158         free ( value );
159  err_get_value:
160  err_parse_setting:
161  err_parse_options:
162         return rc;
163 }
164
165 /**
166  * Get setting value for "set" command
167  *
168  * @v setting           Named setting
169  * @v args              Remaining arguments
170  * @ret value           Setting value
171  * @ret rc              Return status code
172  */
173 static int set_value ( struct named_setting *setting __unused,
174                        char **args, char **value ) {
175
176         *value = concat_args ( args );
177         if ( ! *value )
178                 return -ENOMEM;
179
180         return 0;
181 }
182
183 /**
184  * "set" command
185  *
186  * @v argc              Argument count
187  * @v argv              Argument list
188  * @ret rc              Return status code
189  */
190 static int set_exec ( int argc, char **argv ) {
191         return set_core_exec ( argc, argv, &set_cmd, set_value );
192 }
193
194 /**
195  * Get setting value for "clear" command
196  *
197  * @v setting           Named setting
198  * @v args              Remaining arguments
199  * @ret value           Setting value
200  * @ret rc              Return status code
201  */
202 static int clear_value ( struct named_setting *setting __unused,
203                          char **args __unused, char **value ) {
204
205         *value = NULL;
206         return 0;
207 }
208
209 /**
210  * "clear" command
211  *
212  * @v argc              Argument count
213  * @v argv              Argument list
214  * @ret rc              Return status code
215  */
216 static int clear_exec ( int argc, char **argv ) {
217         return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
218 }
219
220 /**
221  * Get setting value for "read" command
222  *
223  * @v setting           Named setting
224  * @v args              Remaining arguments
225  * @ret value           Setting value
226  * @ret rc              Return status code
227  */
228 static int read_value ( struct named_setting *setting, char **args __unused,
229                         char **value ) {
230         char *existing;
231         int rc;
232
233         /* Read existing value, treating errors as equivalent to an
234          * empty initial setting.
235          */
236         fetchf_setting_copy ( setting->settings, &setting->setting,
237                               NULL, &setting->setting, &existing );
238
239         /* Read new value */
240         if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
241                 goto err_readline;
242
243  err_readline:
244         free ( existing );
245         return rc;
246 }
247
248 /**
249  * "read" command
250  *
251  * @v argc              Argument count
252  * @v argv              Argument list
253  * @ret rc              Return status code
254  */
255 static int read_exec ( int argc, char **argv ) {
256         return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
257 }
258
259 /** "inc" options */
260 struct inc_options {};
261
262 /** "inc" option list */
263 static struct option_descriptor inc_opts[] = {};
264
265 /** "inc" command descriptor */
266 static struct command_descriptor inc_cmd =
267         COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
268                        "<setting> [<increment>]" );
269
270 /**
271  * "inc" command
272  *
273  * @v argc              Argument count
274  * @v argv              Argument list
275  * @ret rc              Return status code
276  */
277 static int inc_exec ( int argc, char **argv ) {
278         struct inc_options opts;
279         struct named_setting setting;
280         unsigned int increment = 1;
281         unsigned long value;
282         int rc;
283
284         /* Parse options */
285         if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
286                 goto err_parse_options;
287
288         /* Parse setting name */
289         if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
290                 goto err_parse_setting;
291
292         /* Parse increment (if present) */
293         if ( ( ( optind + 1 ) < argc ) &&
294              ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
295                 goto err_parse_increment;
296
297         /* Read existing value, treating errors as equivalent to a
298          * zero-valued :int32 initial setting.
299          */
300         if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
301                                      NULL, &setting.setting, &value ) ) != 0 ) {
302                 value = 0;
303                 if ( ! setting.setting.type )
304                         setting.setting.type = &setting_type_int32;
305         }
306
307         /* Increment value */
308         value += increment;
309
310         /* Store updated setting value */
311         if ( ( rc = storen_setting ( setting.settings, &setting.setting,
312                                      value ) ) != 0 ) {
313                 printf ( "Could not store \"%s\": %s\n",
314                          setting.setting.name, strerror ( rc ) );
315                 goto err_store;
316         }
317
318  err_store:
319  err_parse_increment:
320  err_parse_setting:
321  err_parse_options:
322         return rc;
323 }
324
325 /** Non-volatile option commands */
326 struct command nvo_commands[] __command = {
327         {
328                 .name = "show",
329                 .exec = show_exec,
330         },
331         {
332                 .name = "set",
333                 .exec = set_exec,
334         },      
335         {
336                 .name = "clear",
337                 .exec = clear_exec,
338         },
339         {
340                 .name = "read",
341                 .exec = read_exec,
342         },
343         {
344                 .name = "inc",
345                 .exec = inc_exec,
346         },
347 };