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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/open.h>
  29. #include <ipxe/init.h>
  30. #include <usr/ifmgmt.h>
  31. #include <usr/route.h>
  32. #include <usr/dhcpmgmt.h>
  33. #include <usr/imgmgmt.h>
  34. #include <usr/autoboot.h>
  35. /** @file
  36. *
  37. * Automatic booting
  38. *
  39. */
  40. /* Disambiguate the various error causes */
  41. #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
  42. #define EINFO_ENOENT_BOOT \
  43. __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
  44. /**
  45. * Perform PXE menu boot when PXE stack is not available
  46. */
  47. __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
  48. return -ENOTSUP;
  49. }
  50. /**
  51. * Identify the boot network device
  52. *
  53. * @ret netdev Boot network device
  54. */
  55. static struct net_device * find_boot_netdev ( void ) {
  56. return NULL;
  57. }
  58. /**
  59. * Parse next-server and filename into a URI
  60. *
  61. * @v next_server Next-server address
  62. * @v filename Filename
  63. * @ret uri URI, or NULL on failure
  64. */
  65. static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
  66. const char *filename ) {
  67. char buf[ 23 /* "tftp://xxx.xxx.xxx.xxx/" */ + strlen ( filename )
  68. + 1 /* NUL */ ];
  69. struct uri *uri;
  70. /* Parse filename */
  71. uri = parse_uri ( filename );
  72. if ( ! uri )
  73. return NULL;
  74. /* Construct a tftp:// URI for the filename, if applicable.
  75. * We can't just rely on the current working URI, because the
  76. * relative URI resolution will remove the distinction between
  77. * filenames with and without initial slashes, which is
  78. * significant for TFTP.
  79. */
  80. if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
  81. uri_put ( uri );
  82. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  83. inet_ntoa ( next_server ), filename );
  84. uri = parse_uri ( buf );
  85. if ( ! uri )
  86. return NULL;
  87. }
  88. return uri;
  89. }
  90. /** The "keep-san" setting */
  91. struct setting keep_san_setting __setting = {
  92. .name = "keep-san",
  93. .description = "Preserve SAN connection",
  94. .tag = DHCP_EB_KEEP_SAN,
  95. .type = &setting_type_int8,
  96. };
  97. /** The "skip-san-boot" setting */
  98. struct setting skip_san_boot_setting __setting = {
  99. .name = "skip-san-boot",
  100. .description = "Do not boot the SAN drive after connecting",
  101. .tag = DHCP_EB_SKIP_SAN_BOOT,
  102. .type = &setting_type_int8,
  103. };
  104. /**
  105. * Boot from filename and root-path URIs
  106. *
  107. * @v filename Filename
  108. * @v root_path Root path
  109. * @ret rc Return status code
  110. */
  111. int uriboot ( struct uri *filename, struct uri *root_path ) {
  112. int drive;
  113. int rc;
  114. /* Treat empty URIs as absent */
  115. if ( filename && ( ! uri_has_path ( filename ) ) )
  116. filename = NULL;
  117. if ( root_path && ( ! uri_is_absolute ( root_path ) ) )
  118. root_path = NULL;
  119. /* If we have both a filename and a root path, ignore an
  120. * unsupported URI scheme in the root path, since it may
  121. * represent an NFS root.
  122. */
  123. if ( filename && root_path &&
  124. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
  125. printf ( "Ignoring unsupported root path\n" );
  126. root_path = NULL;
  127. }
  128. /* Check that we have something to boot */
  129. if ( ! ( filename || root_path ) ) {
  130. rc = -ENOENT_BOOT;
  131. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  132. goto err_no_boot;
  133. }
  134. /* Hook SAN device, if applicable */
  135. if ( root_path ) {
  136. drive = san_hook ( root_path, 0 );
  137. if ( drive < 0 ) {
  138. rc = drive;
  139. printf ( "Could not open SAN device: %s\n",
  140. strerror ( rc ) );
  141. goto err_san_hook;
  142. }
  143. printf ( "Registered as SAN device %#02x\n", drive );
  144. } else {
  145. drive = -ENODEV;
  146. }
  147. /* Describe SAN device, if applicable */
  148. if ( ( drive >= 0 ) && ( ( rc = san_describe ( drive ) ) != 0 ) ) {
  149. printf ( "Could not describe SAN device %#02x: %s\n",
  150. drive, strerror ( rc ) );
  151. goto err_san_describe;
  152. }
  153. /* Allow a root-path-only boot with skip-san enabled to succeed */
  154. rc = 0;
  155. /* Attempt filename boot if applicable */
  156. if ( filename ) {
  157. if ( ( rc = imgdownload ( filename, NULL, NULL,
  158. register_and_boot_image ) ) != 0 ) {
  159. printf ( "\nCould not chain image: %s\n",
  160. strerror ( rc ) );
  161. /* Fall through to (possibly) attempt a SAN boot
  162. * as a fallback. If no SAN boot is attempted,
  163. * our status will become the return status.
  164. */
  165. } else {
  166. /* Always print an extra newline, because we
  167. * don't know where the NBP may have left the
  168. * cursor.
  169. */
  170. printf ( "\n" );
  171. }
  172. }
  173. /* Attempt SAN boot if applicable */
  174. if ( root_path ) {
  175. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  176. printf ( "Booting from SAN device %#02x\n", drive );
  177. rc = san_boot ( drive );
  178. printf ( "Boot from SAN device %#02x failed: %s\n",
  179. drive, strerror ( rc ) );
  180. } else {
  181. printf ( "Skipping boot from SAN device %#02x\n",
  182. drive );
  183. /* Avoid overwriting a possible failure status
  184. * from a filename boot.
  185. */
  186. }
  187. }
  188. err_san_describe:
  189. /* Unhook SAN device, if applicable */
  190. if ( drive >= 0 ) {
  191. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  192. printf ( "Unregistering SAN device %#02x\n", drive );
  193. san_unhook ( drive );
  194. } else {
  195. printf ( "Preserving connection to SAN device %#02x\n",
  196. drive );
  197. }
  198. }
  199. err_san_hook:
  200. err_no_boot:
  201. return rc;
  202. }
  203. /**
  204. * Close all open net devices
  205. *
  206. * Called before a fresh boot attempt in order to free up memory. We
  207. * don't just close the device immediately after the boot fails,
  208. * because there may still be TCP connections in the process of
  209. * closing.
  210. */
  211. static void close_all_netdevs ( void ) {
  212. struct net_device *netdev;
  213. for_each_netdev ( netdev ) {
  214. ifclose ( netdev );
  215. }
  216. }
  217. /**
  218. * Fetch next-server and filename settings into a URI
  219. *
  220. * @v settings Settings block
  221. * @ret uri URI, or NULL on failure
  222. */
  223. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  224. struct in_addr next_server;
  225. char buf[256];
  226. char *filename;
  227. struct uri *uri;
  228. /* Fetch next-server setting */
  229. fetch_ipv4_setting ( settings, &next_server_setting, &next_server );
  230. if ( next_server.s_addr )
  231. printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
  232. /* Fetch filename setting */
  233. fetch_string_setting ( settings, &filename_setting,
  234. buf, sizeof ( buf ) );
  235. if ( buf[0] )
  236. printf ( "Filename: %s\n", buf );
  237. /* Expand filename setting */
  238. filename = expand_settings ( buf );
  239. if ( ! filename )
  240. return NULL;
  241. /* Parse next server and filename */
  242. uri = parse_next_server_and_filename ( next_server, filename );
  243. free ( filename );
  244. return uri;
  245. }
  246. /**
  247. * Fetch root-path setting into a URI
  248. *
  249. * @v settings Settings block
  250. * @ret uri URI, or NULL on failure
  251. */
  252. static struct uri * fetch_root_path ( struct settings *settings ) {
  253. char buf[256];
  254. char *root_path;
  255. struct uri *uri;
  256. /* Fetch root-path setting */
  257. fetch_string_setting ( settings, &root_path_setting,
  258. buf, sizeof ( buf ) );
  259. if ( buf[0] )
  260. printf ( "Root path: %s\n", buf );
  261. /* Expand filename setting */
  262. root_path = expand_settings ( buf );
  263. if ( ! root_path )
  264. return NULL;
  265. /* Parse root path */
  266. uri = parse_uri ( root_path );
  267. free ( root_path );
  268. return uri;
  269. }
  270. /**
  271. * Check whether or not we have a usable PXE menu
  272. *
  273. * @ret have_menu A usable PXE menu is present
  274. */
  275. static int have_pxe_menu ( void ) {
  276. struct setting vendor_class_id_setting
  277. = { .tag = DHCP_VENDOR_CLASS_ID };
  278. struct setting pxe_discovery_control_setting
  279. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  280. struct setting pxe_boot_menu_setting
  281. = { .tag = DHCP_PXE_BOOT_MENU };
  282. char buf[256];
  283. unsigned int pxe_discovery_control;
  284. fetch_string_setting ( NULL, &vendor_class_id_setting,
  285. buf, sizeof ( buf ) );
  286. pxe_discovery_control =
  287. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  288. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  289. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  290. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  291. setting_exists ( NULL, &filename_setting ) ) ) );
  292. }
  293. /**
  294. * Boot from a network device
  295. *
  296. * @v netdev Network device
  297. * @ret rc Return status code
  298. */
  299. int netboot ( struct net_device *netdev ) {
  300. struct uri *filename;
  301. struct uri *root_path;
  302. int rc;
  303. /* Close all other network devices */
  304. close_all_netdevs();
  305. /* Open device and display device status */
  306. if ( ( rc = ifopen ( netdev ) ) != 0 )
  307. goto err_ifopen;
  308. ifstat ( netdev );
  309. /* Configure device via DHCP */
  310. if ( ( rc = dhcp ( netdev ) ) != 0 )
  311. goto err_dhcp;
  312. route();
  313. /* Try PXE menu boot, if applicable */
  314. if ( have_pxe_menu() ) {
  315. printf ( "Booting from PXE menu\n" );
  316. rc = pxe_menu_boot ( netdev );
  317. goto err_pxe_menu_boot;
  318. }
  319. /* Fetch next server, filename and root path */
  320. filename = fetch_next_server_and_filename ( NULL );
  321. if ( ! filename )
  322. goto err_filename;
  323. root_path = fetch_root_path ( NULL );
  324. if ( ! root_path )
  325. goto err_root_path;
  326. /* Boot using next server, filename and root path */
  327. if ( ( rc = uriboot ( filename, root_path ) ) != 0 )
  328. goto err_uriboot;
  329. err_uriboot:
  330. uri_put ( root_path );
  331. err_root_path:
  332. uri_put ( filename );
  333. err_filename:
  334. err_pxe_menu_boot:
  335. err_dhcp:
  336. err_ifopen:
  337. return rc;
  338. }
  339. /**
  340. * Boot the system
  341. */
  342. int autoboot ( void ) {
  343. struct net_device *boot_netdev;
  344. struct net_device *netdev;
  345. int rc = -ENODEV;
  346. /* If we have an identifable boot device, try that first */
  347. if ( ( boot_netdev = find_boot_netdev() ) )
  348. rc = netboot ( boot_netdev );
  349. /* If that fails, try booting from any of the other devices */
  350. for_each_netdev ( netdev ) {
  351. if ( netdev == boot_netdev )
  352. continue;
  353. rc = netboot ( netdev );
  354. }
  355. printf ( "No more network devices\n" );
  356. return rc;
  357. }