#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=10
STOP=98

uci_apply_defaults() {
	. /lib/functions/system.sh

	cd /etc/uci-defaults || return 0
	files="$(ls)"
	[ -z "$files" ] && return 0
	mkdir -p /tmp/.uci
	for file in $files; do
		( . "./$(basename $file)" ) && rm -f "$file"
	done
	uci commit
}

boot() {
	[ -f /proc/mounts ] || /sbin/mount_root
	[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc
	[ -f /proc/net/vlan/config ] && vconfig set_name_type DEV_PLUS_VID_NO_PAD

	mkdir -p /var/run
	mkdir -p /var/log
	mkdir -p /var/lock
	mkdir -p /var/state
	mkdir -p /tmp/.uci
	chmod 0700 /tmp/.uci
	mkdir -p /tmp/.jail
	touch /var/log/wtmp
	touch /var/log/lastlog
	touch /tmp/resolv.conf.auto
	ln -sf /tmp/resolv.conf.auto /tmp/resolv.conf
	grep -q debugfs /proc/filesystems && /bin/mount -o noatime -t debugfs debugfs /sys/kernel/debug
	# /bin/mount -t ramfs ramfs /etc/config
	# /bin/mount -t ramfs ramfs /etc/profile.d
	[ "$FAILSAFE" = "true" ] && touch /tmp/.failsafe

	# Create static device nodes and insmod bcm modules
	/etc/make_static_devnodes.sh
	/etc/init.d/bcm_basic_driver

	/sbin/kmodloader

	#add by wanghao
	brctl addbr br-wan
	ifconfig br-wan up
	#add end
	mknod /dev/bcmvlan c 323 0

	# allow wifi modules time to settle
	# sleep 3

	# /sbin/wifi detect > /tmp/wireless.tmp
	# [ -s /tmp/wireless.tmp ] && {
	# 	cat /tmp/wireless.tmp >> /etc/config/wireless
	# }
	# rm -f /tmp/wireless.tmp

	# /bin/board_detect
	# uci_apply_defaults
	
	# temporary hack until configd exists
	# /sbin/reload_config

	# create /dev/root if it doesn't exist
	[ -e /dev/root ] || [ -h /dev/root ] || {
		rootdev=$(awk 'BEGIN { RS=" "; FS="="; } $1 == "root" { print $2 }' < /proc/cmdline)
		[ -n "$rootdev" ] && ln -s "$rootdev" /dev/root
	}

	# mknod dev has been done at preinit/05_init_interfaces_brcm
	# loaddev
	# reload profile and usr-config/device-config
	loaduci
	mergeextraconfig
	
	#if [ -f "/bin/nvram" ]; then
	#	if [ "$(nvram kget rftestflag)" != "1" ]; then
	#		touch /tmp/g_wifi_not_cal
	#	fi
	#fi

	#echo "phylink down boot 1" > /dev/console
	#ssdk_sh debug phy set 3 0 0x840 2>&1 1>/dev/null
	#ssdk_sh debug phy set 4 0 0x840 2>&1 1>/dev/null

        if [ -f /sbin/ethctl ]; then
                eth_port_1=$(uci get interfaces.@interface[0].name -c /etc/profile.d)
                eth_port_2=$(uci get interfaces.@interface[1].name -c /etc/profile.d)
                if [ -n "$eth_port_1" ]; then
                         ethctl $eth_port_1 phy-power down
                fi
            
                if [ -n "$eth_port_2" ]; then
                        ethctl $eth_port_2 phy-power down
                fi
        fi

        if [ -f /sbin/ethswctl ]; then
            ext_switch=$(uci get profile.switch.ext_switch -c /etc/profile.d)
            [ -z "$ext_switch" ] && { ext_switch="no"; }

            if [ "$ext_switch" = "no" ]; then
                phy_port_1=$(uci get profile.switch.phy_port_1 -c /etc/profile.d)
                phy_port_2=$(uci get profile.switch.phy_port_2 -c /etc/profile.d)
                if [ -n "$phy_port_1" ]; then
                        ethswctl -c pbvlanmap -n 0 -p $phy_port_1 -v 0x101
                fi
            
                if [ -n "$phy_port_2" ]; then
                        ethswctl -c pbvlanmap -n 0 -p $phy_port_2 -v 0x110
                fi
                ethswctl -c pbvlanmap -n 0 -p 8 -v 0x1ff
                ethswctl -c regaccess -v 0x3c -l 2 -d 0x11
            else
                # open sgmii flowctrl , BCM6756 sgmii port : 5
                ethswctl -c pause -n 0 -p 5 -v 4
            fi
        fi



	/sbin/network_firm
	/sbin/reload_country.sh
}
