image_cmd.c File Reference

Image management commands. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <libgen.h>
#include <getopt.h>
#include <gpxe/image.h>
#include <gpxe/command.h>
#include <usr/imgmgmt.h>

Go to the source code of this file.

Enumerations

enum  image_action { IMG_FETCH = 0, IMG_LOAD, IMG_EXEC }

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int imgfill_cmdline (struct image *image, unsigned int nargs, char **args)
 Fill in image command line.
static void imgfetch_core_syntax (char **argv, enum image_action action)
 "imgfetch"/"module"/"kernel" command syntax message
static int imgfetch_core_exec (struct image_type *image_type, enum image_action action, int argc, char **argv)
 The "imgfetch"/"module"/"kernel" command body.
static int imgfetch_exec (int argc, char **argv)
 The "imgfetch"/"module" command.
static int kernel_exec (int argc, char **argv)
 The "kernel" command.
static int chain_exec (int argc, char **argv)
 The "chain" command.
static void imgload_syntax (char **argv)
 "imgload" command syntax message
static int imgload_exec (int argc, char **argv)
 The "imgload" command.
static void imgargs_syntax (char **argv)
 "imgargs" command syntax message
static int imgargs_exec (int argc, char **argv)
 The "imgargs" command body.
static void imgexec_syntax (char **argv)
 "imgexec" command syntax message
static int imgexec_exec (int argc, char **argv)
 The "imgexec" command.
static void imgstat_syntax (char **argv)
 "imgstat" command syntax message
static int imgstat_exec (int argc, char **argv)
 The "imgstat" command.
static void imgfree_syntax (char **argv)
 "imgstat" command syntax message
static int imgfree_exec (int argc, char **argv)
 The "imgfree" command.

Variables

struct command image_commands[] __command
 Image management commands.


Detailed Description

Image management commands.

Definition in file image_cmd.c.


Enumeration Type Documentation

Enumerator:
IMG_FETCH 
IMG_LOAD 
IMG_EXEC 

Definition at line 37 of file image_cmd.c.

00037                   {
00038         IMG_FETCH = 0,
00039         IMG_LOAD,
00040         IMG_EXEC,
00041 };


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

static int imgfill_cmdline ( struct image image,
unsigned int  nargs,
char **  args 
) [static]

Fill in image command line.

Parameters:
image Image
nargs Argument count
args Argument list
Return values:
rc Return status code

Definition at line 51 of file image_cmd.c.

References assert, image_set_cmdline(), sprintf, and strlen().

Referenced by imgargs_exec(), and imgfetch_core_exec().

00052                                            {
00053         size_t len;
00054         unsigned int i;
00055 
00056         /* Determine total length of command line */
00057         len = 1; /* NUL */
00058         for ( i = 0 ; i < nargs ; i++ )
00059                 len += ( 1 /* possible space */ + strlen ( args[i] ) );
00060 
00061         {
00062                 char buf[len];
00063                 char *ptr = buf;
00064 
00065                 /* Assemble command line */
00066                 buf[0] = '\0';
00067                 for ( i = 0 ; i < nargs ; i++ ) {
00068                         ptr += sprintf ( ptr, "%s%s", ( i ? " " : "" ),
00069                                          args[i] );
00070                 }
00071                 assert ( ptr < ( buf + len ) );
00072 
00073                 return image_set_cmdline ( image, buf );
00074         }
00075 }

static void imgfetch_core_syntax ( char **  argv,
enum image_action  action 
) [static]

"imgfetch"/"module"/"kernel" command syntax message

Parameters:
argv Argument list

Definition at line 82 of file image_cmd.c.

References IMG_EXEC, IMG_FETCH, IMG_LOAD, and printf().

Referenced by imgfetch_core_exec().

00082                                                                            {
00083         static const char *actions[] = {
00084                 [IMG_FETCH]     = "Fetch",
00085                 [IMG_LOAD]      = "Fetch and load",
00086                 [IMG_EXEC]      = "Fetch and execute",
00087         };
00088 
00089         printf ( "Usage:\n"
00090                  "  %s [-n|--name <name>] filename [arguments...]\n"
00091                  "\n"
00092                  "%s executable/loadable image\n",
00093                  argv[0], actions[action] );
00094 }

static int imgfetch_core_exec ( struct image_type image_type,
enum image_action  action,
int  argc,
char **  argv 
) [static]

The "imgfetch"/"module"/"kernel" command body.

Parameters:
image_type Image type to assign (or NULL)
load Image will be automatically loaded after fetching
argc Argument count
argv Argument list
Return values:
rc Return status code

Definition at line 105 of file image_cmd.c.

