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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <gpxe/netdevice.h>
  23. #include <gpxe/dhcp.h>
  24. #include <gpxe/settings.h>
  25. #include <gpxe/image.h>
  26. #include <gpxe/sanboot.h>
  27. #include <gpxe/uri.h>
  28. #include <usr/ifmgmt.h>
  29. #include <usr/route.h>
  30. #include <usr/dhcpmgmt.h>
  31. #include <usr/imgmgmt.h>
  32. #include <usr/autoboot.h>
  33. /** @file
  34. *
  35. * Automatic booting
  36. *
  37. */
  38. /** Shutdown flags for exit */
  39. int shutdown_exit_flags = 0;
  40. /**
  41. * Identify the boot network device
  42. *
  43. * @ret netdev Boot network device
  44. */
  45. static struct net_device * find_boot_netdev ( void ) {
  46. return NULL;
  47. }
  48. /**
  49. * Boot using next-server and filename
  50. *
  51. * @v filename Boot filename
  52. * @ret rc Return status code
  53. */
  54. int boot_next_server_and_filename ( struct in_addr next_server,
  55. const char *filename ) {
  56. struct uri *uri;
  57. struct image *image;
  58. char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
  59. int filename_is_absolute;
  60. int rc;
  61. /* Construct URI */
  62. uri = parse_uri ( filename );
  63. if ( ! uri )
  64. return -ENOMEM;
  65. filename_is_absolute = uri_is_absolute ( uri );
  66. uri_put ( uri );
  67. if ( ! filename_is_absolute ) {
  68. /* Construct a tftp:// URI for the filename. We can't
  69. * just rely on the current working URI, because the
  70. * relative URI resolution will remove the distinction
  71. * between filenames with and without initial slashes,
  72. * which is significant for TFTP.
  73. */
  74. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  75. inet_ntoa ( next_server ), filename );
  76. filename = buf;
  77. }
  78. image = alloc_image();
  79. if ( ! image )
  80. return -ENOMEM;
  81. if ( ( rc = imgfetch ( image, filename,
  82. register_and_autoload_image ) ) != 0 ) {
  83. goto done;
  84. }
  85. if ( ( rc = imgexec ( image ) ) != 0 )
  86. goto done;
  87. done:
  88. image_put ( image );
  89. return rc;
  90. }
  91. /**
  92. * Boot using root path
  93. *
  94. * @v root_path Root path
  95. * @ret rc Return status code
  96. */
  97. int boot_root_path ( const char *root_path ) {
  98. struct sanboot_protocol *sanboot;
  99. /* Quick hack */
  100. for_each_table_entry ( sanboot, SANBOOT_PROTOCOLS ) {
  101. if ( strncmp ( root_path, sanboot->prefix,
  102. strlen ( sanboot->prefix ) ) == 0 ) {
  103. return sanboot->boot ( root_path );
  104. }
  105. }
  106. return -ENOTSUP;
  107. }
  108. /**
  109. * Boot from a network device
  110. *
  111. * @v netdev Network device
  112. * @ret rc Return status code
  113. */
  114. static int netboot ( struct net_device *netdev ) {
  115. struct setting vendor_class_id_setting
  116. = { .tag = DHCP_VENDOR_CLASS_ID };
  117. struct setting pxe_discovery_control_setting
  118. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  119. struct setting pxe_boot_menu_setting
  120. = { .tag = DHCP_PXE_BOOT_MENU };
  121. char buf[256];
  122. struct in_addr next_server;
  123. unsigned int pxe_discovery_control;
  124. int rc;
  125. /* Open device and display device status */
  126. if ( ( rc = ifopen ( netdev ) ) != 0 )
  127. return rc;
  128. ifstat ( netdev );
  129. /* Configure device via DHCP */
  130. if ( ( rc = dhcp ( netdev ) ) != 0 )
  131. return rc;
  132. route();
  133. /* Try PXE menu boot, if applicable */
  134. fetch_string_setting ( NULL, &vendor_class_id_setting,
  135. buf, sizeof ( buf ) );
  136. pxe_discovery_control =
  137. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  138. if ( ( strcmp ( buf, "PXEClient" ) == 0 ) && pxe_menu_boot != NULL &&
  139. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  140. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  141. setting_exists ( NULL, &filename_setting ) ) ) ) {
  142. printf ( "Booting from PXE menu\n" );
  143. return pxe_menu_boot ( netdev );
  144. }
  145. /* Try to download and boot whatever we are given as a filename */
  146. fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
  147. fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
  148. if ( buf[0] ) {
  149. printf ( "Booting from filename \"%s\"\n", buf );
  150. if ( ( rc = boot_next_server_and_filename ( next_server,
  151. buf ) ) != 0 ) {
  152. printf ( "Could not boot from filename \"%s\": %s\n",
  153. buf, strerror ( rc ) );
  154. return rc;
  155. }
  156. return 0;
  157. }
  158. /* No filename; try the root path */
  159. fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
  160. if ( buf[0] ) {
  161. printf ( "Booting from root path \"%s\"\n", buf );
  162. if ( ( rc = boot_root_path ( buf ) ) != 0 ) {
  163. printf ( "Could not boot from root path \"%s\": %s\n",
  164. buf, strerror ( rc ) );
  165. return rc;
  166. }
  167. return 0;
  168. }
  169. printf ( "No filename or root path specified\n" );
  170. return -ENOENT;
  171. }
  172. /**
  173. * Close all open net devices
  174. *
  175. * Called before a fresh boot attempt in order to free up memory. We
  176. * don't just close the device immediately after the boot fails,
  177. * because there may still be TCP connections in the process of
  178. * closing.
  179. */
  180. static void close_all_netdevs ( void ) {
  181. struct net_device *netdev;
  182. for_each_netdev ( netdev ) {
  183. ifclose ( netdev );
  184. }
  185. }
  186. /**
  187. * Boot the system
  188. */
  189. void autoboot ( void ) {
  190. struct net_device *boot_netdev;
  191. struct net_device *netdev;
  192. /* If we have an identifable boot device, try that first */
  193. close_all_netdevs();
  194. if ( ( boot_netdev = find_boot_netdev() ) )
  195. netboot ( boot_netdev );
  196. /* If that fails, try booting from any of the other devices */
  197. for_each_netdev ( netdev ) {
  198. if ( netdev == boot_netdev )
  199. continue;
  200. close_all_netdevs();
  201. netboot ( netdev );
  202. }
  203. printf ( "No more network devices\n" );
  204. }