#!/bin/sh

#==========================
#       Definitions
#==========================

# Defined usb port related parameters
USB_port="90DB"
HSUSB_STATUS=`cat /etc/usb/boot_hsusb_comp | tr '\n' '\0'`
# Maximum number of LEDs
max_num_leds=9
BOARD_ID="/tmp/board_id"

#==========================
#  Platform initialization
#==========================

# Disable system suspend mode
echo "off" > /sys/power/autosleep

# bruno@askey 20200212, Enable AQC throttle function, 
# Change Link speed to 100Mb when PHY temperature > 100 degree Celsius. 
# Change Link speed to 2500Mb when PHY temperature < 80 degree Celsius. 
# Askey 2020/07/31 modify temp1_max_hyst from 80000 to 90000
if [ -e "/sys/class/hwmon/hwmon0/throttle" ]
then
	echo 1 > /sys/class/hwmon/hwmon0/throttle
	# bruno@askey 20200306: update RTL0300 AQC thermal default settings.
	PLATORM_STR=`/usr/bin/infotools -l | grep -aw PLATFORM | cut -d: -f2`
	echo "PLATORM_STR:[${PLATORM_STR}]"
	case "${PLATORM_STR}" in 
		"RTL0300")
			echo "PLATFORM-RTL0300 AQC thermal max:103,hyst:80"
			echo 103000 > /sys/class/hwmon/hwmon0/temp1_max
			echo 90000 > /sys/class/hwmon/hwmon0/temp1_max_hyst
			;;
		*)
			echo "PLATFORM-* AQC thermal is default setting. max:100,hyst:80"
			;;
	esac   
fi

# bridge0: insert bridge netfilter for iptables supprot.
insmod /usr/lib/modules/$(uname -r)/kernel/net/bridge/br_netfilter.ko
insmod /usr/lib/modules/$(uname -r)/extra/shortcut-fe-cm.ko
insmod /usr/lib/modules/$(uname -r)/extra/shortcut-fe-ipv6.ko
insmod /usr/lib/modules/$(uname -r)/extra/shortcut-fe.ko
#insmod /usr/lib/modules/$(uname -r)/extra/gsb.ko

FTM_STATUS=`/usr/bin/e2ptools -i | grep -aw FTM | cut -d: -f2 | tr -d ' ' | tr -d '\n'`
echo "FTM_STATUS:[${FTM_STATUS}]"
case "${FTM_STATUS}" in 
	1)
		# Fot rtl0300, usb compostion in user mode is buggy,
		# do not attempot to change usb temporary.
#		if [ "${HSUSB_STATUS}" != "${USB_port}" ] ; then
#			echo "switch usb composition to ${USB_port}."
#			usb_composition ${USB_port} n y y n
#		fi
		
		echo "turn off ${max_num_leds} sets of LED all"
		echo "none" > /sys/class/leds/led_1/trigger
		echo "none" > /sys/class/leds/led_2/trigger
		for leds_count in `seq 1 ${max_num_leds}` ; do
			echo 0 > /sys/class/leds/led_${leds_count}/brightness
		done
		
		# Set ip for eth0
		ifconfig eth0 192.168.1.1
		ifconfig eth0 up
		;;
	*)
		# Set CPU to performance mode to speed up system boot-up,
		# it will be disabled after xap-core start.
		echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
		
		# RTL0300 system LED initialization
		echo timer > /sys/class/leds/led_9/trigger
		echo 1 > /sys/class/leds/led_9/brightness
		;;
esac

# Export board id infor to /board_id
board_id=`cat /proc/cmdline | sed -n -e 's/^.*\(board_id=\)/\1/p'| awk -F" " '{print $1}' | awk -F"=" '{print $2}'`
if [ $board_id != "" ]
then
	if [ -e $BOARD_ID ]
	then
		rm $BOARD_ID
	fi
	
	touch $BOARD_ID
	echo board_id_full=$board_id >> $BOARD_ID
	let board_group="$(( (board_id >> 8) & 0x1 ))"
	echo Board_GRP=$board_group >> $BOARD_ID
	let board_sku="$(( (board_id >> 6) & 0x3 ))"
	echo Board_SKU=$board_sku >> $BOARD_ID
	let board_mmW="$(( (board_id >> 5) & 0x1 ))"
	echo Support_mmWave=$board_mmW >> $BOARD_ID
	let board_rf="$(( (board_id >> 3) & 0x3 ))"
	echo RF_SKU=$board_rf >> $BOARD_ID
	let board_pcb="$(( board_id  & 0x7 ))"
	echo PCB_REV=$board_pcb >> $BOARD_ID
else
	echo board_id not exist!
fi

#Copy factory_IPACM_cfg.xml to /data/misc/ipa everytime in case of file corruptted.
cp /etc/data/ipa/factory_IPACM_cfg.xml /data/misc/ipa/IPACM_cfg.xml
exit 0
