Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

autoboot.c 15KB

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