efi-stub.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EFI stub implementation that is shared by arm and arm64 architectures.
  4. * This should be #included by the EFI stub implementation files.
  5. *
  6. * Copyright (C) 2013,2014 Linaro Limited
  7. * Roy Franz <[email protected]
  8. * Copyright (C) 2013 Red Hat, Inc.
  9. * Mark Salter <[email protected]>
  10. */
  11. #include <linux/efi.h>
  12. #include <asm/efi.h>
  13. #include "efistub.h"
  14. /*
  15. * This is the base address at which to start allocating virtual memory ranges
  16. * for UEFI Runtime Services.
  17. *
  18. * For ARM/ARM64:
  19. * This is in the low TTBR0 range so that we can use
  20. * any allocation we choose, and eliminate the risk of a conflict after kexec.
  21. * The value chosen is the largest non-zero power of 2 suitable for this purpose
  22. * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
  23. * be mapped efficiently.
  24. * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
  25. * map everything below 1 GB. (512 MB is a reasonable upper bound for the
  26. * entire footprint of the UEFI runtime services memory regions)
  27. *
  28. * For RISC-V:
  29. * There is no specific reason for which, this address (512MB) can't be used
  30. * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
  31. * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
  32. * as well to minimize the code churn.
  33. */
  34. #define EFI_RT_VIRTUAL_BASE SZ_512M
  35. #define EFI_RT_VIRTUAL_SIZE SZ_512M
  36. #ifdef CONFIG_ARM64
  37. # define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64
  38. #elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
  39. # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_MIN
  40. #else /* Only if TASK_SIZE is a constant */
  41. # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
  42. #endif
  43. /*
  44. * Some architectures map the EFI regions into the kernel's linear map using a
  45. * fixed offset.
  46. */
  47. #ifndef EFI_RT_VIRTUAL_OFFSET
  48. #define EFI_RT_VIRTUAL_OFFSET 0
  49. #endif
  50. static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
  51. static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
  52. static struct screen_info *setup_graphics(void)
  53. {
  54. efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  55. efi_status_t status;
  56. unsigned long size;
  57. void **gop_handle = NULL;
  58. struct screen_info *si = NULL;
  59. size = 0;
  60. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  61. &gop_proto, NULL, &size, gop_handle);
  62. if (status == EFI_BUFFER_TOO_SMALL) {
  63. si = alloc_screen_info();
  64. if (!si)
  65. return NULL;
  66. status = efi_setup_gop(si, &gop_proto, size);
  67. if (status != EFI_SUCCESS) {
  68. free_screen_info(si);
  69. return NULL;
  70. }
  71. }
  72. return si;
  73. }
  74. static void install_memreserve_table(void)
  75. {
  76. struct linux_efi_memreserve *rsv;
  77. efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
  78. efi_status_t status;
  79. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
  80. (void **)&rsv);
  81. if (status != EFI_SUCCESS) {
  82. efi_err("Failed to allocate memreserve entry!\n");
  83. return;
  84. }
  85. rsv->next = 0;
  86. rsv->size = 0;
  87. atomic_set(&rsv->count, 0);
  88. status = efi_bs_call(install_configuration_table,
  89. &memreserve_table_guid, rsv);
  90. if (status != EFI_SUCCESS)
  91. efi_err("Failed to install memreserve config table!\n");
  92. }
  93. static u32 get_supported_rt_services(void)
  94. {
  95. const efi_rt_properties_table_t *rt_prop_table;
  96. u32 supported = EFI_RT_SUPPORTED_ALL;
  97. rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
  98. if (rt_prop_table)
  99. supported &= rt_prop_table->runtime_services_supported;
  100. return supported;
  101. }
  102. /*
  103. * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
  104. * that is described in the PE/COFF header. Most of the code is the same
  105. * for both archictectures, with the arch-specific code provided in the
  106. * handle_kernel_image() function.
  107. */
  108. efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
  109. efi_system_table_t *sys_table_arg)
  110. {
  111. efi_loaded_image_t *image;
  112. efi_status_t status;
  113. unsigned long image_addr;
  114. unsigned long image_size = 0;
  115. /* addr/point and size pairs for memory management*/
  116. char *cmdline_ptr = NULL;
  117. int cmdline_size = 0;
  118. efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
  119. unsigned long reserve_addr = 0;
  120. unsigned long reserve_size = 0;
  121. struct screen_info *si;
  122. efi_properties_table_t *prop_tbl;
  123. efi_system_table = sys_table_arg;
  124. /* Check if we were booted by the EFI firmware */
  125. if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  126. status = EFI_INVALID_PARAMETER;
  127. goto fail;
  128. }
  129. status = check_platform_features();
  130. if (status != EFI_SUCCESS)
  131. goto fail;
  132. /*
  133. * Get a handle to the loaded image protocol. This is used to get
  134. * information about the running image, such as size and the command
  135. * line.
  136. */
  137. status = efi_bs_call(handle_protocol, handle, &loaded_image_proto,
  138. (void *)&image);
  139. if (status != EFI_SUCCESS) {
  140. efi_err("Failed to get loaded image protocol\n");
  141. goto fail;
  142. }
  143. /*
  144. * Get the command line from EFI, using the LOADED_IMAGE
  145. * protocol. We are going to copy the command line into the
  146. * device tree, so this can be allocated anywhere.
  147. */
  148. cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
  149. if (!cmdline_ptr) {
  150. efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
  151. status = EFI_OUT_OF_RESOURCES;
  152. goto fail;
  153. }
  154. if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
  155. IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
  156. cmdline_size == 0) {
  157. status = efi_parse_options(CONFIG_CMDLINE);
  158. if (status != EFI_SUCCESS) {
  159. efi_err("Failed to parse options\n");
  160. goto fail_free_cmdline;
  161. }
  162. }
  163. if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
  164. status = efi_parse_options(cmdline_ptr);
  165. if (status != EFI_SUCCESS) {
  166. efi_err("Failed to parse options\n");
  167. goto fail_free_cmdline;
  168. }
  169. }
  170. efi_info("Booting Linux Kernel...\n");
  171. si = setup_graphics();
  172. status = handle_kernel_image(&image_addr, &image_size,
  173. &reserve_addr,
  174. &reserve_size,
  175. image, handle);
  176. if (status != EFI_SUCCESS) {
  177. efi_err("Failed to relocate kernel\n");
  178. goto fail_free_screeninfo;
  179. }
  180. efi_retrieve_tpm2_eventlog();
  181. /* Ask the firmware to clear memory on unclean shutdown */
  182. efi_enable_reset_attack_mitigation();
  183. efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
  184. NULL);
  185. efi_random_get_seed();
  186. /*
  187. * If the NX PE data feature is enabled in the properties table, we
  188. * should take care not to create a virtual mapping that changes the
  189. * relative placement of runtime services code and data regions, as
  190. * they may belong to the same PE/COFF executable image in memory.
  191. * The easiest way to achieve that is to simply use a 1:1 mapping.
  192. */
  193. prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
  194. flat_va_mapping |= prop_tbl &&
  195. (prop_tbl->memory_protection_attribute &
  196. EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
  197. /* force efi_novamap if SetVirtualAddressMap() is unsupported */
  198. efi_novamap |= !(get_supported_rt_services() &
  199. EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
  200. /* hibernation expects the runtime regions to stay in the same place */
  201. if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
  202. /*
  203. * Randomize the base of the UEFI runtime services region.
  204. * Preserve the 2 MB alignment of the region by taking a
  205. * shift of 21 bit positions into account when scaling
  206. * the headroom value using a 32-bit random value.
  207. */
  208. static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
  209. EFI_RT_VIRTUAL_BASE -
  210. EFI_RT_VIRTUAL_SIZE;
  211. u32 rnd;
  212. status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
  213. if (status == EFI_SUCCESS) {
  214. virtmap_base = EFI_RT_VIRTUAL_BASE +
  215. (((headroom >> 21) * rnd) >> (32 - 21));
  216. }
  217. }
  218. install_memreserve_table();
  219. status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
  220. efi_free(image_size, image_addr);
  221. efi_free(reserve_size, reserve_addr);
  222. fail_free_screeninfo:
  223. free_screen_info(si);
  224. fail_free_cmdline:
  225. efi_bs_call(free_pool, cmdline_ptr);
  226. fail:
  227. return status;
  228. }
  229. /*
  230. * efi_allocate_virtmap() - create a pool allocation for the virtmap
  231. *
  232. * Create an allocation that is of sufficient size to hold all the memory
  233. * descriptors that will be passed to SetVirtualAddressMap() to inform the
  234. * firmware about the virtual mapping that will be used under the OS to call
  235. * into the firmware.
  236. */
  237. efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
  238. unsigned long *desc_size, u32 *desc_ver)
  239. {
  240. unsigned long size, mmap_key;
  241. efi_status_t status;
  242. /*
  243. * Use the size of the current memory map as an upper bound for the
  244. * size of the buffer we need to pass to SetVirtualAddressMap() to
  245. * cover all EFI_MEMORY_RUNTIME regions.
  246. */
  247. size = 0;
  248. status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,
  249. desc_ver);
  250. if (status != EFI_BUFFER_TOO_SMALL)
  251. return EFI_LOAD_ERROR;
  252. return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  253. (void **)virtmap);
  254. }
  255. /*
  256. * efi_get_virtmap() - create a virtual mapping for the EFI memory map
  257. *
  258. * This function populates the virt_addr fields of all memory region descriptors
  259. * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
  260. * are also copied to @runtime_map, and their total count is returned in @count.
  261. */
  262. void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
  263. unsigned long desc_size, efi_memory_desc_t *runtime_map,
  264. int *count)
  265. {
  266. u64 efi_virt_base = virtmap_base;
  267. efi_memory_desc_t *in, *out = runtime_map;
  268. int l;
  269. *count = 0;
  270. for (l = 0; l < map_size; l += desc_size) {
  271. u64 paddr, size;
  272. in = (void *)memory_map + l;
  273. if (!(in->attribute & EFI_MEMORY_RUNTIME))
  274. continue;
  275. paddr = in->phys_addr;
  276. size = in->num_pages * EFI_PAGE_SIZE;
  277. in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
  278. if (efi_novamap) {
  279. continue;
  280. }
  281. /*
  282. * Make the mapping compatible with 64k pages: this allows
  283. * a 4k page size kernel to kexec a 64k page size kernel and
  284. * vice versa.
  285. */
  286. if (!flat_va_mapping) {
  287. paddr = round_down(in->phys_addr, SZ_64K);
  288. size += in->phys_addr - paddr;
  289. /*
  290. * Avoid wasting memory on PTEs by choosing a virtual
  291. * base that is compatible with section mappings if this
  292. * region has the appropriate size and physical
  293. * alignment. (Sections are 2 MB on 4k granule kernels)
  294. */
  295. if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
  296. efi_virt_base = round_up(efi_virt_base, SZ_2M);
  297. else
  298. efi_virt_base = round_up(efi_virt_base, SZ_64K);
  299. in->virt_addr += efi_virt_base - paddr;
  300. efi_virt_base += size;
  301. }
  302. memcpy(out, in, desc_size);
  303. out = (void *)out + desc_size;
  304. ++*count;
  305. }
  306. }