linux.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/loader.h>
  19. #include <grub/file.h>
  20. #include <grub/disk.h>
  21. #include <grub/err.h>
  22. #include <grub/misc.h>
  23. #include <grub/types.h>
  24. #include <grub/command.h>
  25. #include <grub/dl.h>
  26. #include <grub/mm.h>
  27. #include <grub/cache.h>
  28. #include <grub/kernel.h>
  29. #include <grub/efi/api.h>
  30. #include <grub/efi/efi.h>
  31. #include <grub/elf.h>
  32. #include <grub/i18n.h>
  33. #include <grub/env.h>
  34. #include <grub/linux.h>
  35. #include <grub/verify.h>
  36. GRUB_MOD_LICENSE ("GPLv3+");
  37. #pragma GCC diagnostic ignored "-Wcast-align"
  38. #define ALIGN_MIN (256*1024*1024)
  39. #define GRUB_ELF_SEARCH 1024
  40. #define BOOT_PARAM_SIZE 16384
  41. struct ia64_boot_param
  42. {
  43. grub_uint64_t command_line; /* physical address of command line. */
  44. grub_uint64_t efi_systab; /* physical address of EFI system table */
  45. grub_uint64_t efi_memmap; /* physical address of EFI memory map */
  46. grub_uint64_t efi_memmap_size; /* size of EFI memory map */
  47. grub_uint64_t efi_memdesc_size; /* size of an EFI memory map descriptor */
  48. grub_uint32_t efi_memdesc_version; /* memory descriptor version */
  49. struct
  50. {
  51. grub_uint16_t num_cols; /* number of columns on console output dev */
  52. grub_uint16_t num_rows; /* number of rows on console output device */
  53. grub_uint16_t orig_x; /* cursor's x position */
  54. grub_uint16_t orig_y; /* cursor's y position */
  55. } console_info;
  56. grub_uint64_t fpswa; /* physical address of the fpswa interface */
  57. grub_uint64_t initrd_start;
  58. grub_uint64_t initrd_size;
  59. };
  60. typedef struct
  61. {
  62. grub_uint32_t revision;
  63. grub_uint32_t reserved;
  64. void *fpswa;
  65. } fpswa_interface_t;
  66. static fpswa_interface_t *fpswa;
  67. #define NEXT_MEMORY_DESCRIPTOR(desc, size) \
  68. ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
  69. static grub_dl_t my_mod;
  70. static int loaded;
  71. /* Kernel base and size. */
  72. static void *kernel_mem;
  73. static grub_efi_uintn_t kernel_pages;
  74. static grub_uint64_t entry;
  75. /* Initrd base and size. */
  76. static void *initrd_mem;
  77. static grub_efi_uintn_t initrd_pages;
  78. static grub_efi_uintn_t initrd_size;
  79. static struct ia64_boot_param *boot_param;
  80. static grub_efi_uintn_t boot_param_pages;
  81. static inline grub_size_t
  82. page_align (grub_size_t size)
  83. {
  84. return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
  85. }
  86. static void
  87. query_fpswa (void)
  88. {
  89. grub_efi_handle_t fpswa_image;
  90. grub_efi_boot_services_t *bs;
  91. grub_efi_status_t status;
  92. grub_efi_uintn_t size;
  93. static const grub_guid_t fpswa_protocol =
  94. { 0xc41b6531, 0x97b9, 0x11d3,
  95. {0x9a, 0x29, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d} };
  96. if (fpswa != NULL)
  97. return;
  98. size = sizeof(grub_efi_handle_t);
  99. bs = grub_efi_system_table->boot_services;
  100. status = bs->locate_handle (GRUB_EFI_BY_PROTOCOL,
  101. (void *) &fpswa_protocol,
  102. NULL, &size, &fpswa_image);
  103. if (status != GRUB_EFI_SUCCESS)
  104. {
  105. grub_printf ("%s\n", _("Could not locate FPSWA driver"));
  106. return;
  107. }
  108. status = bs->handle_protocol (fpswa_image,
  109. (void *) &fpswa_protocol, (void *) &fpswa);
  110. if (status != GRUB_EFI_SUCCESS)
  111. {
  112. grub_printf ("%s\n",
  113. _("FPSWA protocol wasn't able to find the interface"));
  114. return;
  115. }
  116. }
  117. static void
  118. free_pages (void)
  119. {
  120. if (kernel_mem)
  121. {
  122. grub_efi_free_pages ((grub_addr_t) kernel_mem, kernel_pages);
  123. kernel_mem = 0;
  124. }
  125. if (initrd_mem)
  126. {
  127. grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
  128. initrd_mem = 0;
  129. }
  130. if (boot_param)
  131. {
  132. /* Free bootparam. */
  133. grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
  134. boot_param_pages);
  135. boot_param = 0;
  136. }
  137. }
  138. static void *
  139. allocate_pages (grub_uint64_t align, grub_uint64_t size_pages,
  140. grub_uint64_t nobase)
  141. {
  142. grub_uint64_t size;
  143. grub_efi_uintn_t desc_size;
  144. grub_efi_memory_descriptor_t *mmap, *mmap_end;
  145. grub_efi_uintn_t mmap_size, tmp_mmap_size;
  146. grub_efi_memory_descriptor_t *desc;
  147. void *mem = NULL;
  148. size = size_pages << 12;
  149. mmap_size = grub_efi_find_mmap_size ();
  150. if (!mmap_size)
  151. return 0;
  152. /* Read the memory map temporarily, to find free space. */
  153. mmap = grub_malloc (mmap_size);
  154. if (! mmap)
  155. return 0;
  156. tmp_mmap_size = mmap_size;
  157. if (grub_efi_get_memory_map (&tmp_mmap_size, mmap, 0, &desc_size, 0) <= 0)
  158. {
  159. grub_error (GRUB_ERR_IO, "cannot get memory map");
  160. goto fail;
  161. }
  162. mmap_end = NEXT_MEMORY_DESCRIPTOR (mmap, tmp_mmap_size);
  163. /* First, find free pages for the real mode code
  164. and the memory map buffer. */
  165. for (desc = mmap;
  166. desc < mmap_end;
  167. desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
  168. {
  169. grub_uint64_t start, end;
  170. grub_uint64_t aligned_start;
  171. if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
  172. continue;
  173. start = desc->physical_start;
  174. end = start + (desc->num_pages << 12);
  175. /* Align is a power of 2. */
  176. aligned_start = (start + align - 1) & ~(align - 1);
  177. if (aligned_start + size > end)
  178. continue;
  179. if (aligned_start == nobase)
  180. aligned_start += align;
  181. if (aligned_start + size > end)
  182. continue;
  183. mem = grub_efi_allocate_fixed (aligned_start, size_pages);
  184. if (! mem)
  185. {
  186. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
  187. goto fail;
  188. }
  189. break;
  190. }
  191. if (! mem)
  192. {
  193. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate memory");
  194. goto fail;
  195. }
  196. grub_free (mmap);
  197. return mem;
  198. fail:
  199. grub_free (mmap);
  200. free_pages ();
  201. return 0;
  202. }
  203. static void
  204. set_boot_param_console (void)
  205. {
  206. grub_efi_simple_text_output_interface_t *conout;
  207. grub_efi_uintn_t cols, rows;
  208. conout = grub_efi_system_table->con_out;
  209. if (conout->query_mode (conout, conout->mode->mode, &cols, &rows)
  210. != GRUB_EFI_SUCCESS)
  211. return;
  212. grub_dprintf ("linux",
  213. "Console info: cols=%lu rows=%lu x=%u y=%u\n",
  214. cols, rows,
  215. conout->mode->cursor_column, conout->mode->cursor_row);
  216. boot_param->console_info.num_cols = cols;
  217. boot_param->console_info.num_rows = rows;
  218. boot_param->console_info.orig_x = conout->mode->cursor_column;
  219. boot_param->console_info.orig_y = conout->mode->cursor_row;
  220. }
  221. static grub_err_t
  222. grub_linux_boot (void)
  223. {
  224. grub_efi_uintn_t mmap_size;
  225. grub_efi_uintn_t map_key;
  226. grub_efi_uintn_t desc_size;
  227. grub_efi_uint32_t desc_version;
  228. grub_efi_memory_descriptor_t *mmap_buf;
  229. grub_err_t err;
  230. /* FPSWA. */
  231. query_fpswa ();
  232. boot_param->fpswa = (grub_uint64_t)fpswa;
  233. /* Initrd. */
  234. boot_param->initrd_start = (grub_uint64_t)initrd_mem;
  235. boot_param->initrd_size = (grub_uint64_t)initrd_size;
  236. set_boot_param_console ();
  237. grub_dprintf ("linux", "Jump to %016lx\n", entry);
  238. /* MDT.
  239. Must be done after grub_machine_fini because map_key is used by
  240. exit_boot_services. */
  241. mmap_size = grub_efi_find_mmap_size ();
  242. if (! mmap_size)
  243. return grub_errno;
  244. mmap_buf = grub_efi_allocate_any_pages (page_align (mmap_size) >> 12);
  245. if (! mmap_buf)
  246. return grub_error (GRUB_ERR_IO, "cannot allocate memory map");
  247. err = grub_efi_finish_boot_services (&mmap_size, mmap_buf, &map_key,
  248. &desc_size, &desc_version);
  249. if (err)
  250. return err;
  251. boot_param->efi_memmap = (grub_uint64_t)mmap_buf;
  252. boot_param->efi_memmap_size = mmap_size;
  253. boot_param->efi_memdesc_size = desc_size;
  254. boot_param->efi_memdesc_version = desc_version;
  255. /* See you next boot. */
  256. asm volatile ("mov r28=%1; br.sptk.few %0" :: "b"(entry),"r"(boot_param));
  257. /* Never reach here. */
  258. return GRUB_ERR_NONE;
  259. }
  260. static grub_err_t
  261. grub_linux_unload (void)
  262. {
  263. free_pages ();
  264. grub_dl_unref (my_mod);
  265. loaded = 0;
  266. return GRUB_ERR_NONE;
  267. }
  268. static grub_err_t
  269. grub_load_elf64 (grub_file_t file, void *buffer, const char *filename)
  270. {
  271. Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
  272. Elf64_Phdr *phdr;
  273. int i;
  274. grub_uint64_t low_addr;
  275. grub_uint64_t high_addr;
  276. grub_uint64_t align;
  277. grub_uint64_t reloc_offset;
  278. const char *relocate;
  279. if (ehdr->e_ident[EI_MAG0] != ELFMAG0
  280. || ehdr->e_ident[EI_MAG1] != ELFMAG1
  281. || ehdr->e_ident[EI_MAG2] != ELFMAG2
  282. || ehdr->e_ident[EI_MAG3] != ELFMAG3
  283. || ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
  284. return grub_error(GRUB_ERR_UNKNOWN_OS,
  285. N_("invalid arch-independent ELF magic"));
  286. if (ehdr->e_ident[EI_CLASS] != ELFCLASS64
  287. || ehdr->e_version != EV_CURRENT
  288. || ehdr->e_machine != EM_IA_64)
  289. return grub_error (GRUB_ERR_UNKNOWN_OS,
  290. N_("invalid arch-dependent ELF magic"));
  291. if (ehdr->e_type != ET_EXEC)
  292. return grub_error (GRUB_ERR_UNKNOWN_OS,
  293. N_("this ELF file is not of the right type"));
  294. /* FIXME: Should we support program headers at strange locations? */
  295. if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > GRUB_ELF_SEARCH)
  296. return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
  297. entry = ehdr->e_entry;
  298. /* Compute low, high and align addresses. */
  299. low_addr = ~0UL;
  300. high_addr = 0;
  301. align = 0;
  302. for (i = 0; i < ehdr->e_phnum; i++)
  303. {
  304. phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
  305. + i * ehdr->e_phentsize);
  306. if (phdr->p_type == PT_LOAD)
  307. {
  308. if (phdr->p_paddr < low_addr)
  309. low_addr = phdr->p_paddr;
  310. if (phdr->p_paddr + phdr->p_memsz > high_addr)
  311. high_addr = phdr->p_paddr + phdr->p_memsz;
  312. if (phdr->p_align > align)
  313. align = phdr->p_align;
  314. }
  315. }
  316. if (align < ALIGN_MIN)
  317. align = ALIGN_MIN;
  318. if (high_addr == 0)
  319. return grub_error (GRUB_ERR_BAD_OS, "no program entries");
  320. kernel_pages = page_align (high_addr - low_addr) >> 12;
  321. /* Undocumented on purpose. */
  322. relocate = grub_env_get ("linux_relocate");
  323. if (!relocate || grub_strcmp (relocate, "force") != 0)
  324. {
  325. kernel_mem = grub_efi_allocate_fixed (low_addr, kernel_pages);
  326. reloc_offset = 0;
  327. }
  328. /* Try to relocate. */
  329. if (! kernel_mem && (!relocate || grub_strcmp (relocate, "off") != 0))
  330. {
  331. kernel_mem = allocate_pages (align, kernel_pages, low_addr);
  332. if (kernel_mem)
  333. {
  334. reloc_offset = (grub_uint64_t)kernel_mem - low_addr;
  335. grub_dprintf ("linux", " Relocated at %p (offset=%016lx)\n",
  336. kernel_mem, reloc_offset);
  337. entry += reloc_offset;
  338. }
  339. }
  340. if (! kernel_mem)
  341. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  342. "cannot allocate memory for OS");
  343. /* Load every loadable segment in memory. */
  344. for (i = 0; i < ehdr->e_phnum; i++)
  345. {
  346. phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
  347. + i * ehdr->e_phentsize);
  348. if (phdr->p_type == PT_LOAD)
  349. {
  350. grub_dprintf ("linux", " [paddr=%lx load=%lx memsz=%08lx "
  351. "off=%lx flags=%x]\n",
  352. phdr->p_paddr, phdr->p_paddr + reloc_offset,
  353. phdr->p_memsz, phdr->p_offset, phdr->p_flags);
  354. if (grub_file_seek (file, phdr->p_offset) == (grub_off_t)-1)
  355. return grub_errno;
  356. if (grub_file_read (file, (void *) (phdr->p_paddr + reloc_offset),
  357. phdr->p_filesz)
  358. != (grub_ssize_t) phdr->p_filesz)
  359. {
  360. if (!grub_errno)
  361. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  362. filename);
  363. return grub_errno;
  364. }
  365. if (phdr->p_filesz < phdr->p_memsz)
  366. grub_memset
  367. ((char *)(phdr->p_paddr + reloc_offset + phdr->p_filesz),
  368. 0, phdr->p_memsz - phdr->p_filesz);
  369. /* Sync caches if necessary. */
  370. if (phdr->p_flags & PF_X)
  371. grub_arch_sync_caches
  372. ((void *)(phdr->p_paddr + reloc_offset), phdr->p_memsz);
  373. }
  374. }
  375. loaded = 1;
  376. return 0;
  377. }
  378. static grub_err_t
  379. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  380. int argc, char *argv[])
  381. {
  382. grub_file_t file = 0;
  383. char buffer[GRUB_ELF_SEARCH];
  384. char *cmdline, *p;
  385. grub_ssize_t len;
  386. int i;
  387. grub_dl_ref (my_mod);
  388. grub_loader_unset ();
  389. if (argc == 0)
  390. {
  391. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  392. goto fail;
  393. }
  394. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
  395. if (! file)
  396. goto fail;
  397. len = grub_file_read (file, buffer, sizeof (buffer));
  398. if (len < (grub_ssize_t) sizeof (Elf64_Ehdr))
  399. {
  400. if (!grub_errno)
  401. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  402. argv[0]);
  403. goto fail;
  404. }
  405. grub_dprintf ("linux", "Loading linux: %s\n", argv[0]);
  406. if (grub_load_elf64 (file, buffer, argv[0]))
  407. goto fail;
  408. len = sizeof("BOOT_IMAGE=") + 8;
  409. for (i = 0; i < argc; i++)
  410. len += grub_strlen (argv[i]) + 1;
  411. len += sizeof (struct ia64_boot_param) + 512; /* Room for extensions. */
  412. boot_param_pages = page_align (len) >> 12;
  413. boot_param = grub_efi_allocate_any_pages (boot_param_pages);
  414. if (boot_param == 0)
  415. {
  416. grub_error (GRUB_ERR_OUT_OF_MEMORY,
  417. "cannot allocate memory for bootparams");
  418. goto fail;
  419. }
  420. grub_memset (boot_param, 0, len);
  421. cmdline = ((char *)(boot_param + 1)) + 256;
  422. /* Build cmdline. */
  423. p = grub_stpcpy (cmdline, "BOOT_IMAGE");
  424. for (i = 0; i < argc; i++)
  425. {
  426. *p++ = ' ';
  427. p = grub_stpcpy (p, argv[i]);
  428. }
  429. cmdline[10] = '=';
  430. *p = '\0';
  431. if (grub_verify_string (cmdline, GRUB_VERIFY_KERNEL_CMDLINE))
  432. goto fail;
  433. boot_param->command_line = (grub_uint64_t) cmdline;
  434. boot_param->efi_systab = (grub_uint64_t) grub_efi_system_table;
  435. grub_errno = GRUB_ERR_NONE;
  436. grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
  437. fail:
  438. if (file)
  439. grub_file_close (file);
  440. if (grub_errno != GRUB_ERR_NONE)
  441. {
  442. grub_efi_free_pages ((grub_efi_physical_address_t) boot_param,
  443. boot_param_pages);
  444. grub_dl_unref (my_mod);
  445. }
  446. return grub_errno;
  447. }
  448. static grub_err_t
  449. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  450. int argc, char *argv[])
  451. {
  452. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  453. if (argc == 0)
  454. {
  455. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  456. goto fail;
  457. }
  458. if (! loaded)
  459. {
  460. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
  461. goto fail;
  462. }
  463. if (grub_initrd_init (argc, argv, &initrd_ctx))
  464. goto fail;
  465. initrd_size = grub_get_initrd_size (&initrd_ctx);
  466. grub_dprintf ("linux", "Loading initrd\n");
  467. initrd_pages = (page_align (initrd_size) >> 12);
  468. initrd_mem = grub_efi_allocate_any_pages (initrd_pages);
  469. if (! initrd_mem)
  470. {
  471. grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate pages");
  472. goto fail;
  473. }
  474. grub_dprintf ("linux", "[addr=0x%lx, size=0x%lx]\n",
  475. (grub_uint64_t) initrd_mem, initrd_size);
  476. if (grub_initrd_load (&initrd_ctx, initrd_mem))
  477. goto fail;
  478. fail:
  479. grub_initrd_close (&initrd_ctx);
  480. return grub_errno;
  481. }
  482. static grub_err_t
  483. grub_cmd_fpswa (grub_command_t cmd __attribute__ ((unused)),
  484. int argc __attribute__((unused)),
  485. char *argv[] __attribute__((unused)))
  486. {
  487. query_fpswa ();
  488. if (fpswa == NULL)
  489. grub_puts_ (N_("No FPSWA found"));
  490. else
  491. grub_printf (_("FPSWA revision: %x\n"), fpswa->revision);
  492. return GRUB_ERR_NONE;
  493. }
  494. static grub_command_t cmd_linux, cmd_initrd, cmd_fpswa;
  495. GRUB_MOD_INIT(linux)
  496. {
  497. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  498. N_("FILE [ARGS...]"), N_("Load Linux."));
  499. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  500. N_("FILE"), N_("Load initrd."));
  501. cmd_fpswa = grub_register_command ("fpswa", grub_cmd_fpswa,
  502. "", N_("Display FPSWA version."));
  503. my_mod = mod;
  504. }
  505. GRUB_MOD_FINI(linux)
  506. {
  507. grub_unregister_command (cmd_linux);
  508. grub_unregister_command (cmd_initrd);
  509. grub_unregister_command (cmd_fpswa);
  510. }