arm-stub.c 11 KB

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