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

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