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