xref: /linux/drivers/firmware/efi/libstub/arm64-stub.c (revision a460513ed4b6994bfeb7bd86f72853140bc1ac12)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013, 2014 Linaro Ltd;  <roy.franz@linaro.org>
4  *
5  * This file implements the EFI boot stub for the arm64 kernel.
6  * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7  */
8 
9 
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 #include <asm/memory.h>
13 #include <asm/sections.h>
14 #include <asm/sysreg.h>
15 
16 #include "efistub.h"
17 
18 efi_status_t check_platform_features(void)
19 {
20 	u64 tg;
21 
22 	/* UEFI mandates support for 4 KB granularity, no need to check */
23 	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
24 		return EFI_SUCCESS;
25 
26 	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
27 	if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
28 		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29 			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
30 		else
31 			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
32 		return EFI_UNSUPPORTED;
33 	}
34 	return EFI_SUCCESS;
35 }
36 
37 /*
38  * Although relocatable kernels can fix up the misalignment with respect to
39  * MIN_KIMG_ALIGN, the resulting virtual text addresses are subtly out of
40  * sync with those recorded in the vmlinux when kaslr is disabled but the
41  * image required relocation anyway. Therefore retain 2M alignment unless
42  * KASLR is in use.
43  */
44 static u64 min_kimg_align(void)
45 {
46 	return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
47 }
48 
49 efi_status_t handle_kernel_image(unsigned long *image_addr,
50 				 unsigned long *image_size,
51 				 unsigned long *reserve_addr,
52 				 unsigned long *reserve_size,
53 				 efi_loaded_image_t *image)
54 {
55 	efi_status_t status;
56 	unsigned long kernel_size, kernel_memsize = 0;
57 	u32 phys_seed = 0;
58 
59 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
60 		if (!efi_nokaslr) {
61 			status = efi_get_random_bytes(sizeof(phys_seed),
62 						      (u8 *)&phys_seed);
63 			if (status == EFI_NOT_FOUND) {
64 				efi_info("EFI_RNG_PROTOCOL unavailable\n");
65 				efi_nokaslr = true;
66 			} else if (status != EFI_SUCCESS) {
67 				efi_err("efi_get_random_bytes() failed (0x%lx)\n",
68 					status);
69 				efi_nokaslr = true;
70 			}
71 		} else {
72 			efi_info("KASLR disabled on kernel command line\n");
73 		}
74 	}
75 
76 	if (image->image_base != _text)
77 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
78 
79 	kernel_size = _edata - _text;
80 	kernel_memsize = kernel_size + (_end - _edata);
81 	*reserve_size = kernel_memsize;
82 
83 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
84 		/*
85 		 * If KASLR is enabled, and we have some randomness available,
86 		 * locate the kernel at a randomized offset in physical memory.
87 		 */
88 		status = efi_random_alloc(*reserve_size, min_kimg_align(),
89 					  reserve_addr, phys_seed);
90 	} else {
91 		status = EFI_OUT_OF_RESOURCES;
92 	}
93 
94 	if (status != EFI_SUCCESS) {
95 		if (IS_ALIGNED((u64)_text, min_kimg_align())) {
96 			/*
97 			 * Just execute from wherever we were loaded by the
98 			 * UEFI PE/COFF loader if the alignment is suitable.
99 			 */
100 			*image_addr = (u64)_text;
101 			*reserve_size = 0;
102 			return EFI_SUCCESS;
103 		}
104 
105 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
106 						    ULONG_MAX, min_kimg_align());
107 
108 		if (status != EFI_SUCCESS) {
109 			efi_err("Failed to relocate kernel\n");
110 			*reserve_size = 0;
111 			return status;
112 		}
113 	}
114 
115 	*image_addr = *reserve_addr;
116 	memcpy((void *)*image_addr, _text, kernel_size);
117 
118 	return EFI_SUCCESS;
119 }
120