autoboot.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <ipxe/netdevice.h>
  24. #include <ipxe/dhcp.h>
  25. #include <ipxe/settings.h>
  26. #include <ipxe/image.h>
  27. #include <ipxe/sanboot.h>
  28. #include <ipxe/uri.h>
  29. #include <ipxe/open.h>
  30. #include <ipxe/init.h>
  31. #include <ipxe/keys.h>
  32. #include <ipxe/version.h>
  33. #include <ipxe/shell.h>
  34. #include <ipxe/features.h>
  35. #include <ipxe/image.h>
  36. #include <ipxe/timer.h>
  37. #include <usr/ifmgmt.h>
  38. #include <usr/route.h>
  39. #include <usr/dhcpmgmt.h>
  40. #include <usr/imgmgmt.h>
  41. #include <usr/prompt.h>
  42. #include <usr/autoboot.h>
  43. #include <config/general.h>
  44. /** @file
  45. *
  46. * Automatic booting
  47. *
  48. */
  49. /* Disambiguate the various error causes */
  50. #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
  51. #define EINFO_ENOENT_BOOT \
  52. __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
  53. #define NORMAL "\033[0m"
  54. #define BOLD "\033[1m"
  55. #define CYAN "\033[36m"
  56. /** The "scriptlet" setting */
  57. struct setting scriptlet_setting __setting ( SETTING_MISC ) = {
  58. .name = "scriptlet",
  59. .description = "Boot scriptlet",
  60. .tag = DHCP_EB_SCRIPTLET,
  61. .type = &setting_type_string,
  62. };
  63. /**
  64. * Perform PXE menu boot when PXE stack is not available
  65. */
  66. __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
  67. return -ENOTSUP;
  68. }
  69. /**
  70. * Identify the boot network device
  71. *
  72. * @ret netdev Boot network device
  73. */
  74. static struct net_device * find_boot_netdev ( void ) {
  75. return NULL;
  76. }
  77. /**
  78. * Parse next-server and filename into a URI
  79. *
  80. * @v next_server Next-server address
  81. * @v filename Filename
  82. * @ret uri URI, or NULL on failure
  83. */
  84. static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
  85. const char *filename ) {
  86. char buf[ 23 /* "tftp://xxx.xxx.xxx.xxx/" */ + strlen ( filename )
  87. + 1 /* NUL */ ];
  88. struct uri *uri;
  89. /* Parse filename */
  90. uri = parse_uri ( filename );
  91. if ( ! uri )
  92. return NULL;
  93. /* Construct a tftp:// URI for the filename, if applicable.
  94. * We can't just rely on the current working URI, because the
  95. * relative URI resolution will remove the distinction between
  96. * filenames with and without initial slashes, which is
  97. * significant for TFTP.
  98. */
  99. if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
  100. uri_put ( uri );
  101. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  102. inet_ntoa ( next_server ), filename );
  103. uri = parse_uri ( buf );
  104. if ( ! uri )
  105. return NULL;
  106. }
  107. return uri;
  108. }
  109. /** The "keep-san" setting */
  110. struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA ) = {
  111. .name = "keep-san",
  112. .description = "Preserve SAN connection",
  113. .tag = DHCP_EB_KEEP_SAN,
  114. .type = &setting_type_int8,
  115. };
  116. /** The "skip-san-boot" setting */
  117. struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA ) = {
  118. .name = "skip-san-boot",
  119. .description = "Do not boot from SAN device",
  120. .tag = DHCP_EB_SKIP_SAN_BOOT,
  121. .type = &setting_type_int8,
  122. };
  123. /**
  124. * Boot from filename and root-path URIs
  125. *
  126. * @v filename Filename
  127. * @v root_path Root path
  128. * @v drive SAN drive (if applicable)
  129. * @v flags Boot action flags
  130. * @ret rc Return status code
  131. *
  132. * The somewhat tortuous flow of control in this function exists in
  133. * order to ensure that the "sanboot" command remains identical in
  134. * function to a SAN boot via a DHCP-specified root path, and to
  135. * provide backwards compatibility for the "keep-san" and
  136. * "skip-san-boot" options.
  137. */
  138. int uriboot ( struct uri *filename, struct uri *root_path, int drive,
  139. unsigned int flags ) {
  140. struct image *image;
  141. int rc;
  142. /* Hook SAN device, if applicable */
  143. if ( root_path ) {
  144. if ( ( rc = san_hook ( root_path, drive ) ) != 0 ) {
  145. printf ( "Could not open SAN device: %s\n",
  146. strerror ( rc ) );
  147. goto err_san_hook;
  148. }
  149. printf ( "Registered SAN device %#02x\n", drive );
  150. }
  151. /* Describe SAN device, if applicable */
  152. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
  153. if ( ( rc = san_describe ( drive ) ) != 0 ) {
  154. printf ( "Could not describe SAN device %#02x: %s\n",
  155. drive, strerror ( rc ) );
  156. goto err_san_describe;
  157. }
  158. }
  159. /* Allow a root-path-only boot with skip-san enabled to succeed */
  160. rc = 0;
  161. /* Attempt filename boot if applicable */
  162. if ( filename ) {
  163. if ( ( rc = imgdownload ( filename, &image ) ) != 0 )
  164. goto err_download;
  165. image->flags |= IMAGE_AUTO_UNREGISTER;
  166. if ( ( rc = image_exec ( image ) ) != 0 ) {
  167. printf ( "Could not boot image: %s\n",
  168. strerror ( rc ) );
  169. /* Fall through to (possibly) attempt a SAN boot
  170. * as a fallback. If no SAN boot is attempted,
  171. * our status will become the return status.
  172. */
  173. } else {
  174. /* Always print an extra newline, because we
  175. * don't know where the NBP may have left the
  176. * cursor.
  177. */
  178. printf ( "\n" );
  179. }
  180. }
  181. /* Attempt SAN boot if applicable */
  182. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
  183. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  184. printf ( "Booting from SAN device %#02x\n", drive );
  185. rc = san_boot ( drive );
  186. printf ( "Boot from SAN device %#02x failed: %s\n",
  187. drive, strerror ( rc ) );
  188. } else {
  189. printf ( "Skipping boot from SAN device %#02x\n",
  190. drive );
  191. /* Avoid overwriting a possible failure status
  192. * from a filename boot.
  193. */
  194. }
  195. }
  196. err_download:
  197. err_san_describe:
  198. /* Unhook SAN device, if applicable */
  199. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
  200. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  201. san_unhook ( drive );
  202. printf ( "Unregistered SAN device %#02x\n", drive );
  203. } else {
  204. printf ( "Preserving SAN device %#02x\n", drive );
  205. }
  206. }
  207. err_san_hook:
  208. return rc;
  209. }
  210. /**
  211. * Close all open net devices
  212. *
  213. * Called before a fresh boot attempt in order to free up memory. We
  214. * don't just close the device immediately after the boot fails,
  215. * because there may still be TCP connections in the process of
  216. * closing.
  217. */
  218. static void close_all_netdevs ( void ) {
  219. struct net_device *netdev;
  220. for_each_netdev ( netdev ) {
  221. ifclose ( netdev );
  222. }
  223. }
  224. /**
  225. * Fetch next-server and filename settings into a URI
  226. *
  227. * @v settings Settings block
  228. * @ret uri URI, or NULL on failure
  229. */
  230. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  231. struct in_addr next_server = { 0 };
  232. char *raw_filename = NULL;
  233. struct uri *uri = NULL;
  234. char *filename;
  235. /* Determine settings block containing the filename, if any */
  236. settings = fetch_setting_origin ( settings, &filename_setting );
  237. /* If we have a filename, fetch it along with next-server */
  238. if ( settings ) {
  239. fetch_ipv4_setting ( settings, &next_server_setting,
  240. &next_server );
  241. if ( fetch_string_setting_copy ( settings, &filename_setting,
  242. &raw_filename ) < 0 )
  243. goto err_fetch;
  244. }
  245. /* Expand filename setting */
  246. filename = expand_settings ( raw_filename ? raw_filename : "" );
  247. if ( ! filename )
  248. goto err_expand;
  249. /* Parse next server and filename */
  250. if ( next_server.s_addr )
  251. printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
  252. if ( filename[0] )
  253. printf ( "Filename: %s\n", filename );
  254. uri = parse_next_server_and_filename ( next_server, filename );
  255. if ( ! uri )
  256. goto err_parse;
  257. err_parse:
  258. free ( filename );
  259. err_expand:
  260. free ( raw_filename );
  261. err_fetch:
  262. return uri;
  263. }
  264. /**
  265. * Fetch root-path setting into a URI
  266. *
  267. * @v settings Settings block
  268. * @ret uri URI, or NULL on failure
  269. */
  270. static struct uri * fetch_root_path ( struct settings *settings ) {
  271. char buf[256];
  272. char *root_path;
  273. struct uri *uri;
  274. /* Fetch root-path setting */
  275. fetch_string_setting ( settings, &root_path_setting,
  276. buf, sizeof ( buf ) );
  277. if ( buf[0] )
  278. printf ( "Root path: %s\n", buf );
  279. /* Expand filename setting */
  280. root_path = expand_settings ( buf );
  281. if ( ! root_path )
  282. return NULL;
  283. /* Parse root path */
  284. uri = parse_uri ( root_path );
  285. free ( root_path );
  286. return uri;
  287. }
  288. /**
  289. * Check whether or not we have a usable PXE menu
  290. *
  291. * @ret have_menu A usable PXE menu is present
  292. */
  293. static int have_pxe_menu ( void ) {
  294. struct setting vendor_class_id_setting
  295. = { .tag = DHCP_VENDOR_CLASS_ID };
  296. struct setting pxe_discovery_control_setting
  297. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  298. struct setting pxe_boot_menu_setting
  299. = { .tag = DHCP_PXE_BOOT_MENU };
  300. char buf[256];
  301. unsigned int pxe_discovery_control;
  302. fetch_string_setting ( NULL, &vendor_class_id_setting,
  303. buf, sizeof ( buf ) );
  304. pxe_discovery_control =
  305. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  306. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  307. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  308. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  309. setting_exists ( NULL, &filename_setting ) ) ) );
  310. }
  311. /**
  312. * Boot from a network device
  313. *
  314. * @v netdev Network device
  315. * @ret rc Return status code
  316. */
  317. int netboot ( struct net_device *netdev ) {
  318. struct uri *filename;
  319. struct uri *root_path;
  320. int rc;
  321. /* Close all other network devices */
  322. close_all_netdevs();
  323. /* Open device and display device status */
  324. if ( ( rc = ifopen ( netdev ) ) != 0 )
  325. goto err_ifopen;
  326. ifstat ( netdev );
  327. /* Configure device via DHCP */
  328. if ( ( rc = dhcp ( netdev ) ) != 0 )
  329. goto err_dhcp;
  330. route();
  331. /* Try PXE menu boot, if applicable */
  332. if ( have_pxe_menu() ) {
  333. printf ( "Booting from PXE menu\n" );
  334. rc = pxe_menu_boot ( netdev );
  335. goto err_pxe_menu_boot;
  336. }
  337. /* Fetch next server and filename */
  338. filename = fetch_next_server_and_filename ( NULL );
  339. if ( ! filename )
  340. goto err_filename;
  341. if ( ! uri_has_path ( filename ) ) {
  342. /* Ignore empty filename */
  343. uri_put ( filename );
  344. filename = NULL;
  345. }
  346. /* Fetch root path */
  347. root_path = fetch_root_path ( NULL );
  348. if ( ! root_path )
  349. goto err_root_path;
  350. if ( ! uri_is_absolute ( root_path ) ) {
  351. /* Ignore empty root path */
  352. uri_put ( root_path );
  353. root_path = NULL;
  354. }
  355. /* If we have both a filename and a root path, ignore an
  356. * unsupported URI scheme in the root path, since it may
  357. * represent an NFS root.
  358. */
  359. if ( filename && root_path &&
  360. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
  361. printf ( "Ignoring unsupported root path\n" );
  362. uri_put ( root_path );
  363. root_path = NULL;
  364. }
  365. /* Check that we have something to boot */
  366. if ( ! ( filename || root_path ) ) {
  367. rc = -ENOENT_BOOT;
  368. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  369. goto err_no_boot;
  370. }
  371. /* Boot using next server, filename and root path */
  372. if ( ( rc = uriboot ( filename, root_path, san_default_drive(),
  373. ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
  374. goto err_uriboot;
  375. err_uriboot:
  376. err_no_boot:
  377. uri_put ( root_path );
  378. err_root_path:
  379. uri_put ( filename );
  380. err_filename:
  381. err_pxe_menu_boot:
  382. err_dhcp:
  383. err_ifopen:
  384. return rc;
  385. }
  386. /**
  387. * Boot the system
  388. */
  389. int autoboot ( void ) {
  390. struct net_device *boot_netdev;
  391. struct net_device *netdev;
  392. int rc = -ENODEV;
  393. /* If we have an identifable boot device, try that first */
  394. if ( ( boot_netdev = find_boot_netdev() ) )
  395. rc = netboot ( boot_netdev );
  396. /* If that fails, try booting from any of the other devices */
  397. for_each_netdev ( netdev ) {
  398. if ( netdev == boot_netdev )
  399. continue;
  400. rc = netboot ( netdev );
  401. }
  402. printf ( "No more network devices\n" );
  403. return rc;
  404. }
  405. /**
  406. * Prompt for shell entry
  407. *
  408. * @ret enter_shell User wants to enter shell
  409. */
  410. static int shell_banner ( void ) {
  411. /* Skip prompt if timeout is zero */
  412. if ( BANNER_TIMEOUT <= 0 )
  413. return 0;
  414. /* Prompt user */
  415. printf ( "\n" );
  416. return ( prompt ( "Press Ctrl-B for the iPXE command line...",
  417. ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
  418. CTRL_B ) == 0 );
  419. }
  420. /**
  421. * Main iPXE flow of execution
  422. *
  423. * @v netdev Network device, or NULL
  424. */
  425. void ipxe ( struct net_device *netdev ) {
  426. struct feature *feature;
  427. struct image *image;
  428. char *scriptlet;
  429. /*
  430. * Print welcome banner
  431. *
  432. *
  433. * If you wish to brand this build of iPXE, please do so by
  434. * defining the string PRODUCT_NAME in config/general.h.
  435. *
  436. * While nothing in the GPL prevents you from removing all
  437. * references to iPXE or http://ipxe.org, we prefer you not to
  438. * do so.
  439. *
  440. */
  441. printf ( NORMAL "\n\n" PRODUCT_NAME "\n" BOLD "iPXE %s"
  442. NORMAL " -- Open Source Network Boot Firmware -- "
  443. CYAN "http://ipxe.org" NORMAL "\n"
  444. "Features:", product_version );
  445. for_each_table_entry ( feature, FEATURES )
  446. printf ( " %s", feature->name );
  447. printf ( "\n" );
  448. /* Boot system */
  449. if ( ( image = first_image() ) != NULL ) {
  450. /* We have an embedded image; execute it */
  451. image_exec ( image );
  452. } else if ( shell_banner() ) {
  453. /* User wants shell; just give them a shell */
  454. shell();
  455. } else {
  456. fetch_string_setting_copy ( NULL, &scriptlet_setting,
  457. &scriptlet );
  458. if ( scriptlet ) {
  459. /* User has defined a scriptlet; execute it */
  460. system ( scriptlet );
  461. free ( scriptlet );
  462. } else {
  463. /* Try booting. If booting fails, offer the
  464. * user another chance to enter the shell.
  465. */
  466. if ( netdev ) {
  467. netboot ( netdev );
  468. } else {
  469. autoboot();
  470. }
  471. if ( shell_banner() )
  472. shell();
  473. }
  474. }
  475. }