linux.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* linux.c - boot Linux */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2004,2005,2007,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/elf.h>
  20. #include <grub/elfload.h>
  21. #include <grub/loader.h>
  22. #include <grub/dl.h>
  23. #include <grub/mm.h>
  24. #include <grub/misc.h>
  25. #include <grub/ieee1275/ieee1275.h>
  26. #include <grub/machine/loader.h>
  27. #include <grub/command.h>
  28. #include <grub/i18n.h>
  29. #define ELF32_LOADMASK (0xc0000000UL)
  30. #define ELF64_LOADMASK (0xc000000000000000ULL)
  31. static grub_dl_t my_mod;
  32. static int loaded;
  33. static grub_addr_t initrd_addr;
  34. static grub_size_t initrd_size;
  35. static grub_addr_t linux_addr;
  36. static grub_size_t linux_size;
  37. static char *linux_args;
  38. typedef void (*kernel_entry_t) (void *, unsigned long, int (void *),
  39. unsigned long, unsigned long);
  40. static grub_err_t
  41. grub_linux_boot (void)
  42. {
  43. kernel_entry_t linuxmain;
  44. grub_ssize_t actual;
  45. /* Set the command line arguments. */
  46. grub_ieee1275_set_property (grub_ieee1275_chosen, "bootargs", linux_args,
  47. grub_strlen (linux_args) + 1, &actual);
  48. grub_dprintf ("loader", "Entry point: 0x%x\n", linux_addr);
  49. grub_dprintf ("loader", "Initrd at: 0x%x, size 0x%x\n", initrd_addr,
  50. initrd_size);
  51. grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
  52. grub_dprintf ("loader", "Jumping to Linux...\n");
  53. /* Boot the kernel. */
  54. linuxmain = (kernel_entry_t) linux_addr;
  55. linuxmain ((void *) initrd_addr, initrd_size, grub_ieee1275_entry_fn, 0, 0);
  56. return GRUB_ERR_NONE;
  57. }
  58. static grub_err_t
  59. grub_linux_release_mem (void)
  60. {
  61. grub_free (linux_args);
  62. linux_args = 0;
  63. if (linux_addr && grub_ieee1275_release (linux_addr, linux_size))
  64. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
  65. if (initrd_addr && grub_ieee1275_release (initrd_addr, initrd_size))
  66. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot release memory");
  67. linux_addr = 0;
  68. initrd_addr = 0;
  69. return GRUB_ERR_NONE;
  70. }
  71. static grub_err_t
  72. grub_linux_unload (void)
  73. {
  74. grub_err_t err;
  75. err = grub_linux_release_mem ();
  76. grub_dl_unref (my_mod);
  77. loaded = 0;
  78. return err;
  79. }
  80. static grub_err_t
  81. grub_linux_load32_offset_phdr (Elf32_Phdr *phdr, grub_addr_t *addr,
  82. int *do_load,
  83. void *closure __attribute__ ((unused)))
  84. {
  85. if (phdr->p_type != PT_LOAD)
  86. {
  87. *do_load = 0;
  88. return 0;
  89. }
  90. *do_load = 1;
  91. /* Linux's program headers incorrectly contain virtual addresses.
  92. * Translate those to physical, and offset to the area we claimed. */
  93. *addr = (phdr->p_paddr & ~ELF32_LOADMASK) + linux_addr;
  94. return 0;
  95. }
  96. static grub_err_t
  97. grub_linux_load32 (grub_elf_t elf)
  98. {
  99. Elf32_Addr entry;
  100. int found_addr = 0;
  101. /* Linux's entry point incorrectly contains a virtual address. */
  102. entry = elf->ehdr.ehdr32.e_entry & ~ELF32_LOADMASK;
  103. if (entry == 0)
  104. entry = 0x01400000;
  105. linux_size = grub_elf32_size (elf, 0);
  106. if (linux_size == 0)
  107. return grub_errno;
  108. /* Pad it; the kernel scribbles over memory beyond its load address. */
  109. linux_size += 0x100000;
  110. /* On some systems, firmware occupies the memory we're trying to use.
  111. * Happily, Linux can be loaded anywhere (it relocates itself). Iterate
  112. * until we find an open area. */
  113. for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
  114. {
  115. grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
  116. linux_addr, linux_size);
  117. found_addr = grub_claimmap (linux_addr, linux_size);
  118. if (found_addr != -1)
  119. break;
  120. }
  121. if (found_addr == -1)
  122. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
  123. /* Now load the segments into the area we claimed. */
  124. return grub_elf32_load (elf, grub_linux_load32_offset_phdr, 0, 0, 0);
  125. }
  126. static grub_err_t
  127. grub_linux_load64_offset_phdr (Elf64_Phdr *phdr, grub_addr_t *addr,
  128. int *do_load,
  129. void *closure __attribute__((unused)))
  130. {
  131. if (phdr->p_type != PT_LOAD)
  132. {
  133. *do_load = 0;
  134. return 0;
  135. }
  136. *do_load = 1;
  137. /* Linux's program headers incorrectly contain virtual addresses.
  138. * Translate those to physical, and offset to the area we claimed. */
  139. *addr = (phdr->p_paddr & ~ELF64_LOADMASK) + linux_addr;
  140. return 0;
  141. }
  142. static grub_err_t
  143. grub_linux_load64 (grub_elf_t elf)
  144. {
  145. Elf64_Addr entry;
  146. int found_addr = 0;
  147. /* Linux's entry point incorrectly contains a virtual address. */
  148. entry = elf->ehdr.ehdr64.e_entry & ~ELF64_LOADMASK;
  149. if (entry == 0)
  150. entry = 0x01400000;
  151. linux_size = grub_elf64_size (elf, 0);
  152. if (linux_size == 0)
  153. return grub_errno;
  154. /* Pad it; the kernel scribbles over memory beyond its load address. */
  155. linux_size += 0x100000;
  156. /* On some systems, firmware occupies the memory we're trying to use.
  157. * Happily, Linux can be loaded anywhere (it relocates itself). Iterate
  158. * until we find an open area. */
  159. for (linux_addr = entry; linux_addr < entry + 200 * 0x100000; linux_addr += 0x100000)
  160. {
  161. grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
  162. linux_addr, linux_size);
  163. found_addr = grub_claimmap (linux_addr, linux_size);
  164. if (found_addr != -1)
  165. break;
  166. }
  167. if (found_addr == -1)
  168. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't claim memory");
  169. /* Now load the segments into the area we claimed. */
  170. return grub_elf64_load (elf, grub_linux_load64_offset_phdr, 0, 0, 0);
  171. }
  172. static grub_err_t
  173. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  174. int argc, char *argv[])
  175. {
  176. grub_elf_t elf = 0;
  177. int i;
  178. int size;
  179. char *dest;
  180. grub_dl_ref (my_mod);
  181. if (argc == 0)
  182. {
  183. grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
  184. goto out;
  185. }
  186. elf = grub_elf_open (argv[0]);
  187. if (! elf)
  188. goto out;
  189. if (elf->ehdr.ehdr32.e_type != ET_EXEC)
  190. {
  191. grub_error (GRUB_ERR_UNKNOWN_OS,
  192. "this ELF file is not of the right type");
  193. goto out;
  194. }
  195. /* Release the previously used memory. */
  196. grub_loader_unset ();
  197. if (grub_elf_is_elf32 (elf))
  198. grub_linux_load32 (elf);
  199. else
  200. if (grub_elf_is_elf64 (elf))
  201. grub_linux_load64 (elf);
  202. else
  203. {
  204. grub_error (GRUB_ERR_BAD_FILE_TYPE, "unknown ELF class");
  205. goto out;
  206. }
  207. size = sizeof ("BOOT_IMAGE=") + grub_strlen (argv[0]);
  208. for (i = 0; i < argc; i++)
  209. size += grub_strlen (argv[i]) + 1;
  210. linux_args = grub_malloc (size);
  211. if (! linux_args)
  212. goto out;
  213. /* Specify the boot file. */
  214. dest = grub_stpcpy (linux_args, "BOOT_IMAGE=");
  215. dest = grub_stpcpy (dest, argv[0]);
  216. for (i = 1; i < argc; i++)
  217. {
  218. *dest++ = ' ';
  219. dest = grub_stpcpy (dest, argv[i]);
  220. }
  221. out:
  222. if (elf)
  223. grub_elf_close (elf);
  224. if (grub_errno != GRUB_ERR_NONE)
  225. {
  226. grub_linux_release_mem ();
  227. grub_dl_unref (my_mod);
  228. loaded = 0;
  229. }
  230. else
  231. {
  232. grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
  233. initrd_addr = 0;
  234. loaded = 1;
  235. }
  236. return grub_errno;
  237. }
  238. static grub_err_t
  239. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  240. int argc, char *argv[])
  241. {
  242. grub_file_t file = 0;
  243. grub_ssize_t size;
  244. grub_addr_t first_addr;
  245. grub_addr_t addr;
  246. int found_addr = 0;
  247. if (argc == 0)
  248. {
  249. grub_error (GRUB_ERR_BAD_ARGUMENT, "no initrd specified");
  250. goto fail;
  251. }
  252. if (!loaded)
  253. {
  254. grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
  255. goto fail;
  256. }
  257. file = grub_file_open (argv[0]);
  258. if (! file)
  259. goto fail;
  260. first_addr = linux_addr + linux_size;
  261. size = grub_file_size (file);
  262. /* Attempt to claim at a series of addresses until successful in
  263. the same way that grub_rescue_cmd_linux does. */
  264. for (addr = first_addr; addr < first_addr + 200 * 0x100000; addr += 0x100000)
  265. {
  266. grub_dprintf ("loader", "Attempting to claim at 0x%x, size 0x%x.\n",
  267. addr, size);
  268. found_addr = grub_claimmap (addr, size);
  269. if (found_addr != -1)
  270. break;
  271. }
  272. if (found_addr == -1)
  273. {
  274. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot claim memory");
  275. goto fail;
  276. }
  277. grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size);
  278. if (grub_file_read (file, (void *) addr, size) != size)
  279. {
  280. grub_ieee1275_release (addr, size);
  281. grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
  282. goto fail;
  283. }
  284. initrd_addr = addr;
  285. initrd_size = size;
  286. fail:
  287. if (file)
  288. grub_file_close (file);
  289. return grub_errno;
  290. }
  291. static grub_command_t cmd_linux, cmd_initrd;
  292. GRUB_MOD_INIT(linux)
  293. {
  294. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  295. 0, N_("Load Linux."));
  296. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  297. 0, N_("Load initrd."));
  298. my_mod = mod;
  299. }
  300. GRUB_MOD_FINI(linux)
  301. {
  302. grub_unregister_command (cmd_linux);
  303. grub_unregister_command (cmd_initrd);
  304. }