summaryrefslogtreecommitdiffstats
path: root/documentation/GNU-efi
blob: 8dddc4a387a4b2b831f2fb09c80ac9bf4508d0cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
##################################################################################
##										##
##		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