#!/bin/sh
# $Revision: 1.2 $
DBG="/dev/null"
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devpts devpts /dev/pts
echo 3 > /proc/sys/kernel/printk
mdev -s
SYSTYPE=$(awk '/model[^a-z]*:/ { if($3==21) print "NVX"; else print "NV6"; exit; }' /proc/cpuinfo)
if [ $SYSTYPE = "NV6" ]; then
  # 3100/3200/4200 don't have a Marvell NIC
  awk '{ if($2 ~ "11ab.*") exit 1; }' /proc/bus/pci/devices
  if [ $? -eq 0 ]; then
    if grep -q 3100 /sys/class/dmi/id/product_version; then
      SYSTYPE=3100
    elif grep -q 4200v2 /sys/class/dmi/id/product_version; then
      SYSTYPE="3200V2"
    else
      SYSTYPE=3200
    fi
  fi
  # Ultra 2
  if grep -q 808627c1 /proc/bus/pci/devices; then
    SYSTYPE="ULTRA2"
  fi
fi

# Reuseable functions
show_error() {
  echo "ERROR: ${1}!"
/bin/ash
  sleep 300
  poweroff -f
}

find_boot_device() {
  echo -n "Searching for internal boot flash device..."
  for i in `seq 1 60`; do
    # This should be the path for the internal USB port
    if [ $SYSTYPE = "NV6" ]; then
      DEV_PATH=/sys/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host*/target*/*/block/*
    elif [ $SYSTYPE = "NVX" -o $SYSTYPE = "ULTRA2" ]; then
      DEV_PATH=/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-1/1-1:1.0/host*/target*/*/block/*
    elif [ $SYSTYPE = "3100" ]; then
      DEV_PATH=/sys/devices/pci0000:00/0000:00:1a.7/usb1/1-1/1-1:1.0/host*/target*/*/block/*
    elif [ $SYSTYPE = "3200" ]; then
      DEV_PATH=/sys/devices/pci0000:00/0000:00:1d.7/usb2/2-4/2-4:1.0/host*/target*/*/block/*
    elif [ $SYSTYPE = "3200V2" ]; then
      DEV_PATH=/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.5/2-1.5:1.0/host*/target*/*/block/*
    else
      echo "I do not know the internal boot flash location!"
      DEV_PATH="*"
      break
    fi
    BOOT_FLASH=$(basename $DEV_PATH)
    grep -q ${BOOT_FLASH}$ /proc/partitions && break
    usleep 250000
  done
  if [ "$BOOT_FLASH" = "*" ]; then
    echo "Did not find boot flash!"
    show_error "Could not find internal flash device!"
  fi
  mdev -s
  echo "$BOOT_FLASH"
  BOOT_FLASH="/dev/${BOOT_FLASH}"
}

find_external_boot_device() {
  echo -n "Searching for external boot flash device..."
  for i in `seq 1 60`; do
    # This should be the path for the front USB port
    for sd in /sys/block/sd?; do
      EXTERNAL_FLASH=$(basename $sd)
      echo $BOOT_FLASH | grep -q $EXTERNAL_FLASH && continue
      if grep -q ${EXTERNAL_FLASH}1 /proc/partitions; then
        mdev -s
        if ! mount -t vfat /dev/${EXTERNAL_FLASH}1 /sysroot >/dev/null 2>/dev/null; then
	  rm -f /dev/${EXTERNAL_FLASH}1
	  continue
	fi
        IMAGE=`ls /sysroot/RAIDiator-x86-* 2>/dev/null | head -n1`
        if [ ! -s "$IMAGE" ]; then
	  umount /sysroot
	  continue
	fi
        break
      fi
    done
    [ -s "$IMAGE" ] && break
    usleep 250000
  done
  if [ $i -eq 60 ]; then
    echo "Could not find external boot flash!"
    show_error "Could not find external flash device!"
  fi
  umount /sysroot
  echo "$EXTERNAL_FLASH"
  EXTERNAL_FLASH="/dev/${EXTERNAL_FLASH}"
}

update_flash() {
  MD5SUM=$(dd if=$IMAGE bs=16384 count=1 2>/dev/null | grep -a info:: | sed -e 's/.*md5sum=//' -e 's/,.*//')
  if ! [ "$MD5SUM" = "`dd if=$IMAGE bs=16384 skip=1 2>/dev/null | md5sum | awk '{ print $1 }'`" ]; then
    show_error "flash image checksum does not match!"
  fi
  cd /tmp/flash
  dd if=$IMAGE bs=16384 skip=1 2>/dev/null | tar x || show_error "OS image extraction failed!"
  rm -rf bios loader
  mount -t msdos ${BOOT_FLASH}1 /boot_flash
  if ! [ -s "syslinux.cfg" ]; then
    cp /sysroot/syslinux.cfg .
  fi
  if [ $SYSTYPE = "NVX" ]; then
    rm -f kernel
    mv kernel.up kernel
  else
    rm -f kernel.up
  fi
  cp * /boot_flash || show_error "could not update flash device!"
  umount /boot_flash
  cd /
}

write_flash() {
  find_external_boot_device
  find_boot_device
  if mount -t msdos ${BOOT_FLASH}1 /boot_flash; then
    mkdir /tmp/flash
    cp /boot_flash/vpd /tmp/flash/
    umount /boot_flash
  fi
  echo "Formatting USB boot flash device..."
  mkdiskimage $BOOT_FLASH
  blockdev --rereadpt $BOOT_FLASH
  mdev -s
  mkdosfs ${BOOT_FLASH}1 > /dev/null
  syslinux ${BOOT_FLASH}1
  # Copy back VPD as first order
  mount -t msdos ${BOOT_FLASH}1 /boot_flash
  [ -f /tmp/flash/vpd ] && cp /tmp/flash/vpd /boot_flash/
  umount /boot_flash
  echo -e "\ndone"
  mount -t vfat ${EXTERNAL_FLASH}1 /sysroot || show_error "could not mount external flash"
  IMAGE=`ls /sysroot/RAIDiator-x86-* | head -n1`
  [ -s "$IMAGE" ] || show_error "no source flash image found"
  echo -n "Writing internal USB boot flash device..."
  update_flash
  echo "done"
}

netup() {
  udhcpc -q -n -i eth0
  udhcpc -q -n -i eth1
}

write_flash
poweroff
