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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/netdevice.h>
26 #include <ipxe/dhcp.h>
27 #include <ipxe/settings.h>
28 #include <ipxe/image.h>
29 #include <ipxe/sanboot.h>
31 #include <ipxe/open.h>
32 #include <ipxe/init.h>
33 #include <ipxe/keys.h>
34 #include <ipxe/version.h>
35 #include <ipxe/shell.h>
36 #include <ipxe/features.h>
37 #include <ipxe/image.h>
38 #include <ipxe/timer.h>
39 #include <usr/ifmgmt.h>
40 #include <usr/route.h>
41 #include <usr/imgmgmt.h>
42 #include <usr/prompt.h>
43 #include <usr/autoboot.h>
44 #include <config/general.h>
52 /** Link-layer address of preferred autoboot device, if known */
53 static uint8_t autoboot_ll_addr[MAX_LL_ADDR_LEN];
55 /** Device location of preferred autoboot device, if known */
56 static struct device_description autoboot_desc;
58 /** Autoboot device tester */
59 static int ( * is_autoboot_device ) ( struct net_device *netdev );
61 /* Disambiguate the various error causes */
62 #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
63 #define EINFO_ENOENT_BOOT \
64 __einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
66 #define NORMAL "\033[0m"
67 #define BOLD "\033[1m"
68 #define CYAN "\033[36m"
70 /** The "scriptlet" setting */
71 const struct setting scriptlet_setting __setting ( SETTING_MISC, scriptlet ) = {
73 .description = "Boot scriptlet",
74 .tag = DHCP_EB_SCRIPTLET,
75 .type = &setting_type_string,
79 * Perform PXE menu boot when PXE stack is not available
81 __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
86 * Parse next-server and filename into a URI
88 * @v next_server Next-server address
89 * @v filename Filename
90 * @ret uri URI, or NULL on failure
92 static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
93 const char *filename ) {
97 uri = parse_uri ( filename );
101 /* Construct a TFTP URI for the filename, if applicable */
102 if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
104 uri = tftp_uri ( next_server, filename );
112 /** The "keep-san" setting */
113 const struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA,
116 .description = "Preserve SAN connection",
117 .tag = DHCP_EB_KEEP_SAN,
118 .type = &setting_type_int8,
121 /** The "skip-san-boot" setting */
122 const struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA,
124 .name = "skip-san-boot",
125 .description = "Do not boot from SAN device",
126 .tag = DHCP_EB_SKIP_SAN_BOOT,
127 .type = &setting_type_int8,
131 * Boot from filename and root-path URIs
133 * @v filename Filename
134 * @v root_path Root path
135 * @v drive SAN drive (if applicable)
136 * @v flags Boot action flags
137 * @ret rc Return status code
139 * The somewhat tortuous flow of control in this function exists in
140 * order to ensure that the "sanboot" command remains identical in
141 * function to a SAN boot via a DHCP-specified root path, and to
142 * provide backwards compatibility for the "keep-san" and
143 * "skip-san-boot" options.
145 int uriboot ( struct uri *filename, struct uri *root_path, int drive,
146 unsigned int flags ) {
150 /* Hook SAN device, if applicable */
152 if ( ( rc = san_hook ( root_path, drive ) ) != 0 ) {
153 printf ( "Could not open SAN device: %s\n",
157 printf ( "Registered SAN device %#02x\n", drive );
160 /* Describe SAN device, if applicable */
161 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
162 if ( ( rc = san_describe ( drive ) ) != 0 ) {
163 printf ( "Could not describe SAN device %#02x: %s\n",
164 drive, strerror ( rc ) );
165 goto err_san_describe;
169 /* Allow a root-path-only boot with skip-san enabled to succeed */
172 /* Attempt filename boot if applicable */
174 if ( ( rc = imgdownload ( filename, 0, &image ) ) != 0 )
176 image->flags |= IMAGE_AUTO_UNREGISTER;
177 if ( ( rc = image_exec ( image ) ) != 0 ) {
178 printf ( "Could not boot image: %s\n",
180 /* Fall through to (possibly) attempt a SAN boot
181 * as a fallback. If no SAN boot is attempted,
182 * our status will become the return status.
185 /* Always print an extra newline, because we
186 * don't know where the NBP may have left the
193 /* Attempt SAN boot if applicable */
194 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
195 if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
196 printf ( "Booting from SAN device %#02x\n", drive );
197 rc = san_boot ( drive );
198 printf ( "Boot from SAN device %#02x failed: %s\n",
199 drive, strerror ( rc ) );
201 printf ( "Skipping boot from SAN device %#02x\n",
203 /* Avoid overwriting a possible failure status
204 * from a filename boot.
211 /* Unhook SAN device, if applicable */
212 if ( ( drive >= 0 ) && ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
213 if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
214 san_unhook ( drive );
215 printf ( "Unregistered SAN device %#02x\n", drive );
217 printf ( "Preserving SAN device %#02x\n", drive );
225 * Close all open net devices
227 * Called before a fresh boot attempt in order to free up memory. We
228 * don't just close the device immediately after the boot fails,
229 * because there may still be TCP connections in the process of
232 static void close_all_netdevs ( void ) {
233 struct net_device *netdev;
235 for_each_netdev ( netdev ) {
241 * Fetch next-server and filename settings into a URI
243 * @v settings Settings block
244 * @ret uri URI, or NULL on failure
246 struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
247 struct in_addr next_server = { 0 };
248 char *raw_filename = NULL;
249 struct uri *uri = NULL;
252 /* If we have a filename, fetch it along with the next-server
253 * setting from the same settings block.
255 if ( fetch_setting ( settings, &filename_setting, &settings,
256 NULL, NULL, 0 ) >= 0 ) {
257 fetch_string_setting_copy ( settings, &filename_setting,
259 fetch_ipv4_setting ( settings, &next_server_setting,
263 /* Expand filename setting */
264 filename = expand_settings ( raw_filename ? raw_filename : "" );
268 /* Parse next server and filename */
269 if ( next_server.s_addr )
270 printf ( "Next server: %s\n", inet_ntoa ( next_server ) );
272 printf ( "Filename: %s\n", filename );
273 uri = parse_next_server_and_filename ( next_server, filename );
280 free ( raw_filename );
285 * Fetch root-path setting into a URI
287 * @v settings Settings block
288 * @ret uri URI, or NULL on failure
290 static struct uri * fetch_root_path ( struct settings *settings ) {
291 struct uri *uri = NULL;
295 /* Fetch root-path setting */
296 fetch_string_setting_copy ( settings, &root_path_setting,
299 /* Expand filename setting */
300 root_path = expand_settings ( raw_root_path ? raw_root_path : "" );
304 /* Parse root path */
306 printf ( "Root path: %s\n", root_path );
307 uri = parse_uri ( root_path );
314 free ( raw_root_path );
319 * Check whether or not we have a usable PXE menu
321 * @ret have_menu A usable PXE menu is present
323 static int have_pxe_menu ( void ) {
324 struct setting vendor_class_id_setting
325 = { .tag = DHCP_VENDOR_CLASS_ID };
326 struct setting pxe_discovery_control_setting
327 = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
328 struct setting pxe_boot_menu_setting
329 = { .tag = DHCP_PXE_BOOT_MENU };
330 char buf[ 10 /* "PXEClient" + NUL */ ];
331 unsigned int pxe_discovery_control;
333 fetch_string_setting ( NULL, &vendor_class_id_setting,
334 buf, sizeof ( buf ) );
335 pxe_discovery_control =
336 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
338 return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
339 setting_exists ( NULL, &pxe_boot_menu_setting ) &&
340 ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
341 setting_exists ( NULL, &filename_setting ) ) ) );
345 * Boot from a network device
347 * @v netdev Network device
348 * @ret rc Return status code
350 int netboot ( struct net_device *netdev ) {
351 struct uri *filename;
352 struct uri *root_path;
355 /* Close all other network devices */
358 /* Open device and display device status */
359 if ( ( rc = ifopen ( netdev ) ) != 0 )
363 /* Configure device */
364 if ( ( rc = ifconf ( netdev, NULL ) ) != 0 )
368 /* Try PXE menu boot, if applicable */
369 if ( have_pxe_menu() ) {
370 printf ( "Booting from PXE menu\n" );
371 rc = pxe_menu_boot ( netdev );
372 goto err_pxe_menu_boot;
375 /* Fetch next server and filename */
376 filename = fetch_next_server_and_filename ( NULL );
379 if ( ! uri_has_path ( filename ) ) {
380 /* Ignore empty filename */
381 uri_put ( filename );
385 /* Fetch root path */
386 root_path = fetch_root_path ( NULL );
389 if ( ! uri_is_absolute ( root_path ) ) {
390 /* Ignore empty root path */
391 uri_put ( root_path );
395 /* If we have both a filename and a root path, ignore an
396 * unsupported URI scheme in the root path, since it may
397 * represent an NFS root.
399 if ( filename && root_path &&
400 ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) {
401 printf ( "Ignoring unsupported root path\n" );
402 uri_put ( root_path );
406 /* Check that we have something to boot */
407 if ( ! ( filename || root_path ) ) {
409 printf ( "Nothing to boot: %s\n", strerror ( rc ) );
413 /* Boot using next server, filename and root path */
414 if ( ( rc = uriboot ( filename, root_path, san_default_drive(),
415 ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
420 uri_put ( root_path );
422 uri_put ( filename );
431 * Test if network device matches the autoboot device bus type and location
433 * @v netdev Network device
434 * @ret is_autoboot Network device matches the autoboot device
436 static int is_autoboot_busloc ( struct net_device *netdev ) {
438 return ( ( netdev->dev->desc.bus_type == autoboot_desc.bus_type ) &&
439 ( netdev->dev->desc.location == autoboot_desc.location ) );
443 * Identify autoboot device by bus type and location
445 * @v bus_type Bus type
446 * @v location Location
448 void set_autoboot_busloc ( unsigned int bus_type, unsigned int location ) {
450 /* Record autoboot device description */
451 autoboot_desc.bus_type = bus_type;
452 autoboot_desc.location = location;
454 /* Mark autoboot device as present */
455 is_autoboot_device = is_autoboot_busloc;
459 * Test if network device matches the autoboot device link-layer address
461 * @v netdev Network device
462 * @ret is_autoboot Network device matches the autoboot device
464 static int is_autoboot_ll_addr ( struct net_device *netdev ) {
466 return ( memcmp ( netdev->ll_addr, autoboot_ll_addr,
467 netdev->ll_protocol->ll_addr_len ) == 0 );
471 * Identify autoboot device by link-layer address
473 * @v ll_addr Link-layer address
474 * @v len Length of link-layer address
476 void set_autoboot_ll_addr ( const void *ll_addr, size_t len ) {
478 /* Record autoboot link-layer address (truncated if necessary) */
479 if ( len > sizeof ( autoboot_ll_addr ) )
480 len = sizeof ( autoboot_ll_addr );
481 memcpy ( autoboot_ll_addr, ll_addr, len );
483 /* Mark autoboot device as present */
484 is_autoboot_device = is_autoboot_ll_addr;
490 static int autoboot ( void ) {
491 struct net_device *netdev;
494 /* Try booting from each network device. If we have a
495 * specified autoboot device location, then use only devices
496 * matching that location.
498 for_each_netdev ( netdev ) {
500 /* Skip any non-matching devices, if applicable */
501 if ( is_autoboot_device && ( ! is_autoboot_device ( netdev ) ) )
504 /* Attempt booting from this device */
505 rc = netboot ( netdev );
508 printf ( "No more network devices\n" );
513 * Prompt for shell entry
515 * @ret enter_shell User wants to enter shell
517 static int shell_banner ( void ) {
519 /* Skip prompt if timeout is zero */
520 if ( BANNER_TIMEOUT <= 0 )
525 return ( prompt ( "Press Ctrl-B for the iPXE command line...",
526 ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
531 * Main iPXE flow of execution
533 * @v netdev Network device, or NULL
535 void ipxe ( struct net_device *netdev ) {
536 struct feature *feature;
541 * Print welcome banner
544 * If you wish to brand this build of iPXE, please do so by
545 * defining the string PRODUCT_NAME in config/general.h.
547 * While nothing in the GPL prevents you from removing all
548 * references to iPXE or http://ipxe.org, we prefer you not to
552 printf ( NORMAL "\n\n%s\n" BOLD "iPXE %s"
553 NORMAL " -- Open Source Network Boot Firmware -- "
554 CYAN "http://ipxe.org" NORMAL "\n"
555 "Features:", product_name, product_version );
556 for_each_table_entry ( feature, FEATURES )
557 printf ( " %s", feature->name );
561 if ( ( image = first_image() ) != NULL ) {
562 /* We have an embedded image; execute it */
563 image_exec ( image );
564 } else if ( shell_banner() ) {
565 /* User wants shell; just give them a shell */
568 fetch_string_setting_copy ( NULL, &scriptlet_setting,
571 /* User has defined a scriptlet; execute it */
572 system ( scriptlet );
575 /* Try booting. If booting fails, offer the
576 * user another chance to enter the shell.
583 if ( shell_banner() )