arm-stub.c 10 KB

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