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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = {
  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 = {
  99. .name = "skip-san-boot",
  100. .description = "Do not boot the SAN drive after connecting",
  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. * @ret rc Return status code
  110. */
  111. int uriboot ( struct uri *filename, struct uri *root_path ) {
  112. struct image *image;
  113. int drive;
  114. int rc;
  115. /* Allocate image */
  116. image = alloc_image();
  117. if ( ! image ) {
  118. printf ( "Could not allocate image\n" );
  119. rc = -ENOMEM;
  120. goto err_alloc_image;
  121. }
  122. /* Treat empty URIs as absent */
  123. if ( filename && ( ! uri_has_path ( filename ) ) )
  124. filename = NULL;
  125. if ( root_path && ( ! uri_is_absolute ( root_path ) ) )
  126. root_path = NULL;
  127. /* If we have both a filename and a root path, ignore an
  128. * unsupported URI scheme in the root path, since it may
  129. * represent an NFS root.
  130. */
  131. if ( filename && root_path &&
  132. ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
  133. printf ( "Ignoring unsupported root path\n" );
  134. root_path = NULL;
  135. }
  136. /* Check that we have something to boot */
  137. if ( ! ( filename || root_path ) ) {
  138. rc = -ENOENT_BOOT;
  139. printf ( "Nothing to boot: %s\n", strerror ( rc ) );
  140. goto err_no_boot;
  141. }
  142. /* Hook SAN device, if applicable */
  143. if ( root_path ) {
  144. drive = san_hook ( root_path, 0 );
  145. if ( drive < 0 ) {
  146. rc = drive;
  147. printf ( "Could not open SAN device: %s\n",
  148. strerror ( rc ) );
  149. goto err_san_hook;
  150. }
  151. printf ( "Registered as SAN device %#02x\n", drive );
  152. } else {
  153. drive = -ENODEV;
  154. }
  155. /* Describe SAN device, if applicable */
  156. if ( ( drive >= 0 ) && ( ( rc = san_describe ( drive ) ) != 0 ) ) {
  157. printf ( "Could not describe SAN device %#02x: %s\n",
  158. drive, strerror ( rc ) );
  159. goto err_san_describe;
  160. }
  161. /* Allow a root-path-only boot with skip-san enabled to succeed */
  162. rc = 0;
  163. /* Attempt filename boot if applicable */
  164. if ( filename ) {
  165. if ( ( rc = imgdownload ( image, filename,
  166. register_and_boot_image ) ) != 0 ) {
  167. printf ( "\nCould not chain image: %s\n",
  168. strerror ( rc ) );
  169. /* Fall through to (possibly) attempt a SAN boot
  170. * as a fallback. If no SAN boot is attempted,
  171. * our status will become the return status.
  172. */
  173. } else {
  174. /* Always print an extra newline, because we
  175. * don't know where the NBP may have left the
  176. * cursor.
  177. */
  178. printf ( "\n" );
  179. }
  180. }
  181. /* Attempt SAN boot if applicable */
  182. if ( root_path ) {
  183. if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
  184. printf ( "Booting from SAN device %#02x\n", drive );
  185. rc = san_boot ( drive );
  186. printf ( "Boot from SAN device %#02x failed: %s\n",
  187. drive, strerror ( rc ) );
  188. } else {
  189. printf ( "Skipping boot from SAN device %#02x\n",
  190. drive );
  191. /* Avoid overwriting a possible failure status
  192. * from a filename boot.
  193. */
  194. }
  195. }
  196. err_san_describe:
  197. /* Unhook SAN device, if applicable */
  198. if ( drive >= 0 ) {
  199. if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
  200. printf ( "Unregistering SAN device %#02x\n", drive );
  201. san_unhook ( drive );
  202. } else {
  203. printf ( "Preserving connection to SAN device %#02x\n",
  204. drive );
  205. }
  206. }
  207. err_san_hook:
  208. err_no_boot:
  209. image_put ( image );
  210. err_alloc_image:
  211. return rc;
  212. }
  213. /**
  214. * Close all open net devices
  215. *
  216. * Called before a fresh boot attempt in order to free up memory. We
  217. * don't just close the device immediately after the boot fails,
  218. * because there may still be TCP connections in the process of
  219. * closing.
  220. */
  221. static void close_all_netdevs ( void ) {
  222. struct net_device *netdev;
  223. for_each_netdev ( netdev ) {
  224. ifclose ( netdev );
  225. }
  226. }
  227. /**
  228. * Fetch next-server and filename settings into a URI
  229. *
  230. * @v settings Settings block
  231. * @ret uri URI, or NULL on failure
  232. */
  233. struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
  234. struct in_addr next_server;
  235. char buf[256];
  236. char *filename;
  237. struct uri *uri;
  238. /* Fetch next-server setting */
  239. fetch_ipv4_setting ( settings, &next_server_setting, &next_server );
  240. if ( next_server.s_addr )
  241. printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
  242. /* Fetch filename setting */
  243. fetch_string_setting ( settings, &filename_setting,
  244. buf, sizeof ( buf ) );
  245. if ( buf[0] )
  246. printf ( "Filename: %s\n", buf );
  247. /* Expand filename setting */
  248. filename = expand_settings ( buf );
  249. if ( ! filename )
  250. return NULL;
  251. /* Parse next server and filename */
  252. uri = parse_next_server_and_filename ( next_server, filename );
  253. free ( filename );
  254. return uri;
  255. }
  256. /**
  257. * Fetch root-path setting into a URI
  258. *
  259. * @v settings Settings block
  260. * @ret uri URI, or NULL on failure
  261. */
  262. static struct uri * fetch_root_path ( struct settings *settings ) {
  263. char buf[256];
  264. char *root_path;
  265. struct uri *uri;
  266. /* Fetch root-path setting */
  267. fetch_string_setting ( settings, &root_path_setting,
  268. buf, sizeof ( buf ) );
  269. if ( buf[0] )
  270. printf ( "Root path: %s\n", buf );
  271. /* Expand filename setting */
  272. root_path = expand_settings ( buf );
  273. if ( ! root_path )
  274. return NULL;
  275. /* Parse root path */
  276. uri = parse_uri ( root_path );
  277. free ( root_path );
  278. return uri;
  279. }
  280. /**
  281. * Check whether or not we have a usable PXE menu
  282. *
  283. * @ret have_menu A usable PXE menu is present
  284. */
  285. static int have_pxe_menu ( void ) {
  286. struct setting vendor_class_id_setting
  287. = { .tag = DHCP_VENDOR_CLASS_ID };
  288. struct setting pxe_discovery_control_setting
  289. = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
  290. struct setting pxe_boot_menu_setting
  291. = { .tag = DHCP_PXE_BOOT_MENU };
  292. char buf[256];
  293. unsigned int pxe_discovery_control;
  294. fetch_string_setting ( NULL, &vendor_class_id_setting,
  295. buf, sizeof ( buf ) );
  296. pxe_discovery_control =
  297. fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
  298. return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
  299. setting_exists ( NULL, &pxe_boot_menu_setting ) &&
  300. ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
  301. setting_exists ( NULL, &filename_setting ) ) ) );
  302. }
  303. /**
  304. * Boot from a network device
  305. *
  306. * @v netdev Network device
  307. * @ret rc Return status code
  308. */
  309. int netboot ( struct net_device *netdev ) {
  310. struct uri *filename;
  311. struct uri *root_path;
  312. int rc;
  313. /* Close all other network devices */
  314. close_all_netdevs();
  315. /* Open device and display device status */
  316. if ( ( rc = ifopen ( netdev ) ) != 0 )
  317. goto err_ifopen;
  318. ifstat ( netdev );
  319. /* Configure device via DHCP */
  320. if ( ( rc = dhcp ( netdev ) ) != 0 )
  321. goto err_dhcp;
  322. route();
  323. /* Try PXE menu boot, if applicable */
  324. if ( have_pxe_menu() ) {
  325. printf ( "Booting from PXE menu\n" );
  326. rc = pxe_menu_boot ( netdev );
  327. goto err_pxe_menu_boot;
  328. }
  329. /* Fetch next server, filename and root path */
  330. filename = fetch_next_server_and_filename ( NULL );
  331. if ( ! filename )
  332. goto err_filename;
  333. root_path = fetch_root_path ( NULL );
  334. if ( ! root_path )
  335. goto err_root_path;
  336. /* Boot using next server, filename and root path */
  337. if ( ( rc = uriboot ( filename, root_path ) ) != 0 )
  338. goto err_uriboot;
  339. err_uriboot:
  340. uri_put ( root_path );
  341. err_root_path:
  342. uri_put ( filename );
  343. err_filename:
  344. err_pxe_menu_boot:
  345. err_dhcp:
  346. err_ifopen:
  347. return rc;
  348. }
  349. /**
  350. * Boot the system
  351. */
  352. int autoboot ( void ) {
  353. struct net_device *boot_netdev;
  354. struct net_device *netdev;
  355. int rc = -ENODEV;
  356. /* If we have an identifable boot device, try that first */
  357. if ( ( boot_netdev = find_boot_netdev() ) )
  358. rc = netboot ( boot_netdev );
  359. /* If that fails, try booting from any of the other devices */
  360. for_each_netdev ( netdev ) {
  361. if ( netdev == boot_netdev )
  362. continue;
  363. rc = netboot ( netdev );
  364. }
  365. printf ( "No more network devices\n" );
  366. return rc;
  367. }