1 /* 2 * Copyright (C) 2021 Intel Corporation. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <types.h> 8 #include <boot.h> 9 #include <efi.h> 10 #include <efi_mmap.h> 11 #include <logmsg.h> 12 13 static uint32_t hv_memdesc_nr; 14 static struct efi_memory_desc hv_memdesc[MAX_EFI_MMAP_ENTRIES]; 15 sort_efi_mmap_entries(void)16static void sort_efi_mmap_entries(void) 17 { 18 uint32_t i, j; 19 struct efi_memory_desc tmp_memdesc; 20 21 /* Bubble sort */ 22 for (i = 0U; i < (hv_memdesc_nr - 1U); i++) { 23 for (j = 0U; j < (hv_memdesc_nr - i - 1U); j++) { 24 if (hv_memdesc[j].phys_addr > hv_memdesc[j + 1U].phys_addr) { 25 tmp_memdesc = hv_memdesc[j]; 26 hv_memdesc[j] = hv_memdesc[j + 1U]; 27 hv_memdesc[j + 1U] = tmp_memdesc; 28 } 29 } 30 } 31 } 32 33 /** 34 * @pre (uefi_info->memmap_size / uefi_info->memdesc_size) <= MAX_EFI_MMAP_ENTRIES 35 */ init_efi_mmap_entries(struct efi_info * uefi_info)36void init_efi_mmap_entries(struct efi_info *uefi_info) 37 { 38 void *efi_memmap = (void *)((uint64_t)uefi_info->memmap | ((uint64_t)uefi_info->memmap_hi << 32U)); 39 struct efi_memory_desc *efi_memdesc = (struct efi_memory_desc *)efi_memmap; 40 uint32_t entry = 0U; 41 42 ASSERT((uefi_info->memmap_size / uefi_info->memdesc_size) <= MAX_EFI_MMAP_ENTRIES); 43 44 while ((void *)efi_memdesc < (efi_memmap + uefi_info->memmap_size)) { 45 hv_memdesc[entry] = *efi_memdesc; 46 47 /* Per UEFI spec, EFI_MEMORY_DESCRIPTOR array element returned in MemoryMap. 48 * The size is returned to allow for future expansion of the EFI_MEMORY_DESCRIPTOR 49 * in response to hardware innovation. The structure of the EFI_MEMORY_DESCRIPTOR 50 * may be extended in the future but it will remain backwards compatible with the 51 * current definition. Thus OS software must use the DescriptorSize to find the 52 * start of each EFI_MEMORY_DESCRIPTOR in the MemoryMap array. 53 */ 54 efi_memdesc = (struct efi_memory_desc *)((void *)efi_memdesc + uefi_info->memdesc_size); 55 entry ++; 56 } 57 58 hv_memdesc_nr = entry; 59 60 sort_efi_mmap_entries(); 61 } 62 get_efi_mmap_entries_count(void)63uint32_t get_efi_mmap_entries_count(void) 64 { 65 return hv_memdesc_nr; 66 } 67 get_efi_mmap_entry(void)68const struct efi_memory_desc *get_efi_mmap_entry(void) 69 { 70 return hv_memdesc; 71 } 72