autoboot.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <ipxe/init.h>
  29. #include <usr/ifmgmt.h>
  30. #include <usr/route.h>
  31. #include <usr/dhcpmgmt.h>
  32. #include <usr/imgmgmt.h>
  33. #include <usr/autoboot.h>
  34. /** @file
  35. *
  36. * Automatic booting
  37. *
  38. */
  39. /** Shutdown flags for exit */
  40. int shutdown_exit_flags = 0;
  41. /**
  42. * Perform PXE menu boot when PXE stack is not available
  43. */
  44. __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
  45. return -ENOTSUP;
  46. }
  47. /**
  48. * Identify the boot network device
  49. *
  50. * @ret netdev Boot network device
  51. */
  52. static struct net_device * find_boot_netdev ( void ) {
  53. return NULL;
  54. }
  55. /**
  56. * Boot using next-server and filename
  57. *
  58. * @v filename Boot filename
  59. * @ret rc Return status code
  60. */
  61. int boot_next_server_and_filename ( struct in_addr next_server,
  62. const char *filename ) {
  63. struct uri *uri;
  64. struct image *image;
  65. char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ +
  66. ( 3 * strlen(filename) ) /* completely URI-encoded */
  67. + 1 /* NUL */ ];
  68. int filename_is_absolute;
  69. int rc;
  70. /* Construct URI */
  71. uri = parse_uri ( filename );
  72. if ( ! uri ) {
  73. printf ( "Could not parse \"%s\"\n", filename );
  74. rc = -ENOMEM;
  75. goto err_parse_uri;
  76. }
  77. filename_is_absolute = uri_is_absolute ( uri );
  78. uri_put ( uri );
  79. /* Construct a tftp:// URI for the filename, if applicable.
  80. * We can't just rely on the current working URI, because the
  81. * relative URI resolution will remove the distinction between
  82. * filenames with and without initial slashes, which is
  83. * significant for TFTP.
  84. */
  85. if ( ! filename_is_absolute ) {
  86. snprintf ( buf, sizeof ( buf ), "tftp://%s/",
  87. inet_ntoa ( next_server ) );
  88. uri_encode ( filename, buf + strlen ( buf ),
  89. sizeof ( buf ) - strlen ( buf ), URI_PATH );
  90. filename = buf;
  91. }
  92. /* Download and boot image */
  93. image = alloc_image();
  94. if ( ! image ) {
  95. printf ( "Could not allocate image\n" );
  96. rc = -ENOMEM;
  97. goto err_alloc_image;
  98. }
  99. if ( ( rc = imgfetch ( image, filename,
  100. register_and_autoload_image ) ) != 0 ) {
  101. printf ( "Could not fetch image: %s\n", strerror ( rc ) );
  102. goto err_imgfetch;
  103. }
  104. if ( ( rc = imgexec ( image ) ) != 0 ) {
  105. printf ( "Could not execute image: %s\n", strerror ( rc ) );
  106. goto err_imgexec;
  107. }
  108. /* Drop image reference */
  109. image_put ( image );
  110. return 0;
  111. err_imgexec:
  112. err_imgfetch:
  113. image_put ( image );
  114. err_alloc_image:
  115. err_parse_uri:
  116. return rc;
  117. }
  118. /** The "keep-san" setting */
  119. struct setting keep_san_setting __setting = {
  120. .name = "keep-san",
  121. .description = "Preserve SAN connection",
  122. .tag = DHCP_EB_KEEP_SAN,
  123. .type = &setting_type_int8,
  124. };
  125. /** The "skip-san-boot" setting */
  126. struct setting skip_san_boot_setting __setting = {
  127. .name = "skip-san-boot",
  128. .description = "Do not boot the SAN drive after connecting",
  129. .tag = DHCP_EB_SKIP_SAN_BOOT,
  130. .type = &setting_type_int8,
  131. };
  132. /**
  133. * Boot using root path
  134. *
  135. * @v root_path Root path
  136. * @ret rc Return status code
  137. */
  138. int boot_root_path ( const char *root_path ) {
  139. struct uri *uri;
  140. int drive;
  141. int rc;
  142. /* Parse URI */
  143. uri = parse_uri ( root_path );
  144. if ( ! uri ) {
  145. printf ( "Could not parse \"%s\"\n", root_path );
  146. rc = -ENOMEM;
  147. goto err_parse_uri;
  148. }
  149. /* Hook SAN device */
  150. if ( ( drive = san_hook ( uri, 0 ) ) < 0 ) {
  151. rc = drive;
  152. printf ( "Could not open SAN device: %s\n",
  153. strerror ( rc ) );
  154. goto err_open;
  155. }
  156. printf ( "Registered as SAN device %#02x\n", drive );
  157. /* Describe SAN device */
  158. if ( ( rc = san_describe ( drive ) ) != 0 ) {
  159. printf ( "Could not describe SAN device %#02x: %s\n",
  160. drive, strerror ( rc ) );
  161. goto err_describe;
  162. }
  163. /* Boot from SAN device */
  164. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) != 0 ) {
  165. printf ( "Skipping boot from SAN device %#02x\n", drive );
  166. } else {
  167. printf ( "Booting from SAN device %#02x\n", drive );
  168. rc = san_boot ( drive );
  169. printf ( "Boot from SAN device %#02x failed: %s\n",
  170. drive, strerror ( rc ) );
  171. }
  172. /* Leave drive registered, if instructed to do so */
  173. if ( fetch_intz_setting ( NULL, &keep_san_setting ) != 0 ) {
  174. printf ( "Preserving connection to SAN device %#02x\n",
  175. drive );
  176. shutdown_exit_flags |= SHUTDOWN_KEEP_DEVICES;
  177. goto err_keep_san;
  178. }
  179. /* Unhook SAN deivce */
  180. printf ( "Unregistering SAN device %#02x\n", drive );
  181. san_unhook ( drive );
  182. /* Drop URI reference */
  183. uri_put ( uri );
  184. return 0;
  185. err_keep_san:
  186. err_describe:
  187. err_open:
  188. uri_put ( uri );
  189. err_parse_uri:
  190. return rc;
  191. }
  192. /**
  193. * Boot from a network device
  194. *
  195. * @v netdev Network device
  196. * @ret rc Return status code
  197. */
  198. int netboot ( struct net_device *netdev ) {
  199. struct setting vendor_class_id_setting
  200. = { .tag = DHCP_VENDOR_CLASS_ID };
  201. struct setting pxe_discovery_control_setting
  202. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  203. struct setting pxe_boot_menu_setting
  204. = { .tag = DHCP_PXE_BOOT_MENU };
  205. char buf[256];
  206. struct in_addr next_server;
  207. unsigned int pxe_discovery_control;
  208. int rc;
  209. /* Open device and display device status */
  210. if ( ( rc = ifopen ( netdev ) ) != 0 )
  211. return rc;
  212. ifstat ( netdev );
  213. /* Configure device via DHCP */
  214. if ( ( rc = dhcp ( netdev ) ) != 0 )
  215. return rc;
  216. route();
  217. /* Try PXE menu boot, if applicable */
  218. fetch_string_setting ( NULL, &vendor_class_id_setting,
  219. buf, sizeof ( buf ) );
  220. pxe_discovery_control =
  221. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  222. if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  223. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  224. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  225. setting_exists ( NULL, &filename_setting ) ) ) ) {
  226. printf ( "Booting from PXE menu\n" );
  227. return pxe_menu_boot ( netdev );
  228. }
  229. /* Try to download and boot whatever we are given as a filename */
  230. fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
  231. fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
  232. if ( buf[0] ) {
  233. printf ( "Booting from filename \"%s\"\n", buf );
  234. return boot_next_server_and_filename ( next_server, buf );
  235. }
  236. /* No filename; try the root path */
  237. fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
  238. if ( buf[0] ) {
  239. printf ( "Booting from root path \"%s\"\n", buf );
  240. return boot_root_path ( buf );
  241. }
  242. printf ( "No filename or root path specified\n" );
  243. return -ENOENT;
  244. }
  245. /**
  246. * Close all open net devices
  247. *
  248. * Called before a fresh boot attempt in order to free up memory. We
  249. * don't just close the device immediately after the boot fails,
  250. * because there may still be TCP connections in the process of
  251. * closing.
  252. */
  253. static void close_all_netdevs ( void ) {
  254. struct net_device *netdev;
  255. for_each_netdev ( netdev ) {
  256. ifclose ( netdev );
  257. }
  258. }
  259. /**
  260. * Boot the system
  261. */
  262. void autoboot ( void ) {
  263. struct net_device *boot_netdev;
  264. struct net_device *netdev;
  265. /* If we have an identifable boot device, try that first */
  266. close_all_netdevs();
  267. if ( ( boot_netdev = find_boot_netdev() ) )
  268. netboot ( boot_netdev );
  269. /* If that fails, try booting from any of the other devices */
  270. for_each_netdev ( netdev ) {
  271. if ( netdev == boot_netdev )
  272. continue;
  273. close_all_netdevs();
  274. netboot ( netdev );
  275. }
  276. printf ( "No more network devices\n" );
  277. }