UEFI Memory V E820 Memory



Here is the source code for a simple UEFI command line utility to print out the the EFI memory regions:
 
#include <efi.h>
#include <efilib.h>
 
#define PAGE_SIZE 4096
 
const CHAR16 *memory_types[] = {
    L"EfiReservedMemoryType",
    L"EfiLoaderCode",
    L"EfiLoaderData",
    L"EfiBootServicesCode",
    L"EfiBootServicesData",
    L"EfiRuntimeServicesCode",
    L"EfiRuntimeServicesData",
    L"EfiConventionalMemory",
    L"EfiUnusableMemory",
    L"EfiACPIReclaimMemory",
    L"EfiACPIMemoryNVS",
    L"EfiMemoryMappedIO",
    L"EfiMemoryMappedIOPortSpace",
    L"EfiPalCode",
};
 
const CHAR16 *
memory_type_to_str(UINT32 type)
{
    if (type > sizeof(memory_types)/sizeof(CHAR16 *))
        return L"Unknown";
 
    return memory_types[type];
}
 
EFI_STATUS
memory_map(EFI_MEMORY_DESCRIPTOR **map_buf, UINTN *map_size,
           UINTN *map_key, UINTN *desc_size, UINT32 *desc_version)
{
    EFI_STATUS err = EFI_SUCCESS;
 
    *map_size = sizeof(**map_buf) * 31;
 
get_map:
    *map_size += sizeof(**map_buf);
 
    err = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, *map_size, (void **)map_buf);
    if (err != EFI_SUCCESS) {
        Print(L"ERROR: Failed to allocate pool for memory map");
        return err;
    }
 
    err = uefi_call_wrapper(BS->GetMemoryMap, 5, map_size, *map_buf, map_key, desc_size, desc_version);
    if (err != EFI_SUCCESS) {
        if (err == EFI_BUFFER_TOO_SMALL) {
            uefi_call_wrapper(BS->FreePool, 1, (void *)*map_buf);
            goto get_map;
        }
        Print(L"ERROR: Failed to get memory map");
    }
    return err;
}
 
EFI_STATUS
print_memory_map(void)
{
    EFI_MEMORY_DESCRIPTOR *buf;
    UINTN desc_size;
    UINT32 desc_version;
    UINTN size, map_key, mapping_size;
    EFI_MEMORY_DESCRIPTOR *desc;
    EFI_STATUS err = EFI_SUCCESS;
    int i = 0;
 
    err = memory_map(&buf, &size, &map_key, &desc_size, &desc_version);
    if (err != EFI_SUCCESS)
        return err;
 
    Print(L"Memory Map Size: %d\n", size);
    Print(L"Map Key: %d\n", map_key);
    Print(L"Descriptor Version: %d\n", desc_version);
    Print(L"Descriptor Size: %d\n\n", desc_size);
 
    desc = buf;
    while ((void *)desc < (void *)buf + size) {
        mapping_size = desc->NumberOfPages * PAGE_SIZE;
 
        Print(L"[#%02d] Type: %s  Attr: 0x%x\n", i, memory_type_to_str(desc->Type), desc->Attribute);
        Print(L"      Phys: %016llx-%016llx\n", desc->PhysicalStart, desc->PhysicalStart + mapping_size);
        Print(L"      Virt: %016llx-%016llx\n\n", desc->VirtualStart, desc->VirtualStart + mapping_size);
 
        desc = (void *)desc + desc_size;
        i++;
    }
 
    uefi_call_wrapper(BS->FreePool, 1, buf);
    return err;
}
 
 
EFI_STATUS
efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
{
    InitializeLib(image, systab);
 
    print_memory_map();
 
    return EFI_SUCCESS;
}
 

Provide the additional kernel command line option, add_efi_memmap, on the kernel command line.

add_efi_memmap - Include EFI memory map of available physical RAM
                
                 If the EFI memory map has additional entries not in the E820 map,
                 include those entries in the kernels memory map of available physical RAM

Created a suitable UEFI BootXXXX variable containing this kernel option using efibootmgr:

# echo  " root=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx rd.md=0 rd.lvm=0 rd.dm=0 " \
        " KEYTABLE=us SYSFONT=True rd.luks=0 ro LANG=en_US.UTF-8 add_efi_memmap " \
        " rhgb quiet initrd=\initramfs.img" | iconv -f ascii -t ucs2 |
  efibootmgr --create --gpt --label "Fedora 17 (EFISTUB)"  --loader vmlinuz.efi --disk /dev/sdb --append-binary-args -

The kernel options must be encoded as UCS2 instead of ASCII or UTF8 because that is what the UEFI specification calls for.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UEFI memory指的是在UEFI环境下进行内存管理的一种机制。UEFI(Unified Extensible Firmware Interface)是一种新一代的固件接口标准,代替了传统的BIOS。在UEFI环境下,内存管理是通过UEFI提供的函数来进行的。 其中,UEFI提供的函数可以用来从指定类型的内存中分配一定大小的内存区域,并返回被分配的内存地址。这些函数会确保分配的内存是按八字节对齐的。 此外,UEFI还提供了另一类函数,用于分配一定数量的页面,并返回所分配页面范围的基址。这些函数会通过扫描内存映射来定位空闲的内存页面,并找到连续且足够大的页面来满足分配请求。一旦找到满足条件的页面,函数会修改内存映射,以指示这些页面的类型为MemoryType。 因此,UEFI memory管理涉及使用UEFI提供的函数来分配和管理内存,确保内存按需分配且对齐。这些函数提供了灵活的内存管理机制,用于支持不同类型和大小的内存分配需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [A Tour beyond BIOS Memory Map Design in UEFI BIOS](https://download.csdn.net/download/xiaoming141/10017477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【BIOS/UEFIMemory Service(篇目二)DXE内存服务](https://blog.csdn.net/weixin_45258382/article/details/129850439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值