2015-07-20 167 views
1

我试图用gnu-efi来编译uefi代码。但我不明白如何编译我的uefi应用程序代码。如何使用gnu-efi编译uefi应用程序?

我得到gnu-efi 3.0.2,解压缩并输入make && make install。我写你好世界代码:

#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; 
} 

我的操作系统是Ubuntu 15.04。

回答

3
  1. 包括GNU-EFI文件

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

    它看起来像你的包括,其中通过SO

  2. 创建make文件删除;

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 [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] 

参考:

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

+0

我编译成功!谢谢! –

+0

@ Thecoffee17thcup你可以让我知道我如何在Ubuntu中构建它?我一直有“致命的错误:efi.h:没有这样的文件或目录” – Sam

+0

这是一个容易的。您的编译器无法看到'efi.h',请检查您的'make'文件; 'EFIINC =/usr/include/efi'应该指向所需的gnu-efi头文件。 – Pat

相关问题