iPXE
autoboot.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <ipxe/netdevice.h>
00030 #include <ipxe/dhcp.h>
00031 #include <ipxe/settings.h>
00032 #include <ipxe/image.h>
00033 #include <ipxe/sanboot.h>
00034 #include <ipxe/uri.h>
00035 #include <ipxe/open.h>
00036 #include <ipxe/init.h>
00037 #include <ipxe/keys.h>
00038 #include <ipxe/version.h>
00039 #include <ipxe/shell.h>
00040 #include <ipxe/features.h>
00041 #include <ipxe/image.h>
00042 #include <ipxe/timer.h>
00043 #include <usr/ifmgmt.h>
00044 #include <usr/route.h>
00045 #include <usr/imgmgmt.h>
00046 #include <usr/prompt.h>
00047 #include <usr/autoboot.h>
00048 #include <config/general.h>
00049 #include <config/branding.h>
00050 
00051 /** @file
00052  *
00053  * Automatic booting
00054  *
00055  */
00056 
00057 /** Link-layer address of preferred autoboot device, if known */
00058 static uint8_t autoboot_ll_addr[MAX_LL_ADDR_LEN];
00059 
00060 /** Device location of preferred autoboot device, if known */
00061 static struct device_description autoboot_desc;
00062 
00063 /** Autoboot device tester */
00064 static int ( * is_autoboot_device ) ( struct net_device *netdev );
00065 
00066 /* Disambiguate the various error causes */
00067 #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
00068 #define EINFO_ENOENT_BOOT \
00069         __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
00070 
00071 #define NORMAL  "\033[0m"
00072 #define BOLD    "\033[1m"
00073 #define CYAN    "\033[36m"
00074 
00075 /** The "scriptlet" setting */
00076 const struct setting scriptlet_setting __setting ( SETTING_MISC, scriptlet ) = {
00077         .name = "scriptlet",
00078         .description = "Boot scriptlet",
00079         .tag = DHCP_EB_SCRIPTLET,
00080         .type = &setting_type_string,
00081 };
00082 
00083 /**
00084  * Perform PXE menu boot when PXE stack is not available
00085  */
00086 __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
00087         return -ENOTSUP;
00088 }
00089 
00090 /** The "keep-san" setting */
00091 const struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA,
00092                                                   keep-san ) = {
00093         .name = "keep-san",
00094         .description = "Preserve SAN connection",
00095         .tag = DHCP_EB_KEEP_SAN,
00096         .type = &setting_type_int8,
00097 };
00098 
00099 /** The "skip-san-boot" setting */
00100 const struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA,
00101                                                        skip-san-boot ) = {
00102         .name = "skip-san-boot",
00103         .description = "Do not boot from SAN device",
00104         .tag = DHCP_EB_SKIP_SAN_BOOT,
00105         .type = &setting_type_int8,
00106 };
00107 
00108 /**
00109  * Boot from filename and root-path URIs
00110  *
00111  * @v filename          Filename
00112  * @v root_paths        Root path(s)
00113  * @v root_path_count   Number of root paths
00114  * @v drive             SAN drive (if applicable)
00115  * @v san_filename      SAN filename (or NULL to use default)
00116  * @v flags             Boot action flags
00117  * @ret rc              Return status code
00118  *
00119  * The somewhat tortuous flow of control in this function exists in
00120  * order to ensure that the "sanboot" command remains identical in
00121  * function to a SAN boot via a DHCP-specified root path, and to
00122  * provide backwards compatibility for the "keep-san" and
00123  * "skip-san-boot" options.
00124  */
00125 int uriboot ( struct uri *filename, struct uri **root_paths,
00126               unsigned int root_path_count, int drive,
00127               const char *san_filename, unsigned int flags ) {
00128         struct image *image;
00129         int rc;
00130 
00131         /* Hook SAN device, if applicable */
00132         if ( root_path_count ) {
00133                 drive = san_hook ( drive, root_paths, root_path_count,
00134                                    ( ( flags & URIBOOT_NO_SAN_DESCRIBE ) ?
00135                                      SAN_NO_DESCRIBE : 0 ) );
00136                 if ( drive < 0 ) {
00137                         rc = drive;
00138                         printf ( "Could not open SAN device: %s\n",
00139                                  strerror ( rc ) );
00140                         goto err_san_hook;
00141                 }
00142                 printf ( "Registered SAN device %#02x\n", drive );
00143         }
00144 
00145         /* Describe SAN device, if applicable */
00146         if ( ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
00147                 if ( ( rc = san_describe() ) != 0 ) {
00148                         printf ( "Could not describe SAN devices: %s\n",
00149                                  strerror ( rc ) );
00150                         goto err_san_describe;
00151                 }
00152         }
00153 
00154         /* Allow a root-path-only boot with skip-san enabled to succeed */
00155         rc = 0;
00156 
00157         /* Attempt filename boot if applicable */
00158         if ( filename ) {
00159                 if ( ( rc = imgdownload ( filename, 0, &image ) ) != 0 )
00160                         goto err_download;
00161                 imgstat ( image );
00162                 image->flags |= IMAGE_AUTO_UNREGISTER;
00163                 if ( ( rc = image_exec ( image ) ) != 0 ) {
00164                         printf ( "Could not boot image: %s\n",
00165                                  strerror ( rc ) );
00166                         /* Fall through to (possibly) attempt a SAN boot
00167                          * as a fallback.  If no SAN boot is attempted,
00168                          * our status will become the return status.
00169                          */
00170                 } else {
00171                         /* Always print an extra newline, because we
00172                          * don't know where the NBP may have left the
00173                          * cursor.
00174                          */
00175                         printf ( "\n" );
00176                 }
00177         }
00178 
00179         /* Attempt SAN boot if applicable */
00180         if ( ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
00181                 if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
00182                         printf ( "Booting%s%s from SAN device %#02x\n",
00183                                  ( san_filename ? " " : "" ),
00184                                  ( san_filename ? san_filename : "" ), drive );
00185                         rc = san_boot ( drive, san_filename );
00186                         printf ( "Boot from SAN device %#02x failed: %s\n",
00187                                  drive, strerror ( rc ) );
00188                 } else {
00189                         printf ( "Skipping boot from SAN device %#02x\n",
00190                                  drive );
00191                         /* Avoid overwriting a possible failure status
00192                          * from a filename boot.
00193                          */
00194                 }
00195         }
00196 
00197  err_download:
00198  err_san_describe:
00199         /* Unhook SAN device, if applicable */
00200         if ( ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
00201                 if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
00202                         san_unhook ( drive );
00203                         printf ( "Unregistered SAN device %#02x\n", drive );
00204                 } else {
00205                         printf ( "Preserving SAN device %#02x\n", drive );
00206                 }
00207         }
00208  err_san_hook:
00209         return rc;
00210 }
00211 
00212 /**
00213  * Close all open net devices
00214  *
00215  * Called before a fresh boot attempt in order to free up memory.  We
00216  * don't just close the device immediately after the boot fails,
00217  * because there may still be TCP connections in the process of
00218  * closing.
00219  */
00220 static void close_all_netdevs ( void ) {
00221         struct net_device *netdev;
00222 
00223         for_each_netdev ( netdev ) {
00224                 ifclose ( netdev );
00225         }
00226 }
00227 
00228 /**
00229  * Fetch next-server and filename settings into a URI
00230  *
00231  * @v settings          Settings block
00232  * @ret uri             URI, or NULL on failure
00233  */
00234 struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
00235         union {
00236                 struct sockaddr sa;
00237                 struct sockaddr_in sin;
00238         } next_server;
00239         char *raw_filename = NULL;
00240         struct uri *uri = NULL;
00241         char *filename;
00242 
00243         /* Initialise server address */
00244         memset ( &next_server, 0, sizeof ( next_server ) );
00245 
00246         /* If we have a filename, fetch it along with the next-server
00247          * setting from the same settings block.
00248          */
00249         if ( fetch_setting ( settings, &filename_setting, &settings,
00250                              NULL, NULL, 0 ) >= 0 ) {
00251                 fetch_string_setting_copy ( settings, &filename_setting,
00252                                             &raw_filename );
00253                 fetch_ipv4_setting ( settings, &next_server_setting,
00254                                      &next_server.sin.sin_addr );
00255         }
00256         if ( ! raw_filename )
00257                 goto err_fetch;
00258 
00259         /* Populate server address */
00260         if ( next_server.sin.sin_addr.s_addr ) {
00261                 next_server.sin.sin_family = AF_INET;
00262                 printf ( "Next server: %s\n",
00263                          inet_ntoa ( next_server.sin.sin_addr ) );
00264         }
00265 
00266         /* Expand filename setting */
00267         filename = expand_settings ( raw_filename );
00268         if ( ! filename )
00269                 goto err_expand;
00270         if ( filename[0] )
00271                 printf ( "Filename: %s\n", filename );
00272 
00273         /* Construct URI */
00274         uri = pxe_uri ( &next_server.sa, filename );
00275         if ( ! uri )
00276                 goto err_parse;
00277 
00278  err_parse:
00279         free ( filename );
00280  err_expand:
00281         free ( raw_filename );
00282  err_fetch:
00283         return uri;
00284 }
00285 
00286 /**
00287  * Fetch root-path setting into a URI
00288  *
00289  * @v settings          Settings block
00290  * @ret uri             URI, or NULL on failure
00291  */
00292 static struct uri * fetch_root_path ( struct settings *settings ) {
00293         struct uri *uri = NULL;
00294         char *raw_root_path;
00295         char *root_path;
00296 
00297         /* Fetch root-path setting */
00298         fetch_string_setting_copy ( settings, &root_path_setting,
00299                                     &raw_root_path );
00300         if ( ! raw_root_path )
00301                 goto err_fetch;
00302 
00303         /* Expand filename setting */
00304         root_path = expand_settings ( raw_root_path );
00305         if ( ! root_path )
00306                 goto err_expand;
00307         if ( root_path[0] )
00308                 printf ( "Root path: %s\n", root_path );
00309 
00310         /* Parse root path */
00311         uri = parse_uri ( root_path );
00312         if ( ! uri )
00313                 goto err_parse;
00314 
00315  err_parse:
00316         free ( root_path );
00317  err_expand:
00318         free ( raw_root_path );
00319  err_fetch:
00320         return uri;
00321 }
00322 
00323 /**
00324  * Fetch san-filename setting
00325  *
00326  * @v settings          Settings block
00327  * @ret san_filename    SAN filename, or NULL on failure
00328  */
00329 static char * fetch_san_filename ( struct settings *settings ) {
00330         char *raw_san_filename;
00331         char *san_filename = NULL;
00332 
00333         /* Fetch san-filename setting */
00334         fetch_string_setting_copy ( settings, &san_filename_setting,
00335                                     &raw_san_filename );
00336         if ( ! raw_san_filename )
00337                 goto err_fetch;
00338 
00339         /* Expand san-filename setting */
00340         san_filename = expand_settings ( raw_san_filename );
00341         if ( ! san_filename )
00342                 goto err_expand;
00343         if ( san_filename[0] )
00344                 printf ( "SAN filename: %s\n", san_filename );
00345 
00346  err_expand:
00347         free ( raw_san_filename );
00348  err_fetch:
00349         return san_filename;
00350 }
00351 
00352 /**
00353  * Check whether or not we have a usable PXE menu
00354  *
00355  * @ret have_menu       A usable PXE menu is present
00356  */
00357 static int have_pxe_menu ( void ) {
00358         struct setting vendor_class_id_setting
00359                 = { .tag = DHCP_VENDOR_CLASS_ID };
00360         struct setting pxe_discovery_control_setting
00361                 = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
00362         struct setting pxe_boot_menu_setting
00363                 = { .tag = DHCP_PXE_BOOT_MENU };
00364         char buf[ 10 /* "PXEClient" + NUL */ ];
00365         unsigned int pxe_discovery_control;
00366 
00367         fetch_string_setting ( NULL, &vendor_class_id_setting,
00368                                buf, sizeof ( buf ) );
00369         pxe_discovery_control =
00370                 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
00371 
00372         return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
00373                  setting_exists ( NULL, &pxe_boot_menu_setting ) &&
00374                  ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
00375                        setting_exists ( NULL, &filename_setting ) ) ) );
00376 }
00377 
00378 /**
00379  * Boot from a network device
00380  *
00381  * @v netdev            Network device
00382  * @ret rc              Return status code
00383  */
00384 int netboot ( struct net_device *netdev ) {
00385         struct uri *filename;
00386         struct uri *root_path;
00387         char *san_filename;
00388         int rc;
00389 
00390         /* Close all other network devices */
00391         close_all_netdevs();
00392 
00393         /* Open device and display device status */
00394         if ( ( rc = ifopen ( netdev ) ) != 0 )
00395                 goto err_ifopen;
00396         ifstat ( netdev );
00397 
00398         /* Configure device */
00399         if ( ( rc = ifconf ( netdev, NULL ) ) != 0 )
00400                 goto err_dhcp;
00401         route();
00402 
00403         /* Try PXE menu boot, if applicable */
00404         if ( have_pxe_menu() ) {
00405                 printf ( "Booting from PXE menu\n" );
00406                 rc = pxe_menu_boot ( netdev );
00407                 goto err_pxe_menu_boot;
00408         }
00409 
00410         /* Fetch next server and filename (if any) */
00411         filename = fetch_next_server_and_filename ( NULL );
00412 
00413         /* Fetch root path (if any) */
00414         root_path = fetch_root_path ( NULL );
00415 
00416         /* Fetch SAN filename (if any) */
00417         san_filename = fetch_san_filename ( NULL );
00418 
00419         /* If we have both a filename and a root path, ignore an
00420          * unsupported or missing URI scheme in the root path, since
00421          * it may represent an NFS root.
00422          */
00423         if ( filename && root_path &&
00424              ( ( ! uri_is_absolute ( root_path ) ) ||
00425                ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) ) {
00426                 printf ( "Ignoring unsupported root path\n" );
00427                 uri_put ( root_path );
00428                 root_path = NULL;
00429         }
00430 
00431         /* Check that we have something to boot */
00432         if ( ! ( filename || root_path ) ) {
00433                 rc = -ENOENT_BOOT;
00434                 printf ( "Nothing to boot: %s\n", strerror ( rc ) );
00435                 goto err_no_boot;
00436         }
00437 
00438         /* Boot using next server, filename and root path */
00439         if ( ( rc = uriboot ( filename, &root_path, ( root_path ? 1 : 0 ),
00440                               san_default_drive(), san_filename,
00441                               ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
00442                 goto err_uriboot;
00443 
00444  err_uriboot:
00445  err_no_boot:
00446         free ( san_filename );
00447         uri_put ( root_path );
00448         uri_put ( filename );
00449  err_pxe_menu_boot:
00450  err_dhcp:
00451  err_ifopen:
00452         return rc;
00453 }
00454 
00455 /**
00456  * Test if network device matches the autoboot device bus type and location
00457  *
00458  * @v netdev            Network device
00459  * @ret is_autoboot     Network device matches the autoboot device
00460  */
00461 static int is_autoboot_busloc ( struct net_device *netdev ) {
00462         struct device *dev;
00463 
00464         for ( dev = netdev->dev ; dev ; dev = dev->parent ) {
00465                 if ( ( dev->desc.bus_type == autoboot_desc.bus_type ) &&
00466                      ( dev->desc.location == autoboot_desc.location ) )
00467                         return 1;
00468         }
00469         return 0;
00470 }
00471 
00472 /**
00473  * Identify autoboot device by bus type and location
00474  *
00475  * @v bus_type          Bus type
00476  * @v location          Location
00477  */
00478 void set_autoboot_busloc ( unsigned int bus_type, unsigned int location ) {
00479 
00480         /* Record autoboot device description */
00481         autoboot_desc.bus_type = bus_type;
00482         autoboot_desc.location = location;
00483 
00484         /* Mark autoboot device as present */
00485         is_autoboot_device = is_autoboot_busloc;
00486 }
00487 
00488 /**
00489  * Test if network device matches the autoboot device link-layer address
00490  *
00491  * @v netdev            Network device
00492  * @ret is_autoboot     Network device matches the autoboot device
00493  */
00494 static int is_autoboot_ll_addr ( struct net_device *netdev ) {
00495 
00496         return ( memcmp ( netdev->ll_addr, autoboot_ll_addr,
00497                           netdev->ll_protocol->ll_addr_len ) == 0 );
00498 }
00499 
00500 /**
00501  * Identify autoboot device by link-layer address
00502  *
00503  * @v ll_addr           Link-layer address
00504  * @v len               Length of link-layer address
00505  */
00506 void set_autoboot_ll_addr ( const void *ll_addr, size_t len ) {
00507 
00508         /* Record autoboot link-layer address (truncated if necessary) */
00509         if ( len > sizeof ( autoboot_ll_addr ) )
00510                 len = sizeof ( autoboot_ll_addr );
00511         memcpy ( autoboot_ll_addr, ll_addr, len );
00512 
00513         /* Mark autoboot device as present */
00514         is_autoboot_device = is_autoboot_ll_addr;
00515 }
00516 
00517 /**
00518  * Boot the system
00519  */
00520 static int autoboot ( void ) {
00521         struct net_device *netdev;
00522         int rc = -ENODEV;
00523 
00524         /* Try booting from each network device.  If we have a
00525          * specified autoboot device location, then use only devices
00526          * matching that location.
00527          */
00528         for_each_netdev ( netdev ) {
00529 
00530                 /* Skip any non-matching devices, if applicable */
00531                 if ( is_autoboot_device && ( ! is_autoboot_device ( netdev ) ) )
00532                         continue;
00533 
00534                 /* Attempt booting from this device */
00535                 rc = netboot ( netdev );
00536         }
00537 
00538         printf ( "No more network devices\n" );
00539         return rc;
00540 }
00541 
00542 /**
00543  * Prompt for shell entry
00544  *
00545  * @ret enter_shell     User wants to enter shell
00546  */
00547 static int shell_banner ( void ) {
00548 
00549         /* Skip prompt if timeout is zero */
00550         if ( BANNER_TIMEOUT <= 0 )
00551                 return 0;
00552 
00553         /* Prompt user */
00554         printf ( "\n" );
00555         return ( prompt ( "Press Ctrl-B for the " PRODUCT_SHORT_NAME
00556                           " command line...",
00557                           ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
00558                           CTRL_B ) == 0 );
00559 }
00560 
00561 /**
00562  * Main iPXE flow of execution
00563  *
00564  * @v netdev            Network device, or NULL
00565  * @ret rc              Return status code
00566  */
00567 int ipxe ( struct net_device *netdev ) {
00568         struct feature *feature;
00569         struct image *image;
00570         char *scriptlet;
00571         int rc;
00572 
00573         /*
00574          * Print welcome banner
00575          *
00576          *
00577          * If you wish to brand this build of iPXE, please do so by
00578          * defining the string PRODUCT_NAME in config/branding.h.
00579          *
00580          * While nothing in the GPL prevents you from removing all
00581          * references to iPXE or http://ipxe.org, we prefer you not to
00582          * do so.
00583          *
00584          */
00585         printf ( NORMAL "\n\n" PRODUCT_NAME "\n" BOLD PRODUCT_SHORT_NAME " %s"
00586                  NORMAL " -- " PRODUCT_TAG_LINE " -- "
00587                  CYAN PRODUCT_URI NORMAL "\nFeatures:", product_version );
00588         for_each_table_entry ( feature, FEATURES )
00589                 printf ( " %s", feature->name );
00590         printf ( "\n" );
00591 
00592         /* Boot system */
00593         if ( ( image = first_image() ) != NULL ) {
00594                 /* We have an embedded image; execute it */
00595                 return image_exec ( image );
00596         } else if ( shell_banner() ) {
00597                 /* User wants shell; just give them a shell */
00598                 return shell();
00599         } else {
00600                 fetch_string_setting_copy ( NULL, &scriptlet_setting,
00601                                             &scriptlet );
00602                 if ( scriptlet ) {
00603                         /* User has defined a scriptlet; execute it */
00604                         rc = system ( scriptlet );
00605                         free ( scriptlet );
00606                         return rc;
00607                 } else {
00608                         /* Try booting.  If booting fails, offer the
00609                          * user another chance to enter the shell.
00610                          */
00611                         if ( netdev ) {
00612                                 rc = netboot ( netdev );
00613                         } else {
00614                                 rc = autoboot();
00615                         }
00616                         if ( shell_banner() )
00617                                 rc = shell();
00618                         return rc;
00619                 }
00620         }
00621 }