david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/usr/autoboot.c

295 lines
7.3 KiB
C
Raw Normal View History

/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
#include <string.h>
2007-01-19 02:13:12 +01:00
#include <stdio.h>
#include <errno.h>
#include <ipxe/netdevice.h>
#include <ipxe/dhcp.h>
#include <ipxe/settings.h>
#include <ipxe/image.h>
#include <ipxe/sanboot.h>
#include <ipxe/uri.h>
[block] Replace gPXE block-device API with an iPXE asynchronous interface The block device interface used in gPXE predates the invention of even the old gPXE data-transfer interface, let alone the current iPXE generic asynchronous interface mechanism. Bring this old code up to date, with the following benefits: o Block device commands can be cancelled by the requestor. The INT 13 layer uses this to provide a global timeout on all INT 13 calls, with the result that an unexpected passive failure mode (such as an iSCSI target ACKing the request but never sending a response) will lead to a timeout that gets reported back to the INT 13 user, rather than simply freezing the system. o INT 13,00 (reset drive) is now able to reset the underlying block device. INT 13 users, such as DOS, that use INT 13,00 as a method for error recovery now have a chance of recovering. o All block device commands are tagged, with a numerical tag that will show up in debugging output and in packet captures; this will allow easier interpretation of bug reports that include both sources of information. o The extremely ugly hacks used to generate the boot firmware tables have been eradicated and replaced with a generic acpi_describe() method (exploiting the ability of iPXE interfaces to pass through methods to an underlying interface). The ACPI tables are now built in a shared data block within .bss16, rather than each requiring dedicated space in .data16. o The architecture-independent concept of a SAN device has been exposed to the iPXE core through the sanboot API, which provides calls to hook, unhook, boot, and describe SAN devices. This allows for much more flexible usage patterns (such as hooking an empty SAN device and then running an OS installer via TFTP). Signed-off-by: Michael Brown <mcb30@ipxe.org>
2010-09-03 17:11:51 +02:00
#include <ipxe/init.h>
#include <usr/ifmgmt.h>
#include <usr/route.h>
#include <usr/dhcpmgmt.h>
#include <usr/imgmgmt.h>
#include <usr/autoboot.h>
/** @file
*
* Automatic booting
*
*/
/** Shutdown flags for exit */
int shutdown_exit_flags = 0;
/**
* Perform PXE menu boot when PXE stack is not available
*/
__weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
return -ENOTSUP;
}
/**
* Identify the boot network device
*
* @ret netdev Boot network device
*/
static struct net_device * find_boot_netdev ( void ) {
return NULL;
}
/**
* Boot using next-server and filename
*
2007-07-08 23:01:49 +02:00
* @v filename Boot filename
* @ret rc Return status code
*/
int boot_next_server_and_filename ( struct in_addr next_server,
const char *filename ) {
struct uri *uri;
struct image *image;
char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ +
( 3 * strlen(filename) ) /* completely URI-encoded */
+ 1 /* NUL */ ];
int filename_is_absolute;
int rc;
/* Construct URI */
uri = parse_uri ( filename );
if ( ! uri )
return -ENOMEM;
filename_is_absolute = uri_is_absolute ( uri );
uri_put ( uri );
if ( ! filename_is_absolute ) {
/* Construct a tftp:// URI for the filename. We can't
* just rely on the current working URI, because the
* relative URI resolution will remove the distinction
* between filenames with and without initial slashes,
* which is significant for TFTP.
*/
snprintf ( buf, sizeof ( buf ), "tftp://%s/",
inet_ntoa ( next_server ) );
uri_encode ( filename, buf + strlen ( buf ),
sizeof ( buf ) - strlen ( buf ), URI_PATH );
filename = buf;
}
image = alloc_image();
if ( ! image )
2007-07-08 23:01:49 +02:00
return -ENOMEM;
if ( ( rc = imgfetch ( image, filename,
register_and_autoload_image ) ) != 0 ) {
goto done;
}
if ( ( rc = imgexec ( image ) ) != 0 )
goto done;
2007-07-08 23:01:49 +02:00
done:
image_put ( image );
return rc;
2007-07-08 23:01:49 +02:00
}
[block] Replace gPXE block-device API with an iPXE asynchronous interface The block device interface used in gPXE predates the invention of even the old gPXE data-transfer interface, let alone the current iPXE generic asynchronous interface mechanism. Bring this old code up to date, with the following benefits: o Block device commands can be cancelled by the requestor. The INT 13 layer uses this to provide a global timeout on all INT 13 calls, with the result that an unexpected passive failure mode (such as an iSCSI target ACKing the request but never sending a response) will lead to a timeout that gets reported back to the INT 13 user, rather than simply freezing the system. o INT 13,00 (reset drive) is now able to reset the underlying block device. INT 13 users, such as DOS, that use INT 13,00 as a method for error recovery now have a chance of recovering. o All block device commands are tagged, with a numerical tag that will show up in debugging output and in packet captures; this will allow easier interpretation of bug reports that include both sources of information. o The extremely ugly hacks used to generate the boot firmware tables have been eradicated and replaced with a generic acpi_describe() method (exploiting the ability of iPXE interfaces to pass through methods to an underlying interface). The ACPI tables are now built in a shared data block within .bss16, rather than each requiring dedicated space in .data16. o The architecture-independent concept of a SAN device has been exposed to the iPXE core through the sanboot API, which provides calls to hook, unhook, boot, and describe SAN devices. This allows for much more flexible usage patterns (such as hooking an empty SAN device and then running an OS installer via TFTP). Signed-off-by: Michael Brown <mcb30@ipxe.org>
2010-09-03 17:11:51 +02:00
/** The "keep-san" setting */
struct setting keep_san_setting __setting = {
.name = "keep-san",
.description = "Preserve SAN connection",
.tag = DHCP_EB_KEEP_SAN,
.type = &setting_type_int8,
};
2007-07-08 23:01:49 +02:00
/**
* Boot using root path
*
* @v root_path Root path
* @ret rc Return status code
*/
2008-03-04 18:59:26 +01:00
int boot_root_path ( const char *root_path ) {
[block] Replace gPXE block-device API with an iPXE asynchronous interface The block device interface used in gPXE predates the invention of even the old gPXE data-transfer interface, let alone the current iPXE generic asynchronous interface mechanism. Bring this old code up to date, with the following benefits: o Block device commands can be cancelled by the requestor. The INT 13 layer uses this to provide a global timeout on all INT 13 calls, with the result that an unexpected passive failure mode (such as an iSCSI target ACKing the request but never sending a response) will lead to a timeout that gets reported back to the INT 13 user, rather than simply freezing the system. o INT 13,00 (reset drive) is now able to reset the underlying block device. INT 13 users, such as DOS, that use INT 13,00 as a method for error recovery now have a chance of recovering. o All block device commands are tagged, with a numerical tag that will show up in debugging output and in packet captures; this will allow easier interpretation of bug reports that include both sources of information. o The extremely ugly hacks used to generate the boot firmware tables have been eradicated and replaced with a generic acpi_describe() method (exploiting the ability of iPXE interfaces to pass through methods to an underlying interface). The ACPI tables are now built in a shared data block within .bss16, rather than each requiring dedicated space in .data16. o The architecture-independent concept of a SAN device has been exposed to the iPXE core through the sanboot API, which provides calls to hook, unhook, boot, and describe SAN devices. This allows for much more flexible usage patterns (such as hooking an empty SAN device and then running an OS installer via TFTP). Signed-off-by: Michael Brown <mcb30@ipxe.org>
2010-09-03 17:11:51 +02:00
struct uri *uri;
int drive;
int rc;
/* Parse URI */
uri = parse_uri ( root_path );
if ( ! uri ) {
printf ( "Could not parse \"%s\"\n", root_path );
rc = -ENOMEM;
goto err_parse_uri;
}
2007-07-08 23:01:49 +02:00
[block] Replace gPXE block-device API with an iPXE asynchronous interface The block device interface used in gPXE predates the invention of even the old gPXE data-transfer interface, let alone the current iPXE generic asynchronous interface mechanism. Bring this old code up to date, with the following benefits: o Block device commands can be cancelled by the requestor. The INT 13 layer uses this to provide a global timeout on all INT 13 calls, with the result that an unexpected passive failure mode (such as an iSCSI target ACKing the request but never sending a response) will lead to a timeout that gets reported back to the INT 13 user, rather than simply freezing the system. o INT 13,00 (reset drive) is now able to reset the underlying block device. INT 13 users, such as DOS, that use INT 13,00 as a method for error recovery now have a chance of recovering. o All block device commands are tagged, with a numerical tag that will show up in debugging output and in packet captures; this will allow easier interpretation of bug reports that include both sources of information. o The extremely ugly hacks used to generate the boot firmware tables have been eradicated and replaced with a generic acpi_describe() method (exploiting the ability of iPXE interfaces to pass through methods to an underlying interface). The ACPI tables are now built in a shared data block within .bss16, rather than each requiring dedicated space in .data16. o The architecture-independent concept of a SAN device has been exposed to the iPXE core through the sanboot API, which provides calls to hook, unhook, boot, and describe SAN devices. This allows for much more flexible usage patterns (such as hooking an empty SAN device and then running an OS installer via TFTP). Signed-off-by: Michael Brown <mcb30@ipxe.org>
2010-09-03 17:11:51 +02:00
/* Hook SAN device */
if ( ( drive = san_hook ( uri, 0 ) ) < 0 ) {
rc = drive;
printf ( "Could not open SAN device: %s\n",
strerror ( rc ) );
goto err_open;
}
printf ( "Registered as SAN device %#02x\n", drive );
/* Describe SAN device */
if ( ( rc = san_describe ( drive ) ) != 0 ) {
printf ( "Could not describe SAN device %#02x: %s\n",
drive, strerror ( rc ) );
goto err_describe;
}
printf ( "Booting from SAN device %#02x\n", drive );
rc = san_boot ( drive );
printf ( "Boot from SAN device %#02x failed: %s\n",
drive, strerror ( rc ) );
/* Leave drive registered, if instructed to do so */
if ( fetch_intz_setting ( NULL, &keep_san_setting ) != 0 ) {
printf ( "Preserving connection to SAN device %#02x\n",
drive );
shutdown_exit_flags |= SHUTDOWN_KEEP_DEVICES;
goto err_keep_san;
}
/* Unhook SAN deivce */
printf ( "Unregistering SAN device %#02x\n", drive );
san_unhook ( drive );
/* Drop URI reference */
uri_put ( uri );
return 0;
err_keep_san:
err_describe:
err_open:
uri_put ( uri );
err_parse_uri:
return rc;
2007-07-08 23:01:49 +02:00
}
/**
* Boot from a network device
*
* @v netdev Network device
* @ret rc Return status code
*/
2007-07-27 21:31:45 +02:00
static int netboot ( struct net_device *netdev ) {
struct setting vendor_class_id_setting
= { .tag = DHCP_VENDOR_CLASS_ID };
struct setting pxe_discovery_control_setting
= { .tag = DHCP_PXE_DISCOVERY_CONTROL };
struct setting pxe_boot_menu_setting
= { .tag = DHCP_PXE_BOOT_MENU };
2007-07-08 23:01:49 +02:00
char buf[256];
struct in_addr next_server;
unsigned int pxe_discovery_control;
2007-07-08 23:01:49 +02:00
int rc;
/* Open device and display device status */
if ( ( rc = ifopen ( netdev ) ) != 0 )
return rc;
ifstat ( netdev );
/* Configure device via DHCP */
if ( ( rc = dhcp ( netdev ) ) != 0 )
return rc;
route();
/* Try PXE menu boot, if applicable */
fetch_string_setting ( NULL, &vendor_class_id_setting,
buf, sizeof ( buf ) );
pxe_discovery_control =
fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
setting_exists ( NULL, &pxe_boot_menu_setting ) &&
( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
setting_exists ( NULL, &filename_setting ) ) ) ) {
printf ( "Booting from PXE menu\n" );
return pxe_menu_boot ( netdev );
}
2007-07-08 23:01:49 +02:00
/* Try to download and boot whatever we are given as a filename */
fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
2007-07-08 23:01:49 +02:00
if ( buf[0] ) {
printf ( "Booting from filename \"%s\"\n", buf );
if ( ( rc = boot_next_server_and_filename ( next_server,
buf ) ) != 0 ) {
printf ( "Could not boot from filename \"%s\": %s\n",
buf, strerror ( rc ) );
return rc;
}
return 0;
}
2007-07-08 23:01:49 +02:00
/* No filename; try the root path */
fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
2007-07-08 23:01:49 +02:00
if ( buf[0] ) {
printf ( "Booting from root path \"%s\"\n", buf );
if ( ( rc = boot_root_path ( buf ) ) != 0 ) {
printf ( "Could not boot from root path \"%s\": %s\n",
buf, strerror ( rc ) );
return rc;
}
return 0;
2007-07-08 23:01:49 +02:00
}
printf ( "No filename or root path specified\n" );
return -ENOENT;
}
/**
* Close all open net devices
*
* Called before a fresh boot attempt in order to free up memory. We
* don't just close the device immediately after the boot fails,
* because there may still be TCP connections in the process of
* closing.
*/
static void close_all_netdevs ( void ) {
struct net_device *netdev;
for_each_netdev ( netdev ) {
ifclose ( netdev );
}
}
/**
* Boot the system
*/
void autoboot ( void ) {
struct net_device *boot_netdev;
struct net_device *netdev;
/* If we have an identifable boot device, try that first */
close_all_netdevs();
if ( ( boot_netdev = find_boot_netdev() ) )
netboot ( boot_netdev );
/* If that fails, try booting from any of the other devices */
for_each_netdev ( netdev ) {
if ( netdev == boot_netdev )
continue;
close_all_netdevs();
netboot ( netdev );
}
printf ( "No more network devices\n" );
}