menu.c 20 KB
Newer Older
1
/* menu.c - General supporting functionality for menus.  */
2
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010  Free Software Foundation, Inc.
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20 21
#include <grub/normal.h>
#include <grub/misc.h>
22
#include <grub/loader.h>
23
#include <grub/mm.h>
24
#include <grub/time.h>
25
#include <grub/env.h>
26
#include <grub/menu_viewer.h>
27
#include <grub/command.h>
28
#include <grub/parser.h>
29
#include <grub/auth.h>
30
#include <grub/i18n.h>
31
#include <grub/term.h>
32
#include <grub/script_sh.h>
33
#include <grub/gfxterm.h>
34
#include <grub/dl.h>
35 36 37 38 39

/* Time to delay after displaying an error message about a default/fallback
   entry failing to boot.  */
#define DEFAULT_ENTRY_ERROR_DELAY_MS  2500

40 41
grub_err_t (*grub_gfxmenu_try_hook) (int entry, grub_menu_t menu,
				     int nested) = NULL;
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
enum timeout_style {
  TIMEOUT_STYLE_MENU,
  TIMEOUT_STYLE_COUNTDOWN,
  TIMEOUT_STYLE_HIDDEN
};

struct timeout_style_name {
  const char *name;
  enum timeout_style style;
} timeout_style_names[] = {
  {"menu", TIMEOUT_STYLE_MENU},
  {"countdown", TIMEOUT_STYLE_COUNTDOWN},
  {"hidden", TIMEOUT_STYLE_HIDDEN},
  {NULL, 0}
};

59 60 61 62 63
/* Wait until the user pushes any key so that the user
   can see what happened.  */
void
grub_wait_after_message (void)
{
64
  grub_uint64_t endtime;
65
  grub_xputs ("\n");
66
  grub_printf_ (N_("Press any key to continue..."));
67 68 69 70
  grub_refresh ();

  endtime = grub_get_time_ms () + 10000;

71 72
  while (grub_get_time_ms () < endtime
	 && grub_getkey_noblock () == GRUB_TERM_NO_KEY);
73

74
  grub_xputs ("\n");
75
}
76

