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.
Oython
Posts: 15 Joined: Wed Aug 03, 2022 8:17 pm
Libera.chat IRC: no
Post
by Oython » Sat Jan 10, 2026 2:49 am
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
Posts: 6247 Joined: Mon Mar 25, 2013 7:01 pm
Post
by Octocontrabass » Sat Jan 10, 2026 4:58 pm
Oython wrote: ↑ Sat Jan 10, 2026 2:49 am I 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 am Is 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
Post
by Oython » Sun Jan 11, 2026 12:24 am
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
Posts: 6247 Joined: Mon Mar 25, 2013 7:01 pm
Post
by Octocontrabass » Sun Jan 11, 2026 6:01 pm
Oython wrote: ↑ Sun Jan 11, 2026 12:24 am VmWare 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
That seems wrong. Why aren't you using x86_64-elf-gcc or just plain gcc?
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
Post
by Oython » Tue Jan 13, 2026 12:23 am
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.