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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/netdevice.h>
23 #include <gpxe/dhcp.h>
24 #include <gpxe/image.h>
25 #include <usr/ifmgmt.h>
26 #include <usr/route.h>
27 #include <usr/dhcpmgmt.h>
28 #include <usr/imgmgmt.h>
29 #include <usr/autoboot.h>
38 * Identify the boot network device
40 * @ret netdev Boot network device
42 static struct net_device * find_boot_netdev ( void ) {
49 * @v filename Boot filename
50 * @ret rc Return status code
52 static int boot_filename ( const char *filename ) {
56 image = alloc_image();
58 printf ( "Out of memory\n" );
61 if ( ( rc = imgfetch ( image, filename, 0 ) ) != 0 ) {
62 printf ( "Could not retrieve %s: %s\n",
63 filename, strerror ( rc ) );
67 if ( ( rc = imgload ( image ) ) != 0 ) {
68 printf ( "Could not load %s: %s\n", image->name,
73 if ( ( rc = imgexec ( image ) ) != 0 ) {
74 printf ( "Could not execute %s: %s\n", image->name,
84 * Boot using root path
86 * @v root_path Root path
87 * @ret rc Return status code
89 static int boot_root_path ( const char *root_path ) {
93 if ( ( rc = iscsiboot ( root_path ) ) != 0 )
100 * Boot from a network device
102 * @v netdev Network device
103 * @ret rc Return status code
105 int netboot ( struct net_device *netdev ) {
109 /* Open device and display device status */
110 if ( ( rc = ifopen ( netdev ) ) != 0 )
114 /* Configure device via DHCP */
115 if ( ( rc = dhcp ( netdev ) ) != 0 )
119 /* Try to download and boot whatever we are given as a filename */
120 dhcp_snprintf ( buf, sizeof ( buf ),
121 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
123 printf ( "Booting from filename \"%s\"\n", buf );
124 return boot_filename ( buf );
127 /* No filename; try the root path */
128 dhcp_snprintf ( buf, sizeof ( buf ),
129 find_global_dhcp_option ( DHCP_ROOT_PATH ) );
131 printf ( "Booting from root path \"%s\"\n", buf );
132 return boot_root_path ( buf );
135 printf ( "No filename or root path specified\n" );
140 * Close all open net devices
142 * Called before a fresh boot attempt in order to free up memory. We
143 * don't just close the device immediately after the boot fails,
144 * because there may still be TCP connections in the process of
147 static void close_all_netdevs ( void ) {
148 struct net_device *netdev;
150 for_each_netdev ( netdev ) {
158 void autoboot ( void ) {
159 struct net_device *boot_netdev;
160 struct net_device *netdev;
162 /* If we have an identifable boot device, try that first */
164 if ( ( boot_netdev = find_boot_netdev() ) )
165 netboot ( boot_netdev );
167 /* If that fails, try booting from any of the other devices */
168 for_each_netdev ( netdev ) {
169 if ( netdev == boot_netdev )
175 printf ( "No more network devices\n" );