Add build rules for u-boot

Make works with TOOLCHAIN=clang and TOOLCHAIN=gcc-9.2.

test: make u-boot produces a binary that boots with run_qemu.sh
test: verified that all commands use prebuilts from toolchains/

Change-Id: If3a3c13717dbd0f029b4ac94c0746c26397d227f
diff --git a/Makefile b/Makefile
index 2c9fee4..45e44b1 100644
--- a/Makefile
+++ b/Makefile
@@ -62,24 +62,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_51)
@@ -110,14 +110,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)
 
 .PHONY: all
-all: buildroot kvm-unit-tests linux
+all: buildroot kvm-unit-tests linux u-boot
 
 .PHONY: help
 help:
@@ -127,6 +127,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'
@@ -134,6 +135,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'
@@ -239,6 +241,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
 ##