main.c 7.5 KB

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