main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <config.h>
  20. #include <time.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <setjmp.h>
  24. #include <string.h>
  25. #include <signal.h>
  26. #include <sys/types.h>
  27. #include <grub/dl.h>
  28. #include <grub/mm.h>
  29. #include <grub/setjmp.h>
  30. #include <grub/fs.h>
  31. #include <grub/emu/hostdisk.h>
  32. #include <grub/time.h>
  33. #include <grub/emu/console.h>
  34. #include <grub/emu/misc.h>
  35. #include <grub/kernel.h>
  36. #include <grub/normal.h>
  37. #include <grub/emu/getroot.h>
  38. #include <grub/env.h>
  39. #include <grub/partition.h>
  40. #include <grub/i18n.h>
  41. #include <grub/loader.h>
  42. #include <grub/util/misc.h>
  43. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  44. #include "progname.h"
  45. #include <argp.h>
  46. #define ENABLE_RELOCATABLE 0
  47. /* Used for going back to the main function. */
  48. static jmp_buf main_env;
  49. /* Store the prefix specified by an argument. */
  50. static char *root_dev = NULL, *dir = NULL, *tpm_dev = NULL;
  51. grub_addr_t grub_modbase = 0;
  52. void
  53. grub_reboot (void)
  54. {
  55. longjmp (main_env, 1);
  56. grub_fatal ("longjmp failed");
  57. }
  58. void
  59. grub_exit (void)
  60. {
  61. grub_reboot ();
  62. }
  63. void
  64. grub_machine_init (void)
  65. {
  66. }
  67. void
  68. grub_machine_get_bootlocation (char **device, char **path)
  69. {
  70. *device = root_dev;
  71. *path = dir;
  72. }
  73. void
  74. grub_machine_fini (int flags)
  75. {
  76. if (flags & GRUB_LOADER_FLAG_NORETURN)
  77. grub_console_fini ();
  78. }
  79. #define OPT_MEMDISK 257
  80. static struct argp_option options[] = {
  81. {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
  82. {"device-map", 'm', N_("FILE"), 0,
  83. /* TRANSLATORS: There are many devices in device map. */
  84. N_("use FILE as the device map [default=%s]"), 0},
  85. {"memdisk", OPT_MEMDISK, N_("FILE"), 0,
  86. /* TRANSLATORS: There are many devices in device map. */
  87. N_("use FILE as memdisk"), 0},
  88. {"directory", 'd', N_("DIR"), 0,
  89. N_("use GRUB files in the directory DIR [default=%s]"), 0},
  90. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  91. {"hold", 'H', N_("SECS"), OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
  92. {"kexec", 'X', 0, 0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0},
  93. {"tpm-device", 't', N_("DEV"), 0, N_("set TPM device."), 0},
  94. { 0, 0, 0, 0, 0, 0 }
  95. };
  96. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  97. static char *
  98. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  99. {
  100. switch (key)
  101. {
  102. case 'd':
  103. return xasprintf (text, DEFAULT_DIRECTORY);
  104. case 'm':
  105. return xasprintf (text, DEFAULT_DEVICE_MAP);
  106. default:
  107. return (char *) text;
  108. }
  109. }
  110. #pragma GCC diagnostic error "-Wformat-nonliteral"
  111. struct arguments
  112. {
  113. const char *dev_map;
  114. const char *mem_disk;
  115. int hold;
  116. };
  117. static error_t
  118. argp_parser (int key, char *arg, struct argp_state *state)
  119. {
  120. /* Get the input argument from argp_parse, which we
  121. know is a pointer to our arguments structure. */
  122. struct arguments *arguments = state->input;
  123. switch (key)
  124. {
  125. case OPT_MEMDISK:
  126. arguments->mem_disk = arg;
  127. break;
  128. case 'r':
  129. free (root_dev);
  130. root_dev = xstrdup (arg);
  131. break;
  132. case 'd':
  133. free (dir);
  134. dir = xstrdup (arg);
  135. break;
  136. case 'm':
  137. arguments->dev_map = arg;
  138. break;
  139. case 'H':
  140. arguments->hold = (arg ? atoi (arg) : -1);
  141. break;
  142. case 'v':
  143. verbosity++;
  144. break;
  145. case 'X':
  146. grub_util_set_kexecute ();
  147. break;
  148. case 't':
  149. free (tpm_dev);
  150. tpm_dev = xstrdup (arg);
  151. break;
  152. case ARGP_KEY_ARG:
  153. {
  154. /* Too many arguments. */
  155. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  156. fprintf (stderr, "\n");
  157. argp_usage (state);
  158. }
  159. break;
  160. default:
  161. return ARGP_ERR_UNKNOWN;
  162. }
  163. return 0;
  164. }
  165. static struct argp argp = {
  166. options, argp_parser, NULL,
  167. N_("GRUB emulator."),
  168. NULL, help_filter, NULL
  169. };
  170. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  171. int
  172. main (int argc, char *argv[])
  173. {
  174. struct arguments arguments =
  175. {
  176. .dev_map = DEFAULT_DEVICE_MAP,
  177. .hold = 0,
  178. .mem_disk = 0,
  179. };
  180. volatile int hold = 0;
  181. size_t total_module_size = sizeof (struct grub_module_info), memdisk_size = 0;
  182. struct grub_module_info *modinfo;
  183. void *mods;
  184. grub_util_host_init (&argc, &argv);
  185. dir = xstrdup (DEFAULT_DIRECTORY);
  186. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  187. {
  188. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  189. exit(1);
  190. }
  191. if (arguments.mem_disk)
  192. {
  193. memdisk_size = ALIGN_UP(grub_util_get_image_size (arguments.mem_disk), 512);
  194. total_module_size += memdisk_size + sizeof (struct grub_module_header);
  195. }
  196. mods = xmalloc (total_module_size);
  197. modinfo = grub_memset (mods, 0, total_module_size);
  198. mods = (char *) (modinfo + 1);
  199. modinfo->magic = GRUB_MODULE_MAGIC;
  200. modinfo->offset = sizeof (struct grub_module_info);
  201. modinfo->size = total_module_size;
  202. if (arguments.mem_disk)
  203. {
  204. struct grub_module_header *header = (struct grub_module_header *) mods;
  205. header->type = OBJ_TYPE_MEMDISK;
  206. header->size = memdisk_size + sizeof (*header);
  207. mods = header + 1;
  208. grub_util_load_image (arguments.mem_disk, mods);
  209. mods = (char *) mods + memdisk_size;
  210. }
  211. grub_modbase = (grub_addr_t) modinfo;
  212. hold = arguments.hold;
  213. /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
  214. if (hold && verbosity > 0)
  215. /* TRANSLATORS: In this case GRUB tells user what he has to do. */
  216. printf (_("Run `gdb %s %d', and set ARGS.HOLD to zero.\n"),
  217. program_name, (int) getpid ());
  218. while (hold)
  219. {
  220. if (hold > 0)
  221. hold--;
  222. sleep (1);
  223. }
  224. signal (SIGINT, SIG_IGN);
  225. grub_console_init ();
  226. grub_host_init ();
  227. /* XXX: This is a bit unportable. */
  228. grub_util_biosdisk_init (arguments.dev_map);
  229. grub_init_all ();
  230. grub_hostfs_init ();
  231. /* Make sure that there is a root device. */
  232. if (! root_dev)
  233. root_dev = grub_strdup ("host");
  234. dir = xstrdup (dir);
  235. if (tpm_dev)
  236. grub_util_tpm_open (tpm_dev);
  237. /* Start GRUB! */
  238. if (setjmp (main_env) == 0)
  239. grub_main ();
  240. grub_fini_all ();
  241. grub_hostfs_fini ();
  242. grub_host_fini ();
  243. grub_util_tpm_close ();
  244. grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
  245. return 0;
  246. }