#!/bin/sh

#
# *****************************************************************
# BE WARNED that following may have some security implications ... 
# *****************************************************************
#
# A sample start-up script which provides the hook for invoking extra start-up
# script outside the rootfs during start-up time. Two things need to be 
# arranged:
#
# 1. The extra start-up script needs to be accessible from some storage devices
#    (e.g. NAND flash, SATA device, USB storage device, MMC storage device).
# 2. Kernel command line has string "autostart" embedded.
#
# Note that NO authentication/verification of the external start-up scripts
# is performed here in the example. Further enhancements will be 
# required and is recommended.
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin

# Check to see if we need to do that (from kernel command line)?
AUTOSTART_SET=`dmesg | grep "Kernel command line" | grep autostart`
AUTOSTART_SCRIPT=autostart.sh
MOUNTPOINT=/autostart

if [ "$AUTOSTART_SET" = "" ]; then
	exit  0
else
	echo "Autostart enabled, looking for start-up script ..."
fi

# Load all kernel modules needed (NAND, SATA, USB, and MMC as well)
modprobe sata_tango3 > /dev/null 2>&1
modprobe sata_tangox > /dev/null 2>&1
modprobe sd_mod > /dev/null 2>&1
modprobe usb-storage > /dev/null 2>&1
modprobe sigmablock > /dev/null 2>&1
modprobe tangox-ehci-hcd > /dev/null 2>&1
modprobe tangox-ohci-hcd > /dev/null 2>&1
modprobe mmc_block > /dev/null 2>&1
modprobe sdhci > /dev/null 2>&1
modprobe sdhci-tangox > /dev/null 2>&1
 
# Wait for all device(s) to settle down ... 
sleep 5
 
# Figure out major# for NAND block device(s)
SIGMNUM=`cat /proc/partitions | grep sigmblock | wc -l`
SIGMMAJ=`cat /proc/devices | grep sigmblock | head -c 4`

# Creating device node(s) for NAND block device(s)
ITER=0
while [ "$ITER" != "$SIGMNUM" ];
do
	if [ ! -b /dev/nblock$ITER ]; then
		mknod /dev/nblock$ITER b $SIGMMAJ $ITER
	fi
	ITER=`expr $ITER + 1`
done

mkdir -p $MOUNTPOINT

# Try with multiple storage devices, change the list for more/less 
# device(s), or seek order. Once the device and start-up script are
# found, invoking the script in the background, and we are done.

# NAND, assume ext3 to be used
RFS="/dev/nblock7 /dev/nblock8"
for rfs in `echo $RFS`
do
	MOUNT_ERR=
 	mount -t ext3 -o noatime,data=journal $rfs $MOUNTPOINT > /dev/null 2>&1 || MOUNT_ERR="y"
	if [ "$MOUNT_ERR" = "" ]; then
		if [ -f $MOUNTPOINT/$AUTOSTART_SCRIPT ]; then
			echo "$AUTOSTART_SCRIPT found on $rfs ..."
			bash $MOUNTPOINT/$AUTOSTART_SCRIPT &
			exit 0
		fi
		umount -f $MOUNTPOINT
	fi
done

# Other devices (e.g. SATA, USB, MMC)
RFS1="/dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/mmcblk0p1 /dev/mmcblk1p1"
for rfs in `echo $RFS1`
do
	MOUNT_ERR=
 	mount $rfs $MOUNTPOINT > /dev/null 2>&1 || MOUNT_ERR="y"
	if [ "$MOUNT_ERR" = "" ]; then
		if [ -f $MOUNTPOINT/$AUTOSTART_SCRIPT ]; then
			echo "$AUTOSTART_SCRIPT found on $rfs ..."
			bash $MOUNTPOINT/$AUTOSTART_SCRIPT &
			exit 0
		fi
		umount -f $MOUNTPOINT
	fi
done

