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 static inline bool mmap_has_headroom(unsigned long buff_size,
0009                      unsigned long map_size,
0010                      unsigned long desc_size)
0011 {
0012     unsigned long slack = buff_size - map_size;
0013 
0014     return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
0015 }
0016 
0017 /**
0018  * efi_get_memory_map() - get memory map
0019  * @map:    on return pointer to memory map
0020  *
0021  * Retrieve the UEFI memory map. The allocated memory leaves room for
0022  * up to EFI_MMAP_NR_SLACK_SLOTS additional memory map entries.
0023  *
0024  * Return:  status code
0025  */
0026 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map)
0027 {
0028     efi_memory_desc_t *m = NULL;
0029     efi_status_t status;
0030     unsigned long key;
0031     u32 desc_version;
0032 
0033     *map->desc_size =   sizeof(*m);
0034     *map->map_size =    *map->desc_size * 32;
0035     *map->buff_size =   *map->map_size;
0036 again:
0037     status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
0038                  *map->map_size, (void **)&m);
0039     if (status != EFI_SUCCESS)
0040         goto fail;
0041 
0042     *map->desc_size = 0;
0043     key = 0;
0044     status = efi_bs_call(get_memory_map, map->map_size, m,
0045                  &key, map->desc_size, &desc_version);
0046     if (status == EFI_BUFFER_TOO_SMALL ||
0047         !mmap_has_headroom(*map->buff_size, *map->map_size,
0048                    *map->desc_size)) {
0049         efi_bs_call(free_pool, m);
0050         /*
0051          * Make sure there is some entries of headroom so that the
0052          * buffer can be reused for a new map after allocations are
0053          * no longer permitted.  Its unlikely that the map will grow to
0054          * exceed this headroom once we are ready to trigger
0055          * ExitBootServices()
0056          */
0057         *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
0058         *map->buff_size = *map->map_size;
0059         goto again;
0060     }
0061 
0062     if (status == EFI_SUCCESS) {
0063         if (map->key_ptr)
0064             *map->key_ptr = key;
0065         if (map->desc_ver)
0066             *map->desc_ver = desc_version;
0067     } else {
0068         efi_bs_call(free_pool, m);
0069     }
0070 
0071 fail:
0072     *map->map = m;
0073     return status;
0074 }
0075 
0076 /**
0077  * efi_allocate_pages() - Allocate memory pages
0078  * @size:   minimum number of bytes to allocate
0079  * @addr:   On return the address of the first allocated page. The first
0080  *      allocated page has alignment EFI_ALLOC_ALIGN which is an
0081  *      architecture dependent multiple of the page size.
0082  * @max:    the address that the last allocated memory page shall not
0083  *      exceed
0084  *
0085  * Allocate pages as EFI_LOADER_DATA. The allocated pages are aligned according
0086  * to EFI_ALLOC_ALIGN. The last allocated page will not exceed the address
0087  * given by @max.
0088  *
0089  * Return:  status code
0090  */
0091 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
0092                 unsigned long max)
0093 {
0094     efi_physical_addr_t alloc_addr;
0095     efi_status_t status;
0096 
0097     if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
0098         return efi_allocate_pages_aligned(size, addr, max,
0099                           EFI_ALLOC_ALIGN);
0100 
0101     alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
0102     status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
0103                  EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
0104                  &alloc_addr);
0105     if (status != EFI_SUCCESS)
0106         return status;
0107 
0108     *addr = alloc_addr;
0109     return EFI_SUCCESS;
0110 }
0111 
0112 /**
0113  * efi_free() - free memory pages
0114  * @size:   size of the memory area to free in bytes
0115  * @addr:   start of the memory area to free (must be EFI_PAGE_SIZE
0116  *      aligned)
0117  *
0118  * @size is rounded up to a multiple of EFI_ALLOC_ALIGN which is an
0119  * architecture specific multiple of EFI_PAGE_SIZE. So this function should
0120  * only be used to return pages allocated with efi_allocate_pages() or
0121  * efi_low_alloc_above().
0122  */
0123 void efi_free(unsigned long size, unsigned long addr)
0124 {
0125     unsigned long nr_pages;
0126 
0127     if (!size)
0128         return;
0129 
0130     nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
0131     efi_bs_call(free_pages, addr, nr_pages);
0132 }