[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / usr / autoboot.c
blobd76751ba59445d9f1367b5c84a73a979f7b435e3
1 /*
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 );
21 #include <string.h>
22 #include <stdio.h>
23 #include <errno.h>
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>
29 #include <gpxe/uri.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>
36 /** @file
38 * Automatic booting
42 /** Shutdown flags for exit */
43 int shutdown_exit_flags = 0;
45 /**
46 * Perform PXE menu boot when PXE stack is not available
48 __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
49 return -ENOTSUP;
52 /**
53 * Identify the boot network device
55 * @ret netdev Boot network device
57 static struct net_device * find_boot_netdev ( void ) {
58 return NULL;
61 /**
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 ) {
69 struct uri *uri;
70 struct image *image;
71 char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ +
72 ( 3 * strlen(filename) ) /* completely URI-encoded */
73 + 1 /* NUL */ ];
74 int filename_is_absolute;
75 int rc;
77 /* Construct URI */
78 uri = parse_uri ( filename );
79 if ( ! uri )
80 return -ENOMEM;
81 filename_is_absolute = uri_is_absolute ( uri );
82 uri_put ( 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 );
94 filename = buf;
97 image = alloc_image();
98 if ( ! image )
99 return -ENOMEM;
100 if ( ( rc = imgfetch ( image, filename,
101 register_and_autoload_image ) ) != 0 ) {
102 goto done;
104 if ( ( rc = imgexec ( image ) ) != 0 )
105 goto done;
107 done:
108 image_put ( image );
109 return rc;
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;
121 /* Quick hack */
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 );
129 return -ENOTSUP;
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 };
145 char buf[256];
146 struct in_addr next_server;
147 unsigned int pxe_discovery_control;
148 int rc;
150 /* Open device and display device status */
151 if ( ( rc = ifopen ( netdev ) ) != 0 )
152 return rc;
153 ifstat ( netdev );
155 /* Configure device via DHCP */
156 if ( ( rc = dhcp ( netdev ) ) != 0 )
157 return rc;
158 route();
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 ) );
176 if ( buf[0] ) {
177 printf ( "Booting from filename \"%s\"\n", buf );
178 if ( ( rc = boot_next_server_and_filename ( next_server,
179 buf ) ) != 0 ) {
180 printf ( "Could not boot from filename \"%s\": %s\n",
181 buf, strerror ( rc ) );
182 return rc;
184 return 0;
187 /* No filename; try the root path */
188 fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
189 if ( buf[0] ) {
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 ) );
194 return rc;
196 return 0;
199 printf ( "No filename or root path specified\n" );
200 return -ENOENT;
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
209 * closing.
211 static void close_all_netdevs ( void ) {
212 struct net_device *netdev;
214 for_each_netdev ( netdev ) {
215 ifclose ( netdev );
220 * Boot the system
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 */
227 close_all_netdevs();
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 )
234 continue;
235 close_all_netdevs();
236 netboot ( netdev );
239 printf ( "No more network devices\n" );