我正在尝试使用 gnu-efi 编写一个 hello world 类型的程序,但没有引导服务,因为它们在 ExitBootServices 之后变得不可用。在调用 ExitBootServices 之前直接写入视频内存不会显示任何内容。
出于这个原因,我需要调用 ExitBootServices,它需要一个 Mapkey。MapKey 由 GetMemoryMap 函数提供。但是当我调用它时,我的应用程序崩溃了(我正在使用 qemu)。
这是我的代码:
#include <efi.h>
#include <efilib.h>
void write_string( int color, const char *string )
{
volatile char *video = (volatile char*)0xB8000;
while( *string != 0 )
{
*video++ = *string++;
*video++ = color;
}
}
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_LOADED_IMAGE *loaded_image = NULL;
EFI_STATUS status;
InitializeLib(ImageHandle, SystemTable);
status = uefi_call_wrapper(SystemTable->BootServices->HandleProtocol,
3, ImageHandle, &LoadedImageProtocol, (void **)&loaded_image);
if (EFI_ERROR(status)) {
Print(L"handleprotocol: %r\n", status);
return EFI_SUCCESS;
}
/* GetMemoryMap */
UINTN MemoryMapSize = sizeof(EFI_MEMORY_DESCRIPTOR) * 0x10;
EFI_MEMORY_DESCRIPTOR *MemoryMap = AllocatePool (MemoryMapSize);
UINTN MapKey = 0;
UINTN DescriptorSize = 0;
UINT32 DescriptorVersion = 0;
status = uefi_call_wrapper(SystemTable->BootServices->GetMemoryMap,
&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
if (EFI_ERROR(status)) {
Print(L"GetMemoryMap: %r\n", status);
return EFI_SUCCESS;
}
/* ExitBootServices */
status = uefi_call_wrapper(SystemTable->BootServices->ExitBootServices,
ImageHandle, MapKey);
if (EFI_ERROR(status)) {
Print(L"ExitBootServices: %r\n", status);
return EFI_SUCCESS;
}
write_string(0x07, "example");
}
即使在执行 ExitBootServices 之前,qemu 也会因错误而崩溃:
qemu: fatal: Trying to execute code outside RAM or ROM at 0x00000000000b0000
谁能告诉我在做什么有什么问题?谢谢你。