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
43 * Fill in image command line
46 * @v nargs Argument count
47 * @v args Argument list
48 * @ret rc Return status code
50 static int imgfill_cmdline ( struct image
*image
, unsigned int nargs
,
55 memset ( buf
, 0, sizeof ( buf
) );
56 while ( ( used
< sizeof ( buf
) ) && nargs
-- ) {
57 used
+= snprintf ( &buf
[used
], ( sizeof ( buf
) - used
),
61 return image_set_cmdline ( image
, &buf
[1] );
65 * "imgfetch"/"module"/"kernel" command syntax message
67 * @v argv Argument list
69 static void imgfetch_core_syntax ( char **argv
, enum image_action action
) {
70 static const char *actions
[] = {
71 [IMG_FETCH
] = "Fetch",
72 [IMG_LOAD
] = "Fetch and load",
73 [IMG_EXEC
] = "Fetch and execute",
77 " %s [-n|--name <name>] filename [arguments...]\n"
79 "%s executable/loadable image\n",
80 argv
[0], actions
[action
] );
84 * The "imgfetch"/"module"/"kernel" command body
86 * @v image_type Image type to assign (or NULL)
87 * @v load Image will be automatically loaded after fetching
88 * @v argc Argument count
89 * @v argv Argument list
90 * @ret rc Return status code
92 static int imgfetch_core_exec ( struct image_type
*image_type
,
93 enum image_action action
,
94 int argc
, char **argv
) {
95 static struct option longopts
[] = {
96 { "help", 0, NULL
, 'h' },
97 { "name", required_argument
, NULL
, 'n' },
101 const char *name
= NULL
;
103 int ( * image_register
) ( struct image
*image
);
108 while ( ( c
= getopt_long ( argc
, argv
, "hn:",
109 longopts
, NULL
) ) >= 0 ) {
116 /* Display help text */
118 /* Unrecognised/invalid option */
119 imgfetch_core_syntax ( argv
, action
);
124 /* Need at least a filename remaining after the options */
125 if ( optind
== argc
) {
126 imgfetch_core_syntax ( argv
, action
);
129 filename
= argv
[optind
++];
131 name
= basename ( filename
);
134 image
= alloc_image();
136 printf ( "%s\n", strerror ( -ENOMEM
) );
140 /* Fill in image name */
142 if ( ( rc
= image_set_name ( image
, name
) ) != 0 )
146 /* Set image type (if specified) */
147 image
->type
= image_type
;
149 /* Fill in command line */
150 if ( ( rc
= imgfill_cmdline ( image
, ( argc
- optind
),
151 &argv
[optind
] ) ) != 0 )
154 /* Fetch the image */
157 image_register
= register_image
;
160 image_register
= register_and_autoload_image
;
163 image_register
= register_and_autoexec_image
;
169 if ( ( rc
= imgfetch ( image
, filename
, image_register
) ) != 0 ) {
170 printf ( "Could not fetch %s: %s\n",
171 filename
, strerror ( rc
) );
181 * The "imgfetch"/"module" command
183 * @v argc Argument count
184 * @v argv Argument list
187 static int imgfetch_exec ( int argc
, char **argv
) {
190 if ( ( rc
= imgfetch_core_exec ( NULL
, IMG_FETCH
,
191 argc
, argv
) ) != 0 )
198 * The "kernel" command
200 * @v argc Argument count
201 * @v argv Argument list
204 static int kernel_exec ( int argc
, char **argv
) {
207 if ( ( rc
= imgfetch_core_exec ( NULL
, IMG_LOAD
, argc
, argv
) ) != 0 )
214 * The "initrd" command
216 * @v argc Argument count
217 * @v argv Argument list
220 static int initrd_exec ( int argc
, char **argv
) {
223 if ( ( rc
= imgfetch_core_exec ( &initrd_image_type
, IMG_FETCH
,
224 argc
, argv
) ) != 0 )
231 * "imgload" command syntax message
233 * @v argv Argument list
235 static void imgload_syntax ( char **argv
) {
239 "Load executable/loadable image\n",
244 * The "imgload" command
246 * @v argc Argument count
247 * @v argv Argument list
250 static int imgload_exec ( int argc
, char **argv
) {
251 static struct option longopts
[] = {
252 { "help", 0, NULL
, 'h' },
253 { NULL
, 0, NULL
, 0 },
261 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
264 /* Display help text */
266 /* Unrecognised/invalid option */
267 imgload_syntax ( argv
);
272 /* Need exactly one image name remaining after the options */
273 if ( optind
!= ( argc
- 1 ) ) {
274 imgload_syntax ( argv
);
279 /* Load all specified images */
280 image
= find_image ( name
);
282 printf ( "No such image: %s\n", name
);
285 if ( ( rc
= imgload ( image
) ) != 0 ) {
286 printf ( "Could not load %s: %s\n", name
, strerror ( rc
) );
294 * "imgargs" command syntax message
296 * @v argv Argument list
298 static void imgargs_syntax ( char **argv
) {
300 " %s <image name> [<arguments>...]\n"
302 "Set arguments for executable/loadable image\n",
307 * The "imgargs" command body
309 * @v argc Argument count
310 * @v argv Argument list
313 static int imgargs_exec ( int argc
, char **argv
) {
314 static struct option longopts
[] = {
315 { "help", 0, NULL
, 'h' },
316 { NULL
, 0, NULL
, 0 },
324 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
327 /* Display help text */
329 /* Unrecognised/invalid option */
330 imgargs_syntax ( argv
);
335 /* Need at least an image name remaining after the options */
336 if ( optind
== argc
) {
337 imgargs_syntax ( argv
);
340 name
= argv
[optind
++];
342 /* Fill in command line */
343 image
= find_image ( name
);
345 printf ( "No such image: %s\n", name
);
348 if ( ( rc
= imgfill_cmdline ( image
, ( argc
- optind
),
349 &argv
[optind
] ) ) != 0 )
357 * "imgexec" command syntax message
359 * @v argv Argument list
361 static void imgexec_syntax ( char **argv
) {
365 "Execute executable/loadable image\n",
370 * The "imgexec" command
372 * @v argc Argument count
373 * @v argv Argument list
376 static int imgexec_exec ( int argc
, char **argv
) {
377 static struct option longopts
[] = {
378 { "help", 0, NULL
, 'h' },
379 { NULL
, 0, NULL
, 0 },
382 const char *name
= NULL
;
387 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
390 /* Display help text */
392 /* Unrecognised/invalid option */
393 imgexec_syntax ( argv
);
398 /* Need no more than one image name */
399 if ( optind
!= argc
)
400 name
= argv
[optind
++];
401 if ( optind
!= argc
) {
402 imgexec_syntax ( argv
);
406 /* Execute specified image */
408 image
= find_image ( name
);
410 printf ( "No such image: %s\n", name
);
414 image
= imgautoselect();
416 printf ( "No loaded images\n" );
421 if ( ( rc
= imgexec ( image
) ) != 0 ) {
422 printf ( "Could not execute %s: %s\n",
423 image
->name
, strerror ( rc
) );
431 * "imgstat" command syntax message
433 * @v argv Argument list
435 static void imgstat_syntax ( char **argv
) {
439 "List executable/loadable images\n",
444 * The "imgstat" command
446 * @v argc Argument count
447 * @v argv Argument list
450 static int imgstat_exec ( int argc
, char **argv
) {
451 static struct option longopts
[] = {
452 { "help", 0, NULL
, 'h' },
453 { NULL
, 0, NULL
, 0 },
459 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
462 /* Display help text */
464 /* Unrecognised/invalid option */
465 imgstat_syntax ( argv
);
471 if ( optind
!= argc
) {
472 imgstat_syntax ( argv
);
476 /* Show status of all images */
477 for_each_image ( image
) {
484 * "imgstat" command syntax message
486 * @v argv Argument list
488 static void imgfree_syntax ( char **argv
) {
492 "Free all executable/loadable images\n",
497 * The "imgfree" command
499 * @v argc Argument count
500 * @v argv Argument list
503 static int imgfree_exec ( int argc
, char **argv
) {
504 static struct option longopts
[] = {
505 { "help", 0, NULL
, 'h' },
506 { NULL
, 0, NULL
, 0 },
513 while ( ( c
= getopt_long ( argc
, argv
, "h", longopts
, NULL
) ) >= 0 ){
516 /* Display help text */
518 /* Unrecognised/invalid option */
519 imgfree_syntax ( argv
);
525 if ( optind
!= argc
) {
526 imgfree_syntax ( argv
);
530 /* Free all images */
531 list_for_each_entry_safe ( image
, tmp
, &images
, list
) {
537 /** Image management commands */
538 struct command image_commands
[] __command
= {
541 .exec
= imgfetch_exec
,
545 .exec
= imgfetch_exec
, /* synonym for "imgfetch" */
557 .exec
= imgload_exec
,
561 .exec
= imgargs_exec
,
565 .exec
= imgexec_exec
,
568 .name
= "boot", /* synonym for "imgexec" */
569 .exec
= imgexec_exec
,
573 .exec
= imgstat_exec
,
577 .exec
= imgfree_exec
,