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

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