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  /** Filename */
51  char *filename;
52 };
53 
54 /** "sanboot" option list */
55 static union {
56  /* "sanboot" takes all four options */
58  /* "sanhook" takes only --drive and --no-describe */
60  /* "sanunhook" takes only --drive */
62 } opts = {
63  .sanboot = {
64  OPTION_DESC ( "drive", 'd', required_argument,
66  OPTION_DESC ( "no-describe", 'n', no_argument,
67  struct sanboot_options, no_describe, parse_flag ),
68  OPTION_DESC ( "keep", 'k', no_argument,
69  struct sanboot_options, keep, parse_flag ),
70  OPTION_DESC ( "filename", 'f', required_argument,
71  struct sanboot_options, filename, parse_string ),
72  },
73 };
74 
75 
76 /** "sanhook" command descriptor */
78  COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, MAX_ARGUMENTS,
79  "<root-path>" );
80 
81 /** "sanboot" command descriptor */
83  COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, MAX_ARGUMENTS,
84  "[<root-path>]" );
85 
86 /** "sanunhook" command descriptor */
88  COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
89 
90 /**
91  * The "sanboot", "sanhook" and "sanunhook" commands
92  *
93  * @v argc Argument count
94  * @v argv Argument list
95  * @v default_flags Default set of flags for uriboot()
96  * @v no_root_path_flags Additional flags to apply if no root path is present
97  * @ret rc Return status code
98  */
99 static int sanboot_core_exec ( int argc, char **argv,
100  struct command_descriptor *cmd,
101  int default_flags, int no_root_path_flags ) {
102  struct sanboot_options opts;
103  struct uri *uris[argc];
104  int count;
105  int flags;
106  int i;
107  int rc;
108 
109  /* Initialise options */
110  memset ( &opts, 0, sizeof ( opts ) );
111  opts.drive = san_default_drive();
112 
113  /* Parse options */
114  if ( ( rc = reparse_options ( argc, argv, cmd, &opts ) ) != 0 )
115  goto err_parse_options;
116 
117  /* Parse root paths, if present */
118  count = ( argc - optind );
119  for ( i = 0 ; i < count ; i++ ) {
120  uris[i] = parse_uri ( argv[ optind + i ] );
121  if ( ! uris[i] ) {
122  rc = -ENOMEM;
123  goto err_parse_uri;
124  }
125  }
126 
127  /* Construct flags */
128  flags = default_flags;
129  if ( opts.no_describe )
131  if ( opts.keep )
133  if ( ! count )
134  flags |= no_root_path_flags;
135 
136  /* Boot from root path */
137  if ( ( rc = uriboot ( NULL, uris, count, opts.drive, opts.filename,
138  flags ) ) != 0 )
139  goto err_uriboot;
140 
141  err_uriboot:
142  i = count;
143  err_parse_uri:
144  for ( i-- ; i >= 0 ; i-- )
145  uri_put ( uris[i] );
146  err_parse_options:
147  return rc;
148 }
149 
150 /**
151  * The "sanhook" command
152  *
153  * @v argc Argument count
154  * @v argv Argument list
155  * @ret rc Return status code
156  */
157 static int sanhook_exec ( int argc, char **argv ) {
158  return sanboot_core_exec ( argc, argv, &sanhook_cmd,
160  URIBOOT_NO_SAN_UNHOOK ), 0 );
161 }
162 
163 /**
164  * The "sanboot" command
165  *
166  * @v argc Argument count
167  * @v argv Argument list
168  * @ret rc Return status code
169  */
170 static int sanboot_exec ( int argc, char **argv ) {
171  return sanboot_core_exec ( argc, argv, &sanboot_cmd,
173 }
174 
175 /**
176  * The "sanunhook" command
177  *
178  * @v argc Argument count
179  * @v argv Argument list
180  * @ret rc Return status code
181  */
182 static int sanunhook_exec ( int argc, char **argv ) {
183  return sanboot_core_exec ( argc, argv, &sanunhook_cmd,
185  URIBOOT_NO_SAN_BOOT ), 0 );
186 }
187 
188 /** SAN commands */
189 struct command sanboot_commands[] __command = {
190  {
191  .name = "sanhook",
192  .exec = sanhook_exec,
193  },
194  {
195  .name = "sanboot",
196  .exec = sanboot_exec,
197  },
198  {
199  .name = "sanunhook",
200  .exec = sanunhook_exec,
201  },
202 };
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:88
static struct command_descriptor sanboot_cmd
"sanboot" command descriptor
Definition: sanboot_cmd.c:82
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct option_descriptor sanhook[2]
Definition: sanboot_cmd.c:59
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:188
int optind
Current option index.
Definition: getopt.c:51
static struct command_descriptor sanunhook_cmd
"sanunhook" command descriptor
Definition: sanboot_cmd.c:87
static int sanboot_exec(int argc, char **argv)
The "sanboot" command.
Definition: sanboot_cmd.c:170
Error codes.
A command-line command.
Definition: command.h:9
struct command sanboot_commands [] __command
SAN commands.
Definition: sanboot_cmd.c:189
unsigned int san_default_drive(void)
Get default SAN drive number.
Definition: sanboot.c:956
struct option_descriptor sanunhook[1]
Definition: sanboot_cmd.c:61
uint8_t drive
Drive number.
Definition: int13.h:16
Automatic booting.
A command descriptor.
Definition: parseopt.h:76
Uniform Resource Identifiers.
int uriboot(struct uri *filename, struct uri **root_paths, unsigned int root_path_count, int drive, const char *san_filename, unsigned int flags)
Boot from filename and root-path URIs.
Definition: autoboot.c:125
static int sanhook_exec(int argc, char **argv)
The "sanhook" command.
Definition: sanboot_cmd.c:157
#define ENOMEM
Not enough space.
Definition: errno.h:534
Parse command-line options.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:70
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:364
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:200
"sanboot" options
Definition: sanboot_cmd.c:43
Command line option parsing.
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:182
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
struct option_descriptor sanboot[4]
Definition: sanboot_cmd.c:57
static struct command_descriptor sanhook_cmd
"sanhook" command descriptor
Definition: sanboot_cmd.c:77
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:66
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:22
char * filename
Filename.
Definition: sanboot_cmd.c:51
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:107
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A Uniform Resource Identifier.
Definition: uri.h:50
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:99
iPXE sanboot API
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
String functions.
static union @425 opts
"sanboot" option list
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition: uri.c:295
void * memset(void *dest, int character, size_t len) __nonnull
uint8_t flags
Flags.
Definition: ena.h:18