main.c 13 KB
Newer Older
1 2
/* main.c - the normal mode main routine */
/*
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 21 22 23 24 25 26 27
#include <grub/kernel.h>
#include <grub/normal.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/term.h>
#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
#include <grub/bufio.h>
36

37 38
GRUB_MOD_LICENSE ("GPLv3+");

39
#define GRUB_DEFAULT_HISTORY_SIZE	50
40

41 42 43
static int nested_level = 0;
int grub_normal_exit_level = 0;

44 45
void
grub_normal_free_menu (grub_menu_t menu)
46
{
47
  grub_menu_entry_t entry = menu->entry_list;
48

49 50
  while (entry)
    {
51
      grub_menu_entry_t next_entry = entry->next;
52
      grub_size_t i;
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      if (entry->classes)
	{
	  struct grub_menu_entry_class *class;
	  for (class = entry->classes; class; class = class->next)
	    grub_free (class->name);
	  grub_free (entry->classes);
	}

      if (entry->args)
	{
	  for (i = 0; entry->args[i]; i++)
	    grub_free (entry->args[i]);
	  grub_free (entry->args);
	}

      grub_free ((void *) entry->id);
      grub_free ((void *) entry->users);
71
      grub_free ((void *) entry->title);
72
      grub_free ((void *) entry->sourcecode);
73
      grub_free (entry);
74 75 76
      entry = next_entry;
    }

77
  grub_free (menu);
78
  grub_env_unset_menu ();
79 80
}

81 82 83 84
/* Helper for read_config_file.  */
static grub_err_t
read_config_file_getline (char **line, int cont __attribute__ ((unused)),
			  void *data)
85
{
86
  grub_file_t file = data;
87

88
  while (1)
89
    {
90
      char *buf;
91

92 93 94
      *line = buf = grub_file_getline (file);
      if (! buf)
	return grub_errno;
95

96 97 98 99
      if (buf[0] == '#')
	grub_free (*line);
      else
	break;
100 101
    }

102 103 104 105 106 107
  return GRUB_ERR_NONE;
}

static grub_menu_t
read_config_file (const char *config)
{
108
  grub_file_t rawfile, file;
109
  char *old_file = 0, *old_dir = 0;
110
  char *config_dir, *ptr = 0;
111
  const char *ctmp;
112

113 114
  grub_menu_t newmenu;

115
  newmenu = grub_env_get_menu ();
116
  if (! newmenu)
117
    {
118
      newmenu = grub_zalloc (sizeof (*newmenu));
119 120
      if (! newmenu)
	return 0;
121

122
      grub_env_set_menu (newmenu);
123
    }
124

125
  /* Try to open the config file.  */
126 127
  rawfile = grub_file_open (config);
  if (! rawfile)
128 129
    return 0;

130 131 132
  file = grub_bufio_open (rawfile, 0);
  if (! file)
    {
133
      grub_file_close (rawfile);
134 135 136
      return 0;
    }

137 138 139 140 141 142
  ctmp = grub_env_get ("config_file");
  if (ctmp)
    old_file = grub_strdup (ctmp);
  ctmp = grub_env_get ("config_directory");
  if (ctmp)
    old_dir = grub_strdup (ctmp);
143 144 145 146 147 148 149 150 151 152 153 154
  if (*config == '(')
    {
      grub_env_set ("config_file", config);
      config_dir = grub_strdup (config);
    }
  else
    {
      /* $root is guranteed to be defined, otherwise open above would fail */
      config_dir = grub_xasprintf ("(%s)%s", grub_env_get ("root"), config);
      if (config_dir)
	grub_env_set ("config_file", config_dir);
    }
155
  if (config_dir)
156 157 158 159 160 161 162
    {
      ptr = grub_strrchr (config_dir, '/');
      if (ptr)
	*ptr = 0;
      grub_env_set ("config_directory", config_dir);
      grub_free (config_dir);
    }
163 164 165 166

  grub_env_export ("config_file");
  grub_env_export ("config_directory");

167 168 169 170 171 172 173 174
  while (1)
    {
      char *line;

      /* Print an error, if any.  */
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;

175
      if ((read_config_file_getline (&line, 0, file)) || (! line))
176 177
	break;

178
      grub_normal_parse_line (line, read_config_file_getline, file);
179 180 181
      grub_free (line);
    }

182 183 184 185 186 187 188 189
  if (old_file)
    grub_env_set ("config_file", old_file);
  else
    grub_env_unset ("config_file");
  if (old_dir)
    grub_env_set ("config_directory", old_dir);
  else
    grub_env_unset ("config_directory");
190 191
  grub_free (old_file);
  grub_free (old_dir);
192

193
  grub_file_close (file);
194

195
  return newmenu;
196 197 198 199
}

