1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file implements the EFI boot stub for the arm64 kernel.
6*4882a593Smuzhiyun * Adapted from ARM version by Mark Salter <msalter@redhat.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/efi.h>
11*4882a593Smuzhiyun #include <asm/efi.h>
12*4882a593Smuzhiyun #include <asm/memory.h>
13*4882a593Smuzhiyun #include <asm/sections.h>
14*4882a593Smuzhiyun #include <asm/sysreg.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include "efistub.h"
17*4882a593Smuzhiyun
check_platform_features(void)18*4882a593Smuzhiyun efi_status_t check_platform_features(void)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun u64 tg;
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /* UEFI mandates support for 4 KB granularity, no need to check */
23*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
24*4882a593Smuzhiyun return EFI_SUCCESS;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf;
27*4882a593Smuzhiyun if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) {
28*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
29*4882a593Smuzhiyun efi_err("This 64 KB granular kernel is not supported by your CPU\n");
30*4882a593Smuzhiyun else
31*4882a593Smuzhiyun efi_err("This 16 KB granular kernel is not supported by your CPU\n");
32*4882a593Smuzhiyun return EFI_UNSUPPORTED;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun return EFI_SUCCESS;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
39*4882a593Smuzhiyun * to provide space, and fail to zero it). Check for this condition by double
40*4882a593Smuzhiyun * checking that the first and the last byte of the image are covered by the
41*4882a593Smuzhiyun * same EFI memory map entry.
42*4882a593Smuzhiyun */
check_image_region(u64 base,u64 size)43*4882a593Smuzhiyun static bool check_image_region(u64 base, u64 size)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun unsigned long map_size, desc_size, buff_size;
46*4882a593Smuzhiyun efi_memory_desc_t *memory_map;
47*4882a593Smuzhiyun struct efi_boot_memmap map;
48*4882a593Smuzhiyun efi_status_t status;
49*4882a593Smuzhiyun bool ret = false;
50*4882a593Smuzhiyun int map_offset;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun map.map = &memory_map;
53*4882a593Smuzhiyun map.map_size = &map_size;
54*4882a593Smuzhiyun map.desc_size = &desc_size;
55*4882a593Smuzhiyun map.desc_ver = NULL;
56*4882a593Smuzhiyun map.key_ptr = NULL;
57*4882a593Smuzhiyun map.buff_size = &buff_size;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun status = efi_get_memory_map(&map);
60*4882a593Smuzhiyun if (status != EFI_SUCCESS)
61*4882a593Smuzhiyun return false;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun for (map_offset = 0; map_offset < map_size; map_offset += desc_size) {
64*4882a593Smuzhiyun efi_memory_desc_t *md = (void *)memory_map + map_offset;
65*4882a593Smuzhiyun u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * Find the region that covers base, and return whether
69*4882a593Smuzhiyun * it covers base+size bytes.
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun if (base >= md->phys_addr && base < end) {
72*4882a593Smuzhiyun ret = (base + size) <= end;
73*4882a593Smuzhiyun break;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun efi_bs_call(free_pool, memory_map);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun return ret;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
handle_kernel_image(unsigned long * image_addr,unsigned long * image_size,unsigned long * reserve_addr,unsigned long * reserve_size,efi_loaded_image_t * image)82*4882a593Smuzhiyun efi_status_t handle_kernel_image(unsigned long *image_addr,
83*4882a593Smuzhiyun unsigned long *image_size,
84*4882a593Smuzhiyun unsigned long *reserve_addr,
85*4882a593Smuzhiyun unsigned long *reserve_size,
86*4882a593Smuzhiyun efi_loaded_image_t *image)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun efi_status_t status;
89*4882a593Smuzhiyun unsigned long kernel_size, kernel_memsize = 0;
90*4882a593Smuzhiyun u32 phys_seed = 0;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * Although relocatable kernels can fix up the misalignment with
94*4882a593Smuzhiyun * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
95*4882a593Smuzhiyun * subtly out of sync with those recorded in the vmlinux when kaslr is
96*4882a593Smuzhiyun * disabled but the image required relocation anyway. Therefore retain
97*4882a593Smuzhiyun * 2M alignment if KASLR was explicitly disabled, even if it was not
98*4882a593Smuzhiyun * going to be activated to begin with.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
103*4882a593Smuzhiyun if (!efi_nokaslr) {
104*4882a593Smuzhiyun status = efi_get_random_bytes(sizeof(phys_seed),
105*4882a593Smuzhiyun (u8 *)&phys_seed);
106*4882a593Smuzhiyun if (status == EFI_NOT_FOUND) {
107*4882a593Smuzhiyun efi_info("EFI_RNG_PROTOCOL unavailable, KASLR will be disabled\n");
108*4882a593Smuzhiyun efi_nokaslr = true;
109*4882a593Smuzhiyun } else if (status != EFI_SUCCESS) {
110*4882a593Smuzhiyun efi_err("efi_get_random_bytes() failed (0x%lx), KASLR will be disabled\n",
111*4882a593Smuzhiyun status);
112*4882a593Smuzhiyun efi_nokaslr = true;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun } else {
115*4882a593Smuzhiyun efi_info("KASLR disabled on kernel command line\n");
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (image->image_base != _text)
120*4882a593Smuzhiyun efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
123*4882a593Smuzhiyun efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
124*4882a593Smuzhiyun SEGMENT_ALIGN >> 10);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun kernel_size = _edata - _text;
127*4882a593Smuzhiyun kernel_memsize = kernel_size + (_end - _edata);
128*4882a593Smuzhiyun *reserve_size = kernel_memsize;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * If KASLR is enabled, and we have some randomness available,
133*4882a593Smuzhiyun * locate the kernel at a randomized offset in physical memory.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun status = efi_random_alloc(*reserve_size, min_kimg_align,
136*4882a593Smuzhiyun reserve_addr, phys_seed);
137*4882a593Smuzhiyun } else {
138*4882a593Smuzhiyun status = EFI_OUT_OF_RESOURCES;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (status != EFI_SUCCESS) {
142*4882a593Smuzhiyun if (!check_image_region((u64)_text, kernel_memsize)) {
143*4882a593Smuzhiyun efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
144*4882a593Smuzhiyun } else if (IS_ALIGNED((u64)_text, min_kimg_align)) {
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * Just execute from wherever we were loaded by the
147*4882a593Smuzhiyun * UEFI PE/COFF loader if the alignment is suitable.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun *image_addr = (u64)_text;
150*4882a593Smuzhiyun *reserve_size = 0;
151*4882a593Smuzhiyun return EFI_SUCCESS;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
155*4882a593Smuzhiyun ULONG_MAX, min_kimg_align);
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun if (status != EFI_SUCCESS) {
158*4882a593Smuzhiyun efi_err("Failed to relocate kernel\n");
159*4882a593Smuzhiyun *reserve_size = 0;
160*4882a593Smuzhiyun return status;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun *image_addr = *reserve_addr;
165*4882a593Smuzhiyun memcpy((void *)*image_addr, _text, kernel_size);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun return EFI_SUCCESS;
168*4882a593Smuzhiyun }
169