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 },
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 exactly one image name */
324 if ( optind != ( argc - 1 ) ) {
325 imgexec_syntax ( argv );
330 /* Execute specified image */
331 image = find_image ( name );
333 printf ( "No such image: %s\n", name );
336 if ( ( rc = imgexec ( image ) ) != 0 ) {
337 printf ( "Could not execute %s: %s\n", name, strerror ( rc ) );
345 * "imgstat" command syntax message
347 * @v argv Argument list
349 static void imgstat_syntax ( char **argv ) {
353 "List executable/loadable images\n",
358 * The "imgstat" command
360 * @v argc Argument count
361 * @v argv Argument list
364 static int imgstat_exec ( int argc, char **argv ) {
365 static struct option longopts[] = {
366 { "help", 0, NULL, 'h' },
367 { NULL, 0, NULL, 0 },
373 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
376 /* Display help text */
378 /* Unrecognised/invalid option */
379 imgstat_syntax ( argv );
385 if ( optind != argc ) {
386 imgstat_syntax ( argv );
390 /* Show status of all images */
391 for_each_image ( image ) {
398 * "imgstat" command syntax message
400 * @v argv Argument list
402 static void imgfree_syntax ( char **argv ) {
406 "Free all executable/loadable images\n",
411 * The "imgfree" command
413 * @v argc Argument count
414 * @v argv Argument list
417 static int imgfree_exec ( int argc, char **argv ) {
418 static struct option longopts[] = {
419 { "help", 0, NULL, 'h' },
420 { NULL, 0, NULL, 0 },
427 while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
430 /* Display help text */
432 /* Unrecognised/invalid option */
433 imgfree_syntax ( argv );
439 if ( optind != argc ) {
440 imgfree_syntax ( argv );
444 /* Free all images */
445 list_for_each_entry_safe ( image, tmp, &images, list ) {
452 /** Image management commands */
453 struct command image_commands[] __command = {
456 .exec = imgfetch_exec,
460 .exec = imgfetch_exec, /* synonym for "imgfetch" */
468 .exec = imgload_exec,
472 .exec = imgargs_exec,
476 .exec = imgexec_exec,
480 .exec = imgstat_exec,
484 .exec = imgfree_exec,