-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Makefile
171 lines (153 loc) · 5.26 KB
/
Makefile
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
ARCH = x64
# You can alter the subsystem according to your EFI binary target:
# 10 = EFI application
# 11 = EFI boot service driver
# 12 = EFI runtime driver
SUBSYSTEM = 10
# Try to auto-detect the target ARCH
ifeq ($(shell uname -o),Msys)
IS_MINGW32 = $(findstring MINGW32,$(shell uname -s))
IS_MINGW64 = $(findstring MINGW64,$(shell uname -s))
ifeq ($(IS_MINGW32),MINGW32)
ARCH = ia32
endif
ifeq ($(IS_MINGW64),MINGW64)
ARCH = x64
endif
else
ifeq ($(shell uname -m),x86_64)
ARCH = x64
else ifeq ($(shell uname -m),arm)
ARCH = arm
CROSS_COMPILE =
else ifeq ($(shell uname -m),aarch64)
ARCH = aa64
CROSS_COMPILE =
else
ARCH = ia32
endif
endif
# Auto-detect the host arch for MinGW
ifeq ($(shell uname -m),x86_64)
MINGW_HOST = w64
else
MINGW_HOST = w32
endif
ifeq ($(ARCH),x64)
GNUEFI_ARCH = x86_64
GCC_ARCH = x86_64
QEMU_ARCH = x86_64
FW_BASE = OVMF
CROSS_COMPILE = $(GCC_ARCH)-$(MINGW_HOST)-mingw32-
EP_PREFIX =
CFLAGS = -m64 -mno-red-zone
LDFLAGS = -Wl,-dll -Wl,--subsystem,$(SUBSYSTEM)
else ifeq ($(ARCH),ia32)
GNUEFI_ARCH = ia32
GCC_ARCH = i686
QEMU_ARCH = i386
FW_BASE = OVMF
CROSS_COMPILE = $(GCC_ARCH)-$(MINGW_HOST)-mingw32-
EP_PREFIX = _
CFLAGS = -m32 -mno-red-zone
LDFLAGS = -Wl,-dll -Wl,--subsystem,$(SUBSYSTEM)
else ifeq ($(ARCH),arm)
GNUEFI_ARCH = arm
GCC_ARCH = arm
QEMU_ARCH = arm
FW_BASE = QEMU_EFI
CROSS_COMPILE = $(GCC_ARCH)-linux-gnueabihf-
EP_PREFIX =
CFLAGS = -marm -fpic -fshort-wchar
LDFLAGS = -Wl,--no-wchar-size-warning -Wl,--defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
CRT0_LIBS = -lgnuefi
QEMU_OPTS = -M virt -cpu cortex-a15
else ifeq ($(ARCH),aa64)
GNUEFI_ARCH = aarch64
GCC_ARCH = aarch64
QEMU_ARCH = aarch64
FW_BASE = QEMU_EFI
CROSS_COMPILE = $(GCC_ARCH)-linux-gnu-
EP_PREFIX =
CFLAGS = -fpic -fshort-wchar
LDFLAGS = -Wl,--no-wchar-size-warning -Wl,--defsym=EFI_SUBSYSTEM=$(SUBSYSTEM)
CRT0_LIBS = -lgnuefi
QEMU_OPTS = -M virt -cpu cortex-a57
endif
FW_ARCH = $(shell echo $(ARCH) | tr a-z A-Z)
FW_ZIP = $(FW_BASE)-$(FW_ARCH).zip
GNUEFI_DIR = $(CURDIR)/gnu-efi
GNUEFI_LIBS = lib
# If the compiler produces an elf binary, we need to fiddle with a PE crt0
ifneq ($(CRT0_LIBS),)
CRT0_DIR = $(GNUEFI_DIR)/$(GNUEFI_ARCH)/gnuefi
LDFLAGS += -L$(CRT0_DIR) -T $(GNUEFI_DIR)/gnuefi/elf_$(GNUEFI_ARCH)_efi.lds $(CRT0_DIR)/crt0-efi-$(GNUEFI_ARCH).o
GNUEFI_LIBS += gnuefi
endif
# SYSTEMROOT is only defined on Windows systems
ifneq ($(SYSTEMROOT),)
QEMU = "/c/Program Files/qemu/qemu-system-$(QEMU_ARCH)w.exe"
# MinGW on Windows doesn't use (tuple)-ar but (tuple)-gcc-ar
# so we remove the cross compiler tuple altogether
CROSS_COMPILE =
else
QEMU = qemu-system-$(QEMU_ARCH) -nographic
endif
CC := $(CROSS_COMPILE)gcc
OBJCOPY := $(CROSS_COMPILE)objcopy
CFLAGS += -fno-stack-protector -Wshadow -Wall -Wunused -Werror-implicit-function-declaration
CFLAGS += -I$(GNUEFI_DIR)/inc -I$(GNUEFI_DIR)/inc/$(GNUEFI_ARCH) -I$(GNUEFI_DIR)/inc/protocol
CFLAGS += -DCONFIG_$(GNUEFI_ARCH) -D__MAKEWITH_GNUEFI -DGNU_EFI_USE_MS_ABI
LDFLAGS += -L$(GNUEFI_DIR)/$(GNUEFI_ARCH)/lib -e $(EP_PREFIX)efi_main
LDFLAGS += -s -Wl,-Bsymbolic -nostdlib -shared
LIBS = -lefi $(CRT0_LIBS)
ifeq (, $(shell which $(CC)))
$(error The selected compiler ($(CC)) was not found)
endif
GCCVERSION := $(shell $(CC) -dumpversion | cut -f1 -d.)
GCCMINOR := $(shell $(CC) -dumpversion | cut -f2 -d.)
GCCMACHINE := $(shell $(CC) -dumpmachine)
GCCNEWENOUGH := $(shell ( [ $(GCCVERSION) -gt "4" ] \
|| ( [ $(GCCVERSION) -eq "4" ] \
&& [ $(GCCMINOR) -ge "7" ] ) ) \
&& echo 1)
ifneq ($(GCCNEWENOUGH),1)
$(error You need GCC 4.7 or later)
endif
ifneq ($(GCC_ARCH),$(findstring $(GCC_ARCH), $(GCCMACHINE)))
$(error The selected compiler ($(CC)) is not set for $(ARCH))
endif
.PHONY: all clean superclean
all: $(GNUEFI_DIR)/$(GNUEFI_ARCH)/lib/libefi.a main.efi
$(GNUEFI_DIR)/$(GNUEFI_ARCH)/lib/libefi.a:
$(MAKE) -C$(GNUEFI_DIR) CROSS_COMPILE=$(CROSS_COMPILE) ARCH=$(GNUEFI_ARCH) $(GNUEFI_LIBS)
%.efi: %.o
@echo [LD] $(notdir $@)
ifeq ($(CRT0_LIBS),)
@$(CC) $(LDFLAGS) $< -o $@ $(LIBS)
else
@$(CC) $(LDFLAGS) $< -o $*.elf $(LIBS)
@$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel* \
-j .rela* -j .reloc -j .eh_frame -O binary $*.elf $@
@rm -f $*.elf
endif
%.o: %.c
@echo [CC] $(notdir $@)
@$(CC) $(CFLAGS) -ffreestanding -c $<
qemu: CFLAGS += -D_DEBUG
qemu: all $(FW_BASE)_$(FW_ARCH).fd image/efi/boot/boot$(ARCH).efi
$(QEMU) $(QEMU_OPTS) -bios ./$(FW_BASE)_$(FW_ARCH).fd -net none -hda fat:rw:image
image/efi/boot/boot$(ARCH).efi: main.efi
mkdir -p image/efi/boot
cp -f $< $@
$(FW_BASE)_$(FW_ARCH).fd:
wget https://efi.akeo.ie/$(FW_BASE)/$(FW_ZIP)
unzip $(FW_ZIP) $(FW_BASE).fd
mv $(FW_BASE).fd $(FW_BASE)_$(FW_ARCH).fd
rm $(FW_ZIP)
clean:
rm -f main.efi *.o
rm -rf image
superclean: clean
$(MAKE) -C$(GNUEFI_DIR) ARCH=$(GNUEFI_ARCH) clean
rm -f *.fd