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

cat << EOF

# begin boot sequence - sanity checks
setenv bootcmd 'if test -n \$u_k_addr ;then if test -n \$u_ramfs_addr ;then if test -n \$skip_dtb||test -n \$u_dtb&&test -n \$u_dtb_addr;then run l_init ;else run sanity_nodtb ;fi ;else run sanity_noaddr ;fi ;else run sanity_noaddr ;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 'if test \$u_boot = bootz; then u_pfx=vmlinuz; u_rpfx=uInitrd; else u_pfx=uImage; u_rpfx=uInitrd ;fi; run l_wrap; run failed'

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

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

# load initramfs if needed then/and jump to dtb
setenv n_initramfs 'if test -n \$have_i;then run n_dtb ;else if \${fses}load \${iodev} \${devpart} \${u_ramfs_addr} \${dir}\${u_rpfx}-\${u_kernel};then have_i=1;fi ;run n_dtb ;fi'

# load dtb if needed then/and boot
setenv n_dtb 'if test -n \$skip_dtb || test -n \$have_d;then run l_boot_trio ;else if test -n \$skip_dtb;then echo Skipping dtb;run l_boot_trio ;else if \${fses}load \${iodev} \${devpart} \${u_dtb_addr} \${dir}dtb-\${u_kernel}/\${u_dtb} ;then have_d=1;run l_boot_trio ;fi ; fi ;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 if test -n \$pass_dtb ;then run l_boot_dtb ;else run l_boot_nodtb ;fi ;else run l_boot_dtb ;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

