1bf457786SArd Biesheuvel /* 2bf457786SArd Biesheuvel * Copyright (C) 2013, 2014 Linaro Ltd; <roy.franz@linaro.org> 3bf457786SArd Biesheuvel * 4bf457786SArd Biesheuvel * This file implements the EFI boot stub for the arm64 kernel. 5bf457786SArd Biesheuvel * Adapted from ARM version by Mark Salter <msalter@redhat.com> 6bf457786SArd Biesheuvel * 7bf457786SArd Biesheuvel * This program is free software; you can redistribute it and/or modify 8bf457786SArd Biesheuvel * it under the terms of the GNU General Public License version 2 as 9bf457786SArd Biesheuvel * published by the Free Software Foundation. 10bf457786SArd Biesheuvel * 11bf457786SArd Biesheuvel */ 12bf457786SArd Biesheuvel #include <linux/efi.h> 13bf457786SArd Biesheuvel #include <asm/efi.h> 14bf457786SArd Biesheuvel #include <asm/sections.h> 1542b55734SArd Biesheuvel #include <asm/sysreg.h> 16bf457786SArd Biesheuvel 172b5fe07aSArd Biesheuvel #include "efistub.h" 182b5fe07aSArd Biesheuvel 1942b55734SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg) 2042b55734SArd Biesheuvel { 2142b55734SArd Biesheuvel u64 tg; 2242b55734SArd Biesheuvel 2342b55734SArd Biesheuvel /* UEFI mandates support for 4 KB granularity, no need to check */ 2442b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_4K_PAGES)) 2542b55734SArd Biesheuvel return EFI_SUCCESS; 2642b55734SArd Biesheuvel 2742b55734SArd Biesheuvel tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf; 2842b55734SArd Biesheuvel if (tg != ID_AA64MMFR0_TGRAN_SUPPORTED) { 2942b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_64K_PAGES)) 3042b55734SArd Biesheuvel pr_efi_err(sys_table_arg, "This 64 KB granular kernel is not supported by your CPU\n"); 3142b55734SArd Biesheuvel else 3242b55734SArd Biesheuvel pr_efi_err(sys_table_arg, "This 16 KB granular kernel is not supported by your CPU\n"); 3342b55734SArd Biesheuvel return EFI_UNSUPPORTED; 3442b55734SArd Biesheuvel } 3542b55734SArd Biesheuvel return EFI_SUCCESS; 3642b55734SArd Biesheuvel } 37bf457786SArd Biesheuvel 38dae31fd2SArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table_arg, 39bf457786SArd Biesheuvel unsigned long *image_addr, 40bf457786SArd Biesheuvel unsigned long *image_size, 41bf457786SArd Biesheuvel unsigned long *reserve_addr, 42bf457786SArd Biesheuvel unsigned long *reserve_size, 43bf457786SArd Biesheuvel unsigned long dram_base, 44bf457786SArd Biesheuvel efi_loaded_image_t *image) 45bf457786SArd Biesheuvel { 46bf457786SArd Biesheuvel efi_status_t status; 47bf457786SArd Biesheuvel unsigned long kernel_size, kernel_memsize = 0; 48bf457786SArd Biesheuvel void *old_image_addr = (void *)*image_addr; 492dc10ad8SLinus Torvalds unsigned long preferred_offset; 502b5fe07aSArd Biesheuvel u64 phys_seed = 0; 512b5fe07aSArd Biesheuvel 522b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 5360f38de7SArd Biesheuvel if (!nokaslr()) { 542b5fe07aSArd Biesheuvel status = efi_get_random_bytes(sys_table_arg, 552b5fe07aSArd Biesheuvel sizeof(phys_seed), 562b5fe07aSArd Biesheuvel (u8 *)&phys_seed); 572b5fe07aSArd Biesheuvel if (status == EFI_NOT_FOUND) { 582b5fe07aSArd Biesheuvel pr_efi(sys_table_arg, "EFI_RNG_PROTOCOL unavailable, no randomness supplied\n"); 592b5fe07aSArd Biesheuvel } else if (status != EFI_SUCCESS) { 602b5fe07aSArd Biesheuvel pr_efi_err(sys_table_arg, "efi_get_random_bytes() failed\n"); 612b5fe07aSArd Biesheuvel return status; 622b5fe07aSArd Biesheuvel } 632b5fe07aSArd Biesheuvel } else { 642b5fe07aSArd Biesheuvel pr_efi(sys_table_arg, "KASLR disabled on kernel command line\n"); 652b5fe07aSArd Biesheuvel } 662b5fe07aSArd Biesheuvel } 672dc10ad8SLinus Torvalds 682dc10ad8SLinus Torvalds /* 692dc10ad8SLinus Torvalds * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond 702dc10ad8SLinus Torvalds * a 2 MB aligned base, which itself may be lower than dram_base, as 712dc10ad8SLinus Torvalds * long as the resulting offset equals or exceeds it. 722dc10ad8SLinus Torvalds */ 732b5fe07aSArd Biesheuvel preferred_offset = round_down(dram_base, MIN_KIMG_ALIGN) + TEXT_OFFSET; 742dc10ad8SLinus Torvalds if (preferred_offset < dram_base) 752b5fe07aSArd Biesheuvel preferred_offset += MIN_KIMG_ALIGN; 76bf457786SArd Biesheuvel 77bf457786SArd Biesheuvel kernel_size = _edata - _text; 78bf457786SArd Biesheuvel kernel_memsize = kernel_size + (_end - _edata); 79bf457786SArd Biesheuvel 802b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { 81bf457786SArd Biesheuvel /* 826f26b367SArd Biesheuvel * If CONFIG_DEBUG_ALIGN_RODATA is not set, produce a 836f26b367SArd Biesheuvel * displacement in the interval [0, MIN_KIMG_ALIGN) that 846f26b367SArd Biesheuvel * is a multiple of the minimal segment alignment (SZ_64K) 856f26b367SArd Biesheuvel */ 866f26b367SArd Biesheuvel u32 mask = (MIN_KIMG_ALIGN - 1) & ~(SZ_64K - 1); 876f26b367SArd Biesheuvel u32 offset = !IS_ENABLED(CONFIG_DEBUG_ALIGN_RODATA) ? 886f26b367SArd Biesheuvel (phys_seed >> 32) & mask : TEXT_OFFSET; 896f26b367SArd Biesheuvel 906f26b367SArd Biesheuvel /* 912b5fe07aSArd Biesheuvel * If KASLR is enabled, and we have some randomness available, 922b5fe07aSArd Biesheuvel * locate the kernel at a randomized offset in physical memory. 932b5fe07aSArd Biesheuvel */ 946f26b367SArd Biesheuvel *reserve_size = kernel_memsize + offset; 952b5fe07aSArd Biesheuvel status = efi_random_alloc(sys_table_arg, *reserve_size, 962b5fe07aSArd Biesheuvel MIN_KIMG_ALIGN, reserve_addr, 976f26b367SArd Biesheuvel (u32)phys_seed); 982b5fe07aSArd Biesheuvel 996f26b367SArd Biesheuvel *image_addr = *reserve_addr + offset; 1002b5fe07aSArd Biesheuvel } else { 1012b5fe07aSArd Biesheuvel /* 1022b5fe07aSArd Biesheuvel * Else, try a straight allocation at the preferred offset. 103bf457786SArd Biesheuvel * This will work around the issue where, if dram_base == 0x0, 104bf457786SArd Biesheuvel * efi_low_alloc() refuses to allocate at 0x0 (to prevent the 105bf457786SArd Biesheuvel * address of the allocation to be mistaken for a FAIL return 106bf457786SArd Biesheuvel * value or a NULL pointer). It will also ensure that, on 107bf457786SArd Biesheuvel * platforms where the [dram_base, dram_base + TEXT_OFFSET) 108bf457786SArd Biesheuvel * interval is partially occupied by the firmware (like on APM 109bf457786SArd Biesheuvel * Mustang), we can still place the kernel at the address 110bf457786SArd Biesheuvel * 'dram_base + TEXT_OFFSET'. 111bf457786SArd Biesheuvel */ 1122b5fe07aSArd Biesheuvel if (*image_addr == preferred_offset) 1132b5fe07aSArd Biesheuvel return EFI_SUCCESS; 1142b5fe07aSArd Biesheuvel 1152dc10ad8SLinus Torvalds *image_addr = *reserve_addr = preferred_offset; 1162b5fe07aSArd Biesheuvel *reserve_size = round_up(kernel_memsize, EFI_ALLOC_ALIGN); 1172b5fe07aSArd Biesheuvel 118bf457786SArd Biesheuvel status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS, 1192b5fe07aSArd Biesheuvel EFI_LOADER_DATA, 1202b5fe07aSArd Biesheuvel *reserve_size / EFI_PAGE_SIZE, 121bf457786SArd Biesheuvel (efi_physical_addr_t *)reserve_addr); 1222b5fe07aSArd Biesheuvel } 1232b5fe07aSArd Biesheuvel 124bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1252b5fe07aSArd Biesheuvel *reserve_size = kernel_memsize + TEXT_OFFSET; 1262b5fe07aSArd Biesheuvel status = efi_low_alloc(sys_table_arg, *reserve_size, 1272b5fe07aSArd Biesheuvel MIN_KIMG_ALIGN, reserve_addr); 128bf457786SArd Biesheuvel 129bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 130bf457786SArd Biesheuvel pr_efi_err(sys_table_arg, "Failed to relocate kernel\n"); 1312b5fe07aSArd Biesheuvel *reserve_size = 0; 132bf457786SArd Biesheuvel return status; 133bf457786SArd Biesheuvel } 134bf457786SArd Biesheuvel *image_addr = *reserve_addr + TEXT_OFFSET; 135bf457786SArd Biesheuvel } 136bf457786SArd Biesheuvel memcpy((void *)*image_addr, old_image_addr, kernel_size); 137bf457786SArd Biesheuvel 138bf457786SArd Biesheuvel return EFI_SUCCESS; 139bf457786SArd Biesheuvel } 140