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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/embedded.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/iscsiboot.h>
  32. #include <usr/aoeboot.h>
  33. #include <usr/autoboot.h>
  34. /** @file
  35. *
  36. * Automatic booting
  37. *
  38. */
  39. /** Time to wait for link-up */
  40. #define LINK_WAIT_MS 15000
  41. /** Shutdown flags for exit */
  42. int shutdown_exit_flags = 0;
  43. /**
  44. * Identify the boot network device
  45. *
  46. * @ret netdev Boot network device
  47. */
  48. static struct net_device * find_boot_netdev ( void ) {
  49. return NULL;
  50. }
  51. /**
  52. * Boot embedded image
  53. *
  54. * @ret rc Return status code
  55. */
  56. static int boot_embedded_image ( void ) {
  57. struct image *image;
  58. int rc;
  59. image = embedded_image();
  60. if ( !image )
  61. return ENOENT;
  62. if ( ( rc = imgload ( image ) ) != 0 ) {
  63. printf ( "Could not load embedded image: %s\n",
  64. strerror ( rc ) );
  65. } else if ( ( rc = imgexec ( image ) ) != 0 ) {
  66. printf ( "Could not boot embedded image: %s\n",
  67. strerror ( rc ) );
  68. }
  69. image_put ( image );
  70. return rc;
  71. }
  72. /**
  73. * Boot using next-server and filename
  74. *
  75. * @v filename Boot filename
  76. * @ret rc Return status code
  77. */
  78. static int boot_next_server_and_filename ( struct in_addr next_server,
  79. const char *filename ) {
  80. struct uri *uri;
  81. struct image *image;
  82. char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
  83. int filename_is_absolute;
  84. int rc;
  85. /* Construct URI */
  86. uri = parse_uri ( filename );
  87. if ( ! uri ) {
  88. printf ( "Out of memory\n" );
  89. return -ENOMEM;
  90. }
  91. filename_is_absolute = uri_is_absolute ( uri );
  92. uri_put ( uri );
  93. if ( ! filename_is_absolute ) {
  94. /* Construct a tftp:// URI for the filename. We can't
  95. * just rely on the current working URI, because the
  96. * relative URI resolution will remove the distinction
  97. * between filenames with and without initial slashes,
  98. * which is significant for TFTP.
  99. */
  100. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  101. inet_ntoa ( next_server ), filename );
  102. filename = buf;
  103. }
  104. image = alloc_image();
  105. if ( ! image ) {
  106. printf ( "Out of memory\n" );
  107. return -ENOMEM;
  108. }
  109. if ( ( rc = imgfetch ( image, filename,
  110. register_and_autoload_image ) ) != 0 ) {
  111. printf ( "Could not load %s: %s\n",
  112. filename, strerror ( rc ) );
  113. goto done;
  114. }
  115. if ( ( rc = imgexec ( image ) ) != 0 ) {
  116. printf ( "Could not boot %s: %s\n",
  117. filename, strerror ( rc ) );
  118. goto done;
  119. }
  120. done:
  121. image_put ( image );
  122. return rc;
  123. }
  124. /**
  125. * Boot using root path
  126. *
  127. * @v root_path Root path
  128. * @ret rc Return status code
  129. */
  130. int boot_root_path ( const char *root_path ) {
  131. /* Quick hack */
  132. if ( strncmp ( root_path, "iscsi:", 6 ) == 0 ) {
  133. return iscsiboot ( root_path );
  134. } else if ( strncmp ( root_path, "aoe:", 4 ) == 0 ) {
  135. return aoeboot ( root_path );
  136. }
  137. return -ENOTSUP;
  138. }
  139. /**
  140. * Boot from a network device
  141. *
  142. * @v netdev Network device
  143. * @ret rc Return status code
  144. */
  145. static int netboot ( struct net_device *netdev ) {
  146. char buf[256];
  147. struct in_addr next_server;
  148. int rc;
  149. /* Open device and display device status */
  150. if ( ( rc = ifopen ( netdev ) ) != 0 )
  151. return rc;
  152. ifstat ( netdev );
  153. /* Wait for link-up */
  154. printf ( "Waiting for link-up on %s...", netdev->name );
  155. if ( ( rc = iflinkwait ( netdev, LINK_WAIT_MS ) ) != 0 ) {
  156. printf ( " no link detected\n" );
  157. return rc;
  158. }
  159. printf ( " ok\n" );
  160. /* Configure device via DHCP */
  161. if ( ( rc = dhcp ( netdev ) ) != 0 )
  162. return rc;
  163. route();
  164. /* Try to boot an embedded image if we have one */
  165. rc = boot_embedded_image ();
  166. if ( rc != ENOENT )
  167. return rc;
  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. }