]> git.proxmox.com Git - grub2.git/blob - normal/main.c
2009-12-08 Carles Pina i Estany <carles@pina.cat>
[grub2.git] / 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
34 #define GRUB_DEFAULT_HISTORY_SIZE 50
35
36 /* Read a line from the file FILE. */
37 char *
38 grub_file_getline (grub_file_t file)
39 {
40 char c;
41 int pos = 0;
42 int literal = 0;
43 char *cmdline;
44 int max_len = 64;
45
46 /* Initially locate some space. */
47 cmdline = grub_malloc (max_len);
48 if (! cmdline)
49 return 0;
50
51 while (1)
52 {
53 if (grub_file_read (file, &c, 1) != 1)
54 break;
55
56 /* Skip all carriage returns. */
57 if (c == '\r')
58 continue;
59
60 /* Replace tabs with spaces. */
61 if (c == '\t')
62 c = ' ';
63
64 /* The previous is a backslash, then... */
65 if (literal)
66 {
67 /* If it is a newline, replace it with a space and continue. */
68 if (c == '\n')
69 {
70 c = ' ';
71
72 /* Go back to overwrite the backslash. */
73 if (pos > 0)
74 pos--;
75 }
76
77 literal = 0;
78 }
79
80 if (c == '\\')
81 literal = 1;
82
83 if (pos == 0)
84 {
85 if (! grub_isspace (c))
86 cmdline[pos++] = c;
87 }
88 else
89 {
90 if (pos >= max_len)
91 {
92 char *old_cmdline = cmdline;
93 max_len = max_len * 2;
94 cmdline = grub_realloc (cmdline, max_len);
95 if (! cmdline)
96 {
97 grub_free (old_cmdline);
98 return 0;
99 }
100 }
101
102 if (c == '\n')
103 break;
104
105 cmdline[pos++] = c;
106 }
107 }
108
109 cmdline[pos] = '\0';
110
111 /* If the buffer is empty, don't return anything at all. */
112 if (pos == 0)
113 {
114 grub_free (cmdline);
115 cmdline = 0;
116 }
117
118 return cmdline;
119 }
120
121 static void
122 free_menu (grub_menu_t menu)
123 {
124 grub_menu_entry_t entry = menu->entry_list;
125
126 while (entry)
127 {
128 grub_menu_entry_t next_entry = entry->next;
129
130 grub_free ((void *) entry->title);
131 grub_free ((void *) entry->sourcecode);
132 entry = next_entry;
133 }
134
135 grub_free (menu);
136 grub_env_unset_data_slot ("menu");
137 }
138
139 static void
140 free_menu_entry_classes (struct grub_menu_entry_class *head)
141 {
142 /* Free all the classes. */
143 while (head)
144 {
145 struct grub_menu_entry_class *next;
146
147 grub_free (head->name);
148 next = head->next;
149 grub_free (head);
150 head = next;
151 }
152 }
153
154 /* Add a menu entry to the current menu context (as given by the environment
155 variable data slot `menu'). As the configuration file is read, the script
156 parser calls this when a menu entry is to be created. */
157 grub_err_t
158 grub_normal_add_menu_entry (int argc, const char **args,
159 const char *sourcecode)
160 {
161 const char *menutitle = 0;
162 const char *menusourcecode;
163 grub_menu_t menu;
164 grub_menu_entry_t *last;
165 int failed = 0;
166 int i;
167 struct grub_menu_entry_class *classes_head; /* Dummy head node for list. */
168 struct grub_menu_entry_class *classes_tail;
169 char *users = NULL;
170
171 /* Allocate dummy head node for class list. */
172 classes_head = grub_zalloc (sizeof (struct grub_menu_entry_class));
173 if (! classes_head)
174 return grub_errno;
175 classes_tail = classes_head;
176
177 menu = grub_env_get_data_slot ("menu");
178 if (! menu)
179 return grub_error (GRUB_ERR_MENU, "no menu context");
180
181 last = &menu->entry_list;
182
183 menusourcecode = grub_strdup (sourcecode);
184 if (! menusourcecode)
185 return grub_errno;
186
187 /* Parse menu arguments. */
188 for (i = 0; i < argc; i++)
189 {
190 /* Capture arguments. */
191 if (grub_strncmp ("--", args[i], 2) == 0)
192 {
193 const char *arg = &args[i][2];
194
195 /* Handle menu class. */
196 if (grub_strcmp(arg, "class") == 0)
197 {
198 char *class_name;
199 struct grub_menu_entry_class *new_class;
200
201 i++;
202 class_name = grub_strdup (args[i]);
203 if (! class_name)
204 {
205 failed = 1;
206 break;
207 }
208
209 /* Create a new class and add it at the tail of the list. */
210 new_class = grub_zalloc (sizeof (struct grub_menu_entry_class));
211 if (! new_class)
212 {
213 grub_free (class_name);
214 failed = 1;
215 break;
216 }
217 /* Fill in the new class node. */
218 new_class->name = class_name;
219 /* Link the tail to it, and make it the new tail. */
220 classes_tail->next = new_class;
221 classes_tail = new_class;
222 continue;
223 }
224 else if (grub_strcmp(arg, "users") == 0)
225 {
226 i++;
227 users = grub_strdup (args[i]);
228 if (! users)
229 {
230 failed = 1;
231 break;
232 }
233
234 continue;
235 }
236 else
237 {
238 /* Handle invalid argument. */
239 failed = 1;
240 grub_error (GRUB_ERR_MENU,
241 "invalid argument for menuentry: %s", args[i]);
242 break;
243 }
244 }
245
246 /* Capture title. */
247 if (! menutitle)
248 {
249 menutitle = grub_strdup (args[i]);
250 }
251 else
252 {
253 failed = 1;
254 grub_error (GRUB_ERR_MENU,
255 "too many titles for menuentry: %s", args[i]);
256 break;
257 }
258 }
259
260 /* Validate arguments. */
261 if ((! failed) && (! menutitle))
262 {
263 grub_error (GRUB_ERR_MENU, "menuentry is missing title");
264 failed = 1;
265 }
266
267 /* If argument parsing failed, free any allocated resources. */
268 if (failed)
269 {
270 free_menu_entry_classes (classes_head);
271 grub_free ((void *) menutitle);
272 grub_free ((void *) menusourcecode);
273
274 /* Here we assume that grub_error has been used to specify failure details. */
275 return grub_errno;
276 }
277
278 /* Add the menu entry at the end of the list. */
279 while (*last)
280 last = &(*last)->next;
281
282 *last = grub_zalloc (sizeof (**last));
283 if (! *last)
284 {
285 free_menu_entry_classes (classes_head);
286 grub_free ((void *) menutitle);
287 grub_free ((void *) menusourcecode);
288 return grub_errno;
289 }
290
291 (*last)->title = menutitle;
292 (*last)->classes = classes_head;
293 if (users)
294 (*last)->restricted = 1;
295 (*last)->users = users;
296 (*last)->sourcecode = menusourcecode;
297
298 menu->size++;
299
300 return GRUB_ERR_NONE;
301 }
302
303 static grub_menu_t
304 read_config_file (const char *config)
305 {
306 grub_file_t file;
307 grub_parser_t old_parser = 0;
308
309 auto grub_err_t getline (char **line, int cont);
310 grub_err_t getline (char **line, int cont __attribute__ ((unused)))
311 {
312 while (1)
313 {
314 char *buf;
315
316 *line = buf = grub_file_getline (file);
317 if (! buf)
318 return grub_errno;
319
320 if (buf[0] == '#')
321 {
322 if (buf[1] == '!')
323 {
324 grub_parser_t parser;
325 grub_named_list_t list;
326
327 buf += 2;
328 while (grub_isspace (*buf))
329 buf++;
330
331 if (! old_parser)
332 old_parser = grub_parser_get_current ();
333
334 list = GRUB_AS_NAMED_LIST (grub_parser_class.handler_list);
335 parser = grub_named_list_find (list, buf);
336 if (parser)
337 grub_parser_set_current (parser);
338 else
339 {
340 char cmd_name[8 + grub_strlen (buf)];
341
342 /* Perhaps it's not loaded yet, try the autoload
343 command. */
344 grub_strcpy (cmd_name, "parser.");
345 grub_strcat (cmd_name, buf);
346 grub_command_execute (cmd_name, 0, 0);
347 }
348 }
349 grub_free (*line);
350 }
351 else
352 break;
353 }
354
355 return GRUB_ERR_NONE;
356 }
357
358 grub_menu_t newmenu;
359
360 newmenu = grub_env_get_data_slot ("menu");
361 if (! newmenu)
362 {
363 newmenu = grub_zalloc (sizeof (*newmenu));
364 if (! newmenu)
365 return 0;
366
367 grub_env_set_data_slot ("menu", newmenu);
368 }
369
370 /* Try to open the config file. */
371 file = grub_file_open (config);
372 if (! file)
373 return 0;
374
375 grub_reader_loop (getline);
376 grub_file_close (file);
377
378 if (old_parser)
379 grub_parser_set_current (old_parser);
380
381 return newmenu;
382 }
383
384 /* Initialize the screen. */
385 void
386 grub_normal_init_page (void)
387 {
388 grub_uint8_t width, margin;
389
390 #define TITLE ("GNU GRUB version " PACKAGE_VERSION)
391
392 width = grub_getwh () >> 8;
393 margin = (width - (sizeof(TITLE) + 7)) / 2;
394
395 grub_cls ();
396 grub_putchar ('\n');
397
398 while (margin--)
399 grub_putchar (' ');
400
401 grub_printf ("%s\n\n", TITLE);
402
403 #undef TITLE
404 }
405
406 static int reader_nested;
407
408 /* Read the config file CONFIG and execute the menu interface or
409 the command line interface if BATCH is false. */
410 void
411 grub_normal_execute (const char *config, int nested, int batch)
412 {
413 grub_menu_t menu = 0;
414
415 read_command_list ();
416 read_fs_list ();
417 read_handler_list ();
418 grub_command_execute ("parser.grub", 0, 0);
419
420 reader_nested = nested;
421
422 if (config)
423 {
424 menu = read_config_file (config);
425
426 /* Ignore any error. */
427 grub_errno = GRUB_ERR_NONE;
428 }
429
430 if (! batch)
431 {
432 if (menu && menu->size)
433 {
434 grub_menu_viewer_show_menu (menu, nested);
435 if (nested)
436 free_menu (menu);
437 }
438 }
439 }
440
441 /* This starts the normal mode. */
442 void
443 grub_enter_normal_mode (const char *config)
444 {
445 grub_normal_execute (config, 0, 0);
446 }
447
448 /* Enter normal mode from rescue mode. */
449 static grub_err_t
450 grub_cmd_normal (struct grub_command *cmd,
451 int argc, char *argv[])
452 {
453 grub_unregister_command (cmd);
454
455 if (argc == 0)
456 {
457 /* Guess the config filename. It is necessary to make CONFIG static,
458 so that it won't get broken by longjmp. */
459 static char *config;
460 const char *prefix;
461
462 prefix = grub_env_get ("prefix");
463 if (prefix)
464 {
465 config = grub_malloc (grub_strlen (prefix) + sizeof ("/grub.cfg"));
466 if (! config)
467 goto quit;
468
469 grub_sprintf (config, "%s/grub.cfg", prefix);
470 grub_enter_normal_mode (config);
471 grub_free (config);
472 }
473 else
474 grub_enter_normal_mode (0);
475 }
476 else
477 grub_enter_normal_mode (argv[0]);
478
479 quit:
480 return 0;
481 }
482
483 void
484 grub_cmdline_run (int nested)
485 {
486 grub_reader_t reader;
487 grub_err_t err = GRUB_ERR_NONE;
488
489 err = grub_auth_check_authentication (NULL);
490
491 if (err)
492 {
493 grub_print_error ();
494 grub_errno = GRUB_ERR_NONE;
495 return;
496 }
497
498 reader = grub_reader_get_current ();
499
500 reader_nested = nested;
501 if (reader->init)
502 reader->init ();
503 grub_reader_loop (0);
504 }
505
506 static grub_err_t
507 grub_normal_reader_init (void)
508 {
509 grub_normal_init_page ();
510 grub_setcursor (1);
511
512 grub_printf_ (N_("\
513 [ Minimal BASH-like line editing is supported. For the first word, TAB\n\
514 lists possible command completions. Anywhere else TAB lists possible\n\
515 device/file completions.%s ]\n\n"),
516 reader_nested ? " ESC at any time exits." : "");
517
518 return 0;
519 }
520
521 static char cmdline[GRUB_MAX_CMDLINE];
522
523 static grub_err_t
524 grub_normal_read_line (char **line, int cont)
525 {
526 grub_parser_t parser = grub_parser_get_current ();
527 char prompt[sizeof("> ") + grub_strlen (parser->name)];
528
529 grub_sprintf (prompt, "%s> ", parser->name);
530
531 while (1)
532 {
533 cmdline[0] = 0;
534 if (grub_cmdline_get (prompt, cmdline, sizeof (cmdline), 0, 1, 1))
535 break;
536
537 if ((reader_nested) || (cont))
538 {
539 *line = 0;
540 return grub_errno;
541 }
542 }
543
544 *line = grub_strdup (cmdline);
545 return 0;
546 }
547
548 static struct grub_reader grub_normal_reader =
549 {
550 .name = "normal",
551 .init = grub_normal_reader_init,
552 .read_line = grub_normal_read_line
553 };
554
555 static char *
556 grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
557 const char *val)
558 {
559 grub_set_more ((*val == '1'));
560 return grub_strdup (val);
561 }
562
563 GRUB_MOD_INIT(normal)
564 {
565 /* Normal mode shouldn't be unloaded. */
566 if (mod)
567 grub_dl_ref (mod);
568
569 grub_menu_viewer_register (&grub_normal_text_menu_viewer);
570
571 grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
572
573 grub_reader_register ("normal", &grub_normal_reader);
574 grub_reader_set_current (&grub_normal_reader);
575 grub_register_variable_hook ("pager", 0, grub_env_write_pager);
576
577 /* Register a command "normal" for the rescue mode. */
578 grub_register_command_prio ("normal", grub_cmd_normal,
579 0, "Enter normal mode", 0);
580
581 /* Reload terminal colors when these variables are written to. */
582 grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
583 grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);
584
585 /* Preserve hooks after context changes. */
586 grub_env_export ("color_normal");
587 grub_env_export ("color_highlight");
588 }
589
590 GRUB_MOD_FINI(normal)
591 {
592 grub_set_history (0);
593 grub_reader_unregister (&grub_normal_reader);
594 grub_register_variable_hook ("pager", 0, 0);
595 grub_fs_autoload_hook = 0;
596 free_handler_list ();
597 }