arm-stub.c 12 KB

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