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

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