// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
/*
 * Copyright (c) 2024-2026 TQ-Systems GmbH <u-boot@ew.tq-group.com>,
 * D-82229 Seefeld, Germany.
 * Author: Max Merchel
 *
 * shared ubi environment for TQ boards
 */

addubi=
	setenv bootargs "${bootargs}" rootfstype=ubifs ubi.mtd="${ubimtdname}"
        root=ubi0:"${ubirootfsvol}" "${rootfsmode}" rootwait;

load_spi=
	if sf probe; then
                if ubi part "${ubirootfspart}"; then
			if ubifsmount ubi0:"${ubirootfsvol}"; then
				ubifsload "${kernel_addr_r}" /boot/"${image}";
				ubifsload "${fdt_addr_r}" /boot/"${fdtfile}";
                                fdt address "${fdt_addr_r}";
				fdt resize 0x100000;
				for overlay in "${fdt_overlays}"; do
					ubifsload "${fdtoverlay_addr_r}"
					/boot/"${overlay}" &&
					fdt apply "${fdtoverlay_addr_r}";
				done;
				ubifsumount;
			fi;
			ubi detach;
		fi;
	fi

ubiargs=run addubi addtty

ubiboot=
	echo "Booting from UBI ...";
	setenv bootargs;
	run ubiargs;
	if run load_spi; then
		run boot_os;
	else
		echo "ERROR: loading kernel";
	fi;

ubimtdname=mtdname
ubirootfspart=ubi
ubirootfsvol=root
