#!/bin/sh
#
# Copyright (C) 2016 Dream Property GmbH
#

source librecovery

compatible dm520 dm820 dm7080

usage()
{
	echo "Usage: ${0} [-hqtv] <ssbl.bin>"
	exit ${1}
}

xgetopts $@
shift $((${OPTIND} - 1))
[ "$#" -eq 1 ] || usage 1
FILENAME=`xrealpath ${1}`

! is_empty "${FILENAME}" || abort "No bootloader image given"
is_readable_file "${FILENAME}" || abort "Cannot access '${FILENAME}'"
is_file_size_le "${FILENAME}" "${SSBL_SIZE}" || abort "Boot image is too big"

case "${MACHINE}" in
	dm520)
		writespi ${FILENAME} ssbl || abort "Failed to write bootloader image"
		;;
	dm820|dm7080)
		is_writable_blockdev "${FLASH_DEVICE}" || abort "Target block device '${FLASH_DEVICE}' is not a writable block device"
		write_lba "${FILENAME}" "${FLASH_DEVICE}" 8192 || abort "Failed to flash boot image"
		;;
esac
