【问题标题】:UEFI - malloc call does not returnUEFI - malloc 调用不返回
【发布时间】:2019-01-05 10:10:51
【问题描述】:

我会尽量保持简短。

我创建了一个 UEFI 应用程序,它只使用了一个简单的 malloc,但它从不从调用中返回:

main.c:

#include <efi.h>
#include <efilib.h>
#include <stdlib.h>

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) 
{
    InitializeLib(ImageHandle, SystemTable);

    Print(L"trying to allocate memory\n");
    malloc(16);
    Print(L"allocated successfully\n");

    return EFI_SUCCESS;
}

输出:

trying to allocate memory

系统是linux-ubuntu下的QEMU虚拟机:

"qemu-system-x86_64 -cpu qemu64 -bios Bios/bios.bin -drive file=Bios/app.disk,format=raw -global isa-debugcon.iobase=0x402 -debugcon file:app.ovmf.log"

bios.bin 是 UEFI 固件https://wiki.ubuntu.com/UEFI/OVMF

main.efi 被写入 app.disk

dd if=/dev/zero of=Bios/app.disk bs=1 count=1 seek=$(( (128 * 1024) - 1))
sudo mkfs.vfat Bios/app.disk
mkdir Bios/mnt_app
cp Bios/app.disk Bios/mnt_app
sudo mount Bios/app.disk Bios/mnt_app
sudo cp kernel/main.efi Bios/mnt_app

main.c 的构建过程几乎是完整的从https://www.rodsbooks.com/efi-programming/hello.html 复制粘贴:

ARCH            = $(shell uname -m | sed s,i[3456789]86,ia32,)

OBJS            = main.o
TARGET          = main.efi

EFIINC          = /usr/include/efi
EFIINCS         = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol
EFILIB          = /usr/lib
EFI_CRT_OBJS    = $(EFILIB)/crt0-efi-$(ARCH).o
EFI_LDS         = $(EFILIB)/elf_$(ARCH)_efi.lds

CFLAGS          = $(EFIINCS) -fno-stack-protector -fpic \
          -fshort-wchar -mno-red-zone -Wall 

ifeq ($(ARCH),x86_64)
  CFLAGS += -DEFI_FUNCTION_WRAPPER
endif

LDFLAGS         = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \
          -Bsymbolic -L $(EFILIB) $(EFI_CRT_OBJS) 

all: $(TARGET)

main.so: $(OBJS)
    ld $(LDFLAGS) $(OBJS) -o $@ -lefi -lgnuefi

%.efi: %.so
    objcopy -j .text -j .sdata -j .data -j .dynamic \
        -j .dynsym  -j .rel -j .rela -j .reloc \
        --target=efi-app-$(ARCH) $^ $@

    @echo   done building target

【问题讨论】:

  • 请注意您链接的问候页面的第一个项目符号。归根结底,efi 无法使用 C 库。
  • 嗯,有的有,有的没有,恰好这个不是。
  • 不,标准 C 库不可用,期间。 gnu-efi 甚至普通工具链的“特殊性”意味着您可能会意外地获得在操作系统上下文中运行的库。如果您真的很幸运,它们甚至可能不会崩溃。这并不意味着它们应该被使用,因为它们似乎有效。

标签: c operating-system qemu uefi gnu-efi


【解决方案1】:

由于我不知道的原因,malloc 只是无法正常工作,并且可能不打算使用 - 至少看起来是这样。如果您需要动态内存分配,请使用内存池创建自己的 malloc:

void * malloc(UINTN poolSize)
{
    EFI_STATUS status;
    void * handle;
    Print(L"allocating memory pool\n");
    status = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, poolSize, &handle);

    if(status == EFI_OUT_OF_RESOURCES)
    {
        Print(L"out of resources for pool\n");
        return 0;
    }
    else if(status == EFI_INVALID_PARAMETER)
    {
        Print(L"invalid pool type\n");
        return 0;
    }
    else
    {
        Print(L"memory pool successfully allocated\n");
        return handle;
    }
}

免费等价物:

void free(void * pool)
{
    EFI_STATUS status;
    Print(L"freeing memory pool\n");
    status = uefi_call_wrapper(BS->FreePool, 1, pool);

    if(status == EFI_INVALID_PARAMETER)
    {
        Print(L"invalid pool pointer\n");
    }
    else
    {
        Print(L"memory pool successfully freed\n");
    }
}

像通常使用 malloc 和 free 一样使用它:

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) 
{
    InitializeLib(ImageHandle, SystemTable);

    void * memoryPointer = malloc(1024);
    free(memoryPointer);

    return EFI_SUCCESS;
}

输出:

allocating memory pool
memory pool successfully allocated
freeing memory pool
memory pool successfully freed

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 2017-10-08
    • 2015-06-17
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多