#!/usr/bin/make -f

#
# \brief   Tool for creating an MMC image with a platform specific U-Boot setup
# \author  Martin Stein
# \date    2015-09-30
#

#
# Print help text
#
help:
	$(ECHO)
	$(ECHO) "Tool for creating an MMC image with a platform specific U-Boot setup"
	$(ECHO)
	$(ECHO) "usage:"
	$(ECHO)
	$(ECHO) "  create_uboot <platform> [BUILD_DIR=<build-dir>]"
	$(ECHO)
	$(ECHO) "  <platform>   can be:"
	$(ECHO) "                  'hw_wand_quad'"
	$(ECHO) "                  'hw_usb_armory'"
	$(ECHO)
	$(ECHO) "  The image will be located at:"
	$(ECHO) "  $(UBOOT_BUILD_DIR)<platform>/$(shell basename $(MMC_IMG))"
	$(ECHO)

PLATFORMS_ARM := hw_wand_quad hw_usb_armory
PLATFORMS     := $(PLATFORMS_ARM)

# get targeted platform based on the make target
PLATFORM := $(firstword $(filter $(PLATFORMS),$(MAKECMDGOALS)))

# get Genode base directory based on the known location of this tool
GENODE_DIR := $(realpath $(dir $(firstword $(MAKEFILE_LIST)))/..)

UBOOT_URL           := https://github.com/m-stein/uboot
UBOOT_DIR           := $(GENODE_DIR)/contrib/uboot
UBOOT_TAG           := $(UBOOT_DIR)/.gitignore
UBOOT_BRANCH        := genode_$(PLATFORM)
UBOOT_BRANCH_TAG    := $(UBOOT_DIR)/$(UBOOT_BRANCH)
UBOOT_BUILD_ROOT    := $(UBOOT_DIR)/build
UBOOT_BUILD_DIR     := $(UBOOT_BUILD_ROOT)/$(PLATFORM)
UBOOT_BUILD_CONF    := $(UBOOT_BUILD_DIR)/.config
MMC_IMG             := $(UBOOT_BUILD_DIR)/mmc_img
MMC_IMG_SIZE        := 8M
SHELL               := bash
VERBOSE             := @
ECHO                := @echo -e
BRIGHT_COL          := \033[01;33m
DEFAULT_COL         := \033[0m

#
# Determine architecture based on the given platform
#
ifeq ($(filter-out $(PLATFORMS_ARM),$(PLATFORM)),)
ARCH := arm
endif

UBOOT_MAKE := \
	$(VERBOSE)make ARCH=$(ARCH) \
	               CROSS_COMPILE=/usr/local/genode-gcc/bin/genode-$(ARCH)- \
	               O=$(UBOOT_BUILD_DIR) \
	               -j4

#
# Platform settings
#

ifeq ($(PLATFORM), hw_wand_quad)
UBOOT_IMG     := $(UBOOT_BUILD_DIR)/u-boot.img
UBOOT_IMG_SPL := $(UBOOT_BUILD_DIR)/SPL
UBOOT_IMGS    := $(UBOOT_IMG) $(UBOOT_IMG_SPL)
UBOOT_CONF    := wandboard_config
FINISH_IMAGE  := \
	dd if=$(UBOOT_IMG_SPL) of=$(MMC_IMG) bs=1k seek=0  conv=notrunc,fsync; \
	dd if=$(UBOOT_IMG)     of=$(MMC_IMG) bs=1k seek=68 conv=notrunc,fsync;
endif

ifeq ($(PLATFORM), hw_usb_armory)
UBOOT_IMG     := $(UBOOT_BUILD_DIR)/u-boot.imx
UBOOT_IMGS    := $(UBOOT_IMG)
UBOOT_CONF    := usbarmory_config
FINISH_IMAGE  := dd if=$(UBOOT_IMG) of=$(MMC_IMG) bs=512 seek=0 conv=fsync
endif

#
# Generic rules for the creation of the image
#

$(PLATFORM): $(MMC_IMG)

$(MMC_IMG): $(UBOOT_IMGS)
	$(ECHO) "$(BRIGHT_COL)Composing MMC image...$(DEFAULT_COL)"
	$(VERBOSE)dd if=/dev/zero of=$(MMC_IMG) bs=$(MMC_IMG_SIZE) seek=1 count=0
	$(VERBOSE)$(FINISH_IMAGE)
	$(ECHO)
	$(ECHO) "Successfully created MMC image."
	$(ECHO) "You can install the image on an empty MMC for example via:"
	$(ECHO) "sudo dd if=$(MMC_IMG) of=/dev/<YOUR_MMC> bs=1K seek=1 conv=fsync"
	$(ECHO)

$(UBOOT_IMGS): $(UBOOT_BUILD_CONF) $(UBOOT_DIR)
	$(ECHO) "$(BRIGHT_COL)Building U-Boot images...$(DEFAULT_COL)"
	$(UBOOT_MAKE) -C $(UBOOT_BUILD_DIR)

$(UBOOT_BUILD_CONF): $(UBOOT_BRANCH_TAG)
	$(ECHO) "$(BRIGHT_COL)Building U-Boot config...$(DEFAULT_COL)"
	$(VERBOSE)mkdir -p $(UBOOT_BUILD_DIR)
	$(UBOOT_MAKE) -C $(UBOOT_DIR) $(UBOOT_CONF)

$(UBOOT_BRANCH_TAG): $(UBOOT_TAG)
	$(VERBOSE)git -C $(UBOOT_DIR) checkout $(UBOOT_BRANCH)

$(UBOOT_TAG):
	$(ECHO) "$(BRIGHT_COL)Downloading U-Boot sources...$(DEFAULT_COL)"
	$(VERBOSE)git clone $(UBOOT_URL) $(UBOOT_DIR)

.PHONY: $(PLATFORM)

#
# Clean-up rules
#

clean:
	rm -rf $(UBOOT_BUILD_ROOT)

cleanall:
	rm -rf $(UBOOT_DIR)