#!/bin/sh
# Copyright (c) 2016-2019 Algo Communication Products Ltd.

# Write images into NAND flash

if [ "$#" != "1" ]; then
	cat <<EOF
Usage: $0 [ kernel | recovery | data ]
EOF
	exit 1
fi

PATH=/opt/algo/bin:$PATH
TARGET=$1
if [ "$TARGET" = "kernel" ]; then
	flash_eraseall /dev/mtd2
	nandwrite -p /dev/mtd2 /boot/uImage
elif [ "$TARGET" = "recovery" ]; then
	flash_eraseall /dev/mtd3
	nandwrite -p /dev/mtd3 /boot/nand/ubapp.bin

	flash_eraseall /dev/mtd4
	nandwrite -p /dev/mtd4 /boot/nand/rc-uImage

	flash_eraseall /dev/mtd5
	nandwrite -p /dev/mtd5 /boot/nand/rc-rootfs.img

	if [ -f /opt/algo/cache/recovery-stub ]; then
		fw_setenv algorc $(cat /opt/algo/cache/recovery-stub)
	fi
fi
