#
# Makefile
#
# Leendert van Doorn, leendert@watson.ibm.com
# Copyright (c) 2005, International Business Machines Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; If not, see <http://www.gnu.org/licenses/>.
#

XEN_ROOT = $(CURDIR)/../../..
include $(XEN_ROOT)/tools/firmware/Rules.mk


LOADADDR = 0x100000

# SMBIOS spec requires format mm/dd/yyyy
SMBIOS_REL_DATE ?= $(shell date +%m/%d/%Y)

CFLAGS += $(CFLAGS_xeninclude)

# We mustn't use tools-only public interfaces.
CFLAGS += -D__XEN_INTERFACE_VERSION__=__XEN_LATEST_INTERFACE_VERSION__

OBJS  = hvmloader.o mp_tables.o util.o smbios.o 
OBJS += smp.o cacheattr.o xenbus.o vnuma.o
OBJS += e820.o pci.o pir.o ctype.o
OBJS += hvm_param.o
OBJS += ovmf.o seabios.o
ifeq ($(debug),y)
OBJS += tests.o
endif

CIRRUSVGA_DEBUG ?= n

ROMBIOS_DIR := ../rombios

ifeq ($(CONFIG_ROMBIOS),y)
STDVGA_ROM    := ../vgabios/VGABIOS-lgpl-latest.bin
ifeq ($(CIRRUSVGA_DEBUG),y)
CIRRUSVGA_ROM := ../vgabios/VGABIOS-lgpl-latest.cirrus.debug.bin
else
CIRRUSVGA_ROM := ../vgabios/VGABIOS-lgpl-latest.cirrus.bin
endif
ETHERBOOT_ROMS := $(addprefix ../etherboot/ipxe/src/bin/, $(addsuffix .rom, $(ETHERBOOT_NICS)))
endif

ROMS := 

ifeq ($(CONFIG_ROMBIOS),y)
OBJS += optionroms.o 32bitbios_support.o rombios.o
CFLAGS += -DENABLE_ROMBIOS
ROMBIOS_ROM := $(ROMBIOS_DIR)/BIOS-bochs-latest
ROMS += $(ROMBIOS_ROM) $(STDVGA_ROM) $(CIRRUSVGA_ROM) $(ETHERBOOT_ROMS)
endif

.PHONY: all
all: acpi subdirs-all
	$(MAKE) hvmloader

.PHONY: acpi
acpi:
	$(MAKE) -C $(ACPI_PATH)  ACPI_BUILD_DIR=$(CURDIR) DSDT_FILES="$(DSDT_FILES)"

rombios.o: roms.inc
smbios.o: CFLAGS += -D__SMBIOS_DATE__="\"$(SMBIOS_REL_DATE)\""

ACPI_PATH = ../../libacpi
DSDT_FILES = dsdt_anycpu.c dsdt_15cpu.c dsdt_anycpu_qemu_xen.c
ACPI_OBJS = $(patsubst %.c,%.o,$(DSDT_FILES)) build.o static_tables.o
$(ACPI_OBJS): CFLAGS += -I. -DLIBACPI_STDUTILS=\"$(CURDIR)/util.h\"
CFLAGS += -I$(ACPI_PATH)
vpath build.c $(ACPI_PATH)
vpath static_tables.c $(ACPI_PATH)
OBJS += $(ACPI_OBJS)

hvmloader: $(OBJS)
	$(LD) $(LDFLAGS_DIRECT) -N -Ttext $(LOADADDR) -o hvmloader.tmp $^
	$(OBJCOPY) hvmloader.tmp hvmloader
	rm -f hvmloader.tmp

roms.inc: $(ROMS)
	echo "/* Autogenerated file. DO NOT EDIT */" > $@.new

ifneq ($(ROMBIOS_ROM),)
	echo "#ifdef ROM_INCLUDE_ROMBIOS" >> $@.new
	sh ./mkhex rombios $(ROMBIOS_ROM) >> $@.new
	echo "#endif" >> $@.new
endif

ifneq ($(STDVGA_ROM),)
	echo "#ifdef ROM_INCLUDE_VGABIOS" >> $@.new
	sh ./mkhex vgabios_stdvga $(STDVGA_ROM) >> $@.new
	echo "#endif" >> $@.new
endif
ifneq ($(CIRRUSVGA_ROM),)
	echo "#ifdef ROM_INCLUDE_VGABIOS" >> $@.new
	sh ./mkhex vgabios_cirrusvga $(CIRRUSVGA_ROM) >> $@.new
	echo "#endif" >> $@.new
endif
ifneq ($(ETHERBOOT_ROMS),)
	echo "#ifdef ROM_INCLUDE_ETHERBOOT" >> $@.new
	sh ./mkhex etherboot $(ETHERBOOT_ROMS) >> $@.new
	echo "#endif" >> $@.new
endif

	mv $@.new $@

.PHONY: clean
clean: subdirs-clean
	rm -f roms.inc roms.inc.new acpi.h
	rm -f hvmloader hvmloader.tmp *.o $(DEPS)
	$(MAKE) -C $(ACPI_PATH)  ACPI_BUILD_DIR=$(CURDIR) clean

.PHONY: distclean
distclean: clean

-include $(DEPS)
