The coffee 17th cup
The coffee 17th cup

Reputation: 99

How to compile uefi application using gnu-efi?

I tried to compile uefi code using gnu-efi. But I don't understand how to compile my uefi application code.

I get gnu-efi 3.0.2, decompress and type make && make install. I write hello world code:

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

EFI_STATUS efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
    InitializeLib(ImageHandle, SystemTable);
    Print(L"Hello, world!\n");

    return EFI_SUCCESS;
}

My OS is Ubuntu 15.04.

Upvotes: 2

Views: 14691

Answers (3)

MrHetii
MrHetii

Reputation: 1505

In my case I created below Makefile to download and compile gnu-efi if needed into main directory.

I compile it under ubuntu 22.04 and successfully boot in real hardware like Synology NAS ds1515+.

# Based on https://wiki.osdev.org/GNU-EFI#Libraries
#
ARCH            = $(shell uname -m | sed s,i[3456789]86,ia32,)

OBJS            = main.o uart.o
TARGET          = hello.efi

EFIINC          = gnu-efi/inc

EFIINCS         = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol

LIB             = gnu-efi/$(ARCH)/lib
EFILIB          = gnu-efi/$(ARCH)/gnuefi

EFI_CRT_OBJS    = $(EFILIB)/crt0-efi-$(ARCH).o
EFI_LDS         = gnu-efi/gnuefi/elf_$(ARCH)_efi.lds

CFLAGS          = $(EFIINCS) -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar -mno-red-zone -maccumulate-outgoing-args

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: prepare $(TARGET)

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

%.efi: %.so
        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-$(ARCH) --subsystem=10 $^ $@

.PHONY: clean prepare

prepare:
        @test ! -d gnu-efi && (git clone https://git.code.sf.net/p/gnu-efi/code gnu-efi && make -C gnu-efi) || true

clean:
        rm -f $(TARGET) *.o *.so

Upvotes: 0

Matthiasho
Matthiasho

Reputation: 89

In Ubuntu 18.04, these two lines:

LIB             = /usr/lib64

EFILIB          = /usr/lib64/gnuefi

need to be changed to:

LIB             = /usr/lib

EFILIB          = /usr/lib

Upvotes: 4

Pat
Pat

Reputation: 2700

  1. Include the gnu-efi files

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

    it looks like your includes where removed by SO

  2. create the make file;

If you were building a "Hello, World" program for Linux in a Linux environment, you could compile it without a Makefile. Building the program in Linux for EFI, though, is essentially a cross-compilation operation. As such, it necessitates using unusual compilation and linker options, as well as a post-linking operation to convert the program into a form that the EFI will accept. Although you could type all the relevant commands by hand, a Makefile helps a lot.

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/lib64
EFILIB          = /usr/lib64/gnuefi
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 $@ -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) $^ $@

reference:

http://www.rodsbooks.com/efi-programming/hello.html

Upvotes: 6

Related Questions