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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/image.h>
26 #include <gpxe/command.h>
27 #include <gpxe/initrd.h>
28 #include <usr/imgmgmt.h>
32 * Image management commands
37 * Print image description
42 * Fill in image command line
45 * @v nargs Argument count
46 * @v args Argument list
48 void imgfill_cmdline ( struct image *image, unsigned int nargs, char **args ) {
51 image->cmdline[0] = '\0';
52 while ( ( used < sizeof ( image->cmdline ) ) && nargs-- ) {
53 used += snprintf ( &image->cmdline[used],
54 ( sizeof ( image->cmdline ) - used ),
55 "%s%s", ( used ? " " : "" ), *(args++) );
60 * "imgfetch"/"module"/"kernel" command syntax message
62 * @v argv Argument list
64 static void imgfetch_core_syntax ( char **argv, int load ) {
66 " %s [-n|--name <name>] filename [arguments...]\n"
68 "%s executable/loadable image\n",
69 argv[0], ( load ? "Fetch and load" : "Fetch" ) );
73 * The "imgfetch"/"module"/"kernel" command body
75 * @v argc Argument count
76 * @v argv Argument list
77 * @v load Image will be automatically loaded after fetching
78 * @ret image Fetched image
79 * @ret rc Return status code
81 static int imgfetch_core_exec ( int argc, char **argv, int load,
82 struct image **image ) {
83 static struct option longopts[] = {
84 { "help", 0, NULL, 'h' },
85 { "name", required_argument, NULL, 'n' },
88 const char *name = NULL;
94 while ( ( c = getopt_long ( argc, argv, "hn:",
95 longopts, NULL ) ) >= 0 ) {
102 /* Display help text */
104 /* Unrecognised/invalid option */
105 imgfetch_core_syntax ( argv, load );
110 /* Need at least a filename remaining after the options */
111 if ( optind == argc ) {
112 imgfetch_core_syntax ( argv, load );
115 filename = argv[optind++];
117 name = basename ( filename );
119 /* Fetch the image */
120 if ( ( rc = imgfetch ( filename, name, image ) ) != 0 ) {
121 printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
125 /* Fill in command line */
126 imgfill_cmdline ( *image, ( argc - optind ), &argv[optind] );
132 * The "imgfetch"/"module" command
134 * @v argc Argument count
135 * @v argv Argument list
138 static int imgfetch_exec ( int argc, char **argv ) {
142 if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) ) != 0 )
149 * The "kernel" command
151 * @v argc Argument count
152 * @v argv Argument list
155 static int kernel_exec ( int argc, char **argv ) {
159 if ( ( rc = imgfetch_core_exec ( argc, argv, 1, &image ) != 0 ) )
163 if ( ( rc = imgload ( image ) ) != 0 ) {
164 printf ( "Could not load %s: %s\n", image->name,
173 * The "initrd" command
175 * @v argc Argument count
176 * @v argv Argument list
179 static int initrd_exec ( int argc, char **argv ) {
183 if ( ( rc = imgfetch_core_exec ( argc, argv, 0, &image ) != 0 ) )
186 /* Mark image as an intird */
187 image->type = &initrd_image_type;
193 * "imgload" command syntax message
195 * @v argv Argument list
197 static void imgload_syntax ( char **argv ) {
201 "Load executable/loadable image\n",
206 * The "imgload" command
208 * @v argc Argument count
209 * @v argv Argument list
212 static int imgload_exec ( int argc, char **argv ) {
213 static struct option longopts[] = {
214 { "help", 0, NULL, 'h' },
215 { NULL, 0, NULL, 0 },
223 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
226 /* Display help text */
228 /* Unrecognised/invalid option */
229 imgload_syntax ( argv );
234 /* Need exactly one image name remaining after the options */
235 if ( optind != ( argc - 1 ) ) {
236 imgload_syntax ( argv );
241 /* Load all specified images */
242 image = find_image ( name );
244 printf ( "No such image: %s\n", name );
247 if ( ( rc = imgload ( image ) ) != 0 ) {
248 printf ( "Could not load %s: %s\n", name, strerror ( rc ) );
256 * "imgargs" command syntax message
258 * @v argv Argument list
260 static void imgargs_syntax ( char **argv ) {
262 " %s <image name> [<arguments>...]\n"
264 "Set arguments for executable/loadable image\n",
269 * The "imgargs" command body
271 * @v argc Argument count
272 * @v argv Argument list
275 static int imgargs_exec ( int argc, char **argv ) {
276 static struct option longopts[] = {
277 { "help", 0, NULL, 'h' },
278 { NULL, 0, NULL, 0 },
285 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
288 /* Display help text */
290 /* Unrecognised/invalid option */
291 imgargs_syntax ( argv );
296 /* Need at least an image name remaining after the options */
297 if ( optind == argc ) {
298 imgargs_syntax ( argv );
301 name = argv[optind++];
303 /* Fill in command line */
304 image = find_image ( name );
306 printf ( "No such image: %s\n", name );
309 imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
315 * "imgexec" command syntax message
317 * @v argv Argument list
319 static void imgexec_syntax ( char **argv ) {
323 "Execute executable/loadable image\n",
328 * The "imgexec" command
330 * @v argc Argument count
331 * @v argv Argument list
334 static int imgexec_exec ( int argc, char **argv ) {
335 static struct option longopts[] = {
336 { "help", 0, NULL, 'h' },
337 { NULL, 0, NULL, 0 },
340 const char *name = NULL;
345 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
348 /* Display help text */
350 /* Unrecognised/invalid option */
351 imgexec_syntax ( argv );
356 /* Need no more than one image name */
357 if ( optind != argc )
358 name = argv[optind++];
359 if ( optind != argc ) {
360 imgexec_syntax ( argv );
364 /* Execute specified image */
366 image = find_image ( name );
368 printf ( "No such image: %s\n", name );
372 image = imgautoselect();
374 printf ( "No loaded images\n" );
379 if ( ( rc = imgexec ( image ) ) != 0 ) {
380 printf ( "Could not execute %s: %s\n",
381 image->name, strerror ( rc ) );
389 * "imgstat" command syntax message
391 * @v argv Argument list
393 static void imgstat_syntax ( char **argv ) {
397 "List executable/loadable images\n",
402 * The "imgstat" command
404 * @v argc Argument count
405 * @v argv Argument list
408 static int imgstat_exec ( int argc, char **argv ) {
409 static struct option longopts[] = {
410 { "help", 0, NULL, 'h' },
411 { NULL, 0, NULL, 0 },
417 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
420 /* Display help text */
422 /* Unrecognised/invalid option */
423 imgstat_syntax ( argv );
429 if ( optind != argc ) {
430 imgstat_syntax ( argv );
434 /* Show status of all images */
435 for_each_image ( image ) {
442 * "imgstat" command syntax message
444 * @v argv Argument list
446 static void imgfree_syntax ( char **argv ) {
450 "Free all executable/loadable images\n",
455 * The "imgfree" command
457 * @v argc Argument count
458 * @v argv Argument list
461 static int imgfree_exec ( int argc, char **argv ) {
462 static struct option longopts[] = {
463 { "help", 0, NULL, 'h' },
464 { NULL, 0, NULL, 0 },
471 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
474 /* Display help text */
476 /* Unrecognised/invalid option */
477 imgfree_syntax ( argv );
483 if ( optind != argc ) {
484 imgfree_syntax ( argv );
488 /* Free all images */
489 list_for_each_entry_safe ( image, tmp, &images, list ) {
495 /** Image management commands */
496 struct command image_commands[] __command = {
499 .exec = imgfetch_exec,
503 .exec = imgfetch_exec, /* synonym for "imgfetch" */
515 .exec = imgload_exec,
519 .exec = imgargs_exec,
523 .exec = imgexec_exec,
526 .name = "boot", /* synonym for "imgexec" */
527 .exec = imgexec_exec,
531 .exec = imgstat_exec,
535 .exec = imgfree_exec,