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

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