#!/bin/sh
# 
# UFS-910 Barry Allen bainit by gutemine
#
BAVERSION=5.4.5_tested_by_AAF_Team
#

mount_ba_device()
{
  if [ `df | grep "BA" | wc -l` -gt 0 ]; then
    echo "BA Device already mounted!"
    return
  fi

  for DEVS in 1 2 3; do
    DEV_LETTER=`echo abc | cut -b $DEVS`
    DEV=sd$DEV_LETTER
    DEV_LIST=`cat /proc/partitions | grep "$DEV[1-4]" | cut -b 23-26`
    if [ "$DEV_LIST" ]; then
      echo "Device: $DEV"
      PART_COUNT=`echo "$DEV_LIST" | wc -l`
      echo "Number of Partitions: $PART_COUNT"
      for DEV_PART in $DEV_LIST; do
        echo "Partition: $DEV_PART"
        LABEL=`(/config/tune2fs -l /dev/$DEV_PART | grep "Filesystem volume name:" | cut -b 27-80) 2>/dev/null`
        if [ $LABEL ]; then
          echo "Label: $LABEL"
          if [ "$LABEL" = "XBA" -o "$LABEL" = "BA" ]; then
            echo "BA Label found!"
            MOUNT_DEV="/dev/$DEV_PART"
            MOUNT_TYPE=""
            if [ $PART_COUNT -gt 1 ]; then
              PART_NUM=`echo "$DEV_PART" | cut -c 4`
              MOUNT_POINT="/tmp/usb/XBA ($DEVS-$PART_NUM)"
            else
              MOUNT_POINT="/tmp/usb/XBA ($DEVS)"
            fi
            break
          fi
        fi
      done
      if [ "$MOUNT_POINT" ]; then
        break
      fi
    fi
  done
  
  if [ "$MOUNT_POINT" ]; then
    mkdir -p "$MOUNT_POINT"
    mount $MOUNT_TYPE $MOUNT_DEV "$MOUNT_POINT"
    echo "$MOUNT_DEV" >/config/.badevice
    echo "$MOUNT_POINT" >/config/.badirectory
    echo "BA Device $MOUNT_DEV mounted on $MOUNT_POINT"
  else
    echo "BA Device not found!"
  fi
}

TEST=false

if [ -f /config/.badirectory ]; then
   BAROOTM=`cat /config/.badirectory`
else 
   BAROOTM="/tmp/usb/XBA (1)"  
fi

BAROOT="/data/baroot"
BAHOME="$BAROOT/ba"

if [ -f /config/.badevice ]; then
   USB_DEVICE=`cat /config/.badevice` 
else
   USB_DEVICE=/dev/sda1  
fi

#HDROOT="/tmp/usb/Harddisk (1)"
#if [ -f /config/.hddirectory ]; then
#   HDROOT=`cat /config/.hddirectory`
#fi
#HD_DEVICE=/dev/dummy
#if [ -f /config/.hddevice ]; then
#   HD_DEVICE=`cat /config/.hddevice` 
#fi

