Q
Q
Qreen2021-10-31 19:16:03
C++ / C#
Qreen, 2021-10-31 19:16:03

How to properly compile your first EFI application?

So i have the code

#include <efi.h>
#include <efilib.h>
 
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
  InitializeLib(ImageHandle, SystemTable);
  Print(L"Hello, world!\n");
  return EFI_SUCCESS;
}

I need to somehow compile it. I have tried Makefile
ARCH            = $(shell uname -m | sed s,i[3456789]86,ia32,)

OBJS            = main.o
TARGET          = hello.efi

EFIINC          = /usr/include/efi
EFIINCS         = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol
LIB             = /usr/lib
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) -L $(LIB) $(EFI_CRT_OBJS) 

all: $(TARGET)

hello.so: $(OBJS)
  ld $(LDFLAGS) $(OBJS) -o [email protected] -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) $^ [email protected]

it compiles this code, UEFI sees it, but for some reason, when it starts it, it immediately stops and proceeds to boot the OS. It is located as usual, along the path / efi / boot / bootx64 on a fat32 flash drive. At the same time, I have a helloworld file under uefi that I once compiled, the source code of which I lost. Here it works just fine. Those. We conclude that the problem is in the compilation. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tony, 2021-10-31
@AntonSazonov

Read here.
https://habr.com/ru/post/583450/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question