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

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

#
# Set device tree filename depending on the board ID (if defined)
#
if test -n "${board_id}"; then
	setenv fdt_file cc${soc_type}-dvk-id${board_id}.dtb
else
	#
	# Set device tree filename depending on the hardware capabilities
	#
	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

		setenv fdt_file cc${soc_type}-dvk.dtb

		if test "${module_has_bt}" = "1"; then
			setenv overlays cc${soc_type}_ov_bt.dtbo,${overlays}
		fi

		if test "${module_has_wifi}" = "1"; then
			setenv overlays cc${soc_type}_ov_wifi.dtbo,${overlays}
		fi

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

# 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
dboot linux mmc ${mmcbootdev}:${mmcpart}
