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 102ec0f0a3SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg) 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) { 212ec0f0a3SArd Biesheuvel pr_efi_err(sys_table_arg, "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 29801820beSArd Biesheuvel struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg) 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 55801820beSArd Biesheuvel void free_screen_info(efi_system_table_t *sys_table_arg, 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 64318532bfSArd Biesheuvel static efi_status_t reserve_kernel_base(efi_system_table_t *sys_table_arg, 6581a0bc39SRoy Franz unsigned long dram_base, 66318532bfSArd Biesheuvel unsigned long *reserve_addr, 67318532bfSArd Biesheuvel unsigned long *reserve_size) 6881a0bc39SRoy Franz { 6981a0bc39SRoy Franz efi_physical_addr_t alloc_addr; 70318532bfSArd Biesheuvel efi_memory_desc_t *memory_map; 71318532bfSArd Biesheuvel unsigned long nr_pages, map_size, desc_size, buff_size; 72318532bfSArd Biesheuvel efi_status_t status; 73318532bfSArd Biesheuvel unsigned long l; 7481a0bc39SRoy Franz 75318532bfSArd Biesheuvel struct efi_boot_memmap map = { 76318532bfSArd Biesheuvel .map = &memory_map, 77318532bfSArd Biesheuvel .map_size = &map_size, 78318532bfSArd Biesheuvel .desc_size = &desc_size, 79318532bfSArd Biesheuvel .desc_ver = NULL, 80318532bfSArd Biesheuvel .key_ptr = NULL, 81318532bfSArd Biesheuvel .buff_size = &buff_size, 82318532bfSArd Biesheuvel }; 8381a0bc39SRoy Franz 8481a0bc39SRoy Franz /* 8581a0bc39SRoy Franz * Reserve memory for the uncompressed kernel image. This is 8681a0bc39SRoy Franz * all that prevents any future allocations from conflicting 8781a0bc39SRoy Franz * with the kernel. Since we can't tell from the compressed 8881a0bc39SRoy Franz * image how much DRAM the kernel actually uses (due to BSS 8981a0bc39SRoy Franz * size uncertainty) we allocate the maximum possible size. 9081a0bc39SRoy Franz * Do this very early, as prints can cause memory allocations 9181a0bc39SRoy Franz * that may conflict with this. 9281a0bc39SRoy Franz */ 93318532bfSArd Biesheuvel alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE; 94318532bfSArd Biesheuvel nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE; 95318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS, 96318532bfSArd Biesheuvel EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr); 97318532bfSArd Biesheuvel if (status == EFI_SUCCESS) { 98318532bfSArd Biesheuvel if (alloc_addr == dram_base) { 99318532bfSArd Biesheuvel *reserve_addr = alloc_addr; 10081a0bc39SRoy Franz *reserve_size = MAX_UNCOMP_KERNEL_SIZE; 101318532bfSArd Biesheuvel return EFI_SUCCESS; 102318532bfSArd Biesheuvel } 103318532bfSArd Biesheuvel /* 104318532bfSArd Biesheuvel * If we end up here, the allocation succeeded but starts below 105318532bfSArd Biesheuvel * dram_base. This can only occur if the real base of DRAM is 106318532bfSArd Biesheuvel * not a multiple of 128 MB, in which case dram_base will have 107318532bfSArd Biesheuvel * been rounded up. Since this implies that a part of the region 108318532bfSArd Biesheuvel * was already occupied, we need to fall through to the code 109318532bfSArd Biesheuvel * below to ensure that the existing allocations don't conflict. 110318532bfSArd Biesheuvel * For this reason, we use EFI_BOOT_SERVICES_DATA above and not 111318532bfSArd Biesheuvel * EFI_LOADER_DATA, which we wouldn't able to distinguish from 112318532bfSArd Biesheuvel * allocations that we want to disallow. 113318532bfSArd Biesheuvel */ 114318532bfSArd Biesheuvel } 115318532bfSArd Biesheuvel 116318532bfSArd Biesheuvel /* 117318532bfSArd Biesheuvel * If the allocation above failed, we may still be able to proceed: 118318532bfSArd Biesheuvel * if the only allocations in the region are of types that will be 119318532bfSArd Biesheuvel * released to the OS after ExitBootServices(), the decompressor can 120318532bfSArd Biesheuvel * safely overwrite them. 121318532bfSArd Biesheuvel */ 122318532bfSArd Biesheuvel status = efi_get_memory_map(sys_table_arg, &map); 12381a0bc39SRoy Franz if (status != EFI_SUCCESS) { 124318532bfSArd Biesheuvel pr_efi_err(sys_table_arg, 125318532bfSArd Biesheuvel "reserve_kernel_base(): Unable to retrieve memory map.\n"); 126318532bfSArd Biesheuvel return status; 127318532bfSArd Biesheuvel } 128318532bfSArd Biesheuvel 129318532bfSArd Biesheuvel for (l = 0; l < map_size; l += desc_size) { 130318532bfSArd Biesheuvel efi_memory_desc_t *desc; 131318532bfSArd Biesheuvel u64 start, end; 132318532bfSArd Biesheuvel 133318532bfSArd Biesheuvel desc = (void *)memory_map + l; 134318532bfSArd Biesheuvel start = desc->phys_addr; 135318532bfSArd Biesheuvel end = start + desc->num_pages * EFI_PAGE_SIZE; 136318532bfSArd Biesheuvel 137318532bfSArd Biesheuvel /* Skip if entry does not intersect with region */ 138318532bfSArd Biesheuvel if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE || 139318532bfSArd Biesheuvel end <= dram_base) 140318532bfSArd Biesheuvel continue; 141318532bfSArd Biesheuvel 142318532bfSArd Biesheuvel switch (desc->type) { 143318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_CODE: 144318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_DATA: 145318532bfSArd Biesheuvel /* Ignore types that are released to the OS anyway */ 146318532bfSArd Biesheuvel continue; 147318532bfSArd Biesheuvel 148318532bfSArd Biesheuvel case EFI_CONVENTIONAL_MEMORY: 149318532bfSArd Biesheuvel /* 150318532bfSArd Biesheuvel * Reserve the intersection between this entry and the 151318532bfSArd Biesheuvel * region. 152318532bfSArd Biesheuvel */ 153318532bfSArd Biesheuvel start = max(start, (u64)dram_base); 154318532bfSArd Biesheuvel end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE); 155318532bfSArd Biesheuvel 156318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, 157318532bfSArd Biesheuvel EFI_ALLOCATE_ADDRESS, 158318532bfSArd Biesheuvel EFI_LOADER_DATA, 159318532bfSArd Biesheuvel (end - start) / EFI_PAGE_SIZE, 160318532bfSArd Biesheuvel &start); 161318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 162318532bfSArd Biesheuvel pr_efi_err(sys_table_arg, 163318532bfSArd Biesheuvel "reserve_kernel_base(): alloc failed.\n"); 164318532bfSArd Biesheuvel goto out; 165318532bfSArd Biesheuvel } 166318532bfSArd Biesheuvel break; 167318532bfSArd Biesheuvel 168318532bfSArd Biesheuvel case EFI_LOADER_CODE: 169318532bfSArd Biesheuvel case EFI_LOADER_DATA: 170318532bfSArd Biesheuvel /* 171318532bfSArd Biesheuvel * These regions may be released and reallocated for 172318532bfSArd Biesheuvel * another purpose (including EFI_RUNTIME_SERVICE_DATA) 173318532bfSArd Biesheuvel * at any time during the execution of the OS loader, 174318532bfSArd Biesheuvel * so we cannot consider them as safe. 175318532bfSArd Biesheuvel */ 176318532bfSArd Biesheuvel default: 177318532bfSArd Biesheuvel /* 178318532bfSArd Biesheuvel * Treat any other allocation in the region as unsafe */ 179318532bfSArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 180318532bfSArd Biesheuvel goto out; 181318532bfSArd Biesheuvel } 182318532bfSArd Biesheuvel } 183318532bfSArd Biesheuvel 184318532bfSArd Biesheuvel status = EFI_SUCCESS; 185318532bfSArd Biesheuvel out: 186318532bfSArd Biesheuvel efi_call_early(free_pool, memory_map); 187318532bfSArd Biesheuvel return status; 188318532bfSArd Biesheuvel } 189318532bfSArd Biesheuvel 190318532bfSArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table, 191318532bfSArd Biesheuvel unsigned long *image_addr, 192318532bfSArd Biesheuvel unsigned long *image_size, 193318532bfSArd Biesheuvel unsigned long *reserve_addr, 194318532bfSArd Biesheuvel unsigned long *reserve_size, 195318532bfSArd Biesheuvel unsigned long dram_base, 196318532bfSArd Biesheuvel efi_loaded_image_t *image) 197318532bfSArd Biesheuvel { 198318532bfSArd Biesheuvel efi_status_t status; 199318532bfSArd Biesheuvel 200318532bfSArd Biesheuvel /* 201318532bfSArd Biesheuvel * Verify that the DRAM base address is compatible with the ARM 202318532bfSArd Biesheuvel * boot protocol, which determines the base of DRAM by masking 203318532bfSArd Biesheuvel * off the low 27 bits of the address at which the zImage is 204318532bfSArd Biesheuvel * loaded. These assumptions are made by the decompressor, 205318532bfSArd Biesheuvel * before any memory map is available. 206318532bfSArd Biesheuvel */ 207318532bfSArd Biesheuvel dram_base = round_up(dram_base, SZ_128M); 208318532bfSArd Biesheuvel 209318532bfSArd Biesheuvel status = reserve_kernel_base(sys_table, dram_base, reserve_addr, 210318532bfSArd Biesheuvel reserve_size); 211318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 21281a0bc39SRoy Franz pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n"); 21381a0bc39SRoy Franz return status; 21481a0bc39SRoy Franz } 21581a0bc39SRoy Franz 21681a0bc39SRoy Franz /* 21781a0bc39SRoy Franz * Relocate the zImage, so that it appears in the lowest 128 MB 21881a0bc39SRoy Franz * memory window. 21981a0bc39SRoy Franz */ 22081a0bc39SRoy Franz *image_size = image->image_size; 22181a0bc39SRoy Franz status = efi_relocate_kernel(sys_table, image_addr, *image_size, 22281a0bc39SRoy Franz *image_size, 22381a0bc39SRoy Franz dram_base + MAX_UNCOMP_KERNEL_SIZE, 0); 22481a0bc39SRoy Franz if (status != EFI_SUCCESS) { 22581a0bc39SRoy Franz pr_efi_err(sys_table, "Failed to relocate kernel.\n"); 22681a0bc39SRoy Franz efi_free(sys_table, *reserve_size, *reserve_addr); 22781a0bc39SRoy Franz *reserve_size = 0; 22881a0bc39SRoy Franz return status; 22981a0bc39SRoy Franz } 23081a0bc39SRoy Franz 23181a0bc39SRoy Franz /* 23281a0bc39SRoy Franz * Check to see if we were able to allocate memory low enough 23381a0bc39SRoy Franz * in memory. The kernel determines the base of DRAM from the 23481a0bc39SRoy Franz * address at which the zImage is loaded. 23581a0bc39SRoy Franz */ 23681a0bc39SRoy Franz if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) { 23781a0bc39SRoy Franz pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n"); 23881a0bc39SRoy Franz efi_free(sys_table, *reserve_size, *reserve_addr); 23981a0bc39SRoy Franz *reserve_size = 0; 24081a0bc39SRoy Franz efi_free(sys_table, *image_size, *image_addr); 24181a0bc39SRoy Franz *image_size = 0; 24281a0bc39SRoy Franz return EFI_LOAD_ERROR; 24381a0bc39SRoy Franz } 24481a0bc39SRoy Franz return EFI_SUCCESS; 24581a0bc39SRoy Franz } 246