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 [5] 
   struct option_descriptor   imgsingle [4] 
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 108 of file image_cmd.c.

109 {
110 struct imgsingle_options opts;
111 char *name_uri = NULL;
112 char *cmdline = NULL;
113 struct image *image;
114 int rc;
115
116 /* Parse options */
117 if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
118 goto err_parse_options;
119
120 /* Parse name/URI string and command line, if present */
121 if ( optind < argc ) {
122 name_uri = argv[optind];
123 if ( argv[ optind + 1 ] != NULL ) {
124 cmdline = concat_args ( &argv[ optind + 1 ] );
125 if ( ! cmdline ) {
126 rc = -ENOMEM;
127 goto err_parse_cmdline;
128 }
129 }
130 }
131
132 /* Acquire the image */
133 if ( name_uri ) {
134 if ( ( rc = desc->acquire ( name_uri, opts.timeout, opts.quiet,
135 &image ) ) != 0 )
136 goto err_acquire;
137 } else {
139 if ( ! image ) {
140 printf ( "No image selected\n" );
141 goto err_acquire;
142 }
143 }
144
145 /* Carry out command pre-action, if applicable */
146 if ( desc->preaction )
147 desc->preaction ( image );
148
149 /* Set the image name, if applicable */
150 if ( opts.name ) {
151 if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
152 printf ( "Could not name image: %s\n",
153 strerror ( rc ) );
154 goto err_set_name;
155 }
156 }
157
158 /* Set the command-line arguments, if applicable */
159 if ( cmdline ) {
160 if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
161 printf ( "Could not set arguments: %s\n",
162 strerror ( rc ) );
163 goto err_set_cmdline;
164 }
165 }
166
167 /* Set the auto-unregister flag, if applicable */
168 if ( opts.autofree )
170
171 /* Carry out command action, if applicable */
172 if ( desc->action ) {
173 if ( ( rc = desc->action ( image, &opts ) ) != 0 ) {
174 printf ( "Could not %s: %s\n",
175 desc->verb, strerror ( rc ) );
176 goto err_action;
177 }
178 }
179
180 /* Success */
181 rc = 0;
182
183 err_action:
184 err_set_cmdline:
185 err_set_name:
186 err_acquire:
187 free ( cmdline );
188 err_parse_cmdline:
189 err_parse_options:
190 return rc;
191}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
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:425
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition image.c:230
int image_set_name(struct image *image, const char *name)
Set image name.
Definition image.c:185
struct image_tag selected_image
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition image.h:90
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 211 of file image_cmd.c.

211 {
212 return imgsingle_exec ( argc, argv, &imgfetch_desc );
213}
struct imgsingle_descriptor imgfetch_desc
"imgfetch" family command descriptor
Definition image_cmd.c:199
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition image_cmd.c:108

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

223 {
224 return image_select ( image );
225}
int image_select(struct image *image)
Select image for execution.
Definition image.c:597

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

247 {
248 return imgsingle_exec ( argc, argv, &imgselect_desc );
249}
struct imgsingle_descriptor imgselect_desc
"imgselect" family command descriptor
Definition image_cmd.c:233

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

263 {
264 int rc;
265
266 /* Perform replacement or execution as applicable */
267 if ( opts->replace ) {
268
269 /* Try to replace image */
270 if ( ( rc = image_replace ( image ) ) != 0 )
271 return rc;
272
273 /* Stop script and tail-recurse into replacement image */
275
276 } else {
277
278 /* Try to execute image */
279 if ( ( rc = image_exec ( image ) ) != 0 )
280 return rc;
281 }
282
283 return 0;
284}
void shell_stop(int stop)
Set shell stop state.
Definition exec.c:218
int image_exec(struct image *image)
Execute image.
Definition image.c:446
int image_replace(struct image *replacement)
Set replacement image.
Definition image.c:561
@ 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 301 of file image_cmd.c.

301 {
302 return imgsingle_exec ( argc, argv, &imgexec_desc );
303}
struct imgsingle_descriptor imgexec_desc
"imgexec" family command descriptor
Definition image_cmd.c:287

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

324 {
325 return imgsingle_exec ( argc, argv, &imgargs_desc );
326}
struct imgsingle_descriptor imgargs_desc
"imgargs" family command descriptor
Definition image_cmd.c:311

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

