main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.h>
  19. #include <config-util.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;
  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. { 0, 0, 0, 0, 0, 0 }
  93. };
  94. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  95. static char *
  96. help_filter (int key, const char *text, void *input __attribute__ ((unused)))
  97. {
  98. switch (key)
  99. {
  100. case 'd':
  101. return xasprintf (text, DEFAULT_DIRECTORY);
  102. case 'm':
  103. return xasprintf (text, DEFAULT_DEVICE_MAP);
  104. default:
  105. return (char *) text;
  106. }
  107. }
  108. #pragma GCC diagnostic error "-Wformat-nonliteral"
  109. struct arguments
  110. {
  111. const char *dev_map;
  112. const char *mem_disk;
  113. int hold;
  114. };
  115. static error_t
  116. argp_parser (int key, char *arg, struct argp_state *state)
  117. {
  118. /* Get the input argument from argp_parse, which we
  119. know is a pointer to our arguments structure. */
  120. struct arguments *arguments = state->input;
  121. switch (key)
  122. {
  123. case OPT_MEMDISK:
  124. arguments->mem_disk = arg;
  125. break;
  126. case 'r':
  127. free (root_dev);
  128. root_dev = xstrdup (arg);
  129. break;
  130. case 'd':
  131. free (dir);
  132. dir = xstrdup (arg);
  133. break;
  134. case 'm':
  135. arguments->dev_map = arg;
  136. break;
  137. case 'H':
  138. arguments->hold = (arg ? atoi (arg) : -1);
  139. break;
  140. case 'v':
  141. verbosity++;
  142. break;
  143. case ARGP_KEY_ARG:
  144. {
  145. /* Too many arguments. */
  146. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  147. fprintf (stderr, "\n");
  148. argp_usage (state);
  149. }
  150. break;
  151. default:
  152. return ARGP_ERR_UNKNOWN;
  153. }
  154. return 0;
  155. }
  156. static struct argp argp = {
  157. options, argp_parser, NULL,
  158. N_("GRUB emulator."),
  159. NULL, help_filter, NULL
  160. };
  161. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  162. int
  163. main (int argc, char *argv[])
  164. {
  165. struct arguments arguments =
  166. {
  167. .dev_map = DEFAULT_DEVICE_MAP,
  168. .hold = 0,
  169. .mem_disk = 0,
  170. };
  171. volatile int hold = 0;
  172. size_t total_module_size = sizeof (struct grub_module_info), memdisk_size = 0;
  173. struct grub_module_info *modinfo;
  174. void *mods;
  175. grub_util_host_init (&argc, &argv);
  176. dir = xstrdup (DEFAULT_DIRECTORY);
  177. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  178. {
  179. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  180. exit(1);
  181. }
  182. if (arguments.mem_disk)
  183. {
  184. memdisk_size = ALIGN_UP(grub_util_get_image_size (arguments.mem_disk), 512);
  185. total_module_size += memdisk_size + sizeof (struct grub_module_header);
  186. }
  187. mods = xmalloc (total_module_size);
  188. modinfo = grub_memset (mods, 0, total_module_size);
  189. mods = (char *) (modinfo + 1);
  190. modinfo->magic = GRUB_MODULE_MAGIC;
  191. modinfo->offset = sizeof (struct grub_module_info);
  192. modinfo->size = total_module_size;
  193. if (arguments.mem_disk)
  194. {
  195. struct grub_module_header *header = (struct grub_module_header *) mods;
  196. header->type = OBJ_TYPE_MEMDISK;
  197. header->size = memdisk_size + sizeof (*header);
  198. mods = header + 1;
  199. grub_util_load_image (arguments.mem_disk, mods);
  200. mods = (char *) mods + memdisk_size;
  201. }
  202. grub_modbase = (grub_addr_t) modinfo;
  203. hold = arguments.hold;
  204. /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
  205. if (hold && verbosity > 0)
  206. /* TRANSLATORS: In this case GRUB tells user what he has to do. */
  207. printf (_("Run `gdb %s %d', and set ARGS.HOLD to zero.\n"),
  208. program_name, (int) getpid ());
  209. while (hold)
  210. {
  211. if (hold > 0)
  212. hold--;
  213. sleep (1);
  214. }
  215. signal (SIGINT, SIG_IGN);
  216. grub_console_init ();
  217. grub_host_init ();
  218. /* XXX: This is a bit unportable. */
  219. grub_util_biosdisk_init (arguments.dev_map);
  220. grub_init_all ();
  221. grub_hostfs_init ();
  222. /* Make sure that there is a root device. */
  223. if (! root_dev)
  224. root_dev = grub_strdup ("host");
  225. dir = xstrdup (dir);
  226. /* Start GRUB! */
  227. if (setjmp (main_env) == 0)
  228. grub_main ();
  229. grub_fini_all ();
  230. grub_hostfs_fini ();
  231. grub_host_fini ();
  232. grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
  233. return 0;
  234. }