Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / image_cmd.c
1 /*
2  * Copyright (C) 2007 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 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <ipxe/image.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <ipxe/shell.h>
31 #include <usr/imgmgmt.h>
32
33 /** @file
34  *
35  * Image management commands
36  *
37  */
38
39 /** "img{single}" options */
40 struct imgsingle_options {
41         /** Image name */
42         char *name;
43         /** Download timeout */
44         unsigned long timeout;
45         /** Replace image */
46         int replace;
47         /** Free image after execution */
48         int autofree;
49 };
50
51 /** "img{single}" option list */
52 static union {
53         /* "imgexec" takes all three options */
54         struct option_descriptor imgexec[4];
55         /* Other "img{single}" commands take only --name, --timeout,
56          * and --autofree
57          */
58         struct option_descriptor imgsingle[3];
59 } opts = {
60         .imgexec = {
61                 OPTION_DESC ( "name", 'n', required_argument,
62                               struct imgsingle_options, name, parse_string ),
63                 OPTION_DESC ( "timeout", 't', required_argument,
64                               struct imgsingle_options, timeout, parse_timeout),
65                 OPTION_DESC ( "autofree", 'a', no_argument,
66                               struct imgsingle_options, autofree, parse_flag ),
67                 OPTION_DESC ( "replace", 'r', no_argument,
68                               struct imgsingle_options, replace, parse_flag ),
69         },
70 };
71
72 /** An "img{single}" family command descriptor */
73 struct imgsingle_descriptor {
74         /** Command descriptor */
75         struct command_descriptor *cmd;
76         /** Function to use to acquire the image */
77         int ( * acquire ) ( const char *name, unsigned long timeout,
78                             struct image **image );
79         /** Pre-action to take upon image, or NULL */
80         void ( * preaction ) ( struct image *image );
81         /** Action to take upon image, or NULL */
82         int ( * action ) ( struct image *image,
83                            struct imgsingle_options *opts );
84         /** Verb to describe action */
85         const char *verb;
86 };
87
88 /**
89  * The "img{single}" family of commands
90  *
91  * @v argc              Argument count
92  * @v argv              Argument list
93  * @v desc              "img{single}" command descriptor
94  * @v action_name       Action name (for error messages)
95  * @v action            Action to take upon image
96  * @ret rc              Return status code
97  */
98 static int imgsingle_exec ( int argc, char **argv,
99                             struct imgsingle_descriptor *desc ) {
100         struct imgsingle_options opts;
101         char *name_uri = NULL;
102         char *cmdline = NULL;
103         struct image *image;
104         int rc;
105
106         /* Parse options */
107         if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
108                 goto err_parse_options;
109
110         /* Parse name/URI string and command line, if present */
111         if ( optind < argc ) {
112                 name_uri = argv[optind];
113                 if ( argv[ optind + 1 ] != NULL ) {
114                         cmdline = concat_args ( &argv[ optind + 1 ] );
115                         if ( ! cmdline ) {
116                                 rc = -ENOMEM;
117                                 goto err_parse_cmdline;
118                         }
119                 }
120         }
121
122         /* Acquire the image */
123         if ( name_uri ) {
124                 if ( ( rc = desc->acquire ( name_uri, opts.timeout,
125                                             &image ) ) != 0 )
126                         goto err_acquire;
127         } else {
128                 image = image_find_selected();
129                 if ( ! image ) {
130                         printf ( "No image selected\n" );
131                         goto err_acquire;
132                 }
133         }
134
135         /* Carry out command pre-action, if applicable */
136         if ( desc->preaction )
137                 desc->preaction ( image );
138
139         /* Set the image name, if applicable */
140         if ( opts.name ) {
141                 if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
142                         printf ( "Could not name image: %s\n",
143                                  strerror ( rc ) );
144                         goto err_set_name;
145                 }
146         }
147
148         /* Set the command-line arguments, if applicable */
149         if ( cmdline ) {
150                 if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
151                         printf ( "Could not set arguments: %s\n",
152                                  strerror ( rc ) );
153                         goto err_set_cmdline;
154                 }
155         }
156
157         /* Set the auto-unregister flag, if applicable */
158         if ( opts.autofree )
159                 image->flags |= IMAGE_AUTO_UNREGISTER;
160
161         /* Carry out command action, if applicable */
162         if ( desc->action ) {
163                 if ( ( rc = desc->action ( image, &opts ) ) != 0 ) {
164                         printf ( "Could not %s: %s\n",
165                                  desc->verb, strerror ( rc ) );
166                         goto err_action;
167                 }
168         }
169
170         /* Success */
171         rc = 0;
172
173  err_action:
174  err_set_cmdline:
175  err_set_name:
176  err_acquire:
177         free ( cmdline );
178  err_parse_cmdline:
179  err_parse_options:
180         return rc;
181 }
182
183 /** "imgfetch" command descriptor */
184 static struct command_descriptor imgfetch_cmd =
185         COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
186                        1, MAX_ARGUMENTS, "<uri> [<arguments>...]" );
187
188 /** "imgfetch" family command descriptor */
189 struct imgsingle_descriptor imgfetch_desc = {
190         .cmd = &imgfetch_cmd,
191         .acquire = imgdownload_string,
192 };
193
194 /**
195  * The "imgfetch" command
196  *
197  * @v argc              Argument count
198  * @v argv              Argument list
199  * @ret rc              Return status code
200  */
201 static int imgfetch_exec ( int argc, char **argv ) {
202         return imgsingle_exec ( argc, argv, &imgfetch_desc );
203 }
204
205 /**
206  * "imgselect" command action
207  *
208  * @v image             Image
209  * @v opts              Options
210  * @ret rc              Return status code
211  */
212 static int imgselect ( struct image *image,
213                        struct imgsingle_options *opts __unused ) {
214         return image_select ( image );
215 }
216
217 /** "imgselect" command descriptor */
218 static struct command_descriptor imgselect_cmd =
219         COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
220                        1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
221
222 /** "imgselect" family command descriptor */
223 struct imgsingle_descriptor imgselect_desc = {
224         .cmd = &imgselect_cmd,
225         .acquire = imgacquire,
226         .action = imgselect,
227         .verb = "select",
228 };
229
230 /**
231  * The "imgselect" command
232  *
233  * @v argc              Argument count
234  * @v argv              Argument list
235  * @ret rc              Return status code
236  */
237 static int imgselect_exec ( int argc, char **argv ) {
238         return imgsingle_exec ( argc, argv, &imgselect_desc );
239 }
240
241 /** "imgexec" command descriptor */
242 static struct command_descriptor imgexec_cmd =
243         COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
244                        0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" );
245
246 /**
247  * "imgexec" command action
248  *
249  * @v image             Image
250  * @v opts              Options
251  * @ret rc              Return status code
252  */
253 static int imgexec ( struct image *image, struct imgsingle_options *opts ) {
254         int rc;
255
256         /* Perform replacement or execution as applicable */
257         if ( opts->replace ) {
258
259                 /* Try to replace image */
260                 if ( ( rc = image_replace ( image ) ) != 0 )
261                         return rc;
262
263                 /* Stop script and tail-recurse into replacement image */
264                 shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );
265
266         } else {
267
268                 /* Try to execute image */
269                 if ( ( rc = image_exec ( image ) ) != 0 )
270                         return rc;
271         }
272
273         return 0;
274 }
275
276 /** "imgexec" family command descriptor */
277 struct imgsingle_descriptor imgexec_desc = {
278         .cmd = &imgexec_cmd,
279         .acquire = imgacquire,
280         .action = imgexec,
281         .verb = "boot",
282 };
283
284 /**
285  * The "imgexec" command
286  *
287  * @v argc              Argument count
288  * @v argv              Argument list
289  * @ret rc              Return status code
290  */
291 static int imgexec_exec ( int argc, char **argv) {
292         return imgsingle_exec ( argc, argv, &imgexec_desc );
293 }
294
295 /** "imgargs" command descriptor */
296 static struct command_descriptor imgargs_cmd =
297         COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
298                        1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
299
300 /** "imgargs" family command descriptor */
301 struct imgsingle_descriptor imgargs_desc = {
302         .cmd = &imgargs_cmd,
303         .acquire = imgacquire,
304         .preaction = image_clear_cmdline,
305 };
306
307 /**
308  * The "imgargs" command body
309  *
310  * @v argc              Argument count
311  * @v argv              Argument list
312  * @ret rc              Return status code
313  */
314 static int imgargs_exec ( int argc, char **argv ) {
315         return imgsingle_exec ( argc, argv, &imgargs_desc );
316 }
317
318 /** "img{multi}" options */
319 struct imgmulti_options {};
320
321 /** "img{multi}" option list */
322 static struct option_descriptor imgmulti_opts[] = {};
323
324 /** "img{multi}" command descriptor */
325 static struct command_descriptor imgmulti_cmd =
326         COMMAND_DESC ( struct imgmulti_options, imgmulti_opts, 0, MAX_ARGUMENTS,
327                        "[<image>...]" );
328
329 /**
330  * The "img{multi}" family of commands
331  *
332  * @v argc              Argument count
333  * @v argv              Argument list
334  * @v payload           Function to execute on each image
335  * @ret rc              Return status code
336  */
337 static int imgmulti_exec ( int argc, char **argv,
338                            void ( * payload ) ( struct image *image ) ) {
339         struct imgmulti_options opts;
340         struct image *image;
341         struct image *tmp;
342         int i;
343         int rc;
344
345         /* Parse options */
346         if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
347                 return rc;
348
349         /* If no images are explicitly specified, process all images */
350         if ( optind == argc ) {
351                 for_each_image_safe ( image, tmp )
352                         payload ( image );
353                 return 0;
354         }
355
356         /* Otherwise, process specified images */
357         for ( i = optind ; i < argc ; i++ ) {
358                 image = find_image ( argv[i] );
359                 if ( ! image ) {
360                         printf ( "\"%s\": no such image\n", argv[i] );
361                         return -ENOENT;
362                 }
363                 payload ( image );
364         }
365
366         return 0;
367 }
368
369 /**
370  * The "imgstat" command
371  *
372  * @v argc              Argument count
373  * @v argv              Argument list
374  * @ret rc              Return status code
375  */
376 static int imgstat_exec ( int argc, char **argv ) {
377         return imgmulti_exec ( argc, argv, imgstat );
378 }
379
380 /**
381  * The "imgfree" command
382  *
383  * @v argc              Argument count
384  * @v argv              Argument list
385  * @ret rc              Return status code
386  */
387 static int imgfree_exec ( int argc, char **argv ) {
388         return imgmulti_exec ( argc, argv, unregister_image );
389 }
390
391 /** Image management commands */
392 struct command image_commands[] __command = {
393         {
394                 .name = "imgfetch",
395                 .exec = imgfetch_exec,
396         },
397         {
398                 .name = "module",
399                 .exec = imgfetch_exec, /* synonym for "imgfetch" */
400         },
401         {
402                 .name = "initrd",
403                 .exec = imgfetch_exec, /* synonym for "imgfetch" */
404         },
405         {
406                 .name = "kernel",
407                 .exec = imgselect_exec, /* synonym for "imgselect" */
408         },
409         {
410                 .name = "chain",
411                 .exec = imgexec_exec, /* synonym for "imgexec" */
412         },
413         {
414                 .name = "imgselect",
415                 .exec = imgselect_exec,
416         },
417         {
418                 .name = "imgload",
419                 .exec = imgselect_exec, /* synonym for "imgselect" */
420         },
421         {
422                 .name = "imgargs",
423                 .exec = imgargs_exec,
424         },
425         {
426                 .name = "imgexec",
427                 .exec = imgexec_exec,
428         },
429         {
430                 .name = "boot", /* synonym for "imgexec" */
431                 .exec = imgexec_exec,
432         },
433         {
434                 .name = "imgstat",
435                 .exec = imgstat_exec,
436         },
437         {
438                 .name = "imgfree",
439                 .exec = imgfree_exec,
440         },
441 };