setenv bootargs ''

a_script=0x10800000
a_zImage=0x10800000
a_fdt=0x13000000
a_ramdisk=0x13800000
a_initrd=0x13a00000

setenv initrd_high 0xffffffff
if itest.s x6SX == "x${cpu}" || itest.s x7D == "x${cpu}"; then
	a_script=0x80800000
	a_zImage=0x80800000
	a_fdt=0x83000000
	a_ramdisk=0x83800000
	a_initrd=0x83a00000
fi

if itest.s "x" == "x${board}" ; then
	echo "!!!! Error: Your u-boot is outdated. Please upgrade.";
	exit;
fi

if itest.s "x" == "x${dtbname}" ; then
	if itest.s x6SOLO == "x${cpu}" ; then
		dtbname=imx6dl-${board}.dtb;
	elif itest.s x6DL == "x${cpu}" ; then
		dtbname=imx6dl-${board}.dtb;
	elif itest.s x6QP == "x${cpu}" ; then
		dtbname=imx6qp-${board}.dtb;
	elif itest.s x6SX == "x${cpu}" ; then
		dtbname=imx6sx-${board}.dtb;
	elif itest.s x7D == "x${cpu}" ; then
		dtbname=imx7d-${board}.dtb;
	else
		dtbname=imx6q-${board}.dtb;
	fi
fi

if load ${dtype} ${disk}:1 ${a_script} uEnv.txt ; then
    env import -t ${a_script} ${filesize}
fi

if itest.s x == x${bootdir} ; then
	bootdir=/boot/
fi

if itest.s x${bootpart} == x ; then
	bootpart=1
fi

if load ${dtype} ${disk}:${bootpart} ${a_fdt} ${bootdir}${dtbname} ; then
	fdt addr ${a_fdt}
	setenv fdt_high 0xffffffff
else
	echo "!!!! Error loading ${bootdir}${dtbname}";
	exit;
fi

cmd_xxx_present=
fdt resize
if itest.s "x" != "x${cmd_custom}" ; then
	run cmd_custom
	cmd_xxx_present=1;
fi

if itest.s "x" != "x${cmd_hdmi}" ; then
	run cmd_hdmi
	cmd_xxx_present=1;
	if itest.s x == x${allow_noncea} ; then
		setenv bootargs ${bootargs} mxc_hdmi.only_cea=1;
		echo "only CEA modes allowed on HDMI port";
	else
		setenv bootargs ${bootargs} mxc_hdmi.only_cea=0;
		echo "non-CEA modes allowed on HDMI, audio may be affected";
	fi
fi

if itest.s "x" != "x${cmd_lcd}" ; then
	run cmd_lcd
	cmd_xxx_present=1;
fi
if itest.s "x" != "x${cmd_lvds}" ; then
	run cmd_lvds
	cmd_xxx_present=1;
fi
if itest.s "x" != "x${cmd_lvds2}" ; then
	run cmd_lvds2
	cmd_xxx_present=1;
fi

if itest.s "x" == "x${cmd_xxx_present}" ; then
	echo "!!!!!!!!!!!!!!!!"
	echo "warning: your u-boot may be outdated, please upgrade"
	echo "!!!!!!!!!!!!!!!!"
fi

setenv bootargs "${bootargs} console=${console},115200 vmalloc=400M consoleblank=0 rootwait fixrtc"


setexpr b0 ${bootpart} % 0x0a;
setexpr b1 ${bootpart} / 0x0a;
#this is to show a decimal number when really hex is output
setexpr bpart ${b1} * 0x10
setexpr bpart ${bpart} + ${b0};

if test "sata" = "${dtype}" ; then
	setenv bootargs "${bootargs} root=/dev/sda${bpart}" ;
elif test "usb" = "${dtype}" ; then
	setenv bootargs "${bootargs} root=/dev/sda${bpart}" ;
else
	setenv bootargs "${bootargs} root=/dev/mmcblk${disk}p${bpart}"
fi

if itest.s "x" != "x${disable_giga}" ; then
	setenv bootargs ${bootargs} fec.disable_giga=1
fi

if itest.s "x" != "x${wlmac}" ; then
	setenv bootargs ${bootargs} wlcore.mac=${wlmac}
fi

if itest.s "x" != "x${gpumem}" ; then
	setenv bootargs ${bootargs} galcore.contiguousSize=${gpumem}
fi

if itest.s "no" != "${dosplash}" ; then
	setenv bootargs ${bootargs} splash quiet plymouth.ignore-serial-consoles
fi

if itest.s "x" != "x${overlayfs}" ; then
	setenv bootargs ${bootargs} overlayfs=${overlayfs}
	if itest.s "x" != "x${ofs-size}" ; then
		setenv bootargs ${bootargs} ofs-size=${ofs-size}
	fi
fi

if itest.s "x" != "x${cma}" ; then
	setenv bootargs ${bootargs} cma=${cma}
fi

if itest.s "x" != "x${show_fdt}" ; then
	fdt print /
fi

if itest.s "x" != "x${show_env}" ; then
	printenv
fi

if kbd ; then
        if itest.s "xv" == "x${keybd}" ; then
                load ${dtype} ${disk}:${bootpart} ${a_zImage} ${bootdir}/uImage-recovery &&
                load ${dtype} ${disk}:${bootpart} ${a_ramdisk} ${bootdir}/uramdisk-recovery.img &&
                bootm ${a_zImage} ${a_ramdisk};
                echo "--- error launching recovery!"
                exit;
        fi
fi

echo "----------- trying to load /initrd.img";
if load ${dtype} ${disk}:${bootpart} ${a_initrd} /initrd.img ; then
	haverd=1;
	setenv initrd_size ${filesize}
else
	haverd=
fi

if itest.s x${haverd} == x ; then
	if load ${dtype} ${disk}:${bootpart} ${a_zImage} /vmlinuz ; then
		setenv bootargs ${bootargs} rw
		bootz ${a_zImage} - ${a_fdt}
	fi
else
	if load ${dtype} ${disk}:${bootpart} ${a_zImage} /vmlinuz ; then
		bootz ${a_zImage} ${a_initrd}:${initrd_size} ${a_fdt} ;
	fi
fi
echo "Error loading kernel image"
