iPXE
sanboot_cmd.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 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 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <ipxe/uri.h>
31 #include <ipxe/sanboot.h>
32 #include <usr/autoboot.h>
33 
34 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
35 
36 /** @file
37  *
38  * SAN commands
39  *
40  */
41 
42 /** "sanboot" options */
44  /** Drive number */
45  unsigned int drive;
46  /** Do not describe SAN device */
48  /** Keep SAN device */
49  int keep;
50  /** Boot filename */
51  char *filename;
52  /** Required extra filename */
53  char *extra;
54  /** Volume label */
55  char *label;
56  /** UUID */
57  struct uuid_option uuid;
58 };
59 
60 /** "sanboot" option list */
61 static union {
62  /* "sanboot" takes all options */
64  /* "sanhook" takes only --drive and --no-describe */
66  /* "sanunhook" takes only --drive */
68 } opts = {
69  .sanboot = {
70  OPTION_DESC ( "drive", 'd', required_argument,
72  OPTION_DESC ( "no-describe", 'n', no_argument,
73  struct sanboot_options, no_describe, parse_flag ),
74  OPTION_DESC ( "keep", 'k', no_argument,
75  struct sanboot_options, keep, parse_flag ),
76  OPTION_DESC ( "filename", 'f', required_argument,
77  struct sanboot_options, filename, parse_string ),
78  OPTION_DESC ( "extra", 'e', required_argument,
80  OPTION_DESC ( "label", 'l', required_argument,
82  OPTION_DESC ( "uuid", 'u', required_argument,
83  struct sanboot_options, uuid, parse_uuid ),
84  },
85 };
86 
87 /** "sanhook" command descriptor */
89  COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, MAX_ARGUMENTS,
90  "<root-path>" );
91 
92 /** "sanboot" command descriptor */
94  COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, MAX_ARGUMENTS,
95  "[<root-path>]" );
96 
97 /** "sanunhook" command descriptor */
99  COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
100 
101 /**
102  * The "sanboot", "sanhook" and "sanunhook" commands
103  *
104  * @v argc Argument count
105  * @v argv Argument list
106  * @v default_flags Default set of flags for uriboot()
107  * @v no_root_path_flags Additional flags to apply if no root path is present
108  * @ret rc Return status code
109  */
110 static int sanboot_core_exec ( int argc, char **argv,
111  struct command_descriptor *cmd,
112  int default_flags, int no_root_path_flags ) {
113  struct sanboot_options opts;
114  struct san_boot_config config;
115  struct uri *uris[argc];
116  int count;
117  int flags;
118  int i;
119  int rc;
120 
121  /* Initialise options */
122  memset ( &opts, 0, sizeof ( opts ) );
123  opts.drive = san_default_drive();
124 
125  /* Parse options */
126  if ( ( rc = reparse_options ( argc, argv, cmd, &opts ) ) != 0 )
127  goto err_parse_options;
128 
129  /* Parse root paths, if present */
130  count = ( argc - optind );
131  for ( i = 0 ; i < count ; i++ ) {
132  uris[i] = parse_uri ( argv[ optind + i ] );
133  if ( ! uris[i] ) {
134  rc = -ENOMEM;
135  goto err_parse_uri;
136  }
137  }
138 
139  /* Construct configuration parameters */
140  config.filename = opts.filename;
141  config.extra = opts.extra;
142  config.label = opts.label;
143  config.uuid = opts.uuid.value;
144 
145  /* Construct flags */
146  flags = default_flags;
147  if ( opts.no_describe )
149  if ( opts.keep )
151  if ( ! count )
152  flags |= no_root_path_flags;
153 
154  /* Boot from root path */
155  if ( ( rc = uriboot ( NULL, uris, count, opts.drive, &config,
156  flags ) ) != 0 )
157  goto err_uriboot;
158 
159  err_uriboot:
160  i = count;
161  err_parse_uri:
162  for ( i-- ; i >= 0 ; i-- )
163  uri_put ( uris[i] );
164  err_parse_options:
165  return rc;
166 }
167 
168 /**
169  * The "sanhook" command
170  *
171  * @v argc Argument count
172  * @v argv Argument list
173  * @ret rc Return status code
174  */
175 static int sanhook_exec ( int argc, char **argv ) {
176  return sanboot_core_exec ( argc, argv, &sanhook_cmd,
178  URIBOOT_NO_SAN_UNHOOK ), 0 );
179 }
180 
181 /**
182  * The "sanboot" command
183  *
184  * @v argc Argument count
185  * @v argv Argument list
186  * @ret rc Return status code
187  */
188 static int sanboot_exec ( int argc, char **argv ) {
189  return sanboot_core_exec ( argc, argv, &sanboot_cmd,
191 }
192 
193 /**
194  * The "sanunhook" command
195  *
196  * @v argc Argument count
197  * @v argv Argument list
198  * @ret rc Return status code
199  */
200 static int sanunhook_exec ( int argc, char **argv ) {
201  return sanboot_core_exec ( argc, argv, &sanunhook_cmd,
203  URIBOOT_NO_SAN_BOOT ), 0 );
204 }
205 
206 /** SAN commands */
207 struct command sanboot_commands[] __command = {
208  {
209  .name = "sanhook",
210  .exec = sanhook_exec,
211  },
212  {
213  .name = "sanboot",
214  .exec = sanboot_exec,
215  },
216  {
217  .name = "sanunhook",
218  .exec = sanunhook_exec,
219  },
220 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:91
int uriboot(struct uri *filename, struct uri **root_paths, unsigned int root_path_count, int drive, struct san_boot_config *san_config, unsigned int flags)
Boot from filename and root-path URIs.
Definition: autoboot.c:129
static struct command_descriptor sanboot_cmd
"sanboot" command descriptor
Definition: sanboot_cmd.c:93
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct option_descriptor sanhook[2]
Definition: sanboot_cmd.c:65
A text label widget.
Definition: label.h:16
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
int optind
Current option index.
Definition: getopt.c:51
int parse_uuid(char *text, struct uuid_option *uuid)
Parse UUID.
Definition: parseopt.c:135
A UUID command-line option.
Definition: parseopt.h:130
static struct command_descriptor sanunhook_cmd
"sanunhook" command descriptor
Definition: sanboot_cmd.c:98
static int sanboot_exec(int argc, char **argv)
The "sanboot" command.
Definition: sanboot_cmd.c:188
Error codes.
A universally unique ID.
Definition: uuid.h:15
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
A command-line command.
Definition: command.h:9
struct command sanboot_commands [] __command
SAN commands.
Definition: sanboot_cmd.c:207
char * extra
Required extra filename.
Definition: sanboot_cmd.c:53
unsigned int san_default_drive(void)
Get default SAN drive number.
Definition: sanboot.c:970
struct option_descriptor sanunhook[1]
Definition: sanboot_cmd.c:67
uint8_t drive
Drive number.
Definition: int13.h:16
Automatic booting.
A command descriptor.
Definition: parseopt.h:77
Uniform Resource Identifiers.
static int sanhook_exec(int argc, char **argv)
The "sanhook" command.
Definition: sanboot_cmd.c:175
#define ENOMEM
Not enough space.
Definition: errno.h:534
char * label
Volume label.
Definition: sanboot_cmd.c:55
Parse command-line options.
struct option_descriptor sanboot[7]
Definition: sanboot_cmd.c:63
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:73
SAN boot configuration parameters.
Definition: sanboot.h:110
union uuid * uuid
UUID (or NULL to ignore UUID)
Definition: sanboot.h:118
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:97
static union @440 opts
"sanboot" option list
const char * filename
Boot filename (or NULL to use default)
Definition: sanboot.h:112
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:401
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:226
"sanboot" options
Definition: sanboot_cmd.c:43
Command line option parsing.
const char * label
Filesystem label (or NULL to ignore volume label)
Definition: sanboot.h:116
Option does not take an argument.
Definition: getopt.h:16
static int sanunhook_exec(int argc, char **argv)
The "sanunhook" command.
Definition: sanboot_cmd.c:200
int keep
Keep SAN device.
Definition: sanboot_cmd.c:49
unsigned int drive
Drive number.
Definition: sanboot_cmd.c:45
const char * name
Name of the command.
Definition: command.h:11
const char * extra
Required extra filename (or NULL to ignore)
Definition: sanboot.h:114
static struct command_descriptor sanhook_cmd
"sanhook" command descriptor
Definition: sanboot_cmd.c:88
#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
uint16_t count
Number of entries.
Definition: ena.h:22
A command-line option descriptor.
Definition: parseopt.h:23
char * filename
Boot filename.
Definition: sanboot_cmd.c:51
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A Uniform Resource Identifier.
Definition: uri.h:64
int no_describe
Do not describe SAN device.
Definition: sanboot_cmd.c:47
static int sanboot_core_exec(int argc, char **argv, struct command_descriptor *cmd, int default_flags, int no_root_path_flags)
The "sanboot", "sanhook" and "sanunhook" commands.
Definition: sanboot_cmd.c:110
iPXE sanboot API
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
String functions.
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:296
void * memset(void *dest, int character, size_t len) __nonnull
uint8_t flags
Flags.
Definition: ena.h:18