1#
2# Copyright (c) 2011 - 2022, Intel Corporation.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7#
8#    * Redistributions of source code must retain the above copyright
9#      notice, this list of conditions and the following disclaimer.
10#    * Redistributions in binary form must reproduce the above
11#      copyright notice, this list of conditions and the following
12#      disclaimer in the documentation and/or other materials provided
13#      with the distribution.
14#    * Neither the name of Intel Corporation nor the names of its
15#      contributors may be used to endorse or promote products derived
16#      from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32HV_OBJDIR:=build
33HV_SRC:=../../hypervisor
34C_SRCS = boot.c pe.c malloc.c container.c multiboot.c elf32.c
35ACRN_OBJS := $(patsubst %.c,$(EFI_OBJDIR)/%.o,$(C_SRCS))
36INCLUDE_PATH += $(INCDIR)/efi
37INCLUDE_PATH += $(HV_SRC)/include/public
38INCLUDE_PATH += $(HV_SRC)/include/lib
39
40BOARD ?= tgl-rvp
41SCENARIO ?= hybrid_rt
42
43OBJCOPY=objcopy
44
45ARCH := x86_64
46FORMAT := efi-app-x86-64
47
48# Different Linux distributions have the 'gnu-efi' package install
49# its tools and libraries in different folders. The next couple of
50# variables will determine and set the right path for both the
51# tools $(GNUEFI_DIR) and libraries $(LIBDIR)
52GNUEFI_DIR := $(shell find $(SYSROOT)/usr/lib* -name elf_$(ARCH)_efi.lds -type f | xargs dirname)
53LIBDIR := $(subst gnuefi,,$(GNUEFI_DIR))
54CRT0 := $(GNUEFI_DIR)/crt0-efi-$(ARCH).o
55LDSCRIPT := $(GNUEFI_DIR)/elf_$(ARCH)_efi.lds
56
57INCDIR := $(SYSROOT)/usr/include
58
59CFLAGS=-I. -I.. -I$(INCDIR)/efi -I$(INCDIR)/efi/$(ARCH) \
60		-DEFI_FUNCTION_WRAPPER -fPIC -fshort-wchar -ffreestanding \
61		-Wall -I../fs/ -D$(ARCH) -O2	\
62
63CFLAGS += -mno-mmx -mno-sse -mno-sse2 -mno-80387 -mno-fp-ret-in-387
64
65CFLAGS += -fno-delete-null-pointer-checks -fwrapv -mno-red-zone
66
67LDFLAGS=-T $(LDSCRIPT) -Bsymbolic -shared -nostdlib -znocombreloc \
68		-L$(LIBDIR) $(CRT0)
69BOOT=$(EFI_OBJDIR)/boot.efi
70
71all: $(BOOT)
72
73$(BOOT): $(EFI_OBJDIR)/boot.so
74
75$(EFI_OBJDIR)/boot.so: $(ACRN_OBJS)
76	$(LD) $(LDFLAGS) -o $@ $^  -lgnuefi -lefi $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
77
78clean:
79	rm -f $(BOOT) $(EFI_OBJDIR)/boot.so $(ACRN_OBJS)
80
81-include $(ACRN_OBJS:.o=.d)
82
83$(EFI_OBJDIR)/%.o:%.S
84	[ ! -e $@ ] && mkdir -p $(dir $@); \
85	$(CC) $(CFLAGS)   -c -o $@ $< -MMD -MT $@
86
87$(EFI_OBJDIR)/%.o: %.c
88	[ ! -e $@ ] && mkdir -p $(dir $@); \
89	$(CC) $(patsubst %, -I%, $(INCLUDE_PATH)) -I. -c $(CFLAGS) $(ARCH_CFLAGS) $< -o $@ -MMD -MT $@
90
91%.efi: %.so
92	$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel \
93		-j .rela -j .reloc --target=$(FORMAT) $*.so $@
94