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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/dhcp.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/image.h>
  31. #include <ipxe/sanboot.h>
  32. #include <ipxe/uri.h>
  33. #include <ipxe/open.h>
  34. #include <ipxe/init.h>
  35. #include <ipxe/keys.h>
  36. #include <ipxe/version.h>
  37. #include <ipxe/shell.h>
  38. #include <ipxe/features.h>
  39. #include <ipxe/image.h>
  40. #include <ipxe/timer.h>
  41. #include <usr/ifmgmt.h>
  42. #include <usr/route.h>
  43. #include <usr/imgmgmt.h>
  44. #include <usr/prompt.h>
  45. #include <usr/autoboot.h>
  46. #include <config/general.h>
  47. #include <config/branding.h>
  48. /** @file
  49. *
  50. * Automatic booting
  51. *
  52. */
  53. /** Link-layer address of preferred autoboot device, if known */
  54. static uint8_t autoboot_ll_addr[MAX_LL_ADDR_LEN];
  55. /** Device location of preferred autoboot device, if known */
  56. static struct device_description autoboot_desc;
  57. /** Autoboot device tester */
  58. static int ( * is_autoboot_device ) ( struct net_device *netdev );
  59. /* Disambiguate the various error causes */
  60. #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
  61. #define EINFO_ENOENT_BOOT \
  62. __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
  63. #define NORMAL "\033[0m"
  64. #define BOLD "\033[1m"
  65. #define CYAN "\033[36m"
  66. /** The "scriptlet" setting */
  67. const struct setting scriptlet_setting __setting ( SETTING_MISC, scriptlet ) = {
  68. .name = "scriptlet",
  69. .description = "Boot scriptlet",
  70. .tag = DHCP_EB_SCRIPTLET,
  71. .type = &setting_type_string,
  72. };
  73. /**
  74. * Perform PXE menu boot when PXE stack is not available
  75. */
  76. __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
  77. return -ENOTSUP;
  78. }
  79. /** The "keep-san" setting */
  80. const struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA,
  81. keep-san ) = {
  82. .name = "keep-san",
  83. .description = "Preserve SAN connection",
  84. .tag = DHCP_EB_KEEP_SAN,
  85. .type = &setting_type_int8,
  86. };
  87. /** The "skip-san-boot" setting */
  88. const struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA,
  89. skip-san-boot ) = {
  90. .name = "skip-san-boot",
  91. .description = "Do not boot from SAN device",
  92. .tag = DHCP_EB_SKIP_SAN_BOOT,
  93. .type = &setting_type_int8,
  94. };
  95. /**
  96. * Boot from filename and root-path URIs
  97. *
  98. * @v filename Filename
  99. * @v root_paths Root path(s)
  100. * @v root_path_count Number of root paths
  101. * @v drive SAN drive (if applicable)
  102. * @v san_filename SAN filename (or NULL to use default)
  103. * @v flags Boot action flags
  104. * @ret rc Return status code
  105. *
  106. * The somewhat tortuous flow of control in this function exists in
  107. * order to ensure that the "sanboot" command remains identical in
  108. * function to a SAN boot via a DHCP-specified root path, and to
  109. * provide backwards compatibility for the "keep-san" and
  110. * "skip-san-boot" options.
  111. */
  112. int uriboot ( struct uri *filename, struct uri **root_paths,
  113. unsigned int root_path_count, int drive,
  114. const char *san_filename, unsigned int flags ) {
  115. struct image *image;
  116. int rc;
  117. /* Hook SAN device, if applicable */
  118. if ( root_path_count ) {
  119. drive = san_hook ( drive, root_paths, root_path_count,
  120. ( ( flags & URIBOOT_NO_SAN_DESCRIBE ) ?
  121. SAN_NO_DESCRIBE : 0 ) );
  122. if ( drive < 0 ) {
  123. rc = drive;
  124. printf ( "Could not open SAN device: %s\n",
  125. strerror ( rc ) );
  126. goto err_san_hook;
  127. }
  128. printf ( "Registered SAN device %#02x\n", drive );
  129. }
  130. /* Describe SAN device, if applicable */
  131. if ( ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
  132. if ( ( rc = san_describe() ) != 0 ) {
  133. printf ( "Could not describe SAN devices: %s\n",
  134. strerror ( rc ) );
  135. goto err_san_describe;
  136. }
  137. }
  138. /* Allow a root-path-only boot with skip-san enabled to succeed */
  139. rc = 0;
  140. /* Attempt filename boot if applicable */
  141. if ( filename ) {
  142. if ( ( rc = imgdownload ( filename, 0, &image ) ) != 0 )
  143. goto err_download;
  144. imgstat ( image );
  145. image->flags |= IMAGE_AUTO_UNREGISTER;
  146. if ( ( rc = image_exec ( image ) ) != 0 ) {
  147. printf ( "Could not boot image: %s\n",
  148. strerror ( rc ) );
  149. /* Fall through to (possibly) attempt a SAN boot
  150. * as a fallback. If no SAN boot is attempted,
  151. * our status will become the return status.
  152. */
  153. } else {
  154. /* Always print an extra newline, because we
  155. * don't know where the NBP may have left the
  156. * cursor.
  157. */
  158. printf ( "\n" );
  159. }
  160. }
  161. /* Attempt SAN boot if applicable */
  162. if ( ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
  163. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  164. printf ( "Booting%s%s from SAN device %#02x\n",
  165. ( san_filename ? " " : "" ),
  166. ( san_filename ? san_filename : "" ), drive );
  167. rc = san_boot ( drive, san_filename );
  168. printf ( "Boot from SAN device %#02x failed: %s\n",
  169. drive, strerror ( rc ) );
  170. } else {
  171. printf ( "Skipping boot from SAN device %#02x\n",
  172. drive );
  173. /* Avoid overwriting a possible failure status
  174. * from a filename boot.
  175. */
  176. }
  177. }
  178. err_download:
  179. err_san_describe:
  180. /* Unhook SAN device, if applicable */
  181. if ( ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
  182. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  183. san_unhook ( drive );
  184. printf ( "Unregistered SAN device %#02x\n", drive );
  185. } else {
  186. printf ( "Preserving SAN device %#02x\n", drive );
  187. }
  188. }
  189. err_san_hook:
  190. return rc;
  191. }
  192. /**
  193. * Close all open net devices
  194. *
  195. * Called before a fresh boot attempt in order to free up memory. We
  196. * don't just close the device immediately after the boot fails,
  197. * because there may still be TCP connections in the process of
  198. * closing.
  199. */
  200. static void close_all_netdevs ( void ) {
  201. struct net_device *netdev;
  202. for_each_netdev ( netdev ) {
  203. ifclose ( netdev );
  204. }
  205. }
  206. /**
  207. * Fetch next-server and filename settings into a URI
  208. *
  209. * @v settings Settings block
  210. * @ret uri URI, or NULL on failure
  211. */
  212. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  213. union {
  214. struct sockaddr sa;
  215. struct sockaddr_in sin;
  216. } next_server;
  217. char *raw_filename = NULL;
  218. struct uri *uri = NULL;
  219. char *filename;
  220. /* Initialise server address */
  221. memset ( &next_server, 0, sizeof ( next_server ) );
  222. /* If we have a filename, fetch it along with the next-server
  223. * setting from the same settings block.
  224. */
  225. if ( fetch_setting ( settings, &filename_setting, &settings,
  226. NULL, NULL, 0 ) >= 0 ) {
  227. fetch_string_setting_copy ( settings, &filename_setting,
  228. &raw_filename );
  229. fetch_ipv4_setting ( settings, &next_server_setting,
  230. &next_server.sin.sin_addr );
  231. }
  232. if ( ! raw_filename )
  233. goto err_fetch;
  234. /* Populate server address */
  235. if ( next_server.sin.sin_addr.s_addr ) {
  236. next_server.sin.sin_family = AF_INET;
  237. printf ( "Next server: %s\n",
  238. inet_ntoa ( next_server.sin.sin_addr ) );
  239. }
  240. /* Expand filename setting */
  241. filename = expand_settings ( raw_filename );
  242. if ( ! filename )
  243. goto err_expand;
  244. if ( filename[0] )
  245. printf ( "Filename: %s\n", filename );
  246. /* Construct URI */
  247. uri = pxe_uri ( &next_server.sa, filename );
  248. if ( ! uri )
  249. goto err_parse;
  250. err_parse:
  251. free ( filename );
  252. err_expand:
  253. free ( raw_filename );
  254. err_fetch:
  255. return uri;
  256. }
  257. /**
  258. * Fetch root-path setting into a URI
  259. *
  260. * @v settings Settings block
  261. * @ret uri URI, or NULL on failure
  262. */
  263. static struct uri * fetch_root_path ( struct settings *settings ) {
  264. struct uri *uri = NULL;
  265. char *raw_root_path;
  266. char *root_path;
  267. /* Fetch root-path setting */
  268. fetch_string_setting_copy ( settings, &root_path_setting,
  269. &raw_root_path );
  270. if ( ! raw_root_path )
  271. goto err_fetch;
  272. /* Expand filename setting */
  273. root_path = expand_settings ( raw_root_path );
  274. if ( ! root_path )
  275. goto err_expand;
  276. if ( root_path[0] )
  277. printf ( "Root path: %s\n", root_path );
  278. /* Parse root path */
  279. uri = parse_uri ( root_path );
  280. if ( ! uri )
  281. goto err_parse;
  282. err_parse:
  283. free ( root_path );
  284. err_expand:
  285. free ( raw_root_path );
  286. err_fetch:
  287. return uri;
  288. }
  289. /**
  290. * Fetch san-filename setting
  291. *
  292. * @v settings Settings block
  293. * @ret san_filename SAN filename, or NULL on failure
  294. */
  295. static char * fetch_san_filename ( struct settings *settings ) {
  296. char *raw_san_filename;
  297. char *san_filename = NULL;
  298. /* Fetch san-filename setting */
  299. fetch_string_setting_copy ( settings, &san_filename_setting,
  300. &raw_san_filename );
  301. if ( ! raw_san_filename )
  302. goto err_fetch;
  303. /* Expand san-filename setting */
  304. san_filename = expand_settings ( raw_san_filename );
  305. if ( ! san_filename )
  306. goto err_expand;
  307. if ( san_filename[0] )
  308. printf ( "SAN filename: %s\n", san_filename );
  309. err_expand:
  310. free ( raw_san_filename );
  311. err_fetch:
  312. return san_filename;
  313. }
  314. /**
  315. * Check whether or not we have a usable PXE menu
  316. *
  317. * @ret have_menu A usable PXE menu is present
  318. */
  319. static int have_pxe_menu ( void ) {
  320. struct setting vendor_class_id_setting
  321. = { .tag = DHCP_VENDOR_CLASS_ID };
  322. struct setting pxe_discovery_control_setting
  323. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  324. struct setting pxe_boot_menu_setting
  325. = { .tag = DHCP_PXE_BOOT_MENU };
  326. char buf[ 10 /* "PXEClient" + NUL */ ];
  327. unsigned int pxe_discovery_control;
  328. fetch_string_setting ( NULL, &vendor_class_id_setting,
  329. buf, sizeof ( buf ) );
  330. pxe_discovery_control =
  331. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  332. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  333. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  334. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  335. setting_exists ( NULL, &filename_setting ) ) ) );
  336. }
  337. /**
  338. * Boot from a network device
  339. *
  340. * @v netdev Network device
  341. * @ret rc Return status code
  342. */
  343. int netboot ( struct net_device *netdev ) {
  344. struct uri *filename;
  345. struct uri *root_path;
  346. char *san_filename;
  347. int rc;
  348. /* Close all other network devices */
  349. close_all_netdevs();
  350. /* Open device and display device status */
  351. if ( ( rc = ifopen ( netdev ) ) != 0 )
  352. goto err_ifopen;
  353. ifstat ( netdev );
  354. /* Configure device */
  355. if ( ( rc = ifconf ( netdev, NULL ) ) != 0 )
  356. goto err_dhcp;
  357. route();
  358. /* Try PXE menu boot, if applicable */
  359. if ( have_pxe_menu() ) {
  360. printf ( "Booting from PXE menu\n" );
  361. rc = pxe_menu_boot ( netdev );
  362. goto err_pxe_menu_boot;
  363. }
  364. /* Fetch next server and filename (if any) */
  365. filename = fetch_next_server_and_filename ( NULL );
  366. /* Fetch root path (if any) */
  367. root_path = fetch_root_path ( NULL );
  368. /* Fetch SAN filename (if any) */
  369. san_filename = fetch_san_filename ( NULL );
  370. /* If we have both a filename and a root path, ignore an
  371. * unsupported or missing URI scheme in the root path, since
  372. * it may represent an NFS root.
  373. */
  374. if ( filename && root_path &&
  375. ( ( ! uri_is_absolute ( root_path ) ) ||
  376. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) ) {
  377. printf ( "Ignoring unsupported root path\n" );
  378. uri_put ( root_path );
  379. root_path = NULL;
  380. }
  381. /* Check that we have something to boot */
  382. if ( ! ( filename || root_path ) ) {
  383. rc = -ENOENT_BOOT;
  384. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  385. goto err_no_boot;
  386. }
  387. /* Boot using next server, filename and root path */
  388. if ( ( rc = uriboot ( filename, &root_path, ( root_path ? 1 : 0 ),
  389. san_default_drive(), san_filename,
  390. ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
  391. goto err_uriboot;
  392. err_uriboot:
  393. err_no_boot:
  394. free ( san_filename );
  395. uri_put ( root_path );
  396. uri_put ( filename );
  397. err_pxe_menu_boot:
  398. err_dhcp:
  399. err_ifopen:
  400. return rc;
  401. }
  402. /**
  403. * Test if network device matches the autoboot device bus type and location
  404. *
  405. * @v netdev Network device
  406. * @ret is_autoboot Network device matches the autoboot device
  407. */
  408. static int is_autoboot_busloc ( struct net_device *netdev ) {
  409. struct device *dev;
  410. for ( dev = netdev->dev ; dev ; dev = dev->parent ) {
  411. if ( ( dev->desc.bus_type == autoboot_desc.bus_type ) &&
  412. ( dev->desc.location == autoboot_desc.location ) )
  413. return 1;
  414. }
  415. return 0;
  416. }
  417. /**
  418. * Identify autoboot device by bus type and location
  419. *
  420. * @v bus_type Bus type
  421. * @v location Location
  422. */
  423. void set_autoboot_busloc ( unsigned int bus_type, unsigned int location ) {
  424. /* Record autoboot device description */
  425. autoboot_desc.bus_type = bus_type;
  426. autoboot_desc.location = location;
  427. /* Mark autoboot device as present */
  428. is_autoboot_device = is_autoboot_busloc;
  429. }
  430. /**
  431. * Test if network device matches the autoboot device link-layer address
  432. *
  433. * @v netdev Network device
  434. * @ret is_autoboot Network device matches the autoboot device
  435. */
  436. static int is_autoboot_ll_addr ( struct net_device *netdev ) {
  437. return ( memcmp ( netdev->ll_addr, autoboot_ll_addr,
  438. netdev->ll_protocol->ll_addr_len ) == 0 );
  439. }
  440. /**
  441. * Identify autoboot device by link-layer address
  442. *
  443. * @v ll_addr Link-layer address
  444. * @v len Length of link-layer address
  445. */
  446. void set_autoboot_ll_addr ( const void *ll_addr, size_t len ) {
  447. /* Record autoboot link-layer address (truncated if necessary) */
  448. if ( len > sizeof ( autoboot_ll_addr ) )
  449. len = sizeof ( autoboot_ll_addr );
  450. memcpy ( autoboot_ll_addr, ll_addr, len );
  451. /* Mark autoboot device as present */
  452. is_autoboot_device = is_autoboot_ll_addr;
  453. }
  454. /**
  455. * Boot the system
  456. */
  457. static int autoboot ( void ) {
  458. struct net_device *netdev;
  459. int rc = -ENODEV;
  460. /* Try booting from each network device. If we have a
  461. * specified autoboot device location, then use only devices
  462. * matching that location.
  463. */
  464. for_each_netdev ( netdev ) {
  465. /* Skip any non-matching devices, if applicable */
  466. if ( is_autoboot_device && ( ! is_autoboot_device ( netdev ) ) )
  467. continue;
  468. /* Attempt booting from this device */
  469. rc = netboot ( netdev );
  470. }
  471. printf ( "No more network devices\n" );
  472. return rc;
  473. }
  474. /**
  475. * Prompt for shell entry
  476. *
  477. * @ret enter_shell User wants to enter shell
  478. */
  479. static int shell_banner ( void ) {
  480. /* Skip prompt if timeout is zero */
  481. if ( BANNER_TIMEOUT <= 0 )
  482. return 0;
  483. /* Prompt user */
  484. printf ( "\n" );
  485. return ( prompt ( "Press Ctrl-B for the " PRODUCT_SHORT_NAME
  486. " command line...",
  487. ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
  488. CTRL_B ) == 0 );
  489. }
  490. /**
  491. * Main iPXE flow of execution
  492. *
  493. * @v netdev Network device, or NULL
  494. * @ret rc Return status code
  495. */
  496. int ipxe ( struct net_device *netdev ) {
  497. struct feature *feature;
  498. struct image *image;
  499. char *scriptlet;
  500. int rc;
  501. /*
  502. * Print welcome banner
  503. *
  504. *
  505. * If you wish to brand this build of iPXE, please do so by
  506. * defining the string PRODUCT_NAME in config/branding.h.
  507. *
  508. * While nothing in the GPL prevents you from removing all
  509. * references to iPXE or http://ipxe.org, we prefer you not to
  510. * do so.
  511. *
  512. */
  513. printf ( NORMAL "\n\n" PRODUCT_NAME "\n" BOLD PRODUCT_SHORT_NAME " %s"
  514. NORMAL " -- " PRODUCT_TAG_LINE " -- "
  515. CYAN PRODUCT_URI NORMAL "\nFeatures:", product_version );
  516. for_each_table_entry ( feature, FEATURES )
  517. printf ( " %s", feature->name );
  518. printf ( "\n" );
  519. /* Boot system */
  520. if ( ( image = first_image() ) != NULL ) {
  521. /* We have an embedded image; execute it */
  522. return image_exec ( image );
  523. } else if ( shell_banner() ) {
  524. /* User wants shell; just give them a shell */
  525. return shell();
  526. } else {
  527. fetch_string_setting_copy ( NULL, &scriptlet_setting,
  528. &scriptlet );
  529. if ( scriptlet ) {
  530. /* User has defined a scriptlet; execute it */
  531. rc = system ( scriptlet );
  532. free ( scriptlet );
  533. return rc;
  534. } else {
  535. /* Try booting. If booting fails, offer the
  536. * user another chance to enter the shell.
  537. */
  538. if ( netdev ) {
  539. rc = netboot ( netdev );
  540. } else {
  541. rc = autoboot();
  542. }
  543. if ( shell_banner() )
  544. rc = shell();
  545. return rc;
  546. }
  547. }
  548. }