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 ) {
47 * Get the next network device to try
49 * @ret netdev 'Next' network device
51 * This function will cycle through all registered network devices in
52 * order, returning NULL.
54 * This function should be safe against registration/deregistration of
55 * net devices between calls to next_netdev().
57 static struct net_device * next_netdev ( void ) {
58 static struct net_device *last_netdev = NULL;
59 struct net_device *netdev;
61 for_each_netdev ( netdev ) {
62 if ( ! last_netdev ) {
66 if ( last_netdev == netdev )
75 * Boot from a network device
77 * @v netdev Network device
79 void netboot ( struct net_device *netdev ) {
84 /* Open device and display device status */
85 if ( ( rc = ifopen ( netdev ) != 0 ) )
89 /* Configure device via DHCP */
90 if ( ( rc = dhcp ( netdev ) != 0 ) )
94 /* Try to download and boot whatever we are given as a filename */
95 dhcp_snprintf ( filename, sizeof ( filename ),
96 find_global_dhcp_option ( DHCP_BOOTFILE_NAME ) );
97 if ( ! filename[0] ) {
98 printf ( "No boot filename\n" );
101 printf ( "Booting \"%s\"\n", filename );
102 if ( ( rc = imgfetch ( filename, NULL, &image ) ) != 0 ) {
103 printf ( "Could not retrieve %s: %s\n",
104 filename, strerror ( rc ) );
107 if ( ( rc = imgload ( image ) ) != 0 ) {
108 printf ( "Could not load %s: %s\n", image->name,
112 if ( ( rc = imgexec ( image ) ) != 0 ) {
113 printf ( "Could not execute %s: %s\n", image->name,
120 * Close all open net devices
122 * Called before a fresh boot attempt in order to free up memory. We
123 * don't just close the device immediately after the boot fails,
124 * because there may still be TCP connections in the process of
127 static void close_all_netdevs ( void ) {
128 struct net_device *netdev;
130 for_each_netdev ( netdev ) {
138 void autoboot ( void ) {
139 struct net_device *boot_netdev;
140 struct net_device *netdev;
142 /* If we have an identifable boot device, try that first */
144 if ( ( boot_netdev = find_boot_netdev() ) )
145 netboot ( boot_netdev );
147 /* If that fails, try booting from any of the other devices */
148 for_each_netdev ( netdev ) {
149 if ( netdev == boot_netdev )
155 printf ( "No more network devices\n" );