iPXE
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)
 FILE_SECBOOT (PERMITTED)
static int imgsingle_exec (int argc, char **argv, struct imgsingle_descriptor *desc)
 The "img{single}" family of commands.
static int imgfetch_exec (int argc, char **argv)
 The "imgfetch" command.
static int imgselect (struct image *image, struct imgsingle_options *opts __unused)
 "imgselect" command action
static int imgselect_exec (int argc, char **argv)
 The "imgselect" command.
static int imgexec (struct image *image, struct imgsingle_options *opts)
 "imgexec" command action
static int imgexec_exec (int argc, char **argv)
 The "imgexec" command.
static int imgargs_exec (int argc, char **argv)
 The "imgargs" command body.
static int imgmulti_exec (int argc, char **argv, void(*payload)(struct image *image))
 The "img{multi}" family of commands.
static int imgstat_exec (int argc, char **argv)
 The "imgstat" command.
static int imgfree_exec (int argc, char **argv)
 The "imgfree" command.
 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
static struct command_descriptor imgfetch_cmd
 "imgfetch" command descriptor
struct imgsingle_descriptor imgfetch_desc
 "imgfetch" family command descriptor
static struct command_descriptor imgselect_cmd
 "imgselect" command descriptor
struct imgsingle_descriptor imgselect_desc
 "imgselect" family command descriptor
static struct command_descriptor imgexec_cmd
 "imgexec" command descriptor
struct imgsingle_descriptor imgexec_desc
 "imgexec" family command descriptor
static struct command_descriptor imgargs_cmd
 "imgargs" command descriptor
struct imgsingle_descriptor imgargs_desc
 "imgargs" family command descriptor
static struct option_descriptor imgmulti_opts [] = {}
 "img{multi}" option list
static struct command_descriptor imgmulti_cmd
 "img{multi}" command descriptor

Detailed Description

Image management commands.

Definition in file image_cmd.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ imgsingle_exec()

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

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

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()

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

The "imgfetch" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 207 of file image_cmd.c.

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

References imgfetch_desc, and imgsingle_exec().

Referenced by COMMAND(), COMMAND(), and COMMAND().

◆ imgselect()

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

219 {
220 return image_select ( image );
221}
int image_select(struct image *image)
Select image for execution.
Definition image.c:565

References __unused, image_select(), and opts.

Referenced by COMMAND().

◆ imgselect_exec()

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

The "imgselect" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 243 of file image_cmd.c.

243 {
244 return imgsingle_exec ( argc, argv, &imgselect_desc );
245}
struct imgsingle_descriptor imgselect_desc
"imgselect" family command descriptor
Definition image_cmd.c:229

References imgselect_desc, and imgsingle_exec().

Referenced by COMMAND(), COMMAND(), and COMMAND().

◆ imgexec()

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

"imgexec" command action

Parameters
imageImage
optsOptions
Return values
rcReturn status code

Definition at line 259 of file image_cmd.c.

259 {
260 int rc;
261
262 /* Perform replacement or execution as applicable */
263 if ( opts->replace ) {
264
265 /* Try to replace image */
266 if ( ( rc = image_replace ( image ) ) != 0 )
267 return rc;
268
269 /* Stop script and tail-recurse into replacement image */
271
272 } else {
273
274 /* Try to execute image */
275 if ( ( rc = image_exec ( image ) ) != 0 )
276 return rc;
277 }
278
279 return 0;
280}
void shell_stop(int stop)
Set shell stop state.
Definition exec.c:218
int image_exec(struct image *image)
Execute image.
Definition image.c:414
int image_replace(struct image *replacement)
Set replacement image.
Definition image.c:529
@ SHELL_STOP_COMMAND_SEQUENCE
Stop processing commands.
Definition shell.h:30

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

◆ imgexec_exec()

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

The "imgexec" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 297 of file image_cmd.c.

297 {
298 return imgsingle_exec ( argc, argv, &imgexec_desc );
299}
struct imgsingle_descriptor imgexec_desc
"imgexec" family command descriptor
Definition image_cmd.c:283

References imgexec_desc, and imgsingle_exec().

Referenced by COMMAND(), COMMAND(), and COMMAND().

◆ imgargs_exec()

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

320 {
321 return imgsingle_exec ( argc, argv, &imgargs_desc );
322}
struct imgsingle_descriptor imgargs_desc
"imgargs" family command descriptor
Definition image_cmd.c:307

References imgargs_desc, and imgsingle_exec().

Referenced by COMMAND().

◆ imgmulti_exec()

int imgmulti_exec ( int argc,
char ** argv,
void(* payload )(struct image *image) )
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 343 of file image_cmd.c.

344 {
345 struct imgmulti_options opts;
346 struct image *image;
347 struct image *tmp;
348 int i;
349 int rc;
350
351 /* Parse options */
352 if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
353 return rc;
354
355 /* If no images are explicitly specified, process all images */
356 if ( optind == argc ) {
358 payload ( image );
359 return 0;
360 }
361
362 /* Otherwise, process specified images */
363 for ( i = optind ; i < argc ; i++ ) {
364 image = find_image ( argv[i] );
365 if ( ! image ) {
366 printf ( "\"%s\": no such image\n", argv[i] );
367 return -ENOENT;
368 }
369 payload ( image );
370 }
371
372 return 0;
373}
#define ENOENT
No such file or directory.
Definition errno.h:515
struct image * find_image(const char *name)
Find image by name.
Definition image.c:376
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition image.h:195
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition image_cmd.c:331
unsigned long tmp
Definition linux_pci.h:65
"img{multi}" options
Definition image_cmd.c:325

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()

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

