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)
47
grub_printf_ (N_("Press any key to continue..."));
48
(void) grub_getkey ();
52
/* Get a menu entry by its index in the entry list. */
54
grub_menu_get_entry (grub_menu_t menu, int no)
58
for (e = menu->entry_list; e && no > 0; e = e->next, no--)
64
/* Return the current timeout. If the variable "timeout" is not set or
65
invalid, return -1. */
67
grub_menu_get_timeout (void)
72
val = grub_env_get ("timeout");
78
timeout = (int) grub_strtoul (val, 0, 0);
80
/* If the value is invalid, unset the variable. */
81
if (grub_errno != GRUB_ERR_NONE)
83
grub_env_unset ("timeout");
84
grub_errno = GRUB_ERR_NONE;
93
/* Set current timeout in the variable "timeout". */
95
grub_menu_set_timeout (int timeout)
97
/* Ignore TIMEOUT if it is zero, because it will be unset really soon. */
102
grub_snprintf (buf, sizeof (buf), "%d", timeout);
103
grub_env_set ("timeout", buf);
107
/* Get the first entry number from the value of the environment variable NAME,
108
which is a space-separated list of non-negative integers. The entry number
109
which is returned is stripped from the value of NAME. If no entry number
110
can be found, -1 is returned. */
112
get_and_remove_first_entry_number (const char *name)
118
val = grub_env_get (name);
124
entry = (int) grub_strtoul (val, &tail, 0);
126
if (grub_errno == GRUB_ERR_NONE)
128
/* Skip whitespace to find the next digit. */
129
while (*tail && grub_isspace (*tail))
131
grub_env_set (name, tail);
135
grub_env_unset (name);
136
grub_errno = GRUB_ERR_NONE;
146
grub_menu_execute_entry_real (grub_menu_entry_t entry)
150
auto grub_err_t getline (char **line, int cont);
151
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
161
p = grub_strchr (source, '\n');
164
*line = grub_strndup (source, p - source);
166
*line = grub_strdup (source);
167
source = p ? p + 1 : 0;
171
source = entry->sourcecode;
178
grub_normal_parse_line (line, getline);
183
/* Run a menu entry. */
185
grub_menu_execute_entry(grub_menu_entry_t entry)
187
grub_err_t err = GRUB_ERR_NONE;
189
if (entry->restricted)
190
err = grub_auth_check_authentication (entry->users);
195
grub_errno = GRUB_ERR_NONE;
199
grub_env_set ("chosen", entry->title);
201
grub_menu_execute_entry_real (entry);
203
if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
204
/* Implicit execution of boot, only if something is loaded. */
205
grub_command_execute ("boot", 0, 0);
208
/* Execute ENTRY from the menu MENU, falling back to entries specified
209
in the environment variable "fallback" if it fails. CALLBACK is a
210
pointer to a struct of function pointers which are used to allow the
211
caller provide feedback to the user. */
213
grub_menu_execute_with_fallback (grub_menu_t menu,
214
grub_menu_entry_t entry,
215
grub_menu_execute_callback_t callback,
220
callback->notify_booting (entry, callback_data);
222
grub_menu_execute_entry (entry);
224
/* Deal with fallback entries. */
225
while ((fallback_entry = get_and_remove_first_entry_number ("fallback"))
229
grub_errno = GRUB_ERR_NONE;
231
entry = grub_menu_get_entry (menu, fallback_entry);
232
callback->notify_fallback (entry, callback_data);
233
grub_menu_execute_entry (entry);
234
/* If the function call to execute the entry returns at all, then this is
235
taken to indicate a boot failure. For menu entries that do something
236
other than actually boot an operating system, this could assume
237
incorrectly that something failed. */
240
callback->notify_failure (callback_data);
243
static struct grub_menu_viewer *viewers;
246
menu_set_chosen_entry (int entry)
248
struct grub_menu_viewer *cur;
249
for (cur = viewers; cur; cur = cur->next)
250
cur->set_chosen_entry (entry, cur->data);
254
menu_print_timeout (int timeout)
256
struct grub_menu_viewer *cur;
257
for (cur = viewers; cur; cur = cur->next)
258
cur->print_timeout (timeout, cur->data);
264
struct grub_menu_viewer *cur, *next;
265
for (cur = viewers; cur; cur = next)
268
cur->fini (cur->data);
275
menu_init (int entry, grub_menu_t menu, int nested)
277
struct grub_term_output *term;
279
FOR_ACTIVE_TERM_OUTPUTS(term)
283
if (grub_gfxmenu_try_hook && grub_strcmp (term->name, "gfxterm") == 0)
285
err = grub_gfxmenu_try_hook (entry, menu, nested);
288
grub_errno = GRUB_ERR_NONE;
291
err = grub_menu_try_text (term, entry, menu, nested);
295
grub_errno = GRUB_ERR_NONE;
302
struct grub_menu_viewer *cur;
303
for (cur = viewers; cur; cur = cur->next)
304
cur->clear_timeout (cur->data);
308
grub_menu_register_viewer (struct grub_menu_viewer *viewer)
310
viewer->next = viewers;
314
/* Get the entry number from the variable NAME. */
316
get_entry_number (grub_menu_t menu, const char *name)
321
val = grub_env_get (name);
327
entry = (int) grub_strtoul (val, 0, 0);
329
if (grub_errno == GRUB_ERR_BAD_NUMBER)
331
/* See if the variable matches the title of a menu entry. */
332
grub_menu_entry_t e = menu->entry_list;
335
grub_errno = GRUB_ERR_NONE;
339
if (grub_strcmp (e->title, val) == 0)
351
if (grub_errno != GRUB_ERR_NONE)
353
grub_errno = GRUB_ERR_NONE;
362
#define GRUB_MENU_PAGE_SIZE 10
364
/* Show the menu and handle menu entry selection. Returns the menu entry
365
index that should be executed or -1 if no entry should be executed (e.g.,
366
Esc pressed to exit a sub-menu or switching menu viewers).
367
If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
368
entry to be executed is a result of an automatic default selection because
371
run_menu (grub_menu_t menu, int nested, int *auto_boot)
373
grub_uint64_t saved_time;
374
int default_entry, current_entry;
377
default_entry = get_entry_number (menu, "default");
379
/* If DEFAULT_ENTRY is not within the menu entries, fall back to
381
if (default_entry < 0 || default_entry >= menu->size)
384
/* If timeout is 0, drawing is pointless (and ugly). */
385
if (grub_menu_get_timeout () == 0)
388
return default_entry;
391
current_entry = default_entry;
393
/* Initialize the time. */
394
saved_time = grub_get_time_ms ();
397
menu_init (current_entry, menu, nested);
399
timeout = grub_menu_get_timeout ();
402
menu_print_timeout (timeout);
409
timeout = grub_menu_get_timeout ();
411
if (grub_normal_exit_level)
416
grub_uint64_t current_time;
418
current_time = grub_get_time_ms ();
419
if (current_time - saved_time >= 1000)
422
grub_menu_set_timeout (timeout);
423
saved_time = current_time;
424
menu_print_timeout (timeout);
430
grub_env_unset ("timeout");
433
return default_entry;
436
if (grub_checkkey () >= 0 || timeout < 0)
438
c = GRUB_TERM_ASCII_CHAR (grub_getkey ());
442
grub_env_unset ("timeout");
443
grub_env_unset ("fallback");
451
menu_set_chosen_entry (current_entry);
455
current_entry = menu->size - 1;
456
menu_set_chosen_entry (current_entry);
461
if (current_entry > 0)
463
menu_set_chosen_entry (current_entry);
468
if (current_entry < menu->size - 1)
470
menu_set_chosen_entry (current_entry);
473
case GRUB_TERM_PPAGE:
474
if (current_entry < GRUB_MENU_PAGE_SIZE)
477
current_entry -= GRUB_MENU_PAGE_SIZE;
478
menu_set_chosen_entry (current_entry);
481
case GRUB_TERM_NPAGE:
482
if (current_entry + GRUB_MENU_PAGE_SIZE < menu->size)
483
current_entry += GRUB_MENU_PAGE_SIZE;
485
current_entry = menu->size - 1;
486
menu_set_chosen_entry (current_entry);
494
return current_entry;
506
grub_cmdline_run (1);
512
grub_menu_entry_t e = grub_menu_get_entry (menu, current_entry);
514
grub_menu_entry_run (e);
520
grub_menu_entry_t entry;
522
for (i = 0, entry = menu->entry_list; i < menu->size;
523
i++, entry = entry->next)
524
if (entry->hotkey == c)
536
/* Never reach here. */
540
/* Callback invoked immediately before a menu entry is executed. */
542
notify_booting (grub_menu_entry_t entry,
543
void *userdata __attribute__((unused)))
546
grub_printf_ (N_("Booting \'%s\'"), entry->title);
547
grub_printf ("\n\n");
550
/* Callback invoked when a default menu entry executed because of a timeout
551
has failed and an attempt will be made to execute the next fallback
554
notify_fallback (grub_menu_entry_t entry,
555
void *userdata __attribute__((unused)))
558
grub_printf_ (N_("Falling back to \'%s\'"), entry->title);
559
grub_printf ("\n\n");
560
grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS);
563
/* Callback invoked when a menu entry has failed and there is no remaining
564
fallback entry to attempt. */
566
notify_execution_failure (void *userdata __attribute__((unused)))
568
if (grub_errno != GRUB_ERR_NONE)
571
grub_errno = GRUB_ERR_NONE;
574
grub_printf_ (N_("Failed to boot both default and fallback entries.\n"));
575
grub_wait_after_message ();
578
/* Callbacks used by the text menu to provide user feedback when menu entries
580
static struct grub_menu_execute_callback execution_callback =
582
.notify_booting = notify_booting,
583
.notify_fallback = notify_fallback,
584
.notify_failure = notify_execution_failure
588
show_menu (grub_menu_t menu, int nested)
596
boot_entry = run_menu (menu, nested, &auto_boot);
600
e = grub_menu_get_entry (menu, boot_entry);
602
continue; /* Menu is empty. */
608
grub_menu_execute_with_fallback (menu, e, &execution_callback, 0);
612
int chars_before = grub_normal_get_char_counter ();
613
grub_errno = GRUB_ERR_NONE;
614
grub_menu_execute_entry (e);
616
grub_errno = GRUB_ERR_NONE;
618
if (chars_before != grub_normal_get_char_counter ())
619
grub_wait_after_message ();
623
return GRUB_ERR_NONE;
627
grub_show_menu (grub_menu_t menu, int nested)
629
grub_err_t err1, err2;
633
err1 = show_menu (menu, nested);
636
if (grub_normal_exit_level)
639
err2 = grub_auth_check_authentication (NULL);
643
grub_errno = GRUB_ERR_NONE;