if [ $# -eq 0 ]; then
   if [ ! -f "/.bainfo" ]; then
      swapoff -a > /dev/null 2>&1
      # we are in Flash and try to mount USB device to boot 
      if [ ! -f "$BAHOME/ba.sh" ]; then
         echo "$BAVERSION"
         if [ -f /sbin/tune2fs ]; then
           TUNEBIN="/sbin"
         else
           TUNEBIN="/config"
         fi
         mkdir "$BAROOT" > /dev/null 2>&1
         mkdir /tmp/usb > /dev/null 2>&1  
         for i in 1 2 3 4 5 ; do      
         mount -t usbfs none /proc/bus/usb/ 
         sleep 2
         #mkdir /tmp/usb/0 > /dev/null 2>&1
         if [ `$TUNEBIN/tune2fs -l /dev/sda1 | grep BA | wc -l` -gt 0 ]; then
           USB_DEVICE="/dev/sda1"
           echo "$USB_DEVICE" > /config/.badevice
           if [ `$TUNEBIN/tune2fs -l /dev/sda2 | grep "No valid superblock" | wc -l` -gt 0 ]; then
             echo "/tmp/usb/XBA (1)" > /config/.badirectory
           else
             echo "/tmp/usb/XBA (1-1)" > /config/.badirectory  
           fi  
           TEST=true
           break
         else  
           if [ `$TUNEBIN/tune2fs -l /dev/sdb1 | grep BA | wc -l` -gt 0 ]; then
             USB_DEVICE="/dev/sdb1"
             echo "$USB_DEVICE" > /config/.badevice
             if [ `$TUNEBIN/tune2fs -l /dev/sdb2 | grep "No valid superblock" | wc -l` -gt 0 ]; then
               echo "/tmp/usb/XBA (2)" > /config/.badirectory
             else
               echo "/tmp/usb/XBA (2-1)" > /config/.badirectory  
             fi  
             TEST=true
             break 
           else
             if [ `$TUNEBIN/tune2fs -l /dev/sdc1 | grep BA | wc -l` -gt 0 ]; then
               USB_DEVICE="/dev/sdc1"
               echo "$USB_DEVICE" > /config/.badevice
               if [ `$TUNEBIN/tune2fs -l /dev/sdc2 | grep "No valid superblock" | wc -l` -gt 0 ]; then
                 echo "/tmp/usb/XBA (3)" > /config/.badirectory
               else
                 echo "/tmp/usb/XBA (3-1)" > /config/.badirectory  
               fi  
               TEST=true
               break
             else
               if [ `$TUNEBIN/tune2fs -l /dev/sda | grep BA | wc -l` -gt 0 ]; then
                 USB_DEVICE="/dev/sda"
                 echo "$USB_DEVICE" > /config/.badevice
                 echo "/tmp/usb/XBA (1)" > /config/.badirectory
                 TEST=true
                 break
               else
                 if [ `$TUNEBIN/tune2fs -l /dev/sdb | grep BA | wc -l` -gt 0 ]; then
                   USB_DEVICE="/dev/sdb"
                   echo "$USB_DEVICE" > /config/.badevice
                   echo "/tmp/usb/XBA (2)" > /config/.badirectory
                   TEST=true
                   break 
                 else
                   $TUNEBIN/tune2fs -l /dev/sda1 > /tmp/testt 2>&1
                   if [ `cat /tmp/testt | grep "is mounted" | wc -l` -gt 0 ]; then
                     USB_DEVICE="/dev/sda1"
                     echo "$USB_DEVICE" > /config/.badevice
                     if [ `$TUNEBIN/tune2fs -l /dev/sda2 | grep "No valid superblock" | wc -l` -gt 0 ]; then
                       echo "/tmp/usb/XBA (1)" > /config/.badirectory
                     else
                       echo "/tmp/usb/XBA (1-1)" > /config/.badirectory  
                     fi  
                     TEST=true
                     break
                   else
                     $TUNEBIN/tune2fs -l /dev/sdb1 > /tmp/testt 2>&1
                     if [ `cat /tmp/testt | grep "is mounted" | wc -l` -gt 0 ]; then
                       USB_DEVICE="/dev/sdb1"
                       echo "$USB_DEVICE" > /config/.badevice
                       if [ `$TUNEBIN/tune2fs -l /dev/sdb2 | grep "No valid superblock" | wc -l` -gt 0 ]; then
                         echo "/tmp/usb/XBA (2)" > /config/.badirectory
                       else
                         echo "/tmp/usb/XBA (2-1)" > /config/.badirectory  
                       fi  
                       TEST=true
                       break
                     fi
                   fi  
                 fi    
               fi  
             fi    
           fi
         fi
         done
       
        
         if [ $TEST = "false" ]; then
           if [ `df | grep "BA" | wc -l` -gt 0 ]; then
             USB_DEVICE=`df | grep /tmp/usb/XBA | cut -d " " -f 1`
             USB_ROOT=`df | grep $USB_DEVICE | cut -d % -f 2- | cut -c 2-`
             echo "$USB_DEVICE" > /config/.badevice
             echo "$USB_ROOT" > /config/.badirectory
             TEST=true
           fi
         fi    
         
         if [ $TEST = "false" ]; then
           echo "BA Device not found --> use last BA Device --> $USB_DEVICE"
         else
           echo "BA Device found on $USB_DEVICE"  
         fi  
         
         BAROOTM=`cat /config/.badirectory`     
         
         mount $USB_DEVICE "$BAROOT"
         #if [ -f $HD_DEVICE ]; then
         #   mkdir "$HDROOT" > /dev/null 2>&1
         #  mount $HD_DEVICE "$HDROOT"
         #fi
      fi
      if [ ! -f "$BAHOME/ba.sh" ]; then
         umount "$BAROOT"
         echo "Barry Allen not (yet) installed on USB device, or not available"
         echo "Continue booting Flash ..."
         exit 0
      fi
   fi
   
   if [ ! -f "/.bainfo" ]; then
      ln -s "$BAHOME" /tmp/ba
      rm -r /dev/stapi
      /tmp/ba/barryallen.sh
      sync
      rm /tmp/ba
      echo "bootmenu ende \n"
      if [ ! -f "$BAHOME/.baboot" ]; then
         TARGET="Flash"
      else
         TARGET=`cat "$BAHOME/.baboot"`
      fi
#********************************************************************************
      echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
      "$BAHOME/tools/fw_printenv" > /tmp/boot1
      if [ -f "$BAHOME/bootsich" ]; then
        if [ `cat /tmp/boot1 | grep "customboot=" | wc -l` -lt 0 ]; then
          BOOTARGSUSB=`cat "$BAHOME/bootsich" | grep "bootargsusb=" | cut -c13-999`
          if [ "$BOOTARGSUSB" ]; then
            BOOTARGSUSB=`echo -n "$BOOTARGSUSB"`
            "$BAHOME/tools/fw_setenv" bootargsusb $BOOTARGSUSB
          fi  
          BOOTUSB=`cat "$BAHOME/bootsich" | grep "bootusb=" | cut -c9-999`
          if [ "$BOOTUSB" ]; then
            BOOTUSB=`echo -n "$BOOTUSB"`
            "$BAHOME/tools/fw_setenv" bootusb $BOOTUSB
          fi  
          CUSTOMBOOT=`cat "$BAHOME/bootsich" | grep "customboot=" | cut -c12-999`
          if [ "$CUSTOMBOOT" ]; then
            CUSTOMBOOT=`echo -n "$CUSTOMBOOT"`
            "$BAHOME/tools/fw_setenv" customboot $CUSTOMBOOT
          fi  
          SERVERIP=`cat "$BAHOME/bootsich" | grep "serverip=" | cut -c10-999`
          if [ "$SERVERIP" ]; then
            SERVERIP=`echo -n "$SERVERIP"`
            "$BAHOME/tools/fw_setenv" serverip $SERVERIP
          fi  
          IPADDR=`cat "$BAHOME/bootsich" | grep "ipaddr=" | cut -c8-999`
          if [ "$IPADDR" ]; then
            IPADDR=`echo -n "$IPADDR"`
            "$BAHOME/tools/fw_setenv" ipaddr $IPADDR
          fi  
          GATEWAY=`cat "$BAHOME/bootsich" | grep "gateway=" | cut -c9-999`
          if [ "$GATEWAY" ]; then
            GATEWAY=`echo -n "$GATEWAY"`
            "$BAHOME/tools/fw_setenv" gateway $GATEWAY
          fi  
        else
          "$BAHOME/tools/fw_printenv" > "$BAHOME/bootsich"
          sync
        fi  
      else
        if [ `cat /tmp/boot1 | grep "customboot=" | wc -l` -gt 0 ]; then  
          "$BAHOME/tools/fw_printenv" > "$BAHOME/bootsich"
          sync
        fi
      fi    
      
      BOOTP="bootp.conf"
       
      if [ -f "$BAHOME/ba/$TARGET/$BOOTP" ]; then
          echo "starte extern"

          "$BAHOME/tools/fw_printenv" > /tmp/boot1
          if [ `cat /proc/partitions | grep "sdb" | wc -l` -gt 0 ]; then
            if [ -f "$BAHOME/ba/$TARGET/bootp2.conf" ]; then
              BOOTP="bootp2.conf"
            fi  
          fi
          BAFLAG=0
          BA2FLAG=1
          BACMDORG="run ba2cmd; bootm a0040000"
          BA2CMD="if itest.s \$ba2flag == 1; then set ba2flag 0; saveenv;if itest.l *a0fd0000 == -85525248; then go a0fd0000; fi; fi; bootm a0040000"        
          BABOOT="if itest.s \$baflag == 1; then set baflag 0; saveenv; run baargs; run bacmd; fi"  
          BOOTCMD="run baboot;"
          if [ `cat /tmp/boot1 | grep "ba2cmd=" | wc -l` -lt 1 ]; then
            if [ `cat /tmp/boot1 | grep "bootcmd=go a0fd0000" | wc -l` -gt 0 ]; then
              "$BAHOME/tools/fw_setenv" bootcmd $BACMDORG
              "$BAHOME/tools/fw_setenv" ba2cmd $BA2CMD
              "$BAHOME/tools/fw_setenv" ba2flag $BA2FLAG
              echo "use 2nd stage BL"
              echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
              "$BAHOME/tools/fw_printenv" > /tmp/boot1
            else
              if [ `cat /tmp/boot1 | grep "bootcmd=if itest.l" | wc -l` -gt 0 ]; then
                "$BAHOME/tools/fw_setenv" bootcmd $BACMDORG
                "$BAHOME/tools/fw_setenv" ba2cmd $BA2CMD
                "$BAHOME/tools/fw_setenv" ba2flag $BA2FLAG 
                echo "use 2nd stage BL"
                echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
                "$BAHOME/tools/fw_printenv" > /tmp/boot1
              else
                if [ `cat /tmp/boot1 | grep "bootcmd=run ba2cmd" | wc -l` -gt 0 ]; then
                  "$BAHOME/tools/fw_setenv" bootcmd $BACMDORG
                  "$BAHOME/tools/fw_setenv" ba2cmd $BA2CMD
                  "$BAHOME/tools/fw_setenv" ba2flag $BA2FLAG 
                  echo "use 2nd stage BL"
                  echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
                  "$BAHOME/tools/fw_printenv" > /tmp/boot1 
                fi   
              fi 
            fi
          else
            "$BAHOME/tools/fw_setenv" ba2flag $BA2FLAG
            echo "use 2nd stage BL"
            echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
            "$BAHOME/tools/fw_printenv" > /tmp/boot1    
          fi  
          if [ `cat /tmp/boot1 | grep "bootcmd=run baboot" | wc -l` -lt 1 ]; then
            echo "set Parm ---> bootcmd"
            BOOTCMDORG=`cat /tmp/boot1 | grep "bootcmd=" | cut -c9-999`
            BOOTCMD=`echo -n "$BOOTCMD $BOOTCMDORG"`
            "$BAHOME/tools/fw_setenv" bootcmd $BOOTCMD
          fi
          if [ `cat /tmp/boot1 | grep "baboot=$BABOOT" | wc -l` -lt 1 ]; then
            echo "set Parm ---> baboot"
            "$BAHOME/tools/fw_setenv" baboot $BABOOT
          fi  
                    
          BOOTARGS=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep BOOTARGS= | cut -d = -f 2- | cut -c 1-`
          if [ ! "$BOOTARGS" ]; then
            echo "BOOTARGS not Found"
            #BOOTARGS="set bootargs 'console=ttyAS0,115200 root=/dev/mtdblock3 mem=38m bigphysarea=1280 coprocessor_mem=2m@0x04000000,2m@0x04200000'"
          else
            BOOTARGS=`echo "set bootargs $BOOTARGS"`
            if [ `cat /tmp/boot1 | grep "baargs=$BOOTARGS" | wc -l` -lt 1 ]; then
              "$BAHOME/tools/fw_setenv" baargs $BOOTARGS
              echo "set baargs"
            fi  
          fi
          BOOTCMD=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep BOOTCMD= | cut -d = -f 2- | cut -c 1-`  
          if [ ! "$BOOTCMD" ]; then
            echo "BOOTCMD not found"
          else  
            if [ `cat /tmp/boot1 | grep "bacmd=$BOOTCMD" | wc -l` -lt 1 ]; then
              "$BAHOME/tools/fw_setenv" bacmd $BOOTCMD
              echo "set bacmd"
            fi
          fi
          DEVICE=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep BADEVICE= | cut -d = -f 2- | cut -c 1-`  
          if [ ! "$DEVICE" ]; then
            DEVICE="/dev/sda2"
          fi
          DEVID=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep BADEVID= | cut -d = -f 2- | cut -c 1-`  
          if [ ! "$DEVID" ]; then
            DEVID="0:2"
          fi
          
          if [ `cat "$BAHOME/ba/$TARGET/$BOOTP" | grep ASSIGNUSBDEVICES=y | wc -l` -gt 0 ]; then 
            HELPDEV=`echo "$USB_DEVICE" | cut -c1-8`
            HELPPART=`echo "$DEVICE" | cut -c9`
            if [ `cat "$BAHOME/ba/$TARGET/$BOOTP" | grep MOUNTDIFFERENT=1 | wc -l` -gt 0 ]; then 
              DEVICE=`echo "$HELPDEV$HELPPART"`
            else
              if [ `cat "$BAHOME/ba/$TARGET/$BOOTP" | grep MOUNTDIFFERENT=2 | wc -l` -gt 0 ]; then  
                 HELPPART=`echo "$DEVID" | cut -c3`
                 if [ $HELPDEV = "/dev/sda" ]; then
                   DEVID=`echo "0:$HELPPART"`
                 fi
                 if [ $HELPDEV = "/dev/sdb" ]; then
                   DEVID=`echo "1:$HELPPART"`
                 fi
                 if [ $HELPDEV = "/dev/sdc" ]; then
                   DEVID=`echo "2:$HELPPART"`
                 fi
               else   
                 HELPDEV=`echo "$USB_DEVICE" | cut -c1-8`
                 HELPPART=`echo "$DEVICE" | cut -c9`
                 DEVICE=`echo "$HELPDEV$HELPPART"`
                 HELPPART=`echo "$DEVID" | cut -c3`
                 if [ $HELPDEV = "/dev/sda" ]; then
                   DEVID=`echo "0:$HELPPART"`
                 fi
                 if [ $HELPDEV = "/dev/sdb" ]; then
                   DEVID=`echo "1:$HELPPART"`
                 fi
                 if [ $HELPDEV = "/dev/sdc" ]; then
                   DEVID=`echo "2:$HELPPART"`
                 fi
               fi
            fi
          fi           
          if [ `cat /tmp/boot1 | grep "badevice=$DEVICE" | wc -l` -lt 1 ]; then
            "$BAHOME/tools/fw_setenv" badevice $DEVICE
            echo "set badevice"
          fi
          if [ `cat /tmp/boot1 | grep "badevid=$DEVID" | wc -l` -lt 1 ]; then
            "$BAHOME/tools/fw_setenv" badevid $DEVID
            echo "set badevid"
          fi  
          
          
          IPADDR=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep IPADDR= | cut -d = -f 2- | cut -c 1-`
          if [ "$IPADDR" ]; then
            if [ `cat /tmp/boot1 | grep "ipaddr=$IPADDR" | wc -l` -lt 1 ]; then
              "$BAHOME/tools/fw_setenv" ipaddr $IPADDR
              echo "set ipaddr"
            fi
          fi
          SERVERIP=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep SERVERIP= | cut -d = -f 2- | cut -c 1-`
          if [ "$SERVERIP" ]; then
            if [ `cat /tmp/boot1 | grep "serverip=$SERVERIP" | wc -l` -lt 1 ]; then
              "$BAHOME/tools/fw_setenv" serverip $SERVERIP
            fi
          fi 
          GATEWAY=`cat "$BAHOME/ba/$TARGET/$BOOTP" | grep GATEWAY= | cut -d = -f 2- | cut -c 1-`
          if [ "$GATEWAY" ]; then
            if [ `cat /tmp/boot1 | grep "gateway=$GATEWAY" | wc -l` -lt 1 ]; then
              "$BAHOME/tools/fw_setenv" gateway $GATEWAY
            fi
          fi
          BAFLAG=1
          "$BAHOME/tools/fw_setenv" baflag $BAFLAG
          cd "$BAROOT"
          rm activImage > /dev/null 2>&1
          ln -s ./ba/ba/$TARGET ./activImage   
          sync
          if [ -f "/config/.lastboot" ]; then
            TARGET=`cat "/config/.lastboot"`
          else
            TARGET="Flash"
          fi 
          sync
          "$BAHOME/tools/"reboot -f
          reboot
          sleep 10
          #exit 0          
        
      fi
#********************************************************************************      
      echo "$TARGET" > "/config/.lastboot"
      if [ -d "$BAHOME/ba/$TARGET" ]; then
         echo "Booting now Image $TARGET from $BAHOME/ba/$TARGET"
         if [ `mount | grep "$TARGET/proc" | wc -l` -lt 1 ]; then
            echo "mount /proc"
            mount -o bind /proc "$BAHOME/ba/$TARGET/proc"
         fi
         if [ `mount | grep "$TARGET/dev" | wc -l` -lt 1 ]; then
            mount -o bind /dev  "$BAHOME/ba/$TARGET/dev"
            echo "mount /dev"
         fi
         #if [ `mount | grep "$TARGET/dev_org" | wc -l` -lt 1 ]; then
         #   mount -o bind /dev_org  "$BAHOME/ba/$TARGET/dev_org"
         #fi
         if [ ! -f "$BAROOT/ba/ba/$TARGET/config/start-ext.sh" ]; then
           if [ `mount | grep "$TARGET/var" | wc -l` -lt 1 ]; then
              mount -o bind /var  "$BAHOME/ba/$TARGET/var"
           fi
         fi
         if [ `mount | grep "$TARGET/tmp" | wc -l` -lt 1 ]; then
            mount -o bind /tmp  "$BAHOME/ba/$TARGET/tmp"
         fi
         if [ `mount | grep "$TARGET/ramdisk" | wc -l` -lt 1 ]; then
            mount -o bind /ramdisk  "$BAHOME/ba/$TARGET/ramdisk"
         fi
         
         ## mount Flash
         mkdir "$BAHOME/ba/$TARGET/flash" > /dev/null 2>&1
         mkdir "$BAHOME/ba/$TARGET/flash/data" > /dev/null 2>&1
         mkdir "$BAHOME/ba/$TARGET/flash/config" > /dev/null 2>&1
         mount -o bind /data  "$BAHOME/ba/$TARGET/flash/data"
         mount -o bind /config  "$BAHOME/ba/$TARGET/flash/config"
         
         ## Swapfile
         mkdir "$BAHOME/ba/$TARGET/swap" > /dev/null 2>&1
         #mkdir "$BAHOME/ba/$TARGET/$BAROOTM"
         #mount -o bind "$BAROOT" "$BAHOME/ba/$TARGET/$BAROOTM"
                  
         #if [ `mount | grep "$TARGET/$BAROOT" | wc -l` -lt 1 ]; then
         #   mount -o bind "$BAROOT" "$BAHOME/ba/$TARGET/$BAROOT"
         #   echo "mount bind"
         #fi
         #if [ -f $HD_DEVICE ]; then
         #   if [ `mount | grep "$TARGET/$HDROOT" | wc -l` -lt 1 ]; then
         #      mount -o bind "$HDROOT" "$BAHOME/ba/$TARGET/$HDROOT"
         #   fi
         #fi
         # make sure that latest bainit and device is used from image when started
         cp /config/bainit "$BAHOME/ba/$TARGET/etc/init.d/bainit"
         cp /config/bainit "$BAHOME/ba/$TARGET/config/bainit"
         cp /config/.badirectory "$BAHOME/ba/$TARGET/config/.badirectory"
         cp /config/.badevice "$BAHOME/ba/$TARGET/config/.badevice"
         #cp /config/.hddirectory "$BAHOME/ba/$TARGET/config/.hddirectory"
         # now start image in chroot environment with bainit
         
         killall vsftpd > /dev/null 2>&1
         killall telnetd > /dev/null 2>&1
         
         echo "start chroot\n"
         
         exec chroot "$BAHOME/ba/$TARGET" /etc/init.d/bainit
         exit 0
      else
         umount "$BAROOT"
         #BAROOT="/tmp/usb/XBA (1)"
         #if [ -f /config/.badirectory ]; then
         #   BAROOT=`cat /config/.badirectory`
         #fi
         #mount $USB_DEVICE "$BAROOT"
         rmmod stmfb.ko
         rmmod stmcore-display-stx7100.ko
         rmmod cfbcopyarea.ko
         rmmod cfbfillrect.ko
         rmmod cfbimgblt.ko
         rmmod fb.ko
         echo "Target Image directory $BAHOME/ba/$TARGET not found, continue booting Flash"
         exit 0
      fi
   else
      # here comes the startup for the chroot environment
      if [ -f /etc/init.d/rcS ]; then
         /etc/init.d/rcS > /dev/null 2>&1
      fi
      #if [ -f /etc/init.d/rc ]; then
      #   /etc/init.d/rc S > /dev/null 2>&1
      #fi
      
      # start swap
      if [ -f /swap/.status ]; then
        if [ `cat /swap/.status | grep "on" | wc -l` -gt 0 ]; then
          swapon /swap/swapfile
        fi
      fi    
              
      /data/checkhdmount.sh > /tmp/checkhdmount.log &
      # restart gui 
      rmmod stmfb.ko
      rmmod stmcore-display-stx7100.ko
      rmmod cfbcopyarea.ko
      rmmod cfbfillrect.ko
      rmmod cfbimgblt.ko
      rmmod fb.ko
      if [ -f /config/start-ext.sh ]; then
        /config/start-ext.sh
      else  
        if [ -f /config/start.sh ]; then
           /config/start.sh
        else
           /app/start.sh
        fi
      fi  
      sync
      sleep 3
      
   fi
else
   # emulate init by trying to start runlevel that was passed to script
   # Funktionen fr Stickimage
           
      echo "$1"
      case $1 in
      10 | umount)
        echo "bainit: unmounting BA device ..."
        umount "$BAROOTM"
      ;;
      11 | mount)
        if [ `mount | grep /data/baroot | wc -l` -gt 0 ]; then
          echo "bainit: mounting BA deivce ..."
          mkdir "$BAROOTM"> /dev/null 2>&1
          mount $USB_DEVICE "$BAROOTM"
        else
          mount_ba_device
          #echo "you have booted from the Flash, nothing to do"
        fi  
        #if [ -f `mount $USB_DEVICE "$BAROOTM"` ]; then
        #  echo "OK --> mount as $BAROOTM"
        #else
        #  echo "NOTOK"
        #fi  
      ;;
      12 | ftp)
        if [ `mount | grep /data/baroot | wc -l` -gt 0 ]; then
          mkdir "$BAROOTM"> /dev/null 2>&1
          mount $USB_DEVICE "$BAROOTM"
        else 
          if [ `df | grep /tmp/usb/XBA | wc -l` -gt 0 ]; then
            BAROOTM=`df  | grep /tmp/usb/XBA | cut -d % -f 2- | cut -c 2-`
          else
            echo "BA Device not found"
            exit 1
          fi  
        fi
        echo "bainit: starting FTPD"
        "$BAROOTM/ba/tools/startproftpd.sh" 
      ;;
      13 | e2label)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        "$BAROOTM/ba/tools/e2label" $2 $3
        sync 
      ;;
      20 | swap)
        echo "erstelle Swapfile --> Bitte Warten"
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        dd if=/dev/zero of=/swap/swapfile bs=1024 count=16384
        "$BAROOTM/ba/tools/mkswap" /swap/swapfile
        sync 
        echo "Swapfile wurde erstellt"
        echo "swapfile aktivieren mit /config/bainit 21"
        echo "swapfile deaktivieren mit /config/bainit 22"
      ;;
      21 | swapon)
        if [ -f /swap/swapfile ]; then
          swapon /swap/swapfile
          echo "on" > /swap/.status
          sync 
          echo "Swapfile wurde gestartet"
        else
          echo "es mu erst mit /config/bainit 20 ein Swapfile erstellt werden"  
        fi
      ;;
      22 | swapoff)
        swapoff /swap/swapfile
        echo "off" > /swap/.status
        sync 
        echo "Swapfile wurde gestoppt"
      ;;
      91 | setdefault)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        echo "setze Bootparameter auf Standart"
        echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
        "$BAROOTM/ba/tools/fw_printenv" > "/tmp/boot1"
        if [ `cat /tmp/boot1 | grep "bootcmd=go a0fd0000" | wc -l` -gt 0 ]; then
          echo "use 2nd stage BL"
          echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
          "$BAROOTM/ba/tools/fw_printenv" > /tmp/boot1
        else
          if [ `cat /tmp/boot1 | grep "bootcmd=if itest" | wc -l` -gt 0 ]; then
            echo "use 2nd stage BL"
            echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
            "$BAROOTM/ba/tools/fw_printenv" > /tmp/boot1
          fi       
        fi 
        #if [ `cat /tmp/testt | grep "bootcmd=go" | wc -l` -gt 0 ]; then
          BOOTCMD="bootm a0040000"
          if [ `cat /tmp/boot1 | grep "bootcmd=run baboot" | wc -l` -lt 1 ]; then
            BOOTCMDORG=`cat /tmp/boot1 | grep "bootcmd=" | cut -c21-999`
            BOOTCMDORG=`echo -n "$BOOTCMDORG"`
            "$BAROOTM/ba/tools/fw_setenv" bootcmd $BOOTCMDORG
          fi
          #"$BAROOTM/ba/tools/fw_setenv" bootargs $BOOTARGS
          #"$BAROOTM/ba/tools/fw_setenv" bootcmd $BOOTCMD
          "$BAROOTM/ba/tools/fw_setenv" baflag
          "$BAROOTM/ba/tools/fw_setenv" baargs 
          "$BAROOTM/ba/tools/fw_setenv" baboot
          "$BAROOTM/ba/tools/fw_setenv" bacmd
        #fi  
        echo " "
        echo "Bootparameter wieder Standart"
        echo " "
      ;;
      92 | printenv)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
        "$BAROOTM/ba/tools/fw_printenv" > /tmp/testt
        echo "Parameter 1.Bootloader"
        echo "----------------------"
        cat /tmp/testt
        echo " "
        if [ `cat /tmp/testt | grep "bootcmd=go a0fd0000" | wc -l` -gt 0 ]; then
          echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
          "$BAROOTM/ba/tools/fw_printenv" > /tmp/testt
          echo "Parameter 2.Bootloader"
          echo "----------------------"
          cat /tmp/testt
          echo " "
        else
          if [ `cat /tmp/boot1 | grep "bootcmd=if itest" | wc -l` -gt 0 ]; then
            echo "use 2nd stage BL"
            echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
            "$BAROOTM/ba/tools/fw_printenv" > /tmp/testt
            echo "Parameter 2.Bootloader"
            echo "----------------------"
            cat /tmp/testt
            echo " "
          else
            if [ `cat /tmp/boot1 | grep "bootcmd=run ba2cmd" | wc -l` -gt 0 ]; then
              echo "use 2nd stage BL"
              echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
              "$BAROOTM/ba/tools/fw_printenv" > /tmp/testt
              echo "Parameter 2.Bootloader"
              echo "----------------------"
              cat /tmp/testt
              echo " "
            fi  
          fi     
        fi        
      ;;
      93 | 2blon)
        if [ `cat /proc/mtd | grep "mtd9" | wc -l` -gt 0 ]; then
          echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
          BA2FLAG=1
          BOOTCMD="run ba2cmd; bootm a0040000"
          BA2CMD="if itest.s \$ba2flag == 1; then set ba2flag 0; saveenv;if itest.l *a0fd0000 == -85525248; then go a0fd0000; fi; fi; bootm a0040000" 
          "$BAROOTM/ba/tools/fw_setenv" bootcmd $BOOTCMD
          "$BAROOTM/ba/tools/fw_setenv" ba2cmd $BA2CMD
          BA2FLAG="0"  
          "$BAROOTM/ba/tools/fw_setenv" ba2flag $BA2FLAG
          echo "2.BL wurde aktiviert"
        else
          echo "2.BL nicht installiert"
        fi  
      ;;
      94 | 2bloff)
        if [ `cat /proc/mtd | grep "mtd9" | wc -l` -gt 0 ]; then
          echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
          BOOTCMD="bootm a0040000"
          "$BAROOTM/ba/tools/fw_setenv" bootcmd $BOOTCMD
          "$BAROOTM/ba/tools/fw_setenv" ba2cmd 
          "$BAROOTM/ba/tools/fw_setenv" ba2flag
          echo "2.BL wurde deaktiviert"
        else
          echo "2.BL nicht installiert"
        fi  
      ;;
      99 | setenv)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        echo "installiere BA im Uboot"
        echo "/dev/mtd7 0x00000000 0x00010000 0x20000" > /tmp/fw_env.config
        "$BAROOTM/ba/tools/fw_printenv" > /tmp/boot1
        BABOOT="if itest.s \$baflag == 1; then set baflag 0; saveenv; run baargs; run bacmd; fi"  
        BOOTCMD="run baboot;"
        if [ `cat /tmp/boot1 | grep "bootcmd=go a0fd0000" | wc -l` -gt 0 ]; then
          echo "use 2nd stage BL"
          echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
          "$BAROOTM/ba/tools/fw_printenv" > /tmp/boot1
        else  
          if [ `cat /tmp/boot1 | grep "bootcmd=if itest" | wc -l` -gt 0 ]; then
              echo "use 2nd stage BL"
              echo "/dev/mtd8 0x00000000 0x00010000 0x10000" > /tmp/fw_env.config
              "$BAROOTM/ba/tools/fw_printenv" > /tmp/boot1
          fi   
        fi 
        if [ `cat /tmp/boot1 | grep "bootcmd=run baboot" | wc -l` -lt 1 ]; then
          echo "set Parm ---> bootcmd"
          BOOTCMDORG=`cat /tmp/boot1 | grep "bootcmd=" | cut -c9-999`
          BOOTCMD=`echo -n "$BOOTCMD $BOOTCMDORG"`
          "$BAROOTM/ba/tools/fw_setenv" bootcmd $BOOTCMD
        fi
        if [ `cat /tmp/boot1 | grep "baboot=$BABOOT" | wc -l` -lt 1 ]; then
          echo "set Parm ---> baboot"
          "$BAROOTM/ba/tools/fw_setenv" baboot $BABOOT
        fi  
                
        echo " "
        echo "BA wurde in Uboot installiert"
        echo " "
      ;;
      default)
        if [ -f /etc/init.d/rc ]; then
          exec /etc/init.d/rc $1
        else
         $ECHO " busybox init doesn't support run levels, sorry !"
        $CR
       fi
      ;;
      esac
      exit 0 
fi
exit 0
