# SPDX-License-Identifier: GPL-2.0+ OR MIT
#
# Copyright 2020 Toradex
#
# Toradex boot script.
#
# Allows to change boot and rootfs devices independently.
# Supports:
# - boot device type: boot_devtype := {mmc, usb, tftp, dhcp}
# - boot device num (for mmc, usb types): boot_devnum := {0 .. MAX_DEV_NUM}
# - boot partition (for mmc, usb types): boot_part := {1 .. MAX_PART_NUM}
# - root device type: boot_devtype := {mmc, usb}
# - root device num (for mmc, usb types): boot_devnum := {0 .. MAX_DEV_NUM}
# - root partition (for mmc, usb types): boot_part := {1 .. MAX_PART_NUM}
#
# Defaults:
#    root_devtype = boot_devtype = devtype
#    root_devnum = boot_devnum = devnum
#    boot_part = distro_bootpart
#    root_part = 2
#
# TODO:
#  - support NFS in root_devtype

if test ${devtype} = "ubi"; then
    echo "This script is not meant to distro boot from raw NAND flash."
    exit
fi

if test -n ${setup}; then
    run setup
else
    env set setupargs 'console=${console},${baudrate} console=tty1 consoleblank=0'
fi

test -n ${m4boot} || env set m4boot ';'
test -n ${fdtfile} || env set fdtfile ${fdt_file}
test -n ${boot_part} || env set boot_part ${distro_bootpart}
test -n ${root_part} || env set root_part 2
test -n ${boot_devnum} || env set boot_devnum ${devnum}
test -n ${root_devnum} || env set root_devnum ${devnum}
test -n ${kernel_image} || env set kernel_image @@KERNEL_IMAGETYPE@@
test -n ${boot_devtype} || env set boot_devtype ${devtype}
test -n ${root_devtype} || env set root_devtype ${devtype}
test -n ${overlays_file} || env set overlays_file "overlays.txt"

test ${boot_devtype} = "mmc" && env set load_cmd 'load ${boot_devtype} ${boot_devnum}:${boot_part}'
test ${boot_devtype} = "usb" && env set load_cmd 'load ${boot_devtype} ${boot_devnum}:${boot_part}'
test ${boot_devtype} = "tftp" && env set load_cmd 'tftp'
test ${boot_devtype} = "dhcp" && env set load_cmd 'dhcp'

if test ${kernel_image} = "Image.gz"
then
    env set kernel_addr_load ${loadaddr}
    env set bootcmd_unzip 'unzip ${kernel_addr_load} ${kernel_addr_r}'
else
    env set kernel_addr_load ${kernel_addr_r}
    env set bootcmd_unzip ';'
fi

# Set dynamic commands
env set set_bootcmd_kernel 'env set bootcmd_kernel "${load_cmd} \\${kernel_addr_load} \\${kernel_image}"'
env set set_bootcmd_dtb 'env set bootcmd_dtb "${load_cmd} \\${fdt_addr_r} \\${fdtfile}"'
env set set_load_overlays_file 'env set load_overlays_file "${load_cmd} \\${loadaddr} \\${overlays_file}; env import -t \\${loadaddr} \\${filesize}"'
env set set_apply_overlays 'env set apply_overlays "for overlay_file in \\${fdt_overlays}; do echo Applying Overlay: \\${overlay_file} && ${load_cmd} \\${loadaddr} \\${overlay_file} && fdt apply \\${loadaddr}; env set overlay_file; done; true"'

# Set static commands
env set uuid_set 'part uuid ${root_devtype} ${root_devnum}:${root_part} uuid'
env set emmcargs_set 'run uuid_set && env set rootfsargs root=PARTUUID=${uuid} ro rootwait'
env set bootcmd_args 'run emmcargs_set && env set bootargs ${defargs} ${rootfsargs} ${setupargs} ${vidargs} ${tdxargs}'
env set fdt_resize 'fdt addr ${fdt_addr_r} && fdt resize 0x20000'
env set bootcmd_overlays 'run load_overlays_file && run fdt_resize && run apply_overlays'
env set bootcmd_boot '@@KERNEL_BOOTCMD@@ ${kernel_addr_r} - ${fdt_addr_r}'
env set bootcmd_prepare 'run set_bootcmd_kernel; run set_bootcmd_dtb; run set_load_overlays_file; run set_apply_overlays'
env set bootcmd_run 'run m4boot; run bootcmd_dtb && run bootcmd_overlays && run bootcmd_args && run bootcmd_kernel && run bootcmd_unzip && run bootcmd_boot'

run bootcmd_prepare
run bootcmd_run

