#!/bin/sh
# Copyright (c) 2010, 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:/mnt/root/bin:/mnt/root/sbin:/mnt/root/usr/bin:/mnt/root/usr/sbin:/mnt/root/usr/local/bin:/mnt/root/usr/local/sbin
export LD_LIBRARY_PATH=/lib:/usr/lib
export CONSOLE=/dev/ttymxc0
export DISPLAY=:0.0

mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs none /dev
tar zxf /devfs.tar.gz -C /dev

(
  # Bus CS0 config
  peekpoke 32 0x83fda000 0x61008F 
  # CPU GPIO1_3 and GPIO1_7 input read-back fix
  peekpoke 32 0x73FA83D0 0x10
  peekpoke 32 0x73FA83E4 0x10
  # CPU GPIO4_13 and GPIO4_14 input read-back fix
  peekpoke 32 0x73FA81ec 0x13
  peekpoke 32 0x73FA81f0 0x13
) > /dev/null 2>&1 &

setconsole $CONSOLE > /dev/null 2>&1
stty -F $CONSOLE ospeed 115200 sane > /dev/null 2>&1
hostname ts4800
ts4800ctl --autofeed 2 -d -e
/sbin/semtest -n -d -f6
eval `ts4800ctl --info`
export bootdev model baseboard_model

# Load FPGA is file 'ts4800_bitstream.vme.gz' exists
if [ -e /ts4800_bitstream.vme.gz ]; then
  echo ">> Loading FPGA..." > $CONSOLE
  (
  SCRATCH=`peekpoke 16 0xb0010016`
  ts4800ctl --loadfpga=/ts4800_bitstream.vme.gz
  insmod /mx_sdhci.ko
  rmmod mx_sdhci
  peekpoke 16 0xb0010016 $SCRATCH 
  ) > /dev/null 2>&1 
fi

/bin/sh $INITGUI >/dev/null 2>&1 </dev/null &
/bin/sh /check-usb-update >/dev/null 2>&1 </dev/null &

# NOT FASTBOOT
if [ $ROOTENV -ne 0 ]; then
  (
  ifconfig lo 127.0.0.1 up
  route add -net 127.0.0.0 netmask 255.0.0.0 lo
  xuartctl --server --irq 100hz --regstart 0xb0018000 --memstart 0xb0000000

  insmod /mx_sdhci.ko
  nandctl -X -z 131072 --nbdserver lun0:disc,lun0:part1,lun0:part2,lun0:part3,lun0:part4
  nbd-client 127.0.0.1 7525 /dev/nbd0
  nbd-client 127.0.0.1 7526 /dev/nbd1
  nbd-client 127.0.0.1 7527 /dev/nbd2
  nbd-client 127.0.0.1 7528 /dev/nbd3
  nbd-client 127.0.0.1 7529 /dev/nbd4
  if [ $ROOTENV -eq 2 ]; then
    mount -o ro $XNANDROOT /mnt/root
  else
    x=0
    while [ "$x" -lt 30 -a ! -e /sys/block/mmcblk0 ]; do
      x=$((x+1))
      usleep 100000
    done
    mount -o ro $SDROOT /mnt/root
  fi

  ) < /dev/null >/dev/null 2>&1
  if [ -e /mnt/root/notrootfs -o -e /mnt/root/fastboot -o ! -e /mnt/root/sbin/init -o "$usb" -eq 1 ]; then
    if [ "$usb" -eq 1 ]; then
      echo ">> Performing usb update. Rooting to initrd..." > $CONSOLE
    elif [ -e /mnt/root/fastboot ]; then
      echo ">> File 'fastboot' found. Rooting to initrd..." > $CONSOLE
    else 
      echo ">> Root environment failed. Rooting to initrd..." > $CONSOLE
    fi
    (
    mount -t devpts none /dev/pts
    mount -t tmpfs none /dev/shm
    mount -t tmpfs tmpfs /tmp
    ifconfig eth0 192.168.0.50
    /sbin/telnetd
    ) </dev/null >/dev/null 2>&1
  fi

# FASTBOOT
else
  (
  ifconfig lo 127.0.0.1 up
  route add -net 127.0.0.0 netmask 255.0.0.0 lo
  xuartctl --server --irq 100hz --regstart 0xb0018000 --memstart 0xb0000000

  insmod /mx_sdhci.ko
  nandctl -X -z 131072 --nbdserver lun0:disc,lun0:part1,lun0:part2,lun0:part3,lun0:part4
  nbd-client 127.0.0.1 7525 /dev/nbd0
  nbd-client 127.0.0.1 7526 /dev/nbd1
  nbd-client 127.0.0.1 7527 /dev/nbd2
  nbd-client 127.0.0.1 7528 /dev/nbd3
  nbd-client 127.0.0.1 7529 /dev/nbd4
  if [ "$bootdev" == "0x1" -a -e /mnt/root/notrootfs ]; then
    x=0
    while [ "$x" -lt 30 -a ! -e /sys/block/mmcblk0 ]; do
      x=$((x+1))
      usleep 100000
    done
    mount -o ro $SDROOT /mnt/root
  elif [ "$bootdev" == "0x0" -a -e /mnt/root/notrootfs ]; then
    mount -o ro $XNANDROOT /mnt/root 
  fi

  mount -t devpts none /dev/pts
  mount -t tmpfs none /dev/shm
  mount -t tmpfs tmpfs /tmp
  ifconfig eth0 192.168.0.50 up
  /sbin/telnetd
  modprobe smsc95xx
  ) >/dev/null 2>&1 </dev/null  &
fi

if [ $ROOTENV -eq 0 -o -e /mnt/root/notrootfs -o -e /mnt/root/fastboot -o ! -e /mnt/root/sbin/init -o "$usb" -eq 1 ]; then
  (
  let x=`peekpoke 16 0xb001000a`
  secs=$(((x>>4)&0xff))
  tenths=$((x&0xf))
  export BOOTTIME=`printf "%d.%02d" $secs $tenths`
  export ENV=/shinit
  exec setsid cttyhack /bin/sh -i
  ) <$CONSOLE >$CONSOLE 2>&1
fi

if [ -e /mnt/root/sbin/init ]; then
  wait
  killall busybox telnetd Xorg > /dev/null 2>&1
  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

