1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <ipxe/netdevice.h>
30 #include <ipxe/dhcp.h>
31 #include <ipxe/settings.h>
32 #include <ipxe/image.h>
33 #include <ipxe/sanboot.h>
34 #include <ipxe/uri.h>
35 #include <ipxe/open.h>
36 #include <ipxe/init.h>
37 #include <ipxe/keys.h>
38 #include <ipxe/version.h>
39 #include <ipxe/shell.h>
40 #include <ipxe/features.h>
41 #include <ipxe/image.h>
42 #include <ipxe/timer.h>
43 #include <usr/ifmgmt.h>
44 #include <usr/route.h>
45 #include <usr/imgmgmt.h>
46 #include <usr/prompt.h>
47 #include <usr/autoboot.h>
48 #include <config/general.h>
49 #include <config/branding.h>
50 
51 /** @file
52  *
53  * Automatic booting
54  *
55  */
56 
57 /** Link-layer address of preferred autoboot device, if known */
58 static uint8_t autoboot_ll_addr[MAX_LL_ADDR_LEN];
59 
60 /** Device location of preferred autoboot device, if known */
61 static struct device_description autoboot_desc;
62 
63 /** Autoboot device tester */
64 static int ( * is_autoboot_device ) ( struct net_device *netdev );
65 
66 /* Disambiguate the various error causes */
67 #define ENOENT_BOOT __einfo_error ( EINFO_ENOENT_BOOT )
68 #define EINFO_ENOENT_BOOT \
69 	__einfo_uniqify ( EINFO_ENOENT, 0x01, "Nothing to boot" )
70 
71 #define NORMAL	"\033[0m"
72 #define BOLD	"\033[1m"
73 #define CYAN	"\033[36m"
74 
75 /** The "scriptlet" setting */
76 const struct setting scriptlet_setting __setting ( SETTING_MISC, scriptlet ) = {
77 	.name = "scriptlet",
78 	.description = "Boot scriptlet",
79 	.tag = DHCP_EB_SCRIPTLET,
80 	.type = &setting_type_string,
81 };
82 
83 /**
84  * Perform PXE menu boot when PXE stack is not available
85  */
pxe_menu_boot(struct net_device * netdev __unused)86 __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
87 	return -ENOTSUP;
88 }
89 
90 /** The "keep-san" setting */
91 const struct setting keep_san_setting __setting ( SETTING_SANBOOT_EXTRA,
92 						  keep-san ) = {
93 	.name = "keep-san",
94 	.description = "Preserve SAN connection",
95 	.tag = DHCP_EB_KEEP_SAN,
96 	.type = &setting_type_int8,
97 };
98 
99 /** The "skip-san-boot" setting */
100 const struct setting skip_san_boot_setting __setting ( SETTING_SANBOOT_EXTRA,
101 						       skip-san-boot ) = {
102 	.name = "skip-san-boot",
103 	.description = "Do not boot from SAN device",
104 	.tag = DHCP_EB_SKIP_SAN_BOOT,
105 	.type = &setting_type_int8,
106 };
107 
108 /**
109  * Boot from filename and root-path URIs
110  *
111  * @v filename		Filename
112  * @v root_paths	Root path(s)
113  * @v root_path_count	Number of root paths
114  * @v drive		SAN drive (if applicable)
115  * @v san_filename	SAN filename (or NULL to use default)
116  * @v flags		Boot action flags
117  * @ret rc		Return status code
118  *
119  * The somewhat tortuous flow of control in this function exists in
120  * order to ensure that the "sanboot" command remains identical in
121  * function to a SAN boot via a DHCP-specified root path, and to
122  * provide backwards compatibility for the "keep-san" and
123  * "skip-san-boot" options.
124  */
uriboot(struct uri * filename,struct uri ** root_paths,unsigned int root_path_count,int drive,const char * san_filename,unsigned int flags)125 int uriboot ( struct uri *filename, struct uri **root_paths,
126 	      unsigned int root_path_count, int drive,
127 	      const char *san_filename, unsigned int flags ) {
128 	struct image *image;
129 	int rc;
130 
131 	/* Hook SAN device, if applicable */
132 	if ( root_path_count ) {
133 		drive = san_hook ( drive, root_paths, root_path_count,
134 				   ( ( flags & URIBOOT_NO_SAN_DESCRIBE ) ?
135 				     SAN_NO_DESCRIBE : 0 ) );
136 		if ( drive < 0 ) {
137 			rc = drive;
138 			printf ( "Could not open SAN device: %s\n",
139 				 strerror ( rc ) );
140 			goto err_san_hook;
141 		}
142 		printf ( "Registered SAN device %#02x\n", drive );
143 	}
144 
145 	/* Describe SAN device, if applicable */
146 	if ( ! ( flags & URIBOOT_NO_SAN_DESCRIBE ) ) {
147 		if ( ( rc = san_describe() ) != 0 ) {
148 			printf ( "Could not describe SAN devices: %s\n",
149 				 strerror ( rc ) );
150 			goto err_san_describe;
151 		}
152 	}
153 
154 	/* Allow a root-path-only boot with skip-san enabled to succeed */
155 	rc = 0;
156 
157 	/* Attempt filename boot if applicable */
158 	if ( filename ) {
159 		if ( ( rc = imgdownload ( filename, 0, &image ) ) != 0 )
160 			goto err_download;
161 		imgstat ( image );
162 		image->flags |= IMAGE_AUTO_UNREGISTER;
163 		if ( ( rc = image_exec ( image ) ) != 0 ) {
164 			printf ( "Could not boot image: %s\n",
165 				 strerror ( rc ) );
166 			/* Fall through to (possibly) attempt a SAN boot
167 			 * as a fallback.  If no SAN boot is attempted,
168 			 * our status will become the return status.
169 			 */
170 		} else {
171 			/* Always print an extra newline, because we
172 			 * don't know where the NBP may have left the
173 			 * cursor.
174 			 */
175 			printf ( "\n" );
176 		}
177 	}
178 
179 	/* Attempt SAN boot if applicable */
180 	if ( ! ( flags & URIBOOT_NO_SAN_BOOT ) ) {
181 		if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) == 0 ) {
182 			printf ( "Booting%s%s from SAN device %#02x\n",
183 				 ( san_filename ? " " : "" ),
184 				 ( san_filename ? san_filename : "" ), drive );
185 			rc = san_boot ( drive, san_filename );
186 			printf ( "Boot from SAN device %#02x failed: %s\n",
187 				 drive, strerror ( rc ) );
188 		} else {
189 			printf ( "Skipping boot from SAN device %#02x\n",
190 				 drive );
191 			/* Avoid overwriting a possible failure status
192 			 * from a filename boot.
193 			 */
194 		}
195 	}
196 
197  err_download:
198  err_san_describe:
199 	/* Unhook SAN device, if applicable */
200 	if ( ! ( flags & URIBOOT_NO_SAN_UNHOOK ) ) {
201 		if ( fetch_intz_setting ( NULL, &keep_san_setting ) == 0 ) {
202 			san_unhook ( drive );
203 			printf ( "Unregistered SAN device %#02x\n", drive );
204 		} else {
205 			printf ( "Preserving SAN device %#02x\n", drive );
206 		}
207 	}
208  err_san_hook:
209 	return rc;
210 }
211 
212 /**
213  * Close all but one network device
214  *
215  * Called before a fresh boot attempt in order to free up memory.  We
216  * don't just close the device immediately after the boot fails,
217  * because there may still be TCP connections in the process of
218  * closing.
219  */
close_other_netdevs(struct net_device * netdev)220 static void close_other_netdevs ( struct net_device *netdev ) {
221 	struct net_device *other;
222 
223 	for_each_netdev ( other ) {
224 		if ( other != netdev )
225 			ifclose ( other );
226 	}
227 }
228 
229 /**
230  * Fetch next-server and filename settings into a URI
231  *
232  * @v settings		Settings block
233  * @ret uri		URI, or NULL on failure
234  */
fetch_next_server_and_filename(struct settings * settings)235 struct uri * fetch_next_server_and_filename ( struct settings *settings ) {
236 	union {
237 		struct sockaddr sa;
238 		struct sockaddr_in sin;
239 	} next_server;
240 	char *raw_filename = NULL;
241 	struct uri *uri = NULL;
242 	char *filename;
243 
244 	/* Initialise server address */
245 	memset ( &next_server, 0, sizeof ( next_server ) );
246 
247 	/* If we have a filename, fetch it along with the next-server
248 	 * setting from the same settings block.
249 	 */
250 	if ( fetch_setting ( settings, &filename_setting, &settings,
251 			     NULL, NULL, 0 ) >= 0 ) {
252 		fetch_string_setting_copy ( settings, &filename_setting,
253 					    &raw_filename );
254 		fetch_ipv4_setting ( settings, &next_server_setting,
255 				     &next_server.sin.sin_addr );
256 	}
257 	if ( ! raw_filename )
258 		goto err_fetch;
259 
260 	/* Populate server address */
261 	if ( next_server.sin.sin_addr.s_addr ) {
262 		next_server.sin.sin_family = AF_INET;
263 		printf ( "Next server: %s\n",
264 			 inet_ntoa ( next_server.sin.sin_addr ) );
265 	}
266 
267 	/* Expand filename setting */
268 	filename = expand_settings ( raw_filename );
269 	if ( ! filename )
270 		goto err_expand;
271 	if ( filename[0] )
272 		printf ( "Filename: %s\n", filename );
273 
274 	/* Construct URI */
275 	uri = pxe_uri ( &next_server.sa, filename );
276 	if ( ! uri )
277 		goto err_parse;
278 
279  err_parse:
280 	free ( filename );
281  err_expand:
282 	free ( raw_filename );
283  err_fetch:
284 	return uri;
285 }
286 
287 /**
288  * Fetch root-path setting into a URI
289  *
290  * @v settings		Settings block
291  * @ret uri		URI, or NULL on failure
292  */
fetch_root_path(struct settings * settings)293 static struct uri * fetch_root_path ( struct settings *settings ) {
294 	struct uri *uri = NULL;
295 	char *raw_root_path;
296 	char *root_path;
297 
298 	/* Fetch root-path setting */
299 	fetch_string_setting_copy ( settings, &root_path_setting,
300 				    &raw_root_path );
301 	if ( ! raw_root_path )
302 		goto err_fetch;
303 
304 	/* Expand filename setting */
305 	root_path = expand_settings ( raw_root_path );
306 	if ( ! root_path )
307 		goto err_expand;
308 	if ( root_path[0] )
309 		printf ( "Root path: %s\n", root_path );
310 
311 	/* Parse root path */
312 	uri = parse_uri ( root_path );
313 	if ( ! uri )
314 		goto err_parse;
315 
316  err_parse:
317 	free ( root_path );
318  err_expand:
319 	free ( raw_root_path );
320  err_fetch:
321 	return uri;
322 }
323 
324 /**
325  * Fetch san-filename setting
326  *
327  * @v settings		Settings block
328  * @ret san_filename	SAN filename, or NULL on failure
329  */
fetch_san_filename(struct settings * settings)330 static char * fetch_san_filename ( struct settings *settings ) {
331 	char *raw_san_filename;
332 	char *san_filename = NULL;
333 
334 	/* Fetch san-filename setting */
335 	fetch_string_setting_copy ( settings, &san_filename_setting,
336 				    &raw_san_filename );
337 	if ( ! raw_san_filename )
338 		goto err_fetch;
339 
340 	/* Expand san-filename setting */
341 	san_filename = expand_settings ( raw_san_filename );
342 	if ( ! san_filename )
343 		goto err_expand;
344 	if ( san_filename[0] )
345 		printf ( "SAN filename: %s\n", san_filename );
346 
347  err_expand:
348 	free ( raw_san_filename );
349  err_fetch:
350 	return san_filename;
351 }
352 
353 /**
354  * Check whether or not we have a usable PXE menu
355  *
356  * @ret have_menu	A usable PXE menu is present
357  */
have_pxe_menu(void)358 static int have_pxe_menu ( void ) {
359 	struct setting vendor_class_id_setting
360 		= { .tag = DHCP_VENDOR_CLASS_ID };
361 	struct setting pxe_discovery_control_setting
362 		= { .tag = DHCP_PXE_DISCOVERY_CONTROL };
363 	struct setting pxe_boot_menu_setting
364 		= { .tag = DHCP_PXE_BOOT_MENU };
365 	char buf[ 10 /* "PXEClient" + NUL */ ];
366 	unsigned int pxe_discovery_control;
367 
368 	fetch_string_setting ( NULL, &vendor_class_id_setting,
369 			       buf, sizeof ( buf ) );
370 	pxe_discovery_control =
371 		fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
372 
373 	return ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
374 		 setting_exists ( NULL, &pxe_boot_menu_setting ) &&
375 		 ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
376 		       setting_exists ( NULL, &filename_setting ) ) ) );
377 }
378 
379 /**
380  * Boot from a network device
381  *
382  * @v netdev		Network device
383  * @ret rc		Return status code
384  */
netboot(struct net_device * netdev)385 int netboot ( struct net_device *netdev ) {
386 	struct uri *filename;
387 	struct uri *root_path;
388 	char *san_filename;
389 	int rc;
390 
391 	/* Close all other network devices */
392 	close_other_netdevs ( netdev );
393 
394 	/* Open device and display device status */
395 	if ( ( rc = ifopen ( netdev ) ) != 0 )
396 		goto err_ifopen;
397 	ifstat ( netdev );
398 
399 	/* Configure device */
400 	if ( ( rc = ifconf ( netdev, NULL, 0 ) ) != 0 )
401 		goto err_dhcp;
402 	route();
403 
404 	/* Try PXE menu boot, if applicable */
405 	if ( have_pxe_menu() ) {
406 		printf ( "Booting from PXE menu\n" );
407 		rc = pxe_menu_boot ( netdev );
408 		goto err_pxe_menu_boot;
409 	}
410 
411 	/* Fetch next server and filename (if any) */
412 	filename = fetch_next_server_and_filename ( NULL );
413 
414 	/* Fetch root path (if any) */
415 	root_path = fetch_root_path ( NULL );
416 
417 	/* Fetch SAN filename (if any) */
418 	san_filename = fetch_san_filename ( NULL );
419 
420 	/* If we have both a filename and a root path, ignore an
421 	 * unsupported or missing URI scheme in the root path, since
422 	 * it may represent an NFS root.
423 	 */
424 	if ( filename && root_path &&
425 	     ( ( ! uri_is_absolute ( root_path ) ) ||
426 	       ( xfer_uri_opener ( root_path->scheme ) == NULL ) ) ) {
427 		printf ( "Ignoring unsupported root path\n" );
428 		uri_put ( root_path );
429 		root_path = NULL;
430 	}
431 
432 	/* Check that we have something to boot */
433 	if ( ! ( filename || root_path ) ) {
434 		rc = -ENOENT_BOOT;
435 		printf ( "Nothing to boot: %s\n", strerror ( rc ) );
436 		goto err_no_boot;
437 	}
438 
439 	/* Boot using next server, filename and root path */
440 	if ( ( rc = uriboot ( filename, &root_path, ( root_path ? 1 : 0 ),
441 			      san_default_drive(), san_filename,
442 			      ( root_path ? 0 : URIBOOT_NO_SAN ) ) ) != 0 )
443 		goto err_uriboot;
444 
445  err_uriboot:
446  err_no_boot:
447 	free ( san_filename );
448 	uri_put ( root_path );
449 	uri_put ( filename );
450  err_pxe_menu_boot:
451  err_dhcp:
452  err_ifopen:
453 	return rc;
454 }
455 
456 /**
457  * Test if network device matches the autoboot device bus type and location
458  *
459  * @v netdev		Network device
460  * @ret is_autoboot	Network device matches the autoboot device
461  */
is_autoboot_busloc(struct net_device * netdev)462 static int is_autoboot_busloc ( struct net_device *netdev ) {
463 	struct device *dev;
464 
465 	for ( dev = netdev->dev ; dev ; dev = dev->parent ) {
466 		if ( ( dev->desc.bus_type == autoboot_desc.bus_type ) &&
467 		     ( dev->desc.location == autoboot_desc.location ) )
468 			return 1;
469 	}
470 	return 0;
471 }
472 
473 /**
474  * Identify autoboot device by bus type and location
475  *
476  * @v bus_type		Bus type
477  * @v location		Location
478  */
set_autoboot_busloc(unsigned int bus_type,unsigned int location)479 void set_autoboot_busloc ( unsigned int bus_type, unsigned int location ) {
480 
481 	/* Record autoboot device description */
482 	autoboot_desc.bus_type = bus_type;
483 	autoboot_desc.location = location;
484 
485 	/* Mark autoboot device as present */
486 	is_autoboot_device = is_autoboot_busloc;
487 }
488 
489 /**
490  * Test if network device matches the autoboot device link-layer address
491  *
492  * @v netdev		Network device
493  * @ret is_autoboot	Network device matches the autoboot device
494  */
is_autoboot_ll_addr(struct net_device * netdev)495 static int is_autoboot_ll_addr ( struct net_device *netdev ) {
496 
497 	return ( memcmp ( netdev->ll_addr, autoboot_ll_addr,
498 			  netdev->ll_protocol->ll_addr_len ) == 0 );
499 }
500 
501 /**
502  * Identify autoboot device by link-layer address
503  *
504  * @v ll_addr		Link-layer address
505  * @v len		Length of link-layer address
506  */
set_autoboot_ll_addr(const void * ll_addr,size_t len)507 void set_autoboot_ll_addr ( const void *ll_addr, size_t len ) {
508 
509 	/* Record autoboot link-layer address (truncated if necessary) */
510 	if ( len > sizeof ( autoboot_ll_addr ) )
511 		len = sizeof ( autoboot_ll_addr );
512 	memcpy ( autoboot_ll_addr, ll_addr, len );
513 
514 	/* Mark autoboot device as present */
515 	is_autoboot_device = is_autoboot_ll_addr;
516 }
517 
518 /**
519  * Boot the system
520  */
autoboot(void)521 static int autoboot ( void ) {
522 	struct net_device *netdev;
523 	int rc = -ENODEV;
524 
525 	/* Try booting from each network device.  If we have a
526 	 * specified autoboot device location, then use only devices
527 	 * matching that location.
528 	 */
529 	for_each_netdev ( netdev ) {
530 
531 		/* Skip any non-matching devices, if applicable */
532 		if ( is_autoboot_device && ( ! is_autoboot_device ( netdev ) ) )
533 			continue;
534 
535 		/* Attempt booting from this device */
536 		rc = netboot ( netdev );
537 	}
538 
539 	printf ( "No more network devices\n" );
540 	return rc;
541 }
542 
543 /**
544  * Prompt for shell entry
545  *
546  * @ret	enter_shell	User wants to enter shell
547  */
shell_banner(void)548 static int shell_banner ( void ) {
549 
550 	/* Skip prompt if timeout is zero */
551 	if ( BANNER_TIMEOUT <= 0 )
552 		return 0;
553 
554 	/* Prompt user */
555 	printf ( "\n" );
556 	return ( prompt ( "Press Ctrl-B for the " PRODUCT_SHORT_NAME
557 			  " command line...",
558 			  ( ( BANNER_TIMEOUT * TICKS_PER_SEC ) / 10 ),
559 			  CTRL_B ) == 0 );
560 }
561 
562 /**
563  * Main iPXE flow of execution
564  *
565  * @v netdev		Network device, or NULL
566  * @ret rc		Return status code
567  */
ipxe(struct net_device * netdev)568 int ipxe ( struct net_device *netdev ) {
569 	struct feature *feature;
570 	struct image *image;
571 	char *scriptlet;
572 	int rc;
573 
574 	/*
575 	 * Print welcome banner
576 	 *
577 	 *
578 	 * If you wish to brand this build of iPXE, please do so by
579 	 * defining the string PRODUCT_NAME in config/branding.h.
580 	 *
581 	 * While nothing in the GPL prevents you from removing all
582 	 * references to iPXE or http://ipxe.org, we prefer you not to
583 	 * do so.
584 	 *
585 	 */
586 	printf ( NORMAL "\n\n" PRODUCT_NAME "\n" BOLD PRODUCT_SHORT_NAME " %s"
587 		 NORMAL " -- " PRODUCT_TAG_LINE " -- "
588 		 CYAN PRODUCT_URI NORMAL "\nFeatures:", product_version );
589 	for_each_table_entry ( feature, FEATURES )
590 		printf ( " %s", feature->name );
591 	printf ( "\n" );
592 
593 	/* Boot system */
594 	if ( ( image = first_image() ) != NULL ) {
595 		/* We have an embedded image; execute it */
596 		return image_exec ( image );
597 	} else if ( shell_banner() ) {
598 		/* User wants shell; just give them a shell */
599 		return shell();
600 	} else {
601 		fetch_string_setting_copy ( NULL, &scriptlet_setting,
602 					    &scriptlet );
603 		if ( scriptlet ) {
604 			/* User has defined a scriptlet; execute it */
605 			rc = system ( scriptlet );
606 			free ( scriptlet );
607 			return rc;
608 		} else {
609 			/* Try booting.  If booting fails, offer the
610 			 * user another chance to enter the shell.
611 			 */
612 			if ( netdev ) {
613 				rc = netboot ( netdev );
614 			} else {
615 				rc = autoboot();
616 			}
617 			if ( shell_banner() )
618 				rc = shell();
619 			return rc;
620 		}
621 	}
622 }
623