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

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