References alloc_image(), assert, basename(), EINVAL, ENOMEM, getopt_long(), image_put(), image_set_name(), IMG_EXEC, IMG_FETCH, IMG_LOAD, imgfetch(), imgfetch_core_syntax(), imgfill_cmdline(), name, NULL, optarg, optind, printf(), register_and_autoexec_image(), register_and_autoload_image(), register_image(), required_argument, strerror(), and image::type.

Referenced by chain_exec(), imgfetch_exec(), and kernel_exec().

00107                                                         {
00108         static struct option longopts[] = {
00109                 { "help", 0, NULL, 'h' },
00110                 { "name", required_argument, NULL, 'n' },
00111                 { NULL, 0, NULL, 0 },
00112         };
00113         struct image *image;
00114         const char *name = NULL;
00115         char *filename;
00116         int ( * image_register ) ( struct image *image );
00117         int c;
00118         int rc;
00119 
00120         /* Parse options */
00121         while ( ( c = getopt_long ( argc, argv, "hn:",
00122                                     longopts, NULL ) ) >= 0 ) {
00123                 switch ( c ) {
00124                 case 'n':
00125                         /* Set image name */
00126                         name = optarg;
00127                         break;
00128                 case 'h':
00129                         /* Display help text */
00130                 default:
00131                         /* Unrecognised/invalid option */
00132                         imgfetch_core_syntax ( argv, action );
00133                         return -EINVAL;
00134                 }
00135         }
00136 
00137         /* Need at least a filename remaining after the options */
00138         if ( optind == argc ) {
00139                 imgfetch_core_syntax ( argv, action );
00140                 return -EINVAL;
00141         }
00142         filename = argv[optind++];
00143         if ( ! name )
00144                 name = basename ( filename );
00145 
00146         /* Allocate image */
00147         image = alloc_image();
00148         if ( ! image ) {
00149                 printf ( "%s\n", strerror ( -ENOMEM ) );
00150                 return -ENOMEM;
00151         }
00152 
00153         /* Fill in image name */
00154         if ( name ) {
00155                 if ( ( rc = image_set_name ( image, name ) ) != 0 )
00156                         return rc;
00157         }
00158 
00159         /* Set image type (if specified) */
00160         image->type = image_type;
00161 
00162         /* Fill in command line */
00163         if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
00164                                       &argv[optind] ) ) != 0 )
00165                 return rc;
00166 
00167         /* Fetch the image */
00168         switch ( action ) {
00169         case IMG_FETCH:
00170                 image_register = register_image;
00171                 break;
00172         case IMG_LOAD:
00173                 image_register = register_and_autoload_image;
00174                 break;
00175         case IMG_EXEC:
00176                 image_register = register_and_autoexec_image;
00177                 break;
00178         default:
00179                 assert ( 0 );
00180                 return -EINVAL;
00181         }
00182         if ( ( rc = imgfetch ( image, filename, image_register ) ) != 0 ) {
00183                 printf ( "Could not fetch %s: %s\n",
00184                          filename, strerror ( rc ) );
00185                 image_put ( image );
00186                 return rc;
00187         }
00188 
00189         image_put ( image );
00190         return 0;
00191 }

static int imgfetch_exec ( int  argc,
char **  argv 
) [static]

The "imgfetch"/"module" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 200 of file image_cmd.c.

References IMG_FETCH, imgfetch_core_exec(), and NULL.

00200                                                    {
00201         int rc;
00202 
00203         if ( ( rc = imgfetch_core_exec ( NULL, IMG_FETCH,
00204                                          argc, argv ) ) != 0 )
00205                 return rc;
00206 
00207         return 0;
00208 }

static int kernel_exec ( int  argc,
char **  argv 
) [static]

The "kernel" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 217 of file image_cmd.c.

References IMG_LOAD, imgfetch_core_exec(), and NULL.

00217                                                  {
00218         int rc;
00219 
00220         if ( ( rc = imgfetch_core_exec ( NULL, IMG_LOAD, argc, argv ) ) != 0 )
00221                 return rc;
00222 
00223         return 0;
00224 }

static int chain_exec ( int  argc,
char **  argv 
) [static]

The "chain" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 233 of file image_cmd.c.

References IMG_EXEC, imgfetch_core_exec(), and NULL.

00233                                                {
00234         int rc;
00235 
00236         if ( ( rc = imgfetch_core_exec ( NULL, IMG_EXEC, argc, argv ) ) != 0 )
00237                 return rc;
00238 
00239         return 0;
00240 }

static void imgload_syntax ( char **  argv  )  [static]

"imgload" command syntax message

Parameters:
argv Argument list

Definition at line 247 of file image_cmd.c.

References printf().

Referenced by imgload_exec().

