arm-stub.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. status = check_platform_features(sys_table);
  161. if (status != EFI_SUCCESS)
  162. goto fail;
  163. /*
  164. * Get a handle to the loaded image protocol. This is used to get
  165. * information about the running image, such as size and the command
  166. * line.
  167. */
  168. status = sys_table->boottime->handle_protocol(handle,
  169. &loaded_image_proto, (void *)&image);
  170. if (status != EFI_SUCCESS) {
  171. pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
  172. goto fail;
  173. }
  174. dram_base = get_dram_base(sys_table);
  175. if (dram_base == EFI_ERROR) {
  176. pr_efi_err(sys_table, "Failed to find DRAM base\n");
  177. goto fail;
  178. }
  179. status = handle_kernel_image(sys_table, image_addr, &image_size,
  180. &reserve_addr,
  181. &reserve_size,
  182. dram_base, image);
  183. if (status != EFI_SUCCESS) {
  184. pr_efi_err(sys_table, "Failed to relocate kernel\n");
  185. goto fail;
  186. }
  187. /*
  188. * Get the command line from EFI, using the LOADED_IMAGE
  189. * protocol. We are going to copy the command line into the
  190. * device tree, so this can be allocated anywhere.
  191. */
  192. cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
  193. if (!cmdline_ptr) {
  194. pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
  195. goto fail_free_image;
  196. }
  197. status = efi_parse_options(cmdline_ptr);
  198. if (status != EFI_SUCCESS)
  199. pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n");
  200. /*
  201. * Unauthenticated device tree data is a security hazard, so
  202. * ignore 'dtb=' unless UEFI Secure Boot is disabled.
  203. */
  204. if (efi_secureboot_enabled(sys_table)) {
  205. pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
  206. } else {
  207. status = handle_cmdline_files(sys_table, image, cmdline_ptr,
  208. "dtb=",
  209. ~0UL, &fdt_addr, &fdt_size);
  210. if (status != EFI_SUCCESS) {
  211. pr_efi_err(sys_table, "Failed to load device tree!\n");
  212. goto fail_free_cmdline;
  213. }
  214. }
  215. if (fdt_addr) {
  216. pr_efi(sys_table, "Using DTB from command line\n");
  217. } else {
  218. /* Look for a device tree configuration table entry. */
  219. fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
  220. if (fdt_addr)
  221. pr_efi(sys_table, "Using DTB from configuration table\n");
  222. }
  223. if (!fdt_addr)
  224. pr_efi(sys_table, "Generating empty DTB\n");
  225. status = handle_cmdline_files(sys_table, image, cmdline_ptr,
  226. "initrd=", dram_base + SZ_512M,
  227. (unsigned long *)&initrd_addr,
  228. (unsigned long *)&initrd_size);
  229. if (status != EFI_SUCCESS)
  230. pr_efi_err(sys_table, "Failed initrd from command line!\n");
  231. new_fdt_addr = fdt_addr;
  232. status = allocate_new_fdt_and_exit_boot(sys_table, handle,
  233. &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
  234. initrd_addr, initrd_size, cmdline_ptr,
  235. fdt_addr, fdt_size);
  236. /*
  237. * If all went well, we need to return the FDT address to the
  238. * calling function so it can be passed to kernel as part of
  239. * the kernel boot protocol.
  240. */
  241. if (status == EFI_SUCCESS)
  242. return new_fdt_addr;
  243. pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
  244. efi_free(sys_table, initrd_size, initrd_addr);
  245. efi_free(sys_table, fdt_size, fdt_addr);
  246. fail_free_cmdline:
  247. efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
  248. fail_free_image:
  249. efi_free(sys_table, image_size, *image_addr);
  250. efi_free(sys_table, reserve_size, reserve_addr);
  251. fail:
  252. return EFI_ERROR;
  253. }
  254. /*
  255. * This is the base address at which to start allocating virtual memory ranges
  256. * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
  257. * any allocation we choose, and eliminate the risk of a conflict after kexec.
  258. * The value chosen is the largest non-zero power of 2 suitable for this purpose
  259. * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
  260. * be mapped efficiently.
  261. * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
  262. * map everything below 1 GB.
  263. */
  264. #define EFI_RT_VIRTUAL_BASE SZ_512M
  265. static int cmp_mem_desc(const void *l, const void *r)
  266. {
  267. const efi_memory_desc_t *left = l, *right = r;
  268. return (left->phys_addr > right->phys_addr) ? 1 : -1;
  269. }
  270. /*
  271. * Returns whether region @left ends exactly where region @right starts,
  272. * or false if either argument is NULL.
  273. */
  274. static bool regions_are_adjacent(efi_memory_desc_t *left,
  275. efi_memory_desc_t *right)
  276. {
  277. u64 left_end;
  278. if (left == NULL || right == NULL)
  279. return false;
  280. left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
  281. return left_end == right->phys_addr;
  282. }
  283. /*
  284. * Returns whether region @left and region @right have compatible memory type
  285. * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
  286. */
  287. static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
  288. efi_memory_desc_t *right)
  289. {
  290. static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
  291. EFI_MEMORY_WC | EFI_MEMORY_UC |
  292. EFI_MEMORY_RUNTIME;
  293. return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
  294. }
  295. /*
  296. * efi_get_virtmap() - create a virtual mapping for the EFI memory map
  297. *
  298. * This function populates the virt_addr fields of all memory region descriptors
  299. * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
  300. * are also copied to @runtime_map, and their total count is returned in @count.
  301. */
  302. void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
  303. unsigned long desc_size, efi_memory_desc_t *runtime_map,
  304. int *count)
  305. {
  306. u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
  307. efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
  308. int l;
  309. /*
  310. * To work around potential issues with the Properties Table feature
  311. * introduced in UEFI 2.5, which may split PE/COFF executable images
  312. * in memory into several RuntimeServicesCode and RuntimeServicesData
  313. * regions, we need to preserve the relative offsets between adjacent
  314. * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
  315. * The easiest way to find adjacent regions is to sort the memory map
  316. * before traversing it.
  317. */
  318. sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
  319. for (l = 0; l < map_size; l += desc_size, prev = in) {
  320. u64 paddr, size;
  321. in = (void *)memory_map + l;
  322. if (!(in->attribute & EFI_MEMORY_RUNTIME))
  323. continue;
  324. paddr = in->phys_addr;
  325. size = in->num_pages * EFI_PAGE_SIZE;
  326. /*
  327. * Make the mapping compatible with 64k pages: this allows
  328. * a 4k page size kernel to kexec a 64k page size kernel and
  329. * vice versa.
  330. */
  331. if (!regions_are_adjacent(prev, in) ||
  332. !regions_have_compatible_memory_type_attrs(prev, in)) {
  333. paddr = round_down(in->phys_addr, SZ_64K);
  334. size += in->phys_addr - paddr;
  335. /*
  336. * Avoid wasting memory on PTEs by choosing a virtual
  337. * base that is compatible with section mappings if this
  338. * region has the appropriate size and physical
  339. * alignment. (Sections are 2 MB on 4k granule kernels)
  340. */
  341. if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
  342. efi_virt_base = round_up(efi_virt_base, SZ_2M);
  343. else
  344. efi_virt_base = round_up(efi_virt_base, SZ_64K);
  345. }
  346. in->virt_addr = efi_virt_base + in->phys_addr - paddr;
  347. efi_virt_base += size;
  348. memcpy(out, in, desc_size);
  349. out = (void *)out + desc_size;
  350. ++*count;
  351. }
  352. }