]> git.proxmox.com Git - grub2.git/blame - grub-core/normal/main.c
* grub-core/font/font.c (grub_font_load): Add support for default
[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;
48 int pos = 0;
49 int literal = 0;
2cff3677 50 char *cmdline;
51 int max_len = 64;
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
67 /* Replace tabs with spaces. */
68 if (c == '\t')
69 c = ' ';
70
71 /* The previous is a backslash, then... */
72 if (literal)
73 {
74 /* If it is a newline, replace it with a space and continue. */
75 if (c == '\n')
76 {
77 c = ' ';
78
79 /* Go back to overwrite the backslash. */
80 if (pos > 0)
81 pos--;
82 }
83
84 literal = 0;
85 }
86
87 if (c == '\\')
88 literal = 1;
89
d558e6b5 90 if (pos == 0)
ce5bf700 91 {
d558e6b5 92 if (! grub_isspace (c))
ce5bf700 93 cmdline[pos++] = c;
94 }
95 else
96 {
2cff3677 97 if (pos >= max_len)
98 {
99 char *old_cmdline = cmdline;
100 max_len = max_len * 2;
101 cmdline = grub_realloc (cmdline, max_len);
102 if (! cmdline)
103 {
104 grub_free (old_cmdline);
105 return 0;
106 }
107 }
108
e93e4679 109 if (c == '\n')
110 break;
111
2cff3677 112 cmdline[pos++] = c;
ce5bf700 113 }
114 }
115
116 cmdline[pos] = '\0';
2cff3677 117
118 /* If the buffer is empty, don't return anything at all. */
119 if (pos == 0)
120 {
121 grub_free (cmdline);
122 cmdline = 0;
123 }
b39f9d20 124
2cff3677 125 return cmdline;
ce5bf700 126}
127
fc55cc4c
VS
128void
129grub_normal_free_menu (grub_menu_t menu)
ce5bf700 130{
4b13b216 131 grub_menu_entry_t entry = menu->entry_list;
a8aa5762 132
ce5bf700 133 while (entry)
134 {
4b13b216 135 grub_menu_entry_t next_entry = entry->next;
ce5bf700 136
4b13b216 137 grub_free ((void *) entry->title);
77c4a393 138 grub_free ((void *) entry->sourcecode);
ce5bf700 139 entry = next_entry;
140 }
141
4b13b216 142 grub_free (menu);
2fbcbbc3 143 grub_env_unset_menu ();
ce5bf700 144}
145
4b13b216 146static grub_menu_t
d558e6b5 147read_config_file (const char *config)
ce5bf700 148{
4b13b216 149 grub_file_t file;
b39f9d20 150
d558e6b5 151 auto grub_err_t getline (char **line, int cont);
152 grub_err_t getline (char **line, int cont __attribute__ ((unused)))
77c4a393 153 {
d558e6b5 154 while (1)
155 {
156 char *buf;
157
158 *line = buf = grub_file_getline (file);
159 if (! buf)
160 return grub_errno;
161
162 if (buf[0] == '#')
d56a6ac7 163 grub_free (*line);
d558e6b5 164 else
165 break;
166 }
77c4a393 167
168 return GRUB_ERR_NONE;
169 }
170
77c4a393 171 grub_menu_t newmenu;
172
2fbcbbc3 173 newmenu = grub_env_get_menu ();
d558e6b5 174 if (! newmenu)
a8aa5762 175 {
eab58da2 176 newmenu = grub_zalloc (sizeof (*newmenu));
a8aa5762 177 if (! newmenu)
178 return 0;
d558e6b5 179
2fbcbbc3 180 grub_env_set_menu (newmenu);
a8aa5762 181 }
77c4a393 182
ce5bf700 183 /* Try to open the config file. */
4b13b216 184 file = grub_file_open (config);
ce5bf700 185 if (! file)
186 return 0;
187
f4c623e1
VS
188 while (1)
189 {
190 char *line;
191
192 /* Print an error, if any. */
193 grub_print_error ();
194 grub_errno = GRUB_ERR_NONE;
195
196 if ((getline (&line, 0)) || (! line))
197 break;
198
d56a6ac7 199 grub_normal_parse_line (line, getline);
f4c623e1
VS
200 grub_free (line);
201 }
202
77c4a393 203 grub_file_close (file);
6de2ee99 204
65f201ad 205 return newmenu;
ce5bf700 206}
207
ce5bf700 208/* Initialize the screen. */
209void
f4c623e1 210grub_normal_init_page (struct grub_term_output *term)
ce5bf700 211{
b99518d1 212 int msg_len;
213 int posx;
214 const char *msg = _("GNU GRUB version %s");
8b442f3f 215 char *msg_formatted;
b99518d1 216 grub_uint32_t *unicode_msg;
217 grub_uint32_t *last_position;
a2c1332b 218
fa533ebb 219 grub_term_cls (term);
8b442f3f 220
61eb45ee 221 msg_formatted = grub_xasprintf (msg, PACKAGE_VERSION);
8b442f3f
VS
222 if (!msg_formatted)
223 return;
224
a2c1332b 225 msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
b99518d1 226 &unicode_msg, &last_position);
4a8a763c 227 grub_free (msg_formatted);
a2c1332b 228
b99518d1 229 if (msg_len < 0)
230 {
231 return;
232 }
b362c9e9 233
2e713831 234 posx = grub_getstringwidth (unicode_msg, last_position, term);
f4c623e1 235 posx = (grub_term_width (term) - posx) / 2;
2e713831 236 grub_term_gotoxy (term, posx, 1);
b362c9e9 237
f588f1c8 238 grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
701f1df9
VS
239 grub_putcode ('\n', term);
240 grub_putcode ('\n', term);
b99518d1 241 grub_free (unicode_msg);
ce5bf700 242}
243
027de555
VS
244static void
245read_lists (const char *val)
246{
72539694
BC
247 if (! grub_no_autoload)
248 {
249 read_command_list (val);
250 read_fs_list (val);
251 read_crypto_list (val);
252 read_terminal_list (val);
253 }
027de555
VS
254}
255
e880248e 256static char *
027de555
VS
257read_lists_hook (struct grub_env_var *var __attribute__ ((unused)),
258 const char *val)
e880248e 259{
027de555 260 read_lists (val);
e880248e
RM
261 return val ? grub_strdup (val) : NULL;
262}
39c9d41d 263
4241d2b1 264/* Read the config file CONFIG and execute the menu interface or
265 the command line interface if BATCH is false. */
ce5bf700 266void
f04f02e4 267grub_normal_execute (const char *config, int nested, int batch)
ce5bf700 268{
4b13b216 269 grub_menu_t menu = 0;
3fa06487 270 const char *prefix;
ce5bf700 271
3fa06487
CW
272 if (! nested)
273 {
274 prefix = grub_env_get ("prefix");
275 read_lists (prefix);
276 grub_register_variable_hook ("prefix", NULL, read_lists_hook);
3fa06487 277 }
d558e6b5 278
ce5bf700 279 if (config)
280 {
d558e6b5 281 menu = read_config_file (config);
ce5bf700 282
283 /* Ignore any error. */
4b13b216 284 grub_errno = GRUB_ERR_NONE;
ce5bf700 285 }
286
f04f02e4 287 if (! batch)
93f3a1d8 288 {
f04f02e4 289 if (menu && menu->size)
d558e6b5 290 {
dcb883b1 291 grub_show_menu (menu, nested, 0);
d558e6b5 292 if (nested)
fc55cc4c 293 grub_normal_free_menu (menu);
d558e6b5 294 }
93f3a1d8 295 }
ce5bf700 296}
297
d558e6b5 298/* This starts the normal mode. */
299void
300grub_enter_normal_mode (const char *config)
b1b797cb 301{
6066889c 302 nested_level++;
d558e6b5 303 grub_normal_execute (config, 0, 0);
6066889c
VS
304 grub_cmdline_run (0);
305 nested_level--;
306 if (grub_normal_exit_level)
307 grub_normal_exit_level--;
b1b797cb 308}
309
ce5bf700 310/* Enter normal mode from rescue mode. */
b1b797cb 311static grub_err_t
2e713831 312grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
b1b797cb 313 int argc, char *argv[])
ce5bf700 314{
315 if (argc == 0)
316 {
5f3607e0 317 /* Guess the config filename. It is necessary to make CONFIG static,
318 so that it won't get broken by longjmp. */
2e713831 319 char *config;
ce5bf700 320 const char *prefix;
b39f9d20 321
4b13b216 322 prefix = grub_env_get ("prefix");
ce5bf700 323 if (prefix)
324 {
61eb45ee 325 config = grub_xasprintf ("%s/grub.cfg", prefix);
ce5bf700 326 if (! config)
b1b797cb 327 goto quit;
ce5bf700 328
4b13b216 329 grub_enter_normal_mode (config);
330 grub_free (config);
ce5bf700 331 }
332 else
4b13b216 333 grub_enter_normal_mode (0);
ce5bf700 334 }
335 else
4b13b216 336 grub_enter_normal_mode (argv[0]);
b1b797cb 337
338quit:
b1b797cb 339 return 0;
ce5bf700 340}
341
6066889c 342/* Exit from normal mode to rescue mode. */
d558e6b5 343static grub_err_t
6066889c
VS
344grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
345 int argc __attribute__ ((unused)),
346 char *argv[] __attribute__ ((unused)))
d558e6b5 347{
6066889c
VS
348 if (nested_level <= grub_normal_exit_level)
349 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
350 grub_normal_exit_level++;
351 return GRUB_ERR_NONE;
d558e6b5 352}
353
354static grub_err_t
6066889c 355grub_normal_reader_init (int nested)
d558e6b5 356{
f4c623e1 357 struct grub_term_output *term;
7f39d92f 358 const char *msg = _("Minimal BASH-like line editing is supported. For "
359 "the first word, TAB lists possible command completions. Anywhere "
360 "else TAB lists possible device or file completions. %s");
7f39d92f 361 const char *msg_esc = _("ESC at any time exits.");
8b442f3f 362 char *msg_formatted;
7f39d92f 363
61eb45ee 364 msg_formatted = grub_xasprintf (msg, nested ? msg_esc : "");
8b442f3f
VS
365 if (!msg_formatted)
366 return grub_errno;
7f39d92f 367
3be7f8de
VS
368 FOR_ACTIVE_TERM_OUTPUTS(term)
369 {
370 grub_normal_init_page (term);
371 grub_term_setcursor (term, 1);
701f1df9 372
3be7f8de 373 grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
701f1df9
VS
374 grub_putcode ('\n', term);
375 grub_putcode ('\n', term);
3be7f8de 376 }
7f39d92f 377 grub_free (msg_formatted);
a2c1332b 378
d558e6b5 379 return 0;
380}
381
d558e6b5 382static grub_err_t
6066889c 383grub_normal_read_line_real (char **line, int cont, int nested)
d558e6b5 384{
d56a6ac7 385 const char *prompt;
d558e6b5 386
6066889c 387 if (cont)
a9e9dc7c
VS
388 /* TRANSLATORS: it's command line prompt. */
389 prompt = _(">");
6066889c 390 else
a9e9dc7c
VS
391 /* TRANSLATORS: it's command line prompt. */
392 prompt = _("grub>");
d558e6b5 393
b09a4a8d
VS
394 if (!prompt)
395 return grub_errno;
d558e6b5 396
397 while (1)
398 {
2e713831
VS
399 *line = grub_cmdline_get (prompt);
400 if (*line)
a9e9dc7c 401 return 0;
d558e6b5 402
6066889c 403 if (cont || nested)
d558e6b5 404 {
2e713831 405 grub_free (*line);
d558e6b5 406 *line = 0;
407 return grub_errno;
408 }
409 }
a9e9dc7c 410
d558e6b5 411}
412
6066889c
VS
413static grub_err_t
414grub_normal_read_line (char **line, int cont)
415{
416 return grub_normal_read_line_real (line, cont, 0);
417}
418
f4c623e1
VS
419void
420grub_cmdline_run (int nested)
421{
422 grub_err_t err = GRUB_ERR_NONE;
423
424 err = grub_auth_check_authentication (NULL);
425
426 if (err)
427 {
428 grub_print_error ();
429 grub_errno = GRUB_ERR_NONE;
430 return;
431 }
432
6066889c 433 grub_normal_reader_init (nested);
f4c623e1
VS
434
435 while (1)
436 {
437 char *line;
438
6066889c
VS
439 if (grub_normal_exit_level)
440 break;
441
f4c623e1
VS
442 /* Print an error, if any. */
443 grub_print_error ();
444 grub_errno = GRUB_ERR_NONE;
445
6066889c 446 grub_normal_read_line_real (&line, 0, nested);
f4c623e1 447 if (! line)
2e713831 448 break;
f4c623e1 449
d56a6ac7 450 grub_normal_parse_line (line, grub_normal_read_line);
f4c623e1
VS
451 grub_free (line);
452 }
453}
d558e6b5 454
455static char *
456grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
457 const char *val)
458{
459 grub_set_more ((*val == '1'));
460 return grub_strdup (val);
461}
462
919e37b0
VS
463/* clear */
464static grub_err_t
465grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
466 int argc __attribute__ ((unused)),
467 char *argv[] __attribute__ ((unused)))
468{
469 grub_cls ();
470 return 0;
471}
472
473static grub_command_t cmd_clear;
474
0a239a82 475static void (*grub_xputs_saved) (const char *str);
5bfb6e71 476static const char *features[] = {
274416e8
VS
477 "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint",
478 "feature_default_font_path"
5bfb6e71 479};
0a239a82 480
6d099807 481GRUB_MOD_INIT(normal)
ce5bf700 482{
5bfb6e71
VS
483 unsigned i;
484
fc2ef117
VS
485 /* Previously many modules depended on gzio. Be nice to user and load it. */
486 grub_dl_load ("gzio");
487
df895792 488 grub_normal_auth_init ();
2fbcbbc3 489 grub_context_init ();
20ebf732 490 grub_script_init ();
9284756e 491 grub_menu_init ();
2fbcbbc3 492
0a239a82
VS
493 grub_xputs_saved = grub_xputs;
494 grub_xputs = grub_xputs_normal;
495
ce5bf700 496 /* Normal mode shouldn't be unloaded. */
6d099807 497 if (mod)
498 grub_dl_ref (mod);
ce5bf700 499
919e37b0
VS
500 cmd_clear =
501 grub_register_command ("clear", grub_mini_cmd_clear,
502 0, N_("Clear the screen."));
503
4b13b216 504 grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
5aded270 505
d558e6b5 506 grub_register_variable_hook ("pager", 0, grub_env_write_pager);
537dc9be 507 grub_env_export ("pager");
d558e6b5 508
ce5bf700 509 /* Register a command "normal" for the rescue mode. */
2e713831 510 grub_register_command ("normal", grub_cmd_normal,
d8b5cd40 511 0, N_("Enter normal mode."));
6066889c 512 grub_register_command ("normal_exit", grub_cmd_normal_exit,
d8b5cd40 513 0, N_("Exit from normal mode."));
ce5bf700 514
0ece25b1 515 /* Reload terminal colors when these variables are written to. */
516 grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
517 grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);
518
519 /* Preserve hooks after context changes. */
520 grub_env_export ("color_normal");
521 grub_env_export ("color_highlight");
681440aa
BC
522
523 /* Set default color names. */
524 grub_env_set ("color_normal", "white/black");
525 grub_env_set ("color_highlight", "black/white");
b38a4983 526
5bfb6e71
VS
527 for (i = 0; i < ARRAY_SIZE (features); i++)
528 {
529 grub_env_set (features[i], "y");
530 grub_env_export (features[i]);
531 }
92cd0f6e
VS
532 grub_env_set ("grub_cpu", GRUB_TARGET_CPU);
533 grub_env_export ("grub_cpu");
534 grub_env_set ("grub_platform", GRUB_PLATFORM);
535 grub_env_export ("grub_platform");
ce5bf700 536}
537
6d099807 538GRUB_MOD_FINI(normal)
ce5bf700 539{
2fbcbbc3 540 grub_context_fini ();
20ebf732 541 grub_script_fini ();
9284756e 542 grub_menu_fini ();
df895792 543 grub_normal_auth_fini ();
2fbcbbc3 544
0a239a82
VS
545 grub_xputs = grub_xputs_saved;
546
4b13b216 547 grub_set_history (0);
d558e6b5 548 grub_register_variable_hook ("pager", 0, 0);
549 grub_fs_autoload_hook = 0;
919e37b0 550 grub_unregister_command (cmd_clear);
ce5bf700 551}