00247                                            {
00248         printf ( "Usage:\n"
00249                  "  %s <image name>\n"
00250                  "\n"
00251                  "Load executable/loadable image\n",
00252                  argv[0] );
00253 }

static int imgload_exec ( int  argc,
char **  argv 
) [static]

The "imgload" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 262 of file image_cmd.c.

References find_image(), getopt_long(), imgload(), imgload_syntax(), name, NULL, optind, printf(), and strerror().

00262                                                   {
00263         static struct option longopts[] = {
00264                 { "help", 0, NULL, 'h' },
00265                 { NULL, 0, NULL, 0 },
00266         };
00267         struct image *image;
00268         const char *name;
00269         int c;
00270         int rc;
00271 
00272         /* Parse options */
00273         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00274                 switch ( c ) {
00275                 case 'h':
00276                         /* Display help text */
00277                 default:
00278                         /* Unrecognised/invalid option */
00279                         imgload_syntax ( argv );
00280                         return 1;
00281                 }
00282         }
00283 
00284         /* Need exactly one image name remaining after the options */
00285         if ( optind != ( argc - 1 ) ) {
00286                 imgload_syntax ( argv );
00287                 return 1;
00288         }
00289         name = argv[optind];
00290 
00291         /* Load all specified images */
00292         image = find_image ( name );
00293         if ( ! image ) {
00294                 printf ( "No such image: %s\n", name );
00295                 return 1;
00296         }
00297         if ( ( rc = imgload ( image ) ) != 0 ) {
00298                 printf ( "Could not load %s: %s\n", name, strerror ( rc ) );
00299                 return rc;
00300         }
00301 
00302         return 0;
00303 }

static void imgargs_syntax ( char **  argv  )  [static]

"imgargs" command syntax message

Parameters:
argv Argument list

Definition at line 310 of file image_cmd.c.

References printf().

Referenced by imgargs_exec().

00310                                            {
00311         printf ( "Usage:\n"
00312                  "  %s <image name> [<arguments>...]\n"
00313                  "\n"
00314                  "Set arguments for executable/loadable image\n",
00315                  argv[0] );
00316 }

static int imgargs_exec ( int  argc,
char **  argv 
) [static]

The "imgargs" command body.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 325 of file image_cmd.c.

References find_image(), getopt_long(), imgargs_syntax(), imgfill_cmdline(), name, NULL, optind, and printf().

00325                                                   {
00326         static struct option longopts[] = {
00327                 { "help", 0, NULL, 'h' },
00328                 { NULL, 0, NULL, 0 },
00329         };
00330         struct image *image;
00331         const char *name;
00332         int c;
00333         int rc;
00334 
00335         /* Parse options */
00336         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00337                 switch ( c ) {
00338                 case 'h':
00339                         /* Display help text */
00340                 default:
00341                         /* Unrecognised/invalid option */
00342                         imgargs_syntax ( argv );
00343                         return 1;
00344                 }
00345         }
00346 
00347         /* Need at least an image name remaining after the options */
00348         if ( optind == argc ) {
00349                 imgargs_syntax ( argv );
00350                 return 1;
00351         }
00352         name = argv[optind++];
00353 
00354         /* Fill in command line */
00355         image = find_image ( name );
00356         if ( ! image ) {
00357                 printf ( "No such image: %s\n", name );
00358                 return 1;
00359         }
00360         if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
00361                                       &argv[optind] ) ) != 0 )
00362                 return rc;
00363 
00364 
00365         return 0;
00366 }

static void imgexec_syntax ( char **  argv  )  [static]

"imgexec" command syntax message

Parameters:
argv Argument list

Definition at line 373 of file image_cmd.c.

References printf().

Referenced by imgexec_exec().

00373                                            {
00374         printf ( "Usage:\n"
00375                  "  %s <image name>\n"
00376                  "\n"
00377                  "Execute executable/loadable image\n",
00378                  argv[0] );
00379 }

static int imgexec_exec ( int  argc,
char **  argv 
) [static]

The "imgexec" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 388 of file image_cmd.c.

References find_image(), getopt_long(), imgautoselect(), imgexec(), imgexec_syntax(), image::name, name, NULL, optind, printf(), and strerror().

