Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sanboot_cmd.c 5.0KB

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