linux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/command.h>
  27. #include <grub/i18n.h>
  28. #include <grub/memory.h>
  29. #include <grub/lib/cmdline.h>
  30. #include <grub/linux.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. static grub_dl_t my_mod;
  33. static int loaded;
  34. /* /virtual-memory/translations property layout */
  35. struct grub_ieee1275_translation {
  36. grub_uint64_t vaddr;
  37. grub_uint64_t size;
  38. grub_uint64_t data;
  39. };
  40. static struct grub_ieee1275_translation *of_trans;
  41. static int of_num_trans;
  42. static grub_addr_t phys_base;
  43. static grub_addr_t grub_phys_start;
  44. static grub_addr_t grub_phys_end;
  45. static grub_addr_t initrd_addr;
  46. static grub_addr_t initrd_paddr;
  47. static grub_size_t initrd_size;
  48. static Elf64_Addr linux_entry;
  49. static grub_addr_t linux_addr;
  50. static grub_addr_t linux_paddr;
  51. static grub_size_t linux_size;
  52. static char *linux_args;
  53. struct linux_bootstr_info {
  54. int len, valid;
  55. char buf[];
  56. };
  57. struct linux_hdrs {
  58. /* All HdrS versions support these fields. */
  59. unsigned int start_insns[2];
  60. char magic[4]; /* "HdrS" */
  61. unsigned int linux_kernel_version; /* LINUX_VERSION_CODE */
  62. unsigned short hdrs_version;
  63. unsigned short root_flags;
  64. unsigned short root_dev;
  65. unsigned short ram_flags;
  66. unsigned int __deprecated_ramdisk_image;
  67. unsigned int ramdisk_size;
  68. /* HdrS versions 0x0201 and higher only */
  69. char *reboot_command;
  70. /* HdrS versions 0x0202 and higher only */
  71. struct linux_bootstr_info *bootstr_info;
  72. /* HdrS versions 0x0301 and higher only */
  73. unsigned long ramdisk_image;
  74. };
  75. static grub_err_t
  76. grub_linux_boot (void)
  77. {
  78. struct linux_bootstr_info *bp;
  79. struct linux_hdrs *hp;
  80. grub_addr_t addr;
  81. hp = (struct linux_hdrs *) linux_addr;
  82. /* Any pointer we dereference in the kernel image must be relocated
  83. to where we actually loaded the kernel. */
  84. addr = (grub_addr_t) hp->bootstr_info;
  85. addr += (linux_addr - linux_entry);
  86. bp = (struct linux_bootstr_info *) addr;
  87. /* Set the command line arguments, unless the kernel has been
  88. built with a fixed CONFIG_CMDLINE. */
  89. if (!bp->valid)
  90. {
  91. int len = grub_strlen (linux_args) + 1;
  92. if (bp->len < len)
  93. len = bp->len;
  94. grub_memcpy(bp->buf, linux_args, len);
  95. bp->buf[len-1] = '\0';
  96. bp->valid = 1;
  97. }
  98. if (initrd_addr)
  99. {
  100. /* The kernel expects the physical address, adjusted relative
  101. to the lowest address advertised in "/memory"'s available
  102. property.
  103. The history of this is that back when the kernel only supported
  104. specifying a 32-bit ramdisk address, this was the way to still
  105. be able to specify the ramdisk physical address even if memory
  106. started at some place above 4GB.
  107. The magic 0x400000 is KERNBASE, I have no idea why SILO adds
  108. that term into the address, but it does and thus we have to do
  109. it too as this is what the kernel expects. */
  110. hp->ramdisk_image = initrd_paddr - phys_base + 0x400000;
  111. hp->ramdisk_size = initrd_size;
  112. }
  113. grub_dprintf ("loader", "Entry point: 0x%lx\n", linux_addr);
  114. grub_dprintf ("loader", "Initrd at: 0x%lx, size 0x%lx\n", initrd_addr,
  115. initrd_size);
  116. grub_dprintf ("loader", "Boot arguments: %s\n", linux_args);
  117. grub_dprintf ("loader", "Jumping to Linux...\n");
  118. /* Boot the kernel. */
  119. asm volatile ("ldx %0, %%o4\n"
  120. "ldx %1, %%o6\n"
  121. "ldx %2, %%o5\n"
  122. "mov %%g0, %%o0\n"
  123. "mov %%g0, %%o2\n"
  124. "mov %%g0, %%o3\n"
  125. "jmp %%o5\n"
  126. "mov %%g0, %%o1\n": :
  127. "m"(grub_ieee1275_entry_fn),
  128. "m"(grub_ieee1275_original_stack),
  129. "m"(linux_addr));
  130. return GRUB_ERR_NONE;
  131. }
  132. static grub_err_t
  133. grub_linux_release_mem (void)
  134. {
  135. grub_free (linux_args);
  136. linux_args = 0;
  137. linux_addr = 0;
  138. initrd_addr = 0;
  139. return GRUB_ERR_NONE;
  140. }
  141. static grub_err_t
  142. grub_linux_unload (void)
  143. {
  144. grub_err_t err;
  145. err = grub_linux_release_mem ();
  146. grub_dl_unref (my_mod);
  147. loaded = 0;
  148. return err;
  149. }
  150. #define FOUR_MB (4 * 1024 * 1024)
  151. /* Context for alloc_phys. */
  152. struct alloc_phys_ctx
  153. {
  154. grub_addr_t size;
  155. grub_addr_t ret;
  156. };
  157. /* Helper for alloc_phys. */
  158. static int
  159. alloc_phys_choose (grub_uint64_t addr, grub_uint64_t len,
  160. grub_memory_type_t type, void *data)
  161. {
  162. struct alloc_phys_ctx *ctx = data;
  163. grub_addr_t end = addr + len;
  164. if (type != GRUB_MEMORY_AVAILABLE)
  165. return 0;
  166. addr = ALIGN_UP (addr, FOUR_MB);
  167. if (addr + ctx->size >= end)
  168. return 0;
  169. /* OBP available region contains grub. Start at grub_phys_end. */
  170. /* grub_phys_start does not start at the beginning of the memory region */
  171. if ((grub_phys_start >= addr && grub_phys_end < end) ||
  172. (addr > grub_phys_start && addr < grub_phys_end))
  173. {
  174. addr = ALIGN_UP (grub_phys_end, FOUR_MB);
  175. if (addr + ctx->size >= end)
  176. return 0;
  177. }
  178. grub_dprintf("loader",
  179. "addr = 0x%lx grub_phys_start = 0x%lx grub_phys_end = 0x%lx\n",
  180. addr, grub_phys_start, grub_phys_end);
  181. if (loaded)
  182. {
  183. grub_addr_t linux_end = ALIGN_UP (linux_paddr + linux_size, FOUR_MB);
  184. if (addr >= linux_paddr && addr < linux_end)
  185. {
  186. addr = linux_end;
  187. if (addr + ctx->size >= end)
  188. return 0;
  189. }
  190. if ((addr + ctx->size) >= linux_paddr
  191. && (addr + ctx->size) < linux_end)
  192. {
  193. addr = linux_end;
  194. if (addr + ctx->size >= end)
  195. return 0;
  196. }
  197. }
  198. ctx->ret = addr;
  199. return 1;
  200. }
  201. static grub_addr_t
  202. alloc_phys (grub_addr_t size)
  203. {
  204. struct alloc_phys_ctx ctx = {
  205. .size = size,
  206. .ret = (grub_addr_t) -1
  207. };
  208. grub_machine_mmap_iterate (alloc_phys_choose, &ctx);
  209. return ctx.ret;
  210. }
  211. static grub_err_t
  212. grub_linux_load64 (grub_elf_t elf, const char *filename)
  213. {
  214. grub_addr_t off, paddr, base;
  215. int ret;
  216. linux_entry = elf->ehdr.ehdr64.e_entry;
  217. linux_addr = 0x40004000;
  218. off = 0x4000;
  219. linux_size = grub_elf64_size (elf, 0, 0);
  220. if (linux_size == 0)
  221. return grub_errno;
  222. grub_dprintf ("loader", "Attempting to claim at 0x%lx, size 0x%lx.\n",
  223. linux_addr, linux_size);
  224. paddr = alloc_phys (linux_size + off);
  225. if (paddr == (grub_addr_t) -1)
  226. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  227. "couldn't allocate physical memory");
  228. ret = grub_ieee1275_map (paddr, linux_addr - off,
  229. linux_size + off, IEEE1275_MAP_DEFAULT);
  230. if (ret)
  231. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  232. "couldn't map physical memory");
  233. grub_dprintf ("loader", "Loading Linux at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
  234. linux_addr, paddr, linux_size);
  235. linux_paddr = paddr;
  236. base = linux_entry - off;
  237. /* Now load the segments into the area we claimed. */
  238. return grub_elf64_load (elf, filename, (void *) (linux_addr - off - base), GRUB_ELF_LOAD_FLAGS_NONE, 0, 0);
  239. }
  240. static grub_err_t
  241. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  242. int argc, char *argv[])
  243. {
  244. grub_file_t file = 0;
  245. grub_elf_t elf = 0;
  246. int size;
  247. grub_dl_ref (my_mod);
  248. if (argc == 0)
  249. {
  250. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  251. goto out;
  252. }
  253. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
  254. if (!file)
  255. goto out;
  256. elf = grub_elf_file (file, argv[0]);
  257. if (! elf)
  258. goto out;
  259. if (elf->ehdr.ehdr32.e_type != ET_EXEC)
  260. {
  261. grub_error (GRUB_ERR_UNKNOWN_OS,
  262. N_("this ELF file is not of the right type"));
  263. goto out;
  264. }
  265. /* Release the previously used memory. */
  266. grub_loader_unset ();
  267. if (grub_elf_is_elf64 (elf))
  268. grub_linux_load64 (elf, argv[0]);
  269. else
  270. {
  271. grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid arch-dependent ELF magic"));
  272. goto out;
  273. }
  274. size = grub_loader_cmdline_size(argc, argv);
  275. linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
  276. if (! linux_args)
  277. goto out;
  278. /* Create kernel command line. */
  279. grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  280. if (grub_create_loader_cmdline (argc, argv, linux_args + sizeof (LINUX_IMAGE) - 1,
  281. size, GRUB_VERIFY_KERNEL_CMDLINE))
  282. goto out;
  283. out:
  284. if (elf)
  285. grub_elf_close (elf);
  286. else if (file)
  287. grub_file_close (file);
  288. if (grub_errno != GRUB_ERR_NONE)
  289. {
  290. grub_linux_release_mem ();
  291. grub_dl_unref (my_mod);
  292. loaded = 0;
  293. }
  294. else
  295. {
  296. grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
  297. initrd_addr = 0;
  298. loaded = 1;
  299. }
  300. return grub_errno;
  301. }
  302. static grub_err_t
  303. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  304. int argc, char *argv[])
  305. {
  306. grub_size_t size = 0;
  307. grub_addr_t paddr;
  308. grub_addr_t addr;
  309. int ret;
  310. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  311. if (argc == 0)
  312. {
  313. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  314. goto fail;
  315. }
  316. if (!loaded)
  317. {
  318. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
  319. goto fail;
  320. }
  321. if (grub_initrd_init (argc, argv, &initrd_ctx))
  322. goto fail;
  323. size = grub_get_initrd_size (&initrd_ctx);
  324. addr = 0x60000000;
  325. paddr = alloc_phys (size);
  326. if (paddr == (grub_addr_t) -1)
  327. {
  328. grub_error (GRUB_ERR_OUT_OF_MEMORY,
  329. "couldn't allocate physical memory");
  330. goto fail;
  331. }
  332. ret = grub_ieee1275_map (paddr, addr, size, IEEE1275_MAP_DEFAULT);
  333. if (ret)
  334. {
  335. grub_error (GRUB_ERR_OUT_OF_MEMORY,
  336. "couldn't map physical memory");
  337. goto fail;
  338. }
  339. grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n",
  340. addr, paddr, size);
  341. if (grub_initrd_load (&initrd_ctx, (void *) addr))
  342. goto fail;
  343. initrd_addr = addr;
  344. initrd_paddr = paddr;
  345. initrd_size = size;
  346. fail:
  347. grub_initrd_close (&initrd_ctx);
  348. return grub_errno;
  349. }
  350. /* Helper for determine_phys_base. */
  351. static int
  352. get_physbase (grub_uint64_t addr, grub_uint64_t len __attribute__ ((unused)),
  353. grub_memory_type_t type, void *data __attribute__ ((unused)))
  354. {
  355. if (type != GRUB_MEMORY_AVAILABLE)
  356. return 0;
  357. if (addr < phys_base)
  358. phys_base = addr;
  359. return 0;
  360. }
  361. static void
  362. determine_phys_base (void)
  363. {
  364. phys_base = ~(grub_uint64_t) 0;
  365. grub_machine_mmap_iterate (get_physbase, NULL);
  366. }
  367. static void
  368. fetch_translations (void)
  369. {
  370. grub_ieee1275_phandle_t node;
  371. grub_ssize_t actual;
  372. int i;
  373. if (grub_ieee1275_finddevice ("/virtual-memory", &node))
  374. {
  375. grub_printf ("Cannot find /virtual-memory node.\n");
  376. return;
  377. }
  378. if (grub_ieee1275_get_property_length (node, "translations", &actual))
  379. {
  380. grub_printf ("Cannot find /virtual-memory/translations size.\n");
  381. return;
  382. }
  383. of_trans = grub_malloc (actual);
  384. if (!of_trans)
  385. {
  386. grub_printf ("Cannot allocate translations buffer.\n");
  387. return;
  388. }
  389. if (grub_ieee1275_get_property (node, "translations", of_trans, actual, &actual))
  390. {
  391. grub_printf ("Cannot fetch /virtual-memory/translations property.\n");
  392. return;
  393. }
  394. of_num_trans = actual / sizeof(struct grub_ieee1275_translation);
  395. for (i = 0; i < of_num_trans; i++)
  396. {
  397. struct grub_ieee1275_translation *p = &of_trans[i];
  398. if (p->vaddr == 0x2000)
  399. {
  400. grub_addr_t phys, tte = p->data;
  401. phys = tte & ~(0xff00000000001fffULL);
  402. grub_phys_start = phys;
  403. grub_phys_end = grub_phys_start + p->size;
  404. grub_dprintf ("loader", "Grub lives at phys_start[%lx] phys_end[%lx]\n",
  405. (unsigned long) grub_phys_start,
  406. (unsigned long) grub_phys_end);
  407. break;
  408. }
  409. }
  410. }
  411. static grub_command_t cmd_linux, cmd_initrd;
  412. GRUB_MOD_INIT(linux)
  413. {
  414. determine_phys_base ();
  415. fetch_translations ();
  416. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  417. 0, N_("Load Linux."));
  418. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  419. 0, N_("Load initrd."));
  420. my_mod = mod;
  421. }
  422. GRUB_MOD_FINI(linux)
  423. {
  424. grub_unregister_command (cmd_linux);
  425. grub_unregister_command (cmd_initrd);
  426. }