00388                                                   {
00389         static struct option longopts[] = {
00390                 { "help", 0, NULL, 'h' },
00391                 { NULL, 0, NULL, 0 },
00392         };
00393         struct image *image;
00394         const char *name = NULL;
00395         int c;
00396         int rc;
00397 
00398         /* Parse options */
00399         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00400                 switch ( c ) {
00401                 case 'h':
00402                         /* Display help text */
00403                 default:
00404                         /* Unrecognised/invalid option */
00405                         imgexec_syntax ( argv );
00406                         return 1;
00407                 }
00408         }
00409 
00410         /* Need no more than one image name */
00411         if ( optind != argc )
00412                 name = argv[optind++];
00413         if ( optind != argc ) {
00414                 imgexec_syntax ( argv );
00415                 return 1;
00416         }
00417         
00418         /* Execute specified image */
00419         if ( name ) {
00420                 image = find_image ( name );
00421                 if ( ! image ) {
00422                         printf ( "No such image: %s\n", name );
00423                         return 1;
00424                 }
00425         } else {
00426                 image = imgautoselect();
00427                 if ( ! image ) {
00428                         printf ( "No (unique) loaded image\n" );
00429                         return 1;
00430                 }
00431         }
00432 
00433         if ( ( rc = imgexec ( image ) ) != 0 ) {
00434                 printf ( "Could not execute %s: %s\n",
00435                          image->name, strerror ( rc ) );
00436                 return 1;
00437         }
00438 
00439         return 0;
00440 }

static void imgstat_syntax ( char **  argv  )  [static]

"imgstat" command syntax message

Parameters:
argv Argument list

Definition at line 447 of file image_cmd.c.

References printf().

Referenced by imgstat_exec().

00447                                            {
00448         printf ( "Usage:\n"
00449                  "  %s\n"
00450                  "\n"
00451                  "List executable/loadable images\n",
00452                  argv[0] );
00453 }

static int imgstat_exec ( int  argc,
char **  argv 
) [static]

The "imgstat" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 462 of file image_cmd.c.

References for_each_image, getopt_long(), imgstat(), imgstat_syntax(), NULL, and optind.

00462                                                   {
00463         static struct option longopts[] = {
00464                 { "help", 0, NULL, 'h' },
00465                 { NULL, 0, NULL, 0 },
00466         };
00467         struct image *image;
00468         int c;
00469 
00470         /* Parse options */
00471         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00472                 switch ( c ) {
00473                 case 'h':
00474                         /* Display help text */
00475                 default:
00476                         /* Unrecognised/invalid option */
00477                         imgstat_syntax ( argv );
00478                         return 1;
00479                 }
00480         }
00481 
00482         /* No arguments */
00483         if ( optind != argc ) {
00484                 imgstat_syntax ( argv );
00485                 return 1;
00486         }
00487 
00488         /* Show status of all images */
00489         for_each_image ( image ) {
00490                 imgstat ( image );
00491         }
00492         return 0;
00493 }

static void imgfree_syntax ( char **  argv  )  [static]

"imgstat" command syntax message

Parameters:
argv Argument list

Definition at line 500 of file image_cmd.c.

References printf().

Referenced by imgfree_exec().

00500                                            {
00501         printf ( "Usage:\n"
00502                  "  %s [<image name>]\n"
00503                  "\n"
00504                  "Free one or all executable/loadable images\n",
00505                  argv[0] );
00506 }

static int imgfree_exec ( int  argc,
char **  argv 
) [static]

The "imgfree" command.

Parameters:
argc Argument count
argv Argument list
Return values:
rc Exit code

Definition at line 515 of file image_cmd.c.

References find_image(), getopt_long(), images, imgfree(), imgfree_syntax(), image::list, list_for_each_entry_safe, name, NULL, optind, and printf().

00515                                                   {
00516         static struct option longopts[] = {
00517                 { "help", 0, NULL, 'h' },
00518                 { NULL, 0, NULL, 0 },
00519         };
00520         struct image *image;
00521         struct image *tmp;
00522         const char *name = NULL;
00523         int c;
00524 
00525         /* Parse options */
00526         while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
00527                 switch ( c ) {
00528                 case 'h':
00529                         /* Display help text */
00530                 default:
00531                         /* Unrecognised/invalid option */
00532                         imgfree_syntax ( argv );
00533                         return 1;
00534                 }
00535         }
00536 
00537         /* Need no more than one image name */
00538         if ( optind != argc )
00539                 name = argv[optind++];
00540         if ( optind != argc ) {
00541                 imgfree_syntax ( argv );
00542                 return 1;
00543         }
00544 
00545         if ( name ) {
00546                 /* Free specified image (may leak) */
00547                 image = find_image ( name );
00548                 if ( ! image ) {
00549                         printf ( "No such image: %s\n", name );
00550                         return 1;
00551                 }
00552                 imgfree ( image );
00553         } else {
00554                 /* Free all images */
00555                 list_for_each_entry_safe ( image, tmp, &images, list ) {
00556                         imgfree ( image );
00557                 }
00558         }
00559         return 0;
00560 }


Variable Documentation

struct command image_commands [] __command

Image management commands.

Definition at line 563 of file image_cmd.c.


Generated on Tue Apr 6 20:01:46 2010 for gPXE by  doxygen 1.5.7.1