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