]> git.proxmox.com Git - grub2.git/blame - grub-core/normal/main.c
* grub-core/normal/menu.c (run_menu): Fix timeout reference point.
[grub2.git] / grub-core / normal / main.c
CommitLineData
ce5bf700 1/* main.c - the normal mode main routine */
2/*
4b13b216 3 * GRUB -- GRand Unified Bootloader
6fa42fa6 4 * Copyright (C) 2000,2001,2002,2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
ce5bf700 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
ce5bf700 7 * it under the terms of the GNU General Public License as published by
5a79f472 8 * the Free Software Foundation, either version 3 of the License, or
ce5bf700 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
ce5bf700 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
5a79f472 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
ce5bf700 18 */
19
4b13b216 20#include <grub/kernel.h>
21#include <grub/normal.h>
22#include <grub/dl.h>
4b13b216 23#include <grub/misc.h>
24#include <grub/file.h>
25#include <grub/mm.h>
26#include <grub/term.h>
27#include <grub/env.h>
77c4a393 28#include <grub/parser.h>
d558e6b5 29#include <grub/reader.h>
6fa42fa6 30#include <grub/menu_viewer.h>
e7e1f93f 31#include <grub/auth.h>
e2c37719 32#include <grub/i18n.h>
2e713831 33#include <grub/charset.h>
d56a6ac7 34#include <grub/script_sh.h>
ce5bf700 35
e745cf0c
VS
36GRUB_MOD_LICENSE ("GPLv3+");
37
4b13b216 38#define GRUB_DEFAULT_HISTORY_SIZE 50
5aded270 39
6066889c
VS
40static int nested_level = 0;
41int grub_normal_exit_level = 0;
42
ce5bf700 43/* Read a line from the file FILE. */
d05f0df3 44char *
45grub_file_getline (grub_file_t file)
ce5bf700 46{
47 char c;
9fdb2d7b 48 grub_size_t pos = 0;
2cff3677 49 char *cmdline;
9fdb2d7b
VS
50 int have_newline = 0;
51 grub_size_t max_len = 64;
2cff3677 52
53 /* Initially locate some space. */
54 cmdline = grub_malloc (max_len);
55 if (! cmdline)
56 return 0;
ce5bf700 57
58 while (1)
59 {
4b13b216 60 if (grub_file_read (file, &c, 1) != 1)
ce5bf700 61 break;
62
63 /* Skip all carriage returns. */
64 if (c == '\r')
65 continue;
66
ce5bf700 67
684aef11 68 if (pos + 1 >= max_len)
ce5bf700 69 {
9fdb2d7b
VS
70 char *old_cmdline = cmdline;
71 max_len = max_len * 2;
72 cmdline = grub_realloc (cmdline, max_len);
73 if (! cmdline)
ce5bf700 74 {
9fdb2d7b
VS
75 grub_free (old_cmdline);
76 return 0;
ce5bf700 77 }
ce5bf700 78 }
79
9fdb2d7b 80 if (c == '\n')
ce5bf700 81 {
9fdb2d7b
VS
82 have_newline = 1;
83 break;
ce5bf700 84 }
2cff3677 85
9fdb2d7b 86 cmdline[pos++] = c;
ce5bf700 87 }
88
89 cmdline[pos] = '\0';
2cff3677 90
91 /* If the buffer is empty, don't return anything at all. */
9fdb2d7b 92 if (pos == 0 && !have_newline)
2cff3677 93 {
94 grub_free (cmdline);
95 cmdline = 0;
96 }
b39f9d20 97
2cff3677 98 return cmdline;
ce5bf700 99}
100
fc55cc4c
VS
101void
102grub_normal_free_menu (grub_menu_t menu)
ce5bf700 103{
4b13b216 104 grub_menu_entry_t entry = menu->entry_list;
a8aa5762 105
ce5bf700 106 while (entry)
107 {
4b13b216 108 grub_menu_entry_t next_entry = entry->next;
5ad1be82 109 grub_size_t i;
ce5bf700 110
5ad1be82
AV
111 if (entry->classes)
112 {
113 struct grub_menu_entry_class *class;
114 for (class = entry->classes; class; class = class->next)
115 grub_free (class->name);
116 grub_free (entry->classes);
117 }
118
119 if (entry->args)
120 {
121 for (i = 0; entry->args[i]; i++)
122 grub_free (entry->args[i]);
123 grub_free (entry->args);
124 }
125
126 grub_free ((void *) entry->id);
127 grub_free ((void *) entry->users);
4b13b216 128 grub_free ((void *) entry->title);
77c4a393 129 grub_free ((void *) entry->sourcecode);
ce5bf700 130 entry = next_entry;
131 }
132
4b13b216 133 grub_free (menu);
2fbcbbc3 134 grub_env_unset_menu ();
ce5bf700 135}
136
09fd6d82
CW
137/* Helper for read_config_file. */
138static grub_err_t
139read_config_file_getline (char **line, int cont __attribute__ ((unused)),
140 void *data)
ce5bf700 141{
09fd6d82 142 grub_file_t file = data;
b39f9d20 143
09fd6d82 144 while (1)
77c4a393 145 {
09fd6d82 146 char *buf;
d558e6b5 147
09fd6d82
CW
148 *line = buf = grub_file_getline (file);
149 if (! buf)
150 return grub_errno;
d558e6b5 151
09fd6d82
CW
152 if (buf[0] == '#')
153 grub_free (*line);
154 else
155 break;
77c4a393 156 }
157
09fd6d82
CW
158 return GRUB_ERR_NONE;
159}
160
161static grub_menu_t
162read_config_file (const char *config)
163{
164 grub_file_t file;
165 const char *old_file, *old_dir;
166 char *config_dir, *ptr = 0;
167
77c4a393 168 grub_menu_t newmenu;
169
2fbcbbc3 170 newmenu = grub_env_get_menu ();
d558e6b5 171 if (! newmenu)
a8aa5762 172 {
eab58da2 173 newmenu = grub_zalloc (sizeof (*newmenu));
a8aa5762 174 if (! newmenu)
175 return 0;
d558e6b5 176
2fbcbbc3 177 grub_env_set_menu (newmenu);
a8aa5762 178 }
77c4a393 179
ce5bf700 180 /* Try to open the config file. */
4b13b216 181 file = grub_file_open (config);
ce5bf700 182 if (! file)
183 return 0;
184
40e80b94
VS
185 old_file = grub_env_get ("config_file");
186 old_dir = grub_env_get ("config_directory");
187 grub_env_set ("config_file", config);
188 config_dir = grub_strdup (config);
189 if (config_dir)
190 ptr = grub_strrchr (config_dir, '/');
191 if (ptr)
192 *ptr = 0;
193 grub_env_set ("config_directory", config_dir);
194
195 grub_env_export ("config_file");
196 grub_env_export ("config_directory");
197
f4c623e1
VS
198 while (1)
199 {
200 char *line;
201
202 /* Print an error, if any. */
203 grub_print_error ();
204 grub_errno = GRUB_ERR_NONE;
205
09fd6d82 206 if ((read_config_file_getline (&line, 0, file)) || (! line))
f4c623e1
VS
207 break;
208
09fd6d82 209 grub_normal_parse_line (line, read_config_file_getline, file);
f4c623e1
VS
210 grub_free (line);
211 }
212
40e80b94
VS
213 if (old_file)
214 grub_env_set ("config_file", old_file);
215 else
216 grub_env_unset ("config_file");
217 if (old_dir)
218 grub_env_set ("config_directory", old_dir);
219 else
220 grub_env_unset ("config_directory");
221
77c4a393 222 grub_file_close (file);
6de2ee99 223
65f201ad 224 return newmenu;
ce5bf700 225}
226
ce5bf700 227/* Initialize the screen. */
228void
f4c623e1 229grub_normal_init_page (struct grub_term_output *term)
ce5bf700 230{
8b282ad2 231 grub_ssize_t msg_len;
b99518d1 232 int posx;
233 const char *msg = _("GNU GRUB version %s");
8b442f3f 234 char *msg_formatted;
b99518d1 235 grub_uint32_t *unicode_msg;
236 grub_uint32_t *last_position;
a2c1332b 237
fa533ebb 238 grub_term_cls (term);
8b442f3f 239
61eb45ee 240 msg_formatted = grub_xasprintf (msg, PACKAGE_VERSION);
8b442f3f
VS
241 if (!msg_formatted)
242 return;
243
a2c1332b 244 msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
b99518d1 245 &unicode_msg, &last_position);
4a8a763c 246 grub_free (msg_formatted);
a2c1332b 247
b99518d1 248 if (msg_len < 0)
249 {
250 return;
251 }
b362c9e9 252
2e713831 253 posx = grub_getstringwidth (unicode_msg, last_position, term);
f4c623e1 254 posx = (grub_term_width (term) - posx) / 2;
2e713831 255 grub_term_gotoxy (term, posx, 1);
b362c9e9 256
f588f1c8 257 grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
701f1df9
VS
258 grub_putcode ('\n', term);
259 grub_putcode ('\n', term);
b99518d1 260 grub_free (unicode_msg);
ce5bf700 261}
262
027de555
VS
263static void
264read_lists (const char *val)
265{
a6393224 266 if (! grub_no_modules)
72539694
BC
267 {
268 read_command_list (val);
269 read_fs_list (val);
270 read_crypto_list (val);
271 read_terminal_list (val);
272 }
17f38c0f 273 grub_gettext_reread_prefix (val);
027de555
VS
274}
275
e880248e 276static char *
027de555
VS
277read_lists_hook (struct grub_env_var *var __attribute__ ((unused)),
278 const char *val)
e880248e 279{
027de555 280 read_lists (val);
e880248e
RM
281 return val ? grub_strdup (val) : NULL;
282}
39c9d41d 283
4241d2b1 284/* Read the config file CONFIG and execute the menu interface or
285 the command line interface if BATCH is false. */
ce5bf700 286void
f04f02e4 287grub_normal_execute (const char *config, int nested, int batch)
ce5bf700 288{
4b13b216 289 grub_menu_t menu = 0;
3fa06487 290 const char *prefix;
ce5bf700 291
3fa06487
CW
292 if (! nested)
293 {
294 prefix = grub_env_get ("prefix");
295 read_lists (prefix);
296 grub_register_variable_hook ("prefix", NULL, read_lists_hook);
3fa06487 297 }
d558e6b5 298
e744219b
VS
299 grub_boot_time ("Executing config file");
300
ce5bf700 301 if (config)
302 {
d558e6b5 303 menu = read_config_file (config);
ce5bf700 304
305 /* Ignore any error. */
4b13b216 306 grub_errno = GRUB_ERR_NONE;
ce5bf700 307 }
308
e744219b
VS
309 grub_boot_time ("Executed config file");
310
f04f02e4 311 if (! batch)
93f3a1d8 312 {
f04f02e4 313 if (menu && menu->size)
d558e6b5 314 {
e744219b
VS
315
316 grub_boot_time ("Entering menu");
dcb883b1 317 grub_show_menu (menu, nested, 0);
d558e6b5 318 if (nested)
fc55cc4c 319 grub_normal_free_menu (menu);
d558e6b5 320 }
93f3a1d8 321 }
ce5bf700 322}
323
d558e6b5 324/* This starts the normal mode. */
325void
326grub_enter_normal_mode (const char *config)
b1b797cb 327{
e744219b 328 grub_boot_time ("Entering normal mode");
6066889c 329 nested_level++;
d558e6b5 330 grub_normal_execute (config, 0, 0);
e744219b 331 grub_boot_time ("Entering shell");
6066889c
VS
332 grub_cmdline_run (0);
333 nested_level--;
334 if (grub_normal_exit_level)
335 grub_normal_exit_level--;
e744219b 336 grub_boot_time ("Exiting normal mode");
b1b797cb 337}
338
ce5bf700 339/* Enter normal mode from rescue mode. */
b1b797cb 340static grub_err_t
2e713831 341grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
b1b797cb 342 int argc, char *argv[])
ce5bf700 343{
344 if (argc == 0)
345 {
5f3607e0 346 /* Guess the config filename. It is necessary to make CONFIG static,
347 so that it won't get broken by longjmp. */
2e713831 348 char *config;
ce5bf700 349 const char *prefix;
b39f9d20 350
4b13b216 351 prefix = grub_env_get ("prefix");
ce5bf700 352 if (prefix)
353 {
61eb45ee 354 config = grub_xasprintf ("%s/grub.cfg", prefix);
ce5bf700 355 if (! config)
b1b797cb 356 goto quit;
ce5bf700 357
4b13b216 358 grub_enter_normal_mode (config);
359 grub_free (config);
ce5bf700 360 }
361 else
4b13b216 362 grub_enter_normal_mode (0);
ce5bf700 363 }
364 else
4b13b216 365 grub_enter_normal_mode (argv[0]);
b1b797cb 366
367quit:
b1b797cb 368 return 0;
ce5bf700 369}
370
6066889c 371/* Exit from normal mode to rescue mode. */
d558e6b5 372static grub_err_t
6066889c
VS
373grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
374 int argc __attribute__ ((unused)),
375 char *argv[] __attribute__ ((unused)))
d558e6b5 376{
6066889c
VS
377 if (nested_level <= grub_normal_exit_level)
378 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
379 grub_normal_exit_level++;
380 return GRUB_ERR_NONE;
d558e6b5 381}
382
383static grub_err_t
6066889c 384grub_normal_reader_init (int nested)
d558e6b5 385{
f4c623e1 386 struct grub_term_output *term;
7f39d92f 387 const char *msg = _("Minimal BASH-like line editing is supported. For "
388 "the first word, TAB lists possible command completions. Anywhere "
389 "else TAB lists possible device or file completions. %s");
7f39d92f 390 const char *msg_esc = _("ESC at any time exits.");
8b442f3f 391 char *msg_formatted;
7f39d92f 392
61eb45ee 393 msg_formatted = grub_xasprintf (msg, nested ? msg_esc : "");
8b442f3f
VS
394 if (!msg_formatted)
395 return grub_errno;
7f39d92f 396
3be7f8de
VS
397 FOR_ACTIVE_TERM_OUTPUTS(term)
398 {
399 grub_normal_init_page (term);
400 grub_term_setcursor (term, 1);
701f1df9 401
3be7f8de 402 grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
701f1df9
VS
403 grub_putcode ('\n', term);
404 grub_putcode ('\n', term);
3be7f8de 405 }
7f39d92f 406 grub_free (msg_formatted);
a2c1332b 407
d558e6b5 408 return 0;
409}
410
d558e6b5 411static grub_err_t
6066889c 412grub_normal_read_line_real (char **line, int cont, int nested)
d558e6b5 413{
d56a6ac7 414 const char *prompt;
d558e6b5 415
6066889c 416 if (cont)
a9e9dc7c
VS
417 /* TRANSLATORS: it's command line prompt. */
418 prompt = _(">");
6066889c 419 else
a9e9dc7c
VS
420 /* TRANSLATORS: it's command line prompt. */
421 prompt = _("grub>");
d558e6b5 422
b09a4a8d
VS
423 if (!prompt)
424 return grub_errno;
d558e6b5 425
426 while (1)
427 {
2e713831
VS
428 *line = grub_cmdline_get (prompt);
429 if (*line)
a9e9dc7c 430 return 0;
d558e6b5 431
6066889c 432 if (cont || nested)
d558e6b5 433 {
2e713831 434 grub_free (*line);
d558e6b5 435 *line = 0;
436 return grub_errno;
437 }
438 }
a9e9dc7c 439
d558e6b5 440}
441
6066889c 442static grub_err_t
09fd6d82
CW
443grub_normal_read_line (char **line, int cont,
444 void *data __attribute__ ((unused)))
6066889c
VS
445{
446 return grub_normal_read_line_real (line, cont, 0);
447}
448
f4c623e1
VS
449void
450grub_cmdline_run (int nested)
451{
452 grub_err_t err = GRUB_ERR_NONE;
453
454 err = grub_auth_check_authentication (NULL);
455
456 if (err)
457 {
458 grub_print_error ();
459 grub_errno = GRUB_ERR_NONE;
460 return;
461 }
462
6066889c 463 grub_normal_reader_init (nested);
f4c623e1
VS
464
465 while (1)
466 {
467 char *line;
468
6066889c
VS
469 if (grub_normal_exit_level)
470 break;
471
f4c623e1
VS
472 /* Print an error, if any. */
473 grub_print_error ();
474 grub_errno = GRUB_ERR_NONE;
475
6066889c 476 grub_normal_read_line_real (&line, 0, nested);
f4c623e1 477 if (! line)
2e713831 478 break;
f4c623e1 479
09fd6d82 480 grub_normal_parse_line (line, grub_normal_read_line, NULL);
f4c623e1
VS
481 grub_free (line);
482 }
483}
d558e6b5 484
485static char *
486grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
487 const char *val)
488{
489 grub_set_more ((*val == '1'));
490 return grub_strdup (val);
491}
492
919e37b0
VS
493/* clear */
494static grub_err_t
495grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
496 int argc __attribute__ ((unused)),
497 char *argv[] __attribute__ ((unused)))
498{
499 grub_cls ();
500 return 0;
501}
502
503static grub_command_t cmd_clear;
504
0a239a82 505static void (*grub_xputs_saved) (const char *str);
5bfb6e71 506static const char *features[] = {
274416e8 507 "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint",
d9bef9bc 508 "feature_default_font_path", "feature_all_video_module",
7cd0df84
VS
509 "feature_menuentry_id", "feature_menuentry_options", "feature_200_final",
510 "feature_nativedisk_cmd"
5bfb6e71 511};
0a239a82 512
6d099807 513GRUB_MOD_INIT(normal)
ce5bf700 514{
5bfb6e71
VS
515 unsigned i;
516
e744219b
VS
517 grub_boot_time ("Preparing normal module");
518
fc2ef117
VS
519 /* Previously many modules depended on gzio. Be nice to user and load it. */
520 grub_dl_load ("gzio");
377c98cb 521 grub_errno = 0;
fc2ef117 522
df895792 523 grub_normal_auth_init ();
2fbcbbc3 524 grub_context_init ();
20ebf732 525 grub_script_init ();
9284756e 526 grub_menu_init ();
2fbcbbc3 527
0a239a82
VS
528 grub_xputs_saved = grub_xputs;
529 grub_xputs = grub_xputs_normal;
530
ce5bf700 531 /* Normal mode shouldn't be unloaded. */
6d099807 532 if (mod)
533 grub_dl_ref (mod);
ce5bf700 534
919e37b0
VS
535 cmd_clear =
536 grub_register_command ("clear", grub_mini_cmd_clear,
537 0, N_("Clear the screen."));
538
4b13b216 539 grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
5aded270 540
d558e6b5 541 grub_register_variable_hook ("pager", 0, grub_env_write_pager);
537dc9be 542 grub_env_export ("pager");
d558e6b5 543
ce5bf700 544 /* Register a command "normal" for the rescue mode. */
2e713831 545 grub_register_command ("normal", grub_cmd_normal,
d8b5cd40 546 0, N_("Enter normal mode."));
6066889c 547 grub_register_command ("normal_exit", grub_cmd_normal_exit,
d8b5cd40 548 0, N_("Exit from normal mode."));
ce5bf700 549
0ece25b1 550 /* Reload terminal colors when these variables are written to. */
551 grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
552 grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);
553
554 /* Preserve hooks after context changes. */
555 grub_env_export ("color_normal");
556 grub_env_export ("color_highlight");
681440aa
BC
557
558 /* Set default color names. */
559 grub_env_set ("color_normal", "white/black");
560 grub_env_set ("color_highlight", "black/white");
b38a4983 561
5bfb6e71
VS
562 for (i = 0; i < ARRAY_SIZE (features); i++)
563 {
564 grub_env_set (features[i], "y");
565 grub_env_export (features[i]);
566 }
92cd0f6e
VS
567 grub_env_set ("grub_cpu", GRUB_TARGET_CPU);
568 grub_env_export ("grub_cpu");
569 grub_env_set ("grub_platform", GRUB_PLATFORM);
570 grub_env_export ("grub_platform");
e744219b
VS
571
572 grub_boot_time ("Normal module prepared");
ce5bf700 573}
574
6d099807 575GRUB_MOD_FINI(normal)
ce5bf700 576{
2fbcbbc3 577 grub_context_fini ();
20ebf732 578 grub_script_fini ();
9284756e 579 grub_menu_fini ();
df895792 580 grub_normal_auth_fini ();
2fbcbbc3 581
0a239a82
VS
582 grub_xputs = grub_xputs_saved;
583
4b13b216 584 grub_set_history (0);
d558e6b5 585 grub_register_variable_hook ("pager", 0, 0);
586 grub_fs_autoload_hook = 0;
919e37b0 587 grub_unregister_command (cmd_clear);
ce5bf700 588}