main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <grub/kernel.h>
  20. #include <grub/normal.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/file.h>
  24. #include <grub/mm.h>
  25. #include <grub/lib.h>
  26. #include <grub/env.h>
  27. #include <grub/menu.h>
  28. #include <grub/command.h>
  29. #include <grub/i18n.h>
  30. GRUB_EXPORT(grub_normal_exit_level);
  31. static int nested_level = 0;
  32. int grub_normal_exit_level = 0;
  33. static void
  34. read_lists (const char *val)
  35. {
  36. #ifdef GRUB_MACHINE_EMU
  37. (void) val;
  38. #else
  39. read_command_list (val);
  40. read_fs_list (val);
  41. read_crypto_list (val);
  42. read_terminal_list (val);
  43. #endif
  44. }
  45. static char *
  46. read_lists_hook (struct grub_env_var *var __attribute__ ((unused)),
  47. const char *val)
  48. {
  49. read_lists (val);
  50. return val ? grub_strdup (val) : NULL;
  51. }
  52. /* This starts the normal mode. */
  53. static void
  54. grub_enter_normal_mode (const char *config)
  55. {
  56. const char *prefix = grub_env_get ("prefix");
  57. nested_level++;
  58. read_lists (prefix);
  59. read_handler_list ();
  60. grub_autolist_font = grub_autolist_load ("fonts/font.lst");
  61. grub_errno = 0;
  62. grub_register_variable_hook ("prefix", NULL, read_lists_hook);
  63. grub_command_execute ("parser.grub", 0, 0);
  64. grub_command_execute ("controller.normal", 0, 0);
  65. grub_menu_execute (config, 0, 0);
  66. nested_level--;
  67. if (grub_normal_exit_level)
  68. grub_normal_exit_level--;
  69. }
  70. /* Enter normal mode from rescue mode. */
  71. static grub_err_t
  72. grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
  73. int argc, char *argv[])
  74. {
  75. if (argc == 0)
  76. {
  77. /* Guess the config filename. It is necessary to make CONFIG static,
  78. so that it won't get broken by longjmp. */
  79. static char *config;
  80. const char *prefix;
  81. prefix = grub_env_get ("prefix");
  82. if (prefix)
  83. {
  84. config = grub_xasprintf ("%s/burg.cfg", prefix);
  85. if (! config)
  86. goto quit;
  87. grub_enter_normal_mode (config);
  88. grub_free (config);
  89. }
  90. else
  91. grub_enter_normal_mode (0);
  92. }
  93. else
  94. grub_enter_normal_mode (argv[0]);
  95. quit:
  96. return 0;
  97. }
  98. /* Exit from normal mode to rescue mode. */
  99. static grub_err_t
  100. grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
  101. int argc __attribute__ ((unused)),
  102. char *argv[] __attribute__ ((unused)))
  103. {
  104. if (nested_level <= grub_normal_exit_level)
  105. return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
  106. grub_normal_exit_level++;
  107. return GRUB_ERR_NONE;
  108. }
  109. static grub_command_t export_cmd;
  110. static grub_err_t
  111. grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
  112. int argc, char **args)
  113. {
  114. if (argc < 1)
  115. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  116. "no environment variable specified");
  117. grub_env_export (args[0]);
  118. return 0;
  119. }
  120. GRUB_MOD_INIT(normal)
  121. {
  122. grub_env_export ("root");
  123. grub_env_export ("prefix");
  124. export_cmd = grub_register_command ("export", grub_cmd_export,
  125. N_("ENVVAR"), N_("Export a variable."));
  126. /* Normal mode shouldn't be unloaded. */
  127. if (mod)
  128. grub_dl_ref (mod);
  129. grub_history_init (GRUB_DEFAULT_HISTORY_SIZE);
  130. grub_install_newline_hook ();
  131. /* Register a command "normal" for the rescue mode. */
  132. grub_register_command ("normal", grub_cmd_normal,
  133. 0, N_("Enter normal mode."));
  134. grub_register_command ("normal_exit", grub_cmd_normal_exit,
  135. 0, N_("Exit from normal mode."));
  136. }
  137. GRUB_MOD_FINI(normal)
  138. {
  139. grub_unregister_command (export_cmd);
  140. grub_history_init (0);
  141. grub_register_variable_hook ("pager", 0, 0);
  142. grub_fs_autoload_hook = 0;
  143. free_handler_list ();
  144. }