14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 281a0bc39SRoy Franz /* 381a0bc39SRoy Franz * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org> 481a0bc39SRoy Franz */ 581a0bc39SRoy Franz #include <linux/efi.h> 681a0bc39SRoy Franz #include <asm/efi.h> 781a0bc39SRoy Franz 8eeff7d63SArd Biesheuvel #include "efistub.h" 9eeff7d63SArd Biesheuvel 10cd33a5c1SArd Biesheuvel efi_status_t check_platform_features(void) 112ec0f0a3SArd Biesheuvel { 122ec0f0a3SArd Biesheuvel int block; 132ec0f0a3SArd Biesheuvel 142ec0f0a3SArd Biesheuvel /* non-LPAE kernels can run anywhere */ 152ec0f0a3SArd Biesheuvel if (!IS_ENABLED(CONFIG_ARM_LPAE)) 162ec0f0a3SArd Biesheuvel return EFI_SUCCESS; 172ec0f0a3SArd Biesheuvel 182ec0f0a3SArd Biesheuvel /* LPAE kernels need compatible hardware */ 192ec0f0a3SArd Biesheuvel block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0); 202ec0f0a3SArd Biesheuvel if (block < 5) { 218173ec79SArd Biesheuvel pr_efi_err("This LPAE kernel is not supported by your CPU\n"); 222ec0f0a3SArd Biesheuvel return EFI_UNSUPPORTED; 232ec0f0a3SArd Biesheuvel } 242ec0f0a3SArd Biesheuvel return EFI_SUCCESS; 252ec0f0a3SArd Biesheuvel } 262ec0f0a3SArd Biesheuvel 27801820beSArd Biesheuvel static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID; 28801820beSArd Biesheuvel 29cd33a5c1SArd Biesheuvel struct screen_info *alloc_screen_info(void) 30801820beSArd Biesheuvel { 31801820beSArd Biesheuvel struct screen_info *si; 32801820beSArd Biesheuvel efi_status_t status; 33801820beSArd Biesheuvel 34801820beSArd Biesheuvel /* 35801820beSArd Biesheuvel * Unlike on arm64, where we can directly fill out the screen_info 36801820beSArd Biesheuvel * structure from the stub, we need to allocate a buffer to hold 37801820beSArd Biesheuvel * its contents while we hand over to the kernel proper from the 38801820beSArd Biesheuvel * decompressor. 39801820beSArd Biesheuvel */ 40801820beSArd Biesheuvel status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA, 41801820beSArd Biesheuvel sizeof(*si), (void **)&si); 42801820beSArd Biesheuvel 43801820beSArd Biesheuvel if (status != EFI_SUCCESS) 44801820beSArd Biesheuvel return NULL; 45801820beSArd Biesheuvel 46801820beSArd Biesheuvel status = efi_call_early(install_configuration_table, 47801820beSArd Biesheuvel &screen_info_guid, si); 48801820beSArd Biesheuvel if (status == EFI_SUCCESS) 49801820beSArd Biesheuvel return si; 50801820beSArd Biesheuvel 51801820beSArd Biesheuvel efi_call_early(free_pool, si); 52801820beSArd Biesheuvel return NULL; 53801820beSArd Biesheuvel } 54801820beSArd Biesheuvel 55cd33a5c1SArd Biesheuvel void free_screen_info(struct screen_info *si) 56801820beSArd Biesheuvel { 57801820beSArd Biesheuvel if (!si) 58801820beSArd Biesheuvel return; 59801820beSArd Biesheuvel 60801820beSArd Biesheuvel efi_call_early(install_configuration_table, &screen_info_guid, NULL); 61801820beSArd Biesheuvel efi_call_early(free_pool, si); 62801820beSArd Biesheuvel } 63801820beSArd Biesheuvel 64cd33a5c1SArd Biesheuvel static efi_status_t reserve_kernel_base(unsigned long dram_base, 65318532bfSArd Biesheuvel unsigned long *reserve_addr, 66318532bfSArd Biesheuvel unsigned long *reserve_size) 6781a0bc39SRoy Franz { 6881a0bc39SRoy Franz efi_physical_addr_t alloc_addr; 69318532bfSArd Biesheuvel efi_memory_desc_t *memory_map; 70318532bfSArd Biesheuvel unsigned long nr_pages, map_size, desc_size, buff_size; 71318532bfSArd Biesheuvel efi_status_t status; 72318532bfSArd Biesheuvel unsigned long l; 7381a0bc39SRoy Franz 74318532bfSArd Biesheuvel struct efi_boot_memmap map = { 75318532bfSArd Biesheuvel .map = &memory_map, 76318532bfSArd Biesheuvel .map_size = &map_size, 77318532bfSArd Biesheuvel .desc_size = &desc_size, 78318532bfSArd Biesheuvel .desc_ver = NULL, 79318532bfSArd Biesheuvel .key_ptr = NULL, 80318532bfSArd Biesheuvel .buff_size = &buff_size, 81318532bfSArd Biesheuvel }; 8281a0bc39SRoy Franz 8381a0bc39SRoy Franz /* 8481a0bc39SRoy Franz * Reserve memory for the uncompressed kernel image. This is 8581a0bc39SRoy Franz * all that prevents any future allocations from conflicting 8681a0bc39SRoy Franz * with the kernel. Since we can't tell from the compressed 8781a0bc39SRoy Franz * image how much DRAM the kernel actually uses (due to BSS 8881a0bc39SRoy Franz * size uncertainty) we allocate the maximum possible size. 8981a0bc39SRoy Franz * Do this very early, as prints can cause memory allocations 9081a0bc39SRoy Franz * that may conflict with this. 9181a0bc39SRoy Franz */ 92318532bfSArd Biesheuvel alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE; 93318532bfSArd Biesheuvel nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE; 94318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS, 95318532bfSArd Biesheuvel EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr); 96318532bfSArd Biesheuvel if (status == EFI_SUCCESS) { 97318532bfSArd Biesheuvel if (alloc_addr == dram_base) { 98318532bfSArd Biesheuvel *reserve_addr = alloc_addr; 9981a0bc39SRoy Franz *reserve_size = MAX_UNCOMP_KERNEL_SIZE; 100318532bfSArd Biesheuvel return EFI_SUCCESS; 101318532bfSArd Biesheuvel } 102318532bfSArd Biesheuvel /* 103318532bfSArd Biesheuvel * If we end up here, the allocation succeeded but starts below 104318532bfSArd Biesheuvel * dram_base. This can only occur if the real base of DRAM is 105318532bfSArd Biesheuvel * not a multiple of 128 MB, in which case dram_base will have 106318532bfSArd Biesheuvel * been rounded up. Since this implies that a part of the region 107318532bfSArd Biesheuvel * was already occupied, we need to fall through to the code 108318532bfSArd Biesheuvel * below to ensure that the existing allocations don't conflict. 109318532bfSArd Biesheuvel * For this reason, we use EFI_BOOT_SERVICES_DATA above and not 110318532bfSArd Biesheuvel * EFI_LOADER_DATA, which we wouldn't able to distinguish from 111318532bfSArd Biesheuvel * allocations that we want to disallow. 112318532bfSArd Biesheuvel */ 113318532bfSArd Biesheuvel } 114318532bfSArd Biesheuvel 115318532bfSArd Biesheuvel /* 116318532bfSArd Biesheuvel * If the allocation above failed, we may still be able to proceed: 117318532bfSArd Biesheuvel * if the only allocations in the region are of types that will be 118318532bfSArd Biesheuvel * released to the OS after ExitBootServices(), the decompressor can 119318532bfSArd Biesheuvel * safely overwrite them. 120318532bfSArd Biesheuvel */ 121cd33a5c1SArd Biesheuvel status = efi_get_memory_map(&map); 12281a0bc39SRoy Franz if (status != EFI_SUCCESS) { 1238173ec79SArd Biesheuvel pr_efi_err("reserve_kernel_base(): Unable to retrieve memory map.\n"); 124318532bfSArd Biesheuvel return status; 125318532bfSArd Biesheuvel } 126318532bfSArd Biesheuvel 127318532bfSArd Biesheuvel for (l = 0; l < map_size; l += desc_size) { 128318532bfSArd Biesheuvel efi_memory_desc_t *desc; 129318532bfSArd Biesheuvel u64 start, end; 130318532bfSArd Biesheuvel 131318532bfSArd Biesheuvel desc = (void *)memory_map + l; 132318532bfSArd Biesheuvel start = desc->phys_addr; 133318532bfSArd Biesheuvel end = start + desc->num_pages * EFI_PAGE_SIZE; 134318532bfSArd Biesheuvel 135318532bfSArd Biesheuvel /* Skip if entry does not intersect with region */ 136318532bfSArd Biesheuvel if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE || 137318532bfSArd Biesheuvel end <= dram_base) 138318532bfSArd Biesheuvel continue; 139318532bfSArd Biesheuvel 140318532bfSArd Biesheuvel switch (desc->type) { 141318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_CODE: 142318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_DATA: 143318532bfSArd Biesheuvel /* Ignore types that are released to the OS anyway */ 144318532bfSArd Biesheuvel continue; 145318532bfSArd Biesheuvel 146318532bfSArd Biesheuvel case EFI_CONVENTIONAL_MEMORY: 14716993c0fSDan Williams /* Skip soft reserved conventional memory */ 14816993c0fSDan Williams if (efi_soft_reserve_enabled() && 14916993c0fSDan Williams (desc->attribute & EFI_MEMORY_SP)) 15016993c0fSDan Williams continue; 15116993c0fSDan Williams 152318532bfSArd Biesheuvel /* 153318532bfSArd Biesheuvel * Reserve the intersection between this entry and the 154318532bfSArd Biesheuvel * region. 155318532bfSArd Biesheuvel */ 156318532bfSArd Biesheuvel start = max(start, (u64)dram_base); 157318532bfSArd Biesheuvel end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE); 158318532bfSArd Biesheuvel 159318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, 160318532bfSArd Biesheuvel EFI_ALLOCATE_ADDRESS, 161318532bfSArd Biesheuvel EFI_LOADER_DATA, 162318532bfSArd Biesheuvel (end - start) / EFI_PAGE_SIZE, 163318532bfSArd Biesheuvel &start); 164318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 1658173ec79SArd Biesheuvel pr_efi_err("reserve_kernel_base(): alloc failed.\n"); 166318532bfSArd Biesheuvel goto out; 167318532bfSArd Biesheuvel } 168318532bfSArd Biesheuvel break; 169318532bfSArd Biesheuvel 170318532bfSArd Biesheuvel case EFI_LOADER_CODE: 171318532bfSArd Biesheuvel case EFI_LOADER_DATA: 172318532bfSArd Biesheuvel /* 173318532bfSArd Biesheuvel * These regions may be released and reallocated for 174318532bfSArd Biesheuvel * another purpose (including EFI_RUNTIME_SERVICE_DATA) 175318532bfSArd Biesheuvel * at any time during the execution of the OS loader, 176318532bfSArd Biesheuvel * so we cannot consider them as safe. 177318532bfSArd Biesheuvel */ 178318532bfSArd Biesheuvel default: 179318532bfSArd Biesheuvel /* 180318532bfSArd Biesheuvel * Treat any other allocation in the region as unsafe */ 181318532bfSArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 182318532bfSArd Biesheuvel goto out; 183318532bfSArd Biesheuvel } 184318532bfSArd Biesheuvel } 185318532bfSArd Biesheuvel 186318532bfSArd Biesheuvel status = EFI_SUCCESS; 187318532bfSArd Biesheuvel out: 188318532bfSArd Biesheuvel efi_call_early(free_pool, memory_map); 189318532bfSArd Biesheuvel return status; 190318532bfSArd Biesheuvel } 191318532bfSArd Biesheuvel 192cd33a5c1SArd Biesheuvel efi_status_t handle_kernel_image(unsigned long *image_addr, 193318532bfSArd Biesheuvel unsigned long *image_size, 194318532bfSArd Biesheuvel unsigned long *reserve_addr, 195318532bfSArd Biesheuvel unsigned long *reserve_size, 196318532bfSArd Biesheuvel unsigned long dram_base, 197318532bfSArd Biesheuvel efi_loaded_image_t *image) 198318532bfSArd Biesheuvel { 19941cd96faSArd Biesheuvel unsigned long kernel_base; 200318532bfSArd Biesheuvel efi_status_t status; 201318532bfSArd Biesheuvel 202318532bfSArd Biesheuvel /* 203318532bfSArd Biesheuvel * Verify that the DRAM base address is compatible with the ARM 204318532bfSArd Biesheuvel * boot protocol, which determines the base of DRAM by masking 205318532bfSArd Biesheuvel * off the low 27 bits of the address at which the zImage is 206318532bfSArd Biesheuvel * loaded. These assumptions are made by the decompressor, 207318532bfSArd Biesheuvel * before any memory map is available. 208318532bfSArd Biesheuvel */ 20941cd96faSArd Biesheuvel kernel_base = round_up(dram_base, SZ_128M); 210318532bfSArd Biesheuvel 21141cd96faSArd Biesheuvel /* 21241cd96faSArd Biesheuvel * Note that some platforms (notably, the Raspberry Pi 2) put 21341cd96faSArd Biesheuvel * spin-tables and other pieces of firmware at the base of RAM, 21441cd96faSArd Biesheuvel * abusing the fact that the window of TEXT_OFFSET bytes at the 21541cd96faSArd Biesheuvel * base of the kernel image is only partially used at the moment. 21641cd96faSArd Biesheuvel * (Up to 5 pages are used for the swapper page tables) 21741cd96faSArd Biesheuvel */ 21841cd96faSArd Biesheuvel kernel_base += TEXT_OFFSET - 5 * PAGE_SIZE; 21941cd96faSArd Biesheuvel 220cd33a5c1SArd Biesheuvel status = reserve_kernel_base(kernel_base, reserve_addr, reserve_size); 221318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 2228173ec79SArd Biesheuvel pr_efi_err("Unable to allocate memory for uncompressed kernel.\n"); 22381a0bc39SRoy Franz return status; 22481a0bc39SRoy Franz } 22581a0bc39SRoy Franz 22681a0bc39SRoy Franz /* 22781a0bc39SRoy Franz * Relocate the zImage, so that it appears in the lowest 128 MB 22881a0bc39SRoy Franz * memory window. 22981a0bc39SRoy Franz */ 23081a0bc39SRoy Franz *image_size = image->image_size; 231cd33a5c1SArd Biesheuvel status = efi_relocate_kernel(image_addr, *image_size, *image_size, 232220dd769SKairui Song kernel_base + MAX_UNCOMP_KERNEL_SIZE, 0, 0); 23381a0bc39SRoy Franz if (status != EFI_SUCCESS) { 2348173ec79SArd Biesheuvel pr_efi_err("Failed to relocate kernel.\n"); 235cd33a5c1SArd Biesheuvel efi_free(*reserve_size, *reserve_addr); 23681a0bc39SRoy Franz *reserve_size = 0; 23781a0bc39SRoy Franz return status; 23881a0bc39SRoy Franz } 23981a0bc39SRoy Franz 24081a0bc39SRoy Franz /* 24181a0bc39SRoy Franz * Check to see if we were able to allocate memory low enough 24281a0bc39SRoy Franz * in memory. The kernel determines the base of DRAM from the 24381a0bc39SRoy Franz * address at which the zImage is loaded. 24481a0bc39SRoy Franz */ 24581a0bc39SRoy Franz if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) { 2468173ec79SArd Biesheuvel pr_efi_err("Failed to relocate kernel, no low memory available.\n"); 247cd33a5c1SArd Biesheuvel efi_free(*reserve_size, *reserve_addr); 24881a0bc39SRoy Franz *reserve_size = 0; 249cd33a5c1SArd Biesheuvel efi_free(*image_size, *image_addr); 25081a0bc39SRoy Franz *image_size = 0; 25181a0bc39SRoy Franz return EFI_LOAD_ERROR; 25281a0bc39SRoy Franz } 25381a0bc39SRoy Franz return EFI_SUCCESS; 25481a0bc39SRoy Franz } 255