linux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 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/charset.h>
  19. #include <grub/command.h>
  20. #include <grub/err.h>
  21. #include <grub/file.h>
  22. #include <grub/fdt.h>
  23. #include <grub/linux.h>
  24. #include <grub/loader.h>
  25. #include <grub/mm.h>
  26. #include <grub/types.h>
  27. #include <grub/cpu/linux.h>
  28. #include <grub/efi/efi.h>
  29. #include <grub/efi/fdtload.h>
  30. #include <grub/efi/memory.h>
  31. #include <grub/efi/pe32.h>
  32. #include <grub/i18n.h>
  33. #include <grub/lib/cmdline.h>
  34. #include <grub/verify.h>
  35. GRUB_MOD_LICENSE ("GPLv3+");
  36. static grub_dl_t my_mod;
  37. static int loaded;
  38. static void *kernel_addr;
  39. static grub_uint64_t kernel_size;
  40. static char *linux_args;
  41. static grub_uint32_t cmdline_size;
  42. static grub_addr_t initrd_start;
  43. static grub_addr_t initrd_end;
  44. grub_err_t
  45. grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh)
  46. {
  47. if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE)
  48. return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
  49. if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
  50. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  51. N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
  52. grub_dprintf ("linux", "UEFI stub kernel:\n");
  53. grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
  54. return GRUB_ERR_NONE;
  55. }
  56. static grub_err_t
  57. finalize_params_linux (void)
  58. {
  59. int node, retval;
  60. void *fdt;
  61. fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE);
  62. if (!fdt)
  63. goto failure;
  64. node = grub_fdt_find_subnode (fdt, 0, "chosen");
  65. if (node < 0)
  66. node = grub_fdt_add_subnode (fdt, 0, "chosen");
  67. if (node < 1)
  68. goto failure;
  69. /* Set initrd info */
  70. if (initrd_start && initrd_end > initrd_start)
  71. {
  72. grub_dprintf ("linux", "Initrd @ %p-%p\n",
  73. (void *) initrd_start, (void *) initrd_end);
  74. retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
  75. initrd_start);
  76. if (retval)
  77. goto failure;
  78. retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
  79. initrd_end);
  80. if (retval)
  81. goto failure;
  82. }
  83. if (grub_fdt_install() != GRUB_ERR_NONE)
  84. goto failure;
  85. return GRUB_ERR_NONE;
  86. failure:
  87. grub_fdt_unload();
  88. return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
  89. }
  90. grub_err_t
  91. grub_arch_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args)
  92. {
  93. grub_efi_memory_mapped_device_path_t *mempath;
  94. grub_efi_handle_t image_handle;
  95. grub_efi_boot_services_t *b;
  96. grub_efi_status_t status;
  97. grub_efi_loaded_image_t *loaded_image;
  98. int len;
  99. mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
  100. if (!mempath)
  101. return grub_errno;
  102. mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
  103. mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
  104. mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
  105. mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
  106. mempath[0].start_address = addr;
  107. mempath[0].end_address = addr + size;
  108. mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
  109. mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
  110. mempath[1].header.length = sizeof (grub_efi_device_path_t);
  111. b = grub_efi_system_table->boot_services;
  112. status = b->load_image (0, grub_efi_image_handle,
  113. (grub_efi_device_path_t *) mempath,
  114. (void *) addr, size, &image_handle);
  115. if (status != GRUB_EFI_SUCCESS)
  116. return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
  117. grub_dprintf ("linux", "linux command line: '%s'\n", args);
  118. /* Convert command line to UCS-2 */
  119. loaded_image = grub_efi_get_loaded_image (image_handle);
  120. loaded_image->load_options_size = len =
  121. (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t);
  122. loaded_image->load_options =
  123. grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
  124. if (!loaded_image->load_options)
  125. return grub_errno;
  126. loaded_image->load_options_size =
  127. 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
  128. (grub_uint8_t *) args, len, NULL);
  129. grub_dprintf ("linux", "starting image %p\n", image_handle);
  130. status = b->start_image (image_handle, 0, NULL);
  131. /* When successful, not reached */
  132. b->unload_image (image_handle);
  133. grub_efi_free_pages ((grub_addr_t) loaded_image->load_options,
  134. GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
  135. return grub_errno;
  136. }
  137. static grub_err_t
  138. grub_linux_boot (void)
  139. {
  140. if (finalize_params_linux () != GRUB_ERR_NONE)
  141. return grub_errno;
  142. return (grub_arch_efi_linux_boot_image((grub_addr_t)kernel_addr,
  143. kernel_size, linux_args));
  144. }
  145. static grub_err_t
  146. grub_linux_unload (void)
  147. {
  148. grub_dl_unref (my_mod);
  149. loaded = 0;
  150. if (initrd_start)
  151. grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
  152. GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
  153. initrd_start = initrd_end = 0;
  154. grub_free (linux_args);
  155. if (kernel_addr)
  156. grub_efi_free_pages ((grub_addr_t) kernel_addr,
  157. GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  158. grub_fdt_unload ();
  159. return GRUB_ERR_NONE;
  160. }
  161. /*
  162. * As per linux/Documentation/arm/Booting
  163. * ARM initrd needs to be covered by kernel linear mapping,
  164. * so place it in the first 512MB of DRAM.
  165. *
  166. * As per linux/Documentation/arm64/booting.txt
  167. * ARM64 initrd needs to be contained entirely within a 1GB aligned window
  168. * of up to 32GB of size that covers the kernel image as well.
  169. * Since the EFI stub loader will attempt to load the kernel near start of
  170. * RAM, place the buffer in the first 32GB of RAM.
  171. */
  172. #ifdef __arm__
  173. #define INITRD_MAX_ADDRESS_OFFSET (512U * 1024 * 1024)
  174. #else /* __aarch64__ */
  175. #define INITRD_MAX_ADDRESS_OFFSET (32ULL * 1024 * 1024 * 1024)
  176. #endif
  177. /*
  178. * This function returns a pointer to a legally allocated initrd buffer,
  179. * or NULL if unsuccessful
  180. */
  181. static void *
  182. allocate_initrd_mem (int initrd_pages)
  183. {
  184. grub_addr_t max_addr;
  185. if (grub_efi_get_ram_base (&max_addr) != GRUB_ERR_NONE)
  186. return NULL;
  187. max_addr += INITRD_MAX_ADDRESS_OFFSET - 1;
  188. return grub_efi_allocate_pages_real (max_addr, initrd_pages,
  189. GRUB_EFI_ALLOCATE_MAX_ADDRESS,
  190. GRUB_EFI_LOADER_DATA);
  191. }
  192. static grub_err_t
  193. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  194. int argc, char *argv[])
  195. {
  196. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  197. int initrd_size, initrd_pages;
  198. void *initrd_mem = NULL;
  199. if (argc == 0)
  200. {
  201. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  202. goto fail;
  203. }
  204. if (!loaded)
  205. {
  206. grub_error (GRUB_ERR_BAD_ARGUMENT,
  207. N_("you need to load the kernel first"));
  208. goto fail;
  209. }
  210. if (grub_initrd_init (argc, argv, &initrd_ctx))
  211. goto fail;
  212. initrd_size = grub_get_initrd_size (&initrd_ctx);
  213. grub_dprintf ("linux", "Loading initrd\n");
  214. initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
  215. initrd_mem = allocate_initrd_mem (initrd_pages);
  216. if (!initrd_mem)
  217. {
  218. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  219. goto fail;
  220. }
  221. if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
  222. goto fail;
  223. initrd_start = (grub_addr_t) initrd_mem;
  224. initrd_end = initrd_start + initrd_size;
  225. grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
  226. (void *) initrd_start, initrd_size);
  227. fail:
  228. grub_initrd_close (&initrd_ctx);
  229. if (initrd_mem && !initrd_start)
  230. grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
  231. return grub_errno;
  232. }
  233. static grub_err_t
  234. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  235. int argc, char *argv[])
  236. {
  237. grub_file_t file = 0;
  238. struct linux_arch_kernel_header lh;
  239. grub_err_t err;
  240. grub_dl_ref (my_mod);
  241. if (argc == 0)
  242. {
  243. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  244. goto fail;
  245. }
  246. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
  247. if (!file)
  248. goto fail;
  249. kernel_size = grub_file_size (file);
  250. if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
  251. return grub_errno;
  252. if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE)
  253. goto fail;
  254. grub_loader_unset();
  255. grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
  256. kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  257. grub_dprintf ("linux", "kernel numpages: %lld\n",
  258. (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  259. if (!kernel_addr)
  260. {
  261. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  262. goto fail;
  263. }
  264. grub_file_seek (file, 0);
  265. if (grub_file_read (file, kernel_addr, kernel_size)
  266. < (grub_int64_t) kernel_size)
  267. {
  268. if (!grub_errno)
  269. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
  270. goto fail;
  271. }
  272. grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
  273. cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
  274. linux_args = grub_malloc (cmdline_size);
  275. if (!linux_args)
  276. {
  277. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  278. goto fail;
  279. }
  280. grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  281. err = grub_create_loader_cmdline (argc, argv,
  282. linux_args + sizeof (LINUX_IMAGE) - 1,
  283. cmdline_size,
  284. GRUB_VERIFY_KERNEL_CMDLINE);
  285. if (err)
  286. goto fail;
  287. if (grub_errno == GRUB_ERR_NONE)
  288. {
  289. grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
  290. loaded = 1;
  291. }
  292. fail:
  293. if (file)
  294. grub_file_close (file);
  295. if (grub_errno != GRUB_ERR_NONE)
  296. {
  297. grub_dl_unref (my_mod);
  298. loaded = 0;
  299. }
  300. if (linux_args && !loaded)
  301. grub_free (linux_args);
  302. if (kernel_addr && !loaded)
  303. grub_efi_free_pages ((grub_addr_t) kernel_addr,
  304. GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  305. return grub_errno;
  306. }
  307. static grub_command_t cmd_linux, cmd_initrd;
  308. GRUB_MOD_INIT (linux)
  309. {
  310. cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
  311. N_("Load Linux."));
  312. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
  313. N_("Load initrd."));
  314. my_mod = mod;
  315. }
  316. GRUB_MOD_FINI (linux)
  317. {
  318. grub_unregister_command (cmd_linux);
  319. grub_unregister_command (cmd_initrd);
  320. }