Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

sanboot_cmd.c 4.7KB

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