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

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