summaryrefslogblamecommitdiffstats
path: root/documentation/GNU-efi
blob: 63097fee00c1720091d6a80d343c795bbe506c3f (plain) (tree)









































































































                                                                                        





























































                                                                                       
        
 

                                                                           
 





                                                                             




 
##################################################################################
##										##
##		BUILDING HELLOWORLD.EFI WIH GNU-EFI				##	
##										##
##################################################################################

#################################################################################
#				SUMMARY						#
#################################################################################

Minimal working example:

	hello.c
---------------------------------------------------------------------------------
#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;
}


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

COMPILATION:
$ gcc
	-Ignu-efi-dir/inc		||	set this to the efi headers directory

	-fpic				||	UEFI PE executable must be relocatable

	-ffreestanding			||	there's no hosted gcc environment,
						we don't have libc
	-fno-stack-protector		=	
	-fno-stack-check		||	stack must be strictly used,
					||	no additional canaries or
					||	pre-allocated local variable
					||	space allowed
	-mno-red-zone			=

	-fshort-wchar			||	It is very important that UEFI
					||	uses 16bit characters
					||	(wide-characters or wchar_t,
					||	defined as CHAR16 in efi headers

	-maccumulate-outgoing-args	|| 	function calls must include the
					||	number of argumnets passed to the
					|| 	functions

	-c main.c -o main.o

LINKING:
$ ld 	-shared -Bsymbolic -Lgnu-efi-dir/x86_64/lib -Lgnu-efi-dir/x86_64/gnuefi \
	-Tgnu-efi-dir/gnuefi/elf_x86_64_efi.lds \
	gnu-efi-dir/x86_64/gnuefi/crt0-efi-x86_64.o \
	main.o -o main.so -lgnuefi -lefi

	-shared -Bsymbolic		|| tell GNU ld to create so (shared library)

	-L and -T			|| Where to find the static GNU-EFI libraries
					|| (.a) and the linker script

	.o				|| it is important to specify crt0 as the
					|| first. Should work as the last too, but
					|| some had problems

	-l				|| linking with gnuefi is a must, as that 
					|| contains the relocation code. Linking
					|| with efi is optional, but recommended

CONVERT CHARED OBJECT TO EFI EXECUTABLE
$ objcopy -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.*
	-j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 main.so main.efi

	-j				|| which sections to keep during convertion

	--target efi-app-x86_64		|| tells objcop to generate a PE32+ format,
					|| with architecture code 0x8664

	--subsystem=10:			|| most important. Sets file type to UEFI
					|| executable in the PE header

Now you can copy main.efi to your EFI System Partition, and after boot run it
from the EFI Shell. Or you can rename it to EFI\BOOT\BOOTX64.EFI and it should
be executed automatically on boot. 


LIBEFI.A

Has wrappers for the most common UEFI functions, but you might need to call
something not covered. For completeness, it provides:

uefi_call_wrapper(func, numarg, ...);

For example, the "Print" function used in our main.c and which accepts printf
compatible arguments, is under the hood nothing else than a call to:

uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, buffer);