#!/bin/sh

HELP="mac_ctrl [read/write] [value(when writed)]"

MODE=$1
#VALUE=`echo $get_mac | awk 'BEGIN{FS="[ ]";OFS=":"}{print $1,$2,$3,$4,$5,$6}' `
VALUE=$2:$3:$4:$5:$6:$7
echo $VALUE
b1=0
b2=4096
b3=8192

MTD_BLOCK=/dev/mtdblock4

if [ "$MODE" = "read" ] ; then
	echo "read mode" > /dev/null
else
	if [ "$MODE" = "write" ] ; then	
		echo "write mode" > /dev/null
		if [ ! -n "VALUE" ] ; then
				echo $HELP
				exit -1
		fi
	else
		echo $HELP
		exit -1
	fi
fi

if [ "$MODE" = "read" ] ; then   
	i=1
	while [ "$i" -le "3" ] ; do
		if [ "$i" = "1" ] ; then 
			skip="$b1" 
		else
			if [ "$i" = "2" ] ; then 
				skip="$b2" 
			else
				skip="$b3" 
			fi
		fi
		MAC=`dd if="$MTD_BLOCK" bs=32  skip=$skip count=1 |  awk 'NR==1'`
		if [ "$?" = "0" ] ; then
			#echo "$MAC" 
			ifconfig eth0 down           
			ifconfig eth0 hw ether "$MAC"
			if [ "$?" = "0" ] ; then
				break
			fi
		fi

		i=`expr "$i" + 1`
	done 
else
	echo  "$VALUE" | dd of="$MTD_BLOCK" bs=32  seek=$b1 count=1
	echo  "$VALUE" | dd of="$MTD_BLOCK" bs=32  seek=$b2 count=1
	echo  "$VALUE" | dd of="$MTD_BLOCK" bs=32  seek=$b3 count=1
fi
