main.c 7.4 KB

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