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
check_platform_features(void)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 * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
39 * to provide space, and fail to zero it). Check for this condition by double
40 * checking that the first and the last byte of the image are covered by the
41 * same EFI memory map entry.
42 */
check_image_region(u64 base, u64 size)43 static bool check_image_region(u64 base, u64 size)
44 {
45 struct efi_boot_memmap *map;
46 efi_status_t status;
47 bool ret = false;
48 int map_offset;
49
50 status = efi_get_memory_map(&map, false);
51 if (status != EFI_SUCCESS)
52 return false;
53
54 for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) {
55 efi_memory_desc_t *md = (void *)map->map + map_offset;
56 u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
57
58 /*
59 * Find the region that covers base, and return whether
60 * it covers base+size bytes.
61 */
62 if (base >= md->phys_addr && base < end) {
63 ret = (base + size) <= end;
64 break;
65 }
66 }
67
68 efi_bs_call(free_pool, map);
69
70 return ret;
71 }
72
handle_kernel_image(unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr, unsigned long *reserve_size, efi_loaded_image_t *image)73 efi_status_t handle_kernel_image(unsigned long *image_addr,
74 unsigned long *image_size,
75 unsigned long *reserve_addr,
76 unsigned long *reserve_size,
77 efi_loaded_image_t *image)
78 {
79 efi_status_t status;
80 unsigned long kernel_size, kernel_memsize = 0;
81 u32 phys_seed = 0;
82
83 /*
84 * Although relocatable kernels can fix up the misalignment with
85 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
86 * subtly out of sync with those recorded in the vmlinux when kaslr is
87 * disabled but the image required relocation anyway. Therefore retain
88 * 2M alignment if KASLR was explicitly disabled, even if it was not
89 * going to be activated to begin with.
90 */
91 u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
92
93 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
94 if (!efi_nokaslr) {
95 status = efi_get_random_bytes(sizeof(phys_seed),
96 (u8 *)&phys_seed);
97 if (status == EFI_NOT_FOUND) {
98 efi_info("EFI_RNG_PROTOCOL unavailable, KASLR will be disabled\n");
99 efi_nokaslr = true;
100 } else if (status != EFI_SUCCESS) {
101 efi_err("efi_get_random_bytes() failed (0x%lx), KASLR will be disabled\n",
102 status);
103 efi_nokaslr = true;
104 }
105 } else {
106 efi_info("KASLR disabled on kernel command line\n");
107 }
108 }
109
110 if (image->image_base != _text)
111 efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
112
113 if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
114 efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
115 SEGMENT_ALIGN >> 10);
116
117 kernel_size = _edata - _text;
118 kernel_memsize = kernel_size + (_end - _edata);
119 *reserve_size = kernel_memsize;
120
121 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
122 /*
123 * If KASLR is enabled, and we have some randomness available,
124 * locate the kernel at a randomized offset in physical memory.
125 */
126 status = efi_random_alloc(*reserve_size, min_kimg_align,
127 reserve_addr, phys_seed);
128 } else {
129 status = EFI_OUT_OF_RESOURCES;
130 }
131
132 if (status != EFI_SUCCESS) {
133 if (!check_image_region((u64)_text, kernel_memsize)) {
134 efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
135 } else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
136 /*
137 * Just execute from wherever we were loaded by the
138 * UEFI PE/COFF loader if the alignment is suitable.
139 */
140 *image_addr = (u64)_text;
141 *reserve_size = 0;
142 return EFI_SUCCESS;
143 }
144
145 status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
146 ULONG_MAX, min_kimg_align);
147
148 if (status != EFI_SUCCESS) {
149 efi_err("Failed to relocate kernel\n");
150 *reserve_size = 0;
151 return status;
152 }
153 }
154
155 *image_addr = *reserve_addr;
156 memcpy((void *)*image_addr, _text, kernel_size);
157
158 return EFI_SUCCESS;
159 }
160