arm-stub.c 14 KB

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