From df931e3b4bc07ac752ab92075dfffc32bde5f50e Mon Sep 17 00:00:00 2001 From: Regia König Date: Tue, 16 Aug 2022 12:56:08 +0200 Subject: Minimal working example with GNU-efi --- GNU_efi_HelloWorld/.gitignore | 1 + GNU_efi_HelloWorld/Makefile | 9 ++-- GNU_efi_HelloWorld/hello.c | 1 + GNU_efi_HelloWorld/hello.efi | Bin 0 -> 45555 bytes GNU_efi_HelloWorld/hello.o | Bin 1696 -> 1664 bytes GNU_efi_HelloWorld/hello.so | Bin 0 -> 2170968 bytes documentation/GNU-efi | 111 ++++++++++++++++++++++++++++++++++++++++-- test_code/OVMF_VARS.fd | Bin 540672 -> 540672 bytes 8 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 GNU_efi_HelloWorld/.gitignore create mode 100755 GNU_efi_HelloWorld/hello.efi create mode 100755 GNU_efi_HelloWorld/hello.so diff --git a/GNU_efi_HelloWorld/.gitignore b/GNU_efi_HelloWorld/.gitignore new file mode 100644 index 0000000..ca4663b --- /dev/null +++ b/GNU_efi_HelloWorld/.gitignore @@ -0,0 +1 @@ +gnu-efi diff --git a/GNU_efi_HelloWorld/Makefile b/GNU_efi_HelloWorld/Makefile index 4f00d4a..31195ee 100644 --- a/GNU_efi_HelloWorld/Makefile +++ b/GNU_efi_HelloWorld/Makefile @@ -1,12 +1,11 @@ ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,) -OBJS = main.o +OBJS = hello.o TARGET = hello.efi EFIINC = /usr/include/efi -EFFINCS = -I$(EFFINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol -LIB = /usr/lib64 -EFILIB = /usr/lib64/gnuefi +EFFINCS = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol +EFILIB = /usr/lib EFI_CRT_OBJS = $(EFILIB)/crt0-efi-$(ARCH).o EFI_LDS = $(EFILIB)/elf_$(ARCH)_efi.lds @@ -18,7 +17,7 @@ ifeq ($(ARCH),x86_64) endif LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \ - -Bsymbolic -L $(EFILIB) -L $(LIB) $(EFI_CRT_OBJS) + -Bsymbolic -L $(EFILIB) $(EFI_CRT_OBJS) all: $(TARGET) diff --git a/GNU_efi_HelloWorld/hello.c b/GNU_efi_HelloWorld/hello.c index 26cbd42..52c05b3 100644 --- a/GNU_efi_HelloWorld/hello.c +++ b/GNU_efi_HelloWorld/hello.c @@ -4,6 +4,7 @@ EFI_STATUS EFIAPI efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { + InitializeLib(ImageHandle, SystemTable); Print(L"Hello, world!\n"); diff --git a/GNU_efi_HelloWorld/hello.efi b/GNU_efi_HelloWorld/hello.efi new file mode 100755 index 0000000..27145e5 Binary files /dev/null and b/GNU_efi_HelloWorld/hello.efi differ diff --git a/GNU_efi_HelloWorld/hello.o b/GNU_efi_HelloWorld/hello.o index 6e14a71..22a8362 100644 Binary files a/GNU_efi_HelloWorld/hello.o and b/GNU_efi_HelloWorld/hello.o differ diff --git a/GNU_efi_HelloWorld/hello.so b/GNU_efi_HelloWorld/hello.so new file mode 100755 index 0000000..bc418e3 Binary files /dev/null and b/GNU_efi_HelloWorld/hello.so differ diff --git a/documentation/GNU-efi b/documentation/GNU-efi index 7fb0f1f..8dddc4a 100644 --- a/documentation/GNU-efi +++ b/documentation/GNU-efi @@ -1,5 +1,106 @@ -################################################################## -## ## -## BUILDING HELLOWORLD.EFI WIH GNU-EFI ## -## ## -################################################################## +################################################################################## +## ## +## BUILDING HELLOWORLD.EFI WIH GNU-EFI ## +## ## +################################################################################## + +################################################################################# +# SUMMARY # +################################################################################# + +Minimal working example: + + hello.c +--------------------------------------------------------------------------------- +#include +#include + +EFI_STATUS +EFIAPI +efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { + + InitializeLib(ImageHandle, SystemTable); + Print(L"Hello, world!\n"); + + return EFI_SUCCESS; +} + + +Makefile +-------------------------------------------------------------------------------- +ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,) + +OBJS = hello.o +TARGET = hello.efi + +EFIINC = /usr/include/efi +EFFINCS = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol +EFILIB = /usr/lib +EFI_CRT_OBJS = $(EFILIB)/crt0-efi-$(ARCH).o +EFI_LDS = $(EFILIB)/elf_$(ARCH)_efi.lds + +CFLAGS = $(EFFINCS) -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) $(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) $^ $@ + + + + +################################################################################# +# https://wiki.osdev.org/GNU-EFI # +################################################################################# + +GNU-EFI is a very lightweight developing environment to create UEFI applications. +It is a set of libraries and headers for compiling UEFI applications with a +system's native GCC. + +You can use host native compiler, then convert resulting ELF into UEFI-compatible +PE. + OR +Use GCC Cross-Compiler generating PE directly. + +********************************************************************************* +* $ git clone https://git.code.sf.net/p/gnu-efi/code gnu-efi * +* $ cd gnu-efi * +* $ make * +********************************************************************************* + +This should create +* crt0-efi-x86_64.o: + A CRT0 (C runtime initialization code) that will call the + "efi_main" function + +* libgnuefi.a: + A library containing a single function (_relocate) + that is used by the CRT0 + +* (optional) libefi.a: + A library containing convenience functions like CRC computation, string + length calculation, and easy text printing + +HEADERS can be used from: +* /usr/include/efi (updated to the latest) +* from EDK2 package +* Or from gnu-efi/inc + +LINKER SCRIPT: +* gnu-efi/gnuefi/elf_x86_64_efi.lds + OR +* /usr/lib/elf_x86_64_efi.lds + diff --git a/test_code/OVMF_VARS.fd b/test_code/OVMF_VARS.fd index a7fab53..9883b80 100644 Binary files a/test_code/OVMF_VARS.fd and b/test_code/OVMF_VARS.fd differ -- cgit v1.2.3-55-g7522