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