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 10bf457786SArd Biesheuvel #include <linux/efi.h> 11bf457786SArd Biesheuvel #include <asm/efi.h> 12170976bcSMark Rutland #include <asm/memory.h> 136f05106eSArd Biesheuvel #include <asm/sections.h> 1442b55734SArd Biesheuvel #include <asm/sysreg.h> 15bf457786SArd Biesheuvel 162b5fe07aSArd Biesheuvel #include "efistub.h" 172b5fe07aSArd Biesheuvel 18cd33a5c1SArd Biesheuvel efi_status_t check_platform_features(void) 1942b55734SArd Biesheuvel { 2042b55734SArd Biesheuvel u64 tg; 2142b55734SArd Biesheuvel 2242b55734SArd Biesheuvel /* UEFI mandates support for 4 KB granularity, no need to check */ 2342b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_4K_PAGES)) 2442b55734SArd Biesheuvel return EFI_SUCCESS; 2542b55734SArd Biesheuvel 2642b55734SArd Biesheuvel tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_TGRAN_SHIFT) & 0xf; 2726f55386SJames Morse if (tg < ID_AA64MMFR0_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_TGRAN_SUPPORTED_MAX) { 2842b55734SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_64K_PAGES)) 29793473c2SArvind Sankar efi_err("This 64 KB granular kernel is not supported by your CPU\n"); 3042b55734SArd Biesheuvel else 31793473c2SArvind Sankar efi_err("This 16 KB granular kernel is not supported by your CPU\n"); 3242b55734SArd Biesheuvel return EFI_UNSUPPORTED; 3342b55734SArd Biesheuvel } 3442b55734SArd Biesheuvel return EFI_SUCCESS; 3542b55734SArd Biesheuvel } 36bf457786SArd Biesheuvel 3782046702SArd Biesheuvel /* 385b94046eSArd Biesheuvel * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail 395b94046eSArd Biesheuvel * to provide space, and fail to zero it). Check for this condition by double 405b94046eSArd Biesheuvel * checking that the first and the last byte of the image are covered by the 415b94046eSArd Biesheuvel * same EFI memory map entry. 425b94046eSArd Biesheuvel */ 435b94046eSArd Biesheuvel static bool check_image_region(u64 base, u64 size) 445b94046eSArd Biesheuvel { 45*eab31265SArd Biesheuvel struct efi_boot_memmap *map; 465b94046eSArd Biesheuvel efi_status_t status; 475b94046eSArd Biesheuvel bool ret = false; 485b94046eSArd Biesheuvel int map_offset; 495b94046eSArd Biesheuvel 505b94046eSArd Biesheuvel status = efi_get_memory_map(&map); 515b94046eSArd Biesheuvel if (status != EFI_SUCCESS) 525b94046eSArd Biesheuvel return false; 535b94046eSArd Biesheuvel 54*eab31265SArd Biesheuvel for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) { 55*eab31265SArd Biesheuvel efi_memory_desc_t *md = (void *)map->map + map_offset; 565b94046eSArd Biesheuvel u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE; 575b94046eSArd Biesheuvel 585b94046eSArd Biesheuvel /* 595b94046eSArd Biesheuvel * Find the region that covers base, and return whether 605b94046eSArd Biesheuvel * it covers base+size bytes. 615b94046eSArd Biesheuvel */ 625b94046eSArd Biesheuvel if (base >= md->phys_addr && base < end) { 635b94046eSArd Biesheuvel ret = (base + size) <= end; 645b94046eSArd Biesheuvel break; 655b94046eSArd Biesheuvel } 665b94046eSArd Biesheuvel } 675b94046eSArd Biesheuvel 68*eab31265SArd Biesheuvel efi_bs_call(free_pool, map); 695b94046eSArd Biesheuvel 705b94046eSArd Biesheuvel return ret; 715b94046eSArd Biesheuvel } 725b94046eSArd Biesheuvel 73cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr, 74bf457786SArd Biesheuvel unsigned long *image_size, 75bf457786SArd Biesheuvel unsigned long *reserve_addr, 76bf457786SArd Biesheuvel unsigned long *reserve_size, 77416a9f84SArd Biesheuvel efi_loaded_image_t *image, 78416a9f84SArd Biesheuvel efi_handle_t image_handle) 79bf457786SArd Biesheuvel { 80bf457786SArd Biesheuvel efi_status_t status; 81bf457786SArd Biesheuvel unsigned long kernel_size, kernel_memsize = 0; 825d12da9dSArd Biesheuvel u32 phys_seed = 0; 832b5fe07aSArd Biesheuvel 843a262423SArd Biesheuvel /* 853a262423SArd Biesheuvel * Although relocatable kernels can fix up the misalignment with 863a262423SArd Biesheuvel * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are 873a262423SArd Biesheuvel * subtly out of sync with those recorded in the vmlinux when kaslr is 883a262423SArd Biesheuvel * disabled but the image required relocation anyway. Therefore retain 893a262423SArd Biesheuvel * 2M alignment if KASLR was explicitly disabled, even if it was not 903a262423SArd Biesheuvel * going to be activated to begin with. 913a262423SArd Biesheuvel */ 923a262423SArd Biesheuvel u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN; 933a262423SArd Biesheuvel 942b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 9507768c55SArd Biesheuvel efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID; 9607768c55SArd Biesheuvel void *p; 9707768c55SArd Biesheuvel 9807768c55SArd Biesheuvel if (efi_nokaslr) { 9907768c55SArd Biesheuvel efi_info("KASLR disabled on kernel command line\n"); 10007768c55SArd Biesheuvel } else if (efi_bs_call(handle_protocol, image_handle, 10107768c55SArd Biesheuvel &li_fixed_proto, &p) == EFI_SUCCESS) { 10207768c55SArd Biesheuvel efi_info("Image placement fixed by loader\n"); 10307768c55SArd Biesheuvel } else { 104cd33a5c1SArd Biesheuvel status = efi_get_random_bytes(sizeof(phys_seed), 1052b5fe07aSArd Biesheuvel (u8 *)&phys_seed); 1062b5fe07aSArd Biesheuvel if (status == EFI_NOT_FOUND) { 1071c761ee9SMark Brown efi_info("EFI_RNG_PROTOCOL unavailable\n"); 108d32de913SArd Biesheuvel efi_nokaslr = true; 1092b5fe07aSArd Biesheuvel } else if (status != EFI_SUCCESS) { 1101c761ee9SMark Brown efi_err("efi_get_random_bytes() failed (0x%lx)\n", 111d32de913SArd Biesheuvel status); 112d32de913SArd Biesheuvel efi_nokaslr = true; 1132b5fe07aSArd Biesheuvel } 1142b5fe07aSArd Biesheuvel } 1152b5fe07aSArd Biesheuvel } 1162dc10ad8SLinus Torvalds 11782046702SArd Biesheuvel if (image->image_base != _text) 118793473c2SArvind Sankar efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n"); 119bf457786SArd Biesheuvel 120e9b7c3a4SMihai Carabas if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN)) 121e9b7c3a4SMihai Carabas efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n", 122e9b7c3a4SMihai Carabas SEGMENT_ALIGN >> 10); 123c32ac11dSArd Biesheuvel 124bf457786SArd Biesheuvel kernel_size = _edata - _text; 125bf457786SArd Biesheuvel kernel_memsize = kernel_size + (_end - _edata); 126120dc60dSArd Biesheuvel *reserve_size = kernel_memsize; 127bf457786SArd Biesheuvel 1282b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { 129bf457786SArd Biesheuvel /* 1302b5fe07aSArd Biesheuvel * If KASLR is enabled, and we have some randomness available, 1312b5fe07aSArd Biesheuvel * locate the kernel at a randomized offset in physical memory. 1322b5fe07aSArd Biesheuvel */ 1333a262423SArd Biesheuvel status = efi_random_alloc(*reserve_size, min_kimg_align, 1345d12da9dSArd Biesheuvel reserve_addr, phys_seed); 135ff80ef5bSArd Biesheuvel if (status != EFI_SUCCESS) 136ff80ef5bSArd Biesheuvel efi_warn("efi_random_alloc() failed: 0x%lx\n", status); 1372b5fe07aSArd Biesheuvel } else { 13882046702SArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 1392b5fe07aSArd Biesheuvel } 1402b5fe07aSArd Biesheuvel 141bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1425b94046eSArd Biesheuvel if (!check_image_region((u64)_text, kernel_memsize)) { 1435b94046eSArd Biesheuvel efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"); 1443a262423SArd Biesheuvel } else if (IS_ALIGNED((u64)_text, min_kimg_align)) { 14582046702SArd Biesheuvel /* 14682046702SArd Biesheuvel * Just execute from wherever we were loaded by the 14782046702SArd Biesheuvel * UEFI PE/COFF loader if the alignment is suitable. 14882046702SArd Biesheuvel */ 14982046702SArd Biesheuvel *image_addr = (u64)_text; 15082046702SArd Biesheuvel *reserve_size = 0; 15182046702SArd Biesheuvel return EFI_SUCCESS; 15282046702SArd Biesheuvel } 15382046702SArd Biesheuvel 154e71356feSArd Biesheuvel status = efi_allocate_pages_aligned(*reserve_size, reserve_addr, 1553a262423SArd Biesheuvel ULONG_MAX, min_kimg_align); 156bf457786SArd Biesheuvel 157bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 158793473c2SArvind Sankar efi_err("Failed to relocate kernel\n"); 1592b5fe07aSArd Biesheuvel *reserve_size = 0; 160bf457786SArd Biesheuvel return status; 161bf457786SArd Biesheuvel } 162bf457786SArd Biesheuvel } 163c2136dceSArd Biesheuvel 164120dc60dSArd Biesheuvel *image_addr = *reserve_addr; 165c2136dceSArd Biesheuvel memcpy((void *)*image_addr, _text, kernel_size); 166bf457786SArd Biesheuvel 167bf457786SArd Biesheuvel return EFI_SUCCESS; 168bf457786SArd Biesheuvel } 169