# boot-chain, pieces that follow 'boot' or 'run bootcmd'

cat << EOF

# begin boot sequence - sanity checks
setenv bootcmd run sanity sanity2 sanity3 l_init
setenv sanity if test -n \\\$u_k_addr \;then run true\;else run sanity_noaddr \;fi
setenv sanity2 if test -n \\\$u_ramfs_addr \;then run true\;else run sanity_noaddr \;fi
setenv sanity3 'if test -n \$skip_dtb||test -n \$u_dtb&&test -n \$u_dtb_addr;then run true ;else run sanity_nodtb ;fi'
setenv sanity_nodtb 'echo Missing load address or name of dtb; run showdefs; run failed'
setenv sanity_noaddr 'echo Missing load address for kernel or initramfs; run showdefs; run failed'

# basic sanity checks complete, set strings based on boot method
setenv l_init run l_init_u_boot l_wrap \; run failed
setenv l_init_u_boot if test \\\$u_boot = bootz\; then run l_init_u_bootz \; else run l_init_u_bootm \;fi
setenv l_init_u_bootz 'u_pfx=vmlinuz; u_rpfx=uInitrd'
setenv l_init_u_bootm 'u_pfx=uImage; u_rpfx=uInitrd'

# load kernel/ramfs/dtb using combinations of dir, iodev, fses and devpart
setenv l_wrap for dir in \\\$u_dir \; do run l_wrap2 \; done
setenv l_wrap2 for iodev in \\\$u_iodevs \; do run l_wrap3 \; done
setenv l_wrap3 for fses in \\\$u_fs \; do run l_wrap4 \; done 
setenv l_wrap4 for devpart in \\\$u_devpart \; do run n_kernel \; done

# load kernel if needed then/and jump to ramfs
setenv n_kernel if test -n \\\$have_k\;then run n_initramfs \;else run l_kernel \;fi
setenv l_kernel if \\\${fses}load \\\${iodev} \\\${devpart} \\\${u_k_addr} \\\${dir}\\\${u_pfx}-\\\${u_kernel} \;then have_k=1\;fi \;run n_initramfs

# load initramfs if needed then/and jump to dtb
setenv n_initramfs if test -n \\\$have_i\;then run n_dtb \;else run l_initramfs \;fi
setenv l_initramfs if \\\${fses}load \\\${iodev} \\\${devpart} \\\${u_ramfs_addr} \\\${dir}\\\${u_rpfx}-\\\${u_kernel}\;then have_i=1\;fi \;run n_dtb
setenv n_dtb if test -n \\\$skip_dtb \|\| test -n \\\$have_d\;then run l_boot_trio \;else run l_dtb \;fi
setenv l_dtb if test -n \\\$skip_dtb\;then echo Skipping dtb\;run l_boot_trio \;else run l_dtb2 \; fi
setenv l_dtb2 if \\\${fses}load \\\${iodev} \\\${devpart} \\\${u_dtb_addr} \\\${dir}dtb-\\\${u_kernel}/\\\${u_dtb} \;then have_d=1\;run l_boot_trio \;fi
setenv l_boot_trio if test -n \\\$have_k\&\& test -n \\\$have_i \&\&test -n \\\$have_d\;then run l_bootargs \;fi

setenv l_boot_trio if test -n \\\$have_k\&\& test -n \\\$have_i \;then run l_boot_trio_nodtb \;fi
setenv l_boot_trio_nodtb if test -n \\\$skip_dtb\; then run l_bootargs \;else run l_boot_trio_withdtb\; fi
setenv l_boot_trio_withdtb if test -n \\\$have_d\; then run l_bootargs \;else run echo Missing dtb\; fi

# construct final boot arguments
setenv l_bootargs 'run l_bootargs2; echo bootargs are \$bootargs; run l_boot'
setenv l_bootargs2 setenv bootargs '\$base_bootargs \$opt_bootargs root=\$u_root \$u_extraargs \$systemd_args'
setenv l_boot 'if test -n \$skip_dtb ;then run l_boot2 ;else run l_boot_dtb ;fi'
setenv l_boot2 'if test -n \$pass_dtb ;then run l_boot_dtb ;else run l_boot_nodtb ;fi'
setenv l_boot_dtb '\${u_boot} \$u_k_addr \$u_ramfs_addr \$u_dtb_addr'
setenv l_boot_nodtb '\${u_boot} \$u_k_addr \$u_ramfs_addr'

EOF

