iPXE
sanboot_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <errno.h>
00027 #include <getopt.h>
00028 #include <ipxe/command.h>
00029 #include <ipxe/parseopt.h>
00030 #include <ipxe/uri.h>
00031 #include <ipxe/sanboot.h>
00032 #include <usr/autoboot.h>
00033 
00034 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00035 
00036 /** @file
00037  *
00038  * SAN commands
00039  *
00040  */
00041 
00042 /** "sanboot" options */
00043 struct sanboot_options {
00044         /** Drive number */
00045         unsigned int drive;
00046         /** Do not describe SAN device */
00047         int no_describe;
00048         /** Keep SAN device */
00049         int keep;
00050         /** Filename */
00051         char *filename;
00052 };
00053 
00054 /** "sanboot" option list */
00055 static union {
00056         /* "sanboot" takes all four options */
00057         struct option_descriptor sanboot[4];
00058         /* "sanhook" takes only --drive and --no-describe */
00059         struct option_descriptor sanhook[2];
00060         /* "sanunhook" takes only --drive */
00061         struct option_descriptor sanunhook[1];
00062 } opts = {
00063         .sanboot = {
00064                 OPTION_DESC ( "drive", 'd', required_argument,
00065                               struct sanboot_options, drive, parse_integer ),
00066                 OPTION_DESC ( "no-describe", 'n', no_argument,
00067                               struct sanboot_options, no_describe, parse_flag ),
00068                 OPTION_DESC ( "keep", 'k', no_argument,
00069                               struct sanboot_options, keep, parse_flag ),
00070                 OPTION_DESC ( "filename", 'f', required_argument,
00071                               struct sanboot_options, filename, parse_string ),
00072         },
00073 };
00074 
00075 
00076 /** "sanhook" command descriptor */
00077 static struct command_descriptor sanhook_cmd =
00078         COMMAND_DESC ( struct sanboot_options, opts.sanhook, 1, MAX_ARGUMENTS,
00079                        "<root-path>" );
00080 
00081 /** "sanboot" command descriptor */
00082 static struct command_descriptor sanboot_cmd =
00083         COMMAND_DESC ( struct sanboot_options, opts.sanboot, 0, MAX_ARGUMENTS,
00084                        "[<root-path>]" );
00085 
00086 /** "sanunhook" command descriptor */
00087 static struct command_descriptor sanunhook_cmd =
00088         COMMAND_DESC ( struct sanboot_options, opts.sanunhook, 0, 0, NULL );
00089 
00090 /**
00091  * The "sanboot", "sanhook" and "sanunhook" commands
00092  *
00093  * @v argc              Argument count
00094  * @v argv              Argument list
00095  * @v default_flags     Default set of flags for uriboot()
00096  * @v no_root_path_flags Additional flags to apply if no root path is present
00097  * @ret rc              Return status code
00098  */
00099 static int sanboot_core_exec ( int argc, char **argv,
00100                                struct command_descriptor *cmd,
00101                                int default_flags, int no_root_path_flags ) {
00102         struct sanboot_options opts;
00103         struct uri *uris[argc];
00104         int count;
00105         int flags;
00106         int i;
00107         int rc;
00108 
00109         /* Initialise options */
00110         memset ( &opts, 0, sizeof ( opts ) );
00111         opts.drive = san_default_drive();
00112 
00113         /* Parse options */
00114         if ( ( rc = reparse_options ( argc, argv, cmd, &opts ) ) != 0 )
00115                 goto err_parse_options;
00116 
00117         /* Parse root paths, if present */
00118         count = ( argc - optind );
00119         for ( i = 0 ; i < count ; i++ ) {
00120                 uris[i] = parse_uri ( argv[ optind + i ] );
00121                 if ( ! uris[i] ) {
00122                         rc = -ENOMEM;
00123                         goto err_parse_uri;
00124                 }
00125         }
00126 
00127         /* Construct flags */
00128         flags = default_flags;
00129         if ( opts.no_describe )
00130                 flags |= URIBOOT_NO_SAN_DESCRIBE;
00131         if ( opts.keep )
00132                 flags |= URIBOOT_NO_SAN_UNHOOK;
00133         if ( ! count )
00134                 flags |= no_root_path_flags;
00135 
00136         /* Boot from root path */
00137         if ( ( rc = uriboot ( NULL, uris, count, opts.drive, opts.filename,
00138                               flags ) ) != 0 )
00139                 goto err_uriboot;
00140 
00141  err_uriboot:
00142         i = count;
00143  err_parse_uri:
00144         for ( i-- ; i >= 0 ; i-- )
00145                 uri_put ( uris[i] );
00146  err_parse_options:
00147         return rc;
00148 }
00149 
00150 /**
00151  * The "sanhook" command
00152  *
00153  * @v argc              Argument count
00154  * @v argv              Argument list
00155  * @ret rc              Return status code
00156  */
00157 static int sanhook_exec ( int argc, char **argv ) {
00158         return sanboot_core_exec ( argc, argv, &sanhook_cmd,
00159                                    ( URIBOOT_NO_SAN_BOOT |
00160                                      URIBOOT_NO_SAN_UNHOOK ), 0 );
00161 }
00162 
00163 /**
00164  * The "sanboot" command
00165  *
00166  * @v argc              Argument count
00167  * @v argv              Argument list
00168  * @ret rc              Return status code
00169  */
00170 static int sanboot_exec ( int argc, char **argv ) {
00171         return sanboot_core_exec ( argc, argv, &sanboot_cmd,
00172                                    0, URIBOOT_NO_SAN_UNHOOK );
00173 }
00174 
00175 /**
00176  * The "sanunhook" command
00177  *
00178  * @v argc              Argument count
00179  * @v argv              Argument list
00180  * @ret rc              Return status code
00181  */
00182 static int sanunhook_exec ( int argc, char **argv ) {
00183         return sanboot_core_exec ( argc, argv, &sanunhook_cmd,
00184                                    ( URIBOOT_NO_SAN_DESCRIBE |
00185                                      URIBOOT_NO_SAN_BOOT ), 0 );
00186 }
00187 
00188 /** SAN commands */
00189 struct command sanboot_commands[] __command = {
00190         {
00191                 .name = "sanhook",
00192                 .exec = sanhook_exec,
00193         },
00194         {
00195                 .name = "sanboot",
00196                 .exec = sanboot_exec,
00197         },
00198         {
00199                 .name = "sanunhook",
00200                 .exec = sanunhook_exec,
00201         },
00202 };