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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/image.h>
  24. #include <gpxe/embedded.h>
  25. #include <usr/ifmgmt.h>
  26. #include <usr/route.h>
  27. #include <usr/dhcpmgmt.h>
  28. #include <usr/imgmgmt.h>
  29. #include <usr/iscsiboot.h>
  30. #include <usr/aoeboot.h>
  31. #include <usr/autoboot.h>
  32. /** @file
  33. *
  34. * Automatic booting
  35. *
  36. */
  37. /**
  38. * Identify the boot network device
  39. *
  40. * @ret netdev Boot network device
  41. */
  42. static struct net_device * find_boot_netdev ( void ) {
  43. return NULL;
  44. }
  45. /**
  46. * Boot embedded image
  47. *
  48. * @ret rc Return status code
  49. */
  50. static int boot_embedded_image ( void ) {
  51. struct image *image;
  52. int rc;
  53. image = embedded_image();
  54. if ( !image )
  55. return ENOENT;
  56. if ( ( rc = imgload ( image ) ) != 0 ) {
  57. printf ( "Could not load embedded image: %s\n",
  58. strerror ( rc ) );
  59. } else if ( ( rc = imgexec ( image ) ) != 0 ) {
  60. printf ( "Could not boot embedded image: %s\n",
  61. strerror ( rc ) );
  62. }
  63. image_put ( image );
  64. return rc;
  65. }
  66. /**
  67. * Boot using filename
  68. *
  69. * @v filename Boot filename
  70. * @ret rc Return status code
  71. */
  72. static int boot_filename ( const char *filename ) {
  73. struct image *image;
  74. int rc;
  75. image = alloc_image();
  76. if ( ! image ) {
  77. printf ( "Out of memory\n" );
  78. return -ENOMEM;
  79. }
  80. if ( ( rc = imgfetch ( image, filename,
  81. register_and_autoload_image ) ) != 0 ) {
  82. printf ( "Could not load %s: %s\n",
  83. filename, strerror ( rc ) );
  84. goto done;
  85. }
  86. if ( ( rc = imgexec ( image ) ) != 0 ) {
  87. printf ( "Could not boot %s: %s\n",
  88. filename, strerror ( rc ) );
  89. goto done;
  90. }
  91. done:
  92. image_put ( image );
  93. return rc;
  94. }
  95. /**
  96. * Boot using root path
  97. *
  98. * @v root_path Root path
  99. * @ret rc Return status code
  100. */
  101. static int boot_root_path ( const char *root_path ) {
  102. /* Quick hack */
  103. if ( strncmp ( root_path, "iscsi:", 6 ) == 0 ) {
  104. return iscsiboot ( root_path );
  105. } else if ( strncmp ( root_path, "aoe:", 4 ) == 0 ) {
  106. return aoeboot ( root_path );
  107. }
  108. return -ENOTSUP;
  109. }
  110. /**
  111. * Boot from a network device
  112. *
  113. * @v netdev Network device
  114. * @ret rc Return status code
  115. */
  116. static int netboot ( struct net_device *netdev ) {
  117. char buf[256];
  118. int rc;
  119. /* Open device and display device status */
  120. if ( ( rc = ifopen ( netdev ) ) != 0 )
  121. return rc;
  122. ifstat ( netdev );
  123. /* Configure device via DHCP */
  124. if ( ( rc = dhcp ( netdev ) ) != 0 )
  125. return rc;
  126. route();
  127. /* Try to boot an embedded image if we have one */
  128. rc = boot_embedded_image ();
  129. if ( rc != ENOENT )
  130. return rc;
  131. /* Try to download and boot whatever we are given as a filename */
  132. dhcp_snprintf ( buf, sizeof ( buf ),
  133. find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
  134. if ( buf[0] ) {
  135. printf ( "Booting from filename \"%s\"\n", buf );
  136. return boot_filename ( buf );
  137. }
  138. /* No filename; try the root path */
  139. dhcp_snprintf ( buf, sizeof ( buf ),
  140. find_global_dhcp_option ( DHCP_ROOT_PATH ) );
  141. if ( buf[0] ) {
  142. printf ( "Booting from root path \"%s\"\n", buf );
  143. return boot_root_path ( buf );
  144. }
  145. printf ( "No filename or root path specified\n" );
  146. return -ENOENT;
  147. }
  148. /**
  149. * Close all open net devices
  150. *
  151. * Called before a fresh boot attempt in order to free up memory. We
  152. * don't just close the device immediately after the boot fails,
  153. * because there may still be TCP connections in the process of
  154. * closing.
  155. */
  156. static void close_all_netdevs ( void ) {
  157. struct net_device *netdev;
  158. for_each_netdev ( netdev ) {
  159. ifclose ( netdev );
  160. }
  161. }
  162. /**
  163. * Boot the system
  164. */
  165. void autoboot ( void ) {
  166. struct net_device *boot_netdev;
  167. struct net_device *netdev;
  168. /* If we have an identifable boot device, try that first */
  169. close_all_netdevs();
  170. if ( ( boot_netdev = find_boot_netdev() ) )
  171. netboot ( boot_netdev );
  172. /* If that fails, try booting from any of the other devices */
  173. for_each_netdev ( netdev ) {
  174. if ( netdev == boot_netdev )
  175. continue;
  176. close_all_netdevs();
  177. netboot ( netdev );
  178. }
  179. printf ( "No more network devices\n" );
  180. }