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 { 455b94046eSArd Biesheuvel unsigned long map_size, desc_size, buff_size; 465b94046eSArd Biesheuvel efi_memory_desc_t *memory_map; 475b94046eSArd Biesheuvel struct efi_boot_memmap map; 485b94046eSArd Biesheuvel efi_status_t status; 495b94046eSArd Biesheuvel bool ret = false; 505b94046eSArd Biesheuvel int map_offset; 515b94046eSArd Biesheuvel 525b94046eSArd Biesheuvel map.map = &memory_map; 535b94046eSArd Biesheuvel map.map_size = &map_size; 545b94046eSArd Biesheuvel map.desc_size = &desc_size; 555b94046eSArd Biesheuvel map.desc_ver = NULL; 565b94046eSArd Biesheuvel map.key_ptr = NULL; 575b94046eSArd Biesheuvel map.buff_size = &buff_size; 585b94046eSArd Biesheuvel 595b94046eSArd Biesheuvel status = efi_get_memory_map(&map); 605b94046eSArd Biesheuvel if (status != EFI_SUCCESS) 615b94046eSArd Biesheuvel return false; 625b94046eSArd Biesheuvel 635b94046eSArd Biesheuvel for (map_offset = 0; map_offset < map_size; map_offset += desc_size) { 645b94046eSArd Biesheuvel efi_memory_desc_t *md = (void *)memory_map + map_offset; 655b94046eSArd Biesheuvel u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE; 665b94046eSArd Biesheuvel 675b94046eSArd Biesheuvel /* 685b94046eSArd Biesheuvel * Find the region that covers base, and return whether 695b94046eSArd Biesheuvel * it covers base+size bytes. 705b94046eSArd Biesheuvel */ 715b94046eSArd Biesheuvel if (base >= md->phys_addr && base < end) { 725b94046eSArd Biesheuvel ret = (base + size) <= end; 735b94046eSArd Biesheuvel break; 745b94046eSArd Biesheuvel } 755b94046eSArd Biesheuvel } 765b94046eSArd Biesheuvel 775b94046eSArd Biesheuvel efi_bs_call(free_pool, memory_map); 785b94046eSArd Biesheuvel 795b94046eSArd Biesheuvel return ret; 805b94046eSArd Biesheuvel } 815b94046eSArd Biesheuvel 82cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr, 83bf457786SArd Biesheuvel unsigned long *image_size, 84bf457786SArd Biesheuvel unsigned long *reserve_addr, 85bf457786SArd Biesheuvel unsigned long *reserve_size, 86*416a9f84SArd Biesheuvel efi_loaded_image_t *image, 87*416a9f84SArd Biesheuvel efi_handle_t image_handle) 88bf457786SArd Biesheuvel { 89bf457786SArd Biesheuvel efi_status_t status; 90bf457786SArd Biesheuvel unsigned long kernel_size, kernel_memsize = 0; 915d12da9dSArd Biesheuvel u32 phys_seed = 0; 922b5fe07aSArd Biesheuvel 933a262423SArd Biesheuvel /* 943a262423SArd Biesheuvel * Although relocatable kernels can fix up the misalignment with 953a262423SArd Biesheuvel * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are 963a262423SArd Biesheuvel * subtly out of sync with those recorded in the vmlinux when kaslr is 973a262423SArd Biesheuvel * disabled but the image required relocation anyway. Therefore retain 983a262423SArd Biesheuvel * 2M alignment if KASLR was explicitly disabled, even if it was not 993a262423SArd Biesheuvel * going to be activated to begin with. 1003a262423SArd Biesheuvel */ 1013a262423SArd Biesheuvel u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN; 1023a262423SArd Biesheuvel 1032b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 104980771f6SArd Biesheuvel if (!efi_nokaslr) { 105cd33a5c1SArd Biesheuvel status = efi_get_random_bytes(sizeof(phys_seed), 1062b5fe07aSArd Biesheuvel (u8 *)&phys_seed); 1072b5fe07aSArd Biesheuvel if (status == EFI_NOT_FOUND) { 1081c761ee9SMark Brown efi_info("EFI_RNG_PROTOCOL unavailable\n"); 109d32de913SArd Biesheuvel efi_nokaslr = true; 1102b5fe07aSArd Biesheuvel } else if (status != EFI_SUCCESS) { 1111c761ee9SMark Brown efi_err("efi_get_random_bytes() failed (0x%lx)\n", 112d32de913SArd Biesheuvel status); 113d32de913SArd Biesheuvel efi_nokaslr = true; 1142b5fe07aSArd Biesheuvel } 1152b5fe07aSArd Biesheuvel } else { 116793473c2SArvind Sankar efi_info("KASLR disabled on kernel command line\n"); 1172b5fe07aSArd Biesheuvel } 1182b5fe07aSArd Biesheuvel } 1192dc10ad8SLinus Torvalds 12082046702SArd Biesheuvel if (image->image_base != _text) 121793473c2SArvind Sankar efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n"); 122bf457786SArd Biesheuvel 123e9b7c3a4SMihai Carabas if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN)) 124e9b7c3a4SMihai Carabas efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n", 125e9b7c3a4SMihai Carabas SEGMENT_ALIGN >> 10); 126c32ac11dSArd Biesheuvel 127bf457786SArd Biesheuvel kernel_size = _edata - _text; 128bf457786SArd Biesheuvel kernel_memsize = kernel_size + (_end - _edata); 129120dc60dSArd Biesheuvel *reserve_size = kernel_memsize; 130bf457786SArd Biesheuvel 1312b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { 132bf457786SArd Biesheuvel /* 1332b5fe07aSArd Biesheuvel * If KASLR is enabled, and we have some randomness available, 1342b5fe07aSArd Biesheuvel * locate the kernel at a randomized offset in physical memory. 1352b5fe07aSArd Biesheuvel */ 1363a262423SArd Biesheuvel status = efi_random_alloc(*reserve_size, min_kimg_align, 1375d12da9dSArd Biesheuvel reserve_addr, phys_seed); 138ff80ef5bSArd Biesheuvel if (status != EFI_SUCCESS) 139ff80ef5bSArd Biesheuvel efi_warn("efi_random_alloc() failed: 0x%lx\n", status); 1402b5fe07aSArd Biesheuvel } else { 14182046702SArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 1422b5fe07aSArd Biesheuvel } 1432b5fe07aSArd Biesheuvel 144bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1455b94046eSArd Biesheuvel if (!check_image_region((u64)_text, kernel_memsize)) { 1465b94046eSArd Biesheuvel efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"); 1473a262423SArd Biesheuvel } else if (IS_ALIGNED((u64)_text, min_kimg_align)) { 14882046702SArd Biesheuvel /* 14982046702SArd Biesheuvel * Just execute from wherever we were loaded by the 15082046702SArd Biesheuvel * UEFI PE/COFF loader if the alignment is suitable. 15182046702SArd Biesheuvel */ 15282046702SArd Biesheuvel *image_addr = (u64)_text; 15382046702SArd Biesheuvel *reserve_size = 0; 15482046702SArd Biesheuvel return EFI_SUCCESS; 15582046702SArd Biesheuvel } 15682046702SArd Biesheuvel 157e71356feSArd Biesheuvel status = efi_allocate_pages_aligned(*reserve_size, reserve_addr, 1583a262423SArd Biesheuvel ULONG_MAX, min_kimg_align); 159bf457786SArd Biesheuvel 160bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 161793473c2SArvind Sankar efi_err("Failed to relocate kernel\n"); 1622b5fe07aSArd Biesheuvel *reserve_size = 0; 163bf457786SArd Biesheuvel return status; 164bf457786SArd Biesheuvel } 165bf457786SArd Biesheuvel } 166c2136dceSArd Biesheuvel 167120dc60dSArd Biesheuvel *image_addr = *reserve_addr; 168c2136dceSArd Biesheuvel memcpy((void *)*image_addr, _text, kernel_size); 169bf457786SArd Biesheuvel 170bf457786SArd Biesheuvel return EFI_SUCCESS; 171bf457786SArd Biesheuvel } 172