Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/efi.h>
0004 #include <asm/efi.h>
0005 
0006 #include "efistub.h"
0007 
0008 /**
0009  * efi_low_alloc_above() - allocate pages at or above given address
0010  * @size:   size of the memory area to allocate
0011  * @align:  minimum alignment of the allocated memory area. It should
0012  *      a power of two.
0013  * @addr:   on exit the address of the allocated memory
0014  * @min:    minimum address to used for the memory allocation
0015  *
0016  * Allocate at the lowest possible address that is not below @min as
0017  * EFI_LOADER_DATA. The allocated pages are aligned according to @align but at
0018  * least EFI_ALLOC_ALIGN. The first allocated page will not below the address
0019  * given by @min.
0020  *
0021  * Return:  status code
0022  */
0023 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
0024                  unsigned long *addr, unsigned long min)
0025 {
0026     unsigned long map_size, desc_size, buff_size;
0027     efi_memory_desc_t *map;
0028     efi_status_t status;
0029     unsigned long nr_pages;
0030     int i;
0031     struct efi_boot_memmap boot_map;
0032 
0033     boot_map.map        = &map;
0034     boot_map.map_size   = &map_size;
0035     boot_map.desc_size  = &desc_size;
0036     boot_map.desc_ver   = NULL;
0037     boot_map.key_ptr    = NULL;
0038     boot_map.buff_size  = &buff_size;
0039 
0040     status = efi_get_memory_map(&boot_map);
0041     if (status != EFI_SUCCESS)
0042         goto fail;
0043 
0044     /*
0045      * Enforce minimum alignment that EFI or Linux requires when
0046      * requesting a specific address.  We are doing page-based (or
0047      * larger) allocations, and both the address and size must meet
0048      * alignment constraints.
0049      */
0050     if (align < EFI_ALLOC_ALIGN)
0051         align = EFI_ALLOC_ALIGN;
0052 
0053     size = round_up(size, EFI_ALLOC_ALIGN);
0054     nr_pages = size / EFI_PAGE_SIZE;
0055     for (i = 0; i < map_size / desc_size; i++) {
0056         efi_memory_desc_t *desc;
0057         unsigned long m = (unsigned long)map;
0058         u64 start, end;
0059 
0060         desc = efi_early_memdesc_ptr(m, desc_size, i);
0061 
0062         if (desc->type != EFI_CONVENTIONAL_MEMORY)
0063             continue;
0064 
0065         if (efi_soft_reserve_enabled() &&
0066             (desc->attribute & EFI_MEMORY_SP))
0067             continue;
0068 
0069         if (desc->num_pages < nr_pages)
0070             continue;
0071 
0072         start = desc->phys_addr;
0073         end = start + desc->num_pages * EFI_PAGE_SIZE;
0074 
0075         if (start < min)
0076             start = min;
0077 
0078         start = round_up(start, align);
0079         if ((start + size) > end)
0080             continue;
0081 
0082         status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
0083                      EFI_LOADER_DATA, nr_pages, &start);
0084         if (status == EFI_SUCCESS) {
0085             *addr = start;
0086             break;
0087         }
0088     }
0089 
0090     if (i == map_size / desc_size)
0091         status = EFI_NOT_FOUND;
0092 
0093     efi_bs_call(free_pool, map);
0094 fail:
0095     return status;
0096 }
0097 
0098 /**
0099  * efi_relocate_kernel() - copy memory area
0100  * @image_addr:     pointer to address of memory area to copy
0101  * @image_size:     size of memory area to copy
0102  * @alloc_size:     minimum size of memory to allocate, must be greater or
0103  *          equal to image_size
0104  * @preferred_addr: preferred target address
0105  * @alignment:      minimum alignment of the allocated memory area. It
0106  *          should be a power of two.
0107  * @min_addr:       minimum target address
0108  *
0109  * Copy a memory area to a newly allocated memory area aligned according
0110  * to @alignment but at least EFI_ALLOC_ALIGN. If the preferred address
0111  * is not available, the allocated address will not be below @min_addr.
0112  * On exit, @image_addr is updated to the target copy address that was used.
0113  *
0114  * This function is used to copy the Linux kernel verbatim. It does not apply
0115  * any relocation changes.
0116  *
0117  * Return:      status code
0118  */
0119 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
0120                  unsigned long image_size,
0121                  unsigned long alloc_size,
0122                  unsigned long preferred_addr,
0123                  unsigned long alignment,
0124                  unsigned long min_addr)
0125 {
0126     unsigned long cur_image_addr;
0127     unsigned long new_addr = 0;
0128     efi_status_t status;
0129     unsigned long nr_pages;
0130     efi_physical_addr_t efi_addr = preferred_addr;
0131 
0132     if (!image_addr || !image_size || !alloc_size)
0133         return EFI_INVALID_PARAMETER;
0134     if (alloc_size < image_size)
0135         return EFI_INVALID_PARAMETER;
0136 
0137     cur_image_addr = *image_addr;
0138 
0139     /*
0140      * The EFI firmware loader could have placed the kernel image
0141      * anywhere in memory, but the kernel has restrictions on the
0142      * max physical address it can run at.  Some architectures
0143      * also have a preferred address, so first try to relocate
0144      * to the preferred address.  If that fails, allocate as low
0145      * as possible while respecting the required alignment.
0146      */
0147     nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
0148     status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
0149                  EFI_LOADER_DATA, nr_pages, &efi_addr);
0150     new_addr = efi_addr;
0151     /*
0152      * If preferred address allocation failed allocate as low as
0153      * possible.
0154      */
0155     if (status != EFI_SUCCESS) {
0156         status = efi_low_alloc_above(alloc_size, alignment, &new_addr,
0157                          min_addr);
0158     }
0159     if (status != EFI_SUCCESS) {
0160         efi_err("Failed to allocate usable memory for kernel.\n");
0161         return status;
0162     }
0163 
0164     /*
0165      * We know source/dest won't overlap since both memory ranges
0166      * have been allocated by UEFI, so we can safely use memcpy.
0167      */
0168     memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
0169 
0170     /* Return the new address of the relocated image. */
0171     *image_addr = new_addr;
0172 
0173     return status;
0174 }