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.
19 FILE_LICENCE ( GPL2_OR_LATER );
24 #include <gpxe/netdevice.h>
25 #include <gpxe/dhcp.h>
26 #include <gpxe/settings.h>
27 #include <gpxe/image.h>
28 #include <gpxe/sanboot.h>
30 #include <usr/ifmgmt.h>
31 #include <usr/route.h>
32 #include <usr/dhcpmgmt.h>
33 #include <usr/imgmgmt.h>
34 #include <usr/autoboot.h>
42 /** Shutdown flags for exit */
43 int shutdown_exit_flags = 0;
46 * Identify the boot network device
48 * @ret netdev Boot network device
50 static struct net_device * find_boot_netdev ( void ) {
55 * Boot using next-server and filename
57 * @v filename Boot filename
58 * @ret rc Return status code
60 int boot_next_server_and_filename ( struct in_addr next_server,
61 const char *filename ) {
64 char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
65 int filename_is_absolute;
69 uri = parse_uri ( filename );
71 printf ( "Out of memory\n" );
74 filename_is_absolute = uri_is_absolute ( uri );
76 if ( ! filename_is_absolute ) {
77 /* Construct a tftp:// URI for the filename. We can't
78 * just rely on the current working URI, because the
79 * relative URI resolution will remove the distinction
80 * between filenames with and without initial slashes,
81 * which is significant for TFTP.
83 snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
84 inet_ntoa ( next_server ), filename );
88 image = alloc_image();
90 printf ( "Out of memory\n" );
93 if ( ( rc = imgfetch ( image, filename,
94 register_and_autoload_image ) ) != 0 ) {
95 printf ( "Could not load %s: %s\n",
96 filename, strerror ( rc ) );
99 if ( ( rc = imgexec ( image ) ) != 0 ) {
100 printf ( "Could not boot %s: %s\n",
101 filename, strerror ( rc ) );
111 * Boot using root path
113 * @v root_path Root path
114 * @ret rc Return status code
116 int boot_root_path ( const char *root_path ) {
117 struct sanboot_protocol *sanboot;
120 for_each_table_entry ( sanboot, SANBOOT_PROTOCOLS ) {
121 if ( strncmp ( root_path, sanboot->prefix,
122 strlen ( sanboot->prefix ) ) == 0 ) {
123 return sanboot->boot ( root_path );
131 * Boot from a network device
133 * @v netdev Network device
134 * @ret rc Return status code
136 static int netboot ( struct net_device *netdev ) {
137 struct setting vendor_class_id_setting
138 = { .tag = DHCP_VENDOR_CLASS_ID };
139 struct setting pxe_discovery_control_setting
140 = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
141 struct setting pxe_boot_menu_setting
142 = { .tag = DHCP_PXE_BOOT_MENU };
144 struct in_addr next_server;
145 unsigned int pxe_discovery_control;
148 /* Open device and display device status */
149 if ( ( rc = ifopen ( netdev ) ) != 0 )
153 /* Configure device via DHCP */
154 if ( ( rc = dhcp ( netdev ) ) != 0 )
158 /* Try PXE menu boot, if applicable */
159 fetch_string_setting ( NULL, &vendor_class_id_setting,
160 buf, sizeof ( buf ) );
161 pxe_discovery_control =
162 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
163 if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
164 setting_exists ( NULL, &pxe_boot_menu_setting ) &&
165 ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
166 setting_exists ( NULL, &filename_setting ) ) ) ) {
167 printf ( "Booting from PXE menu\n" );
168 return pxe_menu_boot ( netdev );
171 /* Try to download and boot whatever we are given as a filename */
172 fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
173 fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
175 printf ( "Booting from filename \"%s\"\n", buf );
176 return boot_next_server_and_filename ( next_server, buf );
179 /* No filename; try the root path */
180 fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
182 printf ( "Booting from root path \"%s\"\n", buf );
183 return boot_root_path ( buf );
186 printf ( "No filename or root path specified\n" );
191 * Close all open net devices
193 * Called before a fresh boot attempt in order to free up memory. We
194 * don't just close the device immediately after the boot fails,
195 * because there may still be TCP connections in the process of
198 static void close_all_netdevs ( void ) {
199 struct net_device *netdev;
201 for_each_netdev ( netdev ) {
209 void autoboot ( void ) {
210 struct net_device *boot_netdev;
211 struct net_device *netdev;
213 /* If we have an identifable boot device, try that first */
215 if ( ( boot_netdev = find_boot_netdev() ) )
216 netboot ( boot_netdev );
218 /* If that fails, try booting from any of the other devices */
219 for_each_netdev ( netdev ) {
220 if ( netdev == boot_netdev )
226 printf ( "No more network devices\n" );