make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / normal / menu.c
blob1d4d4ec7359da083baa61720515f172546569642
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/normal.h>
20 #include <grub/term.h>
21 #include <grub/misc.h>
22 #include <grub/loader.h>
23 #include <grub/mm.h>
24 #include <grub/machine/time.h>
25 #include <grub/env.h>
26 #include <grub/script.h>
28 static grub_uint8_t grub_color_menu_normal;
29 static grub_uint8_t grub_color_menu_highlight;
31 /* Wait until the user pushes any key so that the user
32 can see what happened. */
33 void
34 grub_wait_after_message (void)
36 grub_printf ("\nPress any key to continue...");
37 (void) grub_getkey ();
40 static void
41 draw_border (void)
43 unsigned i;
45 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
47 grub_gotoxy (GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y);
48 grub_putcode (GRUB_TERM_DISP_UL);
49 for (i = 0; i < (unsigned) GRUB_TERM_BORDER_WIDTH - 2; i++)
50 grub_putcode (GRUB_TERM_DISP_HLINE);
51 grub_putcode (GRUB_TERM_DISP_UR);
53 for (i = 0; i < (unsigned) GRUB_TERM_NUM_ENTRIES; i++)
55 grub_gotoxy (GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y + i + 1);
56 grub_putcode (GRUB_TERM_DISP_VLINE);
57 grub_gotoxy (GRUB_TERM_MARGIN + GRUB_TERM_BORDER_WIDTH - 1,
58 GRUB_TERM_TOP_BORDER_Y + i + 1);
59 grub_putcode (GRUB_TERM_DISP_VLINE);
62 grub_gotoxy (GRUB_TERM_MARGIN,
63 GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES + 1);
64 grub_putcode (GRUB_TERM_DISP_LL);
65 for (i = 0; i < (unsigned) GRUB_TERM_BORDER_WIDTH - 2; i++)
66 grub_putcode (GRUB_TERM_DISP_HLINE);
67 grub_putcode (GRUB_TERM_DISP_LR);
69 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
71 grub_gotoxy (GRUB_TERM_MARGIN,
72 (GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES
73 + GRUB_TERM_MARGIN + 1));
76 static void
77 print_message (int nested, int edit)
79 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
81 if (edit)
83 grub_printf ("\n\
84 Minimum Emacs-like screen editing is supported. TAB lists\n\
85 available completions. Press C-x (\'x\' with Ctrl) to boot,\n\
86 C-c (\'c\' with Ctrl) for a command-line or ESC to return menu.");
88 else
90 grub_printf ("\n\
91 Use the %C and %C keys to select which entry is highlighted.\n",
92 (grub_uint32_t) GRUB_TERM_DISP_UP, (grub_uint32_t) GRUB_TERM_DISP_DOWN);
93 grub_printf ("\
94 Press enter to boot the selected OS, \'e\' to edit the\n\
95 commands before booting or \'c\' for a command-line.");
96 if (nested)
97 grub_printf ("\n\
98 ESC to return previous menu.");
103 static grub_menu_entry_t
104 get_entry (grub_menu_t menu, int no)
106 grub_menu_entry_t e;
108 for (e = menu->entry_list; e && no > 0; e = e->next, no--)
111 return e;
114 static void
115 print_entry (int y, int highlight, grub_menu_entry_t entry)
117 int x;
118 const char *title;
119 grub_ssize_t len;
120 grub_uint32_t *unicode_title;
121 grub_ssize_t i;
122 grub_uint8_t old_color_normal, old_color_highlight;
124 title = entry ? entry->title : "";
125 unicode_title = grub_malloc (grub_strlen (title) * sizeof (*unicode_title));
126 if (! unicode_title)
127 /* XXX How to show this error? */
128 return;
130 len = grub_utf8_to_ucs4 (unicode_title, (grub_uint8_t *) title,
131 grub_strlen (title));
132 if (len < 0)
134 /* It is an invalid sequence. */
135 grub_free (unicode_title);
136 return;
139 grub_getcolor (&old_color_normal, &old_color_highlight);
140 grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
141 grub_setcolorstate (highlight
142 ? GRUB_TERM_COLOR_HIGHLIGHT
143 : GRUB_TERM_COLOR_NORMAL);
145 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN, y);
147 for (x = GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1, i = 0;
148 x < GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH - GRUB_TERM_MARGIN;
149 i++)
151 if (i < len
152 && x <= (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH
153 - GRUB_TERM_MARGIN - 1))
155 grub_ssize_t width;
157 width = grub_getcharwidth (unicode_title[i]);
159 if (x + width > (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH
160 - GRUB_TERM_MARGIN - 1))
161 grub_putcode (GRUB_TERM_DISP_RIGHT);
162 else
163 grub_putcode (unicode_title[i]);
165 x += width;
167 else
169 grub_putchar (' ');
170 x++;
173 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
174 grub_putchar (' ');
176 grub_gotoxy (GRUB_TERM_CURSOR_X, y);
178 grub_setcolor (old_color_normal, old_color_highlight);
179 grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
180 grub_free (unicode_title);
183 static void
184 print_entries (grub_menu_t menu, int first, int offset)
186 grub_menu_entry_t e;
187 int i;
189 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
190 GRUB_TERM_FIRST_ENTRY_Y);
192 if (first)
193 grub_putcode (GRUB_TERM_DISP_UP);
194 else
195 grub_putchar (' ');
197 e = get_entry (menu, first);
199 for (i = 0; i < GRUB_TERM_NUM_ENTRIES; i++)
201 print_entry (GRUB_TERM_FIRST_ENTRY_Y + i, offset == i, e);
202 if (e)
203 e = e->next;
206 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
207 GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES);
209 if (e)
210 grub_putcode (GRUB_TERM_DISP_DOWN);
211 else
212 grub_putchar (' ');
214 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
217 /* Initialize the screen. If NESTED is non-zero, assume that this menu
218 is run from another menu or a command-line. If EDIT is non-zero, show
219 a message for the menu entry editor. */
220 void
221 grub_menu_init_page (int nested, int edit)
223 grub_uint8_t old_color_normal, old_color_highlight;
225 grub_getcolor (&old_color_normal, &old_color_highlight);
227 /* By default, use the same colors for the menu. */
228 grub_color_menu_normal = old_color_normal;
229 grub_color_menu_highlight = old_color_highlight;
231 /* Then give user a chance to replace them. */
232 grub_parse_color_name_pair (&grub_color_menu_normal, grub_env_get ("menu_color_normal"));
233 grub_parse_color_name_pair (&grub_color_menu_highlight, grub_env_get ("menu_color_highlight"));
235 grub_normal_init_page ();
236 grub_setcolor (grub_color_menu_normal, grub_color_menu_highlight);
237 draw_border ();
238 grub_setcolor (old_color_normal, old_color_highlight);
239 print_message (nested, edit);
242 /* Return the current timeout. If the variable "timeout" is not set or
243 invalid, return -1. */
244 static int
245 get_timeout (void)
247 char *val;
248 int timeout;
250 val = grub_env_get ("timeout");
251 if (! val)
252 return -1;
254 grub_error_push ();
256 timeout = (int) grub_strtoul (val, 0, 0);
258 /* If the value is invalid, unset the variable. */
259 if (grub_errno != GRUB_ERR_NONE)
261 grub_env_unset ("timeout");
262 grub_errno = GRUB_ERR_NONE;
263 timeout = -1;
266 grub_error_pop ();
268 return timeout;
271 /* Set current timeout in the variable "timeout". */
272 static void
273 set_timeout (int timeout)
275 /* Ignore TIMEOUT if it is zero, because it will be unset really soon. */
276 if (timeout > 0)
278 char buf[16];
280 grub_sprintf (buf, "%d", timeout);
281 grub_env_set ("timeout", buf);
285 /* Get the entry number from the variable NAME. */
286 static int
287 get_entry_number (const char *name)
289 char *val;
290 int entry;
292 val = grub_env_get (name);
293 if (! val)
294 return -1;
296 grub_error_push ();
298 entry = (int) grub_strtoul (val, 0, 0);
300 if (grub_errno != GRUB_ERR_NONE)
302 grub_errno = GRUB_ERR_NONE;
303 entry = -1;
306 grub_error_pop ();
308 return entry;
311 static void
312 print_timeout (int timeout, int offset, int second_stage)
314 /* NOTE: Do not remove the trailing space characters.
315 They are required to clear the line. */
316 char *msg = " The highlighted entry will be booted automatically in %ds. ";
317 char *msg_end = grub_strchr (msg, '%');
319 grub_gotoxy (second_stage ? (msg_end - msg) : 0, GRUB_TERM_HEIGHT - 3);
320 grub_printf (second_stage ? msg_end : msg, timeout);
321 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
322 grub_refresh ();
325 static int
326 run_menu (grub_menu_t menu, int nested)
328 int first, offset;
329 unsigned long saved_time;
330 int default_entry;
331 int timeout;
333 first = 0;
335 default_entry = get_entry_number ("default");
337 /* If DEFAULT_ENTRY is not within the menu entries, fall back to
338 the first entry. */
339 if (default_entry < 0 || default_entry >= menu->size)
340 default_entry = 0;
342 /* If timeout is 0, drawing is pointless (and ugly). */
343 if (get_timeout () == 0)
344 return default_entry;
346 offset = default_entry;
347 if (offset > GRUB_TERM_NUM_ENTRIES - 1)
349 first = offset - (GRUB_TERM_NUM_ENTRIES - 1);
350 offset = GRUB_TERM_NUM_ENTRIES - 1;
353 /* Initialize the time. */
354 saved_time = grub_get_rtc ();
356 refresh:
357 grub_setcursor (0);
358 grub_menu_init_page (nested, 0);
359 print_entries (menu, first, offset);
360 grub_refresh ();
362 timeout = get_timeout ();
364 if (timeout > 0)
365 print_timeout (timeout, offset, 0);
367 while (1)
369 int c;
370 timeout = get_timeout ();
372 if (timeout > 0)
374 unsigned long current_time;
376 current_time = grub_get_rtc ();
377 if (current_time - saved_time >= GRUB_TICKS_PER_SECOND)
379 timeout--;
380 set_timeout (timeout);
381 saved_time = current_time;
382 print_timeout (timeout, offset, 1);
386 if (timeout == 0)
388 grub_env_unset ("timeout");
389 return default_entry;
392 if (grub_checkkey () >= 0 || timeout < 0)
394 c = GRUB_TERM_ASCII_CHAR (grub_getkey ());
396 if (timeout >= 0)
398 grub_gotoxy (0, GRUB_TERM_HEIGHT - 3);
399 grub_printf ("\
401 grub_env_unset ("timeout");
402 grub_env_unset ("fallback");
403 grub_gotoxy (GRUB_TERM_CURSOR_X, GRUB_TERM_FIRST_ENTRY_Y + offset);
406 switch (c)
408 case 16:
409 case '^':
410 if (offset > 0)
412 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 0,
413 get_entry (menu, first + offset));
414 offset--;
415 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 1,
416 get_entry (menu, first + offset));
418 else if (first > 0)
420 first--;
421 print_entries (menu, first, offset);
423 break;
425 case 14:
426 case 'v':
427 if (menu->size > first + offset + 1)
429 if (offset < GRUB_TERM_NUM_ENTRIES - 1)
431 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 0,
432 get_entry (menu, first + offset));
433 offset++;
434 print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 1,
435 get_entry (menu, first + offset));
437 else
439 first++;
440 print_entries (menu, first, offset);
443 break;
445 case '\n':
446 case '\r':
447 case 6:
448 grub_setcursor (1);
449 return first + offset;
451 case '\e':
452 if (nested)
454 grub_setcursor (1);
455 return -1;
457 break;
459 case 'c':
460 grub_cmdline_run (1);
461 goto refresh;
463 case 'e':
465 grub_menu_entry_t e = get_entry (menu, first + offset);
466 if (e)
467 grub_menu_entry_run (e);
469 goto refresh;
471 default:
472 break;
475 grub_refresh ();
479 /* Never reach here. */
480 return -1;
483 /* Run a menu entry. */
484 static void
485 run_menu_entry (grub_menu_entry_t entry)
487 grub_script_execute (entry->commands);
489 if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
490 /* Implicit execution of boot, only if something is loaded. */
491 grub_command_execute ("boot", 0);
494 void
495 grub_menu_run (grub_menu_t menu, int nested)
497 while (1)
499 int boot_entry;
500 grub_menu_entry_t e;
501 int fallback_entry;
503 boot_entry = run_menu (menu, nested);
504 if (boot_entry < 0)
505 break;
507 e = get_entry (menu, boot_entry);
508 if (! e)
509 continue; /* Menu is empty. */
511 grub_cls ();
512 grub_setcursor (1);
514 grub_printf (" Booting \'%s\'\n\n", e->title);
516 run_menu_entry (e);
518 /* Deal with a fallback entry. */
519 /* FIXME: Multiple fallback entries like GRUB Legacy. */
520 fallback_entry = get_entry_number ("fallback");
521 if (fallback_entry >= 0)
523 grub_print_error ();
524 grub_errno = GRUB_ERR_NONE;
526 e = get_entry (menu, fallback_entry);
527 grub_env_unset ("fallback");
528 grub_printf ("\n Falling back to \'%s\'\n\n", e->title);
529 run_menu_entry (e);
532 if (grub_errno != GRUB_ERR_NONE)
534 grub_print_error ();
535 grub_errno = GRUB_ERR_NONE;
537 grub_wait_after_message ();