Merge "Add build rules for u-boot"
diff --git a/Makefile b/Makefile
index 119bf21..0d05b19 100644
--- a/Makefile
+++ b/Makefile
@@ -75,24 +75,24 @@
 TOOLCHAIN_GCC_92 := $(ROOT_DIR)/toolchains/$(UNNAME_S)-x86/gcc/aarch64-linux-gnu-9.2/
 
 # Set the toolchain build binary paths and prefixes.
+TARGET = aarch64-linux-gnu
+TARGET_GCC_92 = aarch64-none-linux-gnu
+
 ifeq ($(TOOLCHAIN), gcc-4.9)
 TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_49)/bin
-TARGET = aarch64-linux-gnu
 $(warning "The gcc-4.9 toolchain is deprecated and will be removed soon.")
 $(warning "The gcc-5.1 toolchain is the oldest version Linux needs to support.")
 else ifeq ($(TOOLCHAIN), gcc-5.1)
 TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_51)/bin
-TARGET = aarch64-linux-gnu
 else ifeq ($(TOOLCHAIN), gcc-9.2)
 TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_92)/bin
-TARGET = aarch64-none-linux-gnu
+TARGET = $(TARGET_GCC_92)
 else ifeq ($(TOOLCHAIN), clang)
 TOOLCHAIN_BIN := $(TOOLCHAIN_CLANG)/bin
 CC := $(TOOLCHAIN_BIN)/clang
 LD := $(TOOLCHAIN_BIN)/ld.lld
 OBJCOPY := $(TOOLCHAIN_BIN)/llvm-objcopy
 OBJDUMP := $(TOOLCHAIN_BIN)/llvm-objdump
-TARGET = aarch64-linux-gnu
 # The GCC toolchain is needed when building kvm-unit-tests as well as Linux
 # with Clang, because of lack of support for the LLVM integrated assembler.
 GCC_TOOLCHAIN_DIR = $(TOOLCHAIN_GCC_49)
@@ -123,14 +123,14 @@
 default: kvm-unit-tests linux
 
 .PHONY: clean
-clean: buildroot_clean kvm-unit-tests_clean linux_clean
+clean: buildroot_clean kvm-unit-tests_clean linux_clean u-boot_clean
 
 .PHONY: distclean
 distclean:
 	- rm -rf $(OUT_DIR) $(DIST_DIR)
 
 .PHONY: all
-all: buildroot kvm-unit-tests linux
+all: buildroot kvm-unit-tests linux u-boot
 
 .PHONY: help
 help:
@@ -140,6 +140,7 @@
 	@echo '   buildroot            - the buildroot rootfs image as well as the host qemu'
 	@echo '   kvm-unit-tests       - the kvm-unit-tests'
 	@echo '   linux                - the linux kernel'
+	@echo '   u-boot               - the u-boot'
 	@echo ''
 	@echo 'Clean Targets:'
 	@echo '   clean                - cleans all, but keeps the prebuilts'
@@ -147,6 +148,7 @@
 	@echo '   buildroot_clean      - the buildroot rootfs image as well as the host qemu'
 	@echo '   kvm-unit-tests_clean - the kvm-unit-tests'
 	@echo '   linux_clean          - the linux kernel'
+	@echo '   u-boot_clean         - the u-boot'
 	@echo ''
 	@echo 'Test Targets:'
 	@echo '   test                 - runs all tests in qemu-aarch64 simulated environment'
@@ -258,6 +260,61 @@
 
 
 ##
+## u-boot
+##
+UBOOT_DEFCONFIG ?= pvmfw_arm64_defconfig
+UBOOT_VERBOSE ?= $(VERBOSE)
+UBOOT_SRC ?= $(ROOT_DIR)/u-boot
+
+# U-boot doesn't support gcc < 6.0. To avoid compilation failure,
+# we upgrade the active toolchain version for the u-boot build.
+ifeq ($(TOOLCHAIN), gcc-5.1)
+$(warning "gcc below 6.0 is not supported by u-boot, switching to 9.2")
+UBOOT_TOOLCHAIN_BIN := $(TOOLCHAIN_GCC_92)/bin
+UBOOT_TARGET = $(TARGET_GCC_92)
+UBOOT_CC := $(TOOLCHAIN_BIN)/$(TARGET)-gcc
+UBOOT_LD := $(TOOLCHAIN_BIN)/$(TARGET)-ld
+UBOOT_GCC_TOOLCHAIN_BIN := $(TOOLCHAIN_BIN)
+else
+UBOOT_TOOLCHAIN_BIN := $(TOOLCHAIN_BIN)
+UBOOT_TARGET = $(TARGET)
+UBOOT_CC := $(CC)
+UBOOT_LD := $(LD)
+UBOOT_GCC_TOOLCHAIN_BIN := $(GCC_TOOLCHAIN_BIN)
+endif
+
+UBOOT_OUT := $(OUT_DIR)/u-boot
+UBOOT_MAKE := \
+	PATH=$(UBOOT_TOOLCHAIN_BIN):$(UBOOT_GCC_TOOLCHAIN_BIN):$(PATH) \
+	ARCH=$(ARCH) \
+	CROSS_COMPILE="$(UBOOT_TARGET)-" \
+	$(MAKE) \
+	CC=$(UBOOT_CC) \
+	LD=$(UBOOT_LD) \
+	-C $(UBOOT_SRC) \
+	V=$(UBOOT_VERBOSE) \
+	O=$(UBOOT_OUT)
+
+UBOOT_CONFIG := $(UBOOT_OUT)/.config
+
+.PHONY: u-boot_defconfig
+u-boot_defconfig $(UBOOT_CONFIG):
+	+ $(UBOOT_MAKE) $(UBOOT_DEFCONFIG)
+
+.PHONY: u-boot_menuconfig
+u-boot_menuconfig: $(UBOOT_CONFIG)
+	+ $(UBOOT_MAKE) menuconfig
+
+.PHONY: u-boot
+u-boot: $(UBOOT_CONFIG)
+	+ $(UBOOT_MAKE)
+
+.PHONY: u-boot_clean
+u-boot_clean:
+	+ $(UBOOT_MAKE) mrproper
+
+
+##
 ## Generating/Updating prebuilts
 ##