/* Initialize the screen.  */
void
200 201
grub_normal_init_page (struct grub_term_output *term,
		       int y)
202
{
203
  grub_ssize_t msg_len;
204
  int posx;
205
  char *msg_formatted;
206 207
  grub_uint32_t *unicode_msg;
  grub_uint32_t *last_position;
208
 
209
  grub_term_cls (term);
210

211
  msg_formatted = grub_xasprintf (_("GNU GRUB  version %s"), PACKAGE_VERSION);
212 213 214
  if (!msg_formatted)
    return;
 
215
  msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
216
  				     &unicode_msg, &last_position);
217
  grub_free (msg_formatted);
218
 
219 220 221 222
  if (msg_len < 0)
    {
      return;
    }
223

224
  posx = grub_getstringwidth (unicode_msg, last_position, term);
225 226 227
  posx = ((int) grub_term_width (term) - posx) / 2;
  if (posx < 0)
    posx = 0;
228
  grub_term_gotoxy (term, (struct grub_term_coordinate) { posx, y });
229

230
  grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
231 232
  grub_putcode ('\n', term);
  grub_putcode ('\n', term);
233
  grub_free (unicode_msg);
234 235
}

236 237 238
static void
read_lists (const char *val)
{
239
  if (! grub_no_modules)
240 241 242 243 244 245
    {
      read_command_list (val);
      read_fs_list (val);
      read_crypto_list (val);
      read_terminal_list (val);
    }
246
  grub_gettext_reread_prefix (val);
247 248
}

249
static char *
250 251
read_lists_hook (struct grub_env_var *var __attribute__ ((unused)),
		 const char *val)
252
{
253
  read_lists (val);
254 255
  return val ? grub_strdup (val) : NULL;
}
256

257 258
/* Read the config file CONFIG and execute the menu interface or
   the command line interface if BATCH is false.  */
259
void
260
grub_normal_execute (const char *config, int nested, int batch)
261
{
262
  grub_menu_t menu = 0;
263
  const char *prefix;
264

265 266 267 268 269 270
  if (! nested)
    {
      prefix = grub_env_get ("prefix");
      read_lists (prefix);
      grub_register_variable_hook ("prefix", NULL, read_lists_hook);
    }
271

272 273
  grub_boot_time ("Executing config file");

274 275
  if (config)
    {
276
      menu = read_config_file (config);
277 278

      /* Ignore any error.  */
279
      grub_errno = GRUB_ERR_NONE;
280 281
    }

282 283
  grub_boot_time ("Executed config file");

284
  if (! batch)
285
    {
286
      if (menu && menu->size)
287
	{
288 289

	  grub_boot_time ("Entering menu");
290
	  grub_show_menu (menu, nested, 0);
291
	  if (nested)
292
	    grub_normal_free_menu (menu);
293
	}
294
    }
295 296
}

297 298 299
/* This starts the normal mode.  */
void
grub_enter_normal_mode (const char *config)
300
{
301
  grub_boot_time ("Entering normal mode");
302
  nested_level++;
303
  grub_normal_execute (config, 0, 0);
304
  grub_boot_time ("Entering shell");
305
  grub_cmdline_run (0, 1);
306 307 308
  nested_level--;
  if (grub_normal_exit_level)
    grub_normal_exit_level--;
309
  grub_boot_time ("Exiting normal mode");
310 311
}

