iPXE
Data Structures | Functions | Variables
image_cmd.c File Reference

Image management commands. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/image.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/shell.h>
#include <usr/imgmgmt.h>

Go to the source code of this file.

Data Structures

struct  imgsingle_options
 "img{single}" options More...
 
struct  imgsingle_descriptor
 An "img{single}" family command descriptor. More...
 
struct  imgmulti_options
 "img{multi}" options More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static int imgsingle_exec (int argc, char **argv, struct imgsingle_descriptor *desc)
 The "img{single}" family of commands. More...
 
static int imgfetch_exec (int argc, char **argv)
 The "imgfetch" command. More...
 
static int imgselect (struct image *image, struct imgsingle_options *opts __unused)
 "imgselect" command action More...
 
static int imgselect_exec (int argc, char **argv)
 The "imgselect" command. More...
 
static int imgexec (struct image *image, struct imgsingle_options *opts)
 "imgexec" command action More...
 
static int imgexec_exec (int argc, char **argv)
 The "imgexec" command. More...
 
static int imgargs_exec (int argc, char **argv)
 The "imgargs" command body. More...
 
static int imgmulti_exec (int argc, char **argv, void(*payload)(struct image *image))
 The "img{multi}" family of commands. More...
 
static int imgstat_exec (int argc, char **argv)
 The "imgstat" command. More...
 
static int imgfree_exec (int argc, char **argv)
 The "imgfree" command. More...
 
 COMMAND (imgfetch, imgfetch_exec)
 
 COMMAND (module, imgfetch_exec)
 
 COMMAND (initrd, imgfetch_exec)
 
 COMMAND (imgselect, imgselect_exec)
 
 COMMAND (imgload, imgselect_exec)
 
 COMMAND (kernel, imgselect_exec)
 
 COMMAND (imgexec, imgexec_exec)
 
 COMMAND (chain, imgexec_exec)
 
 COMMAND (boot, imgexec_exec)
 
 COMMAND (imgargs, imgargs_exec)
 
 COMMAND (imgstat, imgstat_exec)
 
 COMMAND (imgfree, imgfree_exec)
 

Variables

union {
   struct option_descriptor   imgexec [4]
 
   struct option_descriptor   imgsingle [3]
 
opts
 "img{single}" option list More...
 
static struct command_descriptor imgfetch_cmd
 "imgfetch" command descriptor More...
 
struct imgsingle_descriptor imgfetch_desc
 "imgfetch" family command descriptor More...
 
static struct command_descriptor imgselect_cmd
 "imgselect" command descriptor More...
 
struct imgsingle_descriptor imgselect_desc
 "imgselect" family command descriptor More...
 
static struct command_descriptor imgexec_cmd
 "imgexec" command descriptor More...
 
struct imgsingle_descriptor imgexec_desc
 "imgexec" family command descriptor More...
 
static struct command_descriptor imgargs_cmd
 "imgargs" command descriptor More...
 
struct imgsingle_descriptor imgargs_desc
 "imgargs" family command descriptor More...
 
static struct option_descriptor imgmulti_opts [] = {}
 "img{multi}" option list More...
 
static struct command_descriptor imgmulti_cmd
 "img{multi}" command descriptor More...
 

Detailed Description

Image management commands.

Definition in file image_cmd.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ imgsingle_exec()

static int imgsingle_exec ( int  argc,
char **  argv,
struct imgsingle_descriptor desc 
)
static

The "img{single}" family of commands.

Parameters
argcArgument count
argvArgument list
desc"img{single}" command descriptor
action_nameAction name (for error messages)
actionAction to take upon image
Return values
rcReturn status code

Definition at line 103 of file image_cmd.c.

104  {
105  struct imgsingle_options opts;
106  char *name_uri = NULL;
107  char *cmdline = NULL;
108  struct image *image;
109  int rc;
110 
111  /* Parse options */
112  if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
113  goto err_parse_options;
114 
115  /* Parse name/URI string and command line, if present */
116  if ( optind < argc ) {
117  name_uri = argv[optind];
118  if ( argv[ optind + 1 ] != NULL ) {
119  cmdline = concat_args ( &argv[ optind + 1 ] );
120  if ( ! cmdline ) {
121  rc = -ENOMEM;
122  goto err_parse_cmdline;
123  }
124  }
125  }
126 
127  /* Acquire the image */
128  if ( name_uri ) {
129  if ( ( rc = desc->acquire ( name_uri, opts.timeout,
130  &image ) ) != 0 )
131  goto err_acquire;
132  } else {
134  if ( ! image ) {
135  printf ( "No image selected\n" );
136  goto err_acquire;
137  }
138  }
139 
140  /* Carry out command pre-action, if applicable */
141  if ( desc->preaction )
142  desc->preaction ( image );
143 
144  /* Set the image name, if applicable */
145  if ( opts.name ) {
146  if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
147  printf ( "Could not name image: %s\n",
148  strerror ( rc ) );
149  goto err_set_name;
150  }
151  }
152 
153  /* Set the command-line arguments, if applicable */
154  if ( cmdline ) {
155  if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
156  printf ( "Could not set arguments: %s\n",
157  strerror ( rc ) );
158  goto err_set_cmdline;
159  }
160  }
161 
162  /* Set the auto-unregister flag, if applicable */
163  if ( opts.autofree )
165 
166  /* Carry out command action, if applicable */
167  if ( desc->action ) {
168  if ( ( rc = desc->action ( image, &opts ) ) != 0 ) {
169  printf ( "Could not %s: %s\n",
170  desc->verb, strerror ( rc ) );
171  goto err_action;
172  }
173  }
174 
175  /* Success */
176  rc = 0;
177 
178  err_action:
179  err_set_cmdline:
180  err_set_name:
181  err_acquire:
182  free ( cmdline );
183  err_parse_cmdline:
184  err_parse_options:
185  return rc;
186 }
unsigned int flags
Flags.
Definition: image.h:39
struct image_tag selected_image
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optind
Current option index.
Definition: getopt.c:51
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:23
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:82
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:392
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:180
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:225
"img{single}" options
Definition: image_cmd.c:45
uint32_t cmdline
Definition: multiboot.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static union @448 opts
"img{single}" option list

