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/settings.h>
25 #include <gpxe/image.h>
26 #include <gpxe/embedded.h>
27 #include <gpxe/sanboot.h>
29 #include <usr/ifmgmt.h>
30 #include <usr/route.h>
31 #include <usr/dhcpmgmt.h>
32 #include <usr/imgmgmt.h>
33 #include <usr/autoboot.h>
41 /** Time to wait for link-up */
42 #define LINK_WAIT_MS 15000
44 /** Shutdown flags for exit */
45 int shutdown_exit_flags = 0;
47 /* SAN boot protocols */
48 static struct sanboot_protocol sanboot_protocols[0] \
49 __table_start ( struct sanboot_protocol, sanboot_protocols );
50 static struct sanboot_protocol sanboot_protocols_end[0] \
51 __table_end ( struct sanboot_protocol, sanboot_protocols );
54 * Identify the boot network device
56 * @ret netdev Boot network device
58 static struct net_device * find_boot_netdev ( void ) {
65 * @ret rc Return status code
67 static int boot_embedded_image ( void ) {
71 image = embedded_image();
75 if ( ( rc = imgload ( image ) ) != 0 ) {
76 printf ( "Could not load embedded image: %s\n",
78 } else if ( ( rc = imgexec ( image ) ) != 0 ) {
79 printf ( "Could not boot embedded image: %s\n",
87 * Boot using next-server and filename
89 * @v filename Boot filename
90 * @ret rc Return status code
92 int boot_next_server_and_filename ( struct in_addr next_server,
93 const char *filename ) {
96 char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
97 int filename_is_absolute;
101 uri = parse_uri ( filename );
103 printf ( "Out of memory\n" );
106 filename_is_absolute = uri_is_absolute ( uri );
108 if ( ! filename_is_absolute ) {
109 /* Construct a tftp:// URI for the filename. We can't
110 * just rely on the current working URI, because the
111 * relative URI resolution will remove the distinction
112 * between filenames with and without initial slashes,
113 * which is significant for TFTP.
115 snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
116 inet_ntoa ( next_server ), filename );
120 image = alloc_image();
122 printf ( "Out of memory\n" );
125 if ( ( rc = imgfetch ( image, filename,
126 register_and_autoload_image ) ) != 0 ) {
127 printf ( "Could not load %s: %s\n",
128 filename, strerror ( rc ) );
131 if ( ( rc = imgexec ( image ) ) != 0 ) {
132 printf ( "Could not boot %s: %s\n",
133 filename, strerror ( rc ) );
143 * Boot using root path
145 * @v root_path Root path
146 * @ret rc Return status code
148 int boot_root_path ( const char *root_path ) {
149 struct sanboot_protocol *sanboot;
152 for ( sanboot = sanboot_protocols ;
153 sanboot < sanboot_protocols_end ; sanboot++ ) {
154 if ( strncmp ( root_path, sanboot->prefix,
155 strlen ( sanboot->prefix ) ) == 0 ) {
156 return sanboot->boot ( root_path );
164 * Boot from a network device
166 * @v netdev Network device
167 * @ret rc Return status code
169 static int netboot ( struct net_device *netdev ) {
170 struct setting tmp_setting = { .name = NULL };
172 struct in_addr next_server;
175 /* Open device and display device status */
176 if ( ( rc = ifopen ( netdev ) ) != 0 )
180 /* Wait for link-up */
181 printf ( "Waiting for link-up on %s...", netdev->name );
182 if ( ( rc = iflinkwait ( netdev, LINK_WAIT_MS ) ) != 0 ) {
183 printf ( " no link detected\n" );
188 /* Configure device via DHCP */
189 if ( ( rc = dhcp ( netdev ) ) != 0 )
193 /* Try to boot an embedded image if we have one */
194 rc = boot_embedded_image ();
198 /* Try PXE menu boot, if we have PXE menu options */
199 tmp_setting.tag = DHCP_VENDOR_CLASS_ID;
200 fetch_string_setting ( NULL, &tmp_setting, buf, sizeof ( buf ) );
201 tmp_setting.tag = DHCP_PXE_BOOT_MENU;
202 if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
203 setting_exists ( NULL, &tmp_setting ) ) {
204 printf ( "Booting from PXE menu\n" );
205 return pxe_menu_boot ( netdev );
208 /* Try to download and boot whatever we are given as a filename */
209 fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
210 fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
212 printf ( "Booting from filename \"%s\"\n", buf );
213 return boot_next_server_and_filename ( next_server, buf );
216 /* No filename; try the root path */
217 fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
219 printf ( "Booting from root path \"%s\"\n", buf );
220 return boot_root_path ( buf );
223 printf ( "No filename or root path specified\n" );
228 * Close all open net devices
230 * Called before a fresh boot attempt in order to free up memory. We
231 * don't just close the device immediately after the boot fails,
232 * because there may still be TCP connections in the process of
235 static void close_all_netdevs ( void ) {
236 struct net_device *netdev;
238 for_each_netdev ( netdev ) {
246 void autoboot ( void ) {
247 struct net_device *boot_netdev;
248 struct net_device *netdev;
250 /* If we have an identifable boot device, try that first */
252 if ( ( boot_netdev = find_boot_netdev() ) )
253 netboot ( boot_netdev );
255 /* If that fails, try booting from any of the other devices */
256 for_each_netdev ( netdev ) {
257 if ( netdev == boot_netdev )
263 printf ( "No more network devices\n" );