2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
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>
35 * Image management commands
39 /** "img{single}" options */
40 struct imgsingle_options {
43 /** Download timeout */
44 unsigned long timeout;
47 /** Free image after execution */
51 /** "img{single}" option list */
53 /* "imgexec" takes all three options */
54 struct option_descriptor imgexec[4];
55 /* Other "img{single}" commands take only --name, --timeout,
58 struct option_descriptor imgsingle[3];
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 ),
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 */
89 * The "img{single}" family of commands
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
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;
107 if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
108 goto err_parse_options;
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 ] );
117 goto err_parse_cmdline;
122 /* Acquire the image */
124 if ( ( rc = desc->acquire ( name_uri, opts.timeout,
128 image = image_find_selected();
130 printf ( "No image selected\n" );
135 /* Carry out command pre-action, if applicable */
136 if ( desc->preaction )
137 desc->preaction ( image );
139 /* Set the image name, if applicable */
141 if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
142 printf ( "Could not name image: %s\n",
148 /* Set the command-line arguments, if applicable */
150 if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
151 printf ( "Could not set arguments: %s\n",
153 goto err_set_cmdline;
157 /* Set the auto-unregister flag, if applicable */
159 image->flags |= IMAGE_AUTO_UNREGISTER;
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 ) );
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>...]" );
188 /** "imgfetch" family command descriptor */
189 struct imgsingle_descriptor imgfetch_desc = {
190 .cmd = &imgfetch_cmd,
191 .acquire = imgdownload_string,
195 * The "imgfetch" command
197 * @v argc Argument count
198 * @v argv Argument list
199 * @ret rc Return status code
201 static int imgfetch_exec ( int argc, char **argv ) {
202 return imgsingle_exec ( argc, argv, &imgfetch_desc );
206 * "imgselect" command action
210 * @ret rc Return status code
212 static int imgselect ( struct image *image,
213 struct imgsingle_options *opts __unused ) {
214 return image_select ( image );
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>...]" );
222 /** "imgselect" family command descriptor */
223 struct imgsingle_descriptor imgselect_desc = {
224 .cmd = &imgselect_cmd,
225 .acquire = imgacquire,
231 * The "imgselect" command
233 * @v argc Argument count
234 * @v argv Argument list
235 * @ret rc Return status code
237 static int imgselect_exec ( int argc, char **argv ) {
238 return imgsingle_exec ( argc, argv, &imgselect_desc );
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>...]]" );
247 * "imgexec" command action
251 * @ret rc Return status code
253 static int imgexec ( struct image *image, struct imgsingle_options *opts ) {
256 /* Perform replacement or execution as applicable */
257 if ( opts->replace ) {
259 /* Try to replace image */
260 if ( ( rc = image_replace ( image ) ) != 0 )
263 /* Stop script and tail-recurse into replacement image */
264 shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );
268 /* Try to execute image */
269 if ( ( rc = image_exec ( image ) ) != 0 )
276 /** "imgexec" family command descriptor */
277 struct imgsingle_descriptor imgexec_desc = {
279 .acquire = imgacquire,
285 * The "imgexec" command
287 * @v argc Argument count
288 * @v argv Argument list
289 * @ret rc Return status code
291 static int imgexec_exec ( int argc, char **argv) {
292 return imgsingle_exec ( argc, argv, &imgexec_desc );
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>...]" );
300 /** "imgargs" family command descriptor */
301 struct imgsingle_descriptor imgargs_desc = {
303 .acquire = imgacquire,
304 .preaction = image_clear_cmdline,
308 * The "imgargs" command body
310 * @v argc Argument count
311 * @v argv Argument list
312 * @ret rc Return status code
314 static int imgargs_exec ( int argc, char **argv ) {
315 return imgsingle_exec ( argc, argv, &imgargs_desc );
318 /** "img{multi}" options */
319 struct imgmulti_options {};
321 /** "img{multi}" option list */
322 static struct option_descriptor imgmulti_opts[] = {};
324 /** "img{multi}" command descriptor */
325 static struct command_descriptor imgmulti_cmd =
326 COMMAND_DESC ( struct imgmulti_options, imgmulti_opts, 0, MAX_ARGUMENTS,
330 * The "img{multi}" family of commands
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
337 static int imgmulti_exec ( int argc, char **argv,
338 void ( * payload ) ( struct image *image ) ) {
339 struct imgmulti_options opts;
346 if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
349 /* If no images are explicitly specified, process all images */
350 if ( optind == argc ) {
351 for_each_image_safe ( image, tmp )
356 /* Otherwise, process specified images */
357 for ( i = optind ; i < argc ; i++ ) {
358 image = find_image ( argv[i] );
360 printf ( "\"%s\": no such image\n", argv[i] );
370 * The "imgstat" command
372 * @v argc Argument count
373 * @v argv Argument list
374 * @ret rc Return status code
376 static int imgstat_exec ( int argc, char **argv ) {
377 return imgmulti_exec ( argc, argv, imgstat );
381 * The "imgfree" command
383 * @v argc Argument count
384 * @v argv Argument list
385 * @ret rc Return status code
387 static int imgfree_exec ( int argc, char **argv ) {
388 return imgmulti_exec ( argc, argv, unregister_image );
391 /** Image management commands */
392 struct command image_commands[] __command = {
395 .exec = imgfetch_exec,
399 .exec = imgfetch_exec, /* synonym for "imgfetch" */
403 .exec = imgfetch_exec, /* synonym for "imgfetch" */
407 .exec = imgselect_exec, /* synonym for "imgselect" */
411 .exec = imgexec_exec, /* synonym for "imgexec" */
415 .exec = imgselect_exec,
419 .exec = imgselect_exec, /* synonym for "imgselect" */
423 .exec = imgargs_exec,
427 .exec = imgexec_exec,
430 .name = "boot", /* synonym for "imgexec" */
431 .exec = imgexec_exec,
435 .exec = imgstat_exec,
439 .exec = imgfree_exec,