arm-stub.c 12 KB

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