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
34FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
35FILE_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 */
59};
60
61/** "sanboot" option list */
62static 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,
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 */
111static 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 */
176static int sanhook_exec ( int argc, char **argv ) {
177 return sanboot_core_exec ( argc, argv, &sanhook_cmd,
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 */
189static 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 */
201static 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 */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct golan_eqe_cmd cmd
Definition CIB_PRM.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
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
Automatic booting.
@ URIBOOT_NO_SAN_BOOT
Definition autoboot.h:23
@ URIBOOT_NO_SAN_DESCRIBE
Definition autoboot.h:22
@ URIBOOT_NO_SAN_UNHOOK
Definition autoboot.h:24
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
uint8_t flags
Flags.
Definition ena.h:7
Error codes.
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
uint8_t drive
Drive number.
Definition int13.h:5
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
iPXE sanboot API
uint8_t extra
Signature extra byte.
Definition smbios.h:6
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition parseopt.c:227
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition parseopt.c:402
int parse_string(char *text, char **value)
Parse string value.
Definition parseopt.c:74
int parse_uuid(char *text, struct uuid_option *uuid)
Parse UUID.
Definition parseopt.c:136
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition parseopt.c:92
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
unsigned int san_default_drive(void)
Get default SAN drive number.
Definition sanboot.c:973
static int sanhook_exec(int argc, char **argv)
The "sanhook" command.
static struct command_descriptor sanhook_cmd
"sanhook" command descriptor
Definition sanboot_cmd.c:89
static struct command_descriptor sanunhook_cmd
"sanunhook" command descriptor
Definition sanboot_cmd.c:99
struct option_descriptor sanunhook[1]
Definition sanboot_cmd.c:68
struct option_descriptor sanboot[7]
Definition sanboot_cmd.c:64
struct option_descriptor sanhook[2]
Definition sanboot_cmd.c:66
static struct command_descriptor sanboot_cmd
"sanboot" command descriptor
Definition sanboot_cmd.c:94
static int sanboot_exec(int argc, char **argv)
The "sanboot" command.
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.
static int sanunhook_exec(int argc, char **argv)
The "sanunhook" command.
A command descriptor.
Definition parseopt.h:78
A text label widget.
Definition label.h:16
A command-line option descriptor.
Definition parseopt.h:24
SAN boot configuration parameters.
Definition sanboot.h:111
const char * filename
Boot filename (or NULL to use default)
Definition sanboot.h:113
const char * label
Filesystem label (or NULL to ignore volume label)
Definition sanboot.h:117
union uuid * uuid
UUID (or NULL to ignore UUID)
Definition sanboot.h:119
const char * extra
Required extra filename (or NULL to ignore)
Definition sanboot.h:115
"sanboot" options
Definition sanboot_cmd.c:44
struct uuid_option uuid
UUID.
Definition sanboot_cmd.c:58
int no_describe
Do not describe SAN device.
Definition sanboot_cmd.c:48
unsigned int drive
Drive number.
Definition sanboot_cmd.c:46
char * extra
Required extra filename.
Definition sanboot_cmd.c:54
int keep
Keep SAN device.
Definition sanboot_cmd.c:50
char * filename
Boot filename.
Definition sanboot_cmd.c:52
char * label
Volume label.
Definition sanboot_cmd.c:56
A Uniform Resource Identifier.
Definition uri.h:65
A UUID command-line option.
Definition parseopt.h:131
A universally unique ID.
Definition uuid.h:16
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition uri.c:297
Uniform Resource Identifiers.
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition uri.h:206