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.

autoboot.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2006 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 <string.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <gpxe/netdevice.h>
  22. #include <gpxe/dhcp.h>
  23. #include <gpxe/settings.h>
  24. #include <gpxe/image.h>
  25. #include <gpxe/sanboot.h>
  26. #include <gpxe/uri.h>
  27. #include <usr/ifmgmt.h>
  28. #include <usr/route.h>
  29. #include <usr/dhcpmgmt.h>
  30. #include <usr/imgmgmt.h>
  31. #include <usr/autoboot.h>
  32. /** @file
  33. *
  34. * Automatic booting
  35. *
  36. */
  37. /** Time to wait for link-up */
  38. #define LINK_WAIT_MS 15000
  39. /** Shutdown flags for exit */
  40. int shutdown_exit_flags = 0;
  41. /* SAN boot protocols */
  42. static struct sanboot_protocol sanboot_protocols[0] \
  43. __table_start ( struct sanboot_protocol, sanboot_protocols );
  44. static struct sanboot_protocol sanboot_protocols_end[0] \
  45. __table_end ( struct sanboot_protocol, sanboot_protocols );
  46. /**
  47. * Identify the boot network device
  48. *
  49. * @ret netdev Boot network device
  50. */
  51. static struct net_device * find_boot_netdev ( void ) {
  52. return NULL;
  53. }
  54. /**
  55. * Boot using next-server and filename
  56. *
  57. * @v filename Boot filename
  58. * @ret rc Return status code
  59. */
  60. int boot_next_server_and_filename ( struct in_addr next_server,
  61. const char *filename ) {
  62. struct uri *uri;
  63. struct image *image;
  64. char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
  65. int filename_is_absolute;
  66. int rc;
  67. /* Construct URI */
  68. uri = parse_uri ( filename );
  69. if ( ! uri ) {
  70. printf ( "Out of memory\n" );
  71. return -ENOMEM;
  72. }
  73. filename_is_absolute = uri_is_absolute ( uri );
  74. uri_put ( uri );
  75. if ( ! filename_is_absolute ) {
  76. /* Construct a tftp:// URI for the filename. We can't
  77. * just rely on the current working URI, because the
  78. * relative URI resolution will remove the distinction
  79. * between filenames with and without initial slashes,
  80. * which is significant for TFTP.
  81. */
  82. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  83. inet_ntoa ( next_server ), filename );
  84. filename = buf;
  85. }
  86. image = alloc_image();
  87. if ( ! image ) {
  88. printf ( "Out of memory\n" );
  89. return -ENOMEM;
  90. }
  91. if ( ( rc = imgfetch ( image, filename,
  92. register_and_autoload_image ) ) != 0 ) {
  93. printf ( "Could not load %s: %s\n",
  94. filename, strerror ( rc ) );
  95. goto done;
  96. }
  97. if ( ( rc = imgexec ( image ) ) != 0 ) {
  98. printf ( "Could not boot %s: %s\n",
  99. filename, strerror ( rc ) );
  100. goto done;
  101. }
  102. done:
  103. image_put ( image );
  104. return rc;
  105. }
  106. /**
  107. * Boot using root path
  108. *
  109. * @v root_path Root path
  110. * @ret rc Return status code
  111. */
  112. int boot_root_path ( const char *root_path ) {
  113. struct sanboot_protocol *sanboot;
  114. /* Quick hack */
  115. for ( sanboot = sanboot_protocols ;
  116. sanboot < sanboot_protocols_end ; sanboot++ ) {
  117. if ( strncmp ( root_path, sanboot->prefix,
  118. strlen ( sanboot->prefix ) ) == 0 ) {
  119. return sanboot->boot ( root_path );
  120. }
  121. }
  122. return -ENOTSUP;
  123. }
  124. /**
  125. * Boot from a network device
  126. *
  127. * @v netdev Network device
  128. * @ret rc Return status code
  129. */
  130. static int netboot ( struct net_device *netdev ) {
  131. struct setting vendor_class_id_setting
  132. = { .tag = DHCP_VENDOR_CLASS_ID };
  133. struct setting pxe_discovery_control_setting
  134. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  135. struct setting pxe_boot_menu_setting
  136. = { .tag = DHCP_PXE_BOOT_MENU };
  137. char buf[256];
  138. struct in_addr next_server;
  139. unsigned int pxe_discovery_control;
  140. int rc;
  141. /* Open device and display device status */
  142. if ( ( rc = ifopen ( netdev ) ) != 0 )
  143. return rc;
  144. ifstat ( netdev );
  145. /* Wait for link-up */
  146. printf ( "Waiting for link-up on %s...", netdev->name );
  147. if ( ( rc = iflinkwait ( netdev, LINK_WAIT_MS ) ) != 0 ) {
  148. printf ( " no link detected\n" );
  149. return rc;
  150. }
  151. printf ( " ok\n" );
  152. /* Configure device via DHCP */
  153. if ( ( rc = dhcp ( netdev ) ) != 0 )
  154. return rc;
  155. route();
  156. /* Try PXE menu boot, if applicable */
  157. fetch_string_setting ( NULL, &vendor_class_id_setting,
  158. buf, sizeof ( buf ) );
  159. pxe_discovery_control =
  160. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  161. if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  162. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  163. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  164. setting_exists ( NULL, &filename_setting ) ) ) ) {
  165. printf ( "Booting from PXE menu\n" );
  166. return pxe_menu_boot ( netdev );
  167. }
  168. /* Try to download and boot whatever we are given as a filename */
  169. fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
  170. fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
  171. if ( buf[0] ) {
  172. printf ( "Booting from filename \"%s\"\n", buf );
  173. return boot_next_server_and_filename ( next_server, buf );
  174. }
  175. /* No filename; try the root path */
  176. fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
  177. if ( buf[0] ) {
  178. printf ( "Booting from root path \"%s\"\n", buf );
  179. return boot_root_path ( buf );
  180. }
  181. printf ( "No filename or root path specified\n" );
  182. return -ENOENT;
  183. }
  184. /**
  185. * Close all open net devices
  186. *
  187. * Called before a fresh boot attempt in order to free up memory. We
  188. * don't just close the device immediately after the boot fails,
  189. * because there may still be TCP connections in the process of
  190. * closing.
  191. */
  192. static void close_all_netdevs ( void ) {
  193. struct net_device *netdev;
  194. for_each_netdev ( netdev ) {
  195. ifclose ( netdev );
  196. }
  197. }
  198. /**
  199. * Boot the system
  200. */
  201. void autoboot ( void ) {
  202. struct net_device *boot_netdev;
  203. struct net_device *netdev;
  204. /* If we have an identifable boot device, try that first */
  205. close_all_netdevs();
  206. if ( ( boot_netdev = find_boot_netdev() ) )
  207. netboot ( boot_netdev );
  208. /* If that fails, try booting from any of the other devices */
  209. for_each_netdev ( netdev ) {
  210. if ( netdev == boot_netdev )
  211. continue;
  212. close_all_netdevs();
  213. netboot ( netdev );
  214. }
  215. printf ( "No more network devices\n" );
  216. }