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.

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