2006-12-20 08:04:08 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-01-04 05:20:08 +01:00
|
|
|
#include <string.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2007-01-10 21:21:42 +01:00
|
|
|
#include <errno.h>
|
2007-01-04 05:20:08 +01:00
|
|
|
#include <gpxe/netdevice.h>
|
2007-01-12 11:08:27 +01:00
|
|
|
#include <gpxe/dhcp.h>
|
2008-03-21 00:42:11 +01:00
|
|
|
#include <gpxe/settings.h>
|
2007-01-12 11:08:27 +01:00
|
|
|
#include <gpxe/image.h>
|
2008-10-13 11:05:23 +02:00
|
|
|
#include <gpxe/sanboot.h>
|
2008-07-31 17:55:46 +02:00
|
|
|
#include <gpxe/uri.h>
|
2007-01-10 02:55:07 +01:00
|
|
|
#include <usr/ifmgmt.h>
|
2007-01-10 21:38:20 +01:00
|
|
|
#include <usr/route.h>
|
2007-01-12 11:08:27 +01:00
|
|
|
#include <usr/dhcpmgmt.h>
|
|
|
|
#include <usr/imgmgmt.h>
|
2007-01-10 03:03:20 +01:00
|
|
|
#include <usr/autoboot.h>
|
2006-12-20 08:04:08 +01:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Automatic booting
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-04-22 18:40:50 +02:00
|
|
|
/** Time to wait for link-up */
|
|
|
|
#define LINK_WAIT_MS 15000
|
|
|
|
|
2008-07-17 18:45:17 +02:00
|
|
|
/** Shutdown flags for exit */
|
|
|
|
int shutdown_exit_flags = 0;
|
|
|
|
|
2007-01-10 21:21:42 +01:00
|
|
|
/**
|
|
|
|
* Identify the boot network device
|
|
|
|
*
|
|
|
|
* @ret netdev Boot network device
|
|
|
|
*/
|
|
|
|
static struct net_device * find_boot_netdev ( void ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-31 17:55:46 +02:00
|
|
|
* Boot using next-server and filename
|
2007-01-10 21:21:42 +01:00
|
|
|
*
|
2007-07-08 23:01:49 +02:00
|
|
|
* @v filename Boot filename
|
|
|
|
* @ret rc Return status code
|
2007-01-10 21:21:42 +01:00
|
|
|
*/
|
2009-01-31 08:36:05 +01:00
|
|
|
int boot_next_server_and_filename ( struct in_addr next_server,
|
|
|
|
const char *filename ) {
|
2008-07-31 17:55:46 +02:00
|
|
|
struct uri *uri;
|
2007-01-12 11:08:27 +01:00
|
|
|
struct image *image;
|
2008-07-31 17:55:46 +02:00
|
|
|
char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
|
|
|
|
int filename_is_absolute;
|
2007-01-12 11:08:27 +01:00
|
|
|
int rc;
|
2007-01-10 21:21:42 +01:00
|
|
|
|
2008-07-31 17:55:46 +02:00
|
|
|
/* Construct URI */
|
|
|
|
uri = parse_uri ( filename );
|
|
|
|
if ( ! uri ) {
|
|
|
|
printf ( "Out of memory\n" );
|
|
|
|
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/%s",
|
|
|
|
inet_ntoa ( next_server ), filename );
|
|
|
|
filename = buf;
|
|
|
|
}
|
|
|
|
|
2007-06-28 18:55:29 +02:00
|
|
|
image = alloc_image();
|
|
|
|
if ( ! image ) {
|
|
|
|
printf ( "Out of memory\n" );
|
2007-07-08 23:01:49 +02:00
|
|
|
return -ENOMEM;
|
2007-06-28 18:55:29 +02:00
|
|
|
}
|
2007-08-02 21:18:32 +02:00
|
|
|
if ( ( rc = imgfetch ( image, filename,
|
2007-08-03 13:49:21 +02:00
|
|
|
register_and_autoload_image ) ) != 0 ) {
|
|
|
|
printf ( "Could not load %s: %s\n",
|
|
|
|
filename, strerror ( rc ) );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ( ( rc = imgexec ( image ) ) != 0 ) {
|
2007-08-02 21:27:50 +02:00
|
|
|
printf ( "Could not boot %s: %s\n",
|
2007-01-12 11:08:27 +01:00
|
|
|
filename, strerror ( rc ) );
|
2007-08-03 13:49:21 +02:00
|
|
|
goto done;
|
2007-01-12 11:08:27 +01:00
|
|
|
}
|
2007-07-08 23:01:49 +02:00
|
|
|
|
2007-08-03 13:49:21 +02:00
|
|
|
done:
|
2007-08-02 21:18:32 +02:00
|
|
|
image_put ( image );
|
2007-08-03 13:49:21 +02:00
|
|
|
return rc;
|
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 ) {
|
2008-10-13 11:05:23 +02:00
|
|
|
struct sanboot_protocol *sanboot;
|
2007-07-08 23:01:49 +02:00
|
|
|
|
|
|
|
/* Quick hack */
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( sanboot, SANBOOT_PROTOCOLS ) {
|
2008-10-13 11:05:23 +02:00
|
|
|
if ( strncmp ( root_path, sanboot->prefix,
|
|
|
|
strlen ( sanboot->prefix ) ) == 0 ) {
|
|
|
|
return sanboot->boot ( root_path );
|
|
|
|
}
|
2007-07-29 03:31:14 +02:00
|
|
|
}
|
2007-07-08 23:01:49 +02:00
|
|
|
|
2007-07-29 03:31:14 +02:00
|
|
|
return -ENOTSUP;
|
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 ) {
|
2009-02-03 03:46:06 +01:00
|
|
|
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];
|
2008-07-31 17:55:46 +02:00
|
|
|
struct in_addr next_server;
|
2009-02-03 03:46:06 +01:00
|
|
|
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 );
|
|
|
|
|
2008-04-22 18:40:50 +02:00
|
|
|
/* Wait for link-up */
|
2009-06-19 11:08:21 +02:00
|
|
|
if ( ( rc = iflinkwait ( netdev, LINK_WAIT_MS ) ) != 0 )
|
2008-04-22 18:40:50 +02:00
|
|
|
return rc;
|
|
|
|
|
2007-07-08 23:01:49 +02:00
|
|
|
/* Configure device via DHCP */
|
|
|
|
if ( ( rc = dhcp ( netdev ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
route();
|
|
|
|
|
2009-02-03 03:46:06 +01:00
|
|
|
/* 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 );
|
2009-01-31 08:36:05 +01:00
|
|
|
if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
|
2009-02-03 03:46:06 +01:00
|
|
|
setting_exists ( NULL, &pxe_boot_menu_setting ) &&
|
|
|
|
( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
|
|
|
|
setting_exists ( NULL, &filename_setting ) ) ) ) {
|
2009-01-31 08:36:05 +01:00
|
|
|
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 */
|
2008-07-31 17:55:46 +02:00
|
|
|
fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
|
2008-03-25 21:46:16 +01:00
|
|
|
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 );
|
2008-07-31 17:55:46 +02:00
|
|
|
return boot_next_server_and_filename ( next_server, buf );
|
2007-01-12 11:08:27 +01:00
|
|
|
}
|
2007-07-08 23:01:49 +02:00
|
|
|
|
|
|
|
/* No filename; try the root path */
|
2008-03-25 21:46:16 +01:00
|
|
|
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 );
|
|
|
|
return boot_root_path ( buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ( "No filename or root path specified\n" );
|
|
|
|
return -ENOENT;
|
2007-01-10 21:21:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2007-01-10 03:03:20 +01:00
|
|
|
|
2007-01-10 21:21:42 +01:00
|
|
|
for_each_netdev ( netdev ) {
|
2007-01-10 02:55:07 +01:00
|
|
|
ifclose ( netdev );
|
|
|
|
}
|
2007-01-10 21:21:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
2007-01-04 05:20:08 +01:00
|
|
|
|
2007-01-10 02:55:07 +01:00
|
|
|
printf ( "No more network devices\n" );
|
2006-12-20 08:04:08 +01:00
|
|
|
}
|