linux.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/cpu/fdtload.h>
  29. #include <grub/efi/efi.h>
  30. #include <grub/efi/pe32.h>
  31. #include <grub/i18n.h>
  32. #include <grub/lib/cmdline.h>
  33. GRUB_MOD_LICENSE ("GPLv3+");
  34. static grub_dl_t my_mod;
  35. static int loaded;
  36. static void *kernel_addr;
  37. static grub_uint64_t kernel_size;
  38. static char *linux_args;
  39. static grub_uint32_t cmdline_size;
  40. static grub_addr_t initrd_start;
  41. static grub_addr_t initrd_end;
  42. grub_err_t
  43. grub_arm64_uefi_check_image (struct grub_arm64_linux_kernel_header * lh)
  44. {
  45. if (lh->magic != GRUB_ARM64_LINUX_MAGIC)
  46. return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
  47. if ((lh->code0 & 0xffff) != GRUB_EFI_PE_MAGIC)
  48. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  49. N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
  50. grub_dprintf ("linux", "UEFI stub kernel:\n");
  51. grub_dprintf ("linux", "text_offset = 0x%012llx\n",
  52. (long long unsigned) lh->text_offset);
  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 (0x400);
  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 @ 0x%012lx-0x%012lx\n",
  73. initrd_start, 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_arm64_uefi_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_pages (0,
  124. GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
  125. if (!loaded_image->load_options)
  126. return grub_errno;
  127. loaded_image->load_options_size =
  128. 2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
  129. (grub_uint8_t *) args, len, NULL);
  130. grub_dprintf ("linux", "starting image %p\n", image_handle);
  131. status = b->start_image (image_handle, 0, NULL);
  132. /* When successful, not reached */
  133. b->unload_image (image_handle);
  134. grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options,
  135. GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
  136. return grub_errno;
  137. }
  138. static grub_err_t
  139. grub_linux_boot (void)
  140. {
  141. if (finalize_params_linux () != GRUB_ERR_NONE)
  142. return grub_errno;
  143. return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr,
  144. kernel_size, linux_args));
  145. }
  146. static grub_err_t
  147. grub_linux_unload (void)
  148. {
  149. grub_dl_unref (my_mod);
  150. loaded = 0;
  151. if (initrd_start)
  152. grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
  153. GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
  154. initrd_start = initrd_end = 0;
  155. grub_free (linux_args);
  156. if (kernel_addr)
  157. grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
  158. GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  159. grub_fdt_unload ();
  160. return GRUB_ERR_NONE;
  161. }
  162. static grub_err_t
  163. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  164. int argc, char *argv[])
  165. {
  166. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  167. int initrd_size, initrd_pages;
  168. void *initrd_mem = NULL;
  169. if (argc == 0)
  170. {
  171. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  172. goto fail;
  173. }
  174. if (!loaded)
  175. {
  176. grub_error (GRUB_ERR_BAD_ARGUMENT,
  177. N_("you need to load the kernel first"));
  178. goto fail;
  179. }
  180. if (grub_initrd_init (argc, argv, &initrd_ctx))
  181. goto fail;
  182. initrd_size = grub_get_initrd_size (&initrd_ctx);
  183. grub_dprintf ("linux", "Loading initrd\n");
  184. initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
  185. initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
  186. if (!initrd_mem)
  187. {
  188. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  189. goto fail;
  190. }
  191. if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
  192. goto fail;
  193. initrd_start = (grub_addr_t) initrd_mem;
  194. initrd_end = initrd_start + initrd_size;
  195. grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
  196. (void *) initrd_start, initrd_size);
  197. fail:
  198. grub_initrd_close (&initrd_ctx);
  199. if (initrd_mem && !initrd_start)
  200. grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem,
  201. initrd_pages);
  202. return grub_errno;
  203. }
  204. static grub_err_t
  205. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  206. int argc, char *argv[])
  207. {
  208. grub_file_t file = 0;
  209. struct grub_arm64_linux_kernel_header lh;
  210. grub_dl_ref (my_mod);
  211. if (argc == 0)
  212. {
  213. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  214. goto fail;
  215. }
  216. file = grub_file_open (argv[0]);
  217. if (!file)
  218. goto fail;
  219. kernel_size = grub_file_size (file);
  220. if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
  221. return grub_errno;
  222. if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE)
  223. goto fail;
  224. grub_loader_unset();
  225. grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
  226. kernel_addr = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  227. grub_dprintf ("linux", "kernel numpages: %lld\n",
  228. (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  229. if (!kernel_addr)
  230. {
  231. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  232. goto fail;
  233. }
  234. grub_file_seek (file, 0);
  235. if (grub_file_read (file, kernel_addr, kernel_size)
  236. < (grub_int64_t) kernel_size)
  237. {
  238. if (!grub_errno)
  239. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
  240. goto fail;
  241. }
  242. grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
  243. cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
  244. linux_args = grub_malloc (cmdline_size);
  245. if (!linux_args)
  246. {
  247. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  248. goto fail;
  249. }
  250. grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  251. grub_create_loader_cmdline (argc, argv,
  252. linux_args + sizeof (LINUX_IMAGE) - 1,
  253. cmdline_size);
  254. if (grub_errno == GRUB_ERR_NONE)
  255. {
  256. grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
  257. loaded = 1;
  258. }
  259. fail:
  260. if (file)
  261. grub_file_close (file);
  262. if (grub_errno != GRUB_ERR_NONE)
  263. {
  264. grub_dl_unref (my_mod);
  265. loaded = 0;
  266. }
  267. if (linux_args && !loaded)
  268. grub_free (linux_args);
  269. if (kernel_addr && !loaded)
  270. grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
  271. GRUB_EFI_BYTES_TO_PAGES (kernel_size));
  272. return grub_errno;
  273. }
  274. static grub_command_t cmd_linux, cmd_initrd;
  275. GRUB_MOD_INIT (linux)
  276. {
  277. cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
  278. N_("Load Linux."));
  279. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
  280. N_("Load initrd."));
  281. my_mod = mod;
  282. }
  283. GRUB_MOD_FINI (linux)
  284. {
  285. grub_unregister_command (cmd_linux);
  286. grub_unregister_command (cmd_initrd);
  287. }