arm-stub.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * EFI stub implementation that is shared by arm and arm64 architectures.
  3. * This should be #included by the EFI stub implementation files.
  4. *
  5. * Copyright (C) 2013,2014 Linaro Limited
  6. * Roy Franz <roy.franz@linaro.org
  7. * Copyright (C) 2013 Red Hat, Inc.
  8. * Mark Salter <msalter@redhat.com>
  9. *
  10. * This file is part of the Linux kernel, and is made available under the
  11. * terms of the GNU General Public License version 2.
  12. *
  13. */
  14. #include <linux/efi.h>
  15. #include <linux/sort.h>
  16. #include <asm/efi.h>
  17. #include "efistub.h"
  18. /*
  19. * This is the base address at which to start allocating virtual memory ranges
  20. * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
  21. * any allocation we choose, and eliminate the risk of a conflict after kexec.
  22. * The value chosen is the largest non-zero power of 2 suitable for this purpose
  23. * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
  24. * be mapped efficiently.
  25. * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
  26. * map everything below 1 GB. (512 MB is a reasonable upper bound for the
  27. * entire footprint of the UEFI runtime services memory regions)
  28. */
  29. #define EFI_RT_VIRTUAL_BASE SZ_512M
  30. #define EFI_RT_VIRTUAL_SIZE SZ_512M
  31. #ifdef CONFIG_ARM64
  32. # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_64
  33. #else
  34. # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
  35. #endif
  36. static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
  37. efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
  38. void *__image, void **__fh)
  39. {
  40. efi_file_io_interface_t *io;
  41. efi_loaded_image_t *image = __image;
  42. efi_file_handle_t *fh;
  43. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  44. efi_status_t status;
  45. void *handle = (void *)(unsigned long)image->device_handle;
  46. status = sys_table_arg->boottime->handle_protocol(handle,
  47. &fs_proto, (void **)&io);
  48. if (status != EFI_SUCCESS) {
  49. efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
  50. return status;
  51. }
  52. status = io->open_volume(io, &fh);
  53. if (status != EFI_SUCCESS)
  54. efi_printk(sys_table_arg, "Failed to open volume\n");
  55. *__fh = fh;
  56. return status;
  57. }
  58. void efi_char16_printk(efi_system_table_t *sys_table_arg,
  59. efi_char16_t *str)
  60. {
  61. struct efi_simple_text_output_protocol *out;
  62. out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
  63. out->output_string(out, str);
  64. }
  65. static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
  66. {
  67. efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  68. efi_status_t status;
  69. unsigned long size;
  70. void **gop_handle = NULL;
  71. struct screen_info *si = NULL;
  72. size = 0;
  73. status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  74. &gop_proto, NULL, &size, gop_handle);
  75. if (status == EFI_BUFFER_TOO_SMALL) {
  76. si = alloc_screen_info(sys_table_arg);
  77. if (!si)
  78. return NULL;
  79. efi_setup_gop(sys_table_arg, si, &gop_proto, size);
  80. }
  81. return si;
  82. }
  83. /*
  84. * This function handles the architcture specific differences between arm and
  85. * arm64 regarding where the kernel image must be loaded and any memory that
  86. * must be reserved. On failure it is required to free all
  87. * all allocations it has made.
  88. */
  89. efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
  90. unsigned long *image_addr,
  91. unsigned long *image_size,
  92. unsigned long *reserve_addr,
  93. unsigned long *reserve_size,
  94. unsigned long dram_base,
  95. efi_loaded_image_t *image);
  96. /*
  97. * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
  98. * that is described in the PE/COFF header. Most of the code is the same
  99. * for both archictectures, with the arch-specific code provided in the
  100. * handle_kernel_image() function.
  101. */
  102. unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
  103. unsigned long *image_addr)
  104. {
  105. efi_loaded_image_t *image;
  106. efi_status_t status;
  107. unsigned long image_size = 0;
  108. unsigned long dram_base;
  109. /* addr/point and size pairs for memory management*/
  110. unsigned long initrd_addr;
  111. u64 initrd_size = 0;
  112. unsigned long fdt_addr = 0; /* Original DTB */
  113. unsigned long fdt_size = 0;
  114. char *cmdline_ptr = NULL;
  115. int cmdline_size = 0;
  116. unsigned long new_fdt_addr;
  117. efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
  118. unsigned long reserve_addr = 0;
  119. unsigned long reserve_size = 0;
  120. enum efi_secureboot_mode secure_boot;
  121. struct screen_info *si;
  122. /* Check if we were booted by the EFI firmware */
  123. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  124. goto fail;
  125. status = check_platform_features(sys_table);
  126. if (status != EFI_SUCCESS)
  127. goto fail;
  128. /*
  129. * Get a handle to the loaded image protocol. This is used to get
  130. * information about the running image, such as size and the command
  131. * line.
  132. */
  133. status = sys_table->boottime->handle_protocol(handle,
  134. &loaded_image_proto, (void *)&image);
  135. if (status != EFI_SUCCESS) {
  136. pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
  137. goto fail;
  138. }
  139. dram_base = get_dram_base(sys_table);
  140. if (dram_base == EFI_ERROR) {
  141. pr_efi_err(sys_table, "Failed to find DRAM base\n");
  142. goto fail;
  143. }
  144. /*
  145. * Get the command line from EFI, using the LOADED_IMAGE
  146. * protocol. We are going to copy the command line into the
  147. * device tree, so this can be allocated anywhere.
  148. */
  149. cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
  150. if (!cmdline_ptr) {
  151. pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
  152. goto fail;
  153. }
  154. if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
  155. IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
  156. cmdline_size == 0)
  157. efi_parse_options(CONFIG_CMDLINE);
  158. if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
  159. efi_parse_options(cmdline_ptr);
  160. pr_efi(sys_table, "Booting Linux Kernel...\n");
  161. si = setup_graphics(sys_table);
  162. status = handle_kernel_image(sys_table, image_addr, &image_size,
  163. &reserve_addr,
  164. &reserve_size,
  165. dram_base, image);
  166. if (status != EFI_SUCCESS) {
  167. pr_efi_err(sys_table, "Failed to relocate kernel\n");
  168. goto fail_free_cmdline;
  169. }
  170. secure_boot = efi_get_secureboot(sys_table);
  171. /*
  172. * Unauthenticated device tree data is a security hazard, so ignore
  173. * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure
  174. * boot is enabled if we can't determine its state.
  175. */
  176. if (secure_boot != efi_secureboot_mode_disabled &&
  177. strstr(cmdline_ptr, "dtb=")) {
  178. pr_efi(sys_table, "Ignoring DTB from command line.\n");
  179. } else {
  180. status = handle_cmdline_files(sys_table, image, cmdline_ptr,
  181. "dtb=",
  182. ~0UL, &fdt_addr, &fdt_size);
  183. if (status != EFI_SUCCESS) {
  184. pr_efi_err(sys_table, "Failed to load device tree!\n");
  185. goto fail_free_image;
  186. }
  187. }
  188. if (fdt_addr) {
  189. pr_efi(sys_table, "Using DTB from command line\n");
  190. } else {
  191. /* Look for a device tree configuration table entry. */
  192. fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
  193. if (fdt_addr)
  194. pr_efi(sys_table, "Using DTB from configuration table\n");
  195. }
  196. if (!fdt_addr)
  197. pr_efi(sys_table, "Generating empty DTB\n");
  198. status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
  199. efi_get_max_initrd_addr(dram_base,
  200. *image_addr),
  201. (unsigned long *)&initrd_addr,
  202. (unsigned long *)&initrd_size);
  203. if (status != EFI_SUCCESS)
  204. pr_efi_err(sys_table, "Failed initrd from command line!\n");
  205. efi_random_get_seed(sys_table);
  206. if (!nokaslr()) {
  207. /*
  208. * Randomize the base of the UEFI runtime services region.
  209. * Preserve the 2 MB alignment of the region by taking a
  210. * shift of 21 bit positions into account when scaling
  211. * the headroom value using a 32-bit random value.
  212. */
  213. static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
  214. EFI_RT_VIRTUAL_BASE -
  215. EFI_RT_VIRTUAL_SIZE;
  216. u32 rnd;
  217. status = efi_get_random_bytes(sys_table, sizeof(rnd),
  218. (u8 *)&rnd);
  219. if (status == EFI_SUCCESS) {
  220. virtmap_base = EFI_RT_VIRTUAL_BASE +
  221. (((headroom >> 21) * rnd) >> (32 - 21));
  222. }
  223. }
  224. new_fdt_addr = fdt_addr;
  225. status = allocate_new_fdt_and_exit_boot(sys_table, handle,
  226. &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
  227. initrd_addr, initrd_size, cmdline_ptr,
  228. fdt_addr, fdt_size);
  229. /*
  230. * If all went well, we need to return the FDT address to the
  231. * calling function so it can be passed to kernel as part of
  232. * the kernel boot protocol.
  233. */
  234. if (status == EFI_SUCCESS)
  235. return new_fdt_addr;
  236. pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
  237. efi_free(sys_table, initrd_size, initrd_addr);
  238. efi_free(sys_table, fdt_size, fdt_addr);
  239. fail_free_image:
  240. efi_free(sys_table, image_size, *image_addr);
  241. efi_free(sys_table, reserve_size, reserve_addr);
  242. fail_free_cmdline:
  243. free_screen_info(sys_table, si);
  244. efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
  245. fail:
  246. return EFI_ERROR;
  247. }
  248. static int cmp_mem_desc(const void *l, const void *r)
  249. {
  250. const efi_memory_desc_t *left = l, *right = r;
  251. return (left->phys_addr > right->phys_addr) ? 1 : -1;
  252. }
  253. /*
  254. * Returns whether region @left ends exactly where region @right starts,
  255. * or false if either argument is NULL.
  256. */
  257. static bool regions_are_adjacent(efi_memory_desc_t *left,
  258. efi_memory_desc_t *right)
  259. {
  260. u64 left_end;
  261. if (left == NULL || right == NULL)
  262. return false;
  263. left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
  264. return left_end == right->phys_addr;
  265. }
  266. /*
  267. * Returns whether region @left and region @right have compatible memory type
  268. * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
  269. */
  270. static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
  271. efi_memory_desc_t *right)
  272. {
  273. static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
  274. EFI_MEMORY_WC | EFI_MEMORY_UC |
  275. EFI_MEMORY_RUNTIME;
  276. return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
  277. }
  278. /*
  279. * efi_get_virtmap() - create a virtual mapping for the EFI memory map
  280. *
  281. * This function populates the virt_addr fields of all memory region descriptors
  282. * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
  283. * are also copied to @runtime_map, and their total count is returned in @count.
  284. */
  285. void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
  286. unsigned long desc_size, efi_memory_desc_t *runtime_map,
  287. int *count)
  288. {
  289. u64 efi_virt_base = virtmap_base;
  290. efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
  291. int l;
  292. /*
  293. * To work around potential issues with the Properties Table feature
  294. * introduced in UEFI 2.5, which may split PE/COFF executable images
  295. * in memory into several RuntimeServicesCode and RuntimeServicesData
  296. * regions, we need to preserve the relative offsets between adjacent
  297. * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
  298. * The easiest way to find adjacent regions is to sort the memory map
  299. * before traversing it.
  300. */
  301. sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
  302. for (l = 0; l < map_size; l += desc_size, prev = in) {
  303. u64 paddr, size;
  304. in = (void *)memory_map + l;
  305. if (!(in->attribute & EFI_MEMORY_RUNTIME))
  306. continue;
  307. paddr = in->phys_addr;
  308. size = in->num_pages * EFI_PAGE_SIZE;
  309. /*
  310. * Make the mapping compatible with 64k pages: this allows
  311. * a 4k page size kernel to kexec a 64k page size kernel and
  312. * vice versa.
  313. */
  314. if (!regions_are_adjacent(prev, in) ||
  315. !regions_have_compatible_memory_type_attrs(prev, in)) {
  316. paddr = round_down(in->phys_addr, SZ_64K);
  317. size += in->phys_addr - paddr;
  318. /*
  319. * Avoid wasting memory on PTEs by choosing a virtual
  320. * base that is compatible with section mappings if this
  321. * region has the appropriate size and physical
  322. * alignment. (Sections are 2 MB on 4k granule kernels)
  323. */
  324. if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
  325. efi_virt_base = round_up(efi_virt_base, SZ_2M);
  326. else
  327. efi_virt_base = round_up(efi_virt_base, SZ_64K);
  328. }
  329. in->virt_addr = efi_virt_base + in->phys_addr - paddr;
  330. efi_virt_base += size;
  331. memcpy(out, in, desc_size);
  332. out = (void *)out + desc_size;
  333. ++*count;
  334. }
  335. }