The "imgstat" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 382 of file image_cmd.c.

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

References imgmulti_exec(), and imgstat().

Referenced by COMMAND().

◆ imgfree_exec()

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

The "imgfree" command.

Parameters
argcArgument count
argvArgument list
Return values
rcReturn status code

Definition at line 393 of file image_cmd.c.

393 {
394 return imgmulti_exec ( argc, argv, unregister_image );
395}
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:358

References imgmulti_exec(), and unregister_image().

Referenced by COMMAND().

◆ COMMAND() [1/12]

COMMAND ( imgfetch ,
imgfetch_exec  )

References imgfetch_exec().

◆ COMMAND() [2/12]

COMMAND ( module ,
imgfetch_exec  )

References imgfetch_exec().

◆ COMMAND() [3/12]

COMMAND ( initrd ,
imgfetch_exec  )

References imgfetch_exec().

◆ COMMAND() [4/12]

COMMAND ( imgselect ,
imgselect_exec  )

References imgselect(), and imgselect_exec().

◆ COMMAND() [5/12]

COMMAND ( imgload ,
imgselect_exec  )

References imgselect_exec().

◆ COMMAND() [6/12]

COMMAND ( kernel ,
imgselect_exec  )

References imgselect_exec(), and kernel.

◆ COMMAND() [7/12]

COMMAND ( imgexec ,
imgexec_exec  )

References imgexec, and imgexec_exec().

◆ COMMAND() [8/12]

COMMAND ( chain ,
imgexec_exec  )

References imgexec_exec().

◆ COMMAND() [9/12]

COMMAND ( boot ,
imgexec_exec  )

References imgexec_exec().

◆ COMMAND() [10/12]

COMMAND ( imgargs ,
imgargs_exec  )

References imgargs_exec().

◆ COMMAND() [11/12]

COMMAND ( imgstat ,
imgstat_exec  )

References imgstat(), and imgstat_exec().

◆ COMMAND() [12/12]

COMMAND ( imgfree ,
imgfree_exec  )

References imgfree_exec().

Variable Documentation

◆ imgexec

struct option_descriptor imgexec[4]

Definition at line 60 of file image_cmd.c.

Referenced by COMMAND().

◆ imgsingle

struct option_descriptor imgsingle[3]

Definition at line 64 of file image_cmd.c.

◆ [union]

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:1986
void timeout(int)
@ required_argument
Option requires an argument.
Definition getopt.h:19
@ no_argument
Option does not take an argument.
Definition getopt.h:17
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition parseopt.c:227
int parse_string(char *text, char **value)
Parse string value.
Definition parseopt.c:74
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition parseopt.c:115
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68

"img{single}" option list

◆ 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:98
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition parseopt.h:109

"imgfetch" command descriptor

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

"imgfetch" family command descriptor

Definition at line 195 of file image_cmd.c.

195 {
196 .cmd = &imgfetch_cmd,
197 .acquire = imgdownload_string,
198};

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>...]" )

"imgselect" command descriptor

Definition at line 224 of file image_cmd.c.

◆ imgselect_desc

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

"imgselect" family command descriptor

Definition at line 229 of file image_cmd.c.

229 {
230 .cmd = &imgselect_cmd,
231 .acquire = imgacquire,
232 .action = imgselect,
233 .verb = "select",
234};

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>...]]" )

"imgexec" command descriptor

Definition at line 248 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:60
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition image_cmd.c:248

"imgexec" family command descriptor

Definition at line 283 of file image_cmd.c.

283 {
284 .cmd = &imgexec_cmd,
285 .acquire = imgacquire,
286 .action = imgexec,
287 .verb = "boot",
288};

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>...]" )

"imgargs" command descriptor

Definition at line 302 of file image_cmd.c.

◆ imgargs_desc

struct imgsingle_descriptor imgargs_desc
Initial value:
= {
.cmd = &imgargs_cmd,
.acquire = imgacquire,
.preaction = image_clear_cmdline,
}
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition image.h:259
static struct command_descriptor imgargs_cmd
"imgargs" command descriptor
Definition image_cmd.c:302

"imgargs" family command descriptor

Definition at line 307 of file image_cmd.c.

307 {
308 .cmd = &imgargs_cmd,
309 .acquire = imgacquire,
310 .preaction = image_clear_cmdline,
311};

Referenced by imgargs_exec().

◆ imgmulti_opts

struct option_descriptor imgmulti_opts[] = {}
static

"img{multi}" option list

Definition at line 328 of file image_cmd.c.

328{};

◆ imgmulti_cmd

struct command_descriptor imgmulti_cmd
static
Initial value:
=
"[<image>...]" )
static struct option_descriptor imgmulti_opts[]
"img{multi}" option list
Definition image_cmd.c:328

"img{multi}" command descriptor

Definition at line 331 of file image_cmd.c.

Referenced by imgmulti_exec().