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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <getopt.h>
32 #include <ipxe/image.h>
33 #include <ipxe/command.h>
34 #include <ipxe/parseopt.h>
35 #include <ipxe/shell.h>
36 #include <usr/imgmgmt.h>
37 
38 /** @file
39  *
40  * Image management commands
41  *
42  */
43 
44 /** "img{single}" options */
46  /** Image name */
47  char *name;
48  /** Download timeout */
49  unsigned long timeout;
50  /** Replace image */
51  int replace;
52  /** Free image after execution */
53  int autofree;
54 };
55 
56 /** "img{single}" option list */
57 static union {
58  /* "imgexec" takes all three options */
60  /* Other "img{single}" commands take only --name, --timeout,
61  * and --autofree
62  */
64 } opts = {
65  .imgexec = {
66  OPTION_DESC ( "name", 'n', required_argument,
68  OPTION_DESC ( "timeout", 't', required_argument,
70  OPTION_DESC ( "autofree", 'a', no_argument,
71  struct imgsingle_options, autofree, parse_flag ),
72  OPTION_DESC ( "replace", 'r', no_argument,
73  struct imgsingle_options, replace, parse_flag ),
74  },
75 };
76 
77 /** An "img{single}" family command descriptor */
79  /** Command descriptor */
81  /** Function to use to acquire the image */
82  int ( * acquire ) ( const char *name, unsigned long timeout,
83  struct image **image );
84  /** Pre-action to take upon image, or NULL */
85  void ( * preaction ) ( struct image *image );
86  /** Action to take upon image, or NULL */
87  int ( * action ) ( struct image *image,
88  struct imgsingle_options *opts );
89  /** Verb to describe action */
90  const char *verb;
91 };
92 
93 /**
94  * The "img{single}" family of commands
95  *
96  * @v argc Argument count
97  * @v argv Argument list
98  * @v desc "img{single}" command descriptor
99  * @v action_name Action name (for error messages)
100  * @v action Action to take upon image
101  * @ret rc Return status code
102  */
103 static int imgsingle_exec ( int argc, char **argv,
104  struct imgsingle_descriptor *desc ) {
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 }
187 
188 /** "imgfetch" command descriptor */
190  COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
191  1, MAX_ARGUMENTS, "<uri> [<arguments>...]" );
192 
193 /** "imgfetch" family command descriptor */
195  .cmd = &imgfetch_cmd,
196  .acquire = imgdownload_string,
197 };
198 
199 /**
200  * The "imgfetch" command
201  *
202  * @v argc Argument count
203  * @v argv Argument list
204  * @ret rc Return status code
205  */
206 static int imgfetch_exec ( int argc, char **argv ) {
207  return imgsingle_exec ( argc, argv, &imgfetch_desc );
208 }
209 
210 /**
211  * "imgselect" command action
212  *
213  * @v image Image
214  * @v opts Options
215  * @ret rc Return status code
216  */
217 static int imgselect ( struct image *image,
218  struct imgsingle_options *opts __unused ) {
219  return image_select ( image );
220 }
221 
222 /** "imgselect" command descriptor */
224  COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
225  1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
226 
227 /** "imgselect" family command descriptor */
229  .cmd = &imgselect_cmd,
230  .acquire = imgacquire,
231  .action = imgselect,
232  .verb = "select",
233 };
234 
235 /**
236  * The "imgselect" command
237  *
238  * @v argc Argument count
239  * @v argv Argument list
240  * @ret rc Return status code
241  */
242 static int imgselect_exec ( int argc, char **argv ) {
243  return imgsingle_exec ( argc, argv, &imgselect_desc );
244 }
245 
246 /** "imgexec" command descriptor */
248  COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
249  0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" );
250 
251 /**
252  * "imgexec" command action
253  *
254  * @v image Image
255  * @v opts Options
256  * @ret rc Return status code
257  */
258 static int imgexec ( struct image *image, struct imgsingle_options *opts ) {
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 }
280 
281 /** "imgexec" family command descriptor */
283  .cmd = &imgexec_cmd,
284  .acquire = imgacquire,
285  .action = imgexec,
286  .verb = "boot",
287 };
288 
289 /**
290  * The "imgexec" command
291  *
292  * @v argc Argument count
293  * @v argv Argument list
294  * @ret rc Return status code
295  */
296 static int imgexec_exec ( int argc, char **argv) {
297  return imgsingle_exec ( argc, argv, &imgexec_desc );
298 }
299 
300 /** "imgargs" command descriptor */
302  COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
303  1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" );
304 
305 /** "imgargs" family command descriptor */
307  .cmd = &imgargs_cmd,
308  .acquire = imgacquire,
309  .preaction = image_clear_cmdline,
310 };
311 
312 /**
313  * The "imgargs" command body
314  *
315  * @v argc Argument count
316  * @v argv Argument list
317  * @ret rc Return status code
318  */
319 static int imgargs_exec ( int argc, char **argv ) {
320  return imgsingle_exec ( argc, argv, &imgargs_desc );
321 }
322 
323 /** "img{multi}" options */
325 
326 /** "img{multi}" option list */
327 static struct option_descriptor imgmulti_opts[] = {};
328 
329 /** "img{multi}" command descriptor */
332  "[<image>...]" );
333 
334 /**
335  * The "img{multi}" family of commands
336  *
337  * @v argc Argument count
338  * @v argv Argument list
339  * @v payload Function to execute on each image
340  * @ret rc Return status code
341  */
342 static int imgmulti_exec ( int argc, char **argv,
343  void ( * payload ) ( struct image *image ) ) {
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 }
373 
374 /**
375  * The "imgstat" command
376  *
377  * @v argc Argument count
378  * @v argv Argument list
379  * @ret rc Return status code
380  */
381 static int imgstat_exec ( int argc, char **argv ) {
382  return imgmulti_exec ( argc, argv, imgstat );
383 }
384 
385 /**
386  * The "imgfree" command
387  *
388  * @v argc Argument count
389  * @v argv Argument list
390  * @ret rc Return status code
391  */
392 static int imgfree_exec ( int argc, char **argv ) {
393  return imgmulti_exec ( argc, argv, unregister_image );
394 }
395 
396 /* "imgfetch" and synonyms */
397 COMMAND ( imgfetch, imgfetch_exec );
398 COMMAND ( module, imgfetch_exec );
399 COMMAND ( initrd, imgfetch_exec );
400 
401 /* "imgselect" and synonyms */
403 COMMAND ( imgload, imgselect_exec );
405 
406 /* "imgexec" and synonyms */
408 COMMAND ( chain, imgexec_exec );
409 COMMAND ( boot, imgexec_exec );
410 
411 /* Other image management commands */
412 COMMAND ( imgargs, imgargs_exec );
414 COMMAND ( imgfree, imgfree_exec );
static struct command_descriptor imgfetch_cmd
"imgfetch" command descriptor
Definition: image_cmd.c:189
struct imgsingle_descriptor imgargs_desc
"imgargs" family command descriptor
Definition: image_cmd.c:306
unsigned int flags
Flags.
Definition: image.h:39
struct image_tag selected_image
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int image_select(struct image *image)
Select image for execution.
Definition: image.c:564
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
Minimal command shell.
int optind
Current option index.
Definition: getopt.c:51
struct image * find_image(const char *name)
Find image by name.
Definition: image.c:375
Error codes.
An "img{single}" family command descriptor.
Definition: image_cmd.c:78
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:114
#define ENOENT
No such file or directory.
Definition: errno.h:514
struct imgsingle_descriptor imgfetch_desc
"imgfetch" family command descriptor
Definition: image_cmd.c:194
int autofree
Free image after execution.
Definition: image_cmd.c:53
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
COMMAND(imgfetch, imgfetch_exec)
An executable image.
Definition: image.h:23
"img{multi}" options
Definition: image_cmd.c:324
A command descriptor.
Definition: parseopt.h:77
void shell_stop(int stop)
Set shell stop state.
Definition: exec.c:217
struct imgsingle_descriptor imgselect_desc
"imgselect" family command descriptor
Definition: image_cmd.c:228
static int imgselect_exec(int argc, char **argv)
The "imgselect" command.
Definition: image_cmd.c:242
#define IMAGE_AUTO_UNREGISTER
Image will be automatically unregistered after execution.
Definition: image.h:82
int image_exec(struct image *image)
Execute image.
Definition: image.c:413
struct image * find_image_tag(struct image_tag *tag)
Find image by tag.
Definition: image.c:392
unsigned long tmp
Definition: linux_pci.h:64
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct option_descriptor imgmulti_opts[]
"img{multi}" option list
Definition: image_cmd.c:327
static int imgmulti_exec(int argc, char **argv, void(*payload)(struct image *image))
The "img{multi}" family of commands.
Definition: image_cmd.c:342
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
Executable images.
struct option_descriptor imgsingle[3]
Definition: image_cmd.c:63
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static int imgexec_exec(int argc, char **argv)
The "imgexec" command.
Definition: image_cmd.c:296
void imgstat(struct image *image)
Display status of an image.
Definition: imgmgmt.c:159
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
struct option_descriptor imgexec[4]
Definition: image_cmd.c:59
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:358
uint32_t kernel
Kernel version (numeric)
Definition: ena.h:20
static int imgsingle_exec(int argc, char **argv, struct imgsingle_descriptor *desc)
The "img{single}" family of commands.
Definition: image_cmd.c:103
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static int imgargs_exec(int argc, char **argv)
The "imgargs" command body.
Definition: image_cmd.c:319
int image_replace(struct image *replacement)
Set replacement image.
Definition: image.c:528
#define for_each_image_safe(image, tmp)
Iterate over all registered images, safe against deletion.
Definition: image.h:194
int image_set_name(struct image *image, const char *name)
Set image name.
Definition: image.c:180
Command line option parsing.
static struct command_descriptor imgargs_cmd
"imgargs" command descriptor
Definition: image_cmd.c:301
struct command_descriptor * cmd
Command descriptor.
Definition: image_cmd.c:80
void(* preaction)(struct image *image)
Pre-action to take upon image, or NULL.
Definition: image_cmd.c:85
static struct command_descriptor imgexec_cmd
"imgexec" command descriptor
Definition: image_cmd.c:247
Option does not take an argument.
Definition: getopt.h:16
int(* action)(struct image *image, struct imgsingle_options *opts)
Action to take upon image, or NULL.
Definition: image_cmd.c:87
static int imgselect(struct image *image, struct imgsingle_options *opts __unused)
"imgselect" command action
Definition: image_cmd.c:217
static int imgstat_exec(int argc, char **argv)
The "imgstat" command.
Definition: image_cmd.c:381
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:357
unsigned long timeout
Download timeout.
Definition: image_cmd.c:49
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Image management.
static struct command_descriptor imgmulti_cmd
"img{multi}" command descriptor
Definition: image_cmd.c:330
int imgdownload_string(const char *uri_string, unsigned long timeout, struct image **image)
Download a new image.
Definition: imgmgmt.c:120
#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
Stop processing commands.
Definition: shell.h:29
A command-line option descriptor.
Definition: parseopt.h:23
static void image_clear_cmdline(struct image *image)
Clear image command line.
Definition: image.h:258
struct imgsingle_descriptor imgexec_desc
"imgexec" family command descriptor
Definition: image_cmd.c:282
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
int image_set_cmdline(struct image *image, const char *cmdline)
Set image command line.
Definition: image.c:225
void timeout(int)
int replace
Replace image.
Definition: image_cmd.c:51
"img{single}" options
Definition: image_cmd.c:45
uint32_t cmdline
Definition: multiboot.h:16
const char * verb
Verb to describe action.
Definition: image_cmd.c:90
int(* acquire)(const char *name, unsigned long timeout, struct image **image)
Function to use to acquire the image.
Definition: image_cmd.c:82
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static struct command_descriptor imgselect_cmd
"imgselect" command descriptor
Definition: image_cmd.c:223
String functions.
static int imgfetch_exec(int argc, char **argv)
The "imgfetch" command.
Definition: image_cmd.c:206
char * name
Image name.
Definition: image_cmd.c:47
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:142
static union @448 opts
"img{single}" option list
static int imgfree_exec(int argc, char **argv)
The "imgfree" command.
Definition: image_cmd.c:392