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

Image management commands. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.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...
 

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...
 
struct command image_commands [] __command
 Image management commands. 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 102 of file image_cmd.c.

103  {
104  struct imgsingle_options opts;
105  char *name_uri = NULL;
106  char *cmdline = NULL;
107  struct image *image;
108  int rc;
109 
110  /* Parse options */
111  if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
112  goto err_parse_options;
113 
114  /* Parse name/URI string and command line, if present */
115  if ( optind < argc ) {
116  name_uri = argv[optind];
117  if ( argv[ optind + 1 ] != NULL ) {
118  cmdline = concat_args ( &argv[ optind + 1 ] );
119  if ( ! cmdline ) {
120  rc = -ENOMEM;
121  goto err_parse_cmdline;
122  }
123  }
124  }
125 
126  /* Acquire the image */
127  if ( name_uri ) {
128  if ( ( rc = desc->acquire ( name_uri, opts.timeout,
129  &image ) ) != 0 )
130  goto err_acquire;
131  } else {
133  if ( ! image ) {
134  printf ( "No image selected\n" );
135  goto err_acquire;
136  }
137  }
138 
139  /* Carry out command pre-action, if applicable */
140  if ( desc->preaction )
141  desc->preaction ( image );
142 
143  /* Set the image name, if applicable */
144  if ( opts.name ) {
145  if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
146  printf ( "Could not name image: %s\n",
147  strerror ( rc ) );
148  goto err_set_name;
149  }
150  }
151 
152  /* Set the command-line arguments, if applicable */
153  if ( cmdline ) {
154  if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
155  printf ( "Could not set arguments: %s\n",
156  strerror ( rc ) );
157  goto err_set_cmdline;
158  }
159  }
160 
161  /* Set the auto-unregister flag, if applicable */
162  if ( opts.autofree )
164 
165  /* Carry out command action, if applicable */
166  if ( desc->action ) {
167  if ( ( rc = desc->action ( image, &opts ) ) != 0 ) {
168  printf ( "Could not %s: %s\n",
169  desc->verb, strerror ( rc ) );
170  goto err_action;
171  }
172  }
173 
174  /* Success */
175  rc = 0;
176 
177  err_action:
178  err_set_cmdline:
179  err_set_name:
180  err_acquire:
181  free ( cmdline );
182  err_parse_cmdline:
183  err_parse_options:
184  return rc;
185 }
unsigned int flags
Flags.
Definition: image.h:36
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:447
An executable image.
Definition: image.h:24
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:73
#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:144
struct command_descriptor * cmd
Command descriptor.
Definition: image_cmd.c:79
void(* preaction)(struct image *image)
Pre-action to take upon image, or NULL.
Definition: image_cmd.c:84
int(* action)(struct image *image, struct imgsingle_options *opts)
Action to take upon image, or NULL.
Definition: image_cmd.c:86
struct image * image_find_selected(void)
Find selected image.
Definition: image.c:451
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:166
"img{single}" options
Definition: image_cmd.c:44
uint32_t cmdline
Definition: multiboot.h:16
static union @424 opts
"img{single}" option list
const char * verb
Verb to describe action.
Definition: image_cmd.c:89
int(* acquire)(const char *name, unsigned long timeout, struct image **image)
Function to use to acquire the image.
Definition: image_cmd.c:81
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References imgsingle_descriptor::acquire, imgsingle_descriptor::action, imgsingle_descriptor::cmd, cmdline, concat_args(), ENOMEM, image::flags, free, IMAGE_AUTO_UNREGISTER, image_find_selected(), image_set_cmdline(), image_set_name(), NULL, optind, opts, parse_options(), imgsingle_descriptor::preaction, printf(), rc, strerror(), and imgsingle_descriptor::verb.

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 205 of file image_cmd.c.

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

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 216 of file image_cmd.c.

217  {
218  return image_select ( image );
219 }
int image_select(struct image *image)
Select image for execution.
Definition: image.c:429
An executable image.
Definition: image.h:24

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 241 of file image_cmd.c.

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

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 257 of file image_cmd.c.

