These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / sanboot_cmd.c
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 */
43 struct sanboot_options {
44         /** Drive number */
45         unsigned int drive;
46         /** Do not describe SAN device */
47         int no_describe;
48         /** Keep SAN device */
49         int keep;
50 };
51
52 /** "sanboot" option list */
53 static union {
54         /* "sanboot" takes all three options */
55         struct option_descriptor sanboot[3];
56         /* "sanhook" takes only --drive and --no-describe */
57         struct option_descriptor sanhook[2];
58         /* "sanunhook" takes only --drive */
59         struct option_descriptor sanunhook[1];
60 } opts = {
61         .sanboot = {
62                 OPTION_DESC ( "drive", 'd', required_argument,
63                               struct sanboot_options, drive, parse_integer ),
64                 OPTION_DESC ( "no-describe", 'n', no_argument,
65                               struct sanboot_options, no_describe, parse_flag ),
66                 OPTION_DESC ( "keep", 'k', no_argument,
67                               struct sanboot_options, keep, parse_flag ),
68         },
69 };
70
71
72 /** "sanhook" command descriptor */
73 static struct command_descriptor sanhook_cmd =
74         COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, 1,
75                        "<root-path>" );
76
77 /** "sanboot" command descriptor */
78 static struct command_descriptor sanboot_cmd =
79         COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, 1,
80                        "[<root-path>]" );
81
82 /** "sanunhook" command descriptor */
83 static struct command_descriptor sanunhook_cmd =
84         COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
85
86 /**
87  * The "sanboot", "sanhook" and "sanunhook" commands
88  *
89  * @v argc              Argument count
90  * @v argv              Argument list
91  * @v default_flags     Default set of flags for uriboot()
92  * @v no_root_path_flags Additional flags to apply if no root path is present
93  * @ret rc              Return status code
94  */
95 static int sanboot_core_exec ( int argc, char **argv,
96                                struct command_descriptor *cmd,
97                                int default_flags, int no_root_path_flags ) {
98         struct sanboot_options opts;
99         const char *root_path;
100         struct uri *uri;
101         int flags;
102         int rc;
103
104         /* Initialise options */
105         memset ( &opts, 0, sizeof ( opts ) );
106         opts.drive = san_default_drive();
107
108         /* Parse options */
109         if ( ( rc = reparse_options ( argc, argv, cmd, &opts ) ) != 0 )
110                 goto err_parse_options;
111
112         /* Parse root path, if present */
113         if ( argc > optind ) {
114                 root_path = argv[optind];
115                 uri = parse_uri ( root_path );
116                 if ( ! uri ) {
117                         rc = -ENOMEM;
118                         goto err_parse_uri;
119                 }
120         } else {
121                 root_path = NULL;
122                 uri = NULL;
123         }
124
125         /* Construct flags */
126         flags = default_flags;
127         if ( opts.no_describe )
128                 flags |= URIBOOT_NO_SAN_DESCRIBE;
129         if ( opts.keep )
130                 flags |= URIBOOT_NO_SAN_UNHOOK;
131         if ( ! root_path )
132                 flags |= no_root_path_flags;
133
134         /* Boot from root path */
135         if ( ( rc = uriboot ( NULL, uri, opts.drive, flags ) ) != 0 )
136                 goto err_uriboot;
137
138  err_uriboot:
139         uri_put ( uri );
140  err_parse_uri:
141  err_parse_options:
142         return rc;
143 }
144
145 /**
146  * The "sanhook" command
147  *
148  * @v argc              Argument count
149  * @v argv              Argument list
150  * @ret rc              Return status code
151  */
152 static int sanhook_exec ( int argc, char **argv ) {
153         return sanboot_core_exec ( argc, argv, &sanhook_cmd,
154                                    ( URIBOOT_NO_SAN_BOOT |
155                                      URIBOOT_NO_SAN_UNHOOK ), 0 );
156 }
157
158 /**
159  * The "sanboot" command
160  *
161  * @v argc              Argument count
162  * @v argv              Argument list
163  * @ret rc              Return status code
164  */
165 static int sanboot_exec ( int argc, char **argv ) {
166         return sanboot_core_exec ( argc, argv, &sanboot_cmd,
167                                    0, URIBOOT_NO_SAN_UNHOOK );
168 }
169
170 /**
171  * The "sanunhook" command
172  *
173  * @v argc              Argument count
174  * @v argv              Argument list
175  * @ret rc              Return status code
176  */
177 static int sanunhook_exec ( int argc, char **argv ) {
178         return sanboot_core_exec ( argc, argv, &sanunhook_cmd,
179                                    ( URIBOOT_NO_SAN_DESCRIBE |
180                                      URIBOOT_NO_SAN_BOOT ), 0 );
181 }
182
183 /** SAN commands */
184 struct command sanboot_commands[] __command = {
185         {
186                 .name = "sanhook",
187                 .exec = sanhook_exec,
188         },
189         {
190                 .name = "sanboot",
191                 .exec = sanboot_exec,
192         },
193         {
194                 .name = "sanunhook",
195                 .exec = sanunhook_exec,
196         },
197 };