References cmdline, concat_args(), desc, ENOMEM, find_image_tag(), image::flags, free, IMAGE_AUTO_UNREGISTER, image_set_cmdline(), image_set_name(), NULL, optind, opts, parse_options(), printf(), rc, selected_image, and strerror().

Referenced by imgargs_exec(), imgexec_exec(), imgfetch_exec(), and imgselect_exec().

◆ imgfetch_exec()

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

The "imgfetch" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 206 of file image_cmd.c.

206  {
207  return imgsingle_exec ( argc, argv, &imgfetch_desc );
208 }
struct imgsingle_descriptor imgfetch_desc
"imgfetch" family command descriptor
Definition: image_cmd.c:194
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition: image_cmd.c:103

References imgfetch_desc, and imgsingle_exec().

◆ imgselect()

static int imgselect ( struct image image,
struct imgsingle_options *opts  __unused 
)
static

"imgselect" command action

Parameters
imageImage
optsOptions
Return values
rcReturn status code

Definition at line 217 of file image_cmd.c.

218  {
219  return image_select ( image );
220 }
int image_select(struct image *image)
Select image for execution.
Definition: image.c:564
An executable image.
Definition: image.h:23

References image_select().

◆ imgselect_exec()

static int imgselect_exec ( int  argc,
char **  argv 
)
static

The "imgselect" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 242 of file image_cmd.c.

242  {
243  return imgsingle_exec ( argc, argv, &imgselect_desc );
244 }
struct imgsingle_descriptor imgselect_desc
"imgselect" family command descriptor
Definition: image_cmd.c:228
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition: image_cmd.c:103

References imgselect_desc, and imgsingle_exec().

◆ imgexec()

static int imgexec ( struct image image,
struct imgsingle_options opts 
)
static

"imgexec" command action

Parameters
imageImage
optsOptions
Return values
rcReturn status code

Definition at line 258 of file image_cmd.c.