312
/* Enter normal mode from rescue mode.  */
313
static grub_err_t
314
grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
315
		 int argc, char *argv[])
316 317 318
{
  if (argc == 0)
    {
319 320
      /* Guess the config filename. It is necessary to make CONFIG static,
	 so that it won't get broken by longjmp.  */
321
      char *config;
322
      const char *prefix;
323

324
      prefix = grub_env_get ("prefix");
325 326
      if (prefix)
	{
327
	  config = grub_xasprintf ("%s/grub.cfg", prefix);
328
	  if (! config)
329
	    goto quit;
330

331 332
	  grub_enter_normal_mode (config);
	  grub_free (config);
333 334
	}
      else
335
	grub_enter_normal_mode (0);
336 337
    }
  else
338
    grub_enter_normal_mode (argv[0]);
339 340 341

quit:
  return 0;
342 343
}

344
/* Exit from normal mode to rescue mode.  */
345
static grub_err_t
346 347 348
grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
		      int argc __attribute__ ((unused)),
		      char *argv[] __attribute__ ((unused)))
349
{
350 351 352 353
  if (nested_level <= grub_normal_exit_level)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
  grub_normal_exit_level++;
  return GRUB_ERR_NONE;
354 355 356
}

static grub_err_t
357
grub_normal_reader_init (int nested)
358
{
359
  struct grub_term_output *term;
360
  const char *msg_esc = _("ESC at any time exits.");
361
  char *msg_formatted;
362

363 364 365 366
  msg_formatted = grub_xasprintf (_("Minimal BASH-like line editing is supported. For "
				    "the first word, TAB lists possible command completions. Anywhere "
				    "else TAB lists possible device or file completions. %s"),
				  nested ? msg_esc : "");
367 368
  if (!msg_formatted)
    return grub_errno;
369

370 371
  FOR_ACTIVE_TERM_OUTPUTS(term)
  {
372
    grub_normal_init_page (term, 1);
373
    grub_term_setcursor (term, 1);
374

375 376 377 378 379
    if (grub_term_width (term) > 3 + STANDARD_MARGIN + 20)
      grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
    else
      grub_print_message_indented (msg_formatted, 0, 0, term);
    grub_putcode ('\n', term);
380 381
    grub_putcode ('\n', term);
    grub_putcode ('\n', term);
382
  }
383
  grub_free (msg_formatted);
384
 
385 386 387 388
  return 0;
}

static grub_err_t
389
grub_normal_read_line_real (char **line, int cont, int nested)
390
{
391
  const char *prompt;
392

393
  if (cont)
394 395
    /* TRANSLATORS: it's command line prompt.  */
    prompt = _(">");
396
  else
397 398
    /* TRANSLATORS: it's command line prompt.  */
    prompt = _("grub>");
399

400 401
  if (!prompt)
    return grub_errno;
402 403 404

  while (1)
    {
405 406
      *line = grub_cmdline_get (prompt);
      if (*line)
407
	return 0;
408

409
      if (cont || nested)
410
	{
411
	  grub_free (*line);
412 413 414 415
	  *line = 0;
	  return grub_errno;
	}
    }
416
 
417 418
}

419
static grub_err_t
420 421
grub_normal_read_line (char **line, int cont,
		       void *data __attribute__ ((unused)))
422 423 424 425
{
  return grub_normal_read_line_real (line, cont, 0);
}

426
void
427
grub_cmdline_run (int nested, int force_auth)
428 429 430
{
  grub_err_t err = GRUB_ERR_NONE;

431 432 433 434 435
  do
    {
      err = grub_auth_check_authentication (NULL);
    }
  while (err && force_auth);
436 437 438 439 440 441 442 443

  if (err)
    {
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;
      return;
    }

444
  grub_normal_reader_init (nested);
445 446 447

  while (1)
    {
448
      char *line = NULL;
449

450 451 452
      if (grub_normal_exit_level)
	break;

453 454 455 456
      /* Print an error, if any.  */
      grub_print_error ();
      grub_errno = GRUB_ERR_NONE;

457
      grub_normal_read_line_real (&line, 0, nested);
458
      if (! line)
459
	break;
460

461
      grub_normal_parse_line (line, grub_normal_read_line, NULL);
462 463 464
      grub_free (line);
    }
}
465 466 467 468 469 470 471 472 473

