#!/bin/sh
# $Id: linuxrc,v 1.21 2004/07/15 05:39:00 klhaung Exp $

PATH=/sbin:/bin

echo Busybox /linuxrc starting

echo Mounting /proc filesystem
mount -t proc none /proc

echo=echo
if grep '\bquiet\b' /proc/cmdline > /dev/null; then
  echo=true
  quiet=1
fi

$echo Creating root device
mknod /dev/root b 1 0 2>/dev/null
chmod 700 /dev/root
echo 0x100 > /proc/sys/kernel/real-root-dev

$echo Inserting modules
if [ -z "$quiet" ]; then
  /bin/insert-modules
else
  /bin/insert-modules >/dev/null
fi

# usb keyboard & mouse
if grep -i USB /proc/pci ; then
  insmod usbcore
  # usb 2.0
  insmod ehci-hcd >/dev/null
  insmod usb-uhci >/dev/null
  insmod usb-ohci >/dev/null
  insmod input >/dev/null
  insmod usbkbd
  insmod usbmouse
  insmod hid
  sleep 1
  insmod keybdev >/dev/null
  insmod mousedev >/dev/null
fi

$echo Bringing up loopback interface
ifconfig lo 127.0.0.1 up
route add -net 127.0.0.0 netmask 255.0.0.0 lo

# Hack required for prism2 cards
# It is not yet possible to use iwconfig to configure these cards,
# so we need wlanctl.
#if ifconfig wlan0 down 2> /dev/null; then
#  $echo Setting up wireless link
#  wlanctl wlan0 lnxreq_ifstate ifstate=enable
#  wlanctl wlan0 lnxreq_autojoin ssid= authtype=opensystem
#fi

$echo Obtaining IP address via DHCP

# marked by Blake, Kuo-Lien Huang 
# reason: detect how many devices and do dhcpc request each
#$echo Trying to obtain IP address via wired link [eth0]
#if udhcpc -i eth0 -f -n -q -s /bin/udhcpc-post; then
#  $echo Successfully obtained IP address via wired link [eth0]
#else
#  $echo Failed to obtain IP address via wired link [eth0]
#  $echo Trying to obtain IP address via wireless link [wlan0]
#  udhcpc -i wlan0 -f -n -q -s /bin/udhcpc-post
#fi
#netdevices="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
netdevices="$(cat /proc/net/dev | grep eth | cut -d":" -f1 | sed 's/ *//')"
for device in $netdevices
do
#  $echo "Trying to obtain IP address via $device"
#  if udhcpc -i $device --clientid="" -f -n -q -s /bin/udhcpc-post; then
#  #if udhcpc -i $device -f -n -q -s /bin/udhcpc-post; then
#    $echo "Successfully obtained IP address via $device"
#  else
#    $echo "Failed to obtain IP address via $device"
#  fi

  $echo "Trying to obtain IP address via $device"
  found=0
  retry=0
  while [ $retry -lt 3 ]; do
    if udhcpc -i $device --clientid="" -f -n -q -s /bin/udhcpc-post; then
      found=1
      break
    else
      retry=`expr $retry + 1`
    fi
  done

  if [ $found -eq 1 ]; then
    $echo "Successfully obtained IP address via $device"
    break
  else
    $echo "Failed to obtain IP address via $device"
  fi

done

## in case dhcp failed, do network configuration by hand
if [ ! -d /sysroot/initrd ]; then
  echo -n "Do you want to setup your network by hand [Y|n]? "
  read ans
  if [ "$ans" = "" -o "$ans" = "Y" -o "$ans" = "y" ]; then
    interfaces="$(echo $netdevices)"
    echo -n "Choose the network device [$interfaces]: "
    read interface
    echo -n "IP: "
    read ip
    echo -n "NETMASK: "
    read netmask
    if [ "$netmask" != "" ]; then netmask="netmask $netmask"; fi
    echo -n "BROADCAST: "
    read broadcast
    if [ "$broadcast" != "" ]; then broadcast="broadcast $broadcast"; fi
    echo -n "GATEWAY: "
    read router
    if [ "$interface" != "" -a "$ip" != "" ]; then
      ifconfig $interface $ip $netmask $broadcast
    fi
    if [ "$gateway" != "" ]; then
      route add default gw $router dev $interface
    fi
    rootpath="$router:/tftpboot/$ip"
    if [ -e /bin/drblhost ]; then
      /bin/drblhost $router:6460 $ip
    fi
    echo Mounting root filesystem $rootpath at /sysroot
    mount -t nfs -o nolock,rsize=8192,wsize=8192 $rootpath /sysroot
  fi
fi

if [ -d /sysroot/initrd ]; then
  $echo Unmounting /proc prior to pivot_root
  umount /proc

  $echo Pivoting root to /sysroot
  pivot_root /sysroot /sysroot/initrd
  cd /

  # marked by Blake,Kuo-Lien Huang
  # use tmpfs to replace devfs
  #$echo Remounting devfs at correct place
  #mount -t devfs none /dev
  #$echo Creating /dev in tmpfs
  #mount -t tmpfs -o nr_inodes=24576,size=2M none /dev
  #if [ -f /etc/diskless-image/dev.tgz ]; then
  #  cd /dev
  #  tar xzmf /etc/diskless-image/dev.tgz
  #  chmod 666 dsp*
  #  chmod 666 mixer*
  #  cd /
  #else 
  #  mount -t proc proc /proc
  #  cp /sbin/MAKEDEV /dev
  #  cd /dev
  #  ./MAKEDEV generic
  #  cd /
  #  umount /proc
  #fi

  $echo Releasing locks on old devfs
  exec 0</dev/null
  exec 1>/dev/console
  exec 2>/dev/console

  #$echo Unmounting old devfs
  #umount /initrd/dev
else
  # Failed to mount root: report error and hang
  echo FATAL ERROR: Failed to mount root filesystem
  echo Press Alt-SysRq-B or hit the reset switch to reboot
  while : ; do sleep 6000 ; done
fi

