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

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