multiboot.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. * - VBE support
  23. * - symbol table
  24. * - drives table
  25. * - ROM configuration table
  26. * - APM table
  27. */
  28. #include <grub/loader.h>
  29. #include <grub/command.h>
  30. #include <grub/machine/loader.h>
  31. #include <grub/multiboot.h>
  32. #include <grub/cpu/multiboot.h>
  33. #include <grub/elf.h>
  34. #include <grub/aout.h>
  35. #include <grub/file.h>
  36. #include <grub/err.h>
  37. #include <grub/dl.h>
  38. #include <grub/mm.h>
  39. #include <grub/misc.h>
  40. #include <grub/gzio.h>
  41. #include <grub/env.h>
  42. #include <grub/cpu/relocator.h>
  43. #include <grub/video.h>
  44. #include <grub/memory.h>
  45. #include <grub/i18n.h>
  46. #ifdef GRUB_MACHINE_EFI
  47. #include <grub/efi/efi.h>
  48. #endif
  49. #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_MULTIBOOT) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
  50. #define DEFAULT_VIDEO_MODE "text"
  51. #else
  52. #define DEFAULT_VIDEO_MODE "auto"
  53. #endif
  54. grub_size_t grub_multiboot_alloc_mbi;
  55. char *grub_multiboot_payload_orig;
  56. grub_addr_t grub_multiboot_payload_dest;
  57. grub_size_t grub_multiboot_pure_size;
  58. grub_uint32_t grub_multiboot_payload_eip;
  59. static int accepts_video;
  60. static int accepts_ega_text;
  61. static int console_required;
  62. static grub_dl_t my_mod;
  63. static int
  64. hook (grub_uint64_t addr __attribute__ ((unused)),
  65. grub_uint64_t size __attribute__ ((unused)),
  66. grub_uint32_t type __attribute__ ((unused)),
  67. void *closure)
  68. {
  69. grub_size_t *count = closure;
  70. (*count)++;
  71. return 0;
  72. }
  73. /* Return the length of the Multiboot mmap that will be needed to allocate
  74. our platform's map. */
  75. grub_uint32_t
  76. grub_get_multiboot_mmap_count (void)
  77. {
  78. grub_size_t count = 0;
  79. grub_mmap_iterate (hook, &count);
  80. return count;
  81. }
  82. grub_err_t
  83. grub_multiboot_set_video_mode (void)
  84. {
  85. grub_err_t err;
  86. const char *modevar;
  87. if (accepts_video || !GRUB_MACHINE_HAS_VGA_TEXT)
  88. {
  89. modevar = grub_env_get ("gfxpayload");
  90. if (! modevar || *modevar == 0)
  91. err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
  92. else
  93. {
  94. char *tmp;
  95. tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
  96. if (! tmp)
  97. return grub_errno;
  98. err = grub_video_set_mode (tmp, 0, 0);
  99. grub_free (tmp);
  100. }
  101. }
  102. else
  103. err = grub_video_set_mode ("text", 0, 0);
  104. return err;
  105. }
  106. static grub_err_t
  107. grub_multiboot_boot (void)
  108. {
  109. grub_size_t mbi_size;
  110. grub_err_t err;
  111. struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
  112. state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
  113. mbi_size = grub_multiboot_get_mbi_size ();
  114. if (grub_multiboot_alloc_mbi < mbi_size)
  115. {
  116. grub_multiboot_payload_orig
  117. = grub_relocator32_realloc (grub_multiboot_payload_orig,
  118. grub_multiboot_pure_size + mbi_size);
  119. if (!grub_multiboot_payload_orig)
  120. return grub_errno;
  121. grub_multiboot_alloc_mbi = mbi_size;
  122. }
  123. state.MULTIBOOT_MBI_REGISTER = grub_multiboot_payload_dest
  124. + grub_multiboot_pure_size;
  125. err = grub_multiboot_make_mbi (grub_multiboot_payload_orig,
  126. grub_multiboot_payload_dest,
  127. grub_multiboot_pure_size, mbi_size);
  128. if (err)
  129. return err;
  130. #ifdef GRUB_MACHINE_EFI
  131. if (! grub_efi_finish_boot_services ())
  132. grub_fatal ("cannot exit boot services");
  133. #endif
  134. grub_relocator32_boot (grub_multiboot_payload_orig,
  135. grub_multiboot_payload_dest,
  136. state);
  137. /* Not reached. */
  138. return GRUB_ERR_NONE;
  139. }
  140. static grub_err_t
  141. grub_multiboot_unload (void)
  142. {
  143. grub_multiboot_free_mbi ();
  144. grub_relocator32_free (grub_multiboot_payload_orig);
  145. grub_multiboot_alloc_mbi = 0;
  146. grub_multiboot_payload_orig = NULL;
  147. grub_dl_unref (my_mod);
  148. return GRUB_ERR_NONE;
  149. }
  150. #define MULTIBOOT_LOAD_ELF64
  151. #include "multiboot_elfxx.c"
  152. #undef MULTIBOOT_LOAD_ELF64
  153. #define MULTIBOOT_LOAD_ELF32
  154. #include "multiboot_elfxx.c"
  155. #undef MULTIBOOT_LOAD_ELF32
  156. /* Load ELF32 or ELF64. */
  157. grub_err_t
  158. grub_multiboot_load_elf (grub_file_t file, void *buffer)
  159. {
  160. if (grub_multiboot_is_elf32 (buffer))
  161. return grub_multiboot_load_elf32 (file, buffer);
  162. else if (grub_multiboot_is_elf64 (buffer))
  163. return grub_multiboot_load_elf64 (file, buffer);
  164. return grub_error (GRUB_ERR_UNKNOWN_OS, "unknown ELF class");
  165. }
  166. grub_err_t
  167. grub_multiboot_set_console (int console_type, int accepted_consoles,
  168. int width, int height, int depth,
  169. int console_req)
  170. {
  171. console_required = console_req;
  172. if (!(accepted_consoles
  173. & (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER
  174. | (GRUB_MACHINE_HAS_VGA_TEXT ? GRUB_MULTIBOOT_CONSOLE_EGA_TEXT : 0))))
  175. {
  176. if (console_required)
  177. return grub_error (GRUB_ERR_BAD_OS,
  178. "OS requires a console but none is available");
  179. grub_printf ("WARNING: no console will be available to OS");
  180. accepts_video = 0;
  181. accepts_ega_text = 0;
  182. return GRUB_ERR_NONE;
  183. }
  184. if (console_type == GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER)
  185. {
  186. char *buf;
  187. if (depth && width && height)
  188. buf = grub_xasprintf ("%dx%dx%d,%dx%d,auto", width,
  189. height, depth, width, height);
  190. else if (width && height)
  191. buf = grub_xasprintf ("%dx%d,auto", width, height);
  192. else
  193. buf = grub_strdup ("auto");
  194. if (!buf)
  195. return grub_errno;
  196. grub_env_set ("gfxpayload", buf);
  197. grub_free (buf);
  198. }
  199. else
  200. grub_env_set ("gfxpayload", "text");
  201. accepts_video = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER);
  202. accepts_ega_text = !!(accepted_consoles & GRUB_MULTIBOOT_CONSOLE_EGA_TEXT);
  203. return GRUB_ERR_NONE;
  204. }
  205. static grub_err_t
  206. grub_cmd_multiboot (grub_command_t cmd __attribute__ ((unused)),
  207. int argc, char *argv[])
  208. {
  209. grub_file_t file = 0;
  210. grub_err_t err;
  211. grub_loader_unset ();
  212. if (argc == 0)
  213. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
  214. file = grub_gzfile_open (argv[0], 1);
  215. if (! file)
  216. return grub_error (GRUB_ERR_BAD_ARGUMENT, "couldn't open file");
  217. grub_dl_ref (my_mod);
  218. /* Skip filename. */
  219. grub_multiboot_init_mbi (argc - 1, argv + 1);
  220. grub_relocator32_free (grub_multiboot_payload_orig);
  221. grub_multiboot_payload_orig = NULL;
  222. err = grub_multiboot_load (file);
  223. if (err)
  224. goto fail;
  225. grub_multiboot_set_bootdev ();
  226. grub_loader_set (grub_multiboot_boot, grub_multiboot_unload, 0);
  227. fail:
  228. if (file)
  229. grub_file_close (file);
  230. if (grub_errno != GRUB_ERR_NONE)
  231. {
  232. grub_relocator32_free (grub_multiboot_payload_orig);
  233. grub_multiboot_free_mbi ();
  234. grub_dl_unref (my_mod);
  235. }
  236. return grub_errno;
  237. }
  238. static grub_err_t
  239. grub_cmd_module (grub_command_t cmd __attribute__ ((unused)),
  240. int argc, char *argv[])
  241. {
  242. grub_file_t file = 0;
  243. grub_ssize_t size;
  244. char *module = 0;
  245. grub_err_t err;
  246. if (argc == 0)
  247. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
  248. if (!grub_multiboot_payload_orig)
  249. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  250. "you need to load the multiboot kernel first");
  251. file = grub_gzfile_open (argv[0], 1);
  252. if (! file)
  253. return grub_errno;
  254. size = grub_file_size (file);
  255. module = grub_memalign (MULTIBOOT_MOD_ALIGN, size);
  256. if (! module)
  257. {
  258. grub_file_close (file);
  259. return grub_errno;
  260. }
  261. err = grub_multiboot_add_module ((grub_addr_t) module, size,
  262. argc - 1, argv + 1);
  263. if (err)
  264. {
  265. grub_file_close (file);
  266. return err;
  267. }
  268. if (grub_file_read (file, module, size) != size)
  269. {
  270. grub_file_close (file);
  271. return grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
  272. }
  273. grub_file_close (file);
  274. return GRUB_ERR_NONE;;
  275. }
  276. static grub_command_t cmd_multiboot, cmd_module;
  277. GRUB_MOD_INIT(multiboot)
  278. {
  279. cmd_multiboot =
  280. #ifdef GRUB_USE_MULTIBOOT2
  281. grub_register_command ("multiboot2", grub_cmd_multiboot,
  282. 0, N_("Load a multiboot 2 kernel."));
  283. cmd_module =
  284. grub_register_command ("module2", grub_cmd_module,
  285. 0, N_("Load a multiboot 2 module."));
  286. #else
  287. grub_register_command ("multiboot", grub_cmd_multiboot,
  288. 0, N_("Load a multiboot kernel."));
  289. cmd_module =
  290. grub_register_command ("module", grub_cmd_module,
  291. 0, N_("Load a multiboot module."));
  292. #endif
  293. my_mod = mod;
  294. }
  295. GRUB_MOD_FINI(multiboot)
  296. {
  297. grub_unregister_command (cmd_multiboot);
  298. grub_unregister_command (cmd_module);
  299. }