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