77 78 79
/* Get a menu entry by its index in the entry list.  */
grub_menu_entry_t
grub_menu_get_entry (grub_menu_t menu, int no)
80
{
81
  grub_menu_entry_t e;
82 83 84 85 86 87 88

  for (e = menu->entry_list; e && no > 0; e = e->next, no--)
    ;

  return e;
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* Get the index of a menu entry associated with a given hotkey, or -1.  */
static int
get_entry_index_by_hotkey (grub_menu_t menu, int hotkey)
{
  grub_menu_entry_t entry;
  int i;

  for (i = 0, entry = menu->entry_list; i < menu->size;
       i++, entry = entry->next)
    if (entry->hotkey == hotkey)
      return i;

  return -1;
}

/* Return the timeout style.  If the variable "timeout_style" is not set or
   invalid, default to TIMEOUT_STYLE_MENU.  */
static enum timeout_style
get_timeout_style (void)
{
  const char *val;
  struct timeout_style_name *style_name;

  val = grub_env_get ("timeout_style");
Colin Watson's avatar
Colin Watson committed
113 114 115 116 117 118
  if (!val)
    return TIMEOUT_STYLE_MENU;

  for (style_name = timeout_style_names; style_name->name; style_name++)
    if (grub_strcmp (style_name->name, val) == 0)
      return style_name->style;
119 120 121 122

  return TIMEOUT_STYLE_MENU;
}

123 124
/* Return the current timeout. If the variable "timeout" is not set or
   invalid, return -1.  */
125 126
int
grub_menu_get_timeout (void)
127
{
128
  const char *val;
129
  int timeout;
130

131 132 133
  val = grub_env_get ("timeout");
  if (! val)
    return -1;
134

135
  grub_error_push ();
136

137 138 139 140 141 142 143 144 145 146 147
  timeout = (int) grub_strtoul (val, 0, 0);

  /* If the value is invalid, unset the variable.  */
  if (grub_errno != GRUB_ERR_NONE)
    {
      grub_env_unset ("timeout");
      grub_errno = GRUB_ERR_NONE;
      timeout = -1;
    }

  grub_error_pop ();
148

149 150 151 152
  return timeout;
}

/* Set current timeout in the variable "timeout".  */
153 154
void
grub_menu_set_timeout (int timeout)
155 156 157 158 159
{
  /* Ignore TIMEOUT if it is zero, because it will be unset really soon.  */
  if (timeout > 0)
    {
      char buf[16];
160

161
      grub_snprintf (buf, sizeof (buf), "%d", timeout);
162 163 164 165
      grub_env_set ("timeout", buf);
    }
}

166
/* Get the first entry number from the value of the environment variable NAME,
167
   which is a space-separated list of non-negative integers.  The entry number
168 169
   which is returned is stripped from the value of NAME.  If no entry number
   can be found, -1 is returned.  */
170
static int
171
get_and_remove_first_entry_number (const char *name)
172
{
173
  const char *val;
174
  char *tail;
175
  int entry;
176

177 178 179
  val = grub_env_get (name);
  if (! val)
    return -1;
180

181
  grub_error_push ();
182

183
  entry = (int) grub_strtoul (val, &tail, 0);
184

185 186 187 188 189 190 191 192
  if (grub_errno == GRUB_ERR_NONE)
    {
      /* Skip whitespace to find the next digit.  */
      while (*tail && grub_isspace (*tail))
	tail++;
      grub_env_set (name, tail);
    }
  else
193
    {
194
      grub_env_unset (name);
195 196 197 198 199
      grub_errno = GRUB_ERR_NONE;
      entry = -1;
    }

  grub_error_pop ();
200

201 202 203
  return entry;
}

204
/* Run a menu entry.  */
205 206
static void
grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
207
{
208
  grub_err_t err = GRUB_ERR_NONE;
209
  int errs_before;
210
  grub_menu_t menu = NULL;
211 212 213
  char *optr, *buf, *oldchosen = NULL, *olddefault = NULL;
  const char *ptr, *chosen, *def;
  grub_size_t sz = 0;
214 215 216 217 218 219 220 221 222 223 224

  if (entry->restricted)
    err = grub_auth_check_authentication (entry->users);

  if (err)
    {
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;
      return;
    }

225 226
  errs_before = grub_err_printed_errors;

227 228 229
  chosen = grub_env_get ("chosen");
  def = grub_env_get ("default");

230 231 232 233 234 235 236
  if (entry->submenu)
    {
      grub_env_context_open ();
      menu = grub_zalloc (sizeof (*menu));
      if (! menu)
	return;
      grub_env_set_menu (menu);
237 238
      if (auto_boot)
	grub_env_set ("timeout", "0");
239 240
    }

241
  for (ptr = entry->id; *ptr; ptr++)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    sz += (*ptr == '>') ? 2 : 1;
  if (chosen)
    {
      oldchosen = grub_strdup (chosen);
      if (!oldchosen)
	grub_print_error ();
    }
  if (def)
    {
      olddefault = grub_strdup (def);
      if (!olddefault)
	grub_print_error ();
    }
  sz++;
  if (chosen)
    sz += grub_strlen (chosen);
  sz++;
  buf = grub_malloc (sz);
  if (!buf)
    grub_print_error ();
  else
    {
      optr = buf;
      if (chosen)
	{
	  optr = grub_stpcpy (optr, chosen);
	  *optr++ = '>';
	}
270
      for (ptr = entry->id; *ptr; ptr++)
271 272 273 274 275 276 277 278 279 280
	{
	  if (*ptr == '>')
	    *optr++ = '>';
	  *optr++ = *ptr;
	}
      *optr = 0;
      grub_env_set ("chosen", buf);
      grub_env_export ("chosen");
      grub_free (buf);
    }
281 282

  for (ptr = def; ptr && *ptr; ptr++)
283 284 285 286 287 288 289 290 291
    {
      if (ptr[0] == '>' && ptr[1] == '>')
	{
	  ptr++;
	  continue;
	}
      if (ptr[0] == '>')
	break;
    }
292 293

  if (ptr && ptr[0] && ptr[1])
294 295 296
    grub_env_set ("default", ptr + 1);
  else
    grub_env_unset ("default");
297

298
  grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
299

300 301 302
  if (errs_before != grub_err_printed_errors)
    grub_wait_after_message ();

303 304
  errs_before = grub_err_printed_errors;

305 306
  if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
    /* Implicit execution of boot, only if something is loaded.  */
307
    grub_command_execute ("boot", 0, 0);
308

309 310 311
  if (errs_before != grub_err_printed_errors)
    grub_wait_after_message ();

312 313 314 315
  if (entry->submenu)
    {
      if (menu && menu->size)
	{
316
	  grub_show_menu (menu, 1, auto_boot);
317 318 319 320
	  grub_normal_free_menu (menu);
	}
      grub_env_context_close ();
    }
321 322 323 324 325 326 327 328 329
  if (oldchosen)
    grub_env_set ("chosen", oldchosen);
  else
    grub_env_unset ("chosen");
  if (olddefault)
    grub_env_set ("default", olddefault);
  else
    grub_env_unset ("default");
  grub_env_unset ("timeout");
330 331
}

332 333 334 335
/* Execute ENTRY from the menu MENU, falling back to entries specified
   in the environment variable "fallback" if it fails.  CALLBACK is a
   pointer to a struct of function pointers which are used to allow the
   caller provide feedback to the user.  */
336
static void
337 338
grub_menu_execute_with_fallback (grub_menu_t menu,
				 grub_menu_entry_t entry,
339
				 int autobooted,
340 341
				 grub_menu_execute_callback_t callback,
				 void *callback_data)
342
{
343
  int fallback_entry;
344

345
  callback->notify_booting (entry, callback_data);
346

347
  grub_menu_execute_entry (entry, 1);
348

349 350 351 352 353 354
  /* Deal with fallback entries.  */
  while ((fallback_entry = get_and_remove_first_entry_number ("fallback"))
	 >= 0)
    {
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;
355

356 357
      entry = grub_menu_get_entry (menu, fallback_entry);
      callback->notify_fallback (entry, callback_data);
358
      grub_menu_execute_entry (entry, 1);
359 360 361 362
      /* If the function call to execute the entry returns at all, then this is
	 taken to indicate a boot failure.  For menu entries that do something
	 other than actually boot an operating system, this could assume
	 incorrectly that something failed.  */
363
    }
364

365 366
  if (!autobooted)
    callback->notify_failure (callback_data);
367
}
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
static struct grub_menu_viewer *viewers;

static void
menu_set_chosen_entry (int entry)
{
  struct grub_menu_viewer *cur;
  for (cur = viewers; cur; cur = cur->next)
    cur->set_chosen_entry (entry, cur->data);
}

static void
menu_print_timeout (int timeout)
{
  struct grub_menu_viewer *cur;
  for (cur = viewers; cur; cur = cur->next)
    cur->print_timeout (timeout, cur->data);
}

static void
menu_fini (void)
{
  struct grub_menu_viewer *cur, *next;
  for (cur = viewers; cur; cur = next)
    {
      next = cur->next;
      cur->fini (cur->data);
      grub_free (cur);
    }
  viewers = NULL;
}

static void
menu_init (int entry, grub_menu_t menu, int nested)
{
403
  struct grub_term_output *term;
404
  int gfxmenu = 0;
405 406

  FOR_ACTIVE_TERM_OUTPUTS(term)
407
    if (term->fullscreen)
408
      {
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	if (grub_env_get ("theme"))
	  {
	    if (!grub_gfxmenu_try_hook)
	      {
		grub_dl_load ("gfxmenu");
		grub_print_error ();
	      }
	    if (grub_gfxmenu_try_hook)
	      {
		grub_err_t err;
		err = grub_gfxmenu_try_hook (entry, menu, nested);
		if(!err)
		  {
		    gfxmenu = 1;
		    break;
		  }
	      }
	    else
427 428 429
	      grub_error (GRUB_ERR_BAD_MODULE,
			  N_("module `%s' isn't loaded"),
			  "gfxmenu");
430 431 432
	    grub_print_error ();
	    grub_wait_after_message ();
	  }
433
	grub_errno = GRUB_ERR_NONE;
434
	term->fullscreen ();
435
	break;
436 437
      }

438 439 440 441 442
  FOR_ACTIVE_TERM_OUTPUTS(term)
  {
    grub_err_t err;

    if (grub_strcmp (term->name, "gfxterm") == 0 && gfxmenu)
443
      continue;
444

445 446 447 448 449 450
    err = grub_menu_try_text (term, entry, menu, nested);
    if(!err)
      continue;
    grub_print_error ();
    grub_errno = GRUB_ERR_NONE;
  }
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
}

static void
clear_timeout (void)
{
  struct grub_menu_viewer *cur;
  for (cur = viewers; cur; cur = cur->next)
    cur->clear_timeout (cur->data);
}

void
grub_menu_register_viewer (struct grub_menu_viewer *viewer)
{
  viewer->next = viewers;
  viewers = viewer;
}

468
static int
469
menuentry_eq (const char *id, const char *spec)
470 471
{
  const char *ptr1, *ptr2;
472
  ptr1 = id;
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  ptr2 = spec;
  while (1)
    {
      if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
	return 1;
      if (*ptr2 == '>' && ptr2[1] != '>')
	return 0;
      if (*ptr2 == '>')
	ptr2++;
      if (*ptr1 != *ptr2)
	return 0;
      if (*ptr1 == 0)
	return 1;
      ptr1++;
      ptr2++;
    }
}


492 493 494 495
/* Get the entry number from the variable NAME.  */
static int
get_entry_number (grub_menu_t menu, const char *name)
{
496
  const char *val;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  int entry;

  val = grub_env_get (name);
  if (! val)
    return -1;

  grub_error_push ();

  entry = (int) grub_strtoul (val, 0, 0);

  if (grub_errno == GRUB_ERR_BAD_NUMBER)
    {
      /* See if the variable matches the title of a menu entry.  */
      grub_menu_entry_t e = menu->entry_list;
      int i;

      grub_errno = GRUB_ERR_NONE;

      for (i = 0; e; i++)
	{
517 518
	  if (menuentry_eq (e->title, val)
	      || menuentry_eq (e->id, val))
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	    {
	      entry = i;
	      break;
	    }
	  e = e->next;
	}

      if (! e)
	entry = -1;
    }

  if (grub_errno != GRUB_ERR_NONE)
    {
      grub_errno = GRUB_ERR_NONE;
      entry = -1;
    }

  grub_error_pop ();

  return entry;
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
/* Check whether a second has elapsed since the last tick.  If so, adjust
   the timer and return 1; otherwise, return 0.  */
static int
has_second_elapsed (grub_uint64_t *saved_time)
{
  grub_uint64_t current_time;

  current_time = grub_get_time_ms ();
  if (current_time - *saved_time >= 1000)
    {
      *saved_time = current_time;
      return 1;
    }
  else
    return 0;
}

static void
print_countdown (struct grub_term_coordinate *pos, int n)
{
  grub_term_restore_pos (pos);
  /* NOTE: Do not remove the trailing space characters.
     They are required to clear the line.  */
  grub_printf ("%d    ", n);
  grub_refresh ();
}

568 569 570 571 572 573 574 575 576 577 578 579 580 581
#define GRUB_MENU_PAGE_SIZE 10

/* Show the menu and handle menu entry selection.  Returns the menu entry
   index that should be executed or -1 if no entry should be executed (e.g.,
   Esc pressed to exit a sub-menu or switching menu viewers).
   If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
   entry to be executed is a result of an automatic default selection because
   of the timeout.  */
static int
run_menu (grub_menu_t menu, int nested, int *auto_boot)
{
  grub_uint64_t saved_time;
  int default_entry, current_entry;
  int timeout;
582
  enum timeout_style timeout_style;
583

584
  default_entry = get_entry_number (menu, "default");
585 586 587 588 589 590

  /* If DEFAULT_ENTRY is not within the menu entries, fall back to
     the first entry.  */
  if (default_entry < 0 || default_entry >= menu->size)
    default_entry = 0;

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  timeout = grub_menu_get_timeout ();
  if (timeout < 0)
    /* If there is no timeout, the "countdown" and "hidden" styles result in
       the system doing nothing and providing no or very little indication
       why.  Technically this is what the user asked for, but it's not very
       useful and likely to be a source of confusion, so we disallow this.  */
    grub_env_unset ("timeout_style");

  timeout_style = get_timeout_style ();

  if (timeout_style == TIMEOUT_STYLE_COUNTDOWN
      || timeout_style == TIMEOUT_STYLE_HIDDEN)
    {
      static struct grub_term_coordinate *pos;
      int entry = -1;

      if (timeout_style == TIMEOUT_STYLE_COUNTDOWN && timeout)
	{
	  pos = grub_term_save_pos ();
	  print_countdown (pos, timeout);
	}

      /* Enter interruptible sleep until Escape or a menu hotkey is pressed,
         or the timeout expires.  */
      saved_time = grub_get_time_ms ();
      while (1)
	{
	  int key;

	  key = grub_getkey_noblock ();
	  if (key != GRUB_TERM_NO_KEY)
	    {
	      entry = get_entry_index_by_hotkey (menu, key);
	      if (entry >= 0)
		break;
	    }
	  if (key == GRUB_TERM_ESC)
	    {
	      timeout = -1;
	      break;
	    }

	  if (timeout > 0 && has_second_elapsed (&saved_time))
	    {
	      timeout--;
	      if (timeout_style == TIMEOUT_STYLE_COUNTDOWN)
		print_countdown (pos, timeout);
	    }

	  if (timeout == 0)
	    /* We will fall through to auto-booting the default entry.  */
	    break;
	}

      grub_env_unset ("timeout");
      grub_env_unset ("timeout_style");
      if (entry >= 0)
	{
	  *auto_boot = 0;
	  return entry;
	}
    }

654
  /* If timeout is 0, drawing is pointless (and ugly).  */
655
  if (timeout == 0)
656 657 658 659 660 661 662 663 664 665
    {
      *auto_boot = 1;
      return default_entry;
    }

  current_entry = default_entry;

 refresh:
  menu_init (current_entry, menu, nested);

666 667 668
  /* Initialize the time.  */
  saved_time = grub_get_time_ms ();

669 670 671 672
  timeout = grub_menu_get_timeout ();

  if (timeout > 0)
    menu_print_timeout (timeout);
673 674
  else
    clear_timeout ();
675 676 677 678 679 680

  while (1)
    {
      int c;
      timeout = grub_menu_get_timeout ();

681 682 683
      if (grub_normal_exit_level)
	return -1;

684
      if (timeout > 0 && has_second_elapsed (&saved_time))
685
	{
686 687 688
	  timeout--;
	  grub_menu_set_timeout (timeout);
	  menu_print_timeout (timeout);
689 690 691 692 693 694 695 696 697 698
	}

      if (timeout == 0)
	{
	  grub_env_unset ("timeout");
          *auto_boot = 1;
	  menu_fini ();
	  return default_entry;
	}

699
      c = grub_getkey_noblock ();
700

701 702
      if (c != GRUB_TERM_NO_KEY)
	{
703 704 705 706 707 708 709 710 711
	  if (timeout >= 0)
	    {
	      grub_env_unset ("timeout");
	      grub_env_unset ("fallback");
	      clear_timeout ();
	    }

	  switch (c)
	    {
712 713
	    case GRUB_TERM_KEY_HOME:
	    case GRUB_TERM_CTRL | 'a':
714 715 716 717
	      current_entry = 0;
	      menu_set_chosen_entry (current_entry);
	      break;

718 719
	    case GRUB_TERM_KEY_END:
	    case GRUB_TERM_CTRL | 'e':
720 721 722 723
	      current_entry = menu->size - 1;
	      menu_set_chosen_entry (current_entry);
	      break;

724 725
	    case GRUB_TERM_KEY_UP:
	    case GRUB_TERM_CTRL | 'p':
726
	    case '^':
727 728
	      if (current_entry > 0)
		current_entry--;
729 730 731
	      menu_set_chosen_entry (current_entry);
	      break;

732 733
	    case GRUB_TERM_CTRL | 'n':
	    case GRUB_TERM_KEY_DOWN:
734
	    case 'v':
735 736
	      if (current_entry < menu->size - 1)
		current_entry++;
737 738 739
	      menu_set_chosen_entry (current_entry);
	      break;

740 741
	    case GRUB_TERM_CTRL | 'g':
	    case GRUB_TERM_KEY_PPAGE:
742 743 744 745 746 747 748
	      if (current_entry < GRUB_MENU_PAGE_SIZE)
		current_entry = 0;
	      else
		current_entry -= GRUB_MENU_PAGE_SIZE;
	      menu_set_chosen_entry (current_entry);
	      break;

749 750
	    case GRUB_TERM_CTRL | 'c':
	    case GRUB_TERM_KEY_NPAGE:
751 752
	      if (current_entry + GRUB_MENU_PAGE_SIZE < menu->size)
		current_entry += GRUB_MENU_PAGE_SIZE;
753 754
	      else
		current_entry = menu->size - 1;
755 756 757 758 759
	      menu_set_chosen_entry (current_entry);
	      break;

	    case '\n':
	    case '\r':
760 761
	    case GRUB_TERM_KEY_RIGHT:
	    case GRUB_TERM_CTRL | 'f':
762 763 764 765 766 767 768 769 770 771 772 773 774
	      menu_fini ();
              *auto_boot = 0;
	      return current_entry;

	    case '\e':
	      if (nested)
		{
		  menu_fini ();
		  return -1;
		}
	      break;

	    case 'c':
775
	      menu_fini ();
776
	      grub_cmdline_run (1, 0);
777 778 779
	      goto refresh;

	    case 'e':
780
	      menu_fini ();
781 782 783 784 785 786 787 788
		{
		  grub_menu_entry_t e = grub_menu_get_entry (menu, current_entry);
		  if (e)
		    grub_menu_entry_run (e);
		}
	      goto refresh;

	    default:
789
	      {
790 791 792 793 794 795 796 797 798
		int entry;

		entry = get_entry_index_by_hotkey (menu, c);
		if (entry >= 0)
		  {
		    menu_fini ();
		    *auto_boot = 0;
		    return entry;
		  }
799
	      }
800 801 802 803 804 805 806 807 808 809 810 811 812 813
	      break;
	    }
	}
    }

  /* Never reach here.  */
}

/* Callback invoked immediately before a menu entry is executed.  */
static void
notify_booting (grub_menu_entry_t entry,
		void *userdata __attribute__((unused)))
{
  grub_printf ("  ");
814
  grub_printf_ (N_("Booting `%s'"), entry->title);
815 816 817 818 819 820 821 822 823
  grub_printf ("\n\n");
}

/* Callback invoked when a default menu entry executed because of a timeout
   has failed and an attempt will be made to execute the next fallback
   entry, ENTRY.  */
static void
notify_fallback (grub_menu_entry_t entry,
		 void *userdata __attribute__((unused)))
824
{
825
  grub_printf ("\n   ");
826
  grub_printf_ (N_("Falling back to `%s'"), entry->title);
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  grub_printf ("\n\n");
  grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS);
}

/* Callback invoked when a menu entry has failed and there is no remaining
   fallback entry to attempt.  */
static void
notify_execution_failure (void *userdata __attribute__((unused)))
{
  if (grub_errno != GRUB_ERR_NONE)
    {
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;
    }
  grub_printf ("\n  ");
842
  grub_printf_ (N_("Failed to boot both default and fallback entries.\n"));
843 844 845 846 847 848 849 850 851 852 853 854 855
  grub_wait_after_message ();
}

/* Callbacks used by the text menu to provide user feedback when menu entries
   are executed.  */
static struct grub_menu_execute_callback execution_callback =
{
  .notify_booting = notify_booting,
  .notify_fallback = notify_fallback,
  .notify_failure = notify_execution_failure
};

static grub_err_t
856
show_menu (grub_menu_t menu, int nested, int autobooted)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
{
  while (1)
    {
      int boot_entry;
      grub_menu_entry_t e;
      int auto_boot;

      boot_entry = run_menu (menu, nested, &auto_boot);
      if (boot_entry < 0)
	break;

      e = grub_menu_get_entry (menu, boot_entry);
      if (! e)
	continue; /* Menu is empty.  */

      grub_cls ();

      if (auto_boot)
875 876
	grub_menu_execute_with_fallback (menu, e, autobooted,
					 &execution_callback, 0);
877
      else
878 879 880
	grub_menu_execute_entry (e, 0);
      if (autobooted)
	break;
881 882 883 884 885 886
    }

  return GRUB_ERR_NONE;
}

grub_err_t
887
grub_show_menu (grub_menu_t menu, int nested, int autoboot)
888 889 890 891 892
{
  grub_err_t err1, err2;

  while (1)
    {
893 894
      err1 = show_menu (menu, nested, autoboot);
      autoboot = 0;
895 896
      grub_print_error ();

897 898 899
      if (grub_normal_exit_level)
	break;

900 901 902 903 904 905 906 907 908 909 910 911
      err2 = grub_auth_check_authentication (NULL);
      if (err2)
	{
	  grub_print_error ();
	  grub_errno = GRUB_ERR_NONE;
	  continue;
	}

      break;
    }

  return err1;
912
}