menu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. * (at your option) any later version.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/normal.h>
  20. #include <grub/misc.h>
  21. #include <grub/loader.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/env.h>
  25. #include <grub/menu_viewer.h>
  26. #include <grub/command.h>
  27. #include <grub/parser.h>
  28. #include <grub/auth.h>
  29. #include <grub/i18n.h>
  30. #include <grub/term.h>
  31. #include <grub/script_sh.h>
  32. #include <grub/gfxterm.h>
  33. #include <grub/dl.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
  37. grub_err_t (*grub_gfxmenu_try_hook) (int entry, grub_menu_t menu,
  38. int nested) = NULL;
  39. enum timeout_style {
  40. TIMEOUT_STYLE_MENU,
  41. TIMEOUT_STYLE_COUNTDOWN,
  42. TIMEOUT_STYLE_HIDDEN
  43. };
  44. struct timeout_style_name {
  45. const char *name;
  46. enum timeout_style style;
  47. } timeout_style_names[] = {
  48. {"menu", TIMEOUT_STYLE_MENU},
  49. {"countdown", TIMEOUT_STYLE_COUNTDOWN},
  50. {"hidden", TIMEOUT_STYLE_HIDDEN},
  51. {NULL, 0}
  52. };
  53. /* Wait until the user pushes any key so that the user
  54. can see what happened. */
  55. void
  56. grub_wait_after_message (void)
  57. {
  58. grub_uint64_t endtime;
  59. grub_xputs ("\n");
  60. grub_printf_ (N_("Press any key to continue..."));
  61. grub_refresh ();
  62. endtime = grub_get_time_ms () + 10000;
  63. while (grub_get_time_ms () < endtime
  64. && grub_getkey_noblock () == GRUB_TERM_NO_KEY);
  65. grub_xputs ("\n");
  66. }
  67. /* Get a menu entry by its index in the entry list. */
  68. grub_menu_entry_t
  69. grub_menu_get_entry (grub_menu_t menu, int no)
  70. {
  71. grub_menu_entry_t e;
  72. for (e = menu->entry_list; e && no > 0; e = e->next, no--)
  73. ;
  74. return e;
  75. }
  76. /* Get the index of a menu entry associated with a given hotkey, or -1. */
  77. static int
  78. get_entry_index_by_hotkey (grub_menu_t menu, int hotkey)
  79. {
  80. grub_menu_entry_t entry;
  81. int i;
  82. for (i = 0, entry = menu->entry_list; i < menu->size;
  83. i++, entry = entry->next)
  84. if (entry->hotkey == hotkey)
  85. return i;
  86. return -1;
  87. }
  88. /* Return the timeout style. If the variable "timeout_style" is not set or
  89. invalid, default to TIMEOUT_STYLE_MENU. */
  90. static enum timeout_style
  91. get_timeout_style (void)
  92. {
  93. const char *val;
  94. struct timeout_style_name *style_name;
  95. val = grub_env_get ("timeout_style");
  96. if (!val)
  97. return TIMEOUT_STYLE_MENU;
  98. for (style_name = timeout_style_names; style_name->name; style_name++)
  99. if (grub_strcmp (style_name->name, val) == 0)
  100. return style_name->style;
  101. return TIMEOUT_STYLE_MENU;
  102. }
  103. /* Return the current timeout. If the variable "timeout" is not set or
  104. invalid, return -1. */
  105. int
  106. grub_menu_get_timeout (void)
  107. {
  108. const char *val;
  109. int timeout;
  110. val = grub_env_get ("timeout");
  111. if (! val)
  112. return -1;
  113. grub_error_push ();
  114. timeout = (int) grub_strtoul (val, 0, 0);
  115. /* If the value is invalid, unset the variable. */
  116. if (grub_errno != GRUB_ERR_NONE)
  117. {
  118. grub_env_unset ("timeout");
  119. grub_errno = GRUB_ERR_NONE;
  120. timeout = -1;
  121. }
  122. grub_error_pop ();
  123. return timeout;
  124. }
  125. /* Set current timeout in the variable "timeout". */
  126. void
  127. grub_menu_set_timeout (int timeout)
  128. {
  129. /* Ignore TIMEOUT if it is zero, because it will be unset really soon. */
  130. if (timeout > 0)
  131. {
  132. char buf[16];
  133. grub_snprintf (buf, sizeof (buf), "%d", timeout);
  134. grub_env_set ("timeout", buf);
  135. }
  136. }
  137. /* Get the first entry number from the value of the environment variable NAME,
  138. which is a space-separated list of non-negative integers. The entry number
  139. which is returned is stripped from the value of NAME. If no entry number
  140. can be found, -1 is returned. */
  141. static int
  142. get_and_remove_first_entry_number (const char *name)
  143. {
  144. const char *val, *tail;
  145. int entry;
  146. val = grub_env_get (name);
  147. if (! val)
  148. return -1;
  149. grub_error_push ();
  150. entry = (int) grub_strtoul (val, &tail, 0);
  151. if (grub_errno == GRUB_ERR_NONE)
  152. {
  153. /* Skip whitespace to find the next digit. */
  154. while (*tail && grub_isspace (*tail))
  155. tail++;
  156. grub_env_set (name, tail);
  157. }
  158. else
  159. {
  160. grub_env_unset (name);
  161. grub_errno = GRUB_ERR_NONE;
  162. entry = -1;
  163. }
  164. grub_error_pop ();
  165. return entry;
  166. }
  167. /* Run a menu entry. */
  168. static void
  169. grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot)
  170. {
  171. grub_err_t err = GRUB_ERR_NONE;
  172. int errs_before;
  173. grub_menu_t menu = NULL;
  174. char *optr, *buf, *oldchosen = NULL, *olddefault = NULL;
  175. const char *ptr, *chosen, *def;
  176. grub_size_t sz = 0;
  177. if (entry->restricted)
  178. err = grub_auth_check_authentication (entry->users);
  179. if (err)
  180. {
  181. grub_print_error ();
  182. grub_errno = GRUB_ERR_NONE;
  183. return;
  184. }
  185. errs_before = grub_err_printed_errors;
  186. chosen = grub_env_get ("chosen");
  187. def = grub_env_get ("default");
  188. if (entry->submenu)
  189. {
  190. grub_env_context_open ();
  191. menu = grub_zalloc (sizeof (*menu));
  192. if (! menu)
  193. return;
  194. grub_env_set_menu (menu);
  195. if (auto_boot)
  196. grub_env_set ("timeout", "0");
  197. }
  198. for (ptr = entry->id; *ptr; ptr++)
  199. sz += (*ptr == '>') ? 2 : 1;
  200. if (chosen)
  201. {
  202. oldchosen = grub_strdup (chosen);
  203. if (!oldchosen)
  204. grub_print_error ();
  205. }
  206. if (def)
  207. {
  208. olddefault = grub_strdup (def);
  209. if (!olddefault)
  210. grub_print_error ();
  211. }
  212. sz++;
  213. if (chosen)
  214. sz += grub_strlen (chosen);
  215. sz++;
  216. buf = grub_malloc (sz);
  217. if (!buf)
  218. grub_print_error ();
  219. else
  220. {
  221. optr = buf;
  222. if (chosen)
  223. {
  224. optr = grub_stpcpy (optr, chosen);
  225. *optr++ = '>';
  226. }
  227. for (ptr = entry->id; *ptr; ptr++)
  228. {
  229. if (*ptr == '>')
  230. *optr++ = '>';
  231. *optr++ = *ptr;
  232. }
  233. *optr = 0;
  234. grub_env_set ("chosen", buf);
  235. grub_env_export ("chosen");
  236. grub_free (buf);
  237. }
  238. for (ptr = def; ptr && *ptr; ptr++)
  239. {
  240. if (ptr[0] == '>' && ptr[1] == '>')
  241. {
  242. ptr++;
  243. continue;
  244. }
  245. if (ptr[0] == '>')
  246. break;
  247. }
  248. if (ptr && ptr[0] && ptr[1])
  249. grub_env_set ("default", ptr + 1);
  250. else
  251. grub_env_unset ("default");
  252. grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args);
  253. if (errs_before != grub_err_printed_errors)
  254. grub_wait_after_message ();
  255. errs_before = grub_err_printed_errors;
  256. if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
  257. /* Implicit execution of boot, only if something is loaded. */
  258. grub_command_execute ("boot", 0, 0);
  259. if (errs_before != grub_err_printed_errors)
  260. grub_wait_after_message ();
  261. if (entry->submenu)
  262. {
  263. if (menu && menu->size)
  264. {
  265. grub_show_menu (menu, 1, auto_boot);
  266. grub_normal_free_menu (menu);
  267. }
  268. grub_env_context_close ();
  269. }
  270. if (oldchosen)
  271. grub_env_set ("chosen", oldchosen);
  272. else
  273. grub_env_unset ("chosen");
  274. if (olddefault)
  275. grub_env_set ("default", olddefault);
  276. else
  277. grub_env_unset ("default");
  278. grub_env_unset ("timeout");
  279. }
  280. /* Execute ENTRY from the menu MENU, falling back to entries specified
  281. in the environment variable "fallback" if it fails. CALLBACK is a
  282. pointer to a struct of function pointers which are used to allow the
  283. caller provide feedback to the user. */
  284. static void
  285. grub_menu_execute_with_fallback (grub_menu_t menu,
  286. grub_menu_entry_t entry,
  287. int autobooted,
  288. grub_menu_execute_callback_t callback,
  289. void *callback_data)
  290. {
  291. int fallback_entry;
  292. callback->notify_booting (entry, callback_data);
  293. grub_menu_execute_entry (entry, 1);
  294. /* Deal with fallback entries. */
  295. while ((fallback_entry = get_and_remove_first_entry_number ("fallback"))
  296. >= 0)
  297. {
  298. grub_print_error ();
  299. grub_errno = GRUB_ERR_NONE;
  300. entry = grub_menu_get_entry (menu, fallback_entry);
  301. callback->notify_fallback (entry, callback_data);
  302. grub_menu_execute_entry (entry, 1);
  303. /* If the function call to execute the entry returns at all, then this is
  304. taken to indicate a boot failure. For menu entries that do something
  305. other than actually boot an operating system, this could assume
  306. incorrectly that something failed. */
  307. }
  308. if (!autobooted)
  309. callback->notify_failure (callback_data);
  310. }
  311. static struct grub_menu_viewer *viewers;
  312. static void
  313. menu_set_chosen_entry (int entry)
  314. {
  315. struct grub_menu_viewer *cur;
  316. for (cur = viewers; cur; cur = cur->next)
  317. cur->set_chosen_entry (entry, cur->data);
  318. }
  319. static void
  320. menu_print_timeout (int timeout)
  321. {
  322. struct grub_menu_viewer *cur;
  323. for (cur = viewers; cur; cur = cur->next)
  324. cur->print_timeout (timeout, cur->data);
  325. }
  326. static void
  327. menu_fini (void)
  328. {
  329. struct grub_menu_viewer *cur, *next;
  330. for (cur = viewers; cur; cur = next)
  331. {
  332. next = cur->next;
  333. cur->fini (cur->data);
  334. grub_free (cur);
  335. }
  336. viewers = NULL;
  337. }
  338. static void
  339. menu_init (int entry, grub_menu_t menu, int nested)
  340. {
  341. struct grub_term_output *term;
  342. int gfxmenu = 0;
  343. FOR_ACTIVE_TERM_OUTPUTS(term)
  344. if (term->fullscreen)
  345. {
  346. if (grub_env_get ("theme"))
  347. {
  348. if (!grub_gfxmenu_try_hook)
  349. {
  350. grub_dl_load ("gfxmenu");
  351. grub_print_error ();
  352. }
  353. if (grub_gfxmenu_try_hook)
  354. {
  355. grub_err_t err;
  356. err = grub_gfxmenu_try_hook (entry, menu, nested);
  357. if(!err)
  358. {
  359. gfxmenu = 1;
  360. break;
  361. }
  362. }
  363. else
  364. grub_error (GRUB_ERR_BAD_MODULE,
  365. N_("module `%s' isn't loaded"),
  366. "gfxmenu");
  367. grub_print_error ();
  368. grub_wait_after_message ();
  369. }
  370. grub_errno = GRUB_ERR_NONE;
  371. term->fullscreen ();
  372. break;
  373. }
  374. FOR_ACTIVE_TERM_OUTPUTS(term)
  375. {
  376. grub_err_t err;
  377. if (grub_strcmp (term->name, "gfxterm") == 0 && gfxmenu)
  378. continue;
  379. err = grub_menu_try_text (term, entry, menu, nested);
  380. if(!err)
  381. continue;
  382. grub_print_error ();
  383. grub_errno = GRUB_ERR_NONE;
  384. }
  385. }
  386. static void
  387. clear_timeout (void)
  388. {
  389. struct grub_menu_viewer *cur;
  390. for (cur = viewers; cur; cur = cur->next)
  391. cur->clear_timeout (cur->data);
  392. }
  393. void
  394. grub_menu_register_viewer (struct grub_menu_viewer *viewer)
  395. {
  396. viewer->next = viewers;
  397. viewers = viewer;
  398. }
  399. static int
  400. menuentry_eq (const char *id, const char *spec)
  401. {
  402. const char *ptr1, *ptr2;
  403. ptr1 = id;
  404. ptr2 = spec;
  405. while (1)
  406. {
  407. if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
  408. return 1;
  409. if (*ptr2 == '>' && ptr2[1] != '>')
  410. return 0;
  411. if (*ptr2 == '>')
  412. ptr2++;
  413. if (*ptr1 != *ptr2)
  414. return 0;
  415. if (*ptr1 == 0)
  416. return 1;
  417. ptr1++;
  418. ptr2++;
  419. }
  420. }
  421. /* Get the entry number from the variable NAME. */
  422. static int
  423. get_entry_number (grub_menu_t menu, const char *name)
  424. {
  425. const char *val;
  426. int entry;
  427. val = grub_env_get (name);
  428. if (! val)
  429. return -1;
  430. grub_error_push ();
  431. entry = (int) grub_strtoul (val, 0, 0);
  432. if (grub_errno == GRUB_ERR_BAD_NUMBER)
  433. {
  434. /* See if the variable matches the title of a menu entry. */
  435. grub_menu_entry_t e = menu->entry_list;
  436. int i;
  437. grub_errno = GRUB_ERR_NONE;
  438. for (i = 0; e; i++)
  439. {
  440. if (menuentry_eq (e->title, val)
  441. || menuentry_eq (e->id, val))
  442. {
  443. entry = i;
  444. break;
  445. }
  446. e = e->next;
  447. }
  448. if (! e)
  449. entry = -1;
  450. }
  451. if (grub_errno != GRUB_ERR_NONE)
  452. {
  453. grub_errno = GRUB_ERR_NONE;
  454. entry = -1;
  455. }
  456. grub_error_pop ();
  457. return entry;
  458. }
  459. /* Check whether a second has elapsed since the last tick. If so, adjust
  460. the timer and return 1; otherwise, return 0. */
  461. static int
  462. has_second_elapsed (grub_uint64_t *saved_time)
  463. {
  464. grub_uint64_t current_time;
  465. current_time = grub_get_time_ms ();
  466. if (current_time - *saved_time >= 1000)
  467. {
  468. *saved_time = current_time;
  469. return 1;
  470. }
  471. else
  472. return 0;
  473. }
  474. static void
  475. print_countdown (struct grub_term_coordinate *pos, int n)
  476. {
  477. grub_term_restore_pos (pos);
  478. /* NOTE: Do not remove the trailing space characters.
  479. They are required to clear the line. */
  480. grub_printf ("%d ", n);
  481. grub_refresh ();
  482. }
  483. #define GRUB_MENU_PAGE_SIZE 10
  484. /* Show the menu and handle menu entry selection. Returns the menu entry
  485. index that should be executed or -1 if no entry should be executed (e.g.,
  486. Esc pressed to exit a sub-menu or switching menu viewers).
  487. If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
  488. entry to be executed is a result of an automatic default selection because
  489. of the timeout. */
  490. static int
  491. run_menu (grub_menu_t menu, int nested, int *auto_boot, int *notify_boot)
  492. {
  493. grub_uint64_t saved_time;
  494. int default_entry, current_entry;
  495. int timeout;
  496. enum timeout_style timeout_style;
  497. *notify_boot = 1;
  498. default_entry = get_entry_number (menu, "default");
  499. /* If DEFAULT_ENTRY is not within the menu entries, fall back to
  500. the first entry. */
  501. if (default_entry < 0 || default_entry >= menu->size)
  502. default_entry = 0;
  503. timeout = grub_menu_get_timeout ();
  504. if (timeout < 0)
  505. /* If there is no timeout, the "countdown" and "hidden" styles result in
  506. the system doing nothing and providing no or very little indication
  507. why. Technically this is what the user asked for, but it's not very
  508. useful and likely to be a source of confusion, so we disallow this. */
  509. grub_env_unset ("timeout_style");
  510. timeout_style = get_timeout_style ();
  511. if (timeout_style == TIMEOUT_STYLE_COUNTDOWN
  512. || timeout_style == TIMEOUT_STYLE_HIDDEN)
  513. {
  514. static struct grub_term_coordinate *pos;
  515. int entry = -1;
  516. if (timeout_style == TIMEOUT_STYLE_COUNTDOWN && timeout)
  517. {
  518. pos = grub_term_save_pos ();
  519. print_countdown (pos, timeout);
  520. }
  521. /* Enter interruptible sleep until Escape or a menu hotkey is pressed,
  522. or the timeout expires. */
  523. saved_time = grub_get_time_ms ();
  524. while (1)
  525. {
  526. int key;
  527. key = grub_getkey_noblock ();
  528. if (key != GRUB_TERM_NO_KEY)
  529. {
  530. entry = get_entry_index_by_hotkey (menu, key);
  531. if (entry >= 0)
  532. break;
  533. }
  534. if (grub_key_is_interrupt (key))
  535. {
  536. timeout = -1;
  537. break;
  538. }
  539. if (timeout > 0 && has_second_elapsed (&saved_time))
  540. {
  541. timeout--;
  542. if (timeout_style == TIMEOUT_STYLE_COUNTDOWN)
  543. print_countdown (pos, timeout);
  544. }
  545. if (timeout == 0)
  546. /* We will fall through to auto-booting the default entry. */
  547. break;
  548. }
  549. grub_env_unset ("timeout");
  550. grub_env_unset ("timeout_style");
  551. if (entry >= 0)
  552. {
  553. *auto_boot = 0;
  554. return entry;
  555. }
  556. }
  557. /* If timeout is 0, drawing is pointless (and ugly). */
  558. if (timeout == 0)
  559. {
  560. *auto_boot = 1;
  561. *notify_boot = timeout_style != TIMEOUT_STYLE_HIDDEN;
  562. return default_entry;
  563. }
  564. current_entry = default_entry;
  565. refresh:
  566. menu_init (current_entry, menu, nested);
  567. /* Initialize the time. */
  568. saved_time = grub_get_time_ms ();
  569. timeout = grub_menu_get_timeout ();
  570. if (timeout > 0)
  571. menu_print_timeout (timeout);
  572. else
  573. clear_timeout ();
  574. while (1)
  575. {
  576. int c;
  577. timeout = grub_menu_get_timeout ();
  578. if (grub_normal_exit_level)
  579. return -1;
  580. if (timeout > 0 && has_second_elapsed (&saved_time))
  581. {
  582. timeout--;
  583. grub_menu_set_timeout (timeout);
  584. menu_print_timeout (timeout);
  585. }
  586. if (timeout == 0)
  587. {
  588. grub_env_unset ("timeout");
  589. *auto_boot = 1;
  590. menu_fini ();
  591. return default_entry;
  592. }
  593. c = grub_getkey_noblock ();
  594. /* Negative values are returned on error. */
  595. if ((c != GRUB_TERM_NO_KEY) && (c > 0))
  596. {
  597. if (timeout >= 0)
  598. {
  599. grub_env_unset ("timeout");
  600. grub_env_unset ("fallback");
  601. clear_timeout ();
  602. }
  603. switch (c)
  604. {
  605. case GRUB_TERM_KEY_HOME:
  606. case GRUB_TERM_CTRL | 'a':
  607. current_entry = 0;
  608. menu_set_chosen_entry (current_entry);
  609. break;
  610. case GRUB_TERM_KEY_END:
  611. case GRUB_TERM_CTRL | 'e':
  612. current_entry = menu->size - 1;
  613. menu_set_chosen_entry (current_entry);
  614. break;
  615. case GRUB_TERM_KEY_UP:
  616. case GRUB_TERM_CTRL | 'p':
  617. case '^':
  618. if (current_entry > 0)
  619. current_entry--;
  620. menu_set_chosen_entry (current_entry);
  621. break;
  622. case GRUB_TERM_CTRL | 'n':
  623. case GRUB_TERM_KEY_DOWN:
  624. case 'v':
  625. if (current_entry < menu->size - 1)
  626. current_entry++;
  627. menu_set_chosen_entry (current_entry);
  628. break;
  629. case GRUB_TERM_CTRL | 'g':
  630. case GRUB_TERM_KEY_PPAGE:
  631. if (current_entry < GRUB_MENU_PAGE_SIZE)
  632. current_entry = 0;
  633. else
  634. current_entry -= GRUB_MENU_PAGE_SIZE;
  635. menu_set_chosen_entry (current_entry);
  636. break;
  637. case GRUB_TERM_CTRL | 'c':
  638. case GRUB_TERM_KEY_NPAGE:
  639. if (current_entry + GRUB_MENU_PAGE_SIZE < menu->size)
  640. current_entry += GRUB_MENU_PAGE_SIZE;
  641. else
  642. current_entry = menu->size - 1;
  643. menu_set_chosen_entry (current_entry);
  644. break;
  645. case '\n':
  646. case '\r':
  647. case GRUB_TERM_KEY_RIGHT:
  648. case GRUB_TERM_CTRL | 'f':
  649. menu_fini ();
  650. *auto_boot = 0;
  651. return current_entry;
  652. case GRUB_TERM_ESC:
  653. if (nested)
  654. {
  655. menu_fini ();
  656. return -1;
  657. }
  658. break;
  659. case 'c':
  660. menu_fini ();
  661. grub_cmdline_run (1, 0);
  662. goto refresh;
  663. case 'e':
  664. menu_fini ();
  665. {
  666. grub_menu_entry_t e = grub_menu_get_entry (menu, current_entry);
  667. if (e)
  668. grub_menu_entry_run (e);
  669. }
  670. goto refresh;
  671. case GRUB_TERM_CTRL | 'l':
  672. menu_fini ();
  673. goto refresh;
  674. default:
  675. {
  676. int entry;
  677. entry = get_entry_index_by_hotkey (menu, c);
  678. if (entry >= 0)
  679. {
  680. menu_fini ();
  681. *auto_boot = 0;
  682. return entry;
  683. }
  684. }
  685. break;
  686. }
  687. }
  688. }
  689. /* Never reach here. */
  690. }
  691. /* Callback invoked immediately before a menu entry is executed. */
  692. static void
  693. notify_booting (grub_menu_entry_t entry, void *userdata)
  694. {
  695. int *notify_boot = userdata;
  696. if (*notify_boot)
  697. {
  698. grub_printf (" ");
  699. grub_printf_ (N_("Booting `%s'"), entry->title);
  700. grub_printf ("\n\n");
  701. }
  702. }
  703. /* Callback invoked when a default menu entry executed because of a timeout
  704. has failed and an attempt will be made to execute the next fallback
  705. entry, ENTRY. */
  706. static void
  707. notify_fallback (grub_menu_entry_t entry,
  708. void *userdata __attribute__((unused)))
  709. {
  710. grub_printf ("\n ");
  711. grub_printf_ (N_("Falling back to `%s'"), entry->title);
  712. grub_printf ("\n\n");
  713. grub_millisleep (DEFAULT_ENTRY_ERROR_DELAY_MS);
  714. }
  715. /* Callback invoked when a menu entry has failed and there is no remaining
  716. fallback entry to attempt. */
  717. static void
  718. notify_execution_failure (void *userdata __attribute__((unused)))
  719. {
  720. if (grub_errno != GRUB_ERR_NONE)
  721. {
  722. grub_print_error ();
  723. grub_errno = GRUB_ERR_NONE;
  724. }
  725. grub_printf ("\n ");
  726. grub_printf_ (N_("Failed to boot both default and fallback entries.\n"));
  727. grub_wait_after_message ();
  728. }
  729. /* Callbacks used by the text menu to provide user feedback when menu entries
  730. are executed. */
  731. static struct grub_menu_execute_callback execution_callback =
  732. {
  733. .notify_booting = notify_booting,
  734. .notify_fallback = notify_fallback,
  735. .notify_failure = notify_execution_failure
  736. };
  737. static grub_err_t
  738. show_menu (grub_menu_t menu, int nested, int autobooted)
  739. {
  740. while (1)
  741. {
  742. int boot_entry;
  743. grub_menu_entry_t e;
  744. int auto_boot;
  745. int notify_boot;
  746. boot_entry = run_menu (menu, nested, &auto_boot, &notify_boot);
  747. if (boot_entry < 0)
  748. break;
  749. e = grub_menu_get_entry (menu, boot_entry);
  750. if (! e)
  751. continue; /* Menu is empty. */
  752. if (auto_boot)
  753. grub_menu_execute_with_fallback (menu, e, autobooted,
  754. &execution_callback, &notify_boot);
  755. else
  756. {
  757. grub_cls ();
  758. grub_menu_execute_entry (e, 0);
  759. }
  760. if (autobooted)
  761. break;
  762. }
  763. return GRUB_ERR_NONE;
  764. }
  765. grub_err_t
  766. grub_show_menu (grub_menu_t menu, int nested, int autoboot)
  767. {
  768. grub_err_t err1, err2;
  769. while (1)
  770. {
  771. err1 = show_menu (menu, nested, autoboot);
  772. autoboot = 0;
  773. grub_print_error ();
  774. if (grub_normal_exit_level)
  775. break;
  776. err2 = grub_auth_check_authentication (NULL);
  777. if (err2)
  778. {
  779. grub_print_error ();
  780. grub_errno = GRUB_ERR_NONE;
  781. continue;
  782. }
  783. break;
  784. }
  785. return err1;
  786. }