arm-stub.c 12 KB

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