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

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