#!/bin/sh
# Copyright (c) 2007, Technologic Systems.
# All rights reserved.
#
# Uses initrd as root filesystem and drops to shell-- on exit of serial shell,
# real Linux is booted.  Ethernet is configured as 192.168.0.50 and telnet
# server is run.  If a USB device is present, it is checked for the presence
# of an auto-update script "/tsinit", which is then run.

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/lbin
export LD_LIBRARY_PATH=/lib:/usr/lib
export CONSOLE=/dev/ttyS0

mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs none /dev
mdev -s
mkdir /dev/pts /dev/shm
mount -t devpts none /dev/pts
mount -t tmpfs none /dev/shm
export BOOTTIME=`mvtime`

setconsole $CONSOLE
stty -F $CONSOLE ospeed 115200 > /dev/null 2>&1
check-usb-update >/dev/null 2>&1 </dev/null &

(
  hostname ts7800
  ifconfig lo 127.0.0.1 up  
  route add -net 127.0.0.0 netmask 255.0.0.0 lo
  ifconfig eth0 192.168.0.50 up  
  route add default gateway 192.168.0.11 eth0 
  telnetd 

  insmod /ts7800_nand.ko
  insmod /tssdcard.ko
  mdev -s

  if [ -e /dev/tssdcardb4 -a -e /mnt/root/notrootfs ]; then
    mount -t ext3 -o ro /dev/tssdcardb4 /mnt/root
  fi
  if [ -e /dev/mtdblock3 -a -e /mnt/root/notrootfs ]; then
    mount -t yaffs2 -o ro /dev/mtdblock3 /mnt/root
  fi
  #if [ -e /dev/tssdcardb1 ]; then
  #  mount -t vfat -o ro /dev/tssdcardb1 /devkit
  #fi

  rtc7800
  portmap
 
) > /dev/null 2>&1 &

(
  export PATH=$PATH:/mnt/root/bin:/mnt/root/sbin:/mnt/root/usr/bin:/mnt/root/usr/sbin:/mnt/root/usr/local/bin:/mnt/root/urs/local/sbin
  export ENV=/shinit
  exec /usr/bin/cttyhack /bin/sh -i < $CONSOLE > $CONSOLE 2>&1
)
wait

if [ -e /mnt/root/sbin/init ]; then
  killall busybox telnetd > /dev/null 2>&1
  
  grep -e '/dev/mtdblock3 /mnt/root' /proc/mounts > /dev/null 2>&1
  if [ "$?" -eq 0 ]; then echo ">> Booting Linux (using onboard Flash)..." > $CONSOLE; fi
  grep -e '/dev/tssdcardb4 /mnt/root' /proc/mounts > /dev/null 2>&1
  if [ "$?" -eq 0 ]; then echo ">> Booting Linux (using SD Card)..." > $CONSOLE; fi

  cd /mnt/root
  pivot_root . ./initrd
  ./bin/mount -n --move ./initrd/sys ./sys
  ./bin/mount -n --move ./initrd/proc ./proc
  exec ./usr/sbin/chroot . ./sbin/init < .$CONSOLE > .$CONSOLE 2>&1
fi

