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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <ipxe/netdevice.h>
  23. #include <ipxe/dhcp.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/image.h>
  26. #include <ipxe/sanboot.h>
  27. #include <ipxe/uri.h>
  28. #include <ipxe/open.h>
  29. #include <ipxe/init.h>
  30. #include <usr/ifmgmt.h>
  31. #include <usr/route.h>
  32. #include <usr/dhcpmgmt.h>
  33. #include <usr/imgmgmt.h>
  34. #include <usr/autoboot.h>
  35. /** @file
  36. *
  37. * Automatic booting
  38. *
  39. */
  40. /* Disambiguate the various error causes */
  41. #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
  42. #define EINFO_ENOENT_BOOT \
  43. __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
  44. /**
  45. * Perform PXE menu boot when PXE stack is not available
  46. */
  47. __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
  48. return -ENOTSUP;
  49. }
  50. /**
  51. * Identify the boot network device
  52. *
  53. * @ret netdev Boot network device
  54. */
  55. static struct net_device * find_boot_netdev ( void ) {
  56. return NULL;
  57. }
  58. /**
  59. * Parse next-server and filename into a URI
  60. *
  61. * @v next_server Next-server address
  62. * @v filename Filename
  63. * @ret uri URI, or NULL on failure
  64. */
  65. static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
  66. const char *filename ) {
  67. char buf[ 23 /* "tftp://xxx.xxx.xxx.xxx/" */ + strlen ( filename )
  68. + 1 /* NUL */ ];
  69. struct uri *uri;
  70. /* Parse filename */
  71. uri = parse_uri ( filename );
  72. if ( ! uri )
  73. return NULL;
  74. /* Construct a tftp:// URI for the filename, if applicable.
  75. * We can't just rely on the current working URI, because the
  76. * relative URI resolution will remove the distinction between
  77. * filenames with and without initial slashes, which is
  78. * significant for TFTP.
  79. */
  80. if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
  81. uri_put ( uri );
  82. snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
  83. inet_ntoa ( next_server ), filename );
  84. uri = parse_uri ( buf );
  85. if ( ! uri )
  86. return NULL;
  87. }
  88. return uri;
  89. }
  90. /** The "keep-san" setting */
  91. struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA ) = {
  92. .name = "keep-san",
  93. .description = "Preserve SAN connection",
  94. .tag = DHCP_EB_KEEP_SAN,
  95. .type = &setting_type_int8,
  96. };
  97. /** The "skip-san-boot" setting */
  98. struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA ) = {
  99. .name = "skip-san-boot",
  100. .description = "Do not boot from SAN device",
  101. .tag = DHCP_EB_SKIP_SAN_BOOT,
  102. .type = &setting_type_int8,
  103. };
  104. /**
  105. * Boot from filename and root-path URIs
  106. *
  107. * @v filename Filename
  108. * @v root_path Root path
  109. * @v drive SAN drive (if applicable)
  110. * @v flags Boot action flags
  111. * @ret rc Return status code
  112. *
  113. * The somewhat tortuous flow of control in this function exists in
  114. * order to ensure that the "sanboot" command remains identical in
  115. * function to a SAN boot via a DHCP-specified root path, and to
  116. * provide backwards compatibility for the "keep-san" and
  117. * "skip-san-boot" options.
  118. */
  119. int uriboot ( struct uri *filename, struct uri *root_path, int drive,
  120. unsigned int flags ) {
  121. struct image *image;
  122. int rc;
  123. /* Hook SAN device, if applicable */
  124. if ( root_path ) {
  125. if ( ( rc = san_hook ( root_path, drive ) ) != 0 ) {
  126. printf ( "Could not open SAN device: %s\n",
  127. strerror ( rc ) );
  128. goto err_san_hook;
  129. }
  130. printf ( "Registered SAN device %#02x\n", drive );
  131. }
  132. /* Describe SAN device, if applicable */
  133. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
  134. if ( ( rc = san_describe ( drive ) ) != 0 ) {
  135. printf ( "Could not describe SAN device %#02x: %s\n",
  136. drive, strerror ( rc ) );
  137. goto err_san_describe;
  138. }
  139. }
  140. /* Allow a root-path-only boot with skip-san enabled to succeed */
  141. rc = 0;
  142. /* Attempt filename boot if applicable */
  143. if ( filename ) {
  144. if ( ( rc = imgdownload ( filename, &image ) ) != 0 )
  145. goto err_download;
  146. image->flags |= IMAGE_AUTO_UNREGISTER;
  147. if ( ( rc = image_exec ( image ) ) != 0 ) {
  148. printf ( "Could not boot image: %s\n",
  149. strerror ( rc ) );
  150. /* Fall through to (possibly) attempt a SAN boot
  151. * as a fallback. If no SAN boot is attempted,
  152. * our status will become the return status.
  153. */
  154. } else {
  155. /* Always print an extra newline, because we
  156. * don't know where the NBP may have left the
  157. * cursor.
  158. */
  159. printf ( "\n" );
  160. }
  161. }
  162. /* Attempt SAN boot if applicable */
  163. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
  164. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  165. printf ( "Booting from SAN device %#02x\n", drive );
  166. rc = san_boot ( drive );
  167. printf ( "Boot from SAN device %#02x failed: %s\n",
  168. drive, strerror ( rc ) );
  169. } else {
  170. printf ( "Skipping boot from SAN device %#02x\n",
  171. drive );
  172. /* Avoid overwriting a possible failure status
  173. * from a filename boot.
  174. */
  175. }
  176. }
  177. err_download:
  178. err_san_describe:
  179. /* Unhook SAN device, if applicable */
  180. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
  181. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  182. san_unhook ( drive );
  183. printf ( "Unregistered SAN device %#02x\n", drive );
  184. } else {
  185. printf ( "Preserving SAN device %#02x\n", drive );
  186. }
  187. }
  188. err_san_hook:
  189. return rc;
  190. }
  191. /**
  192. * Close all open net devices
  193. *
  194. * Called before a fresh boot attempt in order to free up memory. We
  195. * don't just close the device immediately after the boot fails,
  196. * because there may still be TCP connections in the process of
  197. * closing.
  198. */
  199. static void close_all_netdevs ( void ) {
  200. struct net_device *netdev;
  201. for_each_netdev ( netdev ) {
  202. ifclose ( netdev );
  203. }
  204. }
  205. /**
  206. * Fetch next-server and filename settings into a URI
  207. *
  208. * @v settings Settings block
  209. * @ret uri URI, or NULL on failure
  210. */
  211. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  212. struct in_addr next_server;
  213. char buf[256];
  214. char *filename;
  215. struct uri *uri;
  216. /* Fetch next-server setting */
  217. fetch_ipv4_setting ( settings, &next_server_setting, &next_server );
  218. if ( next_server.s_addr )
  219. printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
  220. /* Fetch filename setting */
  221. fetch_string_setting ( settings, &filename_setting,
  222. buf, sizeof ( buf ) );
  223. if ( buf[0] )
  224. printf ( "Filename: %s\n", buf );
  225. /* Expand filename setting */
  226. filename = expand_settings ( buf );
  227. if ( ! filename )
  228. return NULL;
  229. /* Parse next server and filename */
  230. uri = parse_next_server_and_filename ( next_server, filename );
  231. free ( filename );
  232. return uri;
  233. }
  234. /**
  235. * Fetch root-path setting into a URI
  236. *
  237. * @v settings Settings block
  238. * @ret uri URI, or NULL on failure
  239. */
  240. static struct uri * fetch_root_path ( struct settings *settings ) {
  241. char buf[256];
  242. char *root_path;
  243. struct uri *uri;
  244. /* Fetch root-path setting */
  245. fetch_string_setting ( settings, &root_path_setting,
  246. buf, sizeof ( buf ) );
  247. if ( buf[0] )
  248. printf ( "Root path: %s\n", buf );
  249. /* Expand filename setting */
  250. root_path = expand_settings ( buf );
  251. if ( ! root_path )
  252. return NULL;
  253. /* Parse root path */
  254. uri = parse_uri ( root_path );
  255. free ( root_path );
  256. return uri;
  257. }
  258. /**
  259. * Check whether or not we have a usable PXE menu
  260. *
  261. * @ret have_menu A usable PXE menu is present
  262. */
  263. static int have_pxe_menu ( void ) {
  264. struct setting vendor_class_id_setting
  265. = { .tag = DHCP_VENDOR_CLASS_ID };
  266. struct setting pxe_discovery_control_setting
  267. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  268. struct setting pxe_boot_menu_setting
  269. = { .tag = DHCP_PXE_BOOT_MENU };
  270. char buf[256];
  271. unsigned int pxe_discovery_control;
  272. fetch_string_setting ( NULL, &vendor_class_id_setting,
  273. buf, sizeof ( buf ) );
  274. pxe_discovery_control =
  275. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  276. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  277. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  278. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  279. setting_exists ( NULL, &filename_setting ) ) ) );
  280. }
  281. /**
  282. * Boot from a network device
  283. *
  284. * @v netdev Network device
  285. * @ret rc Return status code
  286. */
  287. int netboot ( struct net_device *netdev ) {
  288. struct uri *filename;
  289. struct uri *root_path;
  290. int rc;
  291. /* Close all other network devices */
  292. close_all_netdevs();
  293. /* Open device and display device status */
  294. if ( ( rc = ifopen ( netdev ) ) != 0 )
  295. goto err_ifopen;
  296. ifstat ( netdev );
  297. /* Configure device via DHCP */
  298. if ( ( rc = dhcp ( netdev ) ) != 0 )
  299. goto err_dhcp;
  300. route();
  301. /* Try PXE menu boot, if applicable */
  302. if ( have_pxe_menu() ) {
  303. printf ( "Booting from PXE menu\n" );
  304. rc = pxe_menu_boot ( netdev );
  305. goto err_pxe_menu_boot;
  306. }
  307. /* Fetch next server and filename */
  308. filename = fetch_next_server_and_filename ( NULL );
  309. if ( ! filename )
  310. goto err_filename;
  311. if ( ! uri_has_path ( filename ) ) {
  312. /* Ignore empty filename */
  313. uri_put ( filename );
  314. filename = NULL;
  315. }
  316. /* Fetch root path */
  317. root_path = fetch_root_path ( NULL );
  318. if ( ! root_path )
  319. goto err_root_path;
  320. if ( ! uri_is_absolute ( root_path ) ) {
  321. /* Ignore empty root path */
  322. uri_put ( root_path );
  323. root_path = NULL;
  324. }
  325. /* If we have both a filename and a root path, ignore an
  326. * unsupported URI scheme in the root path, since it may
  327. * represent an NFS root.
  328. */
  329. if ( filename && root_path &&
  330. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
  331. printf ( "Ignoring unsupported root path\n" );
  332. uri_put ( root_path );
  333. root_path = NULL;
  334. }
  335. /* Check that we have something to boot */
  336. if ( ! ( filename || root_path ) ) {
  337. rc = -ENOENT_BOOT;
  338. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  339. goto err_no_boot;
  340. }
  341. /* Boot using next server, filename and root path */
  342. if ( ( rc = uriboot ( filename, root_path, san_default_drive(),
  343. ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
  344. goto err_uriboot;
  345. err_uriboot:
  346. err_no_boot:
  347. uri_put ( root_path );
  348. err_root_path:
  349. uri_put ( filename );
  350. err_filename:
  351. err_pxe_menu_boot:
  352. err_dhcp:
  353. err_ifopen:
  354. return rc;
  355. }
  356. /**
  357. * Boot the system
  358. */
  359. int autoboot ( void ) {
  360. struct net_device *boot_netdev;
  361. struct net_device *netdev;
  362. int rc = -ENODEV;
  363. /* If we have an identifable boot device, try that first */
  364. if ( ( boot_netdev = find_boot_netdev() ) )
  365. rc = netboot ( boot_netdev );
  366. /* If that fails, try booting from any of the other devices */
  367. for_each_netdev ( netdev ) {
  368. if ( netdev == boot_netdev )
  369. continue;
  370. rc = netboot ( netdev );
  371. }
  372. printf ( "No more network devices\n" );
  373. return rc;
  374. }