#!/bin/busybox sh

export PATH=/bin:/sbin
/bin/busybox --install -s
ln -s /slib/libc.so.0 /slib/libgcc_s.so.1
mount -t devtmpfs devtmpfs /dev
. /ts.subr

let z=`devmem 0x8000400a 16`
let x=`devmem 0x80004008 16`
let q=`devmem 0x8000400a 16`
if [ $z -eq $q ]; then let x="z<<16|x"; else let x="q<<16"; fi

mount -t proc proc /proc
mount -t sysfs sysfs /sys
mkdir /etc/
ln -s /dev/resolv.conf /etc/resolv.conf
mkdir /dev/pts
mount -t devpts devpts /dev/pts
ifconfig lo 127.0.0.1 up
route add -net 127.0.0.0 netmask 255.0.0.0 lo
eval `tshwctl --getmac --info --baseboard`
export MODEL=${model#0x}
export jp1 jp2 jp3 jp4 jp5 jp6 jp7
export shortmac

# systemd requires any processes that should not be killed to have a 
# '@' as the first character in the process cmdline.  This means that every binary 
# That doesn't want to be killed should be called from $PATH with no leading /, and 
# should use the symlink starting with @.
ln -s /sbin/xuartctl /sbin/@xuartctl
ln -s /sbin/sdctl /sbin/@sdctl
ln -s /sbin/tshwctl /sbin/@tshwctl

# Use random mac generated by kernel if one has not been programmed
if [ $mac == "00:d0:69:ff:ff:ff" ]; then
	export mac=$(cat /sys/class/net/eth0/address)
	export shortmac=$(echo $mac | sed 's/\(..\):\(..\):\(..\):\(..\):\(..\):\(..\)/\4\5\6/')
fi

stty -F /dev/ttyS0 ospeed 115200 raw -echo >/dev/null 2>&1
eval `xuartctl --server --proxy 2>&1`
xuarg="--port=127.0.0.1:$tcp_port"
@xuartctl $xuarg </dev/ttyS0 >/dev/ttyS0 2>&1 &
cpu_uart_pid=$!
export CONSOLE=$ttyname

echo -e "#!/bin/sh\nstty raw -echo -isig && exec xuartctl $xuarg\n" > /xuartlogin
chmod +x /xuartlogin

stty -F $CONSOLE ospeed 115200 sane >/dev/null 2>&1
setconsole -r $CONSOLE
stty -F $CONSOLE rows 25 cols 80 isig

bootname

if [ $jp1 -eq 0 ]; then
	(
		let sec=x/1000000
		let y=x%1000000
		let tenths=y/10000
		export boottime=`printf "%d.%02d" $sec $tenths`
		export ENV=/shinit
		export PATH=$PATH:/mnt/root/bin:/mnt/root/sbin:/usr/bin:/usr/sbin
		while [ x`ps -l | sed "/^D.*xuart/!d;s/.*/1/;q"` != x ]; do
			sleep .01
		done
		kill -STOP $cpu_uart_pid
		setsid sh -c 'exec sh <$CONSOLE >$CONSOLE 2>&1'
	) <$CONSOLE >$CONSOLE 2>&1 &
	shell_pid=$!
fi

echo $MODEL > /dev/tsmodel
echo $baseboard_model > /dev/baseboardid
echo $baseboard > /dev/tsbaseboard
echo -1 > /proc/sys/kernel/sched_rt_runtime_us
hostname ts${MODEL}-$shortmac

@tshwctl -W
echo a2 > /dev/watchdog

if [ $jp7 = 1 ]; then
	@sdctl --lun 0 -z 65536 --nbdserver lun0:disc --bind=127.0.0.1 --dblstor-disable </dev/null >/dev/null 2>&1
	first_linux_part=`fdisk -l /dev/nbd0 | sed '/83 L/!d;s/ .*//;q'`
	ln -s $first_linux_part /dev/rootfs
	mount -o ro $first_linux_part /mnt/root
	mount -o bind /mnt/root/etc /etc/
	ln -s /mnt/root/usr /usr
	ln -s /mnt/root/lib /lib
	ln -s /mnt/root/var/lib/ /var/lib
	mkdir /var/cache
	ln -s /mnt/root/var/cache/fontconfig /var/cache/
else
	tshwctl --getrtc
	start_sdctl
	if [ -e /mnt/root/ts/config ]; then
		source /mnt/root/ts/config
		export CFG_USBINIT CFG_USBTIME
	fi

	if [ "$CFG_TOGGLE_JP2" = "1" ]; then
		let x=`devmem 0x80004002 16`
		let x="x|0x100"
		devmem 0x80004002 16 $x
	fi

	if [ "$CFG_MUTE_CPU_UART" = "1" ]; then 
		kill -9 $cpu_uart_pid
		cpu_uart_pid=""
	else
		kill -CONT $cpu_uart_pid
	fi

	if [ "$CFG_FPGARELOAD" != "0" ]; then
		if [ $MODEL = 4740 ]; then 
			spiflashctl -R 1 -k part3 | /usr/bin/hexdump | grep "aa99 5566"
			if [ $? -eq 0 ] ; then
				umount /mnt/root/
				killall -9 nbd-client sdctl tshwctl
				tshwctl --loadsp6
				tshwctl --redledoff --greenledon
				start_sdctl
				tshwctl -W
				echo a2 > /dev/watchdog
			fi
		elif [ -e /mnt/root/ts/ts${MODEL}-fpga.vme.bz2 ]; then 	
			cp /mnt/root/ts/ts${MODEL}-fpga.vme.bz2 /tmp/
			umount /mnt/root/
			killall -9 nbd-client sdctl tshwctl
			eval $(tshwctl --loadfpga /tmp/ts${MODEL}-fpga.vme.bz2)
			if [ "$loadfpga_ok" != "1" ]; then
				echo "FPGA Reload Failed: $loadfpga_error" >$CONSOLE
				echo "$loadfpga_error" > /tmp/fpgaerror
			fi
			model_reset "$MODEL"
			tshwctl --redledoff --greenledon
			start_sdctl
			tshwctl -W
			echo a2 > /dev/watchdog
		fi
	fi
	mount -o bind /mnt/root/etc /etc/
	ln -s /mnt/root/usr /usr
	ln -s /mnt/root/lib /lib
	ln -s /mnt/root/var/lib/ /var/lib
	mkdir /var/cache
	ln -s /mnt/root/var/cache/fontconfig /var/cache/
	if [ "$CFG_NETCONS_EN" != "0" ]; then
		telnetd -l /xuartlogin
		if [ "$CFG_DNSSD_EN" != "0" ]; then
			dns-sd -R "TS-${MODEL} console [$shortmac]" _telnet._tcp local 23 </dev/null >/dev/null 2>&1 &
		fi
	fi

	@xuartctl --server "$CFG_XUARGS"

	if [ "$CFG_XUART_CONSOLE_EN" = "1" ]; then
		ttyname=""
		eval `@xuartctl --server --port ${CFG_XUART_CONSOLE_PORT}`
		if [ -n "$ttyname" ]; then 
			@xuartctl $xuarg < $ttyname > $ttyname 2>&1 & 
		fi
	fi

	if [ $jp1 = 0 ]; then
		if [ "$CFG_TELNET_EN" != "0" ]; then 
			telnetd -p 2323 -l /bin/sh -F & 
			telnet_pid=$!

			if [ "$CFG_DNSSD_EN" != "0" ]; then
				dns-sd -R "TS-${MODEL} telnet [$shortmac]" _telnet._tcp local 2323 </dev/null >/dev/null 2>&1 &
			fi
		fi

		if [ "$CFG_MDNS_EN" != "0" ]; then
			bunzip2 /sbin/mdnsd.bz2
			mdnsd
		fi

		if [ "$CFG_HTTPD_EN" != "0" ]; then
			httpd </dev/null >/dev/null 2>&1
			if [ "$CFG_DNSSD_EN" != "0" ]; then
				dns-sd -R "TS-${MODEL} http [$shortmac]" _http._tcp local 80 </dev/null >/dev/null 2>&1 &
			fi
		fi
	fi

	# needed for usb otg host
	modprobe g_zero
	echo 1 > /proc/driver/otg

	mac2=$(printf "%x\n" $(echo $((0x$shortmac+1))) | sed "s/.*\(..\)\(..\)\(..\)/00:d0:69:\1:\2:\3/")
	mac3=$(printf "%x\n" $(echo $((0x$shortmac+2))) | sed "s/.*\(..\)\(..\)\(..\)/00:d0:69:\1:\2:\3/")
	if [ "$baseboard" = "8920" ]; then
		ONBOARDSW="1"
		tshwctl --bbclkon --txenon 0,2,4
		muxbus_enable
		lcd_enable
		show_splash 1024x600
		can_enable
		sound_enable
		modprobe ts_lcd calib_x0=73 calib_dx=3900 calib_y0=190 calib_dy=3824
		sgtl5000_setup
		start_xinit
	elif [ "$baseboard" = "8900" ]; then
		muxbus_enable
		tshwctl --bbclkon --txenon 0,4
		can_enable
		usbeth_enable
		modprobe ts_lcd calib_x0=73 calib_dx=3900 calib_y0=190 calib_dy=3824
		fbset -g 800 600 800 1200 32 -t 25000 50 70 0 0 50 50 -hsync high -vsync high
		sgtl5000_setup
		sound_enable
		lcd_enable
		show_splash 800x600
		start_xinit
	elif [ "$baseboard" = "8820" ]; then
		tshwctl --bbclkon # fpga clock
		tshwctl --txenon 0,4 # modbus port
		muxbus_enable
	elif [ "$baseboard" = "8700" ]; then
		tshwctl --bbclk2on # switchchip clock
		reset_bb_switch
		tshwctl --txenon 0,1 # modbus port
		OFFBOARDSW="1"
	elif [ "$baseboard" = "8550" ]; then
		can_enable
		tshwctl --txenon 0
		ONBOARDSW="1"
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8500" ]; then
		tshwctl --txenon 0
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8400" ]; then
		tshwctl --txenon=0,4 --touchon
		can_enable
		usbeth_enable
		modprobe ts_lcd calib_x0=170 calib_dx=3872 calib_y0=140 calib_dy=3792 swapxy=1 negx=1
		wm8750_setup
		sound_enable
		lcd_enable
		## Mirror X axis with LCD_MODE_1
		devmem 0xD4019010 32 $(($(devmem 0xD4019010 32) | 0x10000)) #DDR 
		devmem 0xD4019028 32 $(($(devmem 0xD4019028 32) | 0x10000)) #LOW
		show_splash 640x480
		start_xinit
	elif [ "$baseboard" = "8390" ]; then
		tshwctl --txenon=0,4 --touchon
		can_enable
		usbeth_enable
		modprobe ts_lcd calib_x0=107 calib_dx=3847 calib_y0=270 calib_dy=3628
		sgtl5000_setup
		sound_enable
		lcd_enable
		show_splash 800x480
		start_xinit
	elif [ "$baseboard" = "8380" ]; then
		ONBOARDSW="1"
		tshwctl --touchon --txenon 0
		modprobe ts_lcd calib_x0=99 calib_dx=3993 calib_y0=180 calib_dy=3738
		tshwctl --setdio 31 # en_backlight
		#tshwctl --setdio 27 # modem 5v power
		tshwctl --clrdio 30 # en_spkr#
		sgtl5000_setup
		sound_enable
		lcd_enable
		show_splash 800x480
		start_xinit
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8280" ]; then
		tshwctl --txenon 0 # modbus port
		modprobe ts-st7565p-fb
		ONBOARDSW="1"
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8290" ]; then
		modprobe hdmi_sii9022
		modprobe snd-soc-ts4700
		sound_enable
		lcd_enable
		show_splash 800x600
		start_xinit
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8150" ]; then
		ONBOARDSW="1"
		muxbus_enable
		tshwctl --txenon=0,4 --bbclk3on
	elif [ "$baseboard" = "8160" ]; then
		muxbus_enable
		devmem 0x80008002 16 0x3d00 # disable can standby
		tshwctl --txenon=0,4
		can_enable
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$baseboard" = "8100" ]; then	
		muxbus_enable
		tshwctl --txenon=0,4
		can_enable
		usbeth_enable
	elif [ "$MODEL" = "7700" ]; then
		can_enable
		if [ "$CFG_RESETSW_EN" != "0" ]; then
			tshwctl --resetswitchon
		fi
	elif [ "$MODEL" = "7250" ]; then
		ONBOARDSW="1"
		let fpga_version=revision
		if [ $fpga_version -ge 9 ]; then
			modprobe sja1000_isa mem=0x81004c00 irq=65 clk=24000000
		fi
	fi
fi

ifconfig eth0 hw ether $mac	
if [ $MODEL = 4740 ]; then
	ifconfig eth1 hw ether $mac3
fi

if [ $jp7 != 1 ]; then
	if [ $MODEL = 4712 -o $MODEL = 4720 -o $MODEL = 7250 ]; then
		if [ "$CFG_2ETH" = "2" ]; then
			tshwctl --ethswitch
		elif [ "$CFG_2ETH" = "1" ] || [ -n "$ONBOARDSW" ]; then
			SWIFACE="eth0"
			vconfig add eth0 1
			vconfig add eth0 2
			tshwctl --ethvlan
			ifconfig eth0 up
			ifconfig eth0.1 up
			ifconfig eth0.2 up
		else
			tshwctl --ethswitch
		fi
	else
		unset $ONBOARDSW
	fi

	if [ -n "$OFFBOARDSW" ]; then
		if [ "$CFG_4ETH" = "2" ]; then
			tshwctl --ethswitch --ethbb
		else
			if [ $MODEL = 4740 ]; then
				SWIFACE="eth1"
			else
				SWIFACE="eth0"
			fi

			if [ "$CFG_4ETH" = "1" ]; then
				vconfig add $SWIFACE 1
				vconfig add $SWIFACE 2
				tshwctl --ethwlan --ethbb
				ifconfig $SWIFACE up
				ifconfig $SWIFACE.1 up
				ifconfig $SWIFACE.2 up
			else
				vconfig add $SWIFACE 1
				vconfig add $SWIFACE 2
				vconfig add $SWIFACE 3
				vconfig add $SWIFACE 4
				tshwctl --ethvlan --ethbb
				ifconfig $SWIFACE up
				ifconfig $SWIFACE.1 up
				ifconfig $SWIFACE.2 up
				ifconfig $SWIFACE.3 up
				ifconfig $SWIFACE.4 up
			fi
		fi
	fi

	if [ $jp1 = 0 -a "$CFG_IFPLUGD_EN" != "0" ]; then
		# start ifplugd on interfaces unless an onboard or offboard switch is used
		# if using a switch ifplugd is only started on eth#.0-7 interfaces
		for iface in $(ls /sys/class/net | grep "eth")
		do
			if [ "$iface" != "$SWIFACE" ]; then
				ifplugd -s -F -i $iface </dev/null >/dev/null 2>&1
			fi
		done
	fi
fi

if [ $jp1 -eq 1 -a -e /mnt/root/ts/fastboot ]; then
	(
	  let sec=x/1000000
	  let y=x%1000000
	  let tenths=y/10000
	  echo "/ts/fastboot file present.  Booting to initramfs instead" > $CONSOLE
	  export boottime=`printf "%d.%02d" $sec $tenths`
	  export ENV=/shinit
	  export PATH=$PATH:/mnt/root/bin:/mnt/root/sbin:/usr/bin:/usr/sbin
	  setsid sh -c 'exec sh <$CONSOLE >$CONSOLE 2>&1'
	) <$CONSOLE >$CONSOLE 2>&1 &
	shell_pid=$!
fi
ln -sf $CONSOLE /dev/tsconsole

tshwctl --setrng --redledoff --greenledon

# Run user init scripts
(
	export PATH=$PATH:/mnt/root/bin:/mnt/root/sbin:/usr/bin:/usr/sbin
	if [ -x /mnt/root/ts/init ]; then /mnt/root/ts/init; fi

	if [ $jp7 = 0 ]; then 
		if [ "$CFG_USBINIT" = "1" -a $jp1 = 0 ]; then
			/check-usb-update
		elif [ "$CFG_USBINIT" != "0" ]; then
			/check-usb-update
		fi
	fi
) <$CONSOLE >$CONSOLE 2>&1

if [ -n "$cpu_uart_pid" ]; then kill -CONT $cpu_uart_pid; fi
if [ -n "$shell_pid" ]; then wait $shell_pid; fi
killall -9 ifplugd dns-sd zcip udhcpc httpd mdnsd X >/dev/null 2>&1
kill -9 $telnet_pid

# Blank FB if it exists, and we booted X11 from the initramfs
if [ -e /dev/fb0 -a $jp1 = 0 ]; then
	dd if=/dev/zero bs=1920 count=1080 of=/dev/fb0 >/dev/null 2>&1
fi

if [ -e /mnt/root/sbin/init ]; then
	mount -t proc proc /mnt/root/proc
	mount -t devtmpfs devtmpfs /mnt/root/dev
	mount -t devpts devpts /mnt/root/dev/pts
	mount -t tmpfs tmpfs /mnt/root/slib
	mount -t tmpfs tmpfs /mnt/root/ts/sbin/
	umount /etc/
	cp /sbin/* /mnt/root/ts/sbin/
	cp /slib/* /mnt/root/slib/
	cp /bin/busybox /mnt/root/ts/sbin/
	export PATH=/bin:/sbin:/usr/bin:/usr/sbin
	exec switch_root /mnt/root /sbin/init < $CONSOLE > $CONSOLE 2>&1
fi
