#!/bin/sh
#
# Boot script generator for U-Boot
#
# This software is a part of ISAR.
# Copyright (c) Siemens AG, 2018
#
# SPDX-License-Identifier: MIT

set -e

if [ -f /etc/default/u-boot-script ]; then
	. /etc/default/u-boot-script
fi

BOOT_CMD=$(mktemp)

KERNEL_VERSION=$(find /boot -name vmlinu[xz]-* | sed 's|/boot/vmlinu[xz]-||g' |
		 linux-version sort --reverse | head -1)

case $(uname -m) in
	aarch64|mips|riscv64)
		KERNEL_FILE="vmlinux"
		BOOT="booti"
		;;
	*)
		KERNEL_FILE="vmlinuz"
		BOOT="bootz"
		;;
esac

echo "${SCRIPT_PREPEND}" >> ${BOOT_CMD}

echo "setenv bootargs ${KERNEL_ARGS}" >> ${BOOT_CMD}

echo "echo Loading /usr/lib/linux-image-${KERNEL_VERSION}/\${fdtfile}..." \
     >> ${BOOT_CMD}
echo "load \${devtype} \${devnum}:${ROOT_PARTITION} \${fdt_addr_r}" \
     "/usr/lib/linux-image-${KERNEL_VERSION}/\${fdtfile}" >> ${BOOT_CMD}
echo "echo Loading /boot/${KERNEL_FILE}-${KERNEL_VERSION}..." >> ${BOOT_CMD}
echo "load \${devtype} \${devnum}:\${distro_bootpart} \${kernel_addr_r}" \
     "/boot/${KERNEL_FILE}-${KERNEL_VERSION}" >> ${BOOT_CMD}

case "${NO_INITRD}" in
yes|1)
	INITRD_ADDR="-"
	;;
*)
	echo "echo Loading /boot/initrd.img-${KERNEL_VERSION}..." >> ${BOOT_CMD}
	echo "load \${devtype} \${devnum}:\${distro_bootpart}" \
	     "\${ramdisk_addr_r} /boot/initrd.img-${KERNEL_VERSION}" \
	     >> ${BOOT_CMD}
	INITRD_ADDR="\${ramdisk_addr_r}:\${filesize}"
esac

if [ -n "${OVERLAYS}" ]; then
	echo "fdt addr \${fdt_addr_r}" >> ${BOOT_CMD}
	# grant 1 MB to combined device tree
	echo "fdt resize 0x100000" >> ${BOOT_CMD}
	echo "setexpr overlay_addr_r \${fdt_addr_r} + 0x100000" >> ${BOOT_CMD}
	for OVERLAY in ${OVERLAYS}; do
		if ! echo $OVERLAY | grep -q "^/"; then
			OVERLAY_PATH=/usr/lib/linux-image-${KERNEL_VERSION}/
		fi
		echo "echo Loading ${OVERLAY_PATH}${OVERLAY}..." >> ${BOOT_CMD}
		echo "load \${devtype} \${devnum}:${ROOT_PARTITION}" \
		     "\${overlay_addr_r} ${OVERLAY_PATH}${OVERLAY}" \
		     >> ${BOOT_CMD}
		echo "fdt apply \${overlay_addr_r}" >> ${BOOT_CMD}
	done
fi

echo "${BOOT} \${kernel_addr_r} ${INITRD_ADDR} \${fdt_addr_r}" >> ${BOOT_CMD}

mkimage -T script -A invalid -C none -d ${BOOT_CMD} /boot/boot.scr > /dev/null

rm ${BOOT_CMD}
