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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. 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 ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
  163. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  164. printf ( "Booting from SAN device %#02x\n", drive );
  165. rc = san_boot ( drive );
  166. printf ( "Boot from SAN device %#02x failed: %s\n",
  167. drive, strerror ( rc ) );
  168. } else {
  169. printf ( "Skipping boot from SAN device %#02x\n",
  170. drive );
  171. /* Avoid overwriting a possible failure status
  172. * from a filename boot.
  173. */
  174. }
  175. }
  176. err_download:
  177. err_san_describe:
  178. /* Unhook SAN device, if applicable */
  179. if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
  180. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  181. san_unhook ( drive );
  182. printf ( "Unregistered SAN device %#02x\n", drive );
  183. } else {
  184. printf ( "Preserving SAN device %#02x\n", drive );
  185. }
  186. }
  187. err_san_hook:
  188. return rc;
  189. }
  190. /**
  191. * Close all open net devices
  192. *
  193. * Called before a fresh boot attempt in order to free up memory. We
  194. * don't just close the device immediately after the boot fails,
  195. * because there may still be TCP connections in the process of
  196. * closing.
  197. */
  198. static void close_all_netdevs ( void ) {
  199. struct net_device *netdev;
  200. for_each_netdev ( netdev ) {
  201. ifclose ( netdev );
  202. }
  203. }
  204. /**
  205. * Fetch next-server and filename settings into a URI
  206. *
  207. * @v settings Settings block
  208. * @ret uri URI, or NULL on failure
  209. */
  210. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  211. struct in_addr next_server;
  212. char buf[256];
  213. char *filename;
  214. struct uri *uri;
  215. /* Fetch next-server setting */
  216. fetch_ipv4_setting ( settings, &next_server_setting, &next_server );
  217. if ( next_server.s_addr )
  218. printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
  219. /* Fetch filename setting */
  220. fetch_string_setting ( settings, &filename_setting,
  221. buf, sizeof ( buf ) );
  222. if ( buf[0] )
  223. printf ( "Filename: %s\n", buf );
  224. /* Expand filename setting */
  225. filename = expand_settings ( buf );
  226. if ( ! filename )
  227. return NULL;
  228. /* Parse next server and filename */
  229. uri = parse_next_server_and_filename ( next_server, filename );
  230. free ( filename );
  231. return uri;
  232. }
  233. /**
  234. * Fetch root-path setting into a URI
  235. *
  236. * @v settings Settings block
  237. * @ret uri URI, or NULL on failure
  238. */
  239. static struct uri * fetch_root_path ( struct settings *settings ) {
  240. char buf[256];
  241. char *root_path;
  242. struct uri *uri;
  243. /* Fetch root-path setting */
  244. fetch_string_setting ( settings, &root_path_setting,
  245. buf, sizeof ( buf ) );
  246. if ( buf[0] )
  247. printf ( "Root path: %s\n", buf );
  248. /* Expand filename setting */
  249. root_path = expand_settings ( buf );
  250. if ( ! root_path )
  251. return NULL;
  252. /* Parse root path */
  253. uri = parse_uri ( root_path );
  254. free ( root_path );
  255. return uri;
  256. }
  257. /**
  258. * Check whether or not we have a usable PXE menu
  259. *
  260. * @ret have_menu A usable PXE menu is present
  261. */
  262. static int have_pxe_menu ( void ) {
  263. struct setting vendor_class_id_setting
  264. = { .tag = DHCP_VENDOR_CLASS_ID };
  265. struct setting pxe_discovery_control_setting
  266. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  267. struct setting pxe_boot_menu_setting
  268. = { .tag = DHCP_PXE_BOOT_MENU };
  269. char buf[256];
  270. unsigned int pxe_discovery_control;
  271. fetch_string_setting ( NULL, &vendor_class_id_setting,
  272. buf, sizeof ( buf ) );
  273. pxe_discovery_control =
  274. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  275. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  276. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  277. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  278. setting_exists ( NULL, &filename_setting ) ) ) );
  279. }
  280. /**
  281. * Boot from a network device
  282. *
  283. * @v netdev Network device
  284. * @ret rc Return status code
  285. */
  286. int netboot ( struct net_device *netdev ) {
  287. struct uri *filename;
  288. struct uri *root_path;
  289. int rc;
  290. /* Close all other network devices */
  291. close_all_netdevs();
  292. /* Open device and display device status */
  293. if ( ( rc = ifopen ( netdev ) ) != 0 )
  294. goto err_ifopen;
  295. ifstat ( netdev );
  296. /* Configure device via DHCP */
  297. if ( ( rc = dhcp ( netdev ) ) != 0 )
  298. goto err_dhcp;
  299. route();
  300. /* Try PXE menu boot, if applicable */
  301. if ( have_pxe_menu() ) {
  302. printf ( "Booting from PXE menu\n" );
  303. rc = pxe_menu_boot ( netdev );
  304. goto err_pxe_menu_boot;
  305. }
  306. /* Fetch next server and filename */
  307. filename = fetch_next_server_and_filename ( NULL );
  308. if ( ! filename )
  309. goto err_filename;
  310. if ( ! uri_has_path ( filename ) ) {
  311. /* Ignore empty filename */
  312. uri_put ( filename );
  313. filename = NULL;
  314. }
  315. /* Fetch root path */
  316. root_path = fetch_root_path ( NULL );
  317. if ( ! root_path )
  318. goto err_root_path;
  319. if ( ! uri_is_absolute ( root_path ) ) {
  320. /* Ignore empty root path */
  321. uri_put ( root_path );
  322. root_path = NULL;
  323. }
  324. /* If we have both a filename and a root path, ignore an
  325. * unsupported URI scheme in the root path, since it may
  326. * represent an NFS root.
  327. */
  328. if ( filename && root_path &&
  329. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
  330. printf ( "Ignoring unsupported root path\n" );
  331. uri_put ( root_path );
  332. root_path = NULL;
  333. }
  334. /* Check that we have something to boot */
  335. if ( ! ( filename || root_path ) ) {
  336. rc = -ENOENT_BOOT;
  337. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  338. goto err_no_boot;
  339. }
  340. /* Boot using next server, filename and root path */
  341. if ( ( rc = uriboot ( filename, root_path, san_default_drive(),
  342. ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
  343. goto err_uriboot;
  344. err_uriboot:
  345. err_no_boot:
  346. uri_put ( root_path );
  347. err_root_path:
  348. uri_put ( filename );
  349. err_filename:
  350. err_pxe_menu_boot:
  351. err_dhcp:
  352. err_ifopen:
  353. return rc;
  354. }
  355. /**
  356. * Boot the system
  357. */
  358. int autoboot ( void ) {
  359. struct net_device *boot_netdev;
  360. struct net_device *netdev;
  361. int rc = -ENODEV;
  362. /* If we have an identifable boot device, try that first */
  363. if ( ( boot_netdev = find_boot_netdev() ) )
  364. rc = netboot ( boot_netdev );
  365. /* If that fails, try booting from any of the other devices */
  366. for_each_netdev ( netdev ) {
  367. if ( netdev == boot_netdev )
  368. continue;
  369. rc = netboot ( netdev );
  370. }
  371. printf ( "No more network devices\n" );
  372. return rc;
  373. }