kernel_image=/boot/ostree/torizon-7ec7f2c064358dc7816014bf42f36b8724c880cc9b87e38eecc34c0b171611a8/vmlinuz-6.6.142-7.7.0
ramdisk_image=/boot/ostree/torizon-7ec7f2c064358dc7816014bf42f36b8724c880cc9b87e38eecc34c0b171611a8/initramfs-6.6.142-7.7.0.img
fdtdir=/boot/ostree/torizon-7ec7f2c064358dc7816014bf42f36b8724c880cc9b87e38eecc34c0b171611a8/dtb
bootargs=quiet logo.nologo vt.global_cursor_default=0 plymouth.ignore-serial-consoles splash fbcon=map:3 ostree=/ostree/boot.1/torizon/7ec7f2c064358dc7816014bf42f36b8724c880cc9b87e38eecc34c0b171611a8/0
kernel_image_type=Image.gz
overlays_file="overlays.txt"
otaroot=1
fitconf_fdt_overlays=""

set_bootargs=env set bootcmd_args env set bootargs ${defargs} root=LABEL=otaroot rootfstype=ext4 ${bootargs} ${tdxargs}

set_kernel_load_addr=env set bootcmd_unzip_k ";"; \
                     if test ${kernel_image_type} = "fitImage"; then \
                         env set fitimage_addr_r ${ramdisk_addr_r}; \
                         env set kernel_addr_load ${fitimage_addr_r}; \
                     else \
                         if test -n ${kernel_comp_addr_r}; then \
                             env set kernel_addr_load ${loadaddr}; \
                         elif test ${kernel_image_type} = "Image.gz"; then \
                             env set kernel_addr_load ${loadaddr}; \
                             env set bootcmd_unzip_k "unzip $kernel_addr_load $kernel_addr_r"; \
                         else \
                             env set kernel_addr_load ${kernel_addr_r}; \
                         fi; \
                     fi

load_overlay=load ${devtype} ${devnum}:${otaroot} ${loadaddr} /boot${fdtdir}/${overlays_file}; env import -t ${loadaddr} ${filesize}
apply_overlays=if test ${kernel_image_type} = "fitImage"; then \
                   env set fdt_high; \
                   env set fdt_resize true; \
                   for overlay_file in ${fdt_overlays}; do \
                       echo "Applying Overlay: ${overlay_file}"; \
                       env set fitconf_fdt_overlays "${fitconf_fdt_overlays}"#conf-"${overlay_file}"; \
                   done; \
               else \
                   fdt addr ${fdt_addr_r} && fdt resize 0x20000 && \
                   fdt mknode /chosen overlays && \
                   for overlay_file in ${fdt_overlays}; do \
                       echo "Applying Overlay: ${overlay_file}" && \
                       load ${devtype} ${devnum}:${otaroot} ${loadaddr} /boot${fdtdir}/overlays/${overlay_file} && fdt apply ${loadaddr}; \
                       fdt set /chosen/overlays ${overlay_file} $?; \
                   done; \
               fi

bootcmd_load_k=load ${devtype} ${devnum}:${otaroot} ${kernel_addr_load} "/boot"${kernel_image}
bootcmd_load_r=if test ${kernel_image_type} != "fitImage"; then \
                   load ${devtype} ${devnum}:${otaroot} ${ramdisk_addr_r} "/boot"${ramdisk_image}; env set ramdisk_size ${filesize}; \
               fi || true

check_rollback_needed=if test -n "${kernel_image2}" && test "${rollback}" = "1"; then \
    echo "Rollback enabled. Booting previously deployed version."; \
    env set kernel_image ${kernel_image2}; \
    env set ramdisk_image ${ramdisk_image2}; \
    env set bootargs ${bootargs2}; \
    env set fdtdir ${fdtdir2}; \
    env set fdt_file ${fdt_file2}; \
    env set fdtfile ${fdtfile2}; \
    env set torizon_bootargs_l ${torizon_bootargs_l2}; \
    env set torizon_bootargs_r ${torizon_bootargs_r2}; \
    test -n "${kernel_image_type2}" && env set kernel_image_type ${kernel_image_type2}; \
