#!/bin/sh
# Copyright (c) 2007, Technologic Systems.
# All rights reserved.
#
# If /fastboot or /tsfastboot exists on the SD card(third partition) or JP6 is 
# present on a TS-7300 then use initrd as root filesystem and drops to shell--
# on exit of serial shell, Debian is booted from SD card. 
#
# Prior being dropped to a fastboot shell ethernet is configured as 
# 192.168.0.50 and telnet server is run.  If a USB device is present in the 
# lower USB port, it is checked for the presence of an auto-update script 
# "/tsinit", which is then run. After exiting fastboot if /boot/vmlinux.bin
# exists then this kernel is booted.

let model=`peekpoke 8 0x22000000`
let mask=0x7
let model=$mask\&$model

export model
export PATH=/bin:/mnt/root/bin:/mnt/root/sbin:/mnt/root/usr/bin:/mnt/root/usr/sbin

mount -t proc proc /proc

insmod -q -f /sdcard.o -o sdcard0 dmaenable=0
if [ -e /dev/sdcard0/disc0/part3 ]; then
	mount -o ro /dev/sdcard0/disc0/part3 /mnt/root
fi

export BOOTTIME=`eptime`

if [ "$model" = "3" ]; then     		#TS-7300

	startfpga "$@" &

	if jp 2; then
		jp2=1
		if jp 4; then
			CONSOLE=/dev/ttyAM1
		else
			CONSOLE=/dev/ttyAM0  
		fi 
		stty -F $CONSOLE ospeed 115200 >/dev/null 2>&1
	else
		CONSOLE=/dev/vc/0
	fi 

	export CONSOLE
	(
		peekpoke 32 0x809300c0 0xAA		#unlock software lock
		peekpoke 32 0x80930080 0x88140d00	#disable DMA/enable GPIO pins
		check-usb-update
	) >/dev/null 2>&1 </dev/null

	if jp 6; then
		(	
		export ENV=/serial-shinit
    		if [ "$jp2" -eq "1" ]; then
			/bin/sh -i
      
			rm -f /serialsh.pid >/dev/null 2>&1
			if [ -e /vidsh.pid ]; then
				kill -1 `cat /vidsh.pid`
				>/vidsh-aborted
			else
				>/vidsh-userexited  
			fi 
		fi 
		) <$CONSOLE >$CONSOLE 2>&1
		wait
		echo 
	else
		wait
	fi

	if [ -e /vidsh-userexited ]; then
		CONSOLE="/dev/vc/0"
	fi
	export CONSOLE

elif [ "$model" = "4" ]; then 			#TS-7400

	export CONSOLE=/dev/ttyAM0
	stty -F $CONSOLE ospeed 115200 >/dev/null 2>&1
	setcons  <$CONSOLE >$CONSOLE 2>&1

	check-usb-update >/dev/null 2>&1 </dev/null

	cd /proc
	if [ -e /mnt/root/fastboot -o -e /mnt/root/tsfastboot -o ! -e /mnt/root/bin/mount ]; then
		(
			export ENV=/shinit
			ifconfig eth0 192.168.0.50
			cd /mnt/root
			telnetd
			exec sh -i
		) <$CONSOLE >$CONSOLE 2>&1
		killall busybox
	fi

elif [ "$model" = "2" ]; then			#TS-7260

	if jp 2; then
		if jp 4; then
			CONSOLE=/dev/ttyAM1
		else
			CONSOLE=/dev/ttyAM0  
		fi 
		stty -F $CONSOLE ospeed 115200 >/dev/null 2>&1
	else
		CONSOLE=/dev/null
	fi 

	export CONSOLE
	setcons  <$CONSOLE >$CONSOLE 2>&1

	(
		peekpoke 32 0x809300c0 0xAA		#unlock software lock
		peekpoke 32 0x80930080 0x80140d00	#disable DMA/enable GPIO pins
                local POWREG
                let POWREG=`peekpoke 8 0x12000000`
                let POWREG=$POWREG\|0x02
                peekpoke 8 0x12000000 $POWREG           #Enable USB 5V
		check-usb-update 
	) >/dev/null 2>&1 </dev/null

	cd /proc

	if [ -e /mnt/root/fastboot -o -e /mnt/root/tsfastboot -o ! -e /mnt/root/bin/mount ]; then
		(
			export ENV=/shinit
			ifconfig eth0 192.168.0.50
			cd /mnt/root
			telnetd
			exec sh -i
		) <$CONSOLE >$CONSOLE 2>&1
		killall busybox
	fi

else
	CONSOLE=/dev/ttyAM0
	stty -F $CONSOLE ospeed 115200 >/dev/null 2>&1
	echo "Board not supported"
	echo "Contact Technologic Systems for more information."
	exit
fi 


if test -e /mnt/root/boot/vmlinux.bin; then
	echo ">> Loading vmlinux.bin..." > $CONSOLE
	(
		mount -o remount,ro /dev/rd/0 /
		insmod -q -f bootloader
		KCMD="root=/dev/ram0 init=/stage2 console=null loops=499712"
		bootload -c "$KCMD" -r /dev/rd/0 -s 0x100000 /mnt/root/boot/vmlinux.bin
	) >/dev/null 2>&1
fi

echo ">> Booting Linux..." > $CONSOLE
cd /mnt/root
pivot_root . initrd
bin/mount -n --move initrd/dev dev
bin/mount -n --move initrd/proc proc
exec usr/sbin/chroot . sbin/init <.$CONSOLE >.$CONSOLE 2>&1
