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

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