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