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