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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Boot from a network device
  44. *
  45. * @v netdev Network device
  46. */
  47. void netboot ( struct net_device *netdev ) {
  48. char filename[256];
  49. struct image *image;
  50. int rc;
  51. /* Open device and display device status */
  52. if ( ( rc = ifopen ( netdev ) ) != 0 )
  53. return;
  54. ifstat ( netdev );
  55. /* Configure device via DHCP */
  56. if ( ( rc = dhcp ( netdev ) ) != 0 )
  57. return;
  58. route();
  59. /* Try to download and boot whatever we are given as a filename */
  60. dhcp_snprintf ( filename, sizeof ( filename ),
  61. find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
  62. if ( ! filename[0] ) {
  63. printf ( "No boot filename\n" );
  64. return;
  65. }
  66. printf ( "Booting \"%s\"\n", filename );
  67. image = alloc_image();
  68. if ( ! image ) {
  69. printf ( "Out of memory\n" );
  70. return;
  71. }
  72. if ( ( rc = imgfetch ( image, filename, 0 ) ) != 0 ) {
  73. printf ( "Could not retrieve %s: %s\n",
  74. filename, strerror ( rc ) );
  75. image_put ( image );
  76. return;
  77. }
  78. if ( ( rc = imgload ( image ) ) != 0 ) {
  79. printf ( "Could not load %s: %s\n", image->name,
  80. strerror ( rc ) );
  81. image_put ( image );
  82. return;
  83. }
  84. if ( ( rc = imgexec ( image ) ) != 0 ) {
  85. printf ( "Could not execute %s: %s\n", image->name,
  86. strerror ( rc ) );
  87. image_put ( image );
  88. return;
  89. }
  90. }
  91. /**
  92. * Close all open net devices
  93. *
  94. * Called before a fresh boot attempt in order to free up memory. We
  95. * don't just close the device immediately after the boot fails,
  96. * because there may still be TCP connections in the process of
  97. * closing.
  98. */
  99. static void close_all_netdevs ( void ) {
  100. struct net_device *netdev;
  101. for_each_netdev ( netdev ) {
  102. ifclose ( netdev );
  103. }
  104. }
  105. /**
  106. * Boot the system
  107. */
  108. void autoboot ( void ) {
  109. struct net_device *boot_netdev;
  110. struct net_device *netdev;
  111. /* If we have an identifable boot device, try that first */
  112. close_all_netdevs();
  113. if ( ( boot_netdev = find_boot_netdev() ) )
  114. netboot ( boot_netdev );
  115. /* If that fails, try booting from any of the other devices */
  116. for_each_netdev ( netdev ) {
  117. if ( netdev == boot_netdev )
  118. continue;
  119. close_all_netdevs();
  120. netboot ( netdev );
  121. }
  122. printf ( "No more network devices\n" );
  123. }