257  {
258  int rc;
259 
260  /* Perform replacement or execution as applicable */
261  if ( opts->replace ) {
262 
263  /* Try to replace image */
264  if ( ( rc = image_replace ( image ) ) != 0 )
265  return rc;
266 
267  /* Stop script and tail-recurse into replacement image */
269 
270  } else {
271 
272  /* Try to execute image */
273  if ( ( rc = image_exec ( image ) ) != 0 )
274  return rc;
275  }
276 
277  return 0;
278 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
An executable image.
Definition: image.h:24
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
int image_exec(struct image *image)
Execute image.
Definition: image.c:291
int image_replace(struct image *replacement)
Set replacement image.
Definition: image.c:393
Stop processing commands.
Definition: shell.h:29
static union @424 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 295 of file image_cmd.c.

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

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 318 of file image_cmd.c.

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

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 341 of file image_cmd.c.

342  {
343  struct imgmulti_options opts;
344  struct image *image;
345  struct image *tmp;
346  int i;
347  int rc;
348 
349  /* Parse options */
350  if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
351  return rc;
352 
353  /* If no images are explicitly specified, process all images */
354  if ( optind == argc ) {
356  payload ( image );
357  return 0;
358  }
359 
360  /* Otherwise, process specified images */
361  for ( i = optind ; i < argc ; i++ ) {
362  image = find_image ( argv[i] );
363  if ( ! image ) {
364  printf ( "\"%s\": no such image\n", argv[i] );
365  return -ENOENT;
366  }
367  payload ( image );
368  }
369 
370  return 0;
371 }
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:270
#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:447
An executable image.
Definition: image.h:24
"img{multi}" options
Definition: image_cmd.c:323
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition: image.h:153
uint8_t * tmp
Definition: entropy.h:156
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition: image_cmd.c:329
static union @424 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 380 of file image_cmd.c.

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

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 391 of file image_cmd.c.

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

References imgmulti_exec(), and unregister_image().

Variable Documentation

◆ imgexec

struct option_descriptor imgexec[4]

Definition at line 58 of file image_cmd.c.

◆ imgsingle

struct option_descriptor imgsingle[3]

Definition at line 62 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:111
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:70
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:200
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:66
Option requires an argument.
Definition: getopt.h:18
void timeout(int)
"img{single}" options
Definition: image_cmd.c:44

"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:96
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
"img{single}" options
Definition: image_cmd.c:44
static union @424 opts
"img{single}" option list

"imgfetch" command descriptor

Definition at line 188 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:188
int imgdownload_string(const char *uri_string, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:119

"imgfetch" family command descriptor

Definition at line 193 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:96
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
"img{single}" options
Definition: image_cmd.c:44
static union @424 opts
"img{single}" option list

"imgselect" command descriptor

Definition at line 222 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:216
static struct command_descriptor imgselect_cmd
"imgselect" command descriptor
Definition: image_cmd.c:222
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141

"imgselect" family command descriptor

Definition at line 227 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:96
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
"img{single}" options
Definition: image_cmd.c:44
static union @424 opts
"img{single}" option list

"imgexec" command descriptor

Definition at line 246 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:58
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition: image_cmd.c:246
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141

"imgexec" family command descriptor

Definition at line 281 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:96
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
"img{single}" options
Definition: image_cmd.c:44
static union @424 opts
"img{single}" option list

"imgargs" command descriptor

Definition at line 300 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:300
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition: image.h:215
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:141

"imgargs" family command descriptor

Definition at line 305 of file image_cmd.c.

Referenced by imgargs_exec().

◆ imgmulti_opts

struct option_descriptor imgmulti_opts[] = {}
static

"img{multi}" option list

Definition at line 326 of file image_cmd.c.

◆ imgmulti_cmd

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

"img{multi}" command descriptor

Definition at line 329 of file image_cmd.c.

Referenced by imgmulti_exec().

◆ __command

struct command image_commands [] __command

Image management commands.

Definition at line 396 of file image_cmd.c.