UEFI: break down when calling LocateProtocol

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Oython
Posts: 15
Joined: Wed Aug 03, 2022 8:17 pm
Libera.chat IRC: no

UEFI: break down when calling LocateProtocol

Post by Oython »

I just try to develop my boot loader using UEFI, but I immediately encounter a problem.I am using GNU-efi and my code is simple:

Code: Select all

#include "efi.h"
#include "efilib.h"

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
    InitializeLib(ImageHandle, SystemTable);
    EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
    EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP;
    
    SystemTable->BootServices->SetWatchdogTimer(0, 0, 0, NULL);
    SystemTable->BootServices->LocateProtocol(&gop_guid, NULL, (void**) &GOP);
    return EFI_SUCCESS;
}
I tried to run it in VmWare but it breaks down.
I have tried to recompile the library but it doesn't work.
Is there any problem with my code?
Octocontrabass
Member
Member
Posts: 6247
Joined: Mon Mar 25, 2013 7:01 pm

Re: UEFI: break down when calling LocateProtocol

Post by Octocontrabass »

Oython wrote: Sat Jan 10, 2026 2:49 amI tried to run it in VmWare but it breaks down.
Can you be more specific? What exactly is happening that you don't expect your code to do?
Oython wrote: Sat Jan 10, 2026 2:49 amIs there any problem with my code?
I don't see anything wrong with the code you posted, but there might be a problem with how you're building your code.
Oython
Posts: 15
Joined: Wed Aug 03, 2022 8:17 pm
Libera.chat IRC: no

Re: UEFI: break down when calling LocateProtocol

Post by Oython »

Thanks for your reply.
Can you be more specific? What exactly is happening that you don't expect your code to do?
VmWare just halts and pops out a window saying that the UEFI loader occurs an error.
I don't see anything wrong with the code you posted, but there might be a problem with how you're building your code.
I use the following code to compile my loader:

Code: Select all

all: BOOTX64.EFI

loader.o: loader.c Makefile
	x86_64-pc-cygwin-gcc -I./inc -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args -c loader.c -o loader.o -fPIC

loader.so: loader.o Makefile
	ld -shared -Bsymbolic -L./x86_64/lib -L./x86_64/gnuefi -T./gnuefi/elf_x86_64_efi.lds ./x86_64/gnuefi/crt0-efi-x86_64.o loader.o -o loader.so -lgnuefi -lefi -fPIC

BOOTX64.EFI: loader.so Makefile
	objcopy -j .text -j .sdata -j .data -j .rodata -j .dynamic -j .dynsym  -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 loader.so BOOTX64.EFI
In fact, it's adapted from https://wiki.osdev.org/GNU-EFI
My environment is WSL.
Octocontrabass
Member
Member
Posts: 6247
Joined: Mon Mar 25, 2013 7:01 pm

Re: UEFI: break down when calling LocateProtocol

Post by Octocontrabass »

Oython wrote: Sun Jan 11, 2026 12:24 amVmWare just halts and pops out a window saying that the UEFI loader occurs an error.
Is that because your bootloader returns? Normally a bootloader returning means there was an error. Try adding an infinite loop so it won't return.
Oython wrote: Sun Jan 11, 2026 12:24 am

Code: Select all

x86_64-pc-cygwin-gcc
That seems wrong. Why aren't you using x86_64-elf-gcc or just plain gcc?
Oython wrote: Sun Jan 11, 2026 12:24 amIn fact, it's adapted from https://wiki.osdev.org/GNU-EFI
Those instructions might be outdated, you don't need objcopy when ld can directly link an EFI PE executable.
Oython
Posts: 15
Joined: Wed Aug 03, 2022 8:17 pm
Libera.chat IRC: no

Re: UEFI: break down when calling LocateProtocol

Post by Oython »

Now I give up compiling with gnu-efi and just use the api from uefi.And finally I succeed in runing my code.
I guess that there is a problem in the prior way I compiled the code.
Post Reply