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