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.
24 #include <gpxe/image.h>
25 #include <gpxe/command.h>
26 #include <usr/imgmgmt.h>
30 * Image management commands
35 * Print image description
40 * Fill in image command line
43 * @v nargs Argument count
44 * @v args Argument list
46 void imgfill_cmdline ( struct image *image, unsigned int nargs, char **args ) {
49 image->cmdline[0] = '\0';
50 while ( ( used < sizeof ( image->cmdline ) ) && nargs-- ) {
51 used += snprintf ( &image->cmdline[used],
52 ( sizeof ( image->cmdline ) - used ),
53 "%s%s", ( used ? " " : "" ), *(args++) );
58 * "imgfetch"/"module"/"kernel" command syntax message
60 * @v argv Argument list
62 static void imgfetch_core_syntax ( char **argv, int load ) {
64 " %s [-n|--name <name>] filename [arguments...]\n"
66 "%s executable/loadable image\n",
67 argv[0], ( load ? "Fetch and load" : "Fetch" ) );
71 * The "imgfetch"/"module"/"kernel" command body
73 * @v argc Argument count
74 * @v argv Argument list
75 * @v load Load image after fetching
78 static int imgfetch_core_exec ( int argc, char **argv, int load ) {
79 static struct option longopts[] = {
80 { "help", 0, NULL, 'h' },
81 { "name", required_argument, NULL, 'n' },
85 const char *name = NULL;
91 while ( ( c = getopt_long ( argc, argv, "hn:",
92 longopts, NULL ) ) >= 0 ) {
99 /* Display help text */
101 /* Unrecognised/invalid option */
102 imgfetch_core_syntax ( argv, load );
107 /* Need at least a filename remaining after the options */
108 if ( optind == argc ) {
109 imgfetch_core_syntax ( argv, load );
112 filename = argv[optind++];
114 name = basename ( filename );
116 /* Fetch the image */
117 if ( ( rc = imgfetch ( filename, name, &image ) ) != 0 ) {
118 printf ( "Could not fetch %s: %s\n", name, strerror ( rc ) );
122 /* Fill in command line */
123 imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
125 /* Load image if required */
127 if ( ( rc = imgload ( image ) ) != 0 ) {
128 printf ( "Could not load %s: %s\n", name,
138 * The "imgfetch"/"module" command
140 * @v argc Argument count
141 * @v argv Argument list
144 static int imgfetch_exec ( int argc, char **argv ) {
145 return imgfetch_core_exec ( argc, argv, 0 );
149 * The "kernel" command
151 * @v argc Argument count
152 * @v argv Argument list
155 static int kernel_exec ( int argc, char **argv ) {
156 return imgfetch_core_exec ( argc, argv, 1 );
160 * "imgload" command syntax message
162 * @v argv Argument list
164 static void imgload_syntax ( char **argv ) {
168 "Load executable/loadable image\n",
173 * The "imgload" command
175 * @v argc Argument count
176 * @v argv Argument list
179 static int imgload_exec ( int argc, char **argv ) {
180 static struct option longopts[] = {
181 { "help", 0, NULL, 'h' },
182 { NULL, 0, NULL, 0 },
190 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
193 /* Display help text */
195 /* Unrecognised/invalid option */
196 imgload_syntax ( argv );
201 /* Need exactly one image name remaining after the options */
202 if ( optind != ( argc - 1 ) ) {
203 imgload_syntax ( argv );
208 /* Load all specified images */
209 image = find_image ( name );
211 printf ( "No such image: %s\n", name );
214 if ( ( rc = imgload ( image ) ) != 0 ) {
215 printf ( "Could not load %s: %s\n", name, strerror ( rc ) );
223 * "imgargs" command syntax message
225 * @v argv Argument list
227 static void imgargs_syntax ( char **argv ) {
229 " %s <image name> [<arguments>...]\n"
231 "Set arguments for executable/loadable image\n",
236 * The "imgargs" command body
238 * @v argc Argument count
239 * @v argv Argument list
242 static int imgargs_exec ( int argc, char **argv ) {
243 static struct option longopts[] = {
244 { "help", 0, NULL, 'h' },
245 { NULL, 0, NULL, 0 },
252 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
255 /* Display help text */
257 /* Unrecognised/invalid option */
258 imgargs_syntax ( argv );
263 /* Need at least an image name remaining after the options */
264 if ( optind == argc ) {
265 imgargs_syntax ( argv );
268 name = argv[optind++];
270 /* Fill in command line */
271 image = find_image ( name );
273 printf ( "No such image: %s\n", name );
276 imgfill_cmdline ( image, ( argc - optind ), &argv[optind] );
282 * "imgexec" command syntax message
284 * @v argv Argument list
286 static void imgexec_syntax ( char **argv ) {
290 "Execute executable/loadable image\n",
295 * The "imgexec" command
297 * @v argc Argument count
298 * @v argv Argument list
301 static int imgexec_exec ( int argc, char **argv ) {
302 static struct option longopts[] = {
303 { "help", 0, NULL, 'h' },
304 { NULL, 0, NULL, 0 },
307 const char *name = NULL;
312 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
315 /* Display help text */
317 /* Unrecognised/invalid option */
318 imgexec_syntax ( argv );
323 /* Need no more than one image name */
324 if ( optind != argc )
325 name = argv[optind++];
326 if ( optind != argc ) {
327 imgexec_syntax ( argv );
331 /* Execute specified image */
333 image = find_image ( name );
335 printf ( "No such image: %s\n", name );
339 image = imgautoselect();
341 printf ( "No loaded images\n" );
346 if ( ( rc = imgexec ( image ) ) != 0 ) {
347 printf ( "Could not execute %s: %s\n",
348 image->name, strerror ( rc ) );
356 * "imgstat" command syntax message
358 * @v argv Argument list
360 static void imgstat_syntax ( char **argv ) {
364 "List executable/loadable images\n",
369 * The "imgstat" command
371 * @v argc Argument count
372 * @v argv Argument list
375 static int imgstat_exec ( int argc, char **argv ) {
376 static struct option longopts[] = {
377 { "help", 0, NULL, 'h' },
378 { NULL, 0, NULL, 0 },
384 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
387 /* Display help text */
389 /* Unrecognised/invalid option */
390 imgstat_syntax ( argv );
396 if ( optind != argc ) {
397 imgstat_syntax ( argv );
401 /* Show status of all images */
402 for_each_image ( image ) {
409 * "imgstat" command syntax message
411 * @v argv Argument list
413 static void imgfree_syntax ( char **argv ) {
417 "Free all executable/loadable images\n",
422 * The "imgfree" command
424 * @v argc Argument count
425 * @v argv Argument list
428 static int imgfree_exec ( int argc, char **argv ) {
429 static struct option longopts[] = {
430 { "help", 0, NULL, 'h' },
431 { NULL, 0, NULL, 0 },
438 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
441 /* Display help text */
443 /* Unrecognised/invalid option */
444 imgfree_syntax ( argv );
450 if ( optind != argc ) {
451 imgfree_syntax ( argv );
455 /* Free all images */
456 list_for_each_entry_safe ( image, tmp, &images, list ) {
462 /** Image management commands */
463 struct command image_commands[] __command = {
466 .exec = imgfetch_exec,
470 .exec = imgfetch_exec, /* synonym for "imgfetch" */
478 .exec = imgload_exec,
482 .exec = imgargs_exec,
486 .exec = imgexec_exec,
490 .exec = imgstat_exec,
494 .exec = imgfree_exec,