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

# As the first step in the boot script, check if we are using DualBoot. 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 (only on EMMC)
if test "${mmcbootdev}" = "${emmc_dev}" && test "${dualboot}" = "yes"; then
	if test "${active_system}" = "linux_a"; then
		echo "Booting from system A ${bootattempt}"
		part number mmc ${mmcbootdev} linux_a tmp_mmcpart
		part number mmc ${mmcbootdev} rootfs_a tmp_rootfs_index
	else
		echo "Booting from system B ${bootattempt}"
		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
else
	echo "Booting system ${bootattempt}"
	# 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
		# SD boot (use SD specific rootfs partition uuid)
		setenv mmcroot PARTUUID=b74d3700-a3c2-4806-8cb6-4e0fed784c7f
	fi
fi


# Back up environment variables
setenv ORIG_overlays ${overlays}
setenv ORIG_extra_bootargs ${extra_bootargs}

# Set SOC type to "imx8mn" if not already defined by U-Boot
if test ! -n "${soc_type}"; then
	setenv soc_type "imx8mn"
fi

#
# Determine overlays to apply depending on the hardware capabilities
# described by the HWID, SOM version, and carrier board version.
#
if test -n "${module_ram}"; then
	setexpr som_hv ${hwid_2} \& 38
	setexpr som_hv ${som_hv} / 8

	setexpr module_has_wifi ${hwid_2} \& 10000
	setexpr module_has_wifi ${module_has_wifi} / 10000
	setexpr module_has_bt ${hwid_2} \& 20000
	setexpr module_has_bt ${module_has_bt} / 20000

	if test "${module_has_bt}" = "1" && test -z "${disable_bt}"; then
		setenv overlays ccimx8m_bt.dtbo,${overlays}
	fi

	if test "${module_has_wifi}" = "1" && test -z "${disable_wifi}"; then
		setenv overlays ccimx8m_wifi.dtbo,${overlays}
	fi

	if test "${som_hv}" = "1"; then
		setenv overlays ccimx8mn_v1.dtbo,${overlays}
	fi
fi

# Apply DVK v1-v2 overlay if the board version is < 3
if test "${board_version}" -lt "3"; then
	setenv overlays ccimx8mn-dvk_v1-v2.dtbo,${overlays}
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 overlays ${ORIG_overlays}
setenv ORIG_overlays
setenv extra_bootargs ${ORIG_extra_bootargs}
setenv ORIG_extra_bootargs
