]> git.proxmox.com Git - grub2.git/blame - grub-core/normal/main.c
remove get_endpoint_descriptor and change all functions needing
[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{
72539694
BC
266 if (! grub_no_autoload)
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
ce5bf700 299 if (config)
300 {
d558e6b5 301 menu = read_config_file (config);
ce5bf700 302
303 /* Ignore any error. */
4b13b216 304 grub_errno = GRUB_ERR_NONE;
ce5bf700 305 }
306
f04f02e4 307 if (! batch)
93f3a1d8 308 {
f04f02e4 309 if (menu && menu->size)
d558e6b5 310 {
dcb883b1 311 grub_show_menu (menu, nested, 0);
d558e6b5 312 if (nested)
fc55cc4c 313 grub_normal_free_menu (menu);
d558e6b5 314 }
93f3a1d8 315 }
ce5bf700 316}
317
d558e6b5 318/* This starts the normal mode. */
319void
320grub_enter_normal_mode (const char *config)
b1b797cb 321{
6066889c 322 nested_level++;
d558e6b5 323 grub_normal_execute (config, 0, 0);
6066889c
VS
324 grub_cmdline_run (0);
325 nested_level--;
326 if (grub_normal_exit_level)
327 grub_normal_exit_level--;
b1b797cb 328}
329
ce5bf700 330/* Enter normal mode from rescue mode. */
b1b797cb 331static grub_err_t
2e713831 332grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
b1b797cb 333 int argc, char *argv[])
ce5bf700 334{
335 if (argc == 0)
336 {
5f3607e0 337 /* Guess the config filename. It is necessary to make CONFIG static,
338 so that it won't get broken by longjmp. */
2e713831 339 char *config;
ce5bf700 340 const char *prefix;
b39f9d20 341
4b13b216 342 prefix = grub_env_get ("prefix");
ce5bf700 343 if (prefix)
344 {
61eb45ee 345 config = grub_xasprintf ("%s/grub.cfg", prefix);
ce5bf700 346 if (! config)
b1b797cb 347 goto quit;
ce5bf700 348
4b13b216 349 grub_enter_normal_mode (config);
350 grub_free (config);
ce5bf700 351 }
352 else
4b13b216 353 grub_enter_normal_mode (0);
ce5bf700 354 }
355 else
4b13b216 356 grub_enter_normal_mode (argv[0]);
b1b797cb 357
358quit:
b1b797cb 359 return 0;
ce5bf700 360}
361
6066889c 362/* Exit from normal mode to rescue mode. */
d558e6b5 363static grub_err_t
6066889c
VS
364grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
365 int argc __attribute__ ((unused)),
366 char *argv[] __attribute__ ((unused)))
d558e6b5 367{
6066889c
VS
368 if (nested_level <= grub_normal_exit_level)
369 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
370 grub_normal_exit_level++;
371 return GRUB_ERR_NONE;
d558e6b5 372}
373
374static grub_err_t
6066889c 375grub_normal_reader_init (int nested)
d558e6b5 376{
f4c623e1 377 struct grub_term_output *term;
7f39d92f 378 const char *msg = _("Minimal BASH-like line editing is supported. For "
379 "the first word, TAB lists possible command completions. Anywhere "
380 "else TAB lists possible device or file completions. %s");
7f39d92f 381 const char *msg_esc = _("ESC at any time exits.");
8b442f3f 382 char *msg_formatted;
7f39d92f 383
61eb45ee 384 msg_formatted = grub_xasprintf (msg, nested ? msg_esc : "");
8b442f3f
VS
385 if (!msg_formatted)
386 return grub_errno;
7f39d92f 387
3be7f8de
VS
388 FOR_ACTIVE_TERM_OUTPUTS(term)
389 {
390 grub_normal_init_page (term);
391 grub_term_setcursor (term, 1);
701f1df9 392
3be7f8de 393 grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
701f1df9
VS
394 grub_putcode ('\n', term);
395 grub_putcode ('\n', term);
3be7f8de 396 }
7f39d92f 397 grub_free (msg_formatted);
a2c1332b 398
d558e6b5 399 return 0;
400}
401
d558e6b5 402static grub_err_t
6066889c 403grub_normal_read_line_real (char **line, int cont, int nested)
d558e6b5 404{
d56a6ac7 405 const char *prompt;
d558e6b5 406
6066889c 407 if (cont)
a9e9dc7c
VS
408 /* TRANSLATORS: it's command line prompt. */
409 prompt = _(">");
6066889c 410 else
a9e9dc7c
VS
411 /* TRANSLATORS: it's command line prompt. */
412 prompt = _("grub>");
d558e6b5 413
b09a4a8d
VS
414 if (!prompt)
415 return grub_errno;
d558e6b5 416
417 while (1)
418 {
2e713831
VS
419 *line = grub_cmdline_get (prompt);
420 if (*line)
a9e9dc7c 421 return 0;
d558e6b5 422
6066889c 423 if (cont || nested)
d558e6b5 424 {
2e713831 425 grub_free (*line);
d558e6b5 426 *line = 0;
427 return grub_errno;
428 }
429 }
a9e9dc7c 430
d558e6b5 431}
432
6066889c 433static grub_err_t
09fd6d82
CW
434grub_normal_read_line (char **line, int cont,
435 void *data __attribute__ ((unused)))
6066889c
VS
436{
437 return grub_normal_read_line_real (line, cont, 0);
438}
439
f4c623e1
VS
440void
441grub_cmdline_run (int nested)
442{
443 grub_err_t err = GRUB_ERR_NONE;
444
445 err = grub_auth_check_authentication (NULL);
446
447 if (err)
448 {
449 grub_print_error ();
450 grub_errno = GRUB_ERR_NONE;
451 return;
452 }
453
6066889c 454 grub_normal_reader_init (nested);
f4c623e1
VS
455
456 while (1)
457 {
458 char *line;
459
6066889c
VS
460 if (grub_normal_exit_level)
461 break;
462
f4c623e1
VS
463 /* Print an error, if any. */
464 grub_print_error ();
465 grub_errno = GRUB_ERR_NONE;
466
6066889c 467 grub_normal_read_line_real (&line, 0, nested);
f4c623e1 468 if (! line)
2e713831 469 break;
f4c623e1 470
09fd6d82 471 grub_normal_parse_line (line, grub_normal_read_line, NULL);
f4c623e1
VS
472 grub_free (line);
473 }
474}
d558e6b5 475
476static char *
477grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
478 const char *val)
479{
480 grub_set_more ((*val == '1'));
481 return grub_strdup (val);
482}
483
919e37b0
VS
484/* clear */
485static grub_err_t
486grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
487 int argc __attribute__ ((unused)),
488 char *argv[] __attribute__ ((unused)))
489{
490 grub_cls ();
491 return 0;
492}
493
494static grub_command_t cmd_clear;
495
0a239a82 496static void (*grub_xputs_saved) (const char *str);
5bfb6e71 497static const char *features[] = {
274416e8 498 "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint",
d9bef9bc 499 "feature_default_font_path", "feature_all_video_module",
5f91f701 500 "feature_menuentry_id", "feature_menuentry_options", "feature_200_final"
5bfb6e71 501};
0a239a82 502
6d099807 503GRUB_MOD_INIT(normal)
ce5bf700 504{
5bfb6e71
VS
505 unsigned i;
506
fc2ef117
VS
507 /* Previously many modules depended on gzio. Be nice to user and load it. */
508 grub_dl_load ("gzio");
377c98cb 509 grub_errno = 0;
fc2ef117 510
df895792 511 grub_normal_auth_init ();
2fbcbbc3 512 grub_context_init ();
20ebf732 513 grub_script_init ();
9284756e 514 grub_menu_init ();
2fbcbbc3 515
0a239a82
VS
516 grub_xputs_saved = grub_xputs;
517 grub_xputs = grub_xputs_normal;
518
ce5bf700 519 /* Normal mode shouldn't be unloaded. */
6d099807 520 if (mod)
521 grub_dl_ref (mod);
ce5bf700 522
919e37b0
VS
523 cmd_clear =
524 grub_register_command ("clear", grub_mini_cmd_clear,
525 0, N_("Clear the screen."));
526
4b13b216 527 grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
5aded270 528
d558e6b5 529 grub_register_variable_hook ("pager", 0, grub_env_write_pager);
537dc9be 530 grub_env_export ("pager");
d558e6b5 531
ce5bf700 532 /* Register a command "normal" for the rescue mode. */
2e713831 533 grub_register_command ("normal", grub_cmd_normal,
d8b5cd40 534 0, N_("Enter normal mode."));
6066889c 535 grub_register_command ("normal_exit", grub_cmd_normal_exit,
d8b5cd40 536 0, N_("Exit from normal mode."));
ce5bf700 537
0ece25b1 538 /* Reload terminal colors when these variables are written to. */
539 grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
540 grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);
541
542 /* Preserve hooks after context changes. */
543 grub_env_export ("color_normal");
544 grub_env_export ("color_highlight");
681440aa
BC
545
546 /* Set default color names. */
547 grub_env_set ("color_normal", "white/black");
548 grub_env_set ("color_highlight", "black/white");
b38a4983 549
5bfb6e71
VS
550 for (i = 0; i < ARRAY_SIZE (features); i++)
551 {
552 grub_env_set (features[i], "y");
553 grub_env_export (features[i]);
554 }
92cd0f6e
VS
555 grub_env_set ("grub_cpu", GRUB_TARGET_CPU);
556 grub_env_export ("grub_cpu");
557 grub_env_set ("grub_platform", GRUB_PLATFORM);
558 grub_env_export ("grub_platform");
ce5bf700 559}
560
6d099807 561GRUB_MOD_FINI(normal)
ce5bf700 562{
2fbcbbc3 563 grub_context_fini ();
20ebf732 564 grub_script_fini ();
9284756e 565 grub_menu_fini ();
df895792 566 grub_normal_auth_fini ();
2fbcbbc3 567
0a239a82
VS
568 grub_xputs = grub_xputs_saved;
569
4b13b216 570 grub_set_history (0);
d558e6b5 571 grub_register_variable_hook ("pager", 0, 0);
572 grub_fs_autoload_hook = 0;
919e37b0 573 grub_unregister_command (cmd_clear);
ce5bf700 574}