]> git.proxmox.com Git - grub2.git/blob - grub-core/normal/main.c
707917013dc1f4206444145443e0571517dcf7b3
[grub2.git] / grub-core / normal / main.c
1 /* main.c - the normal mode main routine */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2000,2001,2002,2003,2005,2006,2007,2008,2009 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
20 #include <grub/kernel.h>
21 #include <grub/normal.h>
22 #include <grub/dl.h>
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>
28 #include <grub/parser.h>
29 #include <grub/reader.h>
30 #include <grub/menu_viewer.h>
31 #include <grub/auth.h>
32 #include <grub/i18n.h>
33 #include <grub/charset.h>
34 #include <grub/script_sh.h>
35
36 GRUB_MOD_LICENSE ("GPLv3+");
37
38 #define GRUB_DEFAULT_HISTORY_SIZE 50
39
40 static int nested_level = 0;
41 int grub_normal_exit_level = 0;
42
43 /* Read a line from the file FILE. */
44 char *
45 grub_file_getline (grub_file_t file)
46 {
47 char c;
48 int pos = 0;
49 int literal = 0;
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;
57
58 while (1)
59 {
60 if (grub_file_read (file, &c, 1) != 1)
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
90 if (pos == 0)
91 {
92 if (! grub_isspace (c))
93 cmdline[pos++] = c;
94 }
95 else
96 {
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
109 if (c == '\n')
110 break;
111
112 cmdline[pos++] = c;
113 }
114 }
115
116 cmdline[pos] = '\0';
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 }
124
125 return cmdline;
126 }
127
128 void
129 grub_normal_free_menu (grub_menu_t menu)
130 {
131 grub_menu_entry_t entry = menu->entry_list;
132
133 while (entry)
134 {
135 grub_menu_entry_t next_entry = entry->next;
136
137 grub_free ((void *) entry->title);
138 grub_free ((void *) entry->sourcecode);
139 entry = next_entry;
140 }
141
142 grub_free (menu);
143 grub_env_unset_menu ();
144 }
145
146 static grub_menu_t
147 read_config_file (const char *config)
148 {
149 grub_file_t file;
150
151 auto grub_err_t getline (char **line, int cont);
152 grub_err_t getline (char **line, int cont __attribute__ ((unused)))
153 {
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] == '#')
163 grub_free (*line);
164 else
165 break;
166 }
167
168 return GRUB_ERR_NONE;
169 }
170
171 grub_menu_t newmenu;
172
173 newmenu = grub_env_get_menu ();
174 if (! newmenu)
175 {
176 newmenu = grub_zalloc (sizeof (*newmenu));
177 if (! newmenu)
178 return 0;
179
180 grub_env_set_menu (newmenu);
181 }
182
183 /* Try to open the config file. */
184 file = grub_file_open (config);
185 if (! file)
186 return 0;
187
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
199 grub_normal_parse_line (line, getline);
200 grub_free (line);
201 }
202
203 grub_file_close (file);
204
205 return newmenu;
206 }
207
208 /* Initialize the screen. */
209 void
210 grub_normal_init_page (struct grub_term_output *term)
211 {
212 int msg_len;
213 int posx;
214 const char *msg = _("GNU GRUB version %s");
215 char *msg_formatted;
216 grub_uint32_t *unicode_msg;
217 grub_uint32_t *last_position;
218
219 grub_term_cls (term);
220
221 msg_formatted = grub_xasprintf (msg, PACKAGE_VERSION);
222 if (!msg_formatted)
223 return;
224
225 msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
226 &unicode_msg, &last_position);
227 grub_free (msg_formatted);
228
229 if (msg_len < 0)
230 {
231 return;
232 }
233
234 posx = grub_getstringwidth (unicode_msg, last_position, term);
235 posx = (grub_term_width (term) - posx) / 2;
236 grub_term_gotoxy (term, posx, 1);
237
238 grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
239 grub_putcode ('\n', term);
240 grub_putcode ('\n', term);
241 grub_free (unicode_msg);
242 }
243
244 static void
245 read_lists (const char *val)
246 {
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 }
254 }
255
256 static char *
257 read_lists_hook (struct grub_env_var *var __attribute__ ((unused)),
258 const char *val)
259 {
260 read_lists (val);
261 return val ? grub_strdup (val) : NULL;
262 }
263
264 /* Read the config file CONFIG and execute the menu interface or
265 the command line interface if BATCH is false. */
266 void
267 grub_normal_execute (const char *config, int nested, int batch)
268 {
269 grub_menu_t menu = 0;
270 const char *prefix;
271
272 if (! nested)
273 {
274 prefix = grub_env_get ("prefix");
275 read_lists (prefix);
276 grub_register_variable_hook ("prefix", NULL, read_lists_hook);
277 }
278
279 if (config)
280 {
281 menu = read_config_file (config);
282
283 /* Ignore any error. */
284 grub_errno = GRUB_ERR_NONE;
285 }
286
287 if (! batch)
288 {
289 if (menu && menu->size)
290 {
291 grub_show_menu (menu, nested, 0);
292 if (nested)
293 grub_normal_free_menu (menu);
294 }
295 }
296 }
297
298 /* This starts the normal mode. */
299 void
300 grub_enter_normal_mode (const char *config)
301 {
302 nested_level++;
303 grub_normal_execute (config, 0, 0);
304 grub_cmdline_run (0);
305 nested_level--;
306 if (grub_normal_exit_level)
307 grub_normal_exit_level--;
308 }
309
310 /* Enter normal mode from rescue mode. */
311 static grub_err_t
312 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
313 int argc, char *argv[])
314 {
315 if (argc == 0)
316 {
317 /* Guess the config filename. It is necessary to make CONFIG static,
318 so that it won't get broken by longjmp. */
319 char *config;
320 const char *prefix;
321
322 prefix = grub_env_get ("prefix");
323 if (prefix)
324 {
325 config = grub_xasprintf ("%s/grub.cfg", prefix);
326 if (! config)
327 goto quit;
328
329 grub_enter_normal_mode (config);
330 grub_free (config);
331 }
332 else
333 grub_enter_normal_mode (0);
334 }
335 else
336 grub_enter_normal_mode (argv[0]);
337
338 quit:
339 return 0;
340 }
341
342 /* Exit from normal mode to rescue mode. */
343 static grub_err_t
344 grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
345 int argc __attribute__ ((unused)),
346 char *argv[] __attribute__ ((unused)))
347 {
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;
352 }
353
354 static grub_err_t
355 grub_normal_reader_init (int nested)
356 {
357 struct grub_term_output *term;
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");
361 const char *msg_esc = _("ESC at any time exits.");
362 char *msg_formatted;
363
364 msg_formatted = grub_xasprintf (msg, nested ? msg_esc : "");
365 if (!msg_formatted)
366 return grub_errno;
367
368 FOR_ACTIVE_TERM_OUTPUTS(term)
369 {
370 grub_normal_init_page (term);
371 grub_term_setcursor (term, 1);
372
373 grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
374 grub_putcode ('\n', term);
375 grub_putcode ('\n', term);
376 }
377 grub_free (msg_formatted);
378
379 return 0;
380 }
381
382 static grub_err_t
383 grub_normal_read_line_real (char **line, int cont, int nested)
384 {
385 const char *prompt;
386
387 if (cont)
388 /* TRANSLATORS: it's command line prompt. */
389 prompt = _(">");
390 else
391 /* TRANSLATORS: it's command line prompt. */
392 prompt = _("grub>");
393
394 if (!prompt)
395 return grub_errno;
396
397 while (1)
398 {
399 *line = grub_cmdline_get (prompt);
400 if (*line)
401 return 0;
402
403 if (cont || nested)
404 {
405 grub_free (*line);
406 *line = 0;
407 return grub_errno;
408 }
409 }
410
411 }
412
413 static grub_err_t
414 grub_normal_read_line (char **line, int cont)
415 {
416 return grub_normal_read_line_real (line, cont, 0);
417 }
418
419 void
420 grub_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
433 grub_normal_reader_init (nested);
434
435 while (1)
436 {
437 char *line;
438
439 if (grub_normal_exit_level)
440 break;
441
442 /* Print an error, if any. */
443 grub_print_error ();
444 grub_errno = GRUB_ERR_NONE;
445
446 grub_normal_read_line_real (&line, 0, nested);
447 if (! line)
448 break;
449
450 grub_normal_parse_line (line, grub_normal_read_line);
451 grub_free (line);
452 }
453 }
454
455 static char *
456 grub_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
463 /* clear */
464 static grub_err_t
465 grub_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
473 static grub_command_t cmd_clear;
474
475 static void (*grub_xputs_saved) (const char *str);
476 static const char *features[] = {
477 "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint"
478 };
479
480 GRUB_MOD_INIT(normal)
481 {
482 unsigned i;
483
484 /* Previously many modules depended on gzio. Be nice to user and load it. */
485 grub_dl_load ("gzio");
486
487 grub_normal_auth_init ();
488 grub_context_init ();
489 grub_script_init ();
490 grub_menu_init ();
491
492 grub_xputs_saved = grub_xputs;
493 grub_xputs = grub_xputs_normal;
494
495 /* Normal mode shouldn't be unloaded. */
496 if (mod)
497 grub_dl_ref (mod);
498
499 cmd_clear =
500 grub_register_command ("clear", grub_mini_cmd_clear,
501 0, N_("Clear the screen."));
502
503 grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
504
505 grub_register_variable_hook ("pager", 0, grub_env_write_pager);
506 grub_env_export ("pager");
507
508 /* Register a command "normal" for the rescue mode. */
509 grub_register_command ("normal", grub_cmd_normal,
510 0, N_("Enter normal mode."));
511 grub_register_command ("normal_exit", grub_cmd_normal_exit,
512 0, N_("Exit from normal mode."));
513
514 /* Reload terminal colors when these variables are written to. */
515 grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
516 grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);
517
518 /* Preserve hooks after context changes. */
519 grub_env_export ("color_normal");
520 grub_env_export ("color_highlight");
521
522 /* Set default color names. */
523 grub_env_set ("color_normal", "white/black");
524 grub_env_set ("color_highlight", "black/white");
525
526 for (i = 0; i < ARRAY_SIZE (features); i++)
527 {
528 grub_env_set (features[i], "y");
529 grub_env_export (features[i]);
530 }
531 grub_env_set ("grub_cpu", GRUB_TARGET_CPU);
532 grub_env_export ("grub_cpu");
533 grub_env_set ("grub_platform", GRUB_PLATFORM);
534 grub_env_export ("grub_platform");
535 }
536
537 GRUB_MOD_FINI(normal)
538 {
539 grub_context_fini ();
540 grub_script_fini ();
541 grub_menu_fini ();
542 grub_normal_auth_fini ();
543
544 grub_xputs = grub_xputs_saved;
545
546 grub_set_history (0);
547 grub_register_variable_hook ("pager", 0, 0);
548 grub_fs_autoload_hook = 0;
549 grub_unregister_command (cmd_clear);
550 }