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.2KB

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