fi || true

set_fdt_path=if test -n "${fdtdir}"; then \
                env set fdt_path /boot${fdtdir}/${fdtfile}; \
             else \
                env set fdt_path /boot${fdt_file}; \
             fi || true

bootcmd_dtb=if test ${kernel_image_type} != "fitImage"; then \
                load ${devtype} ${devnum}:${otaroot} ${fdt_addr_r} ${fdt_path}; \
            fi || true; \
            if test ${skip_fdt_overlays} != 1; then \
                    if test -e ${devtype} ${devnum}:${otaroot} /boot${fdtdir}/${overlays_file}; then \
                        run load_overlay && run apply_overlays || true; \
                    fi || true; \
            fi || true

set_bootargs_custom=if test -n "${fdt_overlays}" && test -z "${torizon_bootargs_l}${torizon_bootargs_r}"; then \
                        for overlay_file in ${fdt_overlays}; do \
                            if test "${overlay_file}" = "custom-kargs_overlay.dtbo"; then \
                                if fdt get value custom_kargs /chosen/ bootargs_custom; then \
                                    env set bootargs ${bootargs} ${custom_kargs}; \
                                fi; \
                            fi; \
                        done; \
                    fi || true

set_bootargs_custom2=if test -n "${torizon_bootargs_l}${torizon_bootargs_r}"; then \
                         env set bootargs ${torizon_bootargs_l} ${bootargs} ${torizon_bootargs_r}; \
                     fi || true

board_fixups=if test "${board}" = "verdin-imx8mm"; then \
                 if test "${fdtfile}" = "imx8mm-verdin-nonwifi-v1.1-dahlia.dtb"; then \
                     env set fdtfile imx8mm-verdin-nonwifi-dahlia.dtb; \
                 elif test "${fdtfile}" = "imx8mm-verdin-nonwifi-v1.1-dev.dtb"; then \
                     env set fdtfile imx8mm-verdin-nonwifi-dev.dtb; \
                 elif test "${fdtfile}" = "imx8mm-verdin-wifi-v1.1-dahlia.dtb"; then \
                     env set fdtfile imx8mm-verdin-wifi-dahlia.dtb; \
                 elif test "${fdtfile}" = "imx8mm-verdin-wifi-v1.1-dev.dtb"; then \
                     env set fdtfile imx8mm-verdin-wifi-dev.dtb; \
                 fi; \
             fi || true

bootcmd_boot=if test ${bootscript_debug} != 1; then \
                 if test ${kernel_image_type} = "fitImage"; then \
                     if test -n "${fdtdir}"; then \
                         bootm ${fitimage_addr_r}#conf-freescale_${fdtfile}${fitconf_fdt_overlays} ; \
                     else \
                         bootm ${fitimage_addr_r}#conf-freescale_${fdt_file}${fitconf_fdt_overlays} ; \
                     fi; \
                 else \
                     booti ${kernel_addr_r} ${ramdisk_addr_r}:${ramdisk_size} ${fdt_addr_r}; \
                 fi; \
             fi || true

prog_fuses=true
read_fuses=true
set_vars=true
reset_dev=true

uenv_extra_configs=true

bootcmd_run=run prog_fuses && run read_fuses && run set_vars && run reset_dev && \
            run board_fixups && run check_rollback_needed && run uenv_extra_configs && run set_bootargs && run set_fdt_path && \
            run bootcmd_dtb && run bootcmd_args && run set_bootargs_custom && run set_bootargs_custom2 && \
            run set_kernel_load_addr && run bootcmd_load_k && run bootcmd_unzip_k && run bootcmd_load_r && run bootcmd_boot

