Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sanboot_cmd.c 4.6KB

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