[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / 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 * 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 */
51 static struct command_descriptor show_cmd =
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 */
103 struct set_core_options {};
104
105 /** "set", "clear", and "read" option list */
106 static struct option_descriptor set_core_opts[] = {};
107
108 /** "set" command descriptor */
109 static struct command_descriptor set_cmd =
110 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
111 "<setting> <value>" );
112
113 /** "clear" and "read" command descriptor */
114 static struct command_descriptor clear_read_cmd =
115 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
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 */
177 static int set_value ( struct named_setting *setting __unused,
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 */
206 static int clear_value ( struct named_setting *setting __unused,
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 */
270 static struct command_descriptor inc_cmd =
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 };