1
/* menu.c - General supporting functionality for menus. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
#include <grub/normal.h>
21
#include <grub/misc.h>
22
#include <grub/loader.h>
24
#include <grub/time.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>
34
/* Time to delay after displaying an error message about a default/fallback
35
entry failing to boot. */
36
#define DEFAULT_ENTRY_ERROR_DELAY_MS 2500
38
grub_err_t (*grub_gfxmenu_try_hook) (int entry, grub_menu_t menu,
41
/* Wait until the user pushes any key so that the user
42
can see what happened. */
44
grub_wait_after_message (void)
46
grub_uint64_t endtime;
48
grub_printf_ (N_("Press any key to continue..."));
51
endtime = grub_get_time_ms () + 10000;
53
while (grub_get_time_ms () < endtime)
54
if (grub_checkkey () >= 0)
63
/* Get a menu entry by its index in the entry list. */
65
grub_menu_get_entry (grub_menu_t menu, int no)
69
for (e = menu->entry_list; e && no > 0; e = e->next, no--)
75
/* Return the current timeout. If the variable "timeout" is not set or
76
invalid, return -1. */
78
grub_menu_get_timeout (void)
83
val = grub_env_get ("timeout");
89
timeout = (int) grub_strtoul (val, 0, 0);
91
/* If the value is invalid, unset the variable. */
92
if (grub_errno != GRUB_ERR_NONE)
94
grub_env_unset ("timeout");
95
grub_errno = GRUB_ERR_NONE;
104
/* Set current timeout in the variable "timeout". */
106
grub_menu_set_timeout (int timeout)
108
/* Ignore TIMEOUT if it is zero, because it will be unset really soon. */
113
grub_snprintf (buf, sizeof (buf), "%d", timeout);
114
grub_env_set ("timeout", buf);
118
/* Get the first entry number from the value of the environment variable NAME,
119
which is a space-separated list of non-negative integers. The entry number
120
which is returned is stripped from the value of NAME. If no entry number
121
can be found, -1 is returned. */
123
get_and_remove_first_entry_number (const char *name)
129
val = grub_env_get (name);
135
entry = (int) grub_strtoul (val, &tail, 0);
137
if (grub_errno == GRUB_ERR_NONE)
139
/* Skip whitespace to find the next digit. */
140
while (*tail && grub_isspace (*tail))
142
grub_env_set (name, tail);
146
grub_env_unset (name);
147
grub_errno = GRUB_ERR_NONE;
156
/* Run a menu entry. */
158
grub_menu_execute_entry(grub_menu_entry_t entry)
160
grub_err_t err = GRUB_ERR_NONE;
164
if (entry->restricted)
165
err = grub_auth_check_authentication (entry->users);
170
grub_errno = GRUB_ERR_NONE;
174
errs_before = grub_err_printed_errors;
178
grub_env_context_open ();
179
menu = grub_zalloc (sizeof (*menu));
182
grub_env_set_menu (menu);
185
grub_env_set ("chosen", entry->title);
186
grub_script_execute_sourcecode (entry->sourcecode, entry->argc, entry->args);
188
if (errs_before != grub_err_printed_errors)
189
grub_wait_after_message ();
191
if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
192
/* Implicit execution of boot, only if something is loaded. */
193
grub_command_execute ("boot", 0, 0);
197
if (menu && menu->size)
199
grub_show_menu (menu, 1);
200
grub_normal_free_menu (menu);
202
grub_env_context_close ();
206
/* Execute ENTRY from the menu MENU, falling back to entries specified
207
in the environment variable "fallback" if it fails. CALLBACK is a
208
pointer to a struct of function pointers which are used to allow the
209
caller provide feedback to the user. */
211
grub_menu_execute_with_fallback (grub_menu_t menu,
212
grub_menu_entry_t entry,
213
grub_menu_execute_callback_t callback,
218
callback->notify_booting (entry, callback_data);
220
grub_menu_execute_entry (entry);
222
/* Deal with fallback entries. */
223
while ((fallback_entry = get_and_remove_first_entry_number ("fallback"))
227
grub_errno = GRUB_ERR_NONE;
229
entry = grub_menu_get_entry (menu, fallback_entry);
230
callback->notify_fallback (entry, callback_data);
231
grub_menu_execute_entry (entry);
232
/* If the function call to execute the entry returns at all, then this is
233
taken to indicate a boot failure. For menu entries that do something
234
other than actually boot an operating system, this could assume
235
incorrectly that something failed. */
238
callback->notify_failure (callback_data);
241
static struct grub_menu_viewer *viewers;
244
menu_set_chosen_entry (int entry)
246
struct grub_menu_viewer *cur;
247
for (cur = viewers; cur; cur = cur->next)
248
cur->set_chosen_entry (entry, cur->data);
252
menu_print_timeout (int timeout)
254
struct grub_menu_viewer *cur;
255
for (cur = viewers; cur; cur = cur->next)
256
cur->print_timeout (timeout, cur->data);
262
struct grub_menu_viewer *cur, *next;
263
for (cur = viewers; cur; cur = next)
266
cur->fini (cur->data);
273
menu_init (int entry, grub_menu_t menu, int nested)
275
struct grub_term_output *term;
277
FOR_ACTIVE_TERM_OUTPUTS(term)
281
if (grub_gfxmenu_try_hook && grub_strcmp (term->name, "gfxterm") == 0)
283
err = grub_gfxmenu_try_hook (entry, menu, nested);
286
grub_errno = GRUB_ERR_NONE;
289
err = grub_menu_try_text (term, entry, menu, nested);
293
grub_errno = GRUB_ERR_NONE;
300
struct grub_menu_viewer *cur;
301
for (cur = viewers; cur; cur = cur->next)
302
cur->clear_timeout (cur->data);
306
grub_menu_register_viewer (struct grub_menu_viewer *viewer)
308
viewer->next = viewers;
312
/* Get the entry number from the variable NAME. */
314
get_entry_number (grub_menu_t menu, const char *name)
319
val = grub_env_get (name);
325
entry = (int) grub_strtoul (val, 0, 0);
327
if (grub_errno == GRUB_ERR_BAD_NUMBER)
329
/* See if the variable matches the title of a menu entry. */
330
grub_menu_entry_t e = menu->entry_list;
333
grub_errno = GRUB_ERR_NONE;
337
if (grub_strcmp (e->title, val) == 0)
349
if (grub_errno != GRUB_ERR_NONE)
351
grub_errno = GRUB_ERR_NONE;
360
#define GRUB_MENU_PAGE_SIZE 10
362
/* Show the menu and handle menu entry selection. Returns the menu entry
363
index that should be executed or -1 if no entry should be executed (e.g.,
364
Esc pressed to exit a sub-menu or switching menu viewers).
365
If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
366
entry to be executed is a result of an automatic default selection because
369
run_menu (grub_menu_t menu, int nested, int *auto_boot)
371
grub_uint64_t saved_time;
372
int default_entry, current_entry;
375
default_entry = get_entry_number (menu, "default");
377
/* If DEFAULT_ENTRY is not within the menu entries, fall back to
379
if (default_entry < 0 || default_entry >= menu->size)
382
/* If timeout is 0, drawing is pointless (and ugly). */
383
if (grub_menu_get_timeout () == 0)
386
return default_entry;
389
current_entry = default_entry;
391
/* Initialize the time. */
392
saved_time = grub_get_time_ms ();
395
menu_init (current_entry, menu, nested);
397
timeout = grub_menu_get_timeout ();
400
menu_print_timeout (timeout);
407
timeout = grub_menu_get_timeout ();
409
if (grub_normal_exit_level)
414
grub_uint64_t current_time;
416
current_time = grub_get_time_ms ();
417
if (current_time - saved_time >= 1000)
420
grub_menu_set_timeout (timeout);
421
saved_time = current_time;
422
menu_print_timeout (timeout);
428
grub_env_unset ("timeout");
431
return default_entry;
434
if (grub_checkkey () >= 0 || timeout < 0)
440
grub_env_unset ("timeout");
441
grub_env_unset ("fallback");
447
case GRUB_TERM_KEY_HOME:
448
case GRUB_TERM_CTRL | 'a':
450
menu_set_chosen_entry (current_entry);
453
case GRUB_TERM_KEY_END:
454
case GRUB_TERM_CTRL | 'e':
455
current_entry = menu->size - 1;
456
menu_set_chosen_entry (current_entry);
459
case GRUB_TERM_KEY_UP:
460
case GRUB_TERM_CTRL | 'p':
462
if (current_entry > 0)
464
menu_set_chosen_entry (current_entry);
467
case GRUB_TERM_CTRL | 'n':
468
case GRUB_TERM_KEY_DOWN:
470
if (current_entry < menu->size - 1)
472
menu_set_chosen_entry (current_entry);
475
case GRUB_TERM_CTRL | 'g':
476
case GRUB_TERM_KEY_PPAGE:
477
if (current_entry < GRUB_MENU_PAGE_SIZE)
480
current_entry -= GRUB_MENU_PAGE_SIZE;
481
menu_set_chosen_entry (current_entry);
484
case GRUB_TERM_CTRL | 'c':
485
case GRUB_TERM_KEY_NPAGE:
486
if (current_entry + GRUB_MENU_PAGE_SIZE < menu->size)
487
current_entry += GRUB_MENU_PAGE_SIZE;
489
current_entry = menu->size - 1;
490
menu_set_chosen_entry (current_entry);
495
case GRUB_TERM_KEY_RIGHT:
496
case GRUB_TERM_CTRL | 'f':
499
return current_entry;
511
grub_cmdline_run (1);
517
grub_menu_entry_t e = grub_menu_get_entry (menu, current_entry);
519
grub_menu_entry_run (e);
525
grub_menu_entry_t entry;
527
for (i = 0, entry = menu->entry_list; i < menu->size;
528
i++, entry = entry->next)
529
if (entry->hotkey == c)
541
/* Never reach here. */
545
/* Callback invoked immediately before a menu entry is executed. */
547
notify_booting (grub_menu_entry_t entry,
548
void *userdata __attribute__((unused)))
551
grub_printf_ (N_("Booting \'%s\'"), entry->title);
552
grub_printf ("\n\n");
555
/* Callback invoked when a default menu entry executed because of a timeout
556
has failed and an attempt will be made to execute the next fallback
559
notify_fallback (grub_menu_entry_t entry,
560
void *userdata __attribute__((unused)))
563
grub_printf_ (N_("Falling back to \'%s\'"), entry->title);
564
grub_printf ("\n\n");
565
grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS);
568
/* Callback invoked when a menu entry has failed and there is no remaining
569
fallback entry to attempt. */
571
notify_execution_failure (void *userdata __attribute__((unused)))
573
if (grub_errno != GRUB_ERR_NONE)
576
grub_errno = GRUB_ERR_NONE;
579
grub_printf_ (N_("Failed to boot both default and fallback entries.\n"));
580
grub_wait_after_message ();
583
/* Callbacks used by the text menu to provide user feedback when menu entries
585
static struct grub_menu_execute_callback execution_callback =
587
.notify_booting = notify_booting,
588
.notify_fallback = notify_fallback,
589
.notify_failure = notify_execution_failure
593
show_menu (grub_menu_t menu, int nested)
601
boot_entry = run_menu (menu, nested, &auto_boot);
605
e = grub_menu_get_entry (menu, boot_entry);
607
continue; /* Menu is empty. */
612
grub_menu_execute_with_fallback (menu, e, &execution_callback, 0);
614
grub_menu_execute_entry (e);
617
return GRUB_ERR_NONE;
621
grub_show_menu (grub_menu_t menu, int nested)
623
grub_err_t err1, err2;
627
err1 = show_menu (menu, nested);
630
if (grub_normal_exit_level)
633
err2 = grub_auth_check_authentication (NULL);
637
grub_errno = GRUB_ERR_NONE;