348 {
349 struct imgmulti_options opts;
350 struct image *image;
351 struct image *tmp;
352 int i;
353 int rc;
354
355 /* Parse options */
356 if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
357 return rc;
358
359 /* If no images are explicitly specified, process all images */
360 if ( optind == argc ) {
362 payload ( image );
363 return 0;
364 }
365
366 /* Otherwise, process specified images */
367 for ( i = optind ; i < argc ; i++ ) {
368 image = find_image ( argv[i] );
369 if ( ! image ) {
370 printf ( "\"%s\": no such image\n", argv[i] );
371 return -ENOENT;
372 }
373 payload ( image );
374 }
375
376 return 0;
377}
#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:408
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition image.h:203
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition image_cmd.c:335
unsigned long tmp
Definition linux_pci.h:65
"img{multi}" options
Definition image_cmd.c:329

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

386 {
387 return imgmulti_exec ( argc, argv, imgstat );
388}
static int imgmulti_exec(int argc, char **argv, void(*payload)(struct image *image))
The "img{multi}" family of commands.
Definition image_cmd.c:347
void imgstat(struct image *image)
Display status of an image.
Definition imgmgmt.c:165

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

397 {
398 return imgmulti_exec ( argc, argv, unregister_image );
399}
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:390

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[5]

Definition at line 62 of file image_cmd.c.

Referenced by COMMAND().

◆ imgsingle

struct option_descriptor imgsingle[4]

Definition at line 66 of file image_cmd.c.

◆ [union]

union { ... } opts
Initial value:
= {
.imgexec = {
OPTION_DESC ( "timeout", 't', required_argument,
OPTION_DESC ( "quiet", 'q', no_argument,
struct imgsingle_options, quiet, parse_flag ),
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 194 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:194
int imgdownload_string(const char *uri_string, unsigned long timeout, int quiet, struct image **image)
Download a new image.
Definition imgmgmt.c:125

"imgfetch" family command descriptor

Definition at line 199 of file image_cmd.c.

199 {
200 .cmd = &imgfetch_cmd,
201 .acquire = imgdownload_string,
202};

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 228 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:228
static int imgselect(struct image *image, struct imgsingle_options *opts __unused)
"imgselect" command action
Definition image_cmd.c:222
int imgacquire(const char *name_uri, unsigned long timeout, int quiet, struct image **image)
Acquire an image.
Definition imgmgmt.c:148

"imgselect" family command descriptor

Definition at line 233 of file image_cmd.c.

233 {
234 .cmd = &imgselect_cmd,
235 .acquire = imgacquire,
236 .action = imgselect,
237 .verb = "select",
238};

Referenced by imgselect_exec().

◆ imgexec_cmd

struct command_descriptor imgexec_cmd
static
Initial value:
=
0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" )

"imgexec" command descriptor

Definition at line 252 of file image_cmd.c.

◆ imgexec_desc

struct imgsingle_descriptor imgexec_desc
Initial value:
= {
.cmd = &imgexec_cmd,
.acquire = imgacquire,
.action = imgexec,
.verb = "boot",
}
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition image_cmd.c:252
struct option_descriptor imgexec[5]
Definition image_cmd.c:62

"imgexec" family command descriptor

Definition at line 287 of file image_cmd.c.

287 {
288 .cmd = &imgexec_cmd,
289 .acquire = imgacquire,
290 .action = imgexec,
291 .verb = "boot",
292};

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 306 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:267
static struct command_descriptor imgargs_cmd
"imgargs" command descriptor
Definition image_cmd.c:306

"imgargs" family command descriptor

Definition at line 311 of file image_cmd.c.

311 {
312 .cmd = &imgargs_cmd,
313 .acquire = imgacquire,
314 .preaction = image_clear_cmdline,
315};

Referenced by imgargs_exec().

◆ imgmulti_opts

struct option_descriptor imgmulti_opts[] = {}
static

"img{multi}" option list

Definition at line 332 of file image_cmd.c.

332{};

◆ 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:332

"img{multi}" command descriptor

Definition at line 335 of file image_cmd.c.

Referenced by imgmulti_exec().