multiboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* multiboot.c - boot a multiboot OS image. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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. /*
  20. * FIXME: The following features from the Multiboot specification still
  21. * need to be implemented:
  22. * - drives table
  23. * - ROM configuration table
  24. * - SMBIOS tables
  25. * - Networking information
  26. */
  27. #include <grub/loader.h>
  28. #include <grub/command.h>
  29. #ifdef GRUB_USE_MULTIBOOT2
  30. #include <grub/multiboot2.h>
  31. #define GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER GRUB_MULTIBOOT2_CONSOLE_FRAMEBUFFER
  32. #define GRUB_MULTIBOOT_CONSOLE_EGA_TEXT GRUB_MULTIBOOT2_CONSOLE_EGA_TEXT
  33. #define GRUB_MULTIBOOT(x) grub_multiboot2_ ## x
  34. #else
  35. #include <grub/multiboot.h>
  36. #define GRUB_MULTIBOOT(x) grub_multiboot_ ## x
  37. #endif
  38. #include <grub/cpu/multiboot.h>
  39. #include <grub/elf.h>
  40. #include <grub/aout.h>
  41. #include <grub/file.h>
  42. #include <grub/err.h>
  43. #include <grub/dl.h>
  44. #include <grub/mm.h>
  45. #include <grub/misc.h>
  46. #include <grub/env.h>
  47. #include <grub/cpu/relocator.h>
  48. #include <grub/video.h>
  49. #include <grub/memory.h>
  50. #include <grub/i18n.h>
  51. GRUB_MOD_LICENSE ("GPLv3+");
  52. #ifdef GRUB_MACHINE_EFI
  53. #include <grub/efi/efi.h>
  54. #endif
  55. struct grub_relocator *GRUB_MULTIBOOT (relocator) = NULL;
  56. grub_uint32_t GRUB_MULTIBOOT (payload_eip);
  57. #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
  58. #define DEFAULT_VIDEO_MODE "text"
  59. #else
  60. #define DEFAULT_VIDEO_MODE "auto"
  61. #endif
  62. static int accepts_video;
  63. static int accepts_ega_text;
  64. static int console_required;
  65. static grub_dl_t my_mod;
  66. /* Helper for grub_get_multiboot_mmap_count. */
  67. static int
  68. count_hook (grub_uint64_t addr __attribute__ ((unused)),
  69. grub_uint64_t size __attribute__ ((unused)),
  70. grub_memory_type_t type __attribute__ ((unused)), void *data)
  71. {
  72. grub_size_t *count = data;
  73. (*count)++;
  74. return 0;
  75. }
  76. /* Return the length of the Multiboot mmap that will be needed to allocate
  77. our platform's map. */
  78. grub_uint32_t
  79. GRUB_MULTIBOOT (get_mmap_count) (void)
  80. {
  81. grub_size_t count = 0;
  82. grub_mmap_iterate (count_hook, &count);
  83. return count;
  84. }
  85. grub_err_t
  86. GRUB_MULTIBOOT (set_video_mode) (void)
  87. {
  88. grub_err_t err;
  89. const char *modevar;
  90. #if GRUB_MACHINE_HAS_VGA_TEXT
  91. if (accepts_video)
  92. #endif
  93. {
  94. modevar = grub_env_get ("gfxpayload");
  95. if (! modevar || *modevar == 0)
  96. err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
  97. else
  98. {
  99. char *tmp;
  100. tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
  101. if (! tmp)
  102. return grub_errno;
  103. err = grub_video_set_mode (tmp, 0, 0);
  104. grub_free (tmp);
  105. }
  106. }
  107. #if GRUB_MACHINE_HAS_VGA_TEXT
  108. else
  109. err = grub_video_set_mode ("text", 0, 0);
  110. #endif
  111. return err;
  112. }
  113. #ifdef GRUB_MACHINE_EFI
  114. #ifdef __x86_64__
  115. #define grub_relocator_efi_boot grub_relocator64_efi_boot
  116. #define grub_relocator_efi_state grub_relocator64_efi_state
  117. #endif
  118. #endif
  119. #ifdef grub_relocator_efi_boot
  120. static void
  121. efi_boot (struct grub_relocator *rel,
  122. grub_uint32_t target)
  123. {
  124. #ifdef GRUB_USE_MULTIBOOT2
  125. struct grub_relocator_efi_state state_efi = MULTIBOOT2_EFI_INITIAL_STATE;
  126. #else
  127. struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
  128. #endif
  129. state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
  130. state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
  131. grub_relocator_efi_boot (rel, state_efi);
  132. }
  133. #else
  134. #define grub_efi_is_finished 1
  135. static void
  136. efi_boot (struct grub_relocator *rel __attribute__ ((unused)),
  137. grub_uint32_t target __attribute__ ((unused)))
  138. {
  139. }
  140. #endif
  141. #if defined (__i386__) || defined (__x86_64__)
  142. static void
  143. normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
  144. {
  145. grub_relocator32_boot (rel, state, 0);
  146. }
  147. #else
  148. static void
  149. normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state)
  150. {
  151. grub_relocator32_boot (rel, state);
  152. }
  153. #endif
  154. static grub_err_t
  155. grub_multiboot_boot (void)
  156. {
  157. grub_err_t err;
  158. #ifdef GRUB_USE_MULTIBOOT2
  159. struct grub_relocator32_state state = MULTIBOOT2_INITIAL_STATE;
  160. #else
  161. struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
  162. #endif
  163. state.MULTIBOOT_ENTRY_REGISTER = GRUB_MULTIBOOT (payload_eip);
  164. err = GRUB_MULTIBOOT (make_mbi) (&state.MULTIBOOT_MBI_REGISTER);
  165. if (err)
  166. return err;
  167. if (grub_efi_is_finished)
  168. normal_boot (GRUB_MULTIBOOT (relocator), state);
  169. else
  170. efi_boot (GRUB_MULTIBOOT (relocator), state.MULTIBOOT_MBI_REGISTER);
  171. /* Not reached. */
  172. return GRUB_ERR_NONE;
  173. }
  174. static grub_err_t
  175. grub_multiboot_unload (void)
  176. {
  177. GRUB_MULTIBOOT (free_mbi) ();
  178. grub_relocator_unload (GRUB_MULTIBOOT (relocator));
  179. GRUB_MULTIBOOT (relocator) = NULL;
  180. grub_dl_unref (my_mod);
  181. return GRUB_ERR_NONE;
  182. }
  183. static grub_uint64_t highest_load;
  184. #define MULTIBOOT_LOAD_ELF64
  185. #include "multiboot_elfxx.c"
  186. #undef MULTIBOOT_LOAD_ELF64
  187. #define MULTIBOOT_LOAD_ELF32
  188. #include "multiboot_elfxx.c"
  189. #undef MULTIBOOT_LOAD_ELF32
  190. /* Load ELF32 or ELF64. */
  191. grub_err_t
  192. GRUB_MULTIBOOT (load_elf) (mbi_load_data_t *mld)
  193. {
  194. if (grub_multiboot_is_elf32 (mld->buffer))
  195. return grub_multiboot_load_elf32 (mld);
  196. else if (grub_multiboot_is_elf64 (mld->buffer))
  197. return grub_multiboot_load_elf64 (mld);
  198. return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
  199. }
  200. grub_err_t
  201. GRUB_MULTIBOOT (set_console) (int console_type, int accepted_consoles,
  202. int width, int height, int depth,
  203. int console_req)
  204. {
  205. console_required = console_req;
  206. if (!(accepted_consoles
  207. & (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
  208. | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
  209. {
  210. if (console_required)
  211. return grub_error (GRUB_ERR_BAD_OS,
  212. "OS requires a console but none is available");
  213. grub_puts_ (N_("WARNING: no console will be available to OS"));
  214. accepts_video = 0;
  215. accepts_ega_text = 0;
  216. return GRUB_ERR_NONE;
  217. }
  218. if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
  219. {
  220. char *buf;
  221. if (depth && width && height)
  222. buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
  223. height, depth, width, height);
  224. else if (width && height)
  225. buf = grub_xasprintf ("%dx%d,auto", width, height);
  226. else
  227. buf = grub_strdup ("auto");
  228. if (!buf)
  229. return grub_errno;
  230. grub_env_set ("gfxpayload", buf);
  231. grub_free (buf);
  232. }
  233. else
  234. {
  235. #if GRUB_MACHINE_HAS_VGA_TEXT
  236. grub_env_set ("gfxpayload", "text");
  237. #else
  238. /* Always use video if no VGA text is available. */
  239. grub_env_set ("gfxpayload", "auto");
  240. #endif
  241. }
  242. accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
  243. accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
  244. return GRUB_ERR_NONE;
  245. }
  246. static grub_err_t
  247. grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
  248. int argc, char *argv[])
  249. {
  250. grub_file_t file = 0;
  251. grub_err_t err;
  252. grub_loader_unset ();
  253. highest_load = 0;
  254. #ifndef GRUB_USE_MULTIBOOT2
  255. grub_multiboot_quirks = GRUB_MULTIBOOT_QUIRKS_NONE;
  256. int option_found = 0;
  257. do
  258. {
  259. option_found = 0;
  260. if (argc != 0 && grub_strcmp (argv[0], "--quirk-bad-kludge") == 0)
  261. {
  262. argc--;
  263. argv++;
  264. option_found = 1;
  265. grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE;
  266. }
  267. if (argc != 0 && grub_strcmp (argv[0], "--quirk-modules-after-kernel") == 0)
  268. {
  269. argc--;
  270. argv++;
  271. option_found = 1;
  272. grub_multiboot_quirks |= GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL;
  273. }
  274. } while (option_found);
  275. #endif
  276. if (argc == 0)
  277. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  278. file = grub_file_open (argv[0], GRUB_FILE_TYPE_MULTIBOOT_KERNEL);
  279. if (! file)
  280. return grub_errno;
  281. grub_dl_ref (my_mod);
  282. /* Skip filename. */
  283. GRUB_MULTIBOOT (init_mbi) (argc - 1, argv + 1);
  284. grub_relocator_unload (GRUB_MULTIBOOT (relocator));
  285. GRUB_MULTIBOOT (relocator) = grub_relocator_new ();
  286. if (!GRUB_MULTIBOOT (relocator))
  287. goto fail;
  288. err = GRUB_MULTIBOOT (load) (file, argv[0]);
  289. if (err)
  290. goto fail;
  291. GRUB_MULTIBOOT (set_bootdev) ();
  292. grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
  293. fail:
  294. if (file)
  295. grub_file_close (file);
  296. if (grub_errno != GRUB_ERR_NONE)
  297. {
  298. grub_relocator_unload (GRUB_MULTIBOOT (relocator));
  299. GRUB_MULTIBOOT (relocator) = NULL;
  300. grub_dl_unref (my_mod);
  301. }
  302. return grub_errno;
  303. }
  304. static grub_err_t
  305. grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
  306. int argc, char *argv[])
  307. {
  308. grub_file_t file = 0;
  309. grub_ssize_t size;
  310. void *module = NULL;
  311. grub_addr_t target;
  312. grub_err_t err;
  313. int nounzip = 0;
  314. grub_uint64_t lowest_addr = 0;
  315. if (argc == 0)
  316. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  317. if (grub_strcmp (argv[0], "--nounzip") == 0)
  318. {
  319. argv++;
  320. argc--;
  321. nounzip = 1;
  322. }
  323. if (argc == 0)
  324. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  325. if (!GRUB_MULTIBOOT (relocator))
  326. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  327. N_("you need to load the kernel first"));
  328. file = grub_file_open (argv[0], GRUB_FILE_TYPE_MULTIBOOT_MODULE
  329. | (nounzip ? GRUB_FILE_TYPE_NO_DECOMPRESS : GRUB_FILE_TYPE_NONE));
  330. if (! file)
  331. return grub_errno;
  332. #ifndef GRUB_USE_MULTIBOOT2
  333. lowest_addr = 0x100000;
  334. if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_MODULES_AFTER_KERNEL)
  335. lowest_addr = ALIGN_UP (highest_load + 1048576, 4096);
  336. #endif
  337. size = grub_file_size (file);
  338. if (size)
  339. {
  340. grub_relocator_chunk_t ch;
  341. err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
  342. lowest_addr, UP_TO_TOP32 (size),
  343. size, MULTIBOOT_MOD_ALIGN,
  344. GRUB_RELOCATOR_PREFERENCE_NONE, 1);
  345. if (err)
  346. {
  347. grub_file_close (file);
  348. return err;
  349. }
  350. module = get_virtual_current_address (ch);
  351. target = get_physical_target_address (ch);
  352. }
  353. else
  354. {
  355. module = 0;
  356. target = 0;
  357. }
  358. if (size && grub_file_read (file, module, size) != size)
  359. {
  360. grub_file_close (file);
  361. if (!grub_errno)
  362. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  363. argv[0]);
  364. return grub_errno;
  365. }
  366. grub_file_close (file);
  367. return GRUB_MULTIBOOT (add_module) (target, size, argc - 1, argv + 1);
  368. }
  369. static grub_command_t cmd_multiboot, cmd_module;
  370. GRUB_MOD_INIT(multiboot)
  371. {
  372. cmd_multiboot =
  373. #ifdef GRUB_USE_MULTIBOOT2
  374. grub_register_command ("multiboot2", grub_cmd_multiboot,
  375. 0, N_("Load a multiboot 2 kernel."));
  376. cmd_module =
  377. grub_register_command ("module2", grub_cmd_module,
  378. 0, N_("Load a multiboot 2 module."));
  379. #else
  380. grub_register_command ("multiboot", grub_cmd_multiboot,
  381. 0, N_("Load a multiboot kernel."));
  382. cmd_module =
  383. grub_register_command ("module", grub_cmd_module,
  384. 0, N_("Load a multiboot module."));
  385. #endif
  386. my_mod = mod;
  387. }
  388. GRUB_MOD_FINI(multiboot)
  389. {
  390. grub_unregister_command (cmd_multiboot);
  391. grub_unregister_command (cmd_module);
  392. }