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> 14bf457786SArd Biesheuvel 152b5fe07aSArd Biesheuvel #include "efistub.h" 162b5fe07aSArd Biesheuvel 1782046702SArd Biesheuvel /* 185b94046eSArd Biesheuvel * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail 195b94046eSArd Biesheuvel * to provide space, and fail to zero it). Check for this condition by double 205b94046eSArd Biesheuvel * checking that the first and the last byte of the image are covered by the 215b94046eSArd Biesheuvel * same EFI memory map entry. 225b94046eSArd Biesheuvel */ 235b94046eSArd Biesheuvel static bool check_image_region(u64 base, u64 size) 245b94046eSArd Biesheuvel { 25eab31265SArd Biesheuvel struct efi_boot_memmap *map; 265b94046eSArd Biesheuvel efi_status_t status; 275b94046eSArd Biesheuvel bool ret = false; 285b94046eSArd Biesheuvel int map_offset; 295b94046eSArd Biesheuvel 30171539f5SArd Biesheuvel status = efi_get_memory_map(&map, false); 315b94046eSArd Biesheuvel if (status != EFI_SUCCESS) 325b94046eSArd Biesheuvel return false; 335b94046eSArd Biesheuvel 34eab31265SArd Biesheuvel for (map_offset = 0; map_offset < map->map_size; map_offset += map->desc_size) { 35eab31265SArd Biesheuvel efi_memory_desc_t *md = (void *)map->map + map_offset; 365b94046eSArd Biesheuvel u64 end = md->phys_addr + md->num_pages * EFI_PAGE_SIZE; 375b94046eSArd Biesheuvel 385b94046eSArd Biesheuvel /* 395b94046eSArd Biesheuvel * Find the region that covers base, and return whether 405b94046eSArd Biesheuvel * it covers base+size bytes. 415b94046eSArd Biesheuvel */ 425b94046eSArd Biesheuvel if (base >= md->phys_addr && base < end) { 435b94046eSArd Biesheuvel ret = (base + size) <= end; 445b94046eSArd Biesheuvel break; 455b94046eSArd Biesheuvel } 465b94046eSArd Biesheuvel } 475b94046eSArd Biesheuvel 48eab31265SArd Biesheuvel efi_bs_call(free_pool, map); 495b94046eSArd Biesheuvel 505b94046eSArd Biesheuvel return ret; 515b94046eSArd Biesheuvel } 525b94046eSArd Biesheuvel 53cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr, 54bf457786SArd Biesheuvel unsigned long *image_size, 55bf457786SArd Biesheuvel unsigned long *reserve_addr, 56bf457786SArd Biesheuvel unsigned long *reserve_size, 57416a9f84SArd Biesheuvel efi_loaded_image_t *image, 58416a9f84SArd Biesheuvel efi_handle_t image_handle) 59bf457786SArd Biesheuvel { 60bf457786SArd Biesheuvel efi_status_t status; 6161786170SArd Biesheuvel unsigned long kernel_size, kernel_codesize, kernel_memsize; 625d12da9dSArd Biesheuvel u32 phys_seed = 0; 63895bc3a1SArd Biesheuvel u64 min_kimg_align = efi_get_kimg_min_align(); 643a262423SArd Biesheuvel 652b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 6607768c55SArd Biesheuvel efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID; 6707768c55SArd Biesheuvel void *p; 6807768c55SArd Biesheuvel 6907768c55SArd Biesheuvel if (efi_nokaslr) { 7007768c55SArd Biesheuvel efi_info("KASLR disabled on kernel command line\n"); 7107768c55SArd Biesheuvel } else if (efi_bs_call(handle_protocol, image_handle, 7207768c55SArd Biesheuvel &li_fixed_proto, &p) == EFI_SUCCESS) { 7307768c55SArd Biesheuvel efi_info("Image placement fixed by loader\n"); 7407768c55SArd Biesheuvel } else { 75cd33a5c1SArd Biesheuvel status = efi_get_random_bytes(sizeof(phys_seed), 762b5fe07aSArd Biesheuvel (u8 *)&phys_seed); 772b5fe07aSArd Biesheuvel if (status == EFI_NOT_FOUND) { 781c761ee9SMark Brown efi_info("EFI_RNG_PROTOCOL unavailable\n"); 79d32de913SArd Biesheuvel efi_nokaslr = true; 802b5fe07aSArd Biesheuvel } else if (status != EFI_SUCCESS) { 811c761ee9SMark Brown efi_err("efi_get_random_bytes() failed (0x%lx)\n", 82d32de913SArd Biesheuvel status); 83d32de913SArd Biesheuvel efi_nokaslr = true; 842b5fe07aSArd Biesheuvel } 852b5fe07aSArd Biesheuvel } 862b5fe07aSArd Biesheuvel } 872dc10ad8SLinus Torvalds 88*fc3608aaSArd Biesheuvel if (image->image_base != _text) { 89793473c2SArvind Sankar efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n"); 90*fc3608aaSArd Biesheuvel image->image_base = _text; 91*fc3608aaSArd Biesheuvel } 92bf457786SArd Biesheuvel 93e9b7c3a4SMihai Carabas if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN)) 94e9b7c3a4SMihai Carabas efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n", 95e9b7c3a4SMihai Carabas SEGMENT_ALIGN >> 10); 96c32ac11dSArd Biesheuvel 97bf457786SArd Biesheuvel kernel_size = _edata - _text; 9861786170SArd Biesheuvel kernel_codesize = __inittext_end - _text; 99bf457786SArd Biesheuvel kernel_memsize = kernel_size + (_end - _edata); 100120dc60dSArd Biesheuvel *reserve_size = kernel_memsize; 101bf457786SArd Biesheuvel 1022b5fe07aSArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) { 103bf457786SArd Biesheuvel /* 1042b5fe07aSArd Biesheuvel * If KASLR is enabled, and we have some randomness available, 1052b5fe07aSArd Biesheuvel * locate the kernel at a randomized offset in physical memory. 1062b5fe07aSArd Biesheuvel */ 1073a262423SArd Biesheuvel status = efi_random_alloc(*reserve_size, min_kimg_align, 1089cf42bcaSArd Biesheuvel reserve_addr, phys_seed, 1099cf42bcaSArd Biesheuvel EFI_LOADER_CODE); 110ff80ef5bSArd Biesheuvel if (status != EFI_SUCCESS) 111ff80ef5bSArd Biesheuvel efi_warn("efi_random_alloc() failed: 0x%lx\n", status); 1122b5fe07aSArd Biesheuvel } else { 11382046702SArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 1142b5fe07aSArd Biesheuvel } 1152b5fe07aSArd Biesheuvel 116bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 1175b94046eSArd Biesheuvel if (!check_image_region((u64)_text, kernel_memsize)) { 1185b94046eSArd Biesheuvel efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n"); 119a37dac5cSArd Biesheuvel } else if (IS_ALIGNED((u64)_text, min_kimg_align) && 120a37dac5cSArd Biesheuvel (u64)_end < EFI_ALLOC_LIMIT) { 12182046702SArd Biesheuvel /* 12282046702SArd Biesheuvel * Just execute from wherever we were loaded by the 123a37dac5cSArd Biesheuvel * UEFI PE/COFF loader if the placement is suitable. 12482046702SArd Biesheuvel */ 12582046702SArd Biesheuvel *image_addr = (u64)_text; 12682046702SArd Biesheuvel *reserve_size = 0; 12761786170SArd Biesheuvel return EFI_SUCCESS; 12882046702SArd Biesheuvel } 12982046702SArd Biesheuvel 130e71356feSArd Biesheuvel status = efi_allocate_pages_aligned(*reserve_size, reserve_addr, 1319cf42bcaSArd Biesheuvel ULONG_MAX, min_kimg_align, 1329cf42bcaSArd Biesheuvel EFI_LOADER_CODE); 133bf457786SArd Biesheuvel 134bf457786SArd Biesheuvel if (status != EFI_SUCCESS) { 135793473c2SArvind Sankar efi_err("Failed to relocate kernel\n"); 1362b5fe07aSArd Biesheuvel *reserve_size = 0; 137bf457786SArd Biesheuvel return status; 138bf457786SArd Biesheuvel } 139bf457786SArd Biesheuvel } 140c2136dceSArd Biesheuvel 141120dc60dSArd Biesheuvel *image_addr = *reserve_addr; 142c2136dceSArd Biesheuvel memcpy((void *)*image_addr, _text, kernel_size); 14361786170SArd Biesheuvel caches_clean_inval_pou(*image_addr, *image_addr + kernel_codesize); 1443c60f67bSArd Biesheuvel efi_remap_image(*image_addr, *reserve_size, kernel_codesize); 145aaeb3fc6SArd Biesheuvel 146bf457786SArd Biesheuvel return EFI_SUCCESS; 147bf457786SArd Biesheuvel } 14861786170SArd Biesheuvel 14961786170SArd Biesheuvel asmlinkage void primary_entry(void); 15061786170SArd Biesheuvel 15161786170SArd Biesheuvel unsigned long primary_entry_offset(void) 15261786170SArd Biesheuvel { 15361786170SArd Biesheuvel /* 15461786170SArd Biesheuvel * When built as part of the kernel, the EFI stub cannot branch to the 15561786170SArd Biesheuvel * kernel proper via the image header, as the PE/COFF header is 15661786170SArd Biesheuvel * strictly not part of the in-memory presentation of the image, only 15761786170SArd Biesheuvel * of the file representation. So instead, we need to jump to the 15861786170SArd Biesheuvel * actual entrypoint in the .text region of the image. 15961786170SArd Biesheuvel */ 16061786170SArd Biesheuvel return (char *)primary_entry - _text; 16161786170SArd Biesheuvel } 162