#
# flash-kernel: bootscr.uboot-generic
#

# Bootscript using the new unified bootcmd handling
# introduced with u-boot v2014.10
#
# Expects to be called with the following environment variables set:
#
#  devtype              e.g. mmc/scsi etc
#  devnum               The device number of the given type
#  bootpart             The partition containing the boot files
#  distro_bootpart      The partition containing the boot files
#                       (introduced in u-boot mainline 2016.01)
#  prefix               Prefix within the boot partiion to the boot files
#  kernel_addr_r        Address to load the kernel to
#  fdt_addr_r           Address to load the FDT to
#  ramdisk_addr_r       Address to load the initrd to.
#
# The uboot must support the booti and generic filesystem load commands.

# Workaround lack of baudrate included with console on various iMX
# systems (e.g. wandboard, cubox-i, hummingboard)
if test "${console}" = "ttymxc0" && test -n "${baudrate}"; then
  setenv console "${console},${baudrate}"
fi

if test -n "${console}"; then
  setenv bootargs "${bootargs} console=${console}"
fi

setenv bootargs "ro no_console_suspend cma=512M pci=nomsi ${bootargs} console=ttymxc0,115200 console=tty1"

# Default bootargs for Linux are set here and can be overwritten using
# LINUX_KERNEL_CMDLINE in /etc/default/flash-kernel or by setting them in
# u-boot itself via the setenv command.
setenv bootargs "ro no_console_suspend cma=512M@3G cryptomgr.notests ${bootargs}"

# Board-specific bootargs cannot be overwritten by above methods. If you
# really need to overwrite below lines, create
# /etc/flash-kernel/ubootenv.d/00reform2_ubootenv and fill it with what you
# would like to replace the contents of this file with.
if test "${board}" = "nitrogen8m_som"; then
  setenv bootargs "${bootargs} console=ttymxc0,115200 pci=nomsi"
elif test "${board}" = "MNT Reform 2.0"; then
  # some imx8mq Reforms have this board value
  setenv bootargs "${bootargs} console=ttymxc0,115200 pci=nomsi"
elif test "${board}" = "u200"; then
  # amlogic internal name for a311d dev board
  setenv bootargs "${bootargs} console=ttyAML0,115200 pci=pcie_bus_perf libata.force=noncq nvme_core.default_ps_max_latency_us=0"
elif test "${board}" = "ls1028a"; then
  setenv bootargs "${bootargs} console=ttyS0,115200 iommu.passthrough=1 arm-smmu.disable_bypass=0"
fi

# console=tty1 needs to go at the end of the kernel cmdline because the luks
# passphrase prompt will show up on the last console which must not be the
# serial terminal but the tty displayed to the screen
setenv bootargs "${bootargs} console=tty1"

if test -z "${fk_kvers}"; then
   setenv fk_kvers '6.5.0-1-reform2-arm64'
fi

# These two blocks should be the same apart from the use of
# ${fk_kvers} in the first, the syntax supported by u-boot does not
# lend itself to removing this duplication.

if test -n "${fdtfile}"; then
   setenv fdtpath dtbs/${fk_kvers}/${fdtfile}
else
   setenv fdtpath dtb-${fk_kvers}
fi

if test -z "${distro_bootpart}"; then
  setenv partition ${bootpart}
else
  setenv partition ${distro_bootpart}
fi


# place here any u-boot commands to be executed before boot

if test -z "${fk_image_locations}"; then
  setenv fk_image_locations ${prefix}
fi

for pathprefix in ${fk_image_locations}
do
  if test -e ${devtype} ${devnum}:${partition} ${pathprefix}vmlinuz-${fk_kvers}
  then
    load ${devtype} ${devnum}:${partition} ${kernel_addr_r} ${pathprefix}vmlinuz-${fk_kvers} \
    && load ${devtype} ${devnum}:${partition} ${fdt_addr_r} ${pathprefix}${fdtpath} \
    && load ${devtype} ${devnum}:${partition} ${ramdisk_addr_r} ${pathprefix}initrd.img-${fk_kvers} \
    && echo "Booting Debian ${fk_kvers} from ${devtype} ${devnum}:${partition}..." \
    && booti ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr_r}
  fi
done

for pathprefix in ${fk_image_locations}
do
  if test -e ${devtype} ${devnum}:${partition} ${pathprefix}vmlinuz
  then
    load ${devtype} ${devnum}:${partition} ${kernel_addr_r} ${pathprefix}vmlinuz \
    && load ${devtype} ${devnum}:${partition} ${fdt_addr_r} ${pathprefix}dtb \
    && load ${devtype} ${devnum}:${partition} ${ramdisk_addr_r} ${pathprefix}initrd.img \
    && echo "Booting Debian from ${devtype} ${devnum}:${partition}..." \
    && booti ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr_r}
  fi
done