// 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 nfs environment for TQ boards
 */

addnfs=
	setenv bootargs "${bootargs}" root=/dev/nfs rw
        nfsroot="${serverip}":"${rootpath}",v3,tcp

load_nfs=
	nfs "${kernel_addr_r}" "${serverip}":"${rootpath}"/boot/"${image}";
	nfs "${fdt_addr_r}" "${serverip}":"${rootpath}"/boot/"${fdtfile}";
	fdt address "${fdt_addr_r}";
	fdt resize 0x100000;
	for overlay in "${fdt_overlays}"; do
		nfs "${fdtoverlay_addr_r}"
		        "${serverip}":"${rootpath}"/boot/"${overlay}" &&
			fdt apply "${fdtoverlay_addr_r}";
	done;

load_tftp=
	tftp "${kernel_addr_r}" "${image}";
	tftp "${fdt_addr_r}" "${fdtfile}";
	fdt address "${fdt_addr_r}";
	fdt resize 0x100000;
	for overlay in "${fdt_overlays}"; do
		if tftp "${fdtoverlay_addr_r}" "${overlay}"; then
			fdt apply "${fdtoverlay_addr_r}";
		else
			exit;
		fi;
	done;

netargs=run addnfs addip addtty

netloadcmd=load_tftp

nfsboot=
	echo "Booting from NFS ...";
	setenv bootargs;
	run netargs;
	run check_ipaddr;
	if run ${netloadcmd}; then
		run boot_os;
	else
		echo "ERROR: loading from NFS";
	fi;
