#!/bin/sh
# Install OpenWrt to the NAND of a CZ.NIC Turris 1.x.

. /lib/functions.sh
. /lib/functions/turris1x.sh

IMAGE="$1"
# no root=, the kernel finds the ubiblock of the rootfs volume
BOOTARGSUBI="ubi.mtd=nand_ubi,2048 rootwait console=ttyS0,115200"
# rootfs is the name in U-Boot's own partition table
UBIBOOT="atsha204_mac_read; max6370_wdt_off; setenv bootargs \$bootargsubi; ubi part rootfs 2048; ubi read \$nandbootaddr kernel; bootm \$nandbootaddr"
# a factory reset wipes the microSD boot commands, these bring them back
BOOTARGSMMC="root=/dev/mmcblk0p2 rootfstype=squashfs rootwait console=ttyS0,115200"
MMCBOOT="atsha204_mac_read; max6370_wdt_off; setenv bootargs \$bootargsmmc; bootm \$nandbootaddr"
BOOTCMD="mw ffee0060 40000000; setexpr.b reflash *0xFFA0001F;if test \$reflash -ge \$reflash_timeout; then echo BOOT NOR; run norboot; else echo BOOT NAND; mmc rescan; if fatload mmc 0:1 \$nandbootaddr zImage; then run mmcboot; else run ubiboot; fi; fi"
# the Turris OS factory defaults
BOOTARGSUBI_TOS="root=ubi0:rootfs rootfstype=ubifs ubi.mtd=9,2048 rootflags=chk_data_crc rw console=ttyS0,115200"
UBIBOOT_TOS="max6370_wdt_off; setenv bootargs \$bootargsubi; ubi part rootfs; ubifsmount ubi0:rootfs; ubifsload \$nandfdtaddr /boot/fdt; ubifsload \$nandbootaddr /boot/zImage; bootm \$nandbootaddr - \$nandfdtaddr"

[ "$(board_name)" = "cznic,turris1x" ] || {
	echo "This tool only runs on a CZ.NIC Turris 1.x." >&2
	exit 1
}

[ -n "$IMAGE" ] && [ -f "$IMAGE" ] || {
	echo "Usage: turris1x-nand-install <factory-nand.ubi>" >&2
	exit 1
}

if turris1x_boots_from_nand; then
	echo "The system is running from the NAND right now." >&2
	echo "Boot OpenWrt from a microSD card and run the installation there." >&2
	exit 1
fi

[ "$(dd if="$IMAGE" bs=4 count=1 2>/dev/null)" = "UBI#" ] || {
	echo "$IMAGE is not a UBI image." >&2
	exit 1
}

mtd=$(find_mtd_index nand_ubi)
[ -n "$mtd" ] || {
	echo "NAND partition \"nand_ubi\" not found in /proc/mtd." >&2
	exit 1
}

# a bad CRC makes fw_printenv fall back to an env without nandbootaddr
fw_printenv -n nandbootaddr >/dev/null 2>&1 || {
	echo "fw_printenv found no factory U-Boot environment." >&2
	echo "Refusing to touch the NAND." >&2
	exit 1
}

echo "This will erase the NAND flash (/dev/mtd$mtd) including any Turris OS"
echo "installed there. On the NOR flash only the U-Boot environment is"
echo "changed: holding the RESET button for 6 seconds still restores the"
echo "factory Turris OS to the NAND."
printf "Continue? [y/N] "
read -r answer
case "$answer" in
[yY]*) ;;
*)
	echo "Aborted."
	exit 1
	;;
esac

ubidetach -m "$mtd" 2>/dev/null
ubiformat "/dev/mtd$mtd" -f "$IMAGE" -y -O 2048 || exit 1

fw_setenv -s - <<-EOF || {
	ubiboot $UBIBOOT
	bootargsubi $BOOTARGSUBI
EOF
	echo "fw_setenv failed - the NAND cannot be booted until you run:" >&2
	echo "  fw_setenv ubiboot '$UBIBOOT'" >&2
	echo "  fw_setenv bootargsubi '$BOOTARGSUBI'" >&2
	exit 1
}

echo "Done. Power off, remove the microSD card and power on to boot OpenWrt"
echo "from the NAND."
echo "To return to Turris OS later, hold RESET for 6 seconds to restore it"
echo "from the NOR factory partition. That also restores the factory U-Boot"
echo "environment, so the Turris OS boot commands come back on their own:"
echo "  bootargsubi '$BOOTARGSUBI_TOS'"
echo "  ubiboot '$UBIBOOT_TOS'"
echo "What the factory environment does not have is a microSD branch. To"
echo "boot a card again after such a reset:"
echo "  fw_setenv bootargsmmc '$BOOTARGSMMC'"
echo "  fw_setenv mmcboot '$MMCBOOT'"
echo "  fw_setenv bootcmd '$BOOTCMD'"
