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

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