iPXE
image_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <errno.h>
32#include <getopt.h>
33#include <ipxe/image.h>
34#include <ipxe/command.h>
35#include <ipxe/parseopt.h>
36#include <ipxe/shell.h>
37#include <usr/imgmgmt.h>
38
39/** @file
40 *
41 * Image management commands
42 *
43 */
44
45/** "img{single}" options */
47 /** Image name */
48 char *name;
49 /** Download timeout */
50 unsigned long timeout;
51 /** Display only error messages */
52 int quiet;
53 /** Replace image */
55 /** Free image after execution */
57};
58
59/** "img{single}" option list */
60static union {
61 /* "imgexec" takes all five options */
63 /* Other "img{single}" commands take only --name, --timeout, --quiet,
64 * and --autofree
65 */
67} opts = {
68 .imgexec = {
69 OPTION_DESC ( "name", 'n', required_argument,
71 OPTION_DESC ( "timeout", 't', required_argument,
73 OPTION_DESC ( "quiet", 'q', no_argument,
74 struct imgsingle_options, quiet, parse_flag ),
75 OPTION_DESC ( "autofree", 'a', no_argument,
76 struct imgsingle_options, autofree, parse_flag ),
77 OPTION_DESC ( "replace", 'r', no_argument,
78 struct imgsingle_options, replace, parse_flag ),
79 },
80};
81
82/** An "img{single}" family command descriptor */
84 /** Command descriptor */
86 /** Function to use to acquire the image */
87 int ( * acquire ) ( const char *name, unsigned long timeout, int quiet,
88 struct image **image );
89 /** Pre-action to take upon image, or NULL */
90 void ( * preaction ) ( struct image *image );
91 /** Action to take upon image, or NULL */
92 int ( * action ) ( struct image *image,
93 struct imgsingle_options *opts );
94 /** Verb to describe action */
95 const char *verb;
96};
97
98/**
99 * The "img{single}" family of commands
100 *
101 * @v argc Argument count
102 * @v argv Argument list
103 * @v desc "img{single}" command descriptor
104 * @v action_name Action name (for error messages)
105 * @v action Action to take upon image
106 * @ret rc Return status code
107 */
108static int imgsingle_exec ( int argc, char **argv,
109 struct imgsingle_descriptor *desc ) {
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}
192
193/** "imgfetch" command descriptor */
195 COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
196 1, MAX_ARGUMENTS, "<uri> [<arguments>...]" );
197
198/** "imgfetch" family command descriptor */
200 .cmd = &imgfetch_cmd,
201 .acquire = imgdownload_string,
202};
203
204/**
205 * The "imgfetch" command
206 *
207 * @v argc Argument count
208 * @v argv Argument list
209 * @ret rc Return status code
210 */
211static int imgfetch_exec ( int argc, char **argv ) {
212 return imgsingle_exec ( argc, argv, &imgfetch_desc );
213}
214
215/**
216 * "imgselect" command action
217 *
218 * @v image Image
219 * @v opts Options
220 * @ret rc Return status code
221 */
222static int imgselect ( struct image *image,
224 return image_select ( image );
225}
226
227/** "imgselect" command descriptor */
229 COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
230 1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
231
232/** "imgselect" family command descriptor */
234 .cmd = &imgselect_cmd,
235 .acquire = imgacquire,
236 .action = imgselect,
237 .verb = "select",
238};
239
240/**
241 * The "imgselect" command
242 *
243 * @v argc Argument count
244 * @v argv Argument list
245 * @ret rc Return status code
246 */
247static int imgselect_exec ( int argc, char **argv ) {
248 return imgsingle_exec ( argc, argv, &imgselect_desc );
249}
250
251/** "imgexec" command descriptor */
253 COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
254 0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" );
255
256/**
257 * "imgexec" command action
258 *
259 * @v image Image
260 * @v opts Options
261 * @ret rc Return status code
262 */
263static int imgexec ( struct image *image, struct imgsingle_options *opts ) {
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}
285
286/** "imgexec" family command descriptor */
288 .cmd = &imgexec_cmd,
289 .acquire = imgacquire,
290 .action = imgexec,
291 .verb = "boot",
292};
293
294/**
295 * The "imgexec" command
296 *
297 * @v argc Argument count
298 * @v argv Argument list
299 * @ret rc Return status code
300 */
301static int imgexec_exec ( int argc, char **argv) {
302 return imgsingle_exec ( argc, argv, &imgexec_desc );
303}
304
305/** "imgargs" command descriptor */
307 COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
308 1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
309
310/** "imgargs" family command descriptor */
312 .cmd = &imgargs_cmd,
313 .acquire = imgacquire,
314 .preaction = image_clear_cmdline,
315};
316
317/**
318 * The "imgargs" command body
319 *
320 * @v argc Argument count
321 * @v argv Argument list
322 * @ret rc Return status code
323 */
324static int imgargs_exec ( int argc, char **argv ) {
325 return imgsingle_exec ( argc, argv, &imgargs_desc );
326}
327
328/** "img{multi}" options */
330
331/** "img{multi}" option list */
332static struct option_descriptor imgmulti_opts[] = {};
333
334/** "img{multi}" command descriptor */
337 "[<image>...]" );
338
339/**
340 * The "img{multi}" family of commands
341 *
342 * @v argc Argument count
343 * @v argv Argument list
344 * @v payload Function to execute on each image
345 * @ret rc Return status code
346 */
347static int imgmulti_exec ( int argc, char **argv,
348 void ( * payload ) ( struct image *image ) ) {
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}
378
379/**
380 * The "imgstat" command
381 *
382 * @v argc Argument count
383 * @v argv Argument list
384 * @ret rc Return status code
385 */
386static int imgstat_exec ( int argc, char **argv ) {
387 return imgmulti_exec ( argc, argv, imgstat );
388}
389
390/**
391 * The "imgfree" command
392 *
393 * @v argc Argument count
394 * @v argv Argument list
395 * @ret rc Return status code
396 */
397static int imgfree_exec ( int argc, char **argv ) {
398 return imgmulti_exec ( argc, argv, unregister_image );
399}
400
401/* "imgfetch" and synonyms */
402COMMAND ( imgfetch, imgfetch_exec );
405
406/* "imgselect" and synonyms */
410
411/* "imgexec" and synonyms */
415
416/* Other image management commands */
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
const char * name
Definition ath9k_hw.c:1986
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
void timeout(int)
uint32_t kernel
Kernel version (numeric).
Definition ena.h:9
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
Error codes.
char * concat_args(char **args)
Concatenate arguments.
Definition exec.c:359
void shell_stop(int stop)
Set shell stop state.
Definition exec.c:218
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
@ no_argument
Option does not take an argument.
Definition getopt.h:17
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOENT
No such file or directory.
Definition errno.h:558
#define ENOMEM
Not enough space.
Definition errno.h:578
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
struct image * find_image(const char *name)
Find image by name.
Definition image.c:408
void unregister_image(struct image *image)
Unregister executable image.
Definition image.c:390
int image_exec(struct image *image)
Execute image.
Definition image.c:446
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_replace(struct image *replacement)
Set replacement image.
Definition image.c:561
int image_set_name(struct image *image, const char *name)
Set image name.
Definition image.c:185
int image_select(struct image *image)
Select image for execution.
Definition image.c:597
Executable images.
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition image.h:203
struct image_tag selected_image
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition image.h:267
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition image.h:90
static int imgfetch_exec(int argc, char **argv)
The "imgfetch" command.
Definition image_cmd.c:211
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition image_cmd.c:335
static int imgstat_exec(int argc, char **argv)
The "imgstat" command.
Definition image_cmd.c:386
struct imgsingle_descriptor imgargs_desc
"imgargs" family command descriptor
Definition image_cmd.c:311
static int imgexec_exec(int argc, char **argv)
The "imgexec" command.
Definition image_cmd.c:301
struct option_descriptor imgsingle[4]
Definition image_cmd.c:66
struct imgsingle_descriptor imgfetch_desc
"imgfetch" family command descriptor
Definition image_cmd.c:199
static struct command_descriptor imgselect_cmd
"imgselect" command descriptor
Definition image_cmd.c:228
static int imgfree_exec(int argc, char **argv)
The "imgfree" command.
Definition image_cmd.c:397
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition image_cmd.c:252
static int imgselect(struct image *image, struct imgsingle_options *opts __unused)
"imgselect" command action
Definition image_cmd.c:222
struct imgsingle_descriptor imgselect_desc
"imgselect" family command descriptor
Definition image_cmd.c:233
static union @306150031363321274373032327065175057277367014027 opts
"img{single}" option list
static struct command_descriptor imgargs_cmd
"imgargs" command descriptor
Definition image_cmd.c:306
static struct command_descriptor imgfetch_cmd
"imgfetch" command descriptor
Definition image_cmd.c:194
static int imgargs_exec(int argc, char **argv)
The "imgargs" command body.
Definition image_cmd.c:324
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition image_cmd.c:108
struct imgsingle_descriptor imgexec_desc
"imgexec" family command descriptor
Definition image_cmd.c:287
struct option_descriptor imgexec[5]
Definition image_cmd.c:62
static int imgmulti_exec(int argc, char **argv, void(*payload)(struct image *image))
The "img{multi}" family of commands.
Definition image_cmd.c:347
static int imgselect_exec(int argc, char **argv)
The "imgselect" command.
Definition image_cmd.c:247
static struct option_descriptor imgmulti_opts[]
"img{multi}" option list
Definition image_cmd.c:332
int imgacquire(const char *name_uri, unsigned long timeout, int quiet, struct image **image)
Acquire an image.
Definition imgmgmt.c:148
void imgstat(struct image *image)
Display status of an image.
Definition imgmgmt.c:165
int imgdownload_string(const char *uri_string, unsigned long timeout, int quiet, struct image **image)
Download a new image.
Definition imgmgmt.c:125
Image management.
String functions.
unsigned long tmp
Definition linux_pci.h:65
uint32_t cmdline
Definition multiboot.h:4
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
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#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
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
Minimal command shell.
@ SHELL_STOP_COMMAND_SEQUENCE
Stop processing commands.
Definition shell.h:30
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A command descriptor.
Definition parseopt.h:78
An executable image.
Definition image.h:24
unsigned int flags
Flags.
Definition image.h:40
"img{multi}" options
Definition image_cmd.c:329
An "img{single}" family command descriptor.
Definition image_cmd.c:83
int(* acquire)(const char *name, unsigned long timeout, int quiet, struct image **image)
Function to use to acquire the image.
Definition image_cmd.c:87
struct command_descriptor * cmd
Command descriptor.
Definition image_cmd.c:85
int(* action)(struct image *image, struct imgsingle_options *opts)
Action to take upon image, or NULL.
Definition image_cmd.c:92
const char * verb
Verb to describe action.
Definition image_cmd.c:95
void(* preaction)(struct image *image)
Pre-action to take upon image, or NULL.
Definition image_cmd.c:90
"img{single}" options
Definition image_cmd.c:46
char * name
Image name.
Definition image_cmd.c:48
unsigned long timeout
Download timeout.
Definition image_cmd.c:50
int replace
Replace image.
Definition image_cmd.c:54
int autofree
Free image after execution.
Definition image_cmd.c:56
int quiet
Display only error messages.
Definition image_cmd.c:52
A command-line option descriptor.
Definition parseopt.h:24
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:485