181a0bc39SRoy Franz /* 281a0bc39SRoy Franz * Copyright (C) 2013 Linaro Ltd; <roy.franz@linaro.org> 381a0bc39SRoy Franz * 481a0bc39SRoy Franz * This program is free software; you can redistribute it and/or modify 581a0bc39SRoy Franz * it under the terms of the GNU General Public License version 2 as 681a0bc39SRoy Franz * published by the Free Software Foundation. 781a0bc39SRoy Franz * 881a0bc39SRoy Franz */ 981a0bc39SRoy Franz #include <linux/efi.h> 1081a0bc39SRoy Franz #include <asm/efi.h> 1181a0bc39SRoy Franz 12eeff7d63SArd Biesheuvel #include "efistub.h" 13eeff7d63SArd Biesheuvel 142ec0f0a3SArd Biesheuvel efi_status_t check_platform_features(efi_system_table_t *sys_table_arg) 152ec0f0a3SArd Biesheuvel { 162ec0f0a3SArd Biesheuvel int block; 172ec0f0a3SArd Biesheuvel 182ec0f0a3SArd Biesheuvel /* non-LPAE kernels can run anywhere */ 192ec0f0a3SArd Biesheuvel if (!IS_ENABLED(CONFIG_ARM_LPAE)) 202ec0f0a3SArd Biesheuvel return EFI_SUCCESS; 212ec0f0a3SArd Biesheuvel 222ec0f0a3SArd Biesheuvel /* LPAE kernels need compatible hardware */ 232ec0f0a3SArd Biesheuvel block = cpuid_feature_extract(CPUID_EXT_MMFR0, 0); 242ec0f0a3SArd Biesheuvel if (block < 5) { 252ec0f0a3SArd Biesheuvel pr_efi_err(sys_table_arg, "This LPAE kernel is not supported by your CPU\n"); 262ec0f0a3SArd Biesheuvel return EFI_UNSUPPORTED; 272ec0f0a3SArd Biesheuvel } 282ec0f0a3SArd Biesheuvel return EFI_SUCCESS; 292ec0f0a3SArd Biesheuvel } 302ec0f0a3SArd Biesheuvel 31801820beSArd Biesheuvel static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID; 32801820beSArd Biesheuvel 33801820beSArd Biesheuvel struct screen_info *alloc_screen_info(efi_system_table_t *sys_table_arg) 34801820beSArd Biesheuvel { 35801820beSArd Biesheuvel struct screen_info *si; 36801820beSArd Biesheuvel efi_status_t status; 37801820beSArd Biesheuvel 38801820beSArd Biesheuvel /* 39801820beSArd Biesheuvel * Unlike on arm64, where we can directly fill out the screen_info 40801820beSArd Biesheuvel * structure from the stub, we need to allocate a buffer to hold 41801820beSArd Biesheuvel * its contents while we hand over to the kernel proper from the 42801820beSArd Biesheuvel * decompressor. 43801820beSArd Biesheuvel */ 44801820beSArd Biesheuvel status = efi_call_early(allocate_pool, EFI_RUNTIME_SERVICES_DATA, 45801820beSArd Biesheuvel sizeof(*si), (void **)&si); 46801820beSArd Biesheuvel 47801820beSArd Biesheuvel if (status != EFI_SUCCESS) 48801820beSArd Biesheuvel return NULL; 49801820beSArd Biesheuvel 50801820beSArd Biesheuvel status = efi_call_early(install_configuration_table, 51801820beSArd Biesheuvel &screen_info_guid, si); 52801820beSArd Biesheuvel if (status == EFI_SUCCESS) 53801820beSArd Biesheuvel return si; 54801820beSArd Biesheuvel 55801820beSArd Biesheuvel efi_call_early(free_pool, si); 56801820beSArd Biesheuvel return NULL; 57801820beSArd Biesheuvel } 58801820beSArd Biesheuvel 59801820beSArd Biesheuvel void free_screen_info(efi_system_table_t *sys_table_arg, struct screen_info *si) 60801820beSArd Biesheuvel { 61801820beSArd Biesheuvel if (!si) 62801820beSArd Biesheuvel return; 63801820beSArd Biesheuvel 64801820beSArd Biesheuvel efi_call_early(install_configuration_table, &screen_info_guid, NULL); 65801820beSArd Biesheuvel efi_call_early(free_pool, si); 66801820beSArd Biesheuvel } 67801820beSArd Biesheuvel 68318532bfSArd Biesheuvel static efi_status_t reserve_kernel_base(efi_system_table_t *sys_table_arg, 6981a0bc39SRoy Franz unsigned long dram_base, 70318532bfSArd Biesheuvel unsigned long *reserve_addr, 71318532bfSArd Biesheuvel unsigned long *reserve_size) 7281a0bc39SRoy Franz { 7381a0bc39SRoy Franz efi_physical_addr_t alloc_addr; 74318532bfSArd Biesheuvel efi_memory_desc_t *memory_map; 75318532bfSArd Biesheuvel unsigned long nr_pages, map_size, desc_size, buff_size; 76318532bfSArd Biesheuvel efi_status_t status; 77318532bfSArd Biesheuvel unsigned long l; 7881a0bc39SRoy Franz 79318532bfSArd Biesheuvel struct efi_boot_memmap map = { 80318532bfSArd Biesheuvel .map = &memory_map, 81318532bfSArd Biesheuvel .map_size = &map_size, 82318532bfSArd Biesheuvel .desc_size = &desc_size, 83318532bfSArd Biesheuvel .desc_ver = NULL, 84318532bfSArd Biesheuvel .key_ptr = NULL, 85318532bfSArd Biesheuvel .buff_size = &buff_size, 86318532bfSArd Biesheuvel }; 8781a0bc39SRoy Franz 8881a0bc39SRoy Franz /* 8981a0bc39SRoy Franz * Reserve memory for the uncompressed kernel image. This is 9081a0bc39SRoy Franz * all that prevents any future allocations from conflicting 9181a0bc39SRoy Franz * with the kernel. Since we can't tell from the compressed 9281a0bc39SRoy Franz * image how much DRAM the kernel actually uses (due to BSS 9381a0bc39SRoy Franz * size uncertainty) we allocate the maximum possible size. 9481a0bc39SRoy Franz * Do this very early, as prints can cause memory allocations 9581a0bc39SRoy Franz * that may conflict with this. 9681a0bc39SRoy Franz */ 97318532bfSArd Biesheuvel alloc_addr = dram_base + MAX_UNCOMP_KERNEL_SIZE; 98318532bfSArd Biesheuvel nr_pages = MAX_UNCOMP_KERNEL_SIZE / EFI_PAGE_SIZE; 99318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS, 100318532bfSArd Biesheuvel EFI_BOOT_SERVICES_DATA, nr_pages, &alloc_addr); 101318532bfSArd Biesheuvel if (status == EFI_SUCCESS) { 102318532bfSArd Biesheuvel if (alloc_addr == dram_base) { 103318532bfSArd Biesheuvel *reserve_addr = alloc_addr; 10481a0bc39SRoy Franz *reserve_size = MAX_UNCOMP_KERNEL_SIZE; 105318532bfSArd Biesheuvel return EFI_SUCCESS; 106318532bfSArd Biesheuvel } 107318532bfSArd Biesheuvel /* 108318532bfSArd Biesheuvel * If we end up here, the allocation succeeded but starts below 109318532bfSArd Biesheuvel * dram_base. This can only occur if the real base of DRAM is 110318532bfSArd Biesheuvel * not a multiple of 128 MB, in which case dram_base will have 111318532bfSArd Biesheuvel * been rounded up. Since this implies that a part of the region 112318532bfSArd Biesheuvel * was already occupied, we need to fall through to the code 113318532bfSArd Biesheuvel * below to ensure that the existing allocations don't conflict. 114318532bfSArd Biesheuvel * For this reason, we use EFI_BOOT_SERVICES_DATA above and not 115318532bfSArd Biesheuvel * EFI_LOADER_DATA, which we wouldn't able to distinguish from 116318532bfSArd Biesheuvel * allocations that we want to disallow. 117318532bfSArd Biesheuvel */ 118318532bfSArd Biesheuvel } 119318532bfSArd Biesheuvel 120318532bfSArd Biesheuvel /* 121318532bfSArd Biesheuvel * If the allocation above failed, we may still be able to proceed: 122318532bfSArd Biesheuvel * if the only allocations in the region are of types that will be 123318532bfSArd Biesheuvel * released to the OS after ExitBootServices(), the decompressor can 124318532bfSArd Biesheuvel * safely overwrite them. 125318532bfSArd Biesheuvel */ 126318532bfSArd Biesheuvel status = efi_get_memory_map(sys_table_arg, &map); 12781a0bc39SRoy Franz if (status != EFI_SUCCESS) { 128318532bfSArd Biesheuvel pr_efi_err(sys_table_arg, 129318532bfSArd Biesheuvel "reserve_kernel_base(): Unable to retrieve memory map.\n"); 130318532bfSArd Biesheuvel return status; 131318532bfSArd Biesheuvel } 132318532bfSArd Biesheuvel 133318532bfSArd Biesheuvel for (l = 0; l < map_size; l += desc_size) { 134318532bfSArd Biesheuvel efi_memory_desc_t *desc; 135318532bfSArd Biesheuvel u64 start, end; 136318532bfSArd Biesheuvel 137318532bfSArd Biesheuvel desc = (void *)memory_map + l; 138318532bfSArd Biesheuvel start = desc->phys_addr; 139318532bfSArd Biesheuvel end = start + desc->num_pages * EFI_PAGE_SIZE; 140318532bfSArd Biesheuvel 141318532bfSArd Biesheuvel /* Skip if entry does not intersect with region */ 142318532bfSArd Biesheuvel if (start >= dram_base + MAX_UNCOMP_KERNEL_SIZE || 143318532bfSArd Biesheuvel end <= dram_base) 144318532bfSArd Biesheuvel continue; 145318532bfSArd Biesheuvel 146318532bfSArd Biesheuvel switch (desc->type) { 147318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_CODE: 148318532bfSArd Biesheuvel case EFI_BOOT_SERVICES_DATA: 149318532bfSArd Biesheuvel /* Ignore types that are released to the OS anyway */ 150318532bfSArd Biesheuvel continue; 151318532bfSArd Biesheuvel 152318532bfSArd Biesheuvel case EFI_CONVENTIONAL_MEMORY: 153318532bfSArd Biesheuvel /* 154318532bfSArd Biesheuvel * Reserve the intersection between this entry and the 155318532bfSArd Biesheuvel * region. 156318532bfSArd Biesheuvel */ 157318532bfSArd Biesheuvel start = max(start, (u64)dram_base); 158318532bfSArd Biesheuvel end = min(end, (u64)dram_base + MAX_UNCOMP_KERNEL_SIZE); 159318532bfSArd Biesheuvel 160318532bfSArd Biesheuvel status = efi_call_early(allocate_pages, 161318532bfSArd Biesheuvel EFI_ALLOCATE_ADDRESS, 162318532bfSArd Biesheuvel EFI_LOADER_DATA, 163318532bfSArd Biesheuvel (end - start) / EFI_PAGE_SIZE, 164318532bfSArd Biesheuvel &start); 165318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 166318532bfSArd Biesheuvel pr_efi_err(sys_table_arg, 167318532bfSArd Biesheuvel "reserve_kernel_base(): alloc failed.\n"); 168318532bfSArd Biesheuvel goto out; 169318532bfSArd Biesheuvel } 170318532bfSArd Biesheuvel break; 171318532bfSArd Biesheuvel 172318532bfSArd Biesheuvel case EFI_LOADER_CODE: 173318532bfSArd Biesheuvel case EFI_LOADER_DATA: 174318532bfSArd Biesheuvel /* 175318532bfSArd Biesheuvel * These regions may be released and reallocated for 176318532bfSArd Biesheuvel * another purpose (including EFI_RUNTIME_SERVICE_DATA) 177318532bfSArd Biesheuvel * at any time during the execution of the OS loader, 178318532bfSArd Biesheuvel * so we cannot consider them as safe. 179318532bfSArd Biesheuvel */ 180318532bfSArd Biesheuvel default: 181318532bfSArd Biesheuvel /* 182318532bfSArd Biesheuvel * Treat any other allocation in the region as unsafe */ 183318532bfSArd Biesheuvel status = EFI_OUT_OF_RESOURCES; 184318532bfSArd Biesheuvel goto out; 185318532bfSArd Biesheuvel } 186318532bfSArd Biesheuvel } 187318532bfSArd Biesheuvel 188318532bfSArd Biesheuvel status = EFI_SUCCESS; 189318532bfSArd Biesheuvel out: 190318532bfSArd Biesheuvel efi_call_early(free_pool, memory_map); 191318532bfSArd Biesheuvel return status; 192318532bfSArd Biesheuvel } 193318532bfSArd Biesheuvel 194318532bfSArd Biesheuvel efi_status_t handle_kernel_image(efi_system_table_t *sys_table, 195318532bfSArd Biesheuvel unsigned long *image_addr, 196318532bfSArd Biesheuvel unsigned long *image_size, 197318532bfSArd Biesheuvel unsigned long *reserve_addr, 198318532bfSArd Biesheuvel unsigned long *reserve_size, 199318532bfSArd Biesheuvel unsigned long dram_base, 200318532bfSArd Biesheuvel efi_loaded_image_t *image) 201318532bfSArd Biesheuvel { 202318532bfSArd Biesheuvel efi_status_t status; 203318532bfSArd Biesheuvel 204318532bfSArd Biesheuvel /* 205318532bfSArd Biesheuvel * Verify that the DRAM base address is compatible with the ARM 206318532bfSArd Biesheuvel * boot protocol, which determines the base of DRAM by masking 207318532bfSArd Biesheuvel * off the low 27 bits of the address at which the zImage is 208318532bfSArd Biesheuvel * loaded. These assumptions are made by the decompressor, 209318532bfSArd Biesheuvel * before any memory map is available. 210318532bfSArd Biesheuvel */ 211318532bfSArd Biesheuvel dram_base = round_up(dram_base, SZ_128M); 212318532bfSArd Biesheuvel 213318532bfSArd Biesheuvel status = reserve_kernel_base(sys_table, dram_base, reserve_addr, 214318532bfSArd Biesheuvel reserve_size); 215318532bfSArd Biesheuvel if (status != EFI_SUCCESS) { 21681a0bc39SRoy Franz pr_efi_err(sys_table, "Unable to allocate memory for uncompressed kernel.\n"); 21781a0bc39SRoy Franz return status; 21881a0bc39SRoy Franz } 21981a0bc39SRoy Franz 22081a0bc39SRoy Franz /* 22181a0bc39SRoy Franz * Relocate the zImage, so that it appears in the lowest 128 MB 22281a0bc39SRoy Franz * memory window. 22381a0bc39SRoy Franz */ 22481a0bc39SRoy Franz *image_size = image->image_size; 22581a0bc39SRoy Franz status = efi_relocate_kernel(sys_table, image_addr, *image_size, 22681a0bc39SRoy Franz *image_size, 22781a0bc39SRoy Franz dram_base + MAX_UNCOMP_KERNEL_SIZE, 0); 22881a0bc39SRoy Franz if (status != EFI_SUCCESS) { 22981a0bc39SRoy Franz pr_efi_err(sys_table, "Failed to relocate kernel.\n"); 23081a0bc39SRoy Franz efi_free(sys_table, *reserve_size, *reserve_addr); 23181a0bc39SRoy Franz *reserve_size = 0; 23281a0bc39SRoy Franz return status; 23381a0bc39SRoy Franz } 23481a0bc39SRoy Franz 23581a0bc39SRoy Franz /* 23681a0bc39SRoy Franz * Check to see if we were able to allocate memory low enough 23781a0bc39SRoy Franz * in memory. The kernel determines the base of DRAM from the 23881a0bc39SRoy Franz * address at which the zImage is loaded. 23981a0bc39SRoy Franz */ 24081a0bc39SRoy Franz if (*image_addr + *image_size > dram_base + ZIMAGE_OFFSET_LIMIT) { 24181a0bc39SRoy Franz pr_efi_err(sys_table, "Failed to relocate kernel, no low memory available.\n"); 24281a0bc39SRoy Franz efi_free(sys_table, *reserve_size, *reserve_addr); 24381a0bc39SRoy Franz *reserve_size = 0; 24481a0bc39SRoy Franz efi_free(sys_table, *image_size, *image_addr); 24581a0bc39SRoy Franz *image_size = 0; 24681a0bc39SRoy Franz return EFI_LOAD_ERROR; 24781a0bc39SRoy Franz } 24881a0bc39SRoy Franz return EFI_SUCCESS; 24981a0bc39SRoy Franz } 250