arm-stub.c 12 KB

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