2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/netdevice.h>
30 #include <ipxe/dhcp.h>
31 #include <ipxe/settings.h>
32 #include <ipxe/image.h>
33 #include <ipxe/sanboot.h>
35 #include <ipxe/open.h>
36 #include <ipxe/init.h>
37 #include <ipxe/keys.h>
38 #include <ipxe/version.h>
39 #include <ipxe/shell.h>
40 #include <ipxe/features.h>
41 #include <ipxe/image.h>
42 #include <ipxe/timer.h>
43 #include <usr/ifmgmt.h>
44 #include <usr/route.h>
45 #include <usr/imgmgmt.h>
46 #include <usr/prompt.h>
47 #include <usr/autoboot.h>
48 #include <config/general.h>
49 #include <config/branding.h>
57 /** Link-layer address of preferred autoboot device, if known */
58 static uint8_t autoboot_ll_addr[MAX_LL_ADDR_LEN];
60 /** Device location of preferred autoboot device, if known */
61 static struct device_description autoboot_desc;
63 /** Autoboot device tester */
64 static int ( * is_autoboot_device ) ( struct net_device *netdev );
66 /* Disambiguate the various error causes */
67 #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
68 #define EINFO_ENOENT_BOOT \
69 __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
71 #define NORMAL "\033[0m"
72 #define BOLD "\033[1m"
73 #define CYAN "\033[36m"
75 /** The "scriptlet" setting */
76 const struct setting scriptlet_setting __setting ( SETTING_MISC, scriptlet ) = {
78 .description = "Boot scriptlet",
79 .tag = DHCP_EB_SCRIPTLET,
80 .type = &setting_type_string,
84 * Perform PXE menu boot when PXE stack is not available
86 __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
91 * Parse next-server and filename into a URI
93 * @v next_server Next-server address
94 * @v filename Filename
95 * @ret uri URI, or NULL on failure
97 static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
98 const char *filename ) {
102 uri = parse_uri ( filename );
106 /* Construct a TFTP URI for the filename, if applicable */
107 if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
109 uri = tftp_uri ( next_server, 0, filename );
117 /** The "keep-san" setting */
118 const struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA,
121 .description = "Preserve SAN connection",
122 .tag = DHCP_EB_KEEP_SAN,
123 .type = &setting_type_int8,
126 /** The "skip-san-boot" setting */
127 const struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA,
129 .name = "skip-san-boot",
130 .description = "Do not boot from SAN device",
131 .tag = DHCP_EB_SKIP_SAN_BOOT,
132 .type = &setting_type_int8,
136 * Boot from filename and root-path URIs
138 * @v filename Filename
139 * @v root_path Root path
140 * @v drive SAN drive (if applicable)
141 * @v flags Boot action flags
142 * @ret rc Return status code
144 * The somewhat tortuous flow of control in this function exists in
145 * order to ensure that the "sanboot" command remains identical in
146 * function to a SAN boot via a DHCP-specified root path, and to
147 * provide backwards compatibility for the "keep-san" and
148 * "skip-san-boot" options.
150 int uriboot ( struct uri *filename, struct uri *root_path, int drive,
151 unsigned int flags ) {
155 /* Hook SAN device, if applicable */
157 if ( ( rc = san_hook ( root_path, drive ) ) != 0 ) {
158 printf ( "Could not open SAN device: %s\n",
162 printf ( "Registered SAN device %#02x\n", drive );
165 /* Describe SAN device, if applicable */
166 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
167 if ( ( rc = san_describe ( drive ) ) != 0 ) {
168 printf ( "Could not describe SAN device %#02x: %s\n",
169 drive, strerror ( rc ) );
170 goto err_san_describe;
174 /* Allow a root-path-only boot with skip-san enabled to succeed */
177 /* Attempt filename boot if applicable */
179 if ( ( rc = imgdownload ( filename, 0, &image ) ) != 0 )
182 image->flags |= IMAGE_AUTO_UNREGISTER;
183 if ( ( rc = image_exec ( image ) ) != 0 ) {
184 printf ( "Could not boot image: %s\n",
186 /* Fall through to (possibly) attempt a SAN boot
187 * as a fallback. If no SAN boot is attempted,
188 * our status will become the return status.
191 /* Always print an extra newline, because we
192 * don't know where the NBP may have left the
199 /* Attempt SAN boot if applicable */
200 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
201 if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
202 printf ( "Booting from SAN device %#02x\n", drive );
203 rc = san_boot ( drive );
204 printf ( "Boot from SAN device %#02x failed: %s\n",
205 drive, strerror ( rc ) );
207 printf ( "Skipping boot from SAN device %#02x\n",
209 /* Avoid overwriting a possible failure status
210 * from a filename boot.
217 /* Unhook SAN device, if applicable */
218 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
219 if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
220 san_unhook ( drive );
221 printf ( "Unregistered SAN device %#02x\n", drive );
223 printf ( "Preserving SAN device %#02x\n", drive );
231 * Close all open net devices
233 * Called before a fresh boot attempt in order to free up memory. We
234 * don't just close the device immediately after the boot fails,
235 * because there may still be TCP connections in the process of
238 static void close_all_netdevs ( void ) {
239 struct net_device *netdev;
241 for_each_netdev ( netdev ) {
247 * Fetch next-server and filename settings into a URI
249 * @v settings Settings block
250 * @ret uri URI, or NULL on failure
252 struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
253 struct in_addr next_server = { 0 };
254 char *raw_filename = NULL;
255 struct uri *uri = NULL;
258 /* If we have a filename, fetch it along with the next-server
259 * setting from the same settings block.
261 if ( fetch_setting ( settings, &filename_setting, &settings,
262 NULL, NULL, 0 ) >= 0 ) {
263 fetch_string_setting_copy ( settings, &filename_setting,
265 fetch_ipv4_setting ( settings, &next_server_setting,
269 /* Expand filename setting */
270 filename = expand_settings ( raw_filename ? raw_filename : "" );
274 /* Parse next server and filename */
275 if ( next_server.s_addr )
276 printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
278 printf ( "Filename: %s\n", filename );
279 uri = parse_next_server_and_filename ( next_server, filename );
286 free ( raw_filename );
291 * Fetch root-path setting into a URI
293 * @v settings Settings block
294 * @ret uri URI, or NULL on failure
296 static struct uri * fetch_root_path ( struct settings *settings ) {
297 struct uri *uri = NULL;
301 /* Fetch root-path setting */
302 fetch_string_setting_copy ( settings, &root_path_setting,
305 /* Expand filename setting */
306 root_path = expand_settings ( raw_root_path ? raw_root_path : "" );
310 /* Parse root path */
312 printf ( "Root path: %s\n", root_path );
313 uri = parse_uri ( root_path );
320 free ( raw_root_path );
325 * Check whether or not we have a usable PXE menu
327 * @ret have_menu A usable PXE menu is present
329 static int have_pxe_menu ( void ) {
330 struct setting vendor_class_id_setting
331 = { .tag = DHCP_VENDOR_CLASS_ID };
332 struct setting pxe_discovery_control_setting
333 = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
334 struct setting pxe_boot_menu_setting
335 = { .tag = DHCP_PXE_BOOT_MENU };
336 char buf[ 10 /* "PXEClient" + NUL */ ];
337 unsigned int pxe_discovery_control;
339 fetch_string_setting ( NULL, &vendor_class_id_setting,
340 buf, sizeof ( buf ) );
341 pxe_discovery_control =
342 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
344 return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
345 setting_exists ( NULL, &pxe_boot_menu_setting ) &&
346 ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
347 setting_exists ( NULL, &filename_setting ) ) ) );
351 * Boot from a network device
353 * @v netdev Network device
354 * @ret rc Return status code
356 int netboot ( struct net_device *netdev ) {
357 struct uri *filename;
358 struct uri *root_path;
361 /* Close all other network devices */
364 /* Open device and display device status */
365 if ( ( rc = ifopen ( netdev ) ) != 0 )
369 /* Configure device */
370 if ( ( rc = ifconf ( netdev, NULL ) ) != 0 )
374 /* Try PXE menu boot, if applicable */
375 if ( have_pxe_menu() ) {
376 printf ( "Booting from PXE menu\n" );
377 rc = pxe_menu_boot ( netdev );
378 goto err_pxe_menu_boot;
381 /* Fetch next server and filename */
382 filename = fetch_next_server_and_filename ( NULL );
385 if ( ! uri_has_path ( filename ) ) {
386 /* Ignore empty filename */
387 uri_put ( filename );
391 /* Fetch root path */
392 root_path = fetch_root_path ( NULL );
395 if ( ! uri_is_absolute ( root_path ) ) {
396 /* Ignore empty root path */
397 uri_put ( root_path );
401 /* If we have both a filename and a root path, ignore an
402 * unsupported URI scheme in the root path, since it may
403 * represent an NFS root.
405 if ( filename && root_path &&
406 ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
407 printf ( "Ignoring unsupported root path\n" );
408 uri_put ( root_path );
412 /* Check that we have something to boot */
413 if ( ! ( filename || root_path ) ) {
415 printf ( "Nothing to boot: %s\n", strerror ( rc ) );
419 /* Boot using next server, filename and root path */
420 if ( ( rc = uriboot ( filename, root_path, san_default_drive(),
421 ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
426 uri_put ( root_path );
428 uri_put ( filename );
437 * Test if network device matches the autoboot device bus type and location
439 * @v netdev Network device
440 * @ret is_autoboot Network device matches the autoboot device
442 static int is_autoboot_busloc ( struct net_device *netdev ) {
445 for ( dev = netdev->dev ; dev ; dev = dev->parent ) {
446 if ( ( dev->desc.bus_type == autoboot_desc.bus_type ) &&
447 ( dev->desc.location == autoboot_desc.location ) )
454 * Identify autoboot device by bus type and location
456 * @v bus_type Bus type
457 * @v location Location
459 void set_autoboot_busloc ( unsigned int bus_type, unsigned int location ) {
461 /* Record autoboot device description */
462 autoboot_desc.bus_type = bus_type;
463 autoboot_desc.location = location;
465 /* Mark autoboot device as present */
466 is_autoboot_device = is_autoboot_busloc;
470 * Test if network device matches the autoboot device link-layer address
472 * @v netdev Network device
473 * @ret is_autoboot Network device matches the autoboot device
475 static int is_autoboot_ll_addr ( struct net_device *netdev ) {
477 return ( memcmp ( netdev->ll_addr, autoboot_ll_addr,
478 netdev->ll_protocol->ll_addr_len ) == 0 );
482 * Identify autoboot device by link-layer address
484 * @v ll_addr Link-layer address
485 * @v len Length of link-layer address
487 void set_autoboot_ll_addr ( const void *ll_addr, size_t len ) {
489 /* Record autoboot link-layer address (truncated if necessary) */
490 if ( len > sizeof ( autoboot_ll_addr ) )
491 len = sizeof ( autoboot_ll_addr );
492 memcpy ( autoboot_ll_addr, ll_addr, len );
494 /* Mark autoboot device as present */
495 is_autoboot_device = is_autoboot_ll_addr;
501 static int autoboot ( void ) {
502 struct net_device *netdev;
505 /* Try booting from each network device. If we have a
506 * specified autoboot device location, then use only devices
507 * matching that location.
509 for_each_netdev ( netdev ) {
511 /* Skip any non-matching devices, if applicable */
512 if ( is_autoboot_device && ( ! is_autoboot_device ( netdev ) ) )
515 /* Attempt booting from this device */
516 rc = netboot ( netdev );
519 printf ( "No more network devices\n" );
524 * Prompt for shell entry
526 * @ret enter_shell User wants to enter shell
528 static int shell_banner ( void ) {
530 /* Skip prompt if timeout is zero */
531 if ( BANNER_TIMEOUT <= 0 )
536 return ( prompt ( "Press Ctrl-B for the " PRODUCT_SHORT_NAME
538 ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
543 * Main iPXE flow of execution
545 * @v netdev Network device, or NULL
546 * @ret rc Return status code
548 int ipxe ( struct net_device *netdev ) {
549 struct feature *feature;
555 * Print welcome banner
558 * If you wish to brand this build of iPXE, please do so by
559 * defining the string PRODUCT_NAME in config/branding.h.
561 * While nothing in the GPL prevents you from removing all
562 * references to iPXE or http://ipxe.org, we prefer you not to
566 printf ( NORMAL "\n\n" PRODUCT_NAME "\n" BOLD PRODUCT_SHORT_NAME " %s"
567 NORMAL " -- " PRODUCT_TAG_LINE " -- "
568 CYAN PRODUCT_URI NORMAL "\nFeatures:", product_version );
569 for_each_table_entry ( feature, FEATURES )
570 printf ( " %s", feature->name );
574 if ( ( image = first_image() ) != NULL ) {
575 /* We have an embedded image; execute it */
576 return image_exec ( image );
577 } else if ( shell_banner() ) {
578 /* User wants shell; just give them a shell */
581 fetch_string_setting_copy ( NULL, &scriptlet_setting,
584 /* User has defined a scriptlet; execute it */
585 rc = system ( scriptlet );
589 /* Try booting. If booting fails, offer the
590 * user another chance to enter the shell.
593 rc = netboot ( netdev );
597 if ( shell_banner() )