You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sanboot_cmd.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <getopt.h>
  23. #include <ipxe/command.h>
  24. #include <ipxe/parseopt.h>
  25. #include <ipxe/uri.h>
  26. #include <ipxe/sanboot.h>
  27. #include <usr/autoboot.h>
  28. FILE_LICENCE ( GPL2_OR_LATER );
  29. /** @file
  30. *
  31. * SAN commands
  32. *
  33. */
  34. /** "sanboot" options */
  35. struct sanboot_options {
  36. /** Drive number */
  37. unsigned int drive;
  38. /** Do not describe SAN device */
  39. int no_describe;
  40. /** Keep SAN device */
  41. int keep;
  42. };
  43. /** "sanboot" option list */
  44. static struct option_descriptor sanboot_opts[] = {
  45. OPTION_DESC ( "drive", 'd', required_argument,
  46. struct sanboot_options, drive, parse_integer ),
  47. OPTION_DESC ( "no-describe", 'n', no_argument,
  48. struct sanboot_options, no_describe, parse_flag ),
  49. OPTION_DESC ( "keep", 'k', no_argument,
  50. struct sanboot_options, keep, parse_flag ),
  51. };
  52. /** "sanhook" command descriptor */
  53. static struct command_descriptor sanhook_cmd =
  54. COMMAND_DESC ( struct sanboot_options, sanboot_opts, 1, 1,
  55. "[--drive <drive>] [--no-describe] <root-path>" );
  56. /** "sanboot" command descriptor */
  57. static struct command_descriptor sanboot_cmd =
  58. COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 1,
  59. "[--drive <drive>] [--no-describe] [--keep] "
  60. "[<root-path>]" );
  61. /** "sanunhook" command descriptor */
  62. static struct command_descriptor sanunhook_cmd =
  63. COMMAND_DESC ( struct sanboot_options, sanboot_opts, 0, 0,
  64. "[--drive <drive>]" );
  65. /**
  66. * The "sanboot", "sanhook" and "sanunhook" commands
  67. *
  68. * @v argc Argument count
  69. * @v argv Argument list
  70. * @v default_flags Default set of flags for uriboot()
  71. * @v no_root_path_flags Additional flags to apply if no root path is present
  72. * @ret rc Return status code
  73. */
  74. static int sanboot_core_exec ( int argc, char **argv,
  75. struct command_descriptor *cmd,
  76. int default_flags, int no_root_path_flags ) {
  77. struct sanboot_options opts;
  78. const char *root_path;
  79. struct uri *uri;
  80. int flags;
  81. int rc;
  82. /* Initialise options */
  83. memset ( &opts, 0, sizeof ( opts ) );
  84. opts.drive = san_default_drive();
  85. /* Parse options */
  86. if ( ( rc = reparse_options ( argc, argv, cmd, &opts ) ) != 0 )
  87. goto err_parse_options;
  88. /* Parse root path, if present */
  89. if ( argc > optind ) {
  90. root_path = argv[optind];
  91. uri = parse_uri ( root_path );
  92. if ( ! uri ) {
  93. rc = -ENOMEM;
  94. goto err_parse_uri;
  95. }
  96. } else {
  97. root_path = NULL;
  98. uri = NULL;
  99. }
  100. /* Construct flags */
  101. flags = default_flags;
  102. if ( opts.no_describe )
  103. flags |= URIBOOT_NO_SAN_DESCRIBE;
  104. if ( opts.keep )
  105. flags |= URIBOOT_NO_SAN_UNHOOK;
  106. if ( ! root_path )
  107. flags |= no_root_path_flags;
  108. /* Boot from root path */
  109. if ( ( rc = uriboot ( NULL, uri, opts.drive, flags ) ) != 0 )
  110. goto err_uriboot;
  111. err_uriboot:
  112. uri_put ( uri );
  113. err_parse_uri:
  114. err_parse_options:
  115. return rc;
  116. }
  117. /**
  118. * The "sanhook" command
  119. *
  120. * @v argc Argument count
  121. * @v argv Argument list
  122. * @ret rc Return status code
  123. */
  124. static int sanhook_exec ( int argc, char **argv ) {
  125. return sanboot_core_exec ( argc, argv, &sanhook_cmd,
  126. ( URIBOOT_NO_SAN_BOOT |
  127. URIBOOT_NO_SAN_UNHOOK ), 0 );
  128. }
  129. /**
  130. * The "sanboot" command
  131. *
  132. * @v argc Argument count
  133. * @v argv Argument list
  134. * @ret rc Return status code
  135. */
  136. static int sanboot_exec ( int argc, char **argv ) {
  137. return sanboot_core_exec ( argc, argv, &sanboot_cmd,
  138. 0, URIBOOT_NO_SAN_UNHOOK );
  139. }
  140. /**
  141. * The "sanunhook" command
  142. *
  143. * @v argc Argument count
  144. * @v argv Argument list
  145. * @ret rc Return status code
  146. */
  147. static int sanunhook_exec ( int argc, char **argv ) {
  148. return sanboot_core_exec ( argc, argv, &sanunhook_cmd,
  149. ( URIBOOT_NO_SAN_DESCRIBE |
  150. URIBOOT_NO_SAN_BOOT ), 0 );
  151. }
  152. /** SAN commands */
  153. struct command sanboot_commands[] __command = {
  154. {
  155. .name = "sanhook",
  156. .exec = sanhook_exec,
  157. },
  158. {
  159. .name = "sanboot",
  160. .exec = sanboot_exec,
  161. },
  162. {
  163. .name = "sanunhook",
  164. .exec = sanunhook_exec,
  165. },
  166. };