#!/bin/bash

# IKCONFIG=y, IKCONFIG_PROC=y

EXEC_PATH=/root/mtd-utils
ID="0xec,0xa1,0x00,0x15" # 128M 128KB 2KB

mtd_num=0
ubi_num=0
old_image=/root/image
old_image_fm=/root/image_fm
log_a="NA"
log_b="NA"

cleanup_handler()
{
	$EXEC_PATH/ubidetach -m$mtd_num 2>&1 > /dev/null
	modprobe -r ubi
	modprobe -r nandsim
}

trap 'cleanup_handler' HUP PIPE INT QUIT TERM

config_fm=0
if [[ "`zcat /proc/config.gz | grep FASTMAP`" =~ "CONFIG_MTD_UBI_FASTMAP=y" ]]
then
	config_fm=1
fi

cleanup_handler
modprobe nandsim id_bytes=$ID
if [[ "$1" == "1" ]]
then
	if [[ ! -f $old_image_fm ]]
	then
		if [[ $config_fm == 1 ]]
		then
			$EXEC_PATH/flash_erase /dev/mtd$mtd_num 0 0
			modprobe ubi fm_autoconvert
			$EXEC_PATH/ubiattach -m$mtd_num
			$EXEC_PATH/ubidetach -m$mtd_num
			dd if=/dev/mtd$mtd_num of=$old_image_fm
			modprobe -r ubi
		fi
	fi

	if [[ ! -f $old_image ]]
	then
		$EXEC_PATH/flash_erase /dev/mtd$mtd_num 0 0
		modprobe ubi
		$EXEC_PATH/ubiattach -m$mtd_num
		$EXEC_PATH/ubidetach -m$mtd_num
		dd if=/dev/mtd$mtd_num of=$old_image
		modprobe -r ubi
	fi

	exit 0
fi

print_config()
{
	if [[ $config_fm == 0 ]]
	then
		echo " CONFIG_MTD_UBI_FASTMAP=n"
	else
		echo " CONFIG_MTD_UBI_FASTMAP=y"
	fi
}

run_case()
{
	local module_parm=""
	local fm_enable=$1
	local opt_attach=$2
	local image_path=$3

	if [[ "$fm_enable" == "1" ]]
	then
		module_parm="fm_autoconvert"
	fi

	if [[ "$opt_attach" == "f" ]]
	then
		opt_attach="-f"
	else
		opt_attach=""
	fi

	dmesg -c > /dev/null

	$EXEC_PATH/flash_erase /dev/mtd$mtd_num 0 0 > /dev/null
	modprobe ubi $module_parm
	dd if=$image_path of=/dev/mtd$mtd_num > /dev/null
	$EXEC_PATH/ubiattach $opt_attach -m$mtd_num > /dev/null
	log=`dmesg -c | grep -o "attached by fastmap"`
	if [[ ! "$log_a" =~ "$log" ]]
	then
		echo "    ubiattach $opt_attach FAIL A($log_a vs $log)"
		exit 0
	fi
	$EXEC_PATH/ubidetach -m$mtd_num
	dmesg -c > /dev/null
	$EXEC_PATH/ubiattach -m$mtd_num > /dev/null
	log=`dmesg -c | grep -o "attached by fastmap"`
	if [[ ! "$log_b" =~ "$log" ]]
	then
		echo "    ubiattach $opt_attach FAIL B($log_b vs $log)"
		exit 0
	fi
	echo "    ubiattach $opt_attach SUCC"
	modprobe -r ubi
}

echo "Situation I. Attach UBI image with no existed old fastmap"
if [[ ! -f $old_image ]]
then
	echo "Cannot find $old_image"
	exit 0
fi
print_config
echo "  set fm_autoconvert"
log_a="NA"
log_b="NA"
if [[ $config_fm == 1 ]]
then
	log_b="attached by fastmap"
fi
run_case "1" "nf" $old_image
log_b="NA"
run_case "1" "f" $old_image
echo "  not set fm_autoconvert"
run_case "0" "nf" $old_image
run_case "0" "f" $old_image


echo "Situation II. Attach UBI image with existed old fastmap"
if [[ ! -f $old_image_fm ]]
then
	echo "Cannot find $old_image_fm"
	exit 0
fi
print_config
echo "  set fm_autoconvert"
log_a="NA"
log_b="NA"
if [[ $config_fm == 1 ]]
then
	log_a="attached by fastmap"
	log_b="attached by fastmap"
fi
run_case "1" "nf" $old_image
log_a="NA"
log_b="NA"
run_case "1" "f" $old_image
echo "  not set fm_autoconvert"
if [[ $config_fm == 1 ]]
then
	log_a="attached by fastmap"
	log_b="attached by fastmap"
fi
run_case "0" "nf" $old_image
log_a="NA"
log_b="NA"
run_case "0" "f" $old_image
