14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 2bf457786SArd Biesheuvel /* 3bf457786SArd Biesheuvel * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org> 4bf457786SArd Biesheuvel * 5bf457786SArd Biesheuvel * This file implements the EFI boot stub for the arm64 kernel. 6bf457786SArd Biesheuvel * Adapted from ARM version by Mark Salter <msalter@redhat.com> 7bf457786SArd Biesheuvel */ 80426a4e6SArd Biesheuvel 90426a4e6SArd Biesheuvel /* 100426a4e6SArd Biesheuvel * To prevent the compiler from emitting GOT-indirected (and thus absolute) 110426a4e6SArd Biesheuvel * references to the section markers, override their visibility as 'hidden' 120426a4e6SArd Biesheuvel */ 130426a4e6SArd Biesheuvel #pragma GCC visibility push(hidden) 140426a4e6SArd Biesheuvel #include <asm/sections.h> 150426a4e6SArd Biesheuvel #pragma GCC visibility pop 160426a4e6SArd Biesheuvel 17bf457786SArd Biesheuvel #include <linux/efi.h> 18bf457786SArd Biesheuvel #include <asm/efi.h> 19170976bcSMark Rutland #include <asm/memory.h> 2042b55734SArd Biesheuvel #include <asm/sysreg.h> 21bf457786SArd Biesheuvel 222b5fe07aSArd Biesheuvel #include "efistub.h" 232b5fe07aSArd Biesheuvel 24cd33a5c1SArd Biesheuvel efi_status_t check_platform_features(void) 2542b55734SArd Biesheuvel { 2642b55734SArd Biesheuvel u64 tg; 2742b55734SArd Biesheuvel 2842b55734SArd Biesheuvel /* UEFI mandates support for 4 KB granularity, no need to check */ 2942b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_4K_PAGES)) 3042b55734SArd Biesheuvel return EFI_SUCCESS; 3142b55734SArd Biesheuvel 3242b55734SArd Biesheuvel tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf; 3342b55734SArd Biesheuvel if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) { 3442b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_64K_PAGES)) 358173ec79SArd Biesheuvel pr_efi_err("This 64 KB granular kernel is not supported by your CPU\n"); 3642b55734SArd Biesheuvel else 378173ec79SArd Biesheuvel pr_efi_err("This 16 KB granular kernel is not supported by your CPU\n"); 3842b55734SArd Biesheuvel return EFI_UNSUPPORTED; 3942b55734SArd Biesheuvel } 4042b55734SArd Biesheuvel return EFI_SUCCESS; 4142b55734SArd Biesheuvel } 42bf457786SArd Biesheuvel 43cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr, 44bf457786SArd Biesheuvel unsigned long *image_size, 45bf457786SArd Biesheuvel unsigned long *reserve_addr, 46bf457786SArd Biesheuvel unsigned long *reserve_size, 47bf457786SArd Biesheuvel unsigned long dram_base, 48bf457786SArd Biesheuvel efi_loaded_image_t *image) 49bf457786SArd Biesheuvel { 50bf457786SArd Biesheuvel efi_status_t status; 51bf457786SArd Biesheuvel unsigned long kernel_size, kernel_memsize = 0; 52bf457786SArd Biesheuvel void *old_image_addr = (void *)*image_addr; 532dc10ad8SLinus Torvalds unsigned long preferred_offset; 542b5fe07aSArd Biesheuvel u64 phys_seed = 0; 552b5fe07aSArd Biesheuvel 562b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 5760f38de7SArd Biesheuvel if (!nokaslr()) { 58cd33a5c1SArd Biesheuvel status = efi_get_random_bytes(sizeof(phys_seed), 592b5fe07aSArd Biesheuvel (u8 *)&phys_seed); 602b5fe07aSArd Biesheuvel if (status == EFI_NOT_FOUND) { 618173ec79SArd Biesheuvel pr_efi("EFI_RNG_PROTOCOL unavailable, no randomness supplied\n"); 622b5fe07aSArd Biesheuvel } else if (status != EFI_SUCCESS) { 638173ec79SArd Biesheuvel pr_efi_err("efi_get_random_bytes() failed\n"); 642b5fe07aSArd Biesheuvel return status; 652b5fe07aSArd Biesheuvel } 662b5fe07aSArd Biesheuvel } else { 678173ec79SArd Biesheuvel pr_efi("KASLR disabled on kernel command line\n"); 682b5fe07aSArd Biesheuvel } 692b5fe07aSArd Biesheuvel } 702dc10ad8SLinus Torvalds 712dc10ad8SLinus Torvalds /* 722dc10ad8SLinus Torvalds * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond 732dc10ad8SLinus Torvalds * a 2 MB aligned base, which itself may be lower than dram_base, as 742dc10ad8SLinus Torvalds * long as the resulting offset equals or exceeds it. 752dc10ad8SLinus Torvalds */ 762b5fe07aSArd Biesheuvel preferred_offset = round_down(dram_base, MIN_KIMG_ALIGN) + TEXT_OFFSET; 772dc10ad8SLinus Torvalds if (preferred_offset < dram_base) 782b5fe07aSArd Biesheuvel preferred_offset += MIN_KIMG_ALIGN; 79bf457786SArd Biesheuvel 80bf457786SArd Biesheuvel kernel_size = _edata - _text; 81bf457786SArd Biesheuvel kernel_memsize = kernel_size + (_end - _edata); 82bf457786SArd Biesheuvel 832b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { 84bf457786SArd Biesheuvel /* 856f26b367SArd Biesheuvel * If CONFIG_DEBUG_ALIGN_RODATA is not set, produce a 866f26b367SArd Biesheuvel * displacement in the interval [0, MIN_KIMG_ALIGN) that 87170976bcSMark Rutland * doesn't violate this kernel's de-facto alignment 88170976bcSMark Rutland * constraints. 896f26b367SArd Biesheuvel */ 90170976bcSMark Rutland u32 mask = (MIN_KIMG_ALIGN - 1) & ~(EFI_KIMG_ALIGN - 1); 916f26b367SArd Biesheuvel u32 offset = !IS_ENABLED(CONFIG_DEBUG_ALIGN_RODATA) ? 926f26b367SArd Biesheuvel (phys_seed >> 32) & mask : TEXT_OFFSET; 936f26b367SArd Biesheuvel 946f26b367SArd Biesheuvel /* 954f74d72aSMark Rutland * With CONFIG_RANDOMIZE_TEXT_OFFSET=y, TEXT_OFFSET may not 964f74d72aSMark Rutland * be a multiple of EFI_KIMG_ALIGN, and we must ensure that 974f74d72aSMark Rutland * we preserve the misalignment of 'offset' relative to 984f74d72aSMark Rutland * EFI_KIMG_ALIGN so that statically allocated objects whose 994f74d72aSMark Rutland * alignment exceeds PAGE_SIZE appear correctly aligned in 1004f74d72aSMark Rutland * memory. 1014f74d72aSMark Rutland */ 1024f74d72aSMark Rutland offset |= TEXT_OFFSET % EFI_KIMG_ALIGN; 1034f74d72aSMark Rutland 1044f74d72aSMark Rutland /* 1052b5fe07aSArd Biesheuvel * If KASLR is enabled, and we have some randomness available, 1062b5fe07aSArd Biesheuvel * locate the kernel at a randomized offset in physical memory. 1072b5fe07aSArd Biesheuvel */ 1086f26b367SArd Biesheuvel *reserve_size = kernel_memsize + offset; 109cd33a5c1SArd Biesheuvel status = efi_random_alloc(*reserve_size, 1102b5fe07aSArd Biesheuvel MIN_KIMG_ALIGN, reserve_addr, 1116f26b367SArd Biesheuvel (u32)phys_seed); 1122b5fe07aSArd Biesheuvel 1136f26b367SArd Biesheuvel *image_addr = *reserve_addr + offset; 1142b5fe07aSArd Biesheuvel } else { 1152b5fe07aSArd Biesheuvel /* 1162b5fe07aSArd Biesheuvel * Else, try a straight allocation at the preferred offset. 117bf457786SArd Biesheuvel * This will work around the issue where, if dram_base == 0x0, 118bf457786SArd Biesheuvel * efi_low_alloc() refuses to allocate at 0x0 (to prevent the 119bf457786SArd Biesheuvel * address of the allocation to be mistaken for a FAIL return 120bf457786SArd Biesheuvel * value or a NULL pointer). It will also ensure that, on 121bf457786SArd Biesheuvel * platforms where the [dram_base, dram_base + TEXT_OFFSET) 122bf457786SArd Biesheuvel * interval is partially occupied by the firmware (like on APM 123bf457786SArd Biesheuvel * Mustang), we can still place the kernel at the address 124bf457786SArd Biesheuvel * 'dram_base + TEXT_OFFSET'. 125bf457786SArd Biesheuvel */ 1262b5fe07aSArd Biesheuvel if (*image_addr == preferred_offset) 1272b5fe07aSArd Biesheuvel return EFI_SUCCESS; 1282b5fe07aSArd Biesheuvel 1292dc10ad8SLinus Torvalds *image_addr = *reserve_addr = preferred_offset; 1302b5fe07aSArd Biesheuvel *reserve_size = round_up(kernel_memsize, EFI_ALLOC_ALIGN); 1312b5fe07aSArd Biesheuvel 132bf457786SArd Biesheuvel status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS, 1332b5fe07aSArd Biesheuvel EFI_LOADER_DATA, 1342b5fe07aSArd Biesheuvel *reserve_size / EFI_PAGE_SIZE, 135bf457786SArd Biesheuvel (efi_physical_addr_t *)reserve_addr); 1362b5fe07aSArd Biesheuvel } 1372b5fe07aSArd Biesheuvel 138bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1392b5fe07aSArd Biesheuvel *reserve_size = kernel_memsize + TEXT_OFFSET; 140cd33a5c1SArd Biesheuvel status = efi_low_alloc(*reserve_size, 1412b5fe07aSArd Biesheuvel MIN_KIMG_ALIGN, reserve_addr); 142bf457786SArd Biesheuvel 143bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1448173ec79SArd Biesheuvel pr_efi_err("Failed to relocate kernel\n"); 1452b5fe07aSArd Biesheuvel *reserve_size = 0; 146bf457786SArd Biesheuvel return status; 147bf457786SArd Biesheuvel } 148bf457786SArd Biesheuvel *image_addr = *reserve_addr + TEXT_OFFSET; 149bf457786SArd Biesheuvel } 150bf457786SArd Biesheuvel memcpy((void *)*image_addr, old_image_addr, kernel_size); 151bf457786SArd Biesheuvel 152bf457786SArd Biesheuvel return EFI_SUCCESS; 153bf457786SArd Biesheuvel } 154