aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/usr/autoboot.c
blob: 326292b429d94a58a32ba25a76ac375d127f987e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <gpxe/netdevice.h>
#include <gpxe/dhcp.h>
#include <gpxe/settings.h>
#include <gpxe/image.h>
#include <gpxe/embedded.h>
#include <gpxe/uri.h>
#include <usr/ifmgmt.h>
#include <usr/route.h>
#include <usr/dhcpmgmt.h>
#include <usr/imgmgmt.h>
#include <usr/iscsiboot.h>
#include <usr/aoeboot.h>
#include <usr/autoboot.h>

/** @file
 *
 * Automatic booting
 *
 */

/** Time to wait for link-up */
#define LINK_WAIT_MS 15000

/** Shutdown flags for exit */
int shutdown_exit_flags = 0;

/**
 * Identify the boot network device
 *
 * @ret netdev		Boot network device
 */
static struct net_device * find_boot_netdev ( void ) {
	return NULL;
}

/**
 * Boot embedded image
 *
 * @ret rc		Return status code
 */
static int boot_embedded_image ( void ) {
	struct image *image;
	int rc;

	image = embedded_image();
	if ( !image )
		return ENOENT;

	if ( ( rc = imgload ( image ) ) != 0 ) {
		printf ( "Could not load embedded image: %s\n",
			 strerror ( rc ) );
	} else if ( ( rc = imgexec ( image ) ) != 0 ) {
		printf ( "Could not boot embedded image: %s\n",
			 strerror ( rc ) );
	}
	image_put ( image );
	return rc;
}

/**
 * Boot using next-server and filename
 *
 * @v filename		Boot filename
 * @ret rc		Return status code
 */
static int boot_next_server_and_filename ( struct in_addr next_server,
					   const char *filename ) {
	struct uri *uri;
	struct image *image;
	char buf[ 23 /* tftp://xxx.xxx.xxx.xxx/ */ + strlen(filename) + 1 ];
	int filename_is_absolute;
	int rc;

	/* Construct URI */
	uri = parse_uri ( filename );
	if ( ! uri ) {
		printf ( "Out of memory\n" );
		return -ENOMEM;
	}
	filename_is_absolute = uri_is_absolute ( uri );
	uri_put ( uri );
	if ( ! filename_is_absolute ) {
		/* Construct a tftp:// URI for the filename.  We can't
		 * just rely on the current working URI, because the
		 * relative URI resolution will remove the distinction
		 * between filenames with and without initial slashes,
		 * which is significant for TFTP.
		 */
		snprintf ( buf, sizeof ( buf ), "tftp://%s/%s",
			   inet_ntoa ( next_server ), filename );
		filename = buf;
	}

	image = alloc_image();
	if ( ! image ) {
		printf ( "Out of memory\n" );
		return -ENOMEM;
	}
	if ( ( rc = imgfetch ( image, filename,
			       register_and_autoload_image ) ) != 0 ) {
		printf ( "Could not load %s: %s\n",
			 filename, strerror ( rc ) );
		goto done;
	}
	if ( ( rc = imgexec ( image ) ) != 0 ) {
		printf ( "Could not boot %s: %s\n",
			 filename, strerror ( rc ) );
		goto done;
	}

 done:
	image_put ( image );
	return rc;
}

/**
 * Boot using root path
 *
 * @v root_path		Root path
 * @ret rc		Return status code
 */
int boot_root_path ( const char *root_path ) {

	/* Quick hack */
	if ( strncmp ( root_path, "iscsi:", 6 ) == 0 ) {
		return iscsiboot ( root_path );
	} else if ( strncmp ( root_path, "aoe:", 4 ) == 0 ) {
		return aoeboot ( root_path );
	}

	return -ENOTSUP;
}

/**
 * Boot from a network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int netboot ( struct net_device *netdev ) {
	char buf[256];
	struct in_addr next_server;
	int rc;

	/* Open device and display device status */
	if ( ( rc = ifopen ( netdev ) ) != 0 )
		return rc;
	ifstat ( netdev );

	/* Wait for link-up */
	printf ( "Waiting for link-up on %s...", netdev->name );
	if ( ( rc = iflinkwait ( netdev, LINK_WAIT_MS ) ) != 0 ) {
		printf ( " no link detected\n" );
		return rc;
	}
	printf ( " ok\n" );

	/* Configure device via DHCP */
	if ( ( rc = dhcp ( netdev ) ) != 0 )
		return rc;
	route();

	/* Try to boot an embedded image if we have one */
	rc = boot_embedded_image ();
	if ( rc != ENOENT )
		return rc;

	/* Try to download and boot whatever we are given as a filename */
	fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
	fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
	if ( buf[0] ) {
		printf ( "Booting from filename \"%s\"\n", buf );
		return boot_next_server_and_filename ( next_server, buf );
	}
	
	/* No filename; try the root path */
	fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
	if ( buf[0] ) {
		printf ( "Booting from root path \"%s\"\n", buf );
		return boot_root_path ( buf );
	}

	printf ( "No filename or root path specified\n" );
	return -ENOENT;
}

/**
 * Close all open net devices
 *
 * Called before a fresh boot attempt in order to free up memory.  We
 * don't just close the device immediately after the boot fails,
 * because there may still be TCP connections in the process of
 * closing.
 */
static void close_all_netdevs ( void ) {
	struct net_device *netdev;

	for_each_netdev ( netdev ) {
		ifclose ( netdev );
	}
}

/**
 * Boot the system
 */
void autoboot ( void ) {
	struct net_device *boot_netdev;
	struct net_device *netdev;

	/* If we have an identifable boot device, try that first */
	close_all_netdevs();
	if ( ( boot_netdev = find_boot_netdev() ) )
		netboot ( boot_netdev );

	/* If that fails, try booting from any of the other devices */
	for_each_netdev ( netdev ) {
		if ( netdev == boot_netdev )
			continue;
		close_all_netdevs();
		netboot ( netdev );
	}

	printf ( "No more network devices\n" );
}