main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* main.c - the kernel main routine */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2003,2005,2006,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/stack_protector.h>
  21. #include <grub/misc.h>
  22. #include <grub/symbol.h>
  23. #include <grub/dl.h>
  24. #include <grub/term.h>
  25. #include <grub/file.h>
  26. #include <grub/device.h>
  27. #include <grub/env.h>
  28. #include <grub/mm.h>
  29. #include <grub/command.h>
  30. #include <grub/reader.h>
  31. #include <grub/parser.h>
  32. #include <grub/verify.h>
  33. #include <grub/types.h>
  34. #ifdef GRUB_MACHINE_PCBIOS
  35. #include <grub/machine/memory.h>
  36. #endif
  37. static bool cli_disabled = false;
  38. grub_addr_t
  39. grub_modules_get_end (void)
  40. {
  41. struct grub_module_info *modinfo;
  42. modinfo = (struct grub_module_info *) grub_modbase;
  43. /* Check if there are any modules. */
  44. if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
  45. return grub_modbase;
  46. return grub_modbase + modinfo->size;
  47. }
  48. /* Load all modules in core. */
  49. static void
  50. grub_load_modules (void)
  51. {
  52. struct grub_module_header *header;
  53. FOR_MODULES (header)
  54. {
  55. /* Not an ELF module, skip. */
  56. if (header->type != OBJ_TYPE_ELF)
  57. continue;
  58. if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
  59. (header->size - sizeof (struct grub_module_header))))
  60. grub_fatal ("%s", grub_errmsg);
  61. if (grub_errno)
  62. grub_print_error ();
  63. }
  64. }
  65. static char *load_config;
  66. static void
  67. grub_load_config (void)
  68. {
  69. struct grub_module_header *header;
  70. FOR_MODULES (header)
  71. {
  72. /* Not an embedded config, skip. */
  73. if (header->type != OBJ_TYPE_CONFIG)
  74. continue;
  75. load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
  76. if (!load_config)
  77. {
  78. grub_print_error ();
  79. break;
  80. }
  81. grub_memcpy (load_config, (char *) header +
  82. sizeof (struct grub_module_header),
  83. header->size - sizeof (struct grub_module_header));
  84. load_config[header->size - sizeof (struct grub_module_header)] = 0;
  85. break;
  86. }
  87. }
  88. /* Write hook for the environment variables of root. Remove surrounding
  89. parentheses, if any. */
  90. static char *
  91. grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
  92. const char *val)
  93. {
  94. /* XXX Is it better to check the existence of the device? */
  95. grub_size_t len = grub_strlen (val);
  96. if (val[0] == '(' && val[len - 1] == ')')
  97. return grub_strndup (val + 1, len - 2);
  98. return grub_strdup (val);
  99. }
  100. static void
  101. grub_set_prefix_and_root (void)
  102. {
  103. char *device = NULL;
  104. char *path = NULL;
  105. char *fwdevice = NULL;
  106. char *fwpath = NULL;
  107. char *prefix = NULL;
  108. struct grub_module_header *header;
  109. FOR_MODULES (header)
  110. if (header->type == OBJ_TYPE_PREFIX)
  111. prefix = (char *) header + sizeof (struct grub_module_header);
  112. grub_register_variable_hook ("root", 0, grub_env_write_root);
  113. grub_machine_get_bootlocation (&fwdevice, &fwpath);
  114. if (fwdevice)
  115. {
  116. char *cmdpath;
  117. cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
  118. if (cmdpath)
  119. {
  120. grub_env_set ("cmdpath", cmdpath);
  121. grub_env_export ("cmdpath");
  122. grub_free (cmdpath);
  123. }
  124. }
  125. if (prefix)
  126. {
  127. char *pptr = NULL;
  128. if (prefix[0] == '(')
  129. {
  130. pptr = grub_strrchr (prefix, ')');
  131. if (pptr)
  132. {
  133. device = grub_strndup (prefix + 1, pptr - prefix - 1);
  134. pptr++;
  135. }
  136. }
  137. if (!pptr)
  138. pptr = prefix;
  139. if (pptr[0])
  140. path = grub_strdup (pptr);
  141. }
  142. if (!device && fwdevice)
  143. device = fwdevice;
  144. else if (fwdevice && (device[0] == ',' || !device[0]))
  145. {
  146. /* We have a partition, but still need to fill in the drive. */
  147. char *comma, *new_device;
  148. for (comma = fwdevice; *comma; )
  149. {
  150. if (comma[0] == '\\' && comma[1] == ',')
  151. {
  152. comma += 2;
  153. continue;
  154. }
  155. if (*comma == ',')
  156. break;
  157. comma++;
  158. }
  159. if (*comma)
  160. {
  161. char *drive = grub_strndup (fwdevice, comma - fwdevice);
  162. new_device = grub_xasprintf ("%s%s", drive, device);
  163. grub_free (drive);
  164. }
  165. else
  166. new_device = grub_xasprintf ("%s%s", fwdevice, device);
  167. grub_free (fwdevice);
  168. grub_free (device);
  169. device = new_device;
  170. }
  171. else
  172. grub_free (fwdevice);
  173. if (fwpath && !path)
  174. {
  175. grub_size_t len = grub_strlen (fwpath);
  176. while (len > 1 && fwpath[len - 1] == '/')
  177. fwpath[--len] = 0;
  178. if (len >= sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1
  179. && grub_memcmp (fwpath + len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1), GRUB_TARGET_CPU "-" GRUB_PLATFORM,
  180. sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1) == 0)
  181. fwpath[len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1)] = 0;
  182. path = fwpath;
  183. }
  184. else
  185. grub_free (fwpath);
  186. if (device)
  187. {
  188. char *prefix_set;
  189. prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
  190. if (prefix_set)
  191. {
  192. grub_env_set ("prefix", prefix_set);
  193. grub_free (prefix_set);
  194. }
  195. grub_env_set ("root", device);
  196. }
  197. grub_free (device);
  198. grub_free (path);
  199. grub_print_error ();
  200. }
  201. /* Load the normal mode module and execute the normal mode if possible. */
  202. static void
  203. grub_load_normal_mode (void)
  204. {
  205. /* Load the module. */
  206. grub_dl_load ("normal");
  207. /* Print errors if any. */
  208. grub_print_error ();
  209. grub_errno = 0;
  210. grub_command_execute ("normal", 0, 0);
  211. }
  212. bool
  213. grub_is_cli_disabled (void)
  214. {
  215. return cli_disabled;
  216. }
  217. static void
  218. check_is_cli_disabled (void)
  219. {
  220. struct grub_module_header *header;
  221. header = 0;
  222. FOR_MODULES (header)
  223. {
  224. if (header->type == OBJ_TYPE_DISABLE_CLI)
  225. {
  226. cli_disabled = true;
  227. return;
  228. }
  229. }
  230. }
  231. static void
  232. reclaim_module_space (void)
  233. {
  234. grub_addr_t modstart, modend;
  235. if (!grub_modbase)
  236. return;
  237. #ifdef GRUB_MACHINE_PCBIOS
  238. modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
  239. #else
  240. modstart = grub_modbase;
  241. #endif
  242. modend = grub_modules_get_end ();
  243. grub_modbase = 0;
  244. #if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
  245. grub_mm_init_region ((void *) modstart, modend - modstart);
  246. #else
  247. (void) modstart;
  248. (void) modend;
  249. #endif
  250. }
  251. /* The main routine. */
  252. void __attribute__ ((noreturn))
  253. grub_main (void)
  254. {
  255. #ifdef GRUB_STACK_PROTECTOR
  256. /*
  257. * This call should only be made from a function that does not return because
  258. * functions that return will get instrumented to check that the stack cookie
  259. * does not change and this call will change the stack cookie. Thus a stack
  260. * guard failure will be triggered.
  261. */
  262. grub_update_stack_guard ();
  263. #endif
  264. /* First of all, initialize the machine. */
  265. grub_machine_init ();
  266. grub_boot_time ("After machine init.");
  267. /* This breaks flicker-free boot on EFI systems, so disable it there. */
  268. #ifndef GRUB_MACHINE_EFI
  269. /* Hello. */
  270. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  271. grub_printf ("Welcome to GRUB!\n\n");
  272. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  273. #endif
  274. /* Init verifiers API. */
  275. grub_verifiers_init ();
  276. grub_load_config ();
  277. grub_boot_time ("Before loading embedded modules.");
  278. /* Load pre-loaded modules and free the space. */
  279. grub_register_exported_symbols ();
  280. #ifdef GRUB_LINKER_HAVE_INIT
  281. grub_arch_dl_init_linker ();
  282. #endif
  283. grub_load_modules ();
  284. grub_boot_time ("After loading embedded modules.");
  285. /* Check if the CLI should be disabled */
  286. check_is_cli_disabled ();
  287. /* It is better to set the root device as soon as possible,
  288. for convenience. */
  289. grub_set_prefix_and_root ();
  290. grub_env_export ("root");
  291. grub_env_export ("prefix");
  292. /* Reclaim space used for modules. */
  293. reclaim_module_space ();
  294. grub_boot_time ("After reclaiming module space.");
  295. grub_register_core_commands ();
  296. grub_boot_time ("Before execution of embedded config.");
  297. if (load_config)
  298. grub_parser_execute (load_config);
  299. grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");
  300. grub_load_normal_mode ();
  301. grub_rescue_run ();
  302. }