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 <gpxe/embedded.h>
26 #include <usr/ifmgmt.h>
27 #include <usr/route.h>
28 #include <usr/dhcpmgmt.h>
29 #include <usr/imgmgmt.h>
30 #include <usr/iscsiboot.h>
31 #include <usr/aoeboot.h>
32 #include <usr/autoboot.h>
41 * Identify the boot network device
43 * @ret netdev Boot network device
45 static struct net_device * find_boot_netdev ( void ) {
52 * @ret rc Return status code
54 static int boot_embedded_image ( void ) {
58 image = embedded_image();
62 if ( ( rc = imgload ( image ) ) != 0 ) {
63 printf ( "Could not load embedded image: %s\n",
65 } else if ( ( rc = imgexec ( image ) ) != 0 ) {
66 printf ( "Could not boot embedded image: %s\n",
76 * @v filename Boot filename
77 * @ret rc Return status code
79 static int boot_filename ( const char *filename ) {
83 image = alloc_image();
85 printf ( "Out of memory\n" );
88 if ( ( rc = imgfetch ( image, filename,
89 register_and_autoload_image ) ) != 0 ) {
90 printf ( "Could not load %s: %s\n",
91 filename, strerror ( rc ) );
94 if ( ( rc = imgexec ( image ) ) != 0 ) {
95 printf ( "Could not boot %s: %s\n",
96 filename, strerror ( rc ) );
106 * Boot using root path
108 * @v root_path Root path
109 * @ret rc Return status code
111 int boot_root_path ( const char *root_path ) {
114 if ( strncmp ( root_path, "iscsi:", 6 ) == 0 ) {
115 return iscsiboot ( root_path );
116 } else if ( strncmp ( root_path, "aoe:", 4 ) == 0 ) {
117 return aoeboot ( root_path );
124 * Boot from a network device
126 * @v netdev Network device
127 * @ret rc Return status code
129 static int netboot ( struct net_device *netdev ) {
133 /* Open device and display device status */
134 if ( ( rc = ifopen ( netdev ) ) != 0 )
138 /* Configure device via DHCP */
139 if ( ( rc = dhcp ( netdev ) ) != 0 )
143 /* Try to boot an embedded image if we have one */
144 rc = boot_embedded_image ();
148 /* Try to download and boot whatever we are given as a filename */
149 dhcp_snprintf ( buf, sizeof ( buf ),
150 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
152 printf ( "Booting from filename \"%s\"\n", buf );
153 return boot_filename ( buf );
156 /* No filename; try the root path */
157 dhcp_snprintf ( buf, sizeof ( buf ),
158 find_global_dhcp_option ( DHCP_ROOT_PATH ) );
160 printf ( "Booting from root path \"%s\"\n", buf );
161 return boot_root_path ( buf );
164 printf ( "No filename or root path specified\n" );
169 * Close all open net devices
171 * Called before a fresh boot attempt in order to free up memory. We
172 * don't just close the device immediately after the boot fails,
173 * because there may still be TCP connections in the process of
176 static void close_all_netdevs ( void ) {
177 struct net_device *netdev;
179 for_each_netdev ( netdev ) {
187 void autoboot ( void ) {
188 struct net_device *boot_netdev;
189 struct net_device *netdev;
191 /* If we have an identifable boot device, try that first */
193 if ( ( boot_netdev = find_boot_netdev() ) )
194 netboot ( boot_netdev );
196 /* If that fails, try booting from any of the other devices */
197 for_each_netdev ( netdev ) {
198 if ( netdev == boot_netdev )
204 printf ( "No more network devices\n" );