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