258  {
259  int rc;
260 
261  /* Perform replacement or execution as applicable */
262  if ( opts->replace ) {
263 
264  /* Try to replace image */
265  if ( ( rc = image_replace ( image ) ) != 0 )
266  return rc;
267 
268  /* Stop script and tail-recurse into replacement image */
270 
271  } else {
272 
273  /* Try to execute image */
274  if ( ( rc = image_exec ( image ) ) != 0 )
275  return rc;
276  }
277 
278  return 0;
279 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:23
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
int image_exec(struct image *image)
Execute image.
Definition: image.c:413
int image_replace(struct image *replacement)
Set replacement image.
Definition: image.c:528
Stop processing commands.
Definition: shell.h:29
static union @448 opts
"img{single}" option list

References image_exec(), image_replace(), opts, rc, shell_stop(), and SHELL_STOP_COMMAND_SEQUENCE.

◆ imgexec_exec()

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

The "imgexec" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 296 of file image_cmd.c.

296  {
297  return imgsingle_exec ( argc, argv, &imgexec_desc );
298 }
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition: image_cmd.c:103
struct imgsingle_descriptor imgexec_desc
"imgexec" family command descriptor
Definition: image_cmd.c:282

References imgexec_desc, and imgsingle_exec().

◆ imgargs_exec()

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

The "imgargs" command body.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 319 of file image_cmd.c.

319  {
320  return imgsingle_exec ( argc, argv, &imgargs_desc );
321 }
struct imgsingle_descriptor imgargs_desc
"imgargs" family command descriptor
Definition: image_cmd.c:306
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition: image_cmd.c:103

References imgargs_desc, and imgsingle_exec().

◆ imgmulti_exec()

static int imgmulti_exec ( int  argc,
char **  argv,
void(*)(struct image *image payload 
)
static

The "img{multi}" family of commands.

Parameters
argcArgument count
argvArgument list
payloadFunction to execute on each image
Return values
rcReturn status code

Definition at line 342 of file image_cmd.c.

343  {
344  struct imgmulti_options opts;
345  struct image *image;
346  struct image *tmp;
347  int i;
348  int rc;
349 
350  /* Parse options */
351  if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
352  return rc;
353 
354  /* If no images are explicitly specified, process all images */
355  if ( optind == argc ) {
357  payload ( image );
358  return 0;
359  }
360 
361  /* Otherwise, process specified images */
362  for ( i = optind ; i < argc ; i++ ) {
363  image = find_image ( argv[i] );
364  if ( ! image ) {
365  printf ( "\"%s\": no such image\n", argv[i] );
366  return -ENOENT;
367  }
368  payload ( image );
369  }
370 
371  return 0;
372 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optind
Current option index.
Definition: getopt.c:51
struct image * find_image(const char *name)
Find image by name.
Definition: image.c:375
#define ENOENT
No such file or directory.
Definition: errno.h:514
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
An executable image.
Definition: image.h:23
"img{multi}" options
Definition: image_cmd.c:324
unsigned long tmp
Definition: linux_pci.h:64
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition: image.h:194
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition: image_cmd.c:330
static union @448 opts
"img{single}" option list

References ENOENT, find_image(), for_each_image_safe, imgmulti_cmd, optind, opts, parse_options(), printf(), rc, and tmp.

Referenced by imgfree_exec(), and imgstat_exec().

◆ imgstat_exec()

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

The "imgstat" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 381 of file image_cmd.c.

381  {
382  return imgmulti_exec ( argc, argv, imgstat );
383 }
static int imgmulti_exec(int argc, char **argv, void(*payload)(struct image *image))
The "img{multi}" family of commands.
Definition: image_cmd.c:342
void imgstat(struct image *image)
Display status of an image.
Definition: imgmgmt.c:159

References imgmulti_exec(), and imgstat().

◆ imgfree_exec()

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

The "imgfree" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 392 of file image_cmd.c.

392  {
393  return imgmulti_exec ( argc, argv, unregister_image );
394 }
static int imgmulti_exec(int argc, char **argv, void(*payload)(struct image *image))
The "img{multi}" family of commands.
Definition: image_cmd.c:342
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357

References imgmulti_exec(), and unregister_image().

◆ COMMAND() [1/12]

COMMAND ( imgfetch  ,
imgfetch_exec   
)

◆ COMMAND() [2/12]

COMMAND ( module  ,
imgfetch_exec   
)

◆ COMMAND() [3/12]

COMMAND ( initrd  ,
imgfetch_exec   
)

◆ COMMAND() [4/12]

COMMAND ( imgselect  ,
imgselect_exec   
)

◆ COMMAND() [5/12]

COMMAND ( imgload  ,
imgselect_exec   
)

◆ COMMAND() [6/12]

COMMAND ( kernel  ,
imgselect_exec   
)

◆ COMMAND() [7/12]

COMMAND ( imgexec  ,
imgexec_exec   
)

◆ COMMAND() [8/12]

COMMAND ( chain  ,
imgexec_exec   
)

◆ COMMAND() [9/12]

COMMAND ( boot  ,
imgexec_exec   
)

◆ COMMAND() [10/12]

COMMAND ( imgargs  ,
imgargs_exec   
)

◆ COMMAND() [11/12]

COMMAND ( imgstat  ,
imgstat_exec   
)

◆ COMMAND() [12/12]

COMMAND ( imgfree  ,
imgfree_exec   
)

Variable Documentation

◆ imgexec

struct option_descriptor imgexec[4]

Definition at line 59 of file image_cmd.c.

◆ imgsingle

struct option_descriptor imgsingle[3]

Definition at line 63 of file image_cmd.c.

◆ opts

union { ... } opts
Initial value:
= {
.imgexec = {
OPTION_DESC ( "timeout", 't', required_argument,
OPTION_DESC ( "autofree", 'a', no_argument,
struct imgsingle_options, autofree, parse_flag ),
OPTION_DESC ( "replace", 'r', no_argument,
struct imgsingle_options, replace, parse_flag ),
},
}
const char * name
Definition: ath9k_hw.c:1984
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
Option does not take an argument.
Definition: getopt.h:16
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
void timeout(int)
"img{single}" options
Definition: image_cmd.c:45

"img{single}" option list

Referenced by imgexec(), imgmulti_exec(), and imgsingle_exec().

◆ imgfetch_cmd

struct command_descriptor imgfetch_cmd
static
Initial value:
=
COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
1, MAX_ARGUMENTS, "<uri> [<arguments>...]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
"img{single}" options
Definition: image_cmd.c:45
static union @448 opts
"img{single}" option list

"imgfetch" command descriptor

Definition at line 189 of file image_cmd.c.

◆ imgfetch_desc

struct imgsingle_descriptor imgfetch_desc
Initial value:
= {
.cmd = &imgfetch_cmd,
.acquire = imgdownload_string,
}
static struct command_descriptor imgfetch_cmd
"imgfetch" command descriptor
Definition: image_cmd.c:189
int imgdownload_string(const char *uri_string, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:120

"imgfetch" family command descriptor

Definition at line 194 of file image_cmd.c.

Referenced by imgfetch_exec().

◆ imgselect_cmd

struct command_descriptor imgselect_cmd
static
Initial value:
=
COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
"img{single}" options
Definition: image_cmd.c:45
static union @448 opts
"img{single}" option list

"imgselect" command descriptor

Definition at line 223 of file image_cmd.c.

◆ imgselect_desc

struct imgsingle_descriptor imgselect_desc
Initial value:
= {
.cmd = &imgselect_cmd,
.acquire = imgacquire,
.action = imgselect,
.verb = "select",
}
static int imgselect(struct image *image, struct imgsingle_options *opts __unused)
"imgselect" command action
Definition: image_cmd.c:217
static struct command_descriptor imgselect_cmd
"imgselect" command descriptor
Definition: image_cmd.c:223
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142

"imgselect" family command descriptor

Definition at line 228 of file image_cmd.c.

Referenced by imgselect_exec().

◆ imgexec_cmd

struct command_descriptor imgexec_cmd
static
Initial value:
=
COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
"img{single}" options
Definition: image_cmd.c:45
static union @448 opts
"img{single}" option list

"imgexec" command descriptor

Definition at line 247 of file image_cmd.c.

◆ imgexec_desc

struct imgsingle_descriptor imgexec_desc
Initial value:
= {
.cmd = &imgexec_cmd,
.acquire = imgacquire,
.action = imgexec,
.verb = "boot",
}
struct option_descriptor imgexec[4]
Definition: image_cmd.c:59
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition: image_cmd.c:247
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142

"imgexec" family command descriptor

Definition at line 282 of file image_cmd.c.

Referenced by imgexec_exec().

◆ imgargs_cmd

struct command_descriptor imgargs_cmd
static
Initial value:
=
COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" )
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
"img{single}" options
Definition: image_cmd.c:45
static union @448 opts
"img{single}" option list

"imgargs" command descriptor

Definition at line 301 of file image_cmd.c.

◆ imgargs_desc

struct imgsingle_descriptor imgargs_desc
Initial value:
= {
.cmd = &imgargs_cmd,
.acquire = imgacquire,
.preaction = image_clear_cmdline,
}
static struct command_descriptor imgargs_cmd
"imgargs" command descriptor
Definition: image_cmd.c:301
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition: image.h:258
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142

"imgargs" family command descriptor

Definition at line 306 of file image_cmd.c.

Referenced by imgargs_exec().

◆ imgmulti_opts

struct option_descriptor imgmulti_opts[] = {}
static

"img{multi}" option list

Definition at line 327 of file image_cmd.c.

◆ imgmulti_cmd

struct command_descriptor imgmulti_cmd
static
Initial value:
=
"[<image>...]" )
"img{multi}" options
Definition: image_cmd.c:324
static struct option_descriptor imgmulti_opts[]
"img{multi}" option list
Definition: image_cmd.c:327
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108

"img{multi}" command descriptor

Definition at line 330 of file image_cmd.c.

Referenced by imgmulti_exec().