#
# U-Boot bootscript for EMMC/SD images created by Yocto.
#

# As the first step in the boot script, check if we are using DualBoot and
# if an upgrade is available. This requires the script to change some variables
# and save them, while the rest of the script changes variables only temporarily
# without saving them.

# Build boot attempt message string
if test "${bootlimit}" -gt 0 && test "${bootcount}" -gt 0; then
	bootattempt="(boot attempt ${bootcount}/${bootlimit})"
fi

# Dual boot update verification
if test "${dualboot}" = "yes"; then
	if test "${upgrade_available}" = "1"; then
		echo "Update detected; Booting new system in ${active_system} ${bootattempt}"
	else
		if test "${active_system}" = "linux_a"; then
			echo "Booting from system A"
			part number mmc ${mmcbootdev} linux_a tmp_mmcpart
			part number mmc ${mmcbootdev} rootfs_a tmp_rootfs_index
		else
			echo "Booting from system B"
			part number mmc ${mmcbootdev} linux_b tmp_mmcpart
			part number mmc ${mmcbootdev} rootfs_b tmp_rootfs_index
		fi
		part uuid mmc ${mmcbootdev}:${tmp_rootfs_index} tmp_mmcroot
		if test "${mmcpart}" -ne "${tmp_mmcpart}"; then
			setexpr mmcpart ${tmp_mmcpart}
			env_save_needed="1"
		fi
		if test "${mmcroot}" != "PARTUUID=${tmp_mmcroot}"; then
			setenv mmcroot PARTUUID=${tmp_mmcroot}
			env_save_needed="1"
		fi
		if test "${env_save_needed}" = "1"; then
			env delete tmp_mmcpart tmp_rootfs_index tmp_mmcroot
			env save
		fi
	fi
else
	# Get the UUID of the configured boot partition.
	part uuid mmc ${mmcbootdev}:${mmcpart} bootpart
	# Check the boot source.
	if test "${bootpart}" = "${part1_uuid}"; then
		# We are booting from the eMMC using 'linux'.
		true
	elif test "${bootpart}" = "${part2_uuid}"; then
		# We are booting from the eMMC using 'recovery'.
		setenv boot_initrd true
		setenv initrd_file uramdisk-recovery.img
	else
		# We are booting from the SD card.
		setenv mmcroot /dev/mmcblk${mmcbootdev}p2
	fi
	if test "${upgrade_available}" = "1"; then
		echo "Update detected; Booting new system ${bootattempt}"
	fi
fi


# Back up environment variables
setenv ORIG_extra_bootargs ${extra_bootargs}

#
# Set device tree filename depending on the board ID (if defined)
#
if test -n "${board_id}"; then
	setenv fdt_file ${soc_family}-ccimx6sbc-id${board_id}.dtb
else
	#
	# Set device tree filename depending on the hardware variant
	#
	if test "${module_variant}" = "0x02"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x03"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x04"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x05"; then
		setenv fdt_file imx6q-ccimx6sbc-w.dtb
	elif test "${module_variant}" = "0x06"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x08"; then
		setenv fdt_file imx6q-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x0a"; then
		setenv fdt_file imx6dl-ccimx6sbc-w.dtb
	elif test "${module_variant}" = "0x0b"; then
		setenv fdt_file imx6dl-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x0c"; then
		setenv fdt_file imx6dl-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x0e"; then
		setenv fdt_file imx6q-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x0f"; then
		setenv fdt_file imx6q-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x11"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x12"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x13"; then
		setenv fdt_file imx6dl-ccimx6sbc-wb.dtb
	elif test "${module_variant}" = "0x14"; then
		setenv fdt_file imx6q-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x15"; then
		setenv fdt_file imx6dl-ccimx6sbc.dtb
	elif test "${module_variant}" = "0x16"; then
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	else
		setenv fdt_file imx6q-ccimx6sbc-wb.dtb
	fi
fi

setenv extra_bootargs fbcon=logo-pos:center fbcon=logo-count:1 ${extra_bootargs}
dboot linux mmc ${mmcbootdev}:${mmcpart}



# We only get here in case of an error on the dboot command.

# Undo changes to environment variables
setenv extra_bootargs ${ORIG_extra_bootargs}
setenv ORIG_extra_bootargs
