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 * Perform PXE menu boot when PXE stack is not available
48 __weak
int pxe_menu_boot ( struct net_device
*netdev __unused
) {
53 * Identify the boot network device
55 * @ret netdev Boot network device
57 static struct net_device
* find_boot_netdev ( void ) {
62 * Boot using next-server and filename
64 * @v filename Boot filename
65 * @ret rc Return status code
67 int boot_next_server_and_filename ( struct in_addr next_server
,
68 const char *filename
) {
71 char buf
[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ +
72 ( 3 * strlen(filename
) ) /* completely URI-encoded */
74 int filename_is_absolute
;
78 uri
= parse_uri ( filename
);
81 filename_is_absolute
= uri_is_absolute ( uri
);
83 if ( ! filename_is_absolute
) {
84 /* Construct a tftp:// URI for the filename. We can't
85 * just rely on the current working URI, because the
86 * relative URI resolution will remove the distinction
87 * between filenames with and without initial slashes,
88 * which is significant for TFTP.
90 snprintf ( buf
, sizeof ( buf
), "tftp://%s/",
91 inet_ntoa ( next_server
) );
92 uri_encode ( filename
, buf
+ strlen ( buf
),
93 sizeof ( buf
) - strlen ( buf
), URI_PATH
);
97 image
= alloc_image();
100 if ( ( rc
= imgfetch ( image
, filename
,
101 register_and_autoload_image
) ) != 0 ) {
104 if ( ( rc
= imgexec ( image
) ) != 0 )
113 * Boot using root path
115 * @v root_path Root path
116 * @ret rc Return status code
118 int boot_root_path ( const char *root_path
) {
119 struct sanboot_protocol
*sanboot
;
122 for_each_table_entry ( sanboot
, SANBOOT_PROTOCOLS
) {
123 if ( strncmp ( root_path
, sanboot
->prefix
,
124 strlen ( sanboot
->prefix
) ) == 0 ) {
125 return sanboot
->boot ( root_path
);
133 * Boot from a network device
135 * @v netdev Network device
136 * @ret rc Return status code
138 static int netboot ( struct net_device
*netdev
) {
139 struct setting vendor_class_id_setting
140 = { .tag
= DHCP_VENDOR_CLASS_ID
};
141 struct setting pxe_discovery_control_setting
142 = { .tag
= DHCP_PXE_DISCOVERY_CONTROL
};
143 struct setting pxe_boot_menu_setting
144 = { .tag
= DHCP_PXE_BOOT_MENU
};
146 struct in_addr next_server
;
147 unsigned int pxe_discovery_control
;
150 /* Open device and display device status */
151 if ( ( rc
= ifopen ( netdev
) ) != 0 )
155 /* Configure device via DHCP */
156 if ( ( rc
= dhcp ( netdev
) ) != 0 )
160 /* Try PXE menu boot, if applicable */
161 fetch_string_setting ( NULL
, &vendor_class_id_setting
,
162 buf
, sizeof ( buf
) );
163 pxe_discovery_control
=
164 fetch_uintz_setting ( NULL
, &pxe_discovery_control_setting
);
165 if ( ( strcmp ( buf
, "PXEClient" ) == 0 ) &&
166 setting_exists ( NULL
, &pxe_boot_menu_setting
) &&
167 ( ! ( ( pxe_discovery_control
& PXEBS_SKIP
) &&
168 setting_exists ( NULL
, &filename_setting
) ) ) ) {
169 printf ( "Booting from PXE menu\n" );
170 return pxe_menu_boot ( netdev
);
173 /* Try to download and boot whatever we are given as a filename */
174 fetch_ipv4_setting ( NULL
, &next_server_setting
, &next_server
);
175 fetch_string_setting ( NULL
, &filename_setting
, buf
, sizeof ( buf
) );
177 printf ( "Booting from filename \"%s\"\n", buf
);
178 if ( ( rc
= boot_next_server_and_filename ( next_server
,
180 printf ( "Could not boot from filename \"%s\": %s\n",
181 buf
, strerror ( rc
) );
187 /* No filename; try the root path */
188 fetch_string_setting ( NULL
, &root_path_setting
, buf
, sizeof ( buf
) );
190 printf ( "Booting from root path \"%s\"\n", buf
);
191 if ( ( rc
= boot_root_path ( buf
) ) != 0 ) {
192 printf ( "Could not boot from root path \"%s\": %s\n",
193 buf
, strerror ( rc
) );
199 printf ( "No filename or root path specified\n" );
204 * Close all open net devices
206 * Called before a fresh boot attempt in order to free up memory. We
207 * don't just close the device immediately after the boot fails,
208 * because there may still be TCP connections in the process of
211 static void close_all_netdevs ( void ) {
212 struct net_device
*netdev
;
214 for_each_netdev ( netdev
) {
222 void autoboot ( void ) {
223 struct net_device
*boot_netdev
;
224 struct net_device
*netdev
;
226 /* If we have an identifable boot device, try that first */
228 if ( ( boot_netdev
= find_boot_netdev() ) )
229 netboot ( boot_netdev
);
231 /* If that fails, try booting from any of the other devices */
232 for_each_netdev ( netdev
) {
233 if ( netdev
== boot_netdev
)
239 printf ( "No more network devices\n" );