#!/bin/sh
set -e

dtmodel="/sys/firmware/devicetree/base/model"
if [ -z "$TARGET" ] && [ -f "${dtmodel}" ]; then
	case $(cat "${dtmodel}") in
		Pinebook) TARGET="/usr/lib/u-boot/pinebook" ;;
		Pine64+) TARGET="/usr/lib/u-boot/pine64_plus" ;;
		"Pine64 LTS") TARGET="/usr/lib/u-boot/pine64-lts" ;;
		"Olimex A64-Olinuxino") TARGET="/usr/lib/u-boot/a64-olinuxino/" ;;
		"Olimex A64-Olinuxino-eMMC") TARGET="/usr/lib/u-boot/a64-olinuxino-emmc" ;;
		"Olimex A64 Teres-I") TARGET="/usr/lib/u-boot/teres_i/" ;;
		"OrangePi Zero Plus2") TARGET="/usr/lib/u-boot/orangepi_zero_plus2/" ;;
		"FriendlyARM NanoPi NEO 2") TARGET="/usr/lib/u-boot/nanopi_neo2/" ;;
		"FriendlyARM NanoPi NEO Plus2") TARGET="/usr/lib/u-boot/nanopi_neo_plus2/" ;;
	esac
fi

atf="/usr/lib/arm-trusted-firmware/sun50i_a64/bl31.bin"
if [ -z "$BL31" ] && [ -f "${atf}" ]; then
	BL31="${atf}"
fi

TARGET=${TARGET:-"/usr/lib/u-boot/pine64_plus"}
BL31=${BL31:-"/usr/lib/atf/sun50iw1p1/bl31.bin"}
FIT_GENERATOR=${FIT_GENERATOR:-"mksunxi_fit_atf"}

case "$1" in
    -f|--force)
	FORCE=y
	shift;;
    -*)
	echo >&2 "$0: unknown option '$1'"
	exit 1;;
esac

if [ -z "$(which mkimage)" ]; then
	echo >&2 "$0: mkimage: command not found. Please install u-boot-tools."
	exit 1
fi

DEV="$1"
if [ -z "$DEV" ] || ! shift || [ -n "$*" ]; then
    echo >&2 "Usage: $0 /dev/your-sd-or-mmc-or-image"
    exit 1
fi

if [ ! -w "$DEV" ] && [ -z "$FORCE" ]; then
    echo >&2 "$0: device/image ($DEV) must be writable"
    exit 1
fi
DEV="$(readlink -f "$DEV")"
DIR="$(mktemp -d)"
trap 'rm -rf "$DIR"' 0
# Build tools get confused by paths, thus let's copy all the pieces into
# one dir.

if [ ! -w "$DEV" ] && [ -z "$FORCE" ]; then
    echo >&2 "$0: device/image ($DEV) not accessible via abs path?!?"
    exit 1
fi

cd "$DIR"
if [ -z "$FORCE" ]; then
    # A very simple sanity check.  GPT mandates a "protective MBR" so this works
    # even with GPT partitioning.
    printf '%b' '\0125\0252' >mbr-sign
    if ! cmp -s -i 0:510 -n 2 mbr-sign "$DEV"; then
       echo >&2 "$0: device/image ($DEV) has no MBR partition table"
       exit 1
    fi

    # But, on sunxi64, spl will trample upon GPT.
    printf "EFI PART" >gpt-sign
    if cmp -s -i 0:512 -n 8 gpt-sign "$DEV"; then
       echo >&2 "$0: device/image ($DEV) uses GPT partition table, unusable on sunxi64"
       exit 1
    fi
fi

cp -p $TARGET/*.dtb $TARGET/*.bin .
BL31=$BL31 \
    $FIT_GENERATOR *.dtb > u-boot.its
mkimage -f u-boot.its u-boot.itb
echo "Writing sunxi-spl"
dd conv=notrunc if=sunxi-spl.bin of="$DEV" bs=8k seek=1
echo "Writing u-boot FIT image"
dd conv=notrunc if=u-boot.itb of="$DEV" bs=8k seek=5
sync "$DEV"