static char *
grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
		      const char *val)
{
  grub_set_more ((*val == '1'));
  return grub_strdup (val);
}

474 475 476 477 478 479 480 481 482 483 484 485
/* clear */
static grub_err_t
grub_mini_cmd_clear (struct grub_command *cmd __attribute__ ((unused)),
		   int argc __attribute__ ((unused)),
		   char *argv[] __attribute__ ((unused)))
{
  grub_cls ();
  return 0;
}

static grub_command_t cmd_clear;

486
static void (*grub_xputs_saved) (const char *str);
487
static const char *features[] = {
488
  "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint",
489
  "feature_default_font_path", "feature_all_video_module",
490
  "feature_menuentry_id", "feature_menuentry_options", "feature_200_final",
491
  "feature_nativedisk_cmd", "feature_timeout_style"
492
};
493

494
GRUB_MOD_INIT(normal)
495
{
496 497
  unsigned i;

498 499
  grub_boot_time ("Preparing normal module");

500 501
  /* Previously many modules depended on gzio. Be nice to user and load it.  */
  grub_dl_load ("gzio");
502
  grub_errno = 0;
503

504
  grub_normal_auth_init ();
505
  grub_context_init ();
BVK Chaitanya's avatar
BVK Chaitanya committed
506
  grub_script_init ();
507
  grub_menu_init ();
508

509 510 511
  grub_xputs_saved = grub_xputs;
  grub_xputs = grub_xputs_normal;

512
  /* Normal mode shouldn't be unloaded.  */
513 514
  if (mod)
    grub_dl_ref (mod);
515

516 517 518 519
  cmd_clear =
    grub_register_command ("clear", grub_mini_cmd_clear,
			   0, N_("Clear the screen."));

520
  grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
521

522
  grub_register_variable_hook ("pager", 0, grub_env_write_pager);
523
  grub_env_export ("pager");
524

525
  /* Register a command "normal" for the rescue mode.  */
526
  grub_register_command ("normal", grub_cmd_normal,
527
			 0, N_("Enter normal mode."));
528
  grub_register_command ("normal_exit", grub_cmd_normal_exit,
529
			 0, N_("Exit from normal mode."));
530

531 532 533 534 535 536 537
  /* Reload terminal colors when these variables are written to.  */
  grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
  grub_register_variable_hook ("color_highlight", NULL, grub_env_write_color_highlight);

  /* Preserve hooks after context changes.  */
  grub_env_export ("color_normal");
  grub_env_export ("color_highlight");
538 539

  /* Set default color names.  */
540 541
  grub_env_set ("color_normal", "light-gray/black");
  grub_env_set ("color_highlight", "black/light-gray");
542

543 544 545 546 547
  for (i = 0; i < ARRAY_SIZE (features); i++)
    {
      grub_env_set (features[i], "y");
      grub_env_export (features[i]);
    }
548 549 550 551
  grub_env_set ("grub_cpu", GRUB_TARGET_CPU);
  grub_env_export ("grub_cpu");
  grub_env_set ("grub_platform", GRUB_PLATFORM);
  grub_env_export ("grub_platform");
552 553

  grub_boot_time ("Normal module prepared");
554 555
}

556
GRUB_MOD_FINI(normal)
557
{
558
  grub_context_fini ();
BVK Chaitanya's avatar
BVK Chaitanya committed
559
  grub_script_fini ();
560
  grub_menu_fini ();
561
  grub_normal_auth_fini ();
562

563 564
  grub_xputs = grub_xputs_saved;

565
  grub_set_history (0);
566 567
  grub_register_variable_hook ("pager", 0, 0);
  grub_fs_autoload_hook = 0;
568
  grub_unregister_command (cmd_clear);
569
}