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

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