0001
0002
0003 #include <linux/efi.h>
0004 #include <asm/efi.h>
0005
0006 #include "efistub.h"
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
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 = ↦
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
0046
0047
0048
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
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
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
0141
0142
0143
0144
0145
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
0153
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
0166
0167
0168 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
0169
0170
0171 *image_addr = new_addr;
0172